(git:27c966b)
Loading...
Searching...
No Matches
hfx_ri_kp.F
Go to the documentation of this file.
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 RI-methods for HFX and K-points.
10!> \auhtor Augustin Bussy (01.2023)
11! **************************************************************************************************
12
14 USE admm_types, ONLY: get_admm_env
19 USE bibliography, ONLY: bussy2024,&
20 cite_reference
21 USE cell_types, ONLY: cell_type,&
22 pbc,&
31 USE cp_dbcsr_api, ONLY: &
36 dbcsr_p_type, dbcsr_put_block, dbcsr_release, dbcsr_type, dbcsr_type_no_symmetry, &
37 dbcsr_type_symmetric
44 USE dbt_api, ONLY: &
45 dbt_batched_contract_finalize, dbt_batched_contract_init, dbt_clear, dbt_contract, &
46 dbt_copy, dbt_copy_block, dbt_copy_indexed, dbt_copy_matrix_to_tensor, &
47 dbt_copy_tensor_to_matrix, dbt_create, dbt_destroy, dbt_distribution_destroy, &
48 dbt_distribution_new, dbt_distribution_type, dbt_filter, dbt_finalize, dbt_get_block, &
49 dbt_get_block_data, dbt_get_block_pattern, dbt_get_info, dbt_get_stored_coordinates, &
50 dbt_iterator_blocks_left, dbt_iterator_next_block, dbt_iterator_start, dbt_iterator_stop, &
51 dbt_iterator_type, dbt_mp_environ_pgrid, dbt_pgrid_create, dbt_pgrid_destroy, &
52 dbt_pgrid_type, dbt_put_block, dbt_put_block_data, dbt_reserve_blocks, dbt_scale, dbt_type
55 USE hfx_ri, ONLY: get_idx_to_atom,&
57 USE hfx_types, ONLY: hfx_ri_type
62 USE input_cp2k_hfx, ONLY: ri_pmat
69 USE kinds, ONLY: default_string_length,&
70 dp,&
71 int_8
72 USE kpoint_types, ONLY: get_kpoint_info,&
74 USE machine, ONLY: m_flush,&
75 m_memory,&
77 USE mathlib, ONLY: erfc_cutoff
79 USE message_passing, ONLY: mp_cart_type,&
85 USE physcon, ONLY: angstrom
100 USE qs_tensors, ONLY: &
113 USE virial_types, ONLY: virial_type
114#include "./base/base_uses.f90"
115
116!$ USE OMP_LIB, ONLY: omp_get_num_threads
117
118 IMPLICIT NONE
119 PRIVATE
120
122
123 CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'hfx_ri_kp'
124CONTAINS
125
126! **************************************************************************************************
127!> \brief I_1nitialize the ri_data for K-point. For now, we take the normal, usual existing ri_data
128!> and we adapt it to our needs
129!> \param dbcsr_template ...
130!> \param ri_data ...
131!> \param qs_env ...
132! **************************************************************************************************
133 SUBROUTINE adapt_ri_data_to_kp(dbcsr_template, ri_data, qs_env)
134 TYPE(dbcsr_type), INTENT(INOUT) :: dbcsr_template
135 TYPE(hfx_ri_type), INTENT(INOUT) :: ri_data
136 TYPE(qs_environment_type), POINTER :: qs_env
137
138 INTEGER :: i_img, i_RI, i_spin, iatom, natom, &
139 nblks_RI, nimg, nkind, nspins
140 INTEGER, ALLOCATABLE, DIMENSION(:) :: bsizes_RI_ext, dist1, dist2, dist3
141 TYPE(dft_control_type), POINTER :: dft_control
142 TYPE(mp_para_env_type), POINTER :: para_env
143
144 NULLIFY (dft_control, para_env)
145
146 !The main thing that we need to do is to allocate more space for the integrals, such that there
147 !is room for each periodic image. Note that we only go in 1D, i.e. we store (mu^0 sigma^a|P^0),
148 !and (P^0|Q^a) => the RI basis is always in the main cell.
149
150 !Get kpoint info
151 CALL get_qs_env(qs_env, dft_control=dft_control, natom=natom, para_env=para_env, nkind=nkind)
152 nimg = ri_data%nimg
153 ALLOCATE (ri_data%kp_RI_neighbors(natom*ri_data%ncell_RI, natom))
154 ri_data%kp_RI_neighbors = .false.
155
156 !Along the RI direction we have basis elements spread accross ncell_RI images.
157 nblks_ri = SIZE(ri_data%bsizes_RI_split)
158 ALLOCATE (bsizes_ri_ext(nblks_ri*ri_data%ncell_RI))
159 DO i_ri = 1, ri_data%ncell_RI
160 bsizes_ri_ext((i_ri - 1)*nblks_ri + 1:i_ri*nblks_ri) = ri_data%bsizes_RI_split(:)
161 END DO
162
163 ALLOCATE (ri_data%t_3c_int_ctr_1(1, nimg))
164 CALL create_3c_tensor(ri_data%t_3c_int_ctr_1(1, 1), dist1, dist2, dist3, &
165 ri_data%pgrid_1, ri_data%bsizes_AO_split, bsizes_ri_ext, &
166 ri_data%bsizes_AO_split, [1, 2], [3], name="(AO RI | AO)")
167
168 DO i_img = 2, nimg
169 CALL dbt_create(ri_data%t_3c_int_ctr_1(1, 1), ri_data%t_3c_int_ctr_1(1, i_img))
170 END DO
171 DEALLOCATE (dist1, dist2, dist3)
172
173 ALLOCATE (ri_data%t_3c_int_ctr_2(1, 1))
174 CALL create_3c_tensor(ri_data%t_3c_int_ctr_2(1, 1), dist1, dist2, dist3, &
175 ri_data%pgrid_1, ri_data%bsizes_AO_split, bsizes_ri_ext, &
176 ri_data%bsizes_AO_split, [1], [2, 3], name="(AO RI | AO)")
177 DEALLOCATE (dist1, dist2, dist3)
178
179 !We use full block sizes for the 2c quantities
180 DEALLOCATE (bsizes_ri_ext)
181 nblks_ri = SIZE(ri_data%bsizes_RI)
182 ALLOCATE (bsizes_ri_ext(nblks_ri*ri_data%ncell_RI))
183 DO i_ri = 1, ri_data%ncell_RI
184 bsizes_ri_ext((i_ri - 1)*nblks_ri + 1:i_ri*nblks_ri) = ri_data%bsizes_RI(:)
185 END DO
186
187 ALLOCATE (ri_data%t_2c_inv(1, natom), ri_data%t_2c_int(1, natom), ri_data%t_2c_pot(1, natom))
188 CALL create_2c_tensor(ri_data%t_2c_inv(1, 1), dist1, dist2, ri_data%pgrid_2d, &
189 bsizes_ri_ext, bsizes_ri_ext, &
190 name="(RI | RI)")
191 DEALLOCATE (dist1, dist2)
192 CALL dbt_create(ri_data%t_2c_inv(1, 1), ri_data%t_2c_int(1, 1))
193 CALL dbt_create(ri_data%t_2c_inv(1, 1), ri_data%t_2c_pot(1, 1))
194 DO iatom = 2, natom
195 CALL dbt_create(ri_data%t_2c_inv(1, 1), ri_data%t_2c_inv(1, iatom))
196 CALL dbt_create(ri_data%t_2c_inv(1, 1), ri_data%t_2c_int(1, iatom))
197 CALL dbt_create(ri_data%t_2c_inv(1, 1), ri_data%t_2c_pot(1, iatom))
198 END DO
199
200 ALLOCATE (ri_data%kp_cost(natom, natom, nimg))
201 ri_data%kp_cost = 0.0_dp
202
203 !We store the density and KS matrix in tensor format
204 nspins = dft_control%nspins
205 ALLOCATE (ri_data%rho_ao_t(nspins, nimg), ri_data%ks_t(nspins, nimg))
206 CALL create_2c_tensor(ri_data%rho_ao_t(1, 1), dist1, dist2, ri_data%pgrid_2d, &
207 ri_data%bsizes_AO_split, ri_data%bsizes_AO_split, &
208 name="(AO | AO)")
209 DEALLOCATE (dist1, dist2)
210
211 CALL dbt_create(dbcsr_template, ri_data%ks_t(1, 1))
212
213 IF (nspins == 2) THEN
214 CALL dbt_create(ri_data%rho_ao_t(1, 1), ri_data%rho_ao_t(2, 1))
215 CALL dbt_create(ri_data%ks_t(1, 1), ri_data%ks_t(2, 1))
216 END IF
217 DO i_img = 2, nimg
218 DO i_spin = 1, nspins
219 CALL dbt_create(ri_data%rho_ao_t(1, 1), ri_data%rho_ao_t(i_spin, i_img))
220 CALL dbt_create(ri_data%ks_t(1, 1), ri_data%ks_t(i_spin, i_img))
221 END DO
222 END DO
223
224 END SUBROUTINE adapt_ri_data_to_kp
225
226! **************************************************************************************************
227!> \brief The pre-scf steps for RI-HFX k-points calculation. Namely the calculation of the integrals
228!> \param dbcsr_template ...
229!> \param ri_data ...
230!> \param qs_env ...
231! **************************************************************************************************
232 SUBROUTINE hfx_ri_pre_scf_kp(dbcsr_template, ri_data, qs_env)
233 TYPE(dbcsr_type), INTENT(INOUT) :: dbcsr_template
234 TYPE(hfx_ri_type), INTENT(INOUT) :: ri_data
235 TYPE(qs_environment_type), POINTER :: qs_env
236
237 CHARACTER(LEN=*), PARAMETER :: routineN = 'hfx_ri_pre_scf_kp'
238
239 INTEGER :: handle, i_img, iatom, natom, nimg, nkind
240 TYPE(dbcsr_type), ALLOCATABLE, DIMENSION(:) :: t_2c_op_pot, t_2c_op_RI
241 TYPE(dbt_type), ALLOCATABLE, DIMENSION(:, :) :: t_3c_int
242 TYPE(dft_control_type), POINTER :: dft_control
243
244 NULLIFY (dft_control)
245
246 CALL timeset(routinen, handle)
247
248 CALL get_qs_env(qs_env, dft_control=dft_control, natom=natom, nkind=nkind)
249
250 CALL cleanup_kp(ri_data)
251
252 !We do all the checks on what we allow in this initial implementation
253 IF (ri_data%flavor /= ri_pmat) cpabort("K-points RI-HFX only with RHO flavor")
254 IF (ri_data%same_op) ri_data%same_op = .false. !force the full calculation with RI metric
255 IF (abs(ri_data%eps_pgf_orb - dft_control%qs_control%eps_pgf_orb) > 1.0e-16_dp) THEN
256 cpabort("RI%EPS_PGF_ORB and QS%EPS_PGF_ORB must be identical for RI-HFX k-points")
257 END IF
258
259 CALL get_kp_and_ri_images(ri_data, qs_env)
260 nimg = ri_data%nimg
261
262 !Calculate the integrals
263 ALLOCATE (t_2c_op_pot(nimg), t_2c_op_ri(nimg))
264 ALLOCATE (t_3c_int(1, nimg))
265 CALL hfx_ri_pre_scf_calc_tensors(qs_env, ri_data, t_2c_op_ri, t_2c_op_pot, t_3c_int, do_kpoints=.true.)
266
267 !Make sure the internals have the k-point format
268 CALL adapt_ri_data_to_kp(dbcsr_template, ri_data, qs_env)
269
270 !For each atom i, we calculate the inverse RI metric (P^0 | Q^0)^-1 without external bumping yet
271 !Also store the off-diagonal integrals of the RI metric in case of forces, bumped from the left
272 DO iatom = 1, natom
273 CALL get_ext_2c_int(ri_data%t_2c_inv(1, iatom), t_2c_op_ri, iatom, iatom, 1, ri_data, qs_env, &
274 do_inverse=.true.)
275 !for the forces:
276 !off-diagonl RI metric bumped from the left
277 CALL get_ext_2c_int(ri_data%t_2c_int(1, iatom), t_2c_op_ri, iatom, iatom, 1, ri_data, &
278 qs_env, off_diagonal=.true.)
279 CALL apply_bump(ri_data%t_2c_int(1, iatom), iatom, ri_data, qs_env, from_left=.true., from_right=.false.)
280
281 !RI metric with bumped off-diagonal blocks (but not inverted), depumed from left and right
282 CALL get_ext_2c_int(ri_data%t_2c_pot(1, iatom), t_2c_op_ri, iatom, iatom, 1, ri_data, qs_env, &
283 do_inverse=.true., skip_inverse=.true.)
284 CALL apply_bump(ri_data%t_2c_pot(1, iatom), iatom, ri_data, qs_env, from_left=.true., &
285 from_right=.true., debump=.true.)
286
287 END DO
288
289 DO i_img = 1, nimg
290 CALL dbcsr_release(t_2c_op_ri(i_img))
291 END DO
292
293 ALLOCATE (ri_data%kp_mat_2c_pot(1, nimg))
294 DO i_img = 1, nimg
295 CALL dbcsr_create(ri_data%kp_mat_2c_pot(1, i_img), template=t_2c_op_pot(i_img))
296 CALL dbcsr_copy(ri_data%kp_mat_2c_pot(1, i_img), t_2c_op_pot(i_img))
297 CALL dbcsr_release(t_2c_op_pot(i_img))
298 END DO
299
300 !reorder the 3c integrals such that empty images are bunched up together
301 CALL reorder_3c_ints(t_3c_int(1, :), ri_data)
302
303 !Pre-contract all 3c integrals with the bumped inverse RI metric (P^0|Q^0)^-1,
304 !and store in ri_data%t_3c_int_ctr_1
305 CALL precontract_3c_ints(t_3c_int, ri_data, qs_env)
306
307 CALL timestop(handle)
308
309 END SUBROUTINE hfx_ri_pre_scf_kp
310
311! **************************************************************************************************
312!> \brief clean-up the KP specific data from ri_data
313!> \param ri_data ...
314! **************************************************************************************************
315 SUBROUTINE cleanup_kp(ri_data)
316 TYPE(hfx_ri_type), INTENT(INOUT) :: ri_data
317
318 INTEGER :: i, j
319
320 IF (ALLOCATED(ri_data%kp_cost)) DEALLOCATE (ri_data%kp_cost)
321 IF (ALLOCATED(ri_data%idx_to_img)) DEALLOCATE (ri_data%idx_to_img)
322 IF (ALLOCATED(ri_data%img_to_idx)) DEALLOCATE (ri_data%img_to_idx)
323 IF (ALLOCATED(ri_data%present_images)) DEALLOCATE (ri_data%present_images)
324 IF (ALLOCATED(ri_data%img_to_RI_cell)) DEALLOCATE (ri_data%img_to_RI_cell)
325 IF (ALLOCATED(ri_data%RI_cell_to_img)) DEALLOCATE (ri_data%RI_cell_to_img)
326
327 IF (ALLOCATED(ri_data%kp_mat_2c_pot)) THEN
328 DO j = 1, SIZE(ri_data%kp_mat_2c_pot, 2)
329 DO i = 1, SIZE(ri_data%kp_mat_2c_pot, 1)
330 CALL dbcsr_release(ri_data%kp_mat_2c_pot(i, j))
331 END DO
332 END DO
333 DEALLOCATE (ri_data%kp_mat_2c_pot)
334 END IF
335
336 IF (ALLOCATED(ri_data%kp_t_3c_G_sub)) THEN
337 DO i = 1, SIZE(ri_data%kp_t_3c_G_sub)
338 CALL dbt_destroy(ri_data%kp_t_3c_G_sub(i))
339 END DO
340 DEALLOCATE (ri_data%kp_t_3c_G_sub)
341 END IF
342 IF (ALLOCATED(ri_data%kp_G_to_RI)) DEALLOCATE (ri_data%kp_G_to_RI)
343 IF (ALLOCATED(ri_data%kp_RI_atom)) DEALLOCATE (ri_data%kp_RI_atom)
344 IF (ALLOCATED(ri_data%kp_RI_neighbors)) DEALLOCATE (ri_data%kp_RI_neighbors)
345 ri_data%kp_G_ngroups = 0
346
347 IF (ALLOCATED(ri_data%kp_t_3c_int)) THEN
348 DO i = 1, SIZE(ri_data%kp_t_3c_int)
349 CALL dbt_destroy(ri_data%kp_t_3c_int(i))
350 END DO
351 DEALLOCATE (ri_data%kp_t_3c_int)
352 END IF
353
354 IF (ALLOCATED(ri_data%t_2c_inv)) THEN
355 DO j = 1, SIZE(ri_data%t_2c_inv, 2)
356 DO i = 1, SIZE(ri_data%t_2c_inv, 1)
357 CALL dbt_destroy(ri_data%t_2c_inv(i, j))
358 END DO
359 END DO
360 DEALLOCATE (ri_data%t_2c_inv)
361 END IF
362
363 IF (ALLOCATED(ri_data%t_2c_int)) THEN
364 DO j = 1, SIZE(ri_data%t_2c_int, 2)
365 DO i = 1, SIZE(ri_data%t_2c_int, 1)
366 CALL dbt_destroy(ri_data%t_2c_int(i, j))
367 END DO
368 END DO
369 DEALLOCATE (ri_data%t_2c_int)
370 END IF
371
372 IF (ALLOCATED(ri_data%t_2c_pot)) THEN
373 DO j = 1, SIZE(ri_data%t_2c_pot, 2)
374 DO i = 1, SIZE(ri_data%t_2c_pot, 1)
375 CALL dbt_destroy(ri_data%t_2c_pot(i, j))
376 END DO
377 END DO
378 DEALLOCATE (ri_data%t_2c_pot)
379 END IF
380
381 IF (ALLOCATED(ri_data%t_3c_int_ctr_1)) THEN
382 DO j = 1, SIZE(ri_data%t_3c_int_ctr_1, 2)
383 DO i = 1, SIZE(ri_data%t_3c_int_ctr_1, 1)
384 CALL dbt_destroy(ri_data%t_3c_int_ctr_1(i, j))
385 END DO
386 END DO
387 DEALLOCATE (ri_data%t_3c_int_ctr_1)
388 END IF
389
390 IF (ALLOCATED(ri_data%t_3c_int_ctr_2)) THEN
391 DO j = 1, SIZE(ri_data%t_3c_int_ctr_2, 2)
392 DO i = 1, SIZE(ri_data%t_3c_int_ctr_2, 1)
393 CALL dbt_destroy(ri_data%t_3c_int_ctr_2(i, j))
394 END DO
395 END DO
396 DEALLOCATE (ri_data%t_3c_int_ctr_2)
397 END IF
398
399 IF (ALLOCATED(ri_data%rho_ao_t)) THEN
400 DO j = 1, SIZE(ri_data%rho_ao_t, 2)
401 DO i = 1, SIZE(ri_data%rho_ao_t, 1)
402 CALL dbt_destroy(ri_data%rho_ao_t(i, j))
403 END DO
404 END DO
405 DEALLOCATE (ri_data%rho_ao_t)
406 END IF
407
408 IF (ALLOCATED(ri_data%ks_t)) THEN
409 DO j = 1, SIZE(ri_data%ks_t, 2)
410 DO i = 1, SIZE(ri_data%ks_t, 1)
411 CALL dbt_destroy(ri_data%ks_t(i, j))
412 END DO
413 END DO
414 DEALLOCATE (ri_data%ks_t)
415 END IF
416
417 END SUBROUTINE cleanup_kp
418
419! **************************************************************************************************
420!> \brief Prints a progress bar for the k-point RI-HFX triple loop
421!> \param b_img ...
422!> \param nimg ...
423!> \param iprint ...
424!> \param ri_data ...
425! **************************************************************************************************
426 SUBROUTINE print_progress_bar(b_img, nimg, iprint, ri_data)
427 INTEGER, INTENT(IN) :: b_img, nimg
428 INTEGER, INTENT(INOUT) :: iprint
429 TYPE(hfx_ri_type), INTENT(INOUT) :: ri_data
430
431 CHARACTER(LEN=default_string_length) :: bar
432 INTEGER :: rep
433
434 IF (ri_data%unit_nr > 0) THEN
435 IF (b_img == 1) THEN
436 WRITE (ri_data%unit_nr, '(/T6,A)', advance="no") '[-'
437 CALL m_flush(ri_data%unit_nr)
438 END IF
439 IF (b_img > iprint*nimg/71) THEN
440 rep = max(1, 71/nimg)
441 bar = repeat("-", rep)
442 WRITE (ri_data%unit_nr, '(A)', advance="no") trim(bar)
443 CALL m_flush(ri_data%unit_nr)
444 iprint = iprint + 1
445 END IF
446 IF (b_img == nimg) THEN
447 rep = max(0, 1 + 71 - iprint*rep)
448 bar = repeat("-", rep)
449 WRITE (ri_data%unit_nr, '(A,A)') trim(bar), '-]'
450 CALL m_flush(ri_data%unit_nr)
451 END IF
452 END IF
453
454 END SUBROUTINE print_progress_bar
455
456! **************************************************************************************************
457!> \brief Update the KS matrices for each real-space image
458!> \param qs_env ...
459!> \param ri_data ...
460!> \param ks_matrix ...
461!> \param ehfx ...
462!> \param rho_ao ...
463!> \param geometry_did_change ...
464!> \param nspins ...
465!> \param hf_fraction ...
466! **************************************************************************************************
467 SUBROUTINE hfx_ri_update_ks_kp(qs_env, ri_data, ks_matrix, ehfx, rho_ao, &
468 geometry_did_change, nspins, hf_fraction)
469
470 TYPE(qs_environment_type), POINTER :: qs_env
471 TYPE(hfx_ri_type), INTENT(INOUT) :: ri_data
472 TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: ks_matrix
473 REAL(kind=dp), INTENT(OUT) :: ehfx
474 TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: rho_ao
475 LOGICAL, INTENT(IN) :: geometry_did_change
476 INTEGER, INTENT(IN) :: nspins
477 REAL(kind=dp), INTENT(IN) :: hf_fraction
478
479 CHARACTER(LEN=*), PARAMETER :: routinen = 'hfx_ri_update_ks_kp'
480
481 INTEGER :: b_img, batch_size, group_size, handle, handle2, i_batch, i_img, i_spin, iatom, &
482 iblk, igroup, iprint, jatom, mb_img, n_batch_nze, n_nze, natom, ngroups, nimg, nimg_nze
483 INTEGER(int_8) :: mem, nflop, nflop_sub, nze
484 INTEGER, ALLOCATABLE, DIMENSION(:) :: batch_ranges_at, batch_ranges_nze, &
485 idx_to_at_ao
486 INTEGER, ALLOCATABLE, DIMENSION(:, :) :: iapc_pairs
487 INTEGER, ALLOCATABLE, DIMENSION(:, :, :) :: sparsity_pattern
488 LOGICAL :: estimate_mem, g_ready, print_progress, &
489 use_delta_p
490 REAL(dp) :: etmp, fac, occ, pfac, pref, t1, t2, t3, &
491 t4
492 TYPE(dbcsr_type) :: ks_desymm, rho_desymm, tmp
493 TYPE(dbt_type), ALLOCATABLE, DIMENSION(:) :: ks_t_split, t_2c_ao_tmp, t_3c_work_2
494 TYPE(dbt_type), ALLOCATABLE, DIMENSION(:, :) :: ks_t, ks_t_sub, t_3c_apc, t_3c_apc_sub
495 TYPE(mp_para_env_type), POINTER :: para_env, para_env_sub
496 TYPE(section_vals_type), POINTER :: hfx_section, print_section
497
498 NULLIFY (para_env, para_env_sub, hfx_section, print_section)
499
500 CALL cite_reference(bussy2024)
501
502 CALL timeset(routinen, handle)
503
504 CALL get_qs_env(qs_env, para_env=para_env, natom=natom)
505
506 IF (nspins == 1) THEN
507 fac = 0.5_dp*hf_fraction
508 ELSE
509 fac = 1.0_dp*hf_fraction
510 END IF
511
512 hfx_section => section_vals_get_subs_vals(qs_env%input, "DFT%XC%HF%RI")
513 CALL section_vals_val_get(hfx_section, "KP_NGROUPS", i_val=ngroups)
514 CALL section_vals_val_get(hfx_section, "KP_STACK_SIZE", i_val=batch_size)
515 CALL section_vals_val_get(hfx_section, "KP_USE_DELTA_P", l_val=use_delta_p)
516 ri_data%kp_stack_size = batch_size
517 ri_data%kp_ngroups = ngroups
518
519 IF (geometry_did_change) THEN
520 CALL hfx_ri_pre_scf_kp(ks_matrix(1, 1)%matrix, ri_data, qs_env)
521 END IF
522 nimg = ri_data%nimg
523 nimg_nze = ri_data%nimg_nze
524
525 !We need to calculate the KS matrix for each periodic cell with index b: F_mu^0,nu^b
526 !F_mu^0,nu^b = -0.5 sum_a,c P_sigma^0,lambda^c (mu^0, sigma^a| P^0) V_P^0,Q^b (Q^b| nu^b lambda^a+c)
527 !with V_P^0,Q^b = (P^0|R^0)^-1 * (R^0|S^b) * (S^b|Q^b)^-1
528
529 !We use a local RI basis set for each atom in the system, which inlcudes RI basis elements for
530 !each neighboring atom standing within the KIND radius (decay of Gaussian with smallest exponent)
531
532 !We also limit the number of periodic images we consider accorrding to the HFX potentail in the
533 !RI basis, because if V_P^0,Q^b is zero everywhere, then image b can be ignored (RI basis less diffuse)
534
535 !We manage to calculate each KS matrix doing a double loop on iamges, and a double loop on atoms
536 !First, we pre-contract and store P_sigma^0,lambda^c (mu^0, sigma^a| P^0) (P^0|R^0)^-1 into T_mu^0,lambda^a+c,P^0
537 !The remaining potential action on the right fitted pairs is geometry-dependent.
538 !Store its unique G_j(P^x;nu^0,lambda^l), with x=p-b and l=a+c-b,
539 !and select the required rows for each (iatom,jatom,b_img) before contracting with T.
540
541 !Note that the 3-center integrals are pre-contracted with the RI metric, and that the same tensor can be used
542 !(mu^0, sigma^a| P^0) (P^0|R^0) <===> (S^b|Q^b)^-1 (Q^b| nu^b lambda^a+c) by relabelling the images
543
544 IF (mod(para_env%num_pe, ngroups) /= 0) THEN
545 cpwarn("KP_NGROUPS must be an integer divisor of the total number of MPI ranks. It was set to 1.")
546 ngroups = 1
547 CALL section_vals_val_set(hfx_section, "KP_NGROUPS", i_val=ngroups)
548 END IF
549 IF ((mod(ngroups, natom) /= 0) .AND. (mod(natom, ngroups) /= 0) .AND. geometry_did_change) THEN
550 IF (ngroups > 1) THEN
551 cpwarn("Better load balancing is reached if NGROUPS is a multiple/divisor of the number of atoms")
552 END IF
553 END IF
554 group_size = para_env%num_pe/ngroups
555 igroup = para_env%mepos/group_size
556
557 ALLOCATE (para_env_sub)
558 CALL para_env_sub%from_split(para_env, igroup)
559
560 ! The sparsity pattern of each iatom, jatom pair, on each b_img, and on which subgroup
561 ALLOCATE (sparsity_pattern(natom, natom, nimg))
562 CALL get_sparsity_pattern(sparsity_pattern, ri_data, qs_env)
563 CALL get_sub_dist(sparsity_pattern, ngroups, ri_data)
564
565 ! Build geometry-only G before allocating the density-dependent T workspace.
566 IF (.NOT. ALLOCATED(ri_data%kp_t_3c_G_sub)) THEN
567 CALL build_kp_exchange_integrals(ri_data, qs_env, sparsity_pattern, ngroups, para_env_sub)
568 END IF
569 cpassert(ri_data%kp_G_ngroups == ngroups)
570
571 !By default, build the density tensor based on the difference of this SCF P and that of the prev. SCF
572 pfac = -1.0_dp
573 IF (.NOT. use_delta_p) pfac = 0.0_dp
574 CALL get_pmat_images(ri_data%rho_ao_t, rho_ao, pfac, ri_data, qs_env)
575
576 n_nze = 0
577 DO i_img = 1, nimg
578 DO i_spin = 1, nspins
579 CALL get_tensor_occupancy(ri_data%rho_ao_t(i_spin, i_img), nze, occ)
580 IF (nze > 0) THEN
581 n_nze = n_nze + 1
582 END IF
583 END DO
584 END DO
585 IF (n_nze == nspins) THEN
586 cpwarn("It is highly recommended to restart from a converged GGA K-point calculations.")
587 END IF
588
589 ALLOCATE (ks_t(nspins, nimg))
590 DO i_img = 1, nimg
591 DO i_spin = 1, nspins
592 CALL dbt_create(ri_data%ks_t(1, 1), ks_t(i_spin, i_img))
593 END DO
594 END DO
595
596 ALLOCATE (idx_to_at_ao(SIZE(ri_data%bsizes_AO_split)))
597 CALL get_idx_to_atom(idx_to_at_ao, ri_data%bsizes_AO_split, ri_data%bsizes_AO)
598
599 !First we calculate and store T^1_mu^0,lambda^a+c,P = P_mu^0,lambda^c * (mu_0 sigma^a | P^0) (P^0|R^0)^-1
600 !To avoid doing nimg**2 tiny contractions that do not scale well with a large number of CPUs,
601 !we instead do a single loop over the a+c image index. For each a+c, we get a list of allowed
602 !combination of a,c indices. Then we build TAS tensors P_mu^0,lambda^c with all concerned c's
603 !and (mu^0 sigma^a | P^0)*(P^0|R^0)^-1 with all a's. Then we perform a single contraction with larger tensors,
604 !were the sum over a,c is automatically taken care of
605 ALLOCATE (t_3c_apc(nspins, nimg))
606 DO i_img = 1, nimg
607 DO i_spin = 1, nspins
608 CALL dbt_create(ri_data%t_3c_int_ctr_2(1, 1), t_3c_apc(i_spin, i_img))
609 END DO
610 END DO
611 CALL contract_pmat_3c(t_3c_apc, ri_data%rho_ao_t, ri_data, qs_env)
612
613 !Get all the required tensors in the subgroups
614 ALLOCATE (ks_t_sub(nspins, nimg), t_2c_ao_tmp(1), ks_t_split(2))
615 CALL get_subgroup_ks_tensors(t_2c_ao_tmp, ks_t_split, ks_t_sub, para_env_sub, ri_data)
616
617 ALLOCATE (t_3c_apc_sub(nspins, nimg), t_3c_work_2(3))
618 CALL get_subgroup_3c_tensors(t_3c_work_2, t_3c_apc, t_3c_apc_sub, &
619 para_env_sub, ri_data)
620
621 !We go atom by atom, therefore there is an automatic batching along that direction
622 !Also, because we stack the 3c tensors nimg times, we naturally do some batching there too
623 ALLOCATE (batch_ranges_at(natom + 1))
624 batch_ranges_at(natom + 1) = SIZE(ri_data%bsizes_AO_split) + 1
625 iatom = 0
626 DO iblk = 1, SIZE(ri_data%bsizes_AO_split)
627 IF (idx_to_at_ao(iblk) == iatom + 1) THEN
628 iatom = iatom + 1
629 batch_ranges_at(iatom) = iblk
630 END IF
631 END DO
632
633 n_batch_nze = nimg_nze/batch_size
634 IF (modulo(nimg_nze, batch_size) /= 0) n_batch_nze = n_batch_nze + 1
635 ALLOCATE (batch_ranges_nze(n_batch_nze + 1))
636 DO i_batch = 1, n_batch_nze
637 batch_ranges_nze(i_batch) = (i_batch - 1)*batch_size + 1
638 END DO
639 batch_ranges_nze(n_batch_nze + 1) = nimg_nze + 1
640
641 print_section => section_vals_get_subs_vals(qs_env%input, "DFT%XC%HF%RI%PRINT")
642 CALL section_vals_val_get(print_section, "KP_RI_PROGRESS_BAR", l_val=print_progress)
643 CALL section_vals_val_get(print_section, "KP_RI_MEMORY_ESTIMATE", l_val=estimate_mem)
644
645 ALLOCATE (iapc_pairs(nimg, 2))
646 IF (estimate_mem .AND. geometry_did_change) THEN
647 !Populate work tensors to simulate maximum usage
648 CALL get_iapc_pairs(iapc_pairs, 1, ri_data, qs_env)
649 CALL fill_kp_exchange_stack(t_3c_work_2(2), ri_data, iapc_pairs, 1, 1, 1, &
650 idx_to_at_ao, [batch_ranges_nze(1), batch_ranges_nze(2)])
651 CALL fill_3c_stack(t_3c_work_2(1), t_3c_apc_sub(1, :), iapc_pairs(:, 2), 3, &
652 ri_data, filter_at=1, filter_dim=1, idx_to_at=idx_to_at_ao, &
653 img_bounds=[batch_ranges_nze(1), batch_ranges_nze(2)])
654 CALL fill_3c_stack(t_3c_work_2(3), t_3c_apc_sub(1, :), iapc_pairs(:, 2), 3, &
655 ri_data, filter_at=1, filter_dim=1, idx_to_at=idx_to_at_ao, &
656 img_bounds=[batch_ranges_nze(1), batch_ranges_nze(2)])
657 CALL m_memory(mem)
658 CALL para_env%max(mem)
659 CALL dbt_clear(t_3c_work_2(1))
660 CALL dbt_clear(t_3c_work_2(2))
661 CALL dbt_clear(t_3c_work_2(3))
662
663 IF (ri_data%unit_nr > 0) THEN
664 WRITE (ri_data%unit_nr, fmt="(T3,A,I14)") &
665 "KP-HFX_RI_INFO| Estimated peak memory usage per MPI rank (MiB):", mem/(1024*1024)
666 CALL m_flush(ri_data%unit_nr)
667 END IF
668 END IF
669
670 CALL dbt_batched_contract_init(t_3c_work_2(1), batch_range_1=batch_ranges_at)
671 CALL dbt_batched_contract_init(t_3c_work_2(2), batch_range_1=batch_ranges_at)
672
673 iprint = 1
674 t1 = m_walltime()
675 nflop_sub = 0_int_8
676 ri_data%kp_cost(:, :, :) = 0.0_dp
677 DO b_img = 1, nimg
678 IF (print_progress) CALL print_progress_bar(b_img, nimg, iprint, ri_data)
679 CALL get_iapc_pairs(iapc_pairs, b_img, ri_data, qs_env)
680 CALL dbt_batched_contract_init(ks_t_split(1))
681 CALL dbt_batched_contract_init(ks_t_split(2))
682 DO jatom = 1, natom
683 DO iatom = 1, natom
684 IF (.NOT. sparsity_pattern(iatom, jatom, b_img) == igroup) cycle
685 pref = 1.0_dp
686 IF (iatom == jatom .AND. b_img == 1) pref = 0.5_dp
687
688 !measure the cost of the given i, j, b configuration
689 t3 = m_walltime()
690
691 CALL timeset(routinen//"_3c", handle2)
692
693 ! G_j(P^x; nu^0, lambda^l) is independent of the density.
694 ! Here x=p-b and l=a+c-b; retain the original finite image domain.
695 DO i_batch = 1, n_batch_nze
696 g_ready = .false.
697
698 !Stack the P_sigma^a,lambda^a+c * (mu^0 sigma^a | P^0)*(P^0|R^0)^-1 integrals over a+c and contract
699 !to get the final block of the KS matrix
700 DO i_spin = 1, nspins
701 CALL fill_3c_stack(t_3c_work_2(3), t_3c_apc_sub(i_spin, :), iapc_pairs(:, 2), 3, &
702 ri_data, filter_at=iatom, filter_dim=1, idx_to_at=idx_to_at_ao, &
703 img_bounds=[batch_ranges_nze(i_batch), batch_ranges_nze(i_batch + 1)])
704 CALL get_tensor_occupancy(t_3c_work_2(3), nze, occ)
705
706 IF (nze == 0) cycle
707 IF (.NOT. g_ready) THEN
708 CALL fill_kp_exchange_stack(t_3c_work_2(2), ri_data, iapc_pairs, iatom, jatom, b_img, &
709 idx_to_at_ao, &
710 [batch_ranges_nze(i_batch), batch_ranges_nze(i_batch + 1)])
711 g_ready = .true.
712 END IF
713 CALL dbt_copy(t_3c_work_2(3), t_3c_work_2(1), move_data=.true.)
714 CALL dbt_contract(-pref*fac, t_3c_work_2(1), t_3c_work_2(2), &
715 1.0_dp, ks_t_split(i_spin), map_1=[1], map_2=[2], &
716 contract_1=[2, 3], notcontract_1=[1], &
717 contract_2=[2, 3], notcontract_2=[1], &
718 filter_eps=ri_data%filter_eps, &
719 move_data=i_spin == nspins, flop=nflop)
720 nflop_sub = nflop_sub + nflop
721 END DO
722 END DO !i_batch
723 CALL timestop(handle2)
724
725 t4 = m_walltime()
726 ri_data%kp_cost(iatom, jatom, b_img) = t4 - t3
727 END DO !iatom
728 END DO !jatom
729 CALL dbt_batched_contract_finalize(ks_t_split(1))
730 CALL dbt_batched_contract_finalize(ks_t_split(2))
731
732 DO i_spin = 1, nspins
733 CALL dbt_copy(ks_t_split(i_spin), t_2c_ao_tmp(1), move_data=.true.)
734 CALL dbt_copy(t_2c_ao_tmp(1), ks_t_sub(i_spin, b_img), summation=.true.)
735 END DO
736 END DO !b_img
737 CALL dbt_batched_contract_finalize(t_3c_work_2(1))
738 CALL dbt_batched_contract_finalize(t_3c_work_2(2))
739 CALL para_env%sync()
740 ! DBT returns the average flop count per rank in each subgroup. Reduce only
741 ! this step's subgroup work and keep the accumulated counter per rank.
742 CALL para_env%sum(nflop_sub)
743 ri_data%dbcsr_nflop = ri_data%dbcsr_nflop + (nflop_sub + para_env%num_pe - 1)/para_env%num_pe
744 CALL para_env%sum(ri_data%kp_cost)
745 t2 = m_walltime()
746 ri_data%dbcsr_time = ri_data%dbcsr_time + t2 - t1
747
748 !transfer KS tensor from subgroup to main group
749 CALL gather_ks_matrix(ks_t, ks_t_sub, group_size, sparsity_pattern, para_env, ri_data)
750
751 !clean-up subgroup tensors
752 CALL dbt_destroy(t_2c_ao_tmp(1))
753 CALL dbt_destroy(ks_t_split(1))
754 CALL dbt_destroy(ks_t_split(2))
755 CALL dbt_destroy(t_3c_work_2(1))
756 CALL dbt_destroy(t_3c_work_2(2))
757 CALL dbt_destroy(t_3c_work_2(3))
758 DO i_img = 1, nimg
759 DO i_spin = 1, nspins
760 CALL dbt_destroy(t_3c_apc_sub(i_spin, i_img))
761 CALL dbt_destroy(ks_t_sub(i_spin, i_img))
762 END DO
763 END DO
764 !End of subgroup parallelization
765 CALL para_env_sub%free()
766 DEALLOCATE (para_env_sub)
767
768 !Currently, rho_ao_t holds the density difference (wrt to pref SCF step).
769 !ks_t also hold that diff, while only having half the blocks => need to add to prev ks_t and symmetrize
770 !We need the full thing for the energy, on the next SCF step
771 CALL get_pmat_images(ri_data%rho_ao_t, rho_ao, 0.0_dp, ri_data, qs_env)
772 DO i_spin = 1, nspins
773 DO b_img = 1, nimg
774 CALL dbt_copy(ks_t(i_spin, b_img), ri_data%ks_t(i_spin, b_img), summation=.true.)
775
776 !desymmetrize
777 mb_img = get_opp_index(b_img, qs_env)
778 IF (mb_img > 0 .AND. mb_img <= nimg) THEN
779 CALL dbt_copy(ks_t(i_spin, mb_img), ri_data%ks_t(i_spin, b_img), order=[2, 1], summation=.true.)
780 END IF
781 END DO
782 END DO
783 DO b_img = 1, nimg
784 DO i_spin = 1, nspins
785 CALL dbt_destroy(ks_t(i_spin, b_img))
786 END DO
787 END DO
788
789 !calculate the energy
790 CALL dbt_create(ri_data%ks_t(1, 1), t_2c_ao_tmp(1))
791 CALL dbcsr_create(tmp, template=ks_matrix(1, 1)%matrix, matrix_type=dbcsr_type_symmetric)
792 CALL dbcsr_create(ks_desymm, template=ks_matrix(1, 1)%matrix, matrix_type=dbcsr_type_no_symmetry)
793 CALL dbcsr_create(rho_desymm, template=ks_matrix(1, 1)%matrix, matrix_type=dbcsr_type_no_symmetry)
794 ehfx = 0.0_dp
795 DO i_img = 1, nimg
796 DO i_spin = 1, nspins
797 CALL dbt_filter(ri_data%ks_t(i_spin, i_img), ri_data%filter_eps)
798 CALL dbt_copy(ri_data%ks_t(i_spin, i_img), t_2c_ao_tmp(1))
799 CALL dbt_copy_tensor_to_matrix(t_2c_ao_tmp(1), ks_desymm)
800 CALL dbt_copy_tensor_to_matrix(t_2c_ao_tmp(1), tmp)
801 CALL dbcsr_add(ks_matrix(i_spin, i_img)%matrix, tmp, 1.0_dp, 1.0_dp)
802
803 CALL dbt_copy(ri_data%rho_ao_t(i_spin, i_img), t_2c_ao_tmp(1))
804 CALL dbt_copy_tensor_to_matrix(t_2c_ao_tmp(1), rho_desymm)
805
806 CALL dbcsr_dot(ks_desymm, rho_desymm, etmp)
807 ehfx = ehfx + 0.5_dp*etmp
808
809 IF (.NOT. use_delta_p) CALL dbt_clear(ri_data%ks_t(i_spin, i_img))
810 END DO
811 END DO
812 CALL dbcsr_release(rho_desymm)
813 CALL dbcsr_release(ks_desymm)
814 CALL dbcsr_release(tmp)
815 CALL dbt_destroy(t_2c_ao_tmp(1))
816
817 CALL timestop(handle)
818
819 END SUBROUTINE hfx_ri_update_ks_kp
820
821! **************************************************************************************************
822!> \brief Update the K-points RI-HFX forces
823!> \param qs_env ...
824!> \param ri_data ...
825!> \param nspins ...
826!> \param hf_fraction ...
827!> \param rho_ao ...
828!> \param use_virial ...
829!> \note Because this routine uses stored quantities calculated in the energy calculation, they should
830!> always be called by pairs, and with the same input densities
831! **************************************************************************************************
832 SUBROUTINE hfx_ri_update_forces_kp(qs_env, ri_data, nspins, hf_fraction, rho_ao, use_virial)
833
834 TYPE(qs_environment_type), POINTER :: qs_env
835 TYPE(hfx_ri_type), INTENT(INOUT) :: ri_data
836 INTEGER, INTENT(IN) :: nspins
837 REAL(kind=dp), INTENT(IN) :: hf_fraction
838 TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: rho_ao
839 LOGICAL, INTENT(IN), OPTIONAL :: use_virial
840
841 CHARACTER(LEN=*), PARAMETER :: routinen = 'hfx_ri_update_forces_kp'
842
843 INTEGER :: b_img, batch_size, group_size, handle, handle2, i_batch, i_img, i_loop, i_spin, &
844 i_xyz, iatom, iblk, igroup, j_xyz, jatom, k_xyz, n_batch, natom, ngroups, nimg, nimg_nze
845 INTEGER(int_8) :: nflop, nze
846 INTEGER, ALLOCATABLE, DIMENSION(:) :: atom_of_kind, batch_ranges_at, &
847 batch_ranges_nze, dist1, dist2, &
848 i_images, idx_to_at_ao, idx_to_at_ri, &
849 kind_of
850 INTEGER, ALLOCATABLE, DIMENSION(:, :) :: iapc_pairs
851 INTEGER, ALLOCATABLE, DIMENSION(:, :, :) :: force_pattern, sparsity_pattern
852 INTEGER, DIMENSION(2, 1) :: bounds_iat, bounds_jat
853 LOGICAL :: use_virial_prv
854 REAL(dp) :: fac, occ, pref, t1, t2
855 REAL(dp), DIMENSION(3, 3) :: work_virial
856 TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
857 TYPE(cell_type), POINTER :: cell
858 TYPE(cp_blacs_env_type), POINTER :: blacs_env_sub
859 TYPE(dbcsr_type), ALLOCATABLE, DIMENSION(:) :: mat_2c_pot
860 TYPE(dbcsr_type), ALLOCATABLE, DIMENSION(:, :) :: mat_der_pot, mat_der_pot_sub
861 TYPE(dbcsr_type), POINTER :: dbcsr_template
862 TYPE(dbt_type) :: t_2c_r, t_2c_r_split
863 TYPE(dbt_type), ALLOCATABLE, DIMENSION(:) :: t_2c_bint, t_2c_binv, t_2c_der_pot, &
864 t_2c_inv, t_2c_metric, t_2c_work, &
865 t_3c_der_stack, t_3c_work_2, &
866 t_3c_work_3
867 TYPE(dbt_type), ALLOCATABLE, DIMENSION(:, :) :: rho_ao_t, rho_ao_t_sub, t_2c_der_metric, &
868 t_2c_der_metric_sub, t_3c_apc, t_3c_apc_sub, t_3c_der_ao, t_3c_der_ao_sub, t_3c_der_ri, &
869 t_3c_der_ri_sub
870 TYPE(mp_para_env_type), POINTER :: para_env, para_env_sub
871 TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
872 TYPE(qs_force_type), DIMENSION(:), POINTER :: force
873 TYPE(section_vals_type), POINTER :: hfx_section
874 TYPE(virial_type), POINTER :: virial
875
876 NULLIFY (para_env, para_env_sub, hfx_section, blacs_env_sub, dbcsr_template, force, atomic_kind_set, &
877 virial, particle_set, cell)
878
879 CALL timeset(routinen, handle)
880
881 use_virial_prv = .false.
882 IF (PRESENT(use_virial)) use_virial_prv = use_virial
883
884 IF (nspins == 1) THEN
885 fac = 0.5_dp*hf_fraction
886 ELSE
887 fac = 1.0_dp*hf_fraction
888 END IF
889
890 CALL get_qs_env(qs_env, natom=natom, para_env=para_env, force=force, cell=cell, virial=virial, &
891 atomic_kind_set=atomic_kind_set, particle_set=particle_set)
892 CALL get_atomic_kind_set(atomic_kind_set, kind_of=kind_of, atom_of_kind=atom_of_kind)
893
894 ALLOCATE (idx_to_at_ao(SIZE(ri_data%bsizes_AO_split)))
895 CALL get_idx_to_atom(idx_to_at_ao, ri_data%bsizes_AO_split, ri_data%bsizes_AO)
896
897 ALLOCATE (idx_to_at_ri(SIZE(ri_data%bsizes_RI_split)))
898 CALL get_idx_to_atom(idx_to_at_ri, ri_data%bsizes_RI_split, ri_data%bsizes_RI)
899
900 nimg = ri_data%nimg
901 ALLOCATE (t_3c_der_ri(nimg, 3), t_3c_der_ao(nimg, 3), mat_der_pot(nimg, 3), t_2c_der_metric(natom, 3))
902
903 !We assume that the integrals are available from the SCF
904 !pre-calculate the derivs. 3c tensors as (P^0| sigma^a mu^0), with t_3c_der_AO holding deriv wrt mu^0
905 CALL precalc_derivatives(t_3c_der_ri, t_3c_der_ao, mat_der_pot, t_2c_der_metric, ri_data, qs_env)
906
907 !Calculate the density matrix at each image
908 ALLOCATE (rho_ao_t(nspins, nimg))
909 CALL create_2c_tensor(rho_ao_t(1, 1), dist1, dist2, ri_data%pgrid_2d, &
910 ri_data%bsizes_AO_split, ri_data%bsizes_AO_split, &
911 name="(AO | AO)")
912 DEALLOCATE (dist1, dist2)
913 IF (nspins == 2) CALL dbt_create(rho_ao_t(1, 1), rho_ao_t(2, 1))
914 DO i_img = 2, nimg
915 DO i_spin = 1, nspins
916 CALL dbt_create(rho_ao_t(1, 1), rho_ao_t(i_spin, i_img))
917 END DO
918 END DO
919 CALL get_pmat_images(rho_ao_t, rho_ao, 0.0_dp, ri_data, qs_env)
920
921 !Contract integrals with the density matrix
922 ALLOCATE (t_3c_apc(nspins, nimg))
923 DO i_img = 1, nimg
924 DO i_spin = 1, nspins
925 CALL dbt_create(ri_data%t_3c_int_ctr_2(1, 1), t_3c_apc(i_spin, i_img))
926 END DO
927 END DO
928 CALL contract_pmat_3c(t_3c_apc, rho_ao_t, ri_data, qs_env)
929
930 !Setup the subgroups
931 hfx_section => section_vals_get_subs_vals(qs_env%input, "DFT%XC%HF%RI")
932 CALL section_vals_val_get(hfx_section, "KP_NGROUPS", i_val=ngroups)
933 group_size = para_env%num_pe/ngroups
934 igroup = para_env%mepos/group_size
935
936 ALLOCATE (para_env_sub)
937 CALL para_env_sub%from_split(para_env, igroup)
938 CALL cp_blacs_env_create(blacs_env_sub, para_env_sub)
939
940 !Get the ususal sparsity pattern
941 ALLOCATE (sparsity_pattern(natom, natom, nimg))
942 CALL get_sparsity_pattern(sparsity_pattern, ri_data, qs_env)
943 CALL get_sub_dist(sparsity_pattern, ngroups, ri_data)
944
945 !Get the 2-center quantities in the subgroups (note: main group derivs are deleted wihtin)
946 ALLOCATE (t_2c_inv(natom), mat_2c_pot(nimg), rho_ao_t_sub(nspins, nimg), t_2c_work(5), &
947 t_2c_der_metric_sub(natom, 3), mat_der_pot_sub(nimg, 3), t_2c_bint(natom), &
948 t_2c_metric(natom), t_2c_binv(natom))
949 CALL get_subgroup_2c_derivs(t_2c_inv, t_2c_bint, t_2c_metric, mat_2c_pot, t_2c_work, rho_ao_t, &
950 rho_ao_t_sub, t_2c_der_metric, t_2c_der_metric_sub, mat_der_pot, &
951 mat_der_pot_sub, group_size, ngroups, para_env, para_env_sub, ri_data)
952 CALL dbt_create(t_2c_work(1), t_2c_r) !nRI x nRI
953 CALL dbt_create(t_2c_work(5), t_2c_r_split) !nRI x nRI with split blocks
954
955 ALLOCATE (t_2c_der_pot(3))
956 DO i_xyz = 1, 3
957 CALL dbt_create(t_2c_r, t_2c_der_pot(i_xyz))
958 END DO
959
960 !Get the 3-center quantities in the subgroups. The integrals and t_3c_apc already there
961 ALLOCATE (t_3c_work_2(3), t_3c_work_3(4), t_3c_der_stack(6), t_3c_der_ao_sub(nimg, 3), &
962 t_3c_der_ri_sub(nimg, 3), t_3c_apc_sub(nspins, nimg))
963 CALL get_subgroup_3c_derivs(t_3c_work_2, t_3c_work_3, t_3c_der_ao, t_3c_der_ao_sub, &
964 t_3c_der_ri, t_3c_der_ri_sub, t_3c_apc, t_3c_apc_sub, t_3c_der_stack, &
965 para_env_sub, ri_data)
966
967 !Set up batched contraction (go atom by atom)
968 ALLOCATE (batch_ranges_at(natom + 1))
969 batch_ranges_at(natom + 1) = SIZE(ri_data%bsizes_AO_split) + 1
970 iatom = 0
971 DO iblk = 1, SIZE(ri_data%bsizes_AO_split)
972 IF (idx_to_at_ao(iblk) == iatom + 1) THEN
973 iatom = iatom + 1
974 batch_ranges_at(iatom) = iblk
975 END IF
976 END DO
977
978 CALL dbt_batched_contract_init(t_3c_work_3(1), batch_range_2=batch_ranges_at)
979 CALL dbt_batched_contract_init(t_3c_work_3(2), batch_range_2=batch_ranges_at)
980 CALL dbt_batched_contract_init(t_3c_work_3(3), batch_range_2=batch_ranges_at)
981 CALL dbt_batched_contract_init(t_3c_work_2(1), batch_range_1=batch_ranges_at)
982 CALL dbt_batched_contract_init(t_3c_work_2(2), batch_range_1=batch_ranges_at)
983
984 !Preparing for the stacking of 3c tensors
985 nimg_nze = ri_data%nimg_nze
986 batch_size = ri_data%kp_stack_size
987 n_batch = nimg_nze/batch_size
988 IF (modulo(nimg_nze, batch_size) /= 0) n_batch = n_batch + 1
989 ALLOCATE (batch_ranges_nze(n_batch + 1))
990 DO i_batch = 1, n_batch
991 batch_ranges_nze(i_batch) = (i_batch - 1)*batch_size + 1
992 END DO
993 batch_ranges_nze(n_batch + 1) = nimg_nze + 1
994
995 !Applying the external bump to ((P|Q)_D + B*(P|Q)_OD*B)^-1 from left and right
996 !And keep the bump on LHS only version as well, with B*M^-1 = (M^-1*B)^T
997 DO iatom = 1, natom
998 CALL dbt_create(t_2c_inv(iatom), t_2c_binv(iatom))
999 CALL dbt_copy(t_2c_inv(iatom), t_2c_binv(iatom))
1000 CALL apply_bump(t_2c_binv(iatom), iatom, ri_data, qs_env, from_left=.true., from_right=.false.)
1001 CALL apply_bump(t_2c_inv(iatom), iatom, ri_data, qs_env, from_left=.true., from_right=.true.)
1002 END DO
1003
1004 t1 = m_walltime()
1005 work_virial = 0.0_dp
1006 ALLOCATE (iapc_pairs(nimg, 2), i_images(nimg))
1007 ALLOCATE (force_pattern(natom, natom, nimg))
1008 force_pattern(:, :, :) = -1
1009 !We proceed with 2 loops: one over the sparsity pattern from the SCF, one over the rest
1010 !We use the SCF cost model for the first loop, while we calculate the cost of the upcoming loop
1011 DO i_loop = 1, 2
1012 DO b_img = 1, nimg
1013 DO jatom = 1, natom
1014 DO iatom = 1, natom
1015
1016 pref = -0.5_dp*fac
1017 IF (i_loop == 1 .AND. (.NOT. sparsity_pattern(iatom, jatom, b_img) == igroup)) cycle
1018 IF (i_loop == 2 .AND. (.NOT. force_pattern(iatom, jatom, b_img) == igroup)) cycle
1019
1020 !Get the proper HFX potential 2c integrals (R_i^0|S_j^b), times (S_j^b|Q_j^b)^-1
1021 CALL timeset(routinen//"_2c_1", handle2)
1022 CALL get_ext_2c_int(t_2c_work(1), mat_2c_pot, iatom, jatom, b_img, ri_data, qs_env, &
1023 blacs_env_ext=blacs_env_sub, para_env_ext=para_env_sub, &
1024 dbcsr_template=dbcsr_template)
1025 CALL dbt_contract(1.0_dp, t_2c_work(1), t_2c_inv(jatom), &
1026 0.0_dp, t_2c_work(2), map_1=[1], map_2=[2], &
1027 contract_1=[2], notcontract_1=[1], &
1028 contract_2=[1], notcontract_2=[2], &
1029 filter_eps=ri_data%filter_eps, flop=nflop)
1030 CALL dbt_copy(t_2c_work(2), t_2c_work(5), move_data=.true.) !move to split blocks
1031 CALL dbt_filter(t_2c_work(5), ri_data%filter_eps)
1032 CALL timestop(handle2)
1033
1034 CALL timeset(routinen//"_3c", handle2)
1035 bounds_iat(:, 1) = [sum(ri_data%bsizes_AO(1:iatom - 1)) + 1, sum(ri_data%bsizes_AO(1:iatom))]
1036 bounds_jat(:, 1) = [sum(ri_data%bsizes_AO(1:jatom - 1)) + 1, sum(ri_data%bsizes_AO(1:jatom))]
1037 CALL dbt_clear(t_2c_r_split)
1038
1039 DO i_spin = 1, nspins
1040 CALL dbt_batched_contract_init(rho_ao_t_sub(i_spin, b_img))
1041 END DO
1042
1043 CALL get_iapc_pairs(iapc_pairs, b_img, ri_data, qs_env, i_images) !i = a+c-b
1044 DO i_batch = 1, n_batch
1045
1046 !Stack the 3c derivatives to take the trace later on
1047 DO i_xyz = 1, 3
1048 CALL dbt_clear(t_3c_der_stack(i_xyz))
1049 CALL fill_3c_stack(t_3c_der_stack(i_xyz), t_3c_der_ri_sub(:, i_xyz), &
1050 iapc_pairs(:, 1), 3, ri_data, filter_at=jatom, &
1051 filter_dim=2, idx_to_at=idx_to_at_ao, &
1052 img_bounds=[batch_ranges_nze(i_batch), batch_ranges_nze(i_batch + 1)])
1053
1054 CALL dbt_clear(t_3c_der_stack(3 + i_xyz))
1055 CALL fill_3c_stack(t_3c_der_stack(3 + i_xyz), t_3c_der_ao_sub(:, i_xyz), &
1056 iapc_pairs(:, 1), 3, ri_data, filter_at=jatom, &
1057 filter_dim=2, idx_to_at=idx_to_at_ao, &
1058 img_bounds=[batch_ranges_nze(i_batch), batch_ranges_nze(i_batch + 1)])
1059 END DO
1060
1061 DO i_spin = 1, nspins
1062 !stack the t_3c_apc tensors
1063 CALL dbt_clear(t_3c_work_2(3))
1064 CALL fill_3c_stack(t_3c_work_2(3), t_3c_apc_sub(i_spin, :), iapc_pairs(:, 2), 3, &
1065 ri_data, filter_at=iatom, filter_dim=1, idx_to_at=idx_to_at_ao, &
1066 img_bounds=[batch_ranges_nze(i_batch), batch_ranges_nze(i_batch + 1)])
1067 CALL get_tensor_occupancy(t_3c_work_2(3), nze, occ)
1068 IF (nze == 0) cycle
1069 CALL dbt_copy(t_3c_work_2(3), t_3c_work_2(1), move_data=.true.)
1070
1071 !Contract with the second density matrix: P_mu^0,nu^b * t_3c_apc,
1072 !where t_3c_apc = P_sigma^a,lambda^a+c (mu^0 P^0 sigma^a) *(P^0|R^0)^-1 (stacked along a+c)
1073 CALL dbt_contract(1.0_dp, rho_ao_t_sub(i_spin, b_img), t_3c_work_2(1), &
1074 0.0_dp, t_3c_work_2(2), map_1=[1], map_2=[2, 3], &
1075 contract_1=[1], notcontract_1=[2], &
1076 contract_2=[1], notcontract_2=[2, 3], &
1077 bounds_1=bounds_iat, bounds_2=bounds_jat, &
1078 filter_eps=ri_data%filter_eps, flop=nflop)
1079 ri_data%dbcsr_nflop = ri_data%dbcsr_nflop + nflop
1080
1081 CALL get_tensor_occupancy(t_3c_work_2(2), nze, occ)
1082 IF (nze == 0) cycle
1083
1084 !Contract with V_PQ so that we can take the trace with (Q^b|nu^b lmabda^a+c)^(x)
1085 CALL dbt_copy(t_3c_work_2(2), t_3c_work_3(1), order=[2, 1, 3], move_data=.true.)
1086 CALL dbt_batched_contract_init(t_2c_work(5))
1087 CALL dbt_contract(1.0_dp, t_2c_work(5), t_3c_work_3(1), &
1088 0.0_dp, t_3c_work_3(2), map_1=[1], map_2=[2, 3], &
1089 contract_1=[1], notcontract_1=[2], &
1090 contract_2=[1], notcontract_2=[2, 3], &
1091 filter_eps=ri_data%filter_eps, flop=nflop)
1092 ri_data%dbcsr_nflop = ri_data%dbcsr_nflop + nflop
1093 CALL dbt_batched_contract_finalize(t_2c_work(5))
1094
1095 !Contract with the 3c derivatives to get the force/virial
1096 CALL dbt_copy(t_3c_work_3(2), t_3c_work_3(4), move_data=.true.)
1097 IF (use_virial_prv) THEN
1098 CALL get_force_from_3c_trace(force, t_3c_work_3(4), t_3c_der_stack(1:3), &
1099 t_3c_der_stack(4:6), atom_of_kind, kind_of, &
1100 idx_to_at_ri, idx_to_at_ao, i_images, &
1101 batch_ranges_nze(i_batch), 2.0_dp*pref, &
1102 ri_data, qs_env, work_virial, cell, particle_set)
1103 ELSE
1104 CALL get_force_from_3c_trace(force, t_3c_work_3(4), t_3c_der_stack(1:3), &
1105 t_3c_der_stack(4:6), atom_of_kind, kind_of, &
1106 idx_to_at_ri, idx_to_at_ao, i_images, &
1107 batch_ranges_nze(i_batch), 2.0_dp*pref, &
1108 ri_data, qs_env)
1109 END IF
1110 CALL dbt_clear(t_3c_work_3(4))
1111
1112 !Contract with the 3-center integrals in order to have a matrix R_PQ such that
1113 !we can take the trace sum_PQ R_PQ (P^0|Q^b)^(x)
1114 IF (i_loop == 2) cycle
1115
1116 !Stack the 3c integrals
1117 CALL fill_3c_stack(t_3c_work_3(4), ri_data%kp_t_3c_int, iapc_pairs(:, 1), 3, ri_data, &
1118 filter_at=jatom, filter_dim=2, idx_to_at=idx_to_at_ao, &
1119 img_bounds=[batch_ranges_nze(i_batch), batch_ranges_nze(i_batch + 1)])
1120 CALL dbt_copy(t_3c_work_3(4), t_3c_work_3(3), move_data=.true.)
1121
1122 CALL dbt_batched_contract_init(t_2c_r_split)
1123 CALL dbt_contract(1.0_dp, t_3c_work_3(1), t_3c_work_3(3), &
1124 1.0_dp, t_2c_r_split, map_1=[1], map_2=[2], &
1125 contract_1=[2, 3], notcontract_1=[1], &
1126 contract_2=[2, 3], notcontract_2=[1], &
1127 filter_eps=ri_data%filter_eps, flop=nflop)
1128 ri_data%dbcsr_nflop = ri_data%dbcsr_nflop + nflop
1129 CALL dbt_batched_contract_finalize(t_2c_r_split)
1130 CALL dbt_copy(t_3c_work_3(4), t_3c_work_3(1))
1131 END DO
1132 END DO
1133 DO i_spin = 1, nspins
1134 CALL dbt_batched_contract_finalize(rho_ao_t_sub(i_spin, b_img))
1135 END DO
1136 CALL timestop(handle2)
1137
1138 IF (i_loop == 2) cycle
1139 pref = 2.0_dp*pref
1140 IF (iatom == jatom .AND. b_img == 1) pref = 0.5_dp*pref
1141
1142 CALL timeset(routinen//"_2c_2", handle2)
1143 !Note that the derivatives are in atomic block format (not split)
1144 CALL dbt_copy(t_2c_r_split, t_2c_r, move_data=.true.)
1145
1146 CALL get_ext_2c_int(t_2c_work(1), mat_2c_pot, iatom, jatom, b_img, ri_data, qs_env, &
1147 blacs_env_ext=blacs_env_sub, para_env_ext=para_env_sub, &
1148 dbcsr_template=dbcsr_template)
1149
1150 !We have to calculate: S^-1(iat) * R_PQ * S^-1(jat) to trace with HFX pot der
1151 ! + R_PQ * S^-1(jat) * pot^T to trace with S^(x) (iat)
1152 ! + pot^T * S^-1(iat) *R_PQ to trace with S^(x) (jat)
1153
1154 !Because 3c tensors are all precontracted with the inverse RI metric,
1155 !t_2c_R is currently implicitely multiplied by S^-1(iat) from the left
1156 !and S^-1(jat) from the right, directly in the proper format for the trace
1157 !with the HFX potential derivative
1158
1159 !Trace with HFX pot deriv, that we need to build first
1160 DO i_xyz = 1, 3
1161 CALL get_ext_2c_int(t_2c_der_pot(i_xyz), mat_der_pot_sub(:, i_xyz), iatom, jatom, &
1162 b_img, ri_data, qs_env, blacs_env_ext=blacs_env_sub, &
1163 para_env_ext=para_env_sub, dbcsr_template=dbcsr_template)
1164 END DO
1165
1166 IF (use_virial_prv) THEN
1167 CALL get_2c_der_force(force, t_2c_r, t_2c_der_pot, atom_of_kind, kind_of, &
1168 b_img, pref, ri_data, qs_env, work_virial, cell, particle_set)
1169 ELSE
1170 CALL get_2c_der_force(force, t_2c_r, t_2c_der_pot, atom_of_kind, kind_of, &
1171 b_img, pref, ri_data, qs_env)
1172 END IF
1173
1174 DO i_xyz = 1, 3
1175 CALL dbt_clear(t_2c_der_pot(i_xyz))
1176 END DO
1177
1178 !R_PQ * S^-1(jat) * pot^T (=A)
1179 CALL dbt_contract(1.0_dp, t_2c_metric(iatom), t_2c_r, & !get rid of implicit S^-1(iat)
1180 0.0_dp, t_2c_work(2), map_1=[1], map_2=[2], &
1181 contract_1=[2], notcontract_1=[1], &
1182 contract_2=[1], notcontract_2=[2], &
1183 filter_eps=ri_data%filter_eps, flop=nflop)
1184 ri_data%dbcsr_nflop = ri_data%dbcsr_nflop + nflop
1185 CALL dbt_contract(1.0_dp, t_2c_work(2), t_2c_work(1), &
1186 0.0_dp, t_2c_work(3), map_1=[1], map_2=[2], &
1187 contract_1=[2], notcontract_1=[1], &
1188 contract_2=[2], notcontract_2=[1], &
1189 filter_eps=ri_data%filter_eps, flop=nflop)
1190 ri_data%dbcsr_nflop = ri_data%dbcsr_nflop + nflop
1191
1192 !With the RI bump function, things get more complex. M = (S|P)_D + B*(S|P)_OD*B
1193 !Calculate M^-1*B*A + A*B*M^-1 to contract with B^x. A is in t_2c_work(3)
1194 CALL dbt_contract(1.0_dp, t_2c_work(3), t_2c_binv(iatom), &
1195 0.0_dp, t_2c_work(2), map_1=[1], map_2=[2], &
1196 contract_1=[2], notcontract_1=[1], &
1197 contract_2=[1], notcontract_2=[2], &
1198 filter_eps=ri_data%filter_eps, flop=nflop)
1199 ri_data%dbcsr_nflop = ri_data%dbcsr_nflop + nflop
1200
1201 CALL dbt_contract(1.0_dp, t_2c_binv(iatom), t_2c_work(3), & !use transpose of B*M^-1 = M^-1*B
1202 0.0_dp, t_2c_work(4), map_1=[1], map_2=[2], &
1203 contract_1=[1], notcontract_1=[2], &
1204 contract_2=[1], notcontract_2=[2], &
1205 filter_eps=ri_data%filter_eps, flop=nflop)
1206 ri_data%dbcsr_nflop = ri_data%dbcsr_nflop + nflop
1207
1208 CALL dbt_copy(t_2c_work(2), t_2c_work(4), summation=.true.)
1209 CALL get_2c_bump_forces(force, t_2c_work(4), iatom, atom_of_kind, kind_of, pref, &
1210 ri_data, qs_env, work_virial)
1211
1212 !Calculate -M^-1*B*A*B*M^-1 to contracte with diagonal RI metric deriv. t_2c_work(2) holds A*B*M^-1
1213 CALL dbt_contract(1.0_dp, t_2c_binv(iatom), t_2c_work(2), &
1214 0.0_dp, t_2c_work(4), map_1=[1], map_2=[2], &
1215 contract_1=[1], notcontract_1=[2], &
1216 contract_2=[1], notcontract_2=[2], &
1217 filter_eps=ri_data%filter_eps, flop=nflop)
1218 ri_data%dbcsr_nflop = ri_data%dbcsr_nflop + nflop
1219
1220 IF (use_virial_prv) THEN
1221 CALL get_2c_der_force(force, t_2c_work(4), t_2c_der_metric_sub(iatom, :), atom_of_kind, &
1222 kind_of, 1, -pref, ri_data, qs_env, work_virial, cell, particle_set, &
1223 diag=.true., offdiag=.false.)
1224 ELSE
1225 CALL get_2c_der_force(force, t_2c_work(4), t_2c_der_metric_sub(iatom, :), atom_of_kind, &
1226 kind_of, 1, -pref, ri_data, qs_env, diag=.true., offdiag=.false.)
1227 END IF
1228
1229 !Calculate -B*M^-1*B*A*B*M^-1*B to contract with off-diagonal RI metric derivs
1230 CALL dbt_copy(t_2c_work(4), t_2c_work(2))
1231 CALL apply_bump(t_2c_work(2), iatom, ri_data, qs_env, from_left=.true., from_right=.true.)
1232
1233 IF (use_virial_prv) THEN
1234 CALL get_2c_der_force(force, t_2c_work(2), t_2c_der_metric_sub(iatom, :), atom_of_kind, &
1235 kind_of, 1, -pref, ri_data, qs_env, work_virial, cell, particle_set, &
1236 diag=.false., offdiag=.true.)
1237 ELSE
1238 CALL get_2c_der_force(force, t_2c_work(2), t_2c_der_metric_sub(iatom, :), atom_of_kind, &
1239 kind_of, 1, -pref, ri_data, qs_env, diag=.false., offdiag=.true.)
1240 END IF
1241
1242 !Calculate -O*B*M^-1*B*A*B*M^-1 - M^-1*B*A*B*M^-1*B*O, where O is off-diagonal integrals
1243 !t_2c_work(4) holds M^-1*B*A*B*M^-1, and exploit transpose of B*O (stored in t_2c_bint)
1244 CALL dbt_contract(1.0_dp, t_2c_work(4), t_2c_bint(iatom), &
1245 0.0_dp, t_2c_work(2), map_1=[1], map_2=[2], &
1246 contract_1=[2], notcontract_1=[1], &
1247 contract_2=[1], notcontract_2=[2], &
1248 filter_eps=ri_data%filter_eps, flop=nflop)
1249 ri_data%dbcsr_nflop = ri_data%dbcsr_nflop + nflop
1250
1251 CALL dbt_contract(1.0_dp, t_2c_bint(iatom), t_2c_work(4), &
1252 1.0_dp, t_2c_work(2), map_1=[1], map_2=[2], &
1253 contract_1=[1], notcontract_1=[2], &
1254 contract_2=[1], notcontract_2=[2], &
1255 filter_eps=ri_data%filter_eps, flop=nflop)
1256 ri_data%dbcsr_nflop = ri_data%dbcsr_nflop + nflop
1257
1258 CALL get_2c_bump_forces(force, t_2c_work(2), iatom, atom_of_kind, kind_of, -pref, &
1259 ri_data, qs_env, work_virial)
1260
1261 ! pot^T * S^-1(iat) * R_PQ (=A)
1262 CALL dbt_contract(1.0_dp, t_2c_work(1), t_2c_r, &
1263 0.0_dp, t_2c_work(2), map_1=[1], map_2=[2], &
1264 contract_1=[1], notcontract_1=[2], &
1265 contract_2=[1], notcontract_2=[2], &
1266 filter_eps=ri_data%filter_eps, flop=nflop)
1267 ri_data%dbcsr_nflop = ri_data%dbcsr_nflop + nflop
1268
1269 CALL dbt_contract(1.0_dp, t_2c_work(2), t_2c_metric(jatom), & !get rid of implicit S^-1(jat)
1270 0.0_dp, t_2c_work(3), map_1=[1], map_2=[2], &
1271 contract_1=[2], notcontract_1=[1], &
1272 contract_2=[1], notcontract_2=[2], &
1273 filter_eps=ri_data%filter_eps, flop=nflop)
1274 ri_data%dbcsr_nflop = ri_data%dbcsr_nflop + nflop
1275
1276 !Do the same shenanigans with the S^(x) (jatom)
1277 !Calculate M^-1*B*A + A*B*M^-1 to contract with B^x. A is in t_2c_work(3)
1278 CALL dbt_contract(1.0_dp, t_2c_work(3), t_2c_binv(jatom), &
1279 0.0_dp, t_2c_work(2), map_1=[1], map_2=[2], &
1280 contract_1=[2], notcontract_1=[1], &
1281 contract_2=[1], notcontract_2=[2], &
1282 filter_eps=ri_data%filter_eps, flop=nflop)
1283 ri_data%dbcsr_nflop = ri_data%dbcsr_nflop + nflop
1284
1285 CALL dbt_contract(1.0_dp, t_2c_binv(jatom), t_2c_work(3), & !use transpose of B*M^-1 = M^-1*B
1286 0.0_dp, t_2c_work(4), map_1=[1], map_2=[2], &
1287 contract_1=[1], notcontract_1=[2], &
1288 contract_2=[1], notcontract_2=[2], &
1289 filter_eps=ri_data%filter_eps, flop=nflop)
1290 ri_data%dbcsr_nflop = ri_data%dbcsr_nflop + nflop
1291
1292 CALL dbt_copy(t_2c_work(2), t_2c_work(4), summation=.true.)
1293 CALL get_2c_bump_forces(force, t_2c_work(4), jatom, atom_of_kind, kind_of, pref, &
1294 ri_data, qs_env, work_virial)
1295
1296 !Calculate -M^-1*B*A*B*M^-1 to contracte with diagonal RI metric deriv. t_2c_work(2) holds A*B*M^-1
1297 CALL dbt_contract(1.0_dp, t_2c_binv(jatom), t_2c_work(2), &
1298 0.0_dp, t_2c_work(4), map_1=[1], map_2=[2], &
1299 contract_1=[1], notcontract_1=[2], &
1300 contract_2=[1], notcontract_2=[2], &
1301 filter_eps=ri_data%filter_eps, flop=nflop)
1302 ri_data%dbcsr_nflop = ri_data%dbcsr_nflop + nflop
1303
1304 IF (use_virial_prv) THEN
1305 CALL get_2c_der_force(force, t_2c_work(4), t_2c_der_metric_sub(jatom, :), atom_of_kind, &
1306 kind_of, 1, -pref, ri_data, qs_env, work_virial, cell, particle_set, &
1307 diag=.true., offdiag=.false.)
1308 ELSE
1309 CALL get_2c_der_force(force, t_2c_work(4), t_2c_der_metric_sub(jatom, :), atom_of_kind, &
1310 kind_of, 1, -pref, ri_data, qs_env, diag=.true., offdiag=.false.)
1311 END IF
1312
1313 !Calculate -B*M^-1*B*A*B*M^-1*B to contract with off-diagonal RI metric derivs
1314 CALL dbt_copy(t_2c_work(4), t_2c_work(2))
1315 CALL apply_bump(t_2c_work(2), jatom, ri_data, qs_env, from_left=.true., from_right=.true.)
1316
1317 IF (use_virial_prv) THEN
1318 CALL get_2c_der_force(force, t_2c_work(2), t_2c_der_metric_sub(jatom, :), atom_of_kind, &
1319 kind_of, 1, -pref, ri_data, qs_env, work_virial, cell, particle_set, &
1320 diag=.false., offdiag=.true.)
1321 ELSE
1322 CALL get_2c_der_force(force, t_2c_work(2), t_2c_der_metric_sub(jatom, :), atom_of_kind, &
1323 kind_of, 1, -pref, ri_data, qs_env, diag=.false., offdiag=.true.)
1324 END IF
1325
1326 !Calculate -O*B*M^-1*B*A*B*M^-1 - M^-1*B*A*B*M^-1*B*O, where O is off-diagonal integrals
1327 !t_2c_work(4) holds M^-1*B*A*B*M^-1, and exploit transpose of B*O (stored in t_2c_bint)
1328 CALL dbt_contract(1.0_dp, t_2c_work(4), t_2c_bint(jatom), &
1329 0.0_dp, t_2c_work(2), map_1=[1], map_2=[2], &
1330 contract_1=[2], notcontract_1=[1], &
1331 contract_2=[1], notcontract_2=[2], &
1332 filter_eps=ri_data%filter_eps, flop=nflop)
1333 ri_data%dbcsr_nflop = ri_data%dbcsr_nflop + nflop
1334
1335 CALL dbt_contract(1.0_dp, t_2c_bint(jatom), t_2c_work(4), &
1336 1.0_dp, t_2c_work(2), map_1=[1], map_2=[2], &
1337 contract_1=[1], notcontract_1=[2], &
1338 contract_2=[1], notcontract_2=[2], &
1339 filter_eps=ri_data%filter_eps, flop=nflop)
1340 ri_data%dbcsr_nflop = ri_data%dbcsr_nflop + nflop
1341
1342 CALL get_2c_bump_forces(force, t_2c_work(2), jatom, atom_of_kind, kind_of, -pref, &
1343 ri_data, qs_env, work_virial)
1344
1345 CALL timestop(handle2)
1346 END DO !iatom
1347 END DO !jatom
1348 END DO !b_img
1349
1350 IF (i_loop == 1) THEN
1351 CALL update_pattern_to_forces(force_pattern, sparsity_pattern, ngroups, ri_data, qs_env)
1352 END IF
1353 END DO !i_loop
1354
1355 CALL dbt_batched_contract_finalize(t_3c_work_3(1))
1356 CALL dbt_batched_contract_finalize(t_3c_work_3(2))
1357 CALL dbt_batched_contract_finalize(t_3c_work_3(3))
1358 CALL dbt_batched_contract_finalize(t_3c_work_2(1))
1359 CALL dbt_batched_contract_finalize(t_3c_work_2(2))
1360
1361 IF (use_virial_prv) THEN
1362 DO k_xyz = 1, 3
1363 DO j_xyz = 1, 3
1364 DO i_xyz = 1, 3
1365 virial%pv_fock_4c(i_xyz, j_xyz) = virial%pv_fock_4c(i_xyz, j_xyz) &
1366 + work_virial(i_xyz, k_xyz)*cell%hmat(j_xyz, k_xyz)
1367 END DO
1368 END DO
1369 END DO
1370 END IF
1371
1372 !End of subgroup parallelization
1373 CALL cp_blacs_env_release(blacs_env_sub)
1374 CALL para_env_sub%free()
1375 DEALLOCATE (para_env_sub)
1376
1377 CALL para_env%sync()
1378 t2 = m_walltime()
1379 ri_data%dbcsr_time = ri_data%dbcsr_time + t2 - t1
1380
1381 !clean-up
1382 IF (ASSOCIATED(dbcsr_template)) THEN
1383 CALL dbcsr_release(dbcsr_template)
1384 DEALLOCATE (dbcsr_template)
1385 END IF
1386 CALL dbt_destroy(t_2c_r)
1387 CALL dbt_destroy(t_2c_r_split)
1388 CALL dbt_destroy(t_2c_work(1))
1389 CALL dbt_destroy(t_2c_work(2))
1390 CALL dbt_destroy(t_2c_work(3))
1391 CALL dbt_destroy(t_2c_work(4))
1392 CALL dbt_destroy(t_2c_work(5))
1393 CALL dbt_destroy(t_3c_work_2(1))
1394 CALL dbt_destroy(t_3c_work_2(2))
1395 CALL dbt_destroy(t_3c_work_2(3))
1396 CALL dbt_destroy(t_3c_work_3(1))
1397 CALL dbt_destroy(t_3c_work_3(2))
1398 CALL dbt_destroy(t_3c_work_3(3))
1399 CALL dbt_destroy(t_3c_work_3(4))
1400 CALL dbt_destroy(t_3c_der_stack(1))
1401 CALL dbt_destroy(t_3c_der_stack(2))
1402 CALL dbt_destroy(t_3c_der_stack(3))
1403 CALL dbt_destroy(t_3c_der_stack(4))
1404 CALL dbt_destroy(t_3c_der_stack(5))
1405 CALL dbt_destroy(t_3c_der_stack(6))
1406 DO i_xyz = 1, 3
1407 CALL dbt_destroy(t_2c_der_pot(i_xyz))
1408 END DO
1409 DO iatom = 1, natom
1410 CALL dbt_destroy(t_2c_inv(iatom))
1411 CALL dbt_destroy(t_2c_binv(iatom))
1412 CALL dbt_destroy(t_2c_bint(iatom))
1413 CALL dbt_destroy(t_2c_metric(iatom))
1414 DO i_xyz = 1, 3
1415 CALL dbt_destroy(t_2c_der_metric_sub(iatom, i_xyz))
1416 END DO
1417 END DO
1418 DO i_img = 1, nimg
1419 CALL dbcsr_release(mat_2c_pot(i_img))
1420 DO i_spin = 1, nspins
1421 CALL dbt_destroy(rho_ao_t_sub(i_spin, i_img))
1422 CALL dbt_destroy(t_3c_apc_sub(i_spin, i_img))
1423 END DO
1424 END DO
1425 DO i_xyz = 1, 3
1426 DO i_img = 1, nimg
1427 CALL dbt_destroy(t_3c_der_ri_sub(i_img, i_xyz))
1428 CALL dbt_destroy(t_3c_der_ao_sub(i_img, i_xyz))
1429 CALL dbcsr_release(mat_der_pot_sub(i_img, i_xyz))
1430 END DO
1431 END DO
1432
1433 CALL timestop(handle)
1434
1435 END SUBROUTINE hfx_ri_update_forces_kp
1436
1437! **************************************************************************************************
1438!> \brief A routine the applies the RI bump matrix from the left and/or the right, given an input
1439!> matrix and the central RI atom. We assume atomic block sizes
1440!> \param t_2c_inout ...
1441!> \param atom_i ...
1442!> \param ri_data ...
1443!> \param qs_env ...
1444!> \param from_left ...
1445!> \param from_right ...
1446!> \param debump ...
1447! **************************************************************************************************
1448 SUBROUTINE apply_bump(t_2c_inout, atom_i, ri_data, qs_env, from_left, from_right, debump)
1449 TYPE(dbt_type), INTENT(INOUT) :: t_2c_inout
1450 INTEGER, INTENT(IN) :: atom_i
1451 TYPE(hfx_ri_type), INTENT(INOUT) :: ri_data
1452 TYPE(qs_environment_type), POINTER :: qs_env
1453 LOGICAL, INTENT(IN), OPTIONAL :: from_left, from_right, debump
1454
1455 INTEGER :: i_img, i_ri, iatom, ind(2), j_img, j_ri, &
1456 jatom, natom, nblks(2), nimg, nkind
1457 INTEGER, DIMENSION(:, :), POINTER :: index_to_cell
1458 INTEGER, DIMENSION(:, :, :), POINTER :: cell_to_index
1459 LOGICAL :: found, my_debump, my_left, my_right
1460 REAL(dp) :: bval, r0, r1, ri(3), rj(3), rref(3), &
1461 scoord(3)
1462 REAL(dp), ALLOCATABLE, DIMENSION(:, :) :: blk
1463 TYPE(cell_type), POINTER :: cell
1464 TYPE(dbt_iterator_type) :: iter
1465 TYPE(kpoint_type), POINTER :: kpoints
1466 TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
1467 TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
1468
1469 NULLIFY (qs_kind_set, particle_set, kpoints, index_to_cell, cell_to_index, cell)
1470
1471 CALL get_qs_env(qs_env, natom=natom, nkind=nkind, qs_kind_set=qs_kind_set, cell=cell, &
1472 kpoints=kpoints, particle_set=particle_set)
1473 CALL get_kpoint_info(kpoints, cell_to_index=cell_to_index, index_to_cell=index_to_cell)
1474
1475 my_debump = .false.
1476 IF (PRESENT(debump)) my_debump = debump
1477
1478 my_left = .false.
1479 IF (PRESENT(from_left)) my_left = from_left
1480
1481 my_right = .false.
1482 IF (PRESENT(from_right)) my_right = from_right
1483 cpassert(my_left .OR. my_right)
1484
1485 CALL dbt_get_info(t_2c_inout, nblks_total=nblks)
1486 cpassert(nblks(1) == ri_data%ncell_RI*natom)
1487 cpassert(nblks(2) == ri_data%ncell_RI*natom)
1488
1489 nimg = ri_data%nimg
1490
1491 !Loop over the RI cells and atoms, and apply bump accordingly
1492 r1 = ri_data%kp_RI_range
1493 r0 = ri_data%kp_bump_rad
1494 rref = pbc(particle_set(atom_i)%r, cell)
1495
1496!$OMP PARALLEL DEFAULT(NONE) SHARED(t_2c_inout,natom,ri_data,cell,particle_set,index_to_cell,my_left, &
1497!$OMP my_right,r0,r1,rref,my_debump) &
1498!$OMP PRIVATE(iter,ind,blk,found,i_RI,i_img,iatom,j_RI,j_img,jatom,scoord,ri,rj,bval)
1499 CALL dbt_iterator_start(iter, t_2c_inout)
1500 DO WHILE (dbt_iterator_blocks_left(iter))
1501 CALL dbt_iterator_next_block(iter, ind)
1502 CALL dbt_get_block(t_2c_inout, ind, blk, found)
1503 IF (.NOT. found) cycle
1504
1505 i_ri = (ind(1) - 1)/natom + 1
1506 i_img = ri_data%RI_cell_to_img(i_ri)
1507 iatom = ind(1) - (i_ri - 1)*natom
1508
1509 CALL real_to_scaled(scoord, pbc(particle_set(iatom)%r, cell), cell)
1510 CALL scaled_to_real(ri, scoord(:) + index_to_cell(:, i_img), cell)
1511
1512 j_ri = (ind(2) - 1)/natom + 1
1513 j_img = ri_data%RI_cell_to_img(j_ri)
1514 jatom = ind(2) - (j_ri - 1)*natom
1515
1516 CALL real_to_scaled(scoord, pbc(particle_set(jatom)%r, cell), cell)
1517 CALL scaled_to_real(rj, scoord(:) + index_to_cell(:, j_img), cell)
1518
1519 IF (.NOT. my_debump) THEN
1520 IF (my_left) blk(:, :) = blk(:, :)*bump(norm2(ri - rref), r0, r1)
1521 IF (my_right) blk(:, :) = blk(:, :)*bump(norm2(rj - rref), r0, r1)
1522 ELSE
1523 !Note: by construction, the bump function is never quite zero, as its range is the same
1524 ! as that of the extended RI basis (but we are safe)
1525 bval = bump(norm2(ri - rref), r0, r1)
1526 IF (my_left .AND. bval > epsilon(1.0_dp)) blk(:, :) = blk(:, :)/bval
1527 bval = bump(norm2(rj - rref), r0, r1)
1528 IF (my_right .AND. bval > epsilon(1.0_dp)) blk(:, :) = blk(:, :)/bval
1529 END IF
1530
1531 CALL dbt_put_block(t_2c_inout, ind, shape(blk), blk)
1532
1533 DEALLOCATE (blk)
1534 END DO
1535 CALL dbt_iterator_stop(iter)
1536!$OMP END PARALLEL
1537 CALL dbt_filter(t_2c_inout, ri_data%filter_eps)
1538
1539 END SUBROUTINE apply_bump
1540
1541! **************************************************************************************************
1542!> \brief A routine that calculates the forces due to the derivative of the bump function
1543!> \param force ...
1544!> \param t_2c_in ...
1545!> \param atom_i ...
1546!> \param atom_of_kind ...
1547!> \param kind_of ...
1548!> \param pref ...
1549!> \param ri_data ...
1550!> \param qs_env ...
1551!> \param work_virial ...
1552! **************************************************************************************************
1553 SUBROUTINE get_2c_bump_forces(force, t_2c_in, atom_i, atom_of_kind, kind_of, pref, ri_data, &
1554 qs_env, work_virial)
1555 TYPE(qs_force_type), DIMENSION(:), POINTER :: force
1556 TYPE(dbt_type), INTENT(INOUT) :: t_2c_in
1557 INTEGER, INTENT(IN) :: atom_i
1558 INTEGER, DIMENSION(:), INTENT(IN) :: atom_of_kind, kind_of
1559 REAL(dp), INTENT(IN) :: pref
1560 TYPE(hfx_ri_type), INTENT(INOUT) :: ri_data
1561 TYPE(qs_environment_type), POINTER :: qs_env
1562 REAL(dp), DIMENSION(3, 3), INTENT(INOUT) :: work_virial
1563
1564 INTEGER :: i, i_img, i_ri, i_xyz, iat_of_kind, iatom, ikind, ind(2), j_img, j_ri, j_xyz, &
1565 jat_of_kind, jatom, jkind, natom, nblks(2), nimg, nkind
1566 INTEGER, DIMENSION(:, :), POINTER :: index_to_cell
1567 INTEGER, DIMENSION(:, :, :), POINTER :: cell_to_index
1568 LOGICAL :: found
1569 REAL(dp) :: new_force, r0, r1, ri(3), rj(3), &
1570 rref(3), scoord(3), x
1571 REAL(dp), ALLOCATABLE, DIMENSION(:, :) :: blk
1572 TYPE(cell_type), POINTER :: cell
1573 TYPE(dbt_iterator_type) :: iter
1574 TYPE(kpoint_type), POINTER :: kpoints
1575 TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
1576 TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
1577
1578 NULLIFY (qs_kind_set, particle_set, kpoints, index_to_cell, cell_to_index, cell)
1579
1580 CALL get_qs_env(qs_env, natom=natom, nkind=nkind, qs_kind_set=qs_kind_set, cell=cell, &
1581 kpoints=kpoints, particle_set=particle_set)
1582 CALL get_kpoint_info(kpoints, cell_to_index=cell_to_index, index_to_cell=index_to_cell)
1583
1584 CALL dbt_get_info(t_2c_in, nblks_total=nblks)
1585 cpassert(nblks(1) == ri_data%ncell_RI*natom)
1586 cpassert(nblks(2) == ri_data%ncell_RI*natom)
1587
1588 nimg = ri_data%nimg
1589
1590 !Loop over the RI cells and atoms, and apply bump accordingly
1591 r1 = ri_data%kp_RI_range
1592 r0 = ri_data%kp_bump_rad
1593 rref = pbc(particle_set(atom_i)%r, cell)
1594
1595 iat_of_kind = atom_of_kind(atom_i)
1596 ikind = kind_of(atom_i)
1597
1598!$OMP PARALLEL DEFAULT(NONE) SHARED(t_2c_in,natom,ri_data,cell,particle_set,index_to_cell,pref, &
1599!$OMP force,r0,r1,rref,atom_of_kind,kind_of,iat_of_kind,ikind,work_virial) &
1600!$OMP PRIVATE(iter,ind,blk,found,i_RI,i_img,iatom,j_RI,j_img,jatom,scoord,ri,rj,jkind,jat_of_kind, &
1601!$OMP new_force,i_xyz,i,x,j_xyz)
1602 CALL dbt_iterator_start(iter, t_2c_in)
1603 DO WHILE (dbt_iterator_blocks_left(iter))
1604 CALL dbt_iterator_next_block(iter, ind)
1605 IF (ind(1) /= ind(2)) cycle !bump matrix is diagonal
1606
1607 CALL dbt_get_block(t_2c_in, ind, blk, found)
1608 IF (.NOT. found) cycle
1609
1610 !bump is a function of x = SQRT((R - Rref)^2). We refer to R as jatom, and Rref as atom_i
1611 j_ri = (ind(2) - 1)/natom + 1
1612 j_img = ri_data%RI_cell_to_img(j_ri)
1613 jatom = ind(2) - (j_ri - 1)*natom
1614 jat_of_kind = atom_of_kind(jatom)
1615 jkind = kind_of(jatom)
1616
1617 CALL real_to_scaled(scoord, pbc(particle_set(jatom)%r, cell), cell)
1618 CALL scaled_to_real(rj, scoord(:) + index_to_cell(:, j_img), cell)
1619 x = norm2(rj - rref)
1620 IF (x < r0 .OR. x > r1) cycle
1621
1622 new_force = 0.0_dp
1623 DO i = 1, SIZE(blk, 1)
1624 new_force = new_force + blk(i, i)
1625 END DO
1626 new_force = pref*new_force*dbump(x, r0, r1)
1627
1628 !x = SQRT((R - Rref)^2), so we multiply by dx/dR and dx/dRref
1629 DO i_xyz = 1, 3
1630 !Force acting on second atom
1631!$OMP ATOMIC
1632 force(jkind)%fock_4c(i_xyz, jat_of_kind) = force(jkind)%fock_4c(i_xyz, jat_of_kind) + &
1633 new_force*(rj(i_xyz) - rref(i_xyz))/x
1634
1635 !virial acting on second atom
1636 CALL real_to_scaled(scoord, rj, cell)
1637 DO j_xyz = 1, 3
1638!$OMP ATOMIC
1639 work_virial(i_xyz, j_xyz) = work_virial(i_xyz, j_xyz) &
1640 + new_force*scoord(j_xyz)*(rj(i_xyz) - rref(i_xyz))/x
1641 END DO
1642
1643 !Force acting on reference atom, defining the RI basis
1644!$OMP ATOMIC
1645 force(ikind)%fock_4c(i_xyz, iat_of_kind) = force(ikind)%fock_4c(i_xyz, iat_of_kind) - &
1646 new_force*(rj(i_xyz) - rref(i_xyz))/x
1647
1648 !virial of ref atom
1649 CALL real_to_scaled(scoord, rref, cell)
1650 DO j_xyz = 1, 3
1651!$OMP ATOMIC
1652 work_virial(i_xyz, j_xyz) = work_virial(i_xyz, j_xyz) &
1653 - new_force*scoord(j_xyz)*(rj(i_xyz) - rref(i_xyz))/x
1654 END DO
1655 END DO !i_xyz
1656
1657 DEALLOCATE (blk)
1658 END DO
1659 CALL dbt_iterator_stop(iter)
1660!$OMP END PARALLEL
1661
1662 END SUBROUTINE get_2c_bump_forces
1663
1664! **************************************************************************************************
1665!> \brief The bumb function as defined by Juerg
1666!> \param x ...
1667!> \param r0 ...
1668!> \param r1 ...
1669!> \return ...
1670! **************************************************************************************************
1671 FUNCTION bump(x, r0, r1) RESULT(b)
1672 REAL(dp), INTENT(IN) :: x, r0, r1
1673 REAL(dp) :: b
1674
1675 REAL(dp) :: r
1676
1677 !Head-Gordon
1678 !b = 1.0_dp/(1.0_dp+EXP((r1-r0)/(r1-x)-(r1-r0)/(x-r0)))
1679 !Juerg
1680 r = (x - r0)/(r1 - r0)
1681 b = -6.0_dp*r**5 + 15.0_dp*r**4 - 10.0_dp*r**3 + 1.0_dp
1682 IF (x >= r1) b = 0.0_dp
1683 IF (x <= r0) b = 1.0_dp
1684
1685 END FUNCTION bump
1686
1687! **************************************************************************************************
1688!> \brief The derivative of the bump function
1689!> \param x ...
1690!> \param r0 ...
1691!> \param r1 ...
1692!> \return ...
1693! **************************************************************************************************
1694 FUNCTION dbump(x, r0, r1) RESULT(b)
1695 REAL(dp), INTENT(IN) :: x, r0, r1
1696 REAL(dp) :: b
1697
1698 REAL(dp) :: r
1699
1700 r = (x - r0)/(r1 - r0)
1701 b = (-30.0_dp*r**4 + 60.0_dp*r**3 - 30.0_dp*r**2)/(r1 - r0)
1702 IF (x >= r1) b = 0.0_dp
1703 IF (x <= r0) b = 0.0_dp
1704
1705 END FUNCTION dbump
1706
1707! **************************************************************************************************
1708!> \brief return the cell index a+c corresponding to given cell index i and b, with i = a+c-b
1709!> \param i_index ...
1710!> \param b_index ...
1711!> \param qs_env ...
1712!> \return ...
1713! **************************************************************************************************
1714 FUNCTION get_apc_index_from_ib(i_index, b_index, qs_env) RESULT(apc_index)
1715 INTEGER, INTENT(IN) :: i_index, b_index
1716 TYPE(qs_environment_type), POINTER :: qs_env
1717 INTEGER :: apc_index
1718
1719 INTEGER, DIMENSION(3) :: cell_apc
1720 INTEGER, DIMENSION(:, :), POINTER :: index_to_cell
1721 INTEGER, DIMENSION(:, :, :), POINTER :: cell_to_index
1722 TYPE(kpoint_type), POINTER :: kpoints
1723
1724 CALL get_qs_env(qs_env, kpoints=kpoints)
1725 CALL get_kpoint_info(kpoints, cell_to_index=cell_to_index, index_to_cell=index_to_cell)
1726
1727 !i = a+c-b => a+c = i+b
1728 cell_apc(:) = index_to_cell(:, i_index) + index_to_cell(:, b_index)
1729
1730 IF (any([cell_apc(1), cell_apc(2), cell_apc(3)] < lbound(cell_to_index)) .OR. &
1731 any([cell_apc(1), cell_apc(2), cell_apc(3)] > ubound(cell_to_index))) THEN
1732
1733 apc_index = 0
1734 ELSE
1735 apc_index = cell_to_index(cell_apc(1), cell_apc(2), cell_apc(3))
1736 END IF
1737
1738 END FUNCTION get_apc_index_from_ib
1739
1740! **************************************************************************************************
1741!> \brief return the cell index i corresponding to the summ of cell_a and cell_c
1742!> \param a_index ...
1743!> \param c_index ...
1744!> \param qs_env ...
1745!> \return ...
1746! **************************************************************************************************
1747 FUNCTION get_apc_index(a_index, c_index, qs_env) RESULT(i_index)
1748 INTEGER, INTENT(IN) :: a_index, c_index
1749 TYPE(qs_environment_type), POINTER :: qs_env
1750 INTEGER :: i_index
1751
1752 INTEGER, DIMENSION(3) :: cell_i
1753 INTEGER, DIMENSION(:, :), POINTER :: index_to_cell
1754 INTEGER, DIMENSION(:, :, :), POINTER :: cell_to_index
1755 TYPE(kpoint_type), POINTER :: kpoints
1756
1757 CALL get_qs_env(qs_env, kpoints=kpoints)
1758 CALL get_kpoint_info(kpoints, cell_to_index=cell_to_index, index_to_cell=index_to_cell)
1759
1760 cell_i(:) = index_to_cell(:, a_index) + index_to_cell(:, c_index)
1761
1762 IF (any([cell_i(1), cell_i(2), cell_i(3)] < lbound(cell_to_index)) .OR. &
1763 any([cell_i(1), cell_i(2), cell_i(3)] > ubound(cell_to_index))) THEN
1764
1765 i_index = 0
1766 ELSE
1767 i_index = cell_to_index(cell_i(1), cell_i(2), cell_i(3))
1768 END IF
1769
1770 END FUNCTION get_apc_index
1771
1772! **************************************************************************************************
1773!> \brief return the cell index i corresponding to the summ of cell_a + cell_c - cell_b
1774!> \param apc_index ...
1775!> \param b_index ...
1776!> \param qs_env ...
1777!> \return ...
1778! **************************************************************************************************
1779 FUNCTION get_i_index(apc_index, b_index, qs_env) RESULT(i_index)
1780 INTEGER, INTENT(IN) :: apc_index, b_index
1781 TYPE(qs_environment_type), POINTER :: qs_env
1782 INTEGER :: i_index
1783
1784 INTEGER, DIMENSION(3) :: cell_i
1785 INTEGER, DIMENSION(:, :), POINTER :: index_to_cell
1786 INTEGER, DIMENSION(:, :, :), POINTER :: cell_to_index
1787 TYPE(kpoint_type), POINTER :: kpoints
1788
1789 CALL get_qs_env(qs_env, kpoints=kpoints)
1790 CALL get_kpoint_info(kpoints, cell_to_index=cell_to_index, index_to_cell=index_to_cell)
1791
1792 cell_i(:) = index_to_cell(:, apc_index) - index_to_cell(:, b_index)
1793
1794 IF (any([cell_i(1), cell_i(2), cell_i(3)] < lbound(cell_to_index)) .OR. &
1795 any([cell_i(1), cell_i(2), cell_i(3)] > ubound(cell_to_index))) THEN
1796
1797 i_index = 0
1798 ELSE
1799 i_index = cell_to_index(cell_i(1), cell_i(2), cell_i(3))
1800 END IF
1801
1802 END FUNCTION get_i_index
1803
1804! **************************************************************************************************
1805!> \brief A routine that returns all allowed a,c pairs such that a+c images corresponds to the value
1806!> of the apc_index input. Takes into account that image a corresponds to 3c integrals, which
1807!> are ordered in their own way
1808!> \param ac_pairs ...
1809!> \param apc_index ...
1810!> \param ri_data ...
1811!> \param qs_env ...
1812! **************************************************************************************************
1813 SUBROUTINE get_ac_pairs(ac_pairs, apc_index, ri_data, qs_env)
1814 INTEGER, DIMENSION(:, :), INTENT(INOUT) :: ac_pairs
1815 INTEGER, INTENT(IN) :: apc_index
1816 TYPE(hfx_ri_type), INTENT(INOUT) :: ri_data
1817 TYPE(qs_environment_type), POINTER :: qs_env
1818
1819 INTEGER :: a_index, actual_img, c_index, nimg
1820
1821 nimg = SIZE(ac_pairs, 1)
1822
1823 ac_pairs(:, :) = 0
1824!$OMP PARALLEL DO DEFAULT(NONE) SHARED(ac_pairs,nimg,ri_data,qs_env,apc_index) &
1825!$OMP PRIVATE(a_index,actual_img,c_index)
1826 DO a_index = 1, nimg
1827 actual_img = ri_data%idx_to_img(a_index)
1828 !c = a+c - a
1829 c_index = get_i_index(apc_index, actual_img, qs_env)
1830 ac_pairs(a_index, 1) = a_index
1831 ac_pairs(a_index, 2) = c_index
1832 END DO
1833!$OMP END PARALLEL DO
1834
1835 END SUBROUTINE get_ac_pairs
1836
1837! **************************************************************************************************
1838!> \brief A routine that returns all allowed i,a+c pairs such that, for the given value of b, we have
1839!> i = a+c-b. Takes into account that image i corrsponds to the 3c ints, which are ordered in
1840!> their own way
1841!> \param iapc_pairs ...
1842!> \param b_index ...
1843!> \param ri_data ...
1844!> \param qs_env ...
1845!> \param actual_i_img ...
1846! **************************************************************************************************
1847 SUBROUTINE get_iapc_pairs(iapc_pairs, b_index, ri_data, qs_env, actual_i_img)
1848 INTEGER, DIMENSION(:, :), INTENT(INOUT) :: iapc_pairs
1849 INTEGER, INTENT(IN) :: b_index
1850 TYPE(hfx_ri_type), INTENT(INOUT) :: ri_data
1851 TYPE(qs_environment_type), POINTER :: qs_env
1852 INTEGER, DIMENSION(:), INTENT(INOUT), OPTIONAL :: actual_i_img
1853
1854 INTEGER :: actual_img, apc_index, i_index, nimg
1855
1856 nimg = SIZE(iapc_pairs, 1)
1857 IF (PRESENT(actual_i_img)) actual_i_img(:) = 0
1858
1859 iapc_pairs(:, :) = 0
1860!$OMP PARALLEL DO DEFAULT(NONE) SHARED(iapc_pairs,nimg,ri_data,qs_env,b_index,actual_i_img) &
1861!$OMP PRIVATE(i_index,actual_img,apc_index)
1862 DO i_index = 1, nimg
1863 actual_img = ri_data%idx_to_img(i_index)
1864 apc_index = get_apc_index_from_ib(actual_img, b_index, qs_env)
1865 IF (apc_index == 0) cycle
1866 iapc_pairs(i_index, 1) = i_index
1867 iapc_pairs(i_index, 2) = apc_index
1868 IF (PRESENT(actual_i_img)) actual_i_img(i_index) = actual_img
1869 END DO
1870
1871 END SUBROUTINE get_iapc_pairs
1872
1873! **************************************************************************************************
1874!> \brief A function that, given a cell index a, returun the index corresponding to -a, and zero if
1875!> if out of bounds
1876!> \param a_index ...
1877!> \param qs_env ...
1878!> \return ...
1879! **************************************************************************************************
1880 FUNCTION get_opp_index(a_index, qs_env) RESULT(opp_index)
1881 INTEGER, INTENT(IN) :: a_index
1882 TYPE(qs_environment_type), POINTER :: qs_env
1883 INTEGER :: opp_index
1884
1885 INTEGER, DIMENSION(3) :: opp_cell
1886 INTEGER, DIMENSION(:, :), POINTER :: index_to_cell
1887 INTEGER, DIMENSION(:, :, :), POINTER :: cell_to_index
1888 TYPE(kpoint_type), POINTER :: kpoints
1889
1890 NULLIFY (kpoints, cell_to_index, index_to_cell)
1891
1892 CALL get_qs_env(qs_env, kpoints=kpoints)
1893 CALL get_kpoint_info(kpoints, cell_to_index=cell_to_index, index_to_cell=index_to_cell)
1894
1895 opp_cell(:) = -index_to_cell(:, a_index)
1896
1897 IF (any([opp_cell(1), opp_cell(2), opp_cell(3)] < lbound(cell_to_index)) .OR. &
1898 any([opp_cell(1), opp_cell(2), opp_cell(3)] > ubound(cell_to_index))) THEN
1899
1900 opp_index = 0
1901 ELSE
1902 opp_index = cell_to_index(opp_cell(1), opp_cell(2), opp_cell(3))
1903 END IF
1904
1905 END FUNCTION get_opp_index
1906
1907! **************************************************************************************************
1908!> \brief A routine that returns the actual non-symemtric density matrix for each image, by Fourier
1909!> transforming the kpoint density matrix
1910!> \param rho_ao_t ...
1911!> \param rho_ao ...
1912!> \param scale_prev_p ...
1913!> \param ri_data ...
1914!> \param qs_env ...
1915! **************************************************************************************************
1916 SUBROUTINE get_pmat_images(rho_ao_t, rho_ao, scale_prev_p, ri_data, qs_env)
1917 TYPE(dbt_type), DIMENSION(:, :), INTENT(INOUT) :: rho_ao_t
1918 TYPE(dbcsr_p_type), DIMENSION(:, :), INTENT(INOUT) :: rho_ao
1919 REAL(dp), INTENT(IN) :: scale_prev_p
1920 TYPE(hfx_ri_type), INTENT(INOUT) :: ri_data
1921 TYPE(qs_environment_type), POINTER :: qs_env
1922
1923 INTEGER :: cell_j(3), i_img, i_spin, iatom, icol, &
1924 irow, j_img, jatom, mi_img, mj_img, &
1925 nimg, nspins
1926 INTEGER, DIMENSION(:, :, :), POINTER :: cell_to_index
1927 LOGICAL :: found
1928 REAL(dp) :: fac
1929 REAL(dp), DIMENSION(:, :), POINTER :: pblock, pblock_desymm
1930 TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_ks, rho_desymm
1931 TYPE(dbt_type) :: tmp
1932 TYPE(dft_control_type), POINTER :: dft_control
1933 TYPE(kpoint_type), POINTER :: kpoints
1935 DIMENSION(:), POINTER :: nl_iterator
1936 TYPE(neighbor_list_set_p_type), DIMENSION(:), &
1937 POINTER :: sab_aux_fit, sab_aux_fit_asymm, sab_nl, &
1938 sab_nl_nosym
1939 TYPE(qs_scf_env_type), POINTER :: scf_env
1940
1941 NULLIFY (rho_desymm, kpoints, sab_nl_nosym, scf_env, matrix_ks, dft_control, &
1942 sab_nl, sab_aux_fit, sab_aux_fit_asymm, nl_iterator, cell_to_index, &
1943 pblock, pblock_desymm)
1944
1945 CALL get_qs_env(qs_env, kpoints=kpoints, scf_env=scf_env, matrix_ks_kp=matrix_ks, dft_control=dft_control)
1946 CALL get_kpoint_info(kpoints, sab_nl_nosym=sab_nl_nosym, cell_to_index=cell_to_index, sab_nl=sab_nl)
1947
1948 IF (dft_control%do_admm) THEN
1949 CALL get_admm_env(qs_env%admm_env, matrix_ks_aux_fit_kp=matrix_ks, &
1950 sab_aux_fit=sab_aux_fit, sab_aux_fit_asymm=sab_aux_fit_asymm)
1951 IF (ASSOCIATED(sab_aux_fit) .AND. ASSOCIATED(sab_aux_fit_asymm)) THEN
1952 sab_nl => sab_aux_fit
1953 sab_nl_nosym => sab_aux_fit_asymm
1954 END IF
1955 END IF
1956
1957 nspins = SIZE(matrix_ks, 1)
1958 nimg = ri_data%nimg
1959
1960 ALLOCATE (rho_desymm(nspins, nimg))
1961 DO i_img = 1, nimg
1962 DO i_spin = 1, nspins
1963 ALLOCATE (rho_desymm(i_spin, i_img)%matrix)
1964 CALL dbcsr_create(rho_desymm(i_spin, i_img)%matrix, template=matrix_ks(i_spin, i_img)%matrix, &
1965 matrix_type=dbcsr_type_no_symmetry)
1966 CALL cp_dbcsr_alloc_block_from_nbl(rho_desymm(i_spin, i_img)%matrix, sab_nl_nosym)
1967 END DO
1968 END DO
1969 CALL dbt_create(rho_desymm(1, 1)%matrix, tmp)
1970
1971 !We transfor the symmtric typed (but not actually symmetric: P_ab^i = P_ba^-i) real-spaced density
1972 !matrix into proper non-symemtric ones (using the same nl for consistency)
1973 CALL neighbor_list_iterator_create(nl_iterator, sab_nl)
1974 DO WHILE (neighbor_list_iterate(nl_iterator) == 0)
1975 CALL get_iterator_info(nl_iterator, iatom=iatom, jatom=jatom, cell=cell_j)
1976 j_img = cell_to_index(cell_j(1), cell_j(2), cell_j(3))
1977 IF (j_img > nimg .OR. j_img < 1) cycle
1978
1979 fac = 1.0_dp
1980 IF (iatom == jatom) fac = 0.5_dp
1981 mj_img = get_opp_index(j_img, qs_env)
1982 !if no opposite image, then no sum of P^j + P^-j => need full diag
1983 IF (mj_img == 0) fac = 1.0_dp
1984
1985 irow = iatom
1986 icol = jatom
1987 IF (iatom > jatom) THEN
1988 !because symmetric nl. Value for atom pair i,j is actually stored in j,i if i > j
1989 irow = jatom
1990 icol = iatom
1991 END IF
1992
1993 DO i_spin = 1, nspins
1994 CALL dbcsr_get_block_p(rho_ao(i_spin, j_img)%matrix, irow, icol, pblock, found)
1995 IF (.NOT. found) cycle
1996
1997 !distribution of symm and non-symm matrix match in that way
1998 CALL dbcsr_get_block_p(rho_desymm(i_spin, j_img)%matrix, iatom, jatom, pblock_desymm, found)
1999 IF (.NOT. found) cycle
2000
2001 IF (iatom > jatom) THEN
2002 pblock_desymm(:, :) = fac*transpose(pblock(:, :))
2003 ELSE
2004 pblock_desymm(:, :) = fac*pblock(:, :)
2005 END IF
2006 END DO
2007 END DO
2008 CALL neighbor_list_iterator_release(nl_iterator)
2009
2010 DO i_img = 1, nimg
2011 DO i_spin = 1, nspins
2012 CALL dbt_scale(rho_ao_t(i_spin, i_img), scale_prev_p)
2013
2014 CALL dbt_copy_matrix_to_tensor(rho_desymm(i_spin, i_img)%matrix, tmp)
2015 CALL dbt_copy(tmp, rho_ao_t(i_spin, i_img), summation=.true., move_data=.true.)
2016
2017 !symmetrize by addin transpose of opp img
2018 mi_img = get_opp_index(i_img, qs_env)
2019 IF (mi_img > 0 .AND. mi_img <= nimg) THEN
2020 CALL dbt_copy_matrix_to_tensor(rho_desymm(i_spin, mi_img)%matrix, tmp)
2021 CALL dbt_copy(tmp, rho_ao_t(i_spin, i_img), order=[2, 1], summation=.true., move_data=.true.)
2022 END IF
2023 CALL dbt_filter(rho_ao_t(i_spin, i_img), ri_data%filter_eps)
2024 END DO
2025 END DO
2026
2027 DO i_img = 1, nimg
2028 DO i_spin = 1, nspins
2029 CALL dbcsr_release(rho_desymm(i_spin, i_img)%matrix)
2030 DEALLOCATE (rho_desymm(i_spin, i_img)%matrix)
2031 END DO
2032 END DO
2033
2034 CALL dbt_destroy(tmp)
2035 DEALLOCATE (rho_desymm)
2036
2037 END SUBROUTINE get_pmat_images
2038
2039! **************************************************************************************************
2040!> \brief A routine that, given a cell index b and atom indices ij, returns a 2c tensor with the HFX
2041!> potential (P_i^0|Q_j^b), within the extended RI basis
2042!> \param t_2c_pot ...
2043!> \param mat_orig ...
2044!> \param atom_i ...
2045!> \param atom_j ...
2046!> \param img_b ...
2047!> \param ri_data ...
2048!> \param qs_env ...
2049!> \param do_inverse ...
2050!> \param para_env_ext ...
2051!> \param blacs_env_ext ...
2052!> \param dbcsr_template ...
2053!> \param off_diagonal ...
2054!> \param skip_inverse ...
2055! **************************************************************************************************
2056 SUBROUTINE get_ext_2c_int(t_2c_pot, mat_orig, atom_i, atom_j, img_b, ri_data, qs_env, do_inverse, &
2057 para_env_ext, blacs_env_ext, dbcsr_template, off_diagonal, skip_inverse)
2058 TYPE(dbt_type), INTENT(INOUT) :: t_2c_pot
2059 TYPE(dbcsr_type), DIMENSION(:), INTENT(INOUT) :: mat_orig
2060 INTEGER, INTENT(IN) :: atom_i, atom_j, img_b
2061 TYPE(hfx_ri_type), INTENT(INOUT) :: ri_data
2062 TYPE(qs_environment_type), POINTER :: qs_env
2063 LOGICAL, INTENT(IN), OPTIONAL :: do_inverse
2064 TYPE(mp_para_env_type), OPTIONAL, POINTER :: para_env_ext
2065 TYPE(cp_blacs_env_type), OPTIONAL, POINTER :: blacs_env_ext
2066 TYPE(dbcsr_type), OPTIONAL, POINTER :: dbcsr_template
2067 LOGICAL, INTENT(IN), OPTIONAL :: off_diagonal, skip_inverse
2068
2069 CHARACTER(LEN=*), PARAMETER :: routinen = 'get_ext_2c_int'
2070
2071 INTEGER :: group, handle, handle2, i_img, i_ri, iatom, iblk, ikind, img_tot, j_img, j_ri, &
2072 jatom, jblk, jkind, n_dependent, natom, nblks_ri, nimg, nkind
2073 INTEGER, ALLOCATABLE, DIMENSION(:) :: dist1, dist2
2074 INTEGER, ALLOCATABLE, DIMENSION(:, :) :: present_atoms_i, present_atoms_j
2075 INTEGER, DIMENSION(3) :: cell_b, cell_i, cell_j, cell_tot
2076 INTEGER, DIMENSION(:), POINTER :: col_dist, col_dist_ext, ri_blk_size_ext, &
2077 row_dist, row_dist_ext
2078 INTEGER, DIMENSION(:, :), POINTER :: index_to_cell, pgrid
2079 INTEGER, DIMENSION(:, :, :), POINTER :: cell_to_index
2080 LOGICAL :: do_inverse_prv, found, my_offd, &
2081 skip_inverse_prv, use_template
2082 REAL(dp) :: bfac, dij, r0, r1, threshold
2083 REAL(dp), DIMENSION(3) :: ri, rij, rj, rref, scoord
2084 REAL(dp), DIMENSION(:, :), POINTER :: pblock
2085 TYPE(cell_type), POINTER :: cell
2086 TYPE(cp_blacs_env_type), POINTER :: blacs_env
2087 TYPE(dbcsr_distribution_type) :: dbcsr_dist, dbcsr_dist_ext
2088 TYPE(dbcsr_iterator_type) :: dbcsr_iter
2089 TYPE(dbcsr_type) :: work, work_tight, work_tight_inv
2090 TYPE(dbt_type) :: t_2c_tmp
2091 TYPE(distribution_2d_type), POINTER :: dist_2d
2092 TYPE(gto_basis_set_p_type), ALLOCATABLE, &
2093 DIMENSION(:), TARGET :: basis_set_ri
2094 TYPE(kpoint_type), POINTER :: kpoints
2095 TYPE(mp_para_env_type), POINTER :: para_env
2097 DIMENSION(:), POINTER :: nl_iterator
2098 TYPE(neighbor_list_set_p_type), DIMENSION(:), &
2099 POINTER :: nl_2c
2100 TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
2101 TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
2102
2103 NULLIFY (qs_kind_set, nl_2c, nl_iterator, cell, kpoints, cell_to_index, index_to_cell, dist_2d, &
2104 para_env, pblock, blacs_env, particle_set, col_dist, row_dist, pgrid, &
2105 col_dist_ext, row_dist_ext)
2106
2107 CALL timeset(routinen, handle)
2108
2109 !Idea: run over the neighbor list once for i and once for j, and record in which cell the MIC
2110 ! atoms are. Then loop over the atoms and only take the pairs the we need
2111
2112 CALL get_qs_env(qs_env, natom=natom, nkind=nkind, qs_kind_set=qs_kind_set, cell=cell, &
2113 kpoints=kpoints, para_env=para_env, blacs_env=blacs_env, particle_set=particle_set)
2114 CALL get_kpoint_info(kpoints, cell_to_index=cell_to_index, index_to_cell=index_to_cell)
2115
2116 do_inverse_prv = .false.
2117 IF (PRESENT(do_inverse)) do_inverse_prv = do_inverse
2118 IF (do_inverse_prv) THEN
2119 cpassert(atom_i == atom_j)
2120 END IF
2121
2122 skip_inverse_prv = .false.
2123 IF (PRESENT(skip_inverse)) skip_inverse_prv = skip_inverse
2124
2125 my_offd = .false.
2126 IF (PRESENT(off_diagonal)) my_offd = off_diagonal
2127
2128 IF (PRESENT(para_env_ext)) para_env => para_env_ext
2129 IF (PRESENT(blacs_env_ext)) blacs_env => blacs_env_ext
2130
2131 nimg = SIZE(mat_orig)
2132
2133 CALL timeset(routinen//"_nl_iter", handle2)
2134
2135 !create our own dist_2d in the subgroup
2136 ALLOCATE (dist1(natom), dist2(natom))
2137 DO iatom = 1, natom
2138 dist1(iatom) = mod(iatom, blacs_env%num_pe(1))
2139 dist2(iatom) = mod(iatom, blacs_env%num_pe(2))
2140 END DO
2141 CALL distribution_2d_create(dist_2d, dist1, dist2, nkind, particle_set, blacs_env_ext=blacs_env)
2142
2143 ALLOCATE (basis_set_ri(nkind))
2144 CALL basis_set_list_setup(basis_set_ri, ri_data%ri_basis_type, qs_kind_set)
2145
2146 CALL build_2c_neighbor_lists(nl_2c, basis_set_ri, basis_set_ri, ri_data%ri_metric, &
2147 "HFX_2c_nl_RI", qs_env, sym_ij=.false., dist_2d=dist_2d)
2148
2149 ALLOCATE (present_atoms_i(natom, nimg), present_atoms_j(natom, nimg))
2150 present_atoms_i = 0
2151 present_atoms_j = 0
2152
2153 CALL neighbor_list_iterator_create(nl_iterator, nl_2c)
2154 DO WHILE (neighbor_list_iterate(nl_iterator) == 0)
2155 CALL get_iterator_info(nl_iterator, iatom=iatom, jatom=jatom, r=rij, cell=cell_j, &
2156 ikind=ikind, jkind=jkind)
2157
2158 dij = norm2(rij)
2159
2160 j_img = cell_to_index(cell_j(1), cell_j(2), cell_j(3))
2161 IF (j_img > nimg .OR. j_img < 1) cycle
2162
2163 IF (iatom == atom_i .AND. dij <= ri_data%kp_RI_range) present_atoms_i(jatom, j_img) = 1
2164 IF (iatom == atom_j .AND. dij <= ri_data%kp_RI_range) present_atoms_j(jatom, j_img) = 1
2165 END DO
2166 CALL neighbor_list_iterator_release(nl_iterator)
2167 CALL release_neighbor_list_sets(nl_2c)
2168 CALL distribution_2d_release(dist_2d)
2169 CALL timestop(handle2)
2170
2171 CALL para_env%sum(present_atoms_i)
2172 CALL para_env%sum(present_atoms_j)
2173
2174 ! Retain the local RI basis used by the metric inverse for selecting G rows.
2175 IF (do_inverse_prv .AND. .NOT. skip_inverse_prv) THEN
2176 DO i_img = 1, nimg
2177 i_ri = ri_data%img_to_RI_cell(i_img)
2178 IF (i_ri == 0) cycle
2179 ri_data%kp_RI_neighbors((i_ri - 1)*natom + 1:i_ri*natom, atom_i) = present_atoms_i(:, i_img) > 0
2180 END DO
2181 END IF
2182
2183 !Need to build a work matrix with matching distribution to mat_orig
2184 !If template is provided, use it. If not, we create it.
2185 use_template = .false.
2186 IF (PRESENT(dbcsr_template)) THEN
2187 IF (ASSOCIATED(dbcsr_template)) use_template = .true.
2188 END IF
2189
2190 IF (use_template) THEN
2191 CALL dbcsr_create(work, template=dbcsr_template)
2192 ELSE
2193 CALL dbcsr_get_info(mat_orig(1), distribution=dbcsr_dist)
2194 CALL dbcsr_distribution_get(dbcsr_dist, row_dist=row_dist, col_dist=col_dist, group=group, pgrid=pgrid)
2195 ALLOCATE (row_dist_ext(ri_data%ncell_RI*natom), col_dist_ext(ri_data%ncell_RI*natom))
2196 ALLOCATE (ri_blk_size_ext(ri_data%ncell_RI*natom))
2197 DO i_ri = 1, ri_data%ncell_RI
2198 row_dist_ext((i_ri - 1)*natom + 1:i_ri*natom) = row_dist(:)
2199 col_dist_ext((i_ri - 1)*natom + 1:i_ri*natom) = col_dist(:)
2200 ri_blk_size_ext((i_ri - 1)*natom + 1:i_ri*natom) = ri_data%bsizes_RI(:)
2201 END DO
2202
2203 CALL dbcsr_distribution_new(dbcsr_dist_ext, group=group, pgrid=pgrid, &
2204 row_dist=row_dist_ext, col_dist=col_dist_ext)
2205 CALL dbcsr_create(work, dist=dbcsr_dist_ext, name="RI_ext", matrix_type=dbcsr_type_no_symmetry, &
2206 row_blk_size=ri_blk_size_ext, col_blk_size=ri_blk_size_ext)
2207 CALL dbcsr_distribution_release(dbcsr_dist_ext)
2208 DEALLOCATE (col_dist_ext, row_dist_ext, ri_blk_size_ext)
2209
2210 IF (PRESENT(dbcsr_template)) THEN
2211 ALLOCATE (dbcsr_template)
2212 CALL dbcsr_create(dbcsr_template, template=work)
2213 END IF
2214 END IF !use_template
2215
2216 cell_b(:) = index_to_cell(:, img_b)
2217 DO i_img = 1, nimg
2218 i_ri = ri_data%img_to_RI_cell(i_img)
2219 IF (i_ri == 0) cycle
2220 cell_i(:) = index_to_cell(:, i_img)
2221 DO j_img = 1, nimg
2222 j_ri = ri_data%img_to_RI_cell(j_img)
2223 IF (j_ri == 0) cycle
2224 cell_j(:) = index_to_cell(:, j_img)
2225 cell_tot = cell_j - cell_i + cell_b
2226
2227 IF (any([cell_tot(1), cell_tot(2), cell_tot(3)] < lbound(cell_to_index)) .OR. &
2228 any([cell_tot(1), cell_tot(2), cell_tot(3)] > ubound(cell_to_index))) cycle
2229 img_tot = cell_to_index(cell_tot(1), cell_tot(2), cell_tot(3))
2230 IF (img_tot > nimg .OR. img_tot < 1) cycle
2231
2232 CALL dbcsr_iterator_start(dbcsr_iter, mat_orig(img_tot))
2233 DO WHILE (dbcsr_iterator_blocks_left(dbcsr_iter))
2234 CALL dbcsr_iterator_next_block(dbcsr_iter, row=iatom, column=jatom)
2235 IF (present_atoms_i(iatom, i_img) == 0) cycle
2236 IF (present_atoms_j(jatom, j_img) == 0) cycle
2237 IF (my_offd .AND. (i_ri - 1)*natom + iatom == (j_ri - 1)*natom + jatom) cycle
2238
2239 CALL dbcsr_get_block_p(mat_orig(img_tot), iatom, jatom, pblock, found)
2240 IF (.NOT. found) cycle
2241
2242 CALL dbcsr_put_block(work, (i_ri - 1)*natom + iatom, (j_ri - 1)*natom + jatom, pblock)
2243
2244 END DO
2245 CALL dbcsr_iterator_stop(dbcsr_iter)
2246
2247 END DO !j_img
2248 END DO !i_img
2249 CALL dbcsr_finalize(work)
2250
2251 IF (do_inverse_prv) THEN
2252
2253 r1 = ri_data%kp_RI_range
2254 r0 = ri_data%kp_bump_rad
2255
2256 !Because there are a lot of empty rows/cols in work, we need to get rid of them for inversion
2257 nblks_ri = sum(present_atoms_i)
2258 ALLOCATE (col_dist_ext(nblks_ri), row_dist_ext(nblks_ri), ri_blk_size_ext(nblks_ri))
2259 iblk = 0
2260 DO i_img = 1, nimg
2261 i_ri = ri_data%img_to_RI_cell(i_img)
2262 IF (i_ri == 0) cycle
2263 DO iatom = 1, natom
2264 IF (present_atoms_i(iatom, i_img) == 0) cycle
2265 iblk = iblk + 1
2266 col_dist_ext(iblk) = col_dist(iatom)
2267 row_dist_ext(iblk) = row_dist(iatom)
2268 ri_blk_size_ext(iblk) = ri_data%bsizes_RI(iatom)
2269 END DO
2270 END DO
2271
2272 CALL dbcsr_distribution_new(dbcsr_dist_ext, group=group, pgrid=pgrid, &
2273 row_dist=row_dist_ext, col_dist=col_dist_ext)
2274 CALL dbcsr_create(work_tight, dist=dbcsr_dist_ext, name="RI_ext", matrix_type=dbcsr_type_no_symmetry, &
2275 row_blk_size=ri_blk_size_ext, col_blk_size=ri_blk_size_ext)
2276 CALL dbcsr_create(work_tight_inv, dist=dbcsr_dist_ext, name="RI_ext", matrix_type=dbcsr_type_no_symmetry, &
2277 row_blk_size=ri_blk_size_ext, col_blk_size=ri_blk_size_ext)
2278 CALL dbcsr_distribution_release(dbcsr_dist_ext)
2279 DEALLOCATE (col_dist_ext, row_dist_ext, ri_blk_size_ext)
2280
2281 !We apply a bump function to the RI metric inverse for smooth RI basis extension:
2282 ! S^-1 = B * ((P|Q)_D + B*(P|Q)_OD*B)^-1 * B, with D block-diagonal blocks and OD off-diagonal
2283 rref = pbc(particle_set(atom_i)%r, cell)
2284
2285 iblk = 0
2286 DO i_img = 1, nimg
2287 i_ri = ri_data%img_to_RI_cell(i_img)
2288 IF (i_ri == 0) cycle
2289 DO iatom = 1, natom
2290 IF (present_atoms_i(iatom, i_img) == 0) cycle
2291 iblk = iblk + 1
2292
2293 CALL real_to_scaled(scoord, pbc(particle_set(iatom)%r, cell), cell)
2294 CALL scaled_to_real(ri, scoord(:) + index_to_cell(:, i_img), cell)
2295
2296 jblk = 0
2297 DO j_img = 1, nimg
2298 j_ri = ri_data%img_to_RI_cell(j_img)
2299 IF (j_ri == 0) cycle
2300 DO jatom = 1, natom
2301 IF (present_atoms_j(jatom, j_img) == 0) cycle
2302 jblk = jblk + 1
2303
2304 CALL real_to_scaled(scoord, pbc(particle_set(jatom)%r, cell), cell)
2305 CALL scaled_to_real(rj, scoord(:) + index_to_cell(:, j_img), cell)
2306
2307 CALL dbcsr_get_block_p(work, (i_ri - 1)*natom + iatom, (j_ri - 1)*natom + jatom, pblock, found)
2308 IF (.NOT. found) cycle
2309
2310 bfac = 1.0_dp
2311 IF (iblk /= jblk) bfac = bump(norm2(ri - rref), r0, r1)*bump(norm2(rj - rref), r0, r1)
2312 CALL dbcsr_put_block(work_tight, iblk, jblk, bfac*pblock(:, :))
2313 END DO
2314 END DO
2315 END DO
2316 END DO
2317 CALL dbcsr_finalize(work_tight)
2318 CALL dbcsr_clear(work)
2319
2320 IF (.NOT. skip_inverse_prv) THEN
2321 SELECT CASE (ri_data%t2c_method)
2322 CASE (hfx_ri_do_2c_iter)
2323 threshold = max(ri_data%filter_eps, 1.0e-12_dp)
2324 CALL invert_hotelling(work_tight_inv, work_tight, threshold=threshold, silent=.false.)
2326 CALL dbcsr_copy(work_tight_inv, work_tight)
2327 CALL cp_dbcsr_cholesky_decompose(work_tight_inv, para_env=para_env, blacs_env=blacs_env)
2328 CALL cp_dbcsr_cholesky_invert(work_tight_inv, para_env=para_env, blacs_env=blacs_env, &
2329 uplo_to_full=.true.)
2330 CASE (hfx_ri_do_2c_diag)
2331 CALL dbcsr_copy(work_tight_inv, work_tight)
2332 CALL cp_dbcsr_power(work_tight_inv, -1.0_dp, ri_data%eps_eigval, n_dependent, &
2333 para_env, blacs_env, verbose=ri_data%unit_nr_dbcsr > 0)
2334 END SELECT
2335 ELSE
2336 CALL dbcsr_copy(work_tight_inv, work_tight)
2337 END IF
2338
2339 !move back data to standard extended RI pattern
2340 !Note: we apply the external bump to ((P|Q)_D + B*(P|Q)_OD*B)^-1 later, because this matrix
2341 ! is required for forces
2342 iblk = 0
2343 DO i_img = 1, nimg
2344 i_ri = ri_data%img_to_RI_cell(i_img)
2345 IF (i_ri == 0) cycle
2346 DO iatom = 1, natom
2347 IF (present_atoms_i(iatom, i_img) == 0) cycle
2348 iblk = iblk + 1
2349
2350 jblk = 0
2351 DO j_img = 1, nimg
2352 j_ri = ri_data%img_to_RI_cell(j_img)
2353 IF (j_ri == 0) cycle
2354 DO jatom = 1, natom
2355 IF (present_atoms_j(jatom, j_img) == 0) cycle
2356 jblk = jblk + 1
2357
2358 CALL dbcsr_get_block_p(work_tight_inv, iblk, jblk, pblock, found)
2359 IF (.NOT. found) cycle
2360
2361 CALL dbcsr_put_block(work, (i_ri - 1)*natom + iatom, (j_ri - 1)*natom + jatom, pblock)
2362 END DO
2363 END DO
2364 END DO
2365 END DO
2366 CALL dbcsr_finalize(work)
2367
2368 CALL dbcsr_release(work_tight)
2369 CALL dbcsr_release(work_tight_inv)
2370 END IF
2371
2372 CALL dbt_create(work, t_2c_tmp)
2373 CALL dbt_copy_matrix_to_tensor(work, t_2c_tmp)
2374 CALL dbt_copy(t_2c_tmp, t_2c_pot, move_data=.true.)
2375 CALL dbt_filter(t_2c_pot, ri_data%filter_eps)
2376
2377 CALL dbt_destroy(t_2c_tmp)
2378 CALL dbcsr_release(work)
2379
2380 CALL timestop(handle)
2381
2382 END SUBROUTINE get_ext_2c_int
2383
2384! **************************************************************************************************
2385!> \brief Build the unique, density-independent potential of the fitted AO pairs.
2386!> G_j(P^x; nu^0,lambda^l) = sum_Qq V_PQ(q-x) B_j(Q^q;nu^0,lambda^l).
2387!> A KS block (i,j,b) only selects rows x=p-b of this object. Requests
2388!> from overlapping local RI bases and different b images are combined.
2389!> \param ri_data integral storage; G is invalidated together with the geometry
2390!> \param qs_env simulation environment
2391!> \param pattern selected KS atom pairs; negative entries are absent
2392!> \param ngroups number of equal-sized contraction subgroups
2393!> \param para_env_sub communicator of the local subgroup
2394! **************************************************************************************************
2395 SUBROUTINE build_kp_exchange_integrals(ri_data, qs_env, pattern, ngroups, para_env_sub)
2396 TYPE(hfx_ri_type), INTENT(INOUT) :: ri_data
2397 TYPE(qs_environment_type), POINTER :: qs_env
2398 INTEGER, INTENT(IN) :: pattern(:, :, :), ngroups
2399 TYPE(mp_para_env_type), INTENT(IN) :: para_env_sub
2400
2401 CHARACTER(LEN=*), PARAMETER :: routinen = 'build_kp_exchange_integrals'
2402
2403 INTEGER :: b, batch_size, cell_p(3), cell_x(3), first, g, group, handle, hi(3), i, iatom, &
2404 img, j, jatom, k, last, lo(3), nao, natom, nblocks, ng, nimg, nri, p, p_atom, pcoord(3), &
2405 pdims(3), q, q_atom, slot
2406 INTEGER(int_8) :: nflop
2407 INTEGER, ALLOCATABLE :: ao_pairs(:, :), cell_index(:, :, :), cells_g(:, :), dist1(:), &
2408 dist2(:), dist3(:), idx_to_atom(:), indices(:), pairs(:, :), sizes_g_split(:), &
2409 sizes_ri_split(:), sizes_stack(:), stack_dist(:)
2410 INTEGER, POINTER :: blocks(:, :), cell_to_index(:, :, :), col_dist(:), col_ext(:), &
2411 grid(:, :), index_to_cell(:, :), row_dist(:), row_ext(:), sizes_g(:), sizes_ri(:)
2412 LOGICAL :: found
2413 LOGICAL, ALLOCATABLE :: needed(:, :), valid_images(:, :)
2414 REAL(dp), POINTER :: block(:, :)
2415 TYPE(dbcsr_distribution_type) :: extended_dist, matrix_dist
2416 TYPE(dbcsr_iterator_type) :: matrix_iter
2417 TYPE(dbcsr_type) :: potential
2418 TYPE(dbt_distribution_type) :: tensor_dist
2419 TYPE(dbt_pgrid_type) :: pgrid_sub
2420 TYPE(dbt_type) :: b_stack, g_main, g_stack, v_atom, v_split
2421 TYPE(kpoint_type), POINTER :: kp
2422 TYPE(mp_para_env_type), POINTER :: para_env
2423
2424 NULLIFY (block, blocks, cell_to_index, col_dist, col_ext, grid, index_to_cell, kp, &
2425 para_env, row_dist, row_ext, sizes_g, sizes_ri)
2426
2427 CALL timeset(routinen, handle)
2428 CALL get_qs_env(qs_env, natom=natom, kpoints=kp, para_env=para_env)
2429 CALL get_kpoint_info(kp, index_to_cell=index_to_cell, cell_to_index=cell_to_index)
2430 nimg = ri_data%nimg
2431 nao = SIZE(ri_data%bsizes_AO_split)
2432 nri = SIZE(ri_data%bsizes_RI_split)
2433 batch_size = ri_data%kp_stack_size
2434
2435 ! get_ext_2c_int retained the exact RI basis used by each metric inverse.
2436 ALLOCATE (ri_data%kp_RI_atom(nri))
2437 CALL get_idx_to_atom(ri_data%kp_RI_atom, ri_data%bsizes_RI_split, ri_data%bsizes_RI)
2438
2439 ! Avoid ifx miscompilation of rank-reducing MINVAL/MAXVAL expressions
2440 ! used to define allocation bounds. Each scalar reduction gives the same range.
2441 DO i = 1, 3
2442 lo(i) = minval(index_to_cell(i, ri_data%RI_cell_to_img)) - maxval(index_to_cell(i, 1:nimg))
2443 hi(i) = maxval(index_to_cell(i, ri_data%RI_cell_to_img)) - minval(index_to_cell(i, 1:nimg))
2444 END DO
2445 ALLOCATE (cell_index(lo(1):hi(1), lo(2):hi(2), lo(3):hi(3)))
2446 cell_index = 0
2447 DO b = 1, nimg
2448 DO iatom = 1, natom
2449 IF (all(pattern(iatom, :, b) < 0)) cycle
2450 DO p = 1, ri_data%ncell_RI
2451 IF (.NOT. any(ri_data%kp_RI_neighbors((p - 1)*natom + 1:p*natom, iatom))) cycle
2452 cell_x = index_to_cell(:, ri_data%RI_cell_to_img(p)) - index_to_cell(:, b)
2453 cell_index(cell_x(1), cell_x(2), cell_x(3)) = 1
2454 END DO
2455 END DO
2456 END DO
2457 ! Keep a valid empty tensor layout even if the exchange operator is zero.
2458 IF (count(cell_index /= 0) == 0) cell_index(0, 0, 0) = 1
2459 ng = count(cell_index /= 0)
2460 ALLOCATE (cells_g(3, ng), ri_data%kp_G_to_RI(ng, nimg))
2461 g = 0
2462 DO k = lo(3), hi(3)
2463 DO j = lo(2), hi(2)
2464 DO i = lo(1), hi(1)
2465 IF (cell_index(i, j, k) == 0) cycle
2466 g = g + 1
2467 cell_index(i, j, k) = g
2468 cells_g(:, g) = [i, j, k]
2469 END DO
2470 END DO
2471 END DO
2472 ri_data%kp_G_to_RI = 0
2473 DO b = 1, nimg
2474 DO g = 1, ng
2475 cell_p = cells_g(:, g) + index_to_cell(:, b)
2476 IF (any(cell_p < lbound(cell_to_index)) .OR. any(cell_p > ubound(cell_to_index))) cycle
2477 img = cell_to_index(cell_p(1), cell_p(2), cell_p(3))
2478 IF (img > 0 .AND. img <= nimg) ri_data%kp_G_to_RI(g, b) = ri_data%img_to_RI_cell(img)
2479 END DO
2480 END DO
2481
2482 ALLOCATE (sizes_g(ng*natom), sizes_g_split(ng*nri), sizes_ri(ri_data%ncell_RI*natom), &
2483 sizes_ri_split(ri_data%ncell_RI*nri))
2484 sizes_g(:) = reshape(spread(ri_data%bsizes_RI, 2, ng), [ng*natom])
2485 sizes_g_split(:) = reshape(spread(ri_data%bsizes_RI_split, 2, ng), [ng*nri])
2486 sizes_ri(:) = reshape(spread(ri_data%bsizes_RI, 2, ri_data%ncell_RI), [SIZE(sizes_ri)])
2487 sizes_ri_split(:) = reshape(spread(ri_data%bsizes_RI_split, 2, ri_data%ncell_RI), [SIZE(sizes_ri_split)])
2488
2489 ! V(P^x,Q^q) uses exactly the original finite set of potential images q-x.
2490 ! Repeating the original atomic distribution keeps every inserted block local.
2491 CALL dbcsr_get_info(ri_data%kp_mat_2c_pot(1, 1), distribution=matrix_dist)
2492 CALL dbcsr_distribution_get(matrix_dist, row_dist=row_dist, col_dist=col_dist, group=group, pgrid=grid)
2493 ALLOCATE (row_ext(ng*natom), col_ext(ri_data%ncell_RI*natom))
2494 row_ext(:) = reshape(spread(row_dist, 2, ng), [ng*natom])
2495 col_ext(:) = reshape(spread(col_dist, 2, ri_data%ncell_RI), [ri_data%ncell_RI*natom])
2496 CALL dbcsr_distribution_new(extended_dist, group=group, pgrid=grid, row_dist=row_ext, col_dist=col_ext)
2497 CALL dbcsr_create(potential, dist=extended_dist, name="V_G_RI", matrix_type=dbcsr_type_no_symmetry, &
2498 row_blk_size=sizes_g, col_blk_size=sizes_ri)
2499 CALL dbcsr_distribution_release(extended_dist)
2500 DEALLOCATE (row_ext, col_ext, sizes_g, sizes_ri)
2501 DO img = 1, nimg
2502 CALL dbcsr_iterator_start(matrix_iter, ri_data%kp_mat_2c_pot(1, img))
2503 DO WHILE (dbcsr_iterator_blocks_left(matrix_iter))
2504 CALL dbcsr_iterator_next_block(matrix_iter, row=p_atom, column=q_atom)
2505 CALL dbcsr_get_block_p(ri_data%kp_mat_2c_pot(1, img), p_atom, q_atom, block, found)
2506 IF (.NOT. found) cycle
2507 DO q = 1, ri_data%ncell_RI
2508 cell_x = index_to_cell(:, ri_data%RI_cell_to_img(q)) - index_to_cell(:, img)
2509 IF (any(cell_x < lo) .OR. any(cell_x > hi)) cycle
2510 g = cell_index(cell_x(1), cell_x(2), cell_x(3))
2511 IF (g == 0) cycle
2512 CALL dbcsr_put_block(potential, (g - 1)*natom + p_atom, (q - 1)*natom + q_atom, block)
2513 END DO
2514 END DO
2515 CALL dbcsr_iterator_stop(matrix_iter)
2516 END DO
2517 CALL dbcsr_finalize(potential)
2518 CALL dbt_create(potential, v_atom)
2519 CALL dbt_copy_matrix_to_tensor(potential, v_atom)
2520 CALL dbcsr_release(potential)
2521 CALL dbt_filter(v_atom, ri_data%filter_eps)
2522 CALL create_2c_tensor(v_split, dist1, dist2, ri_data%pgrid_2d, sizes_g_split, sizes_ri_split, name="V_G_RI")
2523 DEALLOCATE (dist1, dist2)
2524 CALL dbt_copy(v_atom, v_split, move_data=.true.)
2525 CALL dbt_destroy(v_atom)
2526 CALL dbt_filter(v_split, ri_data%filter_eps)
2527
2528 ! Stack the fitted pairs, without adding any left-center or output-image label.
2529 ALLOCATE (sizes_stack(batch_size*nao), stack_dist(batch_size*nao), indices(ri_data%nimg_nze))
2530 sizes_stack(:) = reshape(spread(ri_data%bsizes_AO_split, 2, batch_size), [SIZE(sizes_stack)])
2531 DO i = 1, SIZE(indices)
2532 indices(i) = i
2533 END DO
2534 ALLOCATE (dist1(nao), dist2(SIZE(sizes_ri_split)), dist3(nao))
2535 CALL dbt_get_info(ri_data%t_3c_int_ctr_1(1, 1), proc_dist_1=dist1, proc_dist_2=dist2, proc_dist_3=dist3)
2536 stack_dist(:) = reshape(spread(dist3, 2, batch_size), [SIZE(stack_dist)])
2537 CALL dbt_distribution_new(tensor_dist, ri_data%pgrid_1, dist1, dist2, stack_dist)
2538 CALL dbt_create(b_stack, "B_stack", tensor_dist, [1, 2], [3], &
2539 ri_data%bsizes_AO_split, sizes_ri_split, sizes_stack)
2540 CALL dbt_distribution_destroy(tensor_dist)
2541 DEALLOCATE (dist1, dist2, dist3)
2542 ! Only the subgroup G survives setup. Reuse one main-group tensor per image.
2543 ALLOCATE (ri_data%kp_t_3c_G_sub(ri_data%nimg_nze))
2544 pdims = 0
2545 CALL dbt_pgrid_create(para_env_sub, pdims, pgrid_sub, tensor_dims=[nao, ng*nri, nao])
2546 CALL create_3c_tensor(ri_data%kp_t_3c_G_sub(1), dist1, dist2, dist3, pgrid_sub, &
2547 ri_data%bsizes_AO_split, sizes_g_split, ri_data%bsizes_AO_split, [1], [2, 3], name="G")
2548 DO img = 2, ri_data%nimg_nze
2549 CALL dbt_create(ri_data%kp_t_3c_G_sub(1), ri_data%kp_t_3c_G_sub(img))
2550 END DO
2551 DEALLOCATE (dist1, dist2, dist3)
2552 CALL dbt_pgrid_destroy(pgrid_sub)
2553 CALL create_3c_tensor(g_main, dist1, dist2, dist3, ri_data%pgrid_1, &
2554 ri_data%bsizes_AO_split, sizes_g_split, ri_data%bsizes_AO_split, [1], [2, 3], name="G")
2555 stack_dist(:) = reshape(spread(dist3, 2, batch_size), [SIZE(stack_dist)])
2556 CALL dbt_distribution_new(tensor_dist, ri_data%pgrid_1, dist1, dist2, stack_dist)
2557 CALL dbt_create(g_stack, "G_stack", tensor_dist, [1], [2, 3], &
2558 ri_data%bsizes_AO_split, sizes_g_split, sizes_stack)
2559 CALL dbt_distribution_destroy(tensor_dist)
2560 CALL dbt_mp_environ_pgrid(ri_data%pgrid_1, pdims, pcoord)
2561 ALLOCATE (idx_to_atom(nao), pairs(nimg, 2), ao_pairs(nao, batch_size*nao), needed(ng*natom, natom))
2562 CALL get_idx_to_atom(idx_to_atom, ri_data%bsizes_AO_split, ri_data%bsizes_AO)
2563 ! Image partners depend only on geometry, not on the count/pack traversal.
2564 ALLOCATE (valid_images(ri_data%nimg_nze, nimg), blocks(0, 3))
2565 DO b = 1, nimg
2566 CALL get_iapc_pairs(pairs, b, ri_data, qs_env)
2567 valid_images(:, b) = pairs(:ri_data%nimg_nze, 2) >= 1 .AND. pairs(:ri_data%nimg_nze, 2) <= nimg
2568 END DO
2569
2570 DO first = 1, ri_data%nimg_nze, batch_size
2571 last = min(first + batch_size, ri_data%nimg_nze + 1)
2572 CALL fill_3c_stack(b_stack, ri_data%t_3c_int_ctr_1(1, :), indices, 3, ri_data, img_bounds=[first, last])
2573 CALL dbt_filter(b_stack, ri_data%filter_eps)
2574 CALL dbt_get_block_pattern(b_stack, [1, 3], ao_pairs)
2575
2576 ! Reserve only requested RI-row/AO-pair blocks. No dense product space
2577 ! is materialized, including for images that have no valid a+c partner.
2578 nblocks = 0
2579 DO img = first, last - 1
2580 slot = img - first
2581 needed = .false.
2582 DO b = 1, nimg
2583 IF (.NOT. valid_images(img, b)) cycle
2584 DO jatom = 1, natom
2585 DO iatom = 1, natom
2586 IF (pattern(iatom, jatom, b) < 0) cycle
2587 DO g = 1, ng
2588 p = ri_data%kp_G_to_RI(g, b)
2589 IF (p == 0) cycle
2590 needed((g - 1)*natom + 1:g*natom, jatom) = &
2591 needed((g - 1)*natom + 1:g*natom, jatom) .OR. &
2592 ri_data%kp_RI_neighbors((p - 1)*natom + 1:p*natom, iatom)
2593 END DO
2594 END DO
2595 END DO
2596 END DO
2597 DO j = 1, ng*nri
2598 IF (dist2(j) /= pcoord(2)) cycle
2599 g = (j - 1)/nri + 1
2600 p_atom = ri_data%kp_RI_atom(modulo(j - 1, nri) + 1)
2601 DO i = 1, nao
2602 IF (dist1(i) /= pcoord(1)) cycle
2603 IF (.NOT. needed((g - 1)*natom + p_atom, idx_to_atom(i))) cycle
2604 DO k = slot*nao + 1, (slot + 1)*nao
2605 IF (stack_dist(k) /= pcoord(3) .OR. ao_pairs(i, k) == 0) cycle
2606 nblocks = nblocks + 1
2607 IF (nblocks > SIZE(blocks, 1)) CALL reallocate(blocks, 1, max(1, 2*SIZE(blocks, 1)), 1, 3)
2608 blocks(nblocks, :) = [i, j, k]
2609 END DO
2610 END DO
2611 END DO
2612 END DO
2613 CALL dbt_reserve_blocks(g_stack, blocks(:nblocks, :))
2614 CALL dbt_contract(1.0_dp, v_split, b_stack, 0.0_dp, g_stack, &
2615 contract_1=[2], notcontract_1=[1], contract_2=[2], notcontract_2=[1, 3], &
2616 map_1=[2], map_2=[1, 3], retain_sparsity=.true., filter_eps=ri_data%filter_eps, flop=nflop)
2617 ri_data%dbcsr_nflop = ri_data%dbcsr_nflop + nflop
2618 DO img = first, last - 1
2619 CALL unstack_t_3c_apc(g_main, g_stack, img - first + 1)
2620 CALL dbt_finalize(g_main)
2621 CALL dbt_filter(g_main, ri_data%filter_eps)
2622 CALL copy_3c_to_subgroup(ri_data%kp_t_3c_G_sub(img), g_main)
2623 CALL dbt_clear(g_main)
2624 END DO
2625 CALL dbt_clear(b_stack)
2626 CALL dbt_clear(g_stack)
2627 END DO
2628 DEALLOCATE (blocks)
2629 CALL dbt_destroy(b_stack)
2630 CALL dbt_destroy(g_stack)
2631 CALL dbt_destroy(g_main)
2632 CALL dbt_destroy(v_split)
2633 ri_data%kp_G_ngroups = ngroups
2634 CALL timestop(handle)
2635 END SUBROUTINE build_kp_exchange_integrals
2636
2637! **************************************************************************************************
2638!> \brief Select and shift canonical G rows for an existing KS contraction batch.
2639!> The change x -> p=x+b can change the MPI owner. Route each retained
2640!> block to the destination tensor instead of assuming a local copy.
2641!> \param stack output in the current contraction layout and distribution
2642!> \param ri_data geometry-dependent G and local RI neighborhoods
2643!> \param pairs image indices (l,a+c) from get_iapc_pairs
2644!> \param iatom left AO center selecting the RI rows
2645!> \param jatom right AO center selecting the fitted pairs
2646!> \param b_img output KS image
2647!> \param idx_to_at mapping of split AO blocks to atoms
2648!> \param img_bounds half-open image batch
2649! **************************************************************************************************
2650 SUBROUTINE fill_kp_exchange_stack(stack, ri_data, pairs, iatom, jatom, b_img, idx_to_at, img_bounds)
2651 TYPE(dbt_type), INTENT(INOUT) :: stack
2652 TYPE(hfx_ri_type), INTENT(INOUT) :: ri_data
2653 INTEGER, INTENT(IN) :: pairs(:, :), iatom, jatom, b_img, &
2654 idx_to_at(:), img_bounds(2)
2655
2656 CHARACTER(LEN=*), PARAMETER :: routinen = 'fill_kp_exchange_stack'
2657
2658 INTEGER :: g, handle, i, nao, natom, nri, p, r
2659 INTEGER, ALLOCATABLE :: images(:), index_map(:, :)
2660
2661 CALL timeset(routinen, handle)
2662 nao = SIZE(ri_data%bsizes_AO_split)
2663 nri = SIZE(ri_data%bsizes_RI_split)
2664 natom = SIZE(ri_data%bsizes_RI)
2665 ALLOCATE (index_map(max(nao, SIZE(ri_data%kp_G_to_RI, 1)*nri), 3))
2666 index_map = 0
2667 DO i = 1, nao
2668 IF (idx_to_at(i) == jatom) index_map(i, 1) = i
2669 index_map(i, 3) = i
2670 END DO
2671 ! x -> p=x+b selects the original finite RI domain and the left-center neighborhood.
2672 DO g = 1, SIZE(ri_data%kp_G_to_RI, 1)
2673 p = ri_data%kp_G_to_RI(g, b_img)
2674 IF (p == 0) cycle
2675 DO r = 1, nri
2676 IF (.NOT. ri_data%kp_RI_neighbors((p - 1)*natom + ri_data%kp_RI_atom(r), iatom)) cycle
2677 index_map((g - 1)*nri + r, 2) = (p - 1)*nri + r
2678 END DO
2679 END DO
2680 ALLOCATE (images(img_bounds(2) - img_bounds(1)))
2681 images(:) = pairs(img_bounds(1):img_bounds(2) - 1, 1)
2682 WHERE (pairs(img_bounds(1):img_bounds(2) - 1, 2) < 1 .OR. &
2683 pairs(img_bounds(1):img_bounds(2) - 1, 2) > ri_data%nimg .OR. &
2684 images < 1 .OR. images > SIZE(ri_data%kp_t_3c_G_sub)) images = 0
2685 CALL dbt_copy_indexed(ri_data%kp_t_3c_G_sub, stack, images, index_map, stack_dim=3)
2686 CALL timestop(handle)
2687 END SUBROUTINE fill_kp_exchange_stack
2688
2689! **************************************************************************************************
2690!> \brief Pre-contract the density matrices with the 3-center integrals:
2691!> P_sigma^a,lambda^a+c (mu^0 sigma^a| P^0)
2692!> \param t_3c_apc ...
2693!> \param rho_ao_t ...
2694!> \param ri_data ...
2695!> \param qs_env ...
2696! **************************************************************************************************
2697 SUBROUTINE contract_pmat_3c(t_3c_apc, rho_ao_t, ri_data, qs_env)
2698 TYPE(dbt_type), DIMENSION(:, :), INTENT(INOUT) :: t_3c_apc, rho_ao_t
2699 TYPE(hfx_ri_type), INTENT(INOUT) :: ri_data
2700 TYPE(qs_environment_type), POINTER :: qs_env
2701
2702 CHARACTER(len=*), PARAMETER :: routinen = 'contract_pmat_3c'
2703
2704 INTEGER :: apc_img, b_img, batch_size, handle, &
2705 i_batch, i_img, i_spin, idx, j_batch, &
2706 n_batch_img, n_batch_nze, nimg, &
2707 nimg_nze, nspins
2708 INTEGER(int_8) :: nflop, nze
2709 INTEGER, ALLOCATABLE, DIMENSION(:) :: apc_filter, batch_ranges_img, &
2710 batch_ranges_nze, int_indices
2711 INTEGER, ALLOCATABLE, DIMENSION(:, :) :: ac_pairs, iapc_pairs, rho_images
2712 REAL(dp) :: occ, t1, t2
2713 TYPE(dbt_type), ALLOCATABLE, DIMENSION(:) :: ints_stack, res_stack, rho_stack
2714 TYPE(dft_control_type), POINTER :: dft_control
2715
2716 CALL timeset(routinen, handle)
2717
2718 CALL get_qs_env(qs_env, dft_control=dft_control)
2719
2720 nimg = ri_data%nimg
2721 nimg_nze = ri_data%nimg_nze
2722 nspins = dft_control%nspins
2723
2724 batch_size = ri_data%kp_stack_size
2725
2726 ALLOCATE (apc_filter(nimg), iapc_pairs(nimg, 2))
2727 apc_filter = 0
2728 DO b_img = 1, nimg
2729 CALL get_iapc_pairs(iapc_pairs, b_img, ri_data, qs_env)
2730 DO i_img = 1, nimg_nze
2731 idx = iapc_pairs(i_img, 2)
2732 IF (idx < 1 .OR. idx > nimg) cycle
2733 apc_filter(idx) = 1
2734 END DO
2735 END DO
2736
2737 !batching over all images
2738 n_batch_img = nimg/batch_size
2739 IF (modulo(nimg, batch_size) /= 0) n_batch_img = n_batch_img + 1
2740 ALLOCATE (batch_ranges_img(n_batch_img + 1))
2741 DO i_batch = 1, n_batch_img
2742 batch_ranges_img(i_batch) = (i_batch - 1)*batch_size + 1
2743 END DO
2744 batch_ranges_img(n_batch_img + 1) = nimg + 1
2745
2746 !batching over images with non-zero 3c integrals
2747 n_batch_nze = nimg_nze/batch_size
2748 IF (modulo(nimg_nze, batch_size) /= 0) n_batch_nze = n_batch_nze + 1
2749 ALLOCATE (batch_ranges_nze(n_batch_nze + 1))
2750 DO i_batch = 1, n_batch_nze
2751 batch_ranges_nze(i_batch) = (i_batch - 1)*batch_size + 1
2752 END DO
2753 batch_ranges_nze(n_batch_nze + 1) = nimg_nze + 1
2754
2755 !Create the stack tensors in the approriate distribution
2756 ALLOCATE (rho_stack(2), ints_stack(2), res_stack(2))
2757 CALL get_stack_tensors(res_stack, rho_stack, ints_stack, rho_ao_t(1, 1), &
2758 ri_data%t_3c_int_ctr_1(1, 1), batch_size, ri_data, qs_env)
2759
2760 ALLOCATE (ac_pairs(nimg, 2), int_indices(nimg_nze), rho_images(batch_size, batch_size))
2761 DO i_img = 1, nimg_nze
2762 int_indices(i_img) = i_img
2763 END DO
2764
2765 t1 = m_walltime()
2766 DO j_batch = 1, n_batch_nze
2767 !First batch is over the integrals. They are always in the same order, consistent with get_ac_pairs
2768 CALL fill_3c_stack(ints_stack(1), ri_data%t_3c_int_ctr_1(1, :), int_indices, 3, ri_data, &
2769 img_bounds=[batch_ranges_nze(j_batch), batch_ranges_nze(j_batch + 1)])
2770 CALL dbt_copy(ints_stack(1), ints_stack(2), move_data=.true.)
2771
2772 DO i_spin = 1, nspins
2773 DO i_batch = 1, n_batch_img
2774 !Collect the image pairs before filling the whole density-matrix stack.
2775 rho_images(:, :) = 0
2776 DO apc_img = batch_ranges_img(i_batch), batch_ranges_img(i_batch + 1) - 1
2777 IF (apc_filter(apc_img) == 0) cycle
2778 CALL get_ac_pairs(ac_pairs, apc_img, ri_data, qs_env)
2779 rho_images(1:batch_ranges_nze(j_batch + 1) - batch_ranges_nze(j_batch), &
2780 apc_img - batch_ranges_img(i_batch) + 1) = &
2781 ac_pairs(batch_ranges_nze(j_batch):batch_ranges_nze(j_batch + 1) - 1, 2)
2782 END DO !apc_img
2783 CALL fill_2c_stack(rho_stack(1), rho_ao_t(i_spin, :), rho_images, ri_data)
2784 CALL get_tensor_occupancy(rho_stack(1), nze, occ)
2785 IF (nze == 0) cycle
2786 CALL dbt_copy(rho_stack(1), rho_stack(2), move_data=.true.)
2787
2788 !The actual contraction
2789 CALL dbt_batched_contract_init(rho_stack(2))
2790 CALL dbt_contract(1.0_dp, ints_stack(2), rho_stack(2), &
2791 0.0_dp, res_stack(2), map_1=[1, 2], map_2=[3], &
2792 contract_1=[3], notcontract_1=[1, 2], &
2793 contract_2=[1], notcontract_2=[2], &
2794 filter_eps=ri_data%filter_eps, flop=nflop)
2795 ri_data%dbcsr_nflop = ri_data%dbcsr_nflop + nflop
2796 CALL dbt_batched_contract_finalize(rho_stack(2))
2797 CALL dbt_copy(res_stack(2), res_stack(1), move_data=.true.)
2798
2799 CALL accumulate_3c_stack(t_3c_apc(i_spin, batch_ranges_img(i_batch):batch_ranges_img(i_batch + 1) - 1), &
2800 res_stack(1), &
2801 apc_filter(batch_ranges_img(i_batch):batch_ranges_img(i_batch + 1) - 1))
2802
2803 END DO !i_batch
2804 END DO !i_spin
2805 END DO !j_batch
2806 DEALLOCATE (batch_ranges_img)
2807 DEALLOCATE (batch_ranges_nze)
2808 t2 = m_walltime()
2809 ri_data%dbcsr_time = ri_data%dbcsr_time + t2 - t1
2810
2811 CALL dbt_destroy(rho_stack(1))
2812 CALL dbt_destroy(rho_stack(2))
2813 CALL dbt_destroy(ints_stack(1))
2814 CALL dbt_destroy(ints_stack(2))
2815 CALL dbt_destroy(res_stack(1))
2816 CALL dbt_destroy(res_stack(2))
2817
2818 CALL timestop(handle)
2819
2820 END SUBROUTINE contract_pmat_3c
2821
2822! **************************************************************************************************
2823!> \brief Pre-contract 3-center integrals with the bumped invrse RI metric, for each atom
2824!> \param t_3c_int ...
2825!> \param ri_data ...
2826!> \param qs_env ...
2827! **************************************************************************************************
2828 SUBROUTINE precontract_3c_ints(t_3c_int, ri_data, qs_env)
2829 TYPE(dbt_type), DIMENSION(:, :), INTENT(INOUT) :: t_3c_int
2830 TYPE(hfx_ri_type), INTENT(INOUT) :: ri_data
2831 TYPE(qs_environment_type), POINTER :: qs_env
2832
2833 CHARACTER(len=*), PARAMETER :: routinen = 'precontract_3c_ints'
2834
2835 INTEGER :: batch_size, handle, i_batch, i_img, &
2836 i_ri, iatom, is, n_batch, natom, &
2837 nblks, nblks_3c(3), nimg
2838 INTEGER(int_8) :: nflop
2839 INTEGER, ALLOCATABLE, DIMENSION(:) :: batch_ranges, bsizes_ri_ext, bsizes_ri_ext_split, &
2840 bsizes_stack, dist1, dist2, dist3, dist_stack3, idx_to_at_ao, int_indices
2841 TYPE(dbt_distribution_type) :: t_dist
2842 TYPE(dbt_type) :: t_2c_ri_tmp(2), t_3c_tmp(3)
2843
2844 CALL timeset(routinen, handle)
2845
2846 CALL get_qs_env(qs_env, natom=natom)
2847
2848 nimg = ri_data%nimg
2849 ALLOCATE (int_indices(nimg))
2850 DO i_img = 1, nimg
2851 int_indices(i_img) = i_img
2852 END DO
2853
2854 ALLOCATE (idx_to_at_ao(SIZE(ri_data%bsizes_AO_split)))
2855 CALL get_idx_to_atom(idx_to_at_ao, ri_data%bsizes_AO_split, ri_data%bsizes_AO)
2856
2857 nblks = SIZE(ri_data%bsizes_RI_split)
2858 ALLOCATE (bsizes_ri_ext(ri_data%ncell_RI*natom))
2859 ALLOCATE (bsizes_ri_ext_split(ri_data%ncell_RI*nblks))
2860 DO i_ri = 1, ri_data%ncell_RI
2861 bsizes_ri_ext((i_ri - 1)*natom + 1:i_ri*natom) = ri_data%bsizes_RI(:)
2862 bsizes_ri_ext_split((i_ri - 1)*nblks + 1:i_ri*nblks) = ri_data%bsizes_RI_split(:)
2863 END DO
2864 CALL create_2c_tensor(t_2c_ri_tmp(1), dist1, dist2, ri_data%pgrid_2d, &
2865 bsizes_ri_ext, bsizes_ri_ext, &
2866 name="(RI | RI)")
2867 DEALLOCATE (dist1, dist2)
2868 CALL create_2c_tensor(t_2c_ri_tmp(2), dist1, dist2, ri_data%pgrid_2d, &
2869 bsizes_ri_ext_split, bsizes_ri_ext_split, &
2870 name="(RI | RI)")
2871 DEALLOCATE (dist1, dist2)
2872
2873 !For more efficiency, we stack multiple images of the 3-center integrals into a single tensor
2874 batch_size = ri_data%kp_stack_size
2875 n_batch = nimg/batch_size
2876 IF (modulo(nimg, batch_size) /= 0) n_batch = n_batch + 1
2877 ALLOCATE (batch_ranges(n_batch + 1))
2878 DO i_batch = 1, n_batch
2879 batch_ranges(i_batch) = (i_batch - 1)*batch_size + 1
2880 END DO
2881 batch_ranges(n_batch + 1) = nimg + 1
2882
2883 nblks = SIZE(ri_data%bsizes_AO_split)
2884 ALLOCATE (bsizes_stack(batch_size*nblks))
2885 DO is = 1, batch_size
2886 bsizes_stack((is - 1)*nblks + 1:is*nblks) = ri_data%bsizes_AO_split(:)
2887 END DO
2888
2889 CALL dbt_get_info(t_3c_int(1, 1), nblks_total=nblks_3c)
2890 ALLOCATE (dist1(nblks_3c(1)), dist2(nblks_3c(2)), dist3(nblks_3c(3)), dist_stack3(batch_size*nblks_3c(3)))
2891 CALL dbt_get_info(t_3c_int(1, 1), proc_dist_1=dist1, proc_dist_2=dist2, proc_dist_3=dist3)
2892 DO is = 1, batch_size
2893 dist_stack3((is - 1)*nblks_3c(3) + 1:is*nblks_3c(3)) = dist3(:)
2894 END DO
2895
2896 CALL dbt_distribution_new(t_dist, ri_data%pgrid, dist1, dist2, dist_stack3)
2897 CALL dbt_create(t_3c_tmp(1), "ints_stack", t_dist, [1], [2, 3], bsizes_ri_ext_split, &
2898 ri_data%bsizes_AO_split, bsizes_stack)
2899 CALL dbt_distribution_destroy(t_dist)
2900 DEALLOCATE (dist1, dist2, dist3, dist_stack3)
2901
2902 CALL dbt_create(t_3c_tmp(1), t_3c_tmp(2))
2903 CALL dbt_create(t_3c_int(1, 1), t_3c_tmp(3))
2904
2905 DO iatom = 1, natom
2906 CALL dbt_copy(ri_data%t_2c_inv(1, iatom), t_2c_ri_tmp(1))
2907 CALL apply_bump(t_2c_ri_tmp(1), iatom, ri_data, qs_env, from_left=.true., from_right=.true.)
2908 CALL dbt_copy(t_2c_ri_tmp(1), t_2c_ri_tmp(2), move_data=.true.)
2909
2910 CALL dbt_batched_contract_init(t_2c_ri_tmp(2))
2911 DO i_batch = 1, n_batch
2912
2913 CALL fill_3c_stack(t_3c_tmp(1), t_3c_int(1, :), int_indices, 3, ri_data, &
2914 img_bounds=[batch_ranges(i_batch), batch_ranges(i_batch + 1)], &
2915 filter_at=iatom, filter_dim=2, idx_to_at=idx_to_at_ao)
2916
2917 CALL dbt_contract(1.0_dp, t_2c_ri_tmp(2), t_3c_tmp(1), &
2918 0.0_dp, t_3c_tmp(2), map_1=[1], map_2=[2, 3], &
2919 contract_1=[2], notcontract_1=[1], &
2920 contract_2=[1], notcontract_2=[2, 3], &
2921 filter_eps=ri_data%filter_eps, flop=nflop)
2922 ri_data%dbcsr_nflop = ri_data%dbcsr_nflop + nflop
2923
2924 DO i_img = batch_ranges(i_batch), batch_ranges(i_batch + 1) - 1
2925 CALL unstack_t_3c_apc(t_3c_tmp(3), t_3c_tmp(2), i_img - batch_ranges(i_batch) + 1)
2926 CALL dbt_copy(t_3c_tmp(3), ri_data%t_3c_int_ctr_1(1, i_img), summation=.true., &
2927 order=[2, 1, 3], move_data=.true.)
2928 END DO
2929 CALL dbt_clear(t_3c_tmp(1))
2930 END DO
2931 CALL dbt_batched_contract_finalize(t_2c_ri_tmp(2))
2932
2933 END DO
2934 CALL dbt_destroy(t_2c_ri_tmp(1))
2935 CALL dbt_destroy(t_2c_ri_tmp(2))
2936 CALL dbt_destroy(t_3c_tmp(1))
2937 CALL dbt_destroy(t_3c_tmp(2))
2938 CALL dbt_destroy(t_3c_tmp(3))
2939
2940 DO i_img = 1, nimg
2941 CALL dbt_destroy(t_3c_int(1, i_img))
2942 END DO
2943
2944 CALL timestop(handle)
2945
2946 END SUBROUTINE precontract_3c_ints
2947
2948! **************************************************************************************************
2949!> \brief Copy the data of a 2D tensor living in the main MPI group to a sub-group, given the proc
2950!> mapping from one to the other (e.g. for a proc idx in the subgroup, we get the idx in the main)
2951!> \param t2c_sub ...
2952!> \param t2c_main ...
2953!> \param group_size ...
2954!> \param ngroups ...
2955!> \param para_env ...
2956! **************************************************************************************************
2957 SUBROUTINE copy_2c_to_subgroup(t2c_sub, t2c_main, group_size, ngroups, para_env)
2958 TYPE(dbt_type), INTENT(INOUT) :: t2c_sub, t2c_main
2959 INTEGER, INTENT(IN) :: group_size, ngroups
2960 TYPE(mp_para_env_type), POINTER :: para_env
2961
2962 INTEGER :: batch_size, i, i_batch, i_msg, iblk, &
2963 igroup, iproc, ir, is, jblk, n_batch, &
2964 nocc, tag
2965 INTEGER, ALLOCATABLE, DIMENSION(:) :: bsizes1, bsizes2
2966 INTEGER, ALLOCATABLE, DIMENSION(:, :) :: block_dest, block_source
2967 INTEGER, ALLOCATABLE, DIMENSION(:, :, :) :: current_dest
2968 INTEGER, DIMENSION(2) :: ind, nblks
2969 LOGICAL :: found
2970 REAL(dp), ALLOCATABLE, DIMENSION(:, :) :: blk
2971 TYPE(cp_2d_r_p_type), ALLOCATABLE, DIMENSION(:) :: recv_buff, send_buff
2972 TYPE(dbt_iterator_type) :: iter
2973 TYPE(mp_request_type), ALLOCATABLE, DIMENSION(:) :: recv_req, send_req
2974
2975 !Stategy: we loop over the main tensor, and send all the data. Then we loop over the sub tensor
2976 ! and receive it. We do all of it with async MPI communication. The sub tensor needs
2977 ! to have blocks pre-reserved though
2978
2979 CALL dbt_get_info(t2c_main, nblks_total=nblks)
2980
2981 !Loop over the main tensor, count how many blocks are there, which ones, and on which proc
2982 ALLOCATE (block_source(nblks(1), nblks(2)))
2983 block_source = -1
2984 nocc = 0
2985!$OMP PARALLEL DEFAULT(NONE) SHARED(t2c_main,para_env,nocc,block_source) PRIVATE(iter,ind,blk,found)
2986 CALL dbt_iterator_start(iter, t2c_main)
2987 DO WHILE (dbt_iterator_blocks_left(iter))
2988 CALL dbt_iterator_next_block(iter, ind)
2989 CALL dbt_get_block(t2c_main, ind, blk, found)
2990 IF (.NOT. found) cycle
2991
2992 block_source(ind(1), ind(2)) = para_env%mepos
2993!$OMP ATOMIC
2994 nocc = nocc + 1
2995 DEALLOCATE (blk)
2996 END DO
2997 CALL dbt_iterator_stop(iter)
2998!$OMP END PARALLEL
2999
3000 CALL para_env%sum(nocc)
3001 CALL para_env%sum(block_source)
3002 block_source = block_source + para_env%num_pe - 1
3003 IF (nocc == 0) RETURN
3004
3005 !Loop over the sub tensor, get the block destination
3006 igroup = para_env%mepos/group_size
3007 ALLOCATE (block_dest(nblks(1), nblks(2)))
3008 block_dest = -1
3009 DO jblk = 1, nblks(2)
3010 DO iblk = 1, nblks(1)
3011 IF (block_source(iblk, jblk) == -1) cycle
3012
3013 CALL dbt_get_stored_coordinates(t2c_sub, [iblk, jblk], iproc)
3014 block_dest(iblk, jblk) = igroup*group_size + iproc !mapping of iproc in subgroup to main group idx
3015 END DO
3016 END DO
3017
3018 ALLOCATE (bsizes1(nblks(1)), bsizes2(nblks(2)))
3019 CALL dbt_get_info(t2c_main, blk_size_1=bsizes1, blk_size_2=bsizes2)
3020
3021 ALLOCATE (current_dest(nblks(1), nblks(2), 0:ngroups - 1))
3022 DO igroup = 0, ngroups - 1
3023 !for a given subgroup, need to make the destination available to everyone in the main group
3024 current_dest(:, :, igroup) = block_dest(:, :)
3025 CALL para_env%bcast(current_dest(:, :, igroup), source=igroup*group_size) !bcast from first proc in sub-group
3026 END DO
3027
3028 !We go by batches, which cannot be larger than the maximum MPI tag value
3029 batch_size = min(para_env%get_tag_ub(), 128000, nocc*ngroups)
3030 n_batch = (nocc*ngroups)/batch_size
3031 IF (modulo(nocc*ngroups, batch_size) /= 0) n_batch = n_batch + 1
3032
3033 DO i_batch = 1, n_batch
3034 !Loop over groups, blocks and send/receive
3035 ALLOCATE (send_buff(batch_size), recv_buff(batch_size))
3036 ALLOCATE (send_req(batch_size), recv_req(batch_size))
3037 ir = 0
3038 is = 0
3039 i_msg = 0
3040 DO jblk = 1, nblks(2)
3041 DO iblk = 1, nblks(1)
3042 DO igroup = 0, ngroups - 1
3043 IF (block_source(iblk, jblk) == -1) cycle
3044
3045 i_msg = i_msg + 1
3046 IF (i_msg < (i_batch - 1)*batch_size + 1 .OR. i_msg > i_batch*batch_size) cycle
3047
3048 !a unique tag per block, within this batch
3049 tag = i_msg - (i_batch - 1)*batch_size
3050
3051 found = .false.
3052 IF (para_env%mepos == block_source(iblk, jblk)) THEN
3053 CALL dbt_get_block(t2c_main, [iblk, jblk], blk, found)
3054 END IF
3055
3056 !If blocks live on same proc, simply copy. Else MPI send/recv
3057 IF (block_source(iblk, jblk) == current_dest(iblk, jblk, igroup)) THEN
3058 IF (found) CALL dbt_put_block(t2c_sub, [iblk, jblk], shape(blk), blk)
3059 ELSE
3060 IF (para_env%mepos == block_source(iblk, jblk) .AND. found) THEN
3061 ALLOCATE (send_buff(tag)%array(bsizes1(iblk), bsizes2(jblk)))
3062 send_buff(tag)%array(:, :) = blk(:, :)
3063 is = is + 1
3064 CALL para_env%isend(msgin=send_buff(tag)%array, dest=current_dest(iblk, jblk, igroup), &
3065 request=send_req(is), tag=tag)
3066 END IF
3067
3068 IF (para_env%mepos == current_dest(iblk, jblk, igroup)) THEN
3069 ALLOCATE (recv_buff(tag)%array(bsizes1(iblk), bsizes2(jblk)))
3070 ir = ir + 1
3071 CALL para_env%irecv(msgout=recv_buff(tag)%array, source=block_source(iblk, jblk), &
3072 request=recv_req(ir), tag=tag)
3073 END IF
3074 END IF
3075
3076 IF (found) DEALLOCATE (blk)
3077 END DO
3078 END DO
3079 END DO
3080
3081 CALL mp_waitall(send_req(1:is))
3082 CALL mp_waitall(recv_req(1:ir))
3083 !clean-up
3084 DO i = 1, batch_size
3085 IF (ASSOCIATED(send_buff(i)%array)) DEALLOCATE (send_buff(i)%array)
3086 END DO
3087
3088 !Finally copy the data from the buffer to the sub-tensor
3089 i_msg = 0
3090 DO jblk = 1, nblks(2)
3091 DO iblk = 1, nblks(1)
3092 DO igroup = 0, ngroups - 1
3093 IF (block_source(iblk, jblk) == -1) cycle
3094
3095 i_msg = i_msg + 1
3096 IF (i_msg < (i_batch - 1)*batch_size + 1 .OR. i_msg > i_batch*batch_size) cycle
3097
3098 !a unique tag per block, within this batch
3099 tag = i_msg - (i_batch - 1)*batch_size
3100
3101 IF (para_env%mepos == current_dest(iblk, jblk, igroup) .AND. &
3102 block_source(iblk, jblk) /= current_dest(iblk, jblk, igroup)) THEN
3103
3104 ALLOCATE (blk(bsizes1(iblk), bsizes2(jblk)))
3105 blk(:, :) = recv_buff(tag)%array(:, :)
3106 CALL dbt_put_block(t2c_sub, [iblk, jblk], shape(blk), blk)
3107 DEALLOCATE (blk)
3108 END IF
3109 END DO
3110 END DO
3111 END DO
3112
3113 !clean-up
3114 DO i = 1, batch_size
3115 IF (ASSOCIATED(recv_buff(i)%array)) DEALLOCATE (recv_buff(i)%array)
3116 END DO
3117 DEALLOCATE (send_buff, recv_buff, send_req, recv_req)
3118 END DO !i_batch
3119 CALL dbt_finalize(t2c_sub)
3120
3121 END SUBROUTINE copy_2c_to_subgroup
3122
3123! **************************************************************************************************
3124!> \brief Copy stored 3c blocks to subgroups with identical tensor layouts.
3125!> Route only existing blocks; destination ranks are relative to a subgroup.
3126!> \param t3c_sub destination tensor in the local subgroup
3127!> \param t3c_main source tensor in the main communicator
3128!> \param iatom_to_subgroup optional atom-to-subgroup selection
3129!> \param dim_at tensor dimension identifying the atom
3130!> \param idx_to_at mapping of blocks in dim_at to atoms
3131! **************************************************************************************************
3132 SUBROUTINE copy_3c_to_subgroup(t3c_sub, t3c_main, iatom_to_subgroup, dim_at, idx_to_at)
3133 TYPE(dbt_type), INTENT(INOUT) :: t3c_sub, t3c_main
3134 TYPE(cp_1d_logical_p_type), INTENT(IN), OPTIONAL :: iatom_to_subgroup(:)
3135 INTEGER, INTENT(IN), OPTIONAL :: dim_at, idx_to_at(:)
3136
3137 CHARACTER(LEN=*), PARAMETER :: routinen = 'copy_3c_to_subgroup'
3138
3139 INTEGER :: handle, i
3140 LOGICAL, ALLOCATABLE :: group_mask(:, :)
3141
3142 CALL timeset(routinen, handle)
3143 IF (PRESENT(iatom_to_subgroup) .AND. PRESENT(dim_at) .AND. PRESENT(idx_to_at)) THEN
3144 ALLOCATE (group_mask(SIZE(iatom_to_subgroup(1)%array), SIZE(idx_to_at)))
3145 DO i = 1, SIZE(idx_to_at)
3146 group_mask(:, i) = iatom_to_subgroup(idx_to_at(i))%array(:)
3147 END DO
3148 CALL dbt_copy_indexed(t3c_main, t3c_sub, group_mask=group_mask, group_dim=dim_at)
3149 ELSE
3150 CALL dbt_copy_indexed(t3c_main, t3c_sub)
3151 END IF
3152 CALL timestop(handle)
3153 END SUBROUTINE copy_3c_to_subgroup
3154
3155! **************************************************************************************************
3156!> \brief A routine that gather the pieces of the KS matrix accross the subgroup and puts it in the
3157!> main group. Each b_img, iatom, jatom tuple is one a single CPU
3158!> \param ks_t ...
3159!> \param ks_t_sub ...
3160!> \param group_size ...
3161!> \param sparsity_pattern ...
3162!> \param para_env ...
3163!> \param ri_data ...
3164! **************************************************************************************************
3165 SUBROUTINE gather_ks_matrix(ks_t, ks_t_sub, group_size, sparsity_pattern, para_env, ri_data)
3166 TYPE(dbt_type), DIMENSION(:, :), INTENT(INOUT) :: ks_t, ks_t_sub
3167 INTEGER, INTENT(IN) :: group_size
3168 INTEGER, DIMENSION(:, :, :), INTENT(IN) :: sparsity_pattern
3169 TYPE(mp_para_env_type), POINTER :: para_env
3170 TYPE(hfx_ri_type), INTENT(INOUT) :: ri_data
3171
3172 CHARACTER(len=*), PARAMETER :: routinen = 'gather_ks_matrix'
3173
3174 INTEGER :: b_img, dest, handle, i, i_spin, iatom, &
3175 igroup, ir, is, jatom, n_mess, natom, &
3176 nimg, nspins, source, tag
3177 LOGICAL :: found
3178 REAL(dp), ALLOCATABLE, DIMENSION(:, :) :: blk
3179 TYPE(cp_2d_r_p_type), ALLOCATABLE, DIMENSION(:) :: recv_buff, send_buff
3180 TYPE(mp_request_type), ALLOCATABLE, DIMENSION(:) :: recv_req, send_req
3181
3182 CALL timeset(routinen, handle)
3183
3184 nimg = SIZE(sparsity_pattern, 3)
3185 natom = SIZE(sparsity_pattern, 2)
3186 nspins = SIZE(ks_t, 1)
3187
3188 DO b_img = 1, nimg
3189 n_mess = 0
3190 DO i_spin = 1, nspins
3191 DO jatom = 1, natom
3192 DO iatom = 1, natom
3193 IF (sparsity_pattern(iatom, jatom, b_img) > -1) n_mess = n_mess + 1
3194 END DO
3195 END DO
3196 END DO
3197
3198 ALLOCATE (send_buff(n_mess), recv_buff(n_mess))
3199 ALLOCATE (send_req(n_mess), recv_req(n_mess))
3200 ir = 0
3201 is = 0
3202 n_mess = 0
3203 tag = 0
3204
3205 DO i_spin = 1, nspins
3206 DO jatom = 1, natom
3207 DO iatom = 1, natom
3208 IF (sparsity_pattern(iatom, jatom, b_img) < 0) cycle
3209 n_mess = n_mess + 1
3210 tag = tag + 1
3211
3212 !sending the message
3213 CALL dbt_get_stored_coordinates(ks_t(i_spin, b_img), [iatom, jatom], dest)
3214 CALL dbt_get_stored_coordinates(ks_t_sub(i_spin, b_img), [iatom, jatom], source) !source within sub
3215 igroup = sparsity_pattern(iatom, jatom, b_img)
3216 source = source + igroup*group_size
3217 IF (para_env%mepos == source) THEN
3218 CALL dbt_get_block(ks_t_sub(i_spin, b_img), [iatom, jatom], blk, found)
3219 IF (source == dest) THEN
3220 IF (found) CALL dbt_put_block(ks_t(i_spin, b_img), [iatom, jatom], shape(blk), blk)
3221 ELSE
3222 ALLOCATE (send_buff(n_mess)%array(ri_data%bsizes_AO(iatom), ri_data%bsizes_AO(jatom)))
3223 send_buff(n_mess)%array(:, :) = 0.0_dp
3224 IF (found) THEN
3225 send_buff(n_mess)%array(:, :) = blk(:, :)
3226 END IF
3227 is = is + 1
3228 CALL para_env%isend(msgin=send_buff(n_mess)%array, dest=dest, &
3229 request=send_req(is), tag=tag)
3230 END IF
3231 DEALLOCATE (blk)
3232 END IF
3233
3234 !receiving the message
3235 IF (para_env%mepos == dest .AND. source /= dest) THEN
3236 ALLOCATE (recv_buff(n_mess)%array(ri_data%bsizes_AO(iatom), ri_data%bsizes_AO(jatom)))
3237 ir = ir + 1
3238 CALL para_env%irecv(msgout=recv_buff(n_mess)%array, source=source, &
3239 request=recv_req(ir), tag=tag)
3240 END IF
3241 END DO !iatom
3242 END DO !jatom
3243 END DO !ispin
3244
3245 CALL mp_waitall(send_req(1:is))
3246 CALL mp_waitall(recv_req(1:ir))
3247
3248 !Copy the messages received into the KS matrix
3249 n_mess = 0
3250 DO i_spin = 1, nspins
3251 DO jatom = 1, natom
3252 DO iatom = 1, natom
3253 IF (sparsity_pattern(iatom, jatom, b_img) < 0) cycle
3254 n_mess = n_mess + 1
3255
3256 CALL dbt_get_stored_coordinates(ks_t(i_spin, b_img), [iatom, jatom], dest)
3257 IF (para_env%mepos == dest) THEN
3258 IF (.NOT. ASSOCIATED(recv_buff(n_mess)%array)) cycle
3259 ALLOCATE (blk(ri_data%bsizes_AO(iatom), ri_data%bsizes_AO(jatom)))
3260 blk(:, :) = recv_buff(n_mess)%array(:, :)
3261 CALL dbt_put_block(ks_t(i_spin, b_img), [iatom, jatom], shape(blk), blk)
3262 DEALLOCATE (blk)
3263 END IF
3264 END DO
3265 END DO
3266 END DO
3267
3268 !clean-up
3269 DO i = 1, n_mess
3270 IF (ASSOCIATED(send_buff(i)%array)) DEALLOCATE (send_buff(i)%array)
3271 IF (ASSOCIATED(recv_buff(i)%array)) DEALLOCATE (recv_buff(i)%array)
3272 END DO
3273 DEALLOCATE (send_buff, recv_buff, send_req, recv_req)
3274 END DO !b_img
3275
3276 CALL timestop(handle)
3277
3278 END SUBROUTINE gather_ks_matrix
3279
3280! **************************************************************************************************
3281!> \brief copy all required 2c tensors from the main MPI group to the subgroups
3282!> \param t_2c_ao_tmp ...
3283!> \param ks_t_split ...
3284!> \param ks_t_sub ...
3285!> \param para_env_sub ...
3286!> \param ri_data ...
3287! **************************************************************************************************
3288 SUBROUTINE get_subgroup_ks_tensors(t_2c_ao_tmp, ks_t_split, ks_t_sub, para_env_sub, ri_data)
3289 TYPE(dbt_type), DIMENSION(:), INTENT(INOUT) :: t_2c_ao_tmp, ks_t_split
3290 TYPE(dbt_type), DIMENSION(:, :), INTENT(INOUT) :: ks_t_sub
3291 TYPE(mp_para_env_type), POINTER :: para_env_sub
3292 TYPE(hfx_ri_type), INTENT(INOUT) :: ri_data
3293
3294 CHARACTER(len=*), PARAMETER :: routinen = 'get_subgroup_ks_tensors'
3295
3296 INTEGER :: handle, i_img, i_spin, nimg, nspins
3297 INTEGER, ALLOCATABLE, DIMENSION(:) :: dist1, dist2
3298 INTEGER, DIMENSION(2) :: pdims_2d
3299 TYPE(dbt_pgrid_type) :: pgrid_2d
3300
3301 CALL timeset(routinen, handle)
3302
3303 !Create the 2d pgrid
3304 pdims_2d = 0
3305 CALL dbt_pgrid_create(para_env_sub, pdims_2d, pgrid_2d)
3306
3307 !the AO based tensors
3308 CALL create_2c_tensor(ks_t_split(1), dist1, dist2, pgrid_2d, &
3309 ri_data%bsizes_AO_split, ri_data%bsizes_AO_split, &
3310 name="(AO | AO)")
3311 DEALLOCATE (dist1, dist2)
3312 CALL dbt_create(ks_t_split(1), ks_t_split(2))
3313
3314 CALL create_2c_tensor(t_2c_ao_tmp(1), dist1, dist2, pgrid_2d, &
3315 ri_data%bsizes_AO, ri_data%bsizes_AO, &
3316 name="(AO | AO)")
3317 DEALLOCATE (dist1, dist2)
3318
3319 nspins = SIZE(ks_t_sub, 1)
3320 nimg = SIZE(ks_t_sub, 2)
3321 DO i_img = 1, nimg
3322 DO i_spin = 1, nspins
3323 CALL dbt_create(t_2c_ao_tmp(1), ks_t_sub(i_spin, i_img))
3324 END DO
3325 END DO
3326
3327 CALL dbt_pgrid_destroy(pgrid_2d)
3328 CALL timestop(handle)
3329
3330 END SUBROUTINE get_subgroup_ks_tensors
3331
3332! **************************************************************************************************
3333!> \brief copy all required 3c tensors from the main MPI group to the subgroups
3334!> \param t_3c_work_2 ...
3335!> \param t_3c_apc ...
3336!> \param t_3c_apc_sub ...
3337!> \param para_env_sub ...
3338!> \param ri_data ...
3339! **************************************************************************************************
3340 SUBROUTINE get_subgroup_3c_tensors(t_3c_work_2, t_3c_apc, t_3c_apc_sub, &
3341 para_env_sub, ri_data)
3342 TYPE(dbt_type), DIMENSION(:), INTENT(INOUT) :: t_3c_work_2
3343 TYPE(dbt_type), DIMENSION(:, :), INTENT(INOUT) :: t_3c_apc, t_3c_apc_sub
3344 TYPE(mp_para_env_type), POINTER :: para_env_sub
3345 TYPE(hfx_ri_type), INTENT(INOUT) :: ri_data
3346
3347 CHARACTER(len=*), PARAMETER :: routinen = 'get_subgroup_3c_tensors'
3348
3349 INTEGER :: batch_size, handle, handle2, i_img, &
3350 i_ri, i_spin, ib, natom, nblks_ao, &
3351 nblks_ri, nimg, nspins
3352 INTEGER(int_8) :: nze
3353 INTEGER, ALLOCATABLE, DIMENSION(:) :: bsizes_ri_ext, bsizes_ri_ext_split, &
3354 bsizes_stack, dist1, dist2, dist3, &
3355 dist_stack, idx_to_at
3356 INTEGER, DIMENSION(3) :: pdims
3357 REAL(dp) :: occ
3358 TYPE(dbt_distribution_type) :: t_dist
3359 TYPE(dbt_pgrid_type) :: pgrid
3360 TYPE(dbt_type) :: tmp, work_atom_block, work_atom_block_sub
3361
3362 CALL timeset(routinen, handle)
3363
3364 nblks_ri = SIZE(ri_data%bsizes_RI_split)
3365 ALLOCATE (bsizes_ri_ext_split(ri_data%ncell_RI*nblks_ri))
3366 DO i_ri = 1, ri_data%ncell_RI
3367 bsizes_ri_ext_split((i_ri - 1)*nblks_ri + 1:i_ri*nblks_ri) = ri_data%bsizes_RI_split(:)
3368 END DO
3369
3370 natom = SIZE(ri_data%bsizes_RI)
3371 ALLOCATE (bsizes_ri_ext(ri_data%ncell_RI*natom))
3372 DO i_ri = 1, ri_data%ncell_RI
3373 bsizes_ri_ext((i_ri - 1)*natom + 1:i_ri*natom) = ri_data%bsizes_RI(:)
3374 END DO
3375
3376 batch_size = ri_data%kp_stack_size
3377 nblks_ao = SIZE(ri_data%bsizes_AO_split)
3378 ALLOCATE (bsizes_stack(batch_size*nblks_ao))
3379 DO ib = 1, batch_size
3380 bsizes_stack((ib - 1)*nblks_ao + 1:ib*nblks_ao) = ri_data%bsizes_AO_split(:)
3381 END DO
3382
3383 !Do the same for the t_3c_ctr_2 configuration
3384 pdims = 0
3385 CALL dbt_pgrid_create(para_env_sub, pdims, pgrid, &
3386 tensor_dims=[1, SIZE(bsizes_ri_ext_split), batch_size*SIZE(ri_data%bsizes_AO_split)])
3387
3388 !For more efficient communication, we use intermediate tensors with larger block size
3389 CALL create_3c_tensor(work_atom_block_sub, dist1, dist2, dist3, &
3390 pgrid, ri_data%bsizes_AO, bsizes_ri_ext, &
3391 ri_data%bsizes_AO, [1], [2, 3], name="(AO RI | AO)")
3392 DEALLOCATE (dist1, dist2, dist3)
3393
3394 CALL create_3c_tensor(work_atom_block, dist1, dist2, dist3, &
3395 ri_data%pgrid_1, ri_data%bsizes_AO, bsizes_ri_ext, &
3396 ri_data%bsizes_AO, [1], [2, 3], name="(AO RI | AO)")
3397 DEALLOCATE (dist1, dist2, dist3)
3398
3399 !template for t_3c_apc_sub
3400 CALL create_3c_tensor(tmp, dist1, dist2, dist3, &
3401 pgrid, ri_data%bsizes_AO_split, bsizes_ri_ext_split, &
3402 ri_data%bsizes_AO_split, [1], [2, 3], name="(AO RI | AO)")
3403
3404 !create t_3c_work_2 tensors in a distribution that matches the above
3405 ALLOCATE (dist_stack(batch_size*nblks_ao))
3406 DO ib = 1, batch_size
3407 dist_stack((ib - 1)*nblks_ao + 1:ib*nblks_ao) = dist3(:)
3408 END DO
3409
3410 CALL dbt_distribution_new(t_dist, pgrid, dist1, dist2, dist_stack)
3411 CALL dbt_create(t_3c_work_2(1), "work_2_stack", t_dist, [1], [2, 3], &
3412 ri_data%bsizes_AO_split, bsizes_ri_ext_split, bsizes_stack)
3413 CALL dbt_create(t_3c_work_2(1), t_3c_work_2(2))
3414 CALL dbt_create(t_3c_work_2(1), t_3c_work_2(3))
3415 CALL dbt_distribution_destroy(t_dist)
3416 DEALLOCATE (dist1, dist2, dist3, dist_stack)
3417
3418 !Finally copy data from t_3c_apc to the subgroups
3419 ALLOCATE (idx_to_at(SIZE(ri_data%bsizes_AO)))
3420 CALL get_idx_to_atom(idx_to_at, ri_data%bsizes_AO, ri_data%bsizes_AO)
3421 nspins = SIZE(t_3c_apc, 1)
3422 nimg = SIZE(t_3c_apc, 2)
3423 CALL timeset(routinen//"_apc", handle2)
3424 DO i_img = 1, nimg
3425 DO i_spin = 1, nspins
3426 CALL dbt_create(tmp, t_3c_apc_sub(i_spin, i_img))
3427 CALL get_tensor_occupancy(t_3c_apc(i_spin, i_img), nze, occ)
3428 IF (nze == 0) cycle
3429 CALL dbt_copy(t_3c_apc(i_spin, i_img), work_atom_block, move_data=.true.)
3430 CALL copy_3c_to_subgroup(work_atom_block_sub, work_atom_block, &
3431 ri_data%iatom_to_subgroup, 1, idx_to_at)
3432 CALL dbt_copy(work_atom_block_sub, t_3c_apc_sub(i_spin, i_img), move_data=.true.)
3433 CALL dbt_filter(t_3c_apc_sub(i_spin, i_img), ri_data%filter_eps)
3434 END DO
3435 DO i_spin = 1, nspins
3436 CALL dbt_destroy(t_3c_apc(i_spin, i_img))
3437 END DO
3438 END DO
3439 CALL timestop(handle2)
3440 CALL dbt_pgrid_destroy(pgrid)
3441 CALL dbt_destroy(tmp)
3442 CALL dbt_destroy(work_atom_block)
3443 CALL dbt_destroy(work_atom_block_sub)
3444
3445 CALL timestop(handle)
3446
3447 END SUBROUTINE get_subgroup_3c_tensors
3448
3449! **************************************************************************************************
3450!> \brief copy all required 2c force tensors from the main MPI group to the subgroups
3451!> \param t_2c_inv ...
3452!> \param t_2c_bint ...
3453!> \param t_2c_metric ...
3454!> \param mat_2c_pot ...
3455!> \param t_2c_work ...
3456!> \param rho_ao_t ...
3457!> \param rho_ao_t_sub ...
3458!> \param t_2c_der_metric ...
3459!> \param t_2c_der_metric_sub ...
3460!> \param mat_der_pot ...
3461!> \param mat_der_pot_sub ...
3462!> \param group_size ...
3463!> \param ngroups ...
3464!> \param para_env ...
3465!> \param para_env_sub ...
3466!> \param ri_data ...
3467!> \note Main MPI group tensors are deleted within this routine, for memory optimization
3468! **************************************************************************************************
3469 SUBROUTINE get_subgroup_2c_derivs(t_2c_inv, t_2c_bint, t_2c_metric, mat_2c_pot, t_2c_work, rho_ao_t, &
3470 rho_ao_t_sub, t_2c_der_metric, t_2c_der_metric_sub, mat_der_pot, &
3471 mat_der_pot_sub, group_size, ngroups, para_env, para_env_sub, ri_data)
3472 TYPE(dbt_type), DIMENSION(:), INTENT(INOUT) :: t_2c_inv, t_2c_bint, t_2c_metric
3473 TYPE(dbcsr_type), DIMENSION(:), INTENT(INOUT) :: mat_2c_pot
3474 TYPE(dbt_type), DIMENSION(:), INTENT(INOUT) :: t_2c_work
3475 TYPE(dbt_type), DIMENSION(:, :), INTENT(INOUT) :: rho_ao_t, rho_ao_t_sub, t_2c_der_metric, &
3476 t_2c_der_metric_sub
3477 TYPE(dbcsr_type), DIMENSION(:, :), INTENT(INOUT) :: mat_der_pot, mat_der_pot_sub
3478 INTEGER, INTENT(IN) :: group_size, ngroups
3479 TYPE(mp_para_env_type), POINTER :: para_env, para_env_sub
3480 TYPE(hfx_ri_type), INTENT(INOUT) :: ri_data
3481
3482 CHARACTER(len=*), PARAMETER :: routinen = 'get_subgroup_2c_derivs'
3483
3484 INTEGER :: handle, i, i_img, i_ri, i_spin, i_xyz, &
3485 iatom, iproc, j, natom, nblks, nimg, &
3486 nspins
3487 INTEGER(int_8) :: nze
3488 INTEGER, ALLOCATABLE, DIMENSION(:) :: bsizes_ri_ext, bsizes_ri_ext_split, &
3489 dist1, dist2
3490 INTEGER, DIMENSION(2) :: pdims_2d
3491 INTEGER, DIMENSION(:), POINTER :: col_dist, ri_blk_size, row_dist
3492 INTEGER, DIMENSION(:, :), POINTER :: dbcsr_pgrid
3493 REAL(dp) :: occ
3494 TYPE(dbcsr_distribution_type) :: dbcsr_dist_sub
3495 TYPE(dbt_pgrid_type) :: pgrid_2d
3496 TYPE(dbt_type) :: work, work_sub
3497
3498 CALL timeset(routinen, handle)
3499
3500 !Note: a fair portion of this routine is copied from the energy version of it
3501 !Create the 2d pgrid
3502 pdims_2d = 0
3503 CALL dbt_pgrid_create(para_env_sub, pdims_2d, pgrid_2d)
3504
3505 natom = SIZE(ri_data%bsizes_RI)
3506 nblks = SIZE(ri_data%bsizes_RI_split)
3507 ALLOCATE (bsizes_ri_ext(ri_data%ncell_RI*natom))
3508 ALLOCATE (bsizes_ri_ext_split(ri_data%ncell_RI*nblks))
3509 DO i_ri = 1, ri_data%ncell_RI
3510 bsizes_ri_ext((i_ri - 1)*natom + 1:i_ri*natom) = ri_data%bsizes_RI(:)
3511 bsizes_ri_ext_split((i_ri - 1)*nblks + 1:i_ri*nblks) = ri_data%bsizes_RI_split(:)
3512 END DO
3513
3514 !nRI x nRI 2c tensors
3515 CALL create_2c_tensor(t_2c_inv(1), dist1, dist2, pgrid_2d, &
3516 bsizes_ri_ext, bsizes_ri_ext, &
3517 name="(RI | RI)")
3518 DEALLOCATE (dist1, dist2)
3519
3520 CALL dbt_create(t_2c_inv(1), t_2c_bint(1))
3521 CALL dbt_create(t_2c_inv(1), t_2c_metric(1))
3522 DO iatom = 2, natom
3523 CALL dbt_create(t_2c_inv(1), t_2c_inv(iatom))
3524 CALL dbt_create(t_2c_inv(1), t_2c_bint(iatom))
3525 CALL dbt_create(t_2c_inv(1), t_2c_metric(iatom))
3526 END DO
3527 CALL dbt_create(t_2c_inv(1), t_2c_work(1))
3528 CALL dbt_create(t_2c_inv(1), t_2c_work(2))
3529 CALL dbt_create(t_2c_inv(1), t_2c_work(3))
3530 CALL dbt_create(t_2c_inv(1), t_2c_work(4))
3531
3532 CALL create_2c_tensor(t_2c_work(5), dist1, dist2, pgrid_2d, &
3533 bsizes_ri_ext_split, bsizes_ri_ext_split, &
3534 name="(RI | RI)")
3535 DEALLOCATE (dist1, dist2)
3536
3537 !copy the data from the main group.
3538 DO iatom = 1, natom
3539 CALL copy_2c_to_subgroup(t_2c_inv(iatom), ri_data%t_2c_inv(1, iatom), group_size, ngroups, para_env)
3540 CALL copy_2c_to_subgroup(t_2c_bint(iatom), ri_data%t_2c_int(1, iatom), group_size, ngroups, para_env)
3541 CALL copy_2c_to_subgroup(t_2c_metric(iatom), ri_data%t_2c_pot(1, iatom), group_size, ngroups, para_env)
3542 END DO
3543
3544 !This includes the derivatives of the RI metric, for which there is one per atom
3545 DO i_xyz = 1, 3
3546 DO iatom = 1, natom
3547 CALL dbt_create(t_2c_inv(1), t_2c_der_metric_sub(iatom, i_xyz))
3548 CALL copy_2c_to_subgroup(t_2c_der_metric_sub(iatom, i_xyz), t_2c_der_metric(iatom, i_xyz), &
3549 group_size, ngroups, para_env)
3550 CALL dbt_destroy(t_2c_der_metric(iatom, i_xyz))
3551 END DO
3552 END DO
3553
3554 !AO x AO 2c tensors
3555 CALL create_2c_tensor(rho_ao_t_sub(1, 1), dist1, dist2, pgrid_2d, &
3556 ri_data%bsizes_AO_split, ri_data%bsizes_AO_split, &
3557 name="(AO | AO)")
3558 DEALLOCATE (dist1, dist2)
3559 nspins = SIZE(rho_ao_t, 1)
3560 nimg = SIZE(rho_ao_t, 2)
3561
3562 DO i_img = 1, nimg
3563 DO i_spin = 1, nspins
3564 IF (.NOT. (i_img == 1 .AND. i_spin == 1)) THEN
3565 CALL dbt_create(rho_ao_t_sub(1, 1), rho_ao_t_sub(i_spin, i_img))
3566 END IF
3567 CALL copy_2c_to_subgroup(rho_ao_t_sub(i_spin, i_img), rho_ao_t(i_spin, i_img), &
3568 group_size, ngroups, para_env)
3569 CALL dbt_destroy(rho_ao_t(i_spin, i_img))
3570 END DO
3571 END DO
3572
3573 !The RIxRI matrices, going through tensors
3574 CALL create_2c_tensor(work_sub, dist1, dist2, pgrid_2d, &
3575 ri_data%bsizes_RI, ri_data%bsizes_RI, &
3576 name="(RI | RI)")
3577 CALL dbt_create(ri_data%kp_mat_2c_pot(1, 1), work)
3578
3579 ALLOCATE (dbcsr_pgrid(0:pdims_2d(1) - 1, 0:pdims_2d(2) - 1))
3580 iproc = 0
3581 DO i = 0, pdims_2d(1) - 1
3582 DO j = 0, pdims_2d(2) - 1
3583 dbcsr_pgrid(i, j) = iproc
3584 iproc = iproc + 1
3585 END DO
3586 END DO
3587
3588 !We need to have the same exact 2d block dist as the tensors
3589 ALLOCATE (col_dist(natom), row_dist(natom))
3590 row_dist(:) = dist1(:)
3591 col_dist(:) = dist2(:)
3592
3593 ALLOCATE (ri_blk_size(natom))
3594 ri_blk_size(:) = ri_data%bsizes_RI(:)
3595
3596 CALL dbcsr_distribution_new(dbcsr_dist_sub, group=para_env_sub%get_handle(), pgrid=dbcsr_pgrid, &
3597 row_dist=row_dist, col_dist=col_dist)
3598 CALL dbcsr_create(mat_2c_pot(1), dist=dbcsr_dist_sub, name="sub", matrix_type=dbcsr_type_no_symmetry, &
3599 row_blk_size=ri_blk_size, col_blk_size=ri_blk_size)
3600
3601 !The HFX potential
3602 DO i_img = 1, nimg
3603 IF (i_img > 1) CALL dbcsr_create(mat_2c_pot(i_img), template=mat_2c_pot(1))
3604 CALL dbt_copy_matrix_to_tensor(ri_data%kp_mat_2c_pot(1, i_img), work)
3605 CALL get_tensor_occupancy(work, nze, occ)
3606 IF (nze == 0) cycle
3607
3608 CALL copy_2c_to_subgroup(work_sub, work, group_size, ngroups, para_env)
3609 CALL dbt_copy_tensor_to_matrix(work_sub, mat_2c_pot(i_img))
3610 CALL dbcsr_filter(mat_2c_pot(i_img), ri_data%filter_eps)
3611 CALL dbt_clear(work_sub)
3612 END DO
3613
3614 !The derivatives of the HFX potential
3615 DO i_xyz = 1, 3
3616 DO i_img = 1, nimg
3617 CALL dbcsr_create(mat_der_pot_sub(i_img, i_xyz), template=mat_2c_pot(1))
3618 CALL dbt_copy_matrix_to_tensor(mat_der_pot(i_img, i_xyz), work)
3619 CALL dbcsr_release(mat_der_pot(i_img, i_xyz))
3620 CALL get_tensor_occupancy(work, nze, occ)
3621 IF (nze == 0) cycle
3622
3623 CALL copy_2c_to_subgroup(work_sub, work, group_size, ngroups, para_env)
3624 CALL dbt_copy_tensor_to_matrix(work_sub, mat_der_pot_sub(i_img, i_xyz))
3625 CALL dbcsr_filter(mat_der_pot_sub(i_img, i_xyz), ri_data%filter_eps)
3626 CALL dbt_clear(work_sub)
3627 END DO
3628 END DO
3629
3630 CALL dbt_destroy(work)
3631 CALL dbt_destroy(work_sub)
3632 CALL dbt_pgrid_destroy(pgrid_2d)
3633 CALL dbcsr_distribution_release(dbcsr_dist_sub)
3634 DEALLOCATE (col_dist, row_dist, ri_blk_size, dbcsr_pgrid)
3635
3636 CALL timestop(handle)
3637
3638 END SUBROUTINE get_subgroup_2c_derivs
3639
3640! **************************************************************************************************
3641!> \brief copy all required 3c derivative tensors from the main MPI group to the subgroups
3642!> \param t_3c_work_2 ...
3643!> \param t_3c_work_3 ...
3644!> \param t_3c_der_AO ...
3645!> \param t_3c_der_AO_sub ...
3646!> \param t_3c_der_RI ...
3647!> \param t_3c_der_RI_sub ...
3648!> \param t_3c_apc ...
3649!> \param t_3c_apc_sub ...
3650!> \param t_3c_der_stack ...
3651!> \param para_env_sub ...
3652!> \param ri_data ...
3653!> \note the tensor containing the derivatives in the main MPI group are deleted for memory
3654! **************************************************************************************************
3655 SUBROUTINE get_subgroup_3c_derivs(t_3c_work_2, t_3c_work_3, t_3c_der_AO, t_3c_der_AO_sub, &
3656 t_3c_der_RI, t_3c_der_RI_sub, t_3c_apc, t_3c_apc_sub, &
3657 t_3c_der_stack, para_env_sub, &
3658 ri_data)
3659 TYPE(dbt_type), DIMENSION(:), INTENT(INOUT) :: t_3c_work_2, t_3c_work_3
3660 TYPE(dbt_type), DIMENSION(:, :), INTENT(INOUT) :: t_3c_der_ao, t_3c_der_ao_sub, &
3661 t_3c_der_ri, t_3c_der_ri_sub, &
3662 t_3c_apc, t_3c_apc_sub
3663 TYPE(dbt_type), DIMENSION(:), INTENT(INOUT) :: t_3c_der_stack
3664 TYPE(mp_para_env_type), POINTER :: para_env_sub
3665 TYPE(hfx_ri_type), INTENT(INOUT) :: ri_data
3666
3667 CHARACTER(len=*), PARAMETER :: routinen = 'get_subgroup_3c_derivs'
3668
3669 INTEGER :: batch_size, handle, i_img, i_ri, i_spin, &
3670 i_xyz, ib, nblks_ao, nblks_ri, nimg, &
3671 nspins, pdims(3)
3672 INTEGER(int_8) :: nze
3673 INTEGER, ALLOCATABLE, DIMENSION(:) :: bsizes_ri_ext, bsizes_ri_ext_split, &
3674 bsizes_stack, dist1, dist2, dist3, &
3675 dist_stack, idx_to_at
3676 REAL(dp) :: occ
3677 TYPE(dbt_distribution_type) :: t_dist
3678 TYPE(dbt_pgrid_type) :: pgrid
3679 TYPE(dbt_type) :: tmp, work_atom_block, work_atom_block_sub
3680
3681 CALL timeset(routinen, handle)
3682
3683 !We use intermediate tensors with larger block size for more optimized communication
3684 nimg = ri_data%nimg
3685 batch_size = ri_data%kp_stack_size
3686 nblks_ri = SIZE(ri_data%bsizes_RI)
3687 ALLOCATE (bsizes_ri_ext(ri_data%ncell_RI*nblks_ri))
3688 DO i_ri = 1, ri_data%ncell_RI
3689 bsizes_ri_ext((i_ri - 1)*nblks_ri + 1:i_ri*nblks_ri) = ri_data%bsizes_RI(:)
3690 END DO
3691
3692 nblks_ri = SIZE(ri_data%bsizes_RI_split)
3693 ALLOCATE (bsizes_ri_ext_split(ri_data%ncell_RI*nblks_ri))
3694 DO i_ri = 1, ri_data%ncell_RI
3695 bsizes_ri_ext_split((i_ri - 1)*nblks_ri + 1:i_ri*nblks_ri) = ri_data%bsizes_RI_split(:)
3696 END DO
3697 pdims = 0
3698 CALL dbt_pgrid_create(para_env_sub, pdims, pgrid, &
3699 tensor_dims=[SIZE(bsizes_ri_ext_split), 1, batch_size*SIZE(ri_data%bsizes_AO_split)])
3700
3701 CALL create_3c_tensor(tmp, dist1, dist2, dist3, pgrid, bsizes_ri_ext_split, &
3702 ri_data%bsizes_AO_split, ri_data%bsizes_AO_split, [1], [2, 3], name="(RI | AO AO)")
3703 DEALLOCATE (dist1, dist2, dist3)
3704
3705 CALL create_3c_tensor(work_atom_block_sub, dist1, dist2, dist3, &
3706 pgrid, bsizes_ri_ext, ri_data%bsizes_AO, &
3707 ri_data%bsizes_AO, [1], [2, 3], name="(RI | AO AO)")
3708 DEALLOCATE (dist1, dist2, dist3)
3709
3710 CALL create_3c_tensor(work_atom_block, dist1, dist2, dist3, &
3711 ri_data%pgrid_2, bsizes_ri_ext, ri_data%bsizes_AO, &
3712 ri_data%bsizes_AO, [1], [2, 3], name="(RI | AO AO)")
3713 DEALLOCATE (dist1, dist2, dist3)
3714 CALL dbt_pgrid_destroy(pgrid)
3715
3716 ! SCF retains G=VB. The force contractions also need B itself, so build
3717 ! its subgroup copy only when forces are requested, using the original layout.
3718 IF (.NOT. ALLOCATED(ri_data%kp_t_3c_int)) THEN
3719 ALLOCATE (ri_data%kp_t_3c_int(nimg))
3720 DO i_img = 1, nimg
3721 CALL dbt_create(tmp, ri_data%kp_t_3c_int(i_img))
3722 CALL get_tensor_occupancy(ri_data%t_3c_int_ctr_1(1, i_img), nze, occ)
3723 IF (nze == 0) cycle
3724 CALL dbt_copy(ri_data%t_3c_int_ctr_1(1, i_img), work_atom_block, order=[2, 1, 3])
3725 CALL copy_3c_to_subgroup(work_atom_block_sub, work_atom_block)
3726 CALL dbt_copy(work_atom_block_sub, ri_data%kp_t_3c_int(i_img), move_data=.true.)
3727 CALL dbt_filter(ri_data%kp_t_3c_int(i_img), ri_data%filter_eps)
3728 END DO
3729 END IF
3730 CALL dbt_destroy(tmp)
3731
3732 !We use the 3c integrals on the subgroup as template for the derivatives
3733 DO i_xyz = 1, 3
3734 DO i_img = 1, nimg
3735 CALL dbt_create(ri_data%kp_t_3c_int(1), t_3c_der_ao_sub(i_img, i_xyz))
3736 CALL get_tensor_occupancy(t_3c_der_ao(i_img, i_xyz), nze, occ)
3737 IF (nze == 0) cycle
3738
3739 CALL dbt_copy(t_3c_der_ao(i_img, i_xyz), work_atom_block, move_data=.true.)
3740 CALL copy_3c_to_subgroup(work_atom_block_sub, work_atom_block)
3741 CALL dbt_copy(work_atom_block_sub, t_3c_der_ao_sub(i_img, i_xyz), move_data=.true.)
3742 CALL dbt_filter(t_3c_der_ao_sub(i_img, i_xyz), ri_data%filter_eps)
3743 END DO
3744
3745 DO i_img = 1, nimg
3746 CALL dbt_create(ri_data%kp_t_3c_int(1), t_3c_der_ri_sub(i_img, i_xyz))
3747 CALL get_tensor_occupancy(t_3c_der_ri(i_img, i_xyz), nze, occ)
3748 IF (nze == 0) cycle
3749
3750 CALL dbt_copy(t_3c_der_ri(i_img, i_xyz), work_atom_block, move_data=.true.)
3751 CALL copy_3c_to_subgroup(work_atom_block_sub, work_atom_block)
3752 CALL dbt_copy(work_atom_block_sub, t_3c_der_ri_sub(i_img, i_xyz), move_data=.true.)
3753 CALL dbt_filter(t_3c_der_ri_sub(i_img, i_xyz), ri_data%filter_eps)
3754 END DO
3755
3756 DO i_img = 1, nimg
3757 CALL dbt_destroy(t_3c_der_ri(i_img, i_xyz))
3758 CALL dbt_destroy(t_3c_der_ao(i_img, i_xyz))
3759 END DO
3760 END DO
3761 CALL dbt_destroy(work_atom_block_sub)
3762 CALL dbt_destroy(work_atom_block)
3763
3764 !Deal with t_3c_apc
3765 pdims = 0
3766 CALL dbt_pgrid_create(para_env_sub, pdims, pgrid, &
3767 tensor_dims=[1, SIZE(bsizes_ri_ext_split), batch_size*SIZE(ri_data%bsizes_AO_split)])
3768
3769 CALL create_3c_tensor(work_atom_block_sub, dist1, dist2, dist3, &
3770 pgrid, ri_data%bsizes_AO, bsizes_ri_ext, &
3771 ri_data%bsizes_AO, [1], [2, 3], name="(AO RI | AO)")
3772 DEALLOCATE (dist1, dist2, dist3)
3773
3774 CALL create_3c_tensor(work_atom_block, dist1, dist2, dist3, &
3775 ri_data%pgrid_1, ri_data%bsizes_AO, bsizes_ri_ext, &
3776 ri_data%bsizes_AO, [1], [2, 3], name="(AO RI | AO)")
3777 DEALLOCATE (dist1, dist2, dist3)
3778
3779 CALL create_3c_tensor(tmp, dist1, dist2, dist3, &
3780 pgrid, ri_data%bsizes_AO_split, bsizes_ri_ext_split, &
3781 ri_data%bsizes_AO_split, [1], [2, 3], name="(AO RI | AO)")
3782 DEALLOCATE (dist1, dist2, dist3)
3783
3784 ALLOCATE (idx_to_at(SIZE(ri_data%bsizes_AO)))
3785 CALL get_idx_to_atom(idx_to_at, ri_data%bsizes_AO, ri_data%bsizes_AO)
3786 nspins = SIZE(t_3c_apc, 1)
3787 DO i_img = 1, nimg
3788 DO i_spin = 1, nspins
3789 CALL dbt_create(tmp, t_3c_apc_sub(i_spin, i_img))
3790 CALL get_tensor_occupancy(t_3c_apc(i_spin, i_img), nze, occ)
3791 IF (nze == 0) cycle
3792 CALL dbt_copy(t_3c_apc(i_spin, i_img), work_atom_block, move_data=.true.)
3793 CALL copy_3c_to_subgroup(work_atom_block_sub, work_atom_block, &
3794 ri_data%iatom_to_subgroup, 1, idx_to_at)
3795 CALL dbt_copy(work_atom_block_sub, t_3c_apc_sub(i_spin, i_img), move_data=.true.)
3796 CALL dbt_filter(t_3c_apc_sub(i_spin, i_img), ri_data%filter_eps)
3797 END DO
3798 DO i_spin = 1, nspins
3799 CALL dbt_destroy(t_3c_apc(i_spin, i_img))
3800 END DO
3801 END DO
3802 CALL dbt_destroy(tmp)
3803 CALL dbt_destroy(work_atom_block)
3804 CALL dbt_destroy(work_atom_block_sub)
3805 CALL dbt_pgrid_destroy(pgrid)
3806
3807 !t_3c_work_3 based on structure of 3c integrals/derivs
3808 batch_size = ri_data%kp_stack_size
3809 nblks_ao = SIZE(ri_data%bsizes_AO_split)
3810 ALLOCATE (bsizes_stack(batch_size*nblks_ao))
3811 DO ib = 1, batch_size
3812 bsizes_stack((ib - 1)*nblks_ao + 1:ib*nblks_ao) = ri_data%bsizes_AO_split(:)
3813 END DO
3814
3815 ALLOCATE (dist1(ri_data%ncell_RI*nblks_ri), dist2(nblks_ao), dist3(nblks_ao))
3816 CALL dbt_get_info(ri_data%kp_t_3c_int(1), proc_dist_1=dist1, proc_dist_2=dist2, &
3817 proc_dist_3=dist3, pdims=pdims)
3818
3819 ALLOCATE (dist_stack(batch_size*nblks_ao))
3820 DO ib = 1, batch_size
3821 dist_stack((ib - 1)*nblks_ao + 1:ib*nblks_ao) = dist3(:)
3822 END DO
3823
3824 CALL dbt_pgrid_create(para_env_sub, pdims, pgrid)
3825 CALL dbt_distribution_new(t_dist, pgrid, dist1, dist2, dist_stack)
3826 CALL dbt_create(t_3c_work_3(1), "work_3_stack", t_dist, [1], [2, 3], &
3827 bsizes_ri_ext_split, ri_data%bsizes_AO_split, bsizes_stack)
3828 CALL dbt_create(t_3c_work_3(1), t_3c_work_3(2))
3829 CALL dbt_create(t_3c_work_3(1), t_3c_work_3(3))
3830 CALL dbt_create(t_3c_work_3(1), t_3c_work_3(4))
3831 CALL dbt_distribution_destroy(t_dist)
3832 CALL dbt_pgrid_destroy(pgrid)
3833 DEALLOCATE (dist1, dist2, dist3, dist_stack)
3834
3835 !the derivatives are stacked in the same way
3836 CALL dbt_create(t_3c_work_3(1), t_3c_der_stack(1))
3837 CALL dbt_create(t_3c_work_3(1), t_3c_der_stack(2))
3838 CALL dbt_create(t_3c_work_3(1), t_3c_der_stack(3))
3839 CALL dbt_create(t_3c_work_3(1), t_3c_der_stack(4))
3840 CALL dbt_create(t_3c_work_3(1), t_3c_der_stack(5))
3841 CALL dbt_create(t_3c_work_3(1), t_3c_der_stack(6))
3842
3843 !t_3c_work_2 based on structure of t_3c_apc
3844 ALLOCATE (dist1(nblks_ao), dist2(ri_data%ncell_RI*nblks_ri), dist3(nblks_ao))
3845 CALL dbt_get_info(t_3c_apc_sub(1, 1), proc_dist_1=dist1, proc_dist_2=dist2, &
3846 proc_dist_3=dist3, pdims=pdims)
3847
3848 ALLOCATE (dist_stack(batch_size*nblks_ao))
3849 DO ib = 1, batch_size
3850 dist_stack((ib - 1)*nblks_ao + 1:ib*nblks_ao) = dist3(:)
3851 END DO
3852
3853 CALL dbt_pgrid_create(para_env_sub, pdims, pgrid)
3854 CALL dbt_distribution_new(t_dist, pgrid, dist1, dist2, dist_stack)
3855 CALL dbt_create(t_3c_work_2(1), "work_3_stack", t_dist, [1], [2, 3], &
3856 ri_data%bsizes_AO_split, bsizes_ri_ext_split, bsizes_stack)
3857 CALL dbt_create(t_3c_work_2(1), t_3c_work_2(2))
3858 CALL dbt_create(t_3c_work_2(1), t_3c_work_2(3))
3859 CALL dbt_distribution_destroy(t_dist)
3860 CALL dbt_pgrid_destroy(pgrid)
3861 DEALLOCATE (dist1, dist2, dist3, dist_stack)
3862
3863 CALL timestop(handle)
3864
3865 END SUBROUTINE get_subgroup_3c_derivs
3866
3867! **************************************************************************************************
3868!> \brief A routine that reorders the t_3c_int tensors such that all items which are fully empty
3869!> are bunched together. This way, we can get much more efficient screening based on NZE
3870!> \param t_3c_ints ...
3871!> \param ri_data ...
3872! **************************************************************************************************
3873 SUBROUTINE reorder_3c_ints(t_3c_ints, ri_data)
3874 TYPE(dbt_type), DIMENSION(:), INTENT(INOUT) :: t_3c_ints
3875 TYPE(hfx_ri_type), INTENT(INOUT) :: ri_data
3876
3877 CHARACTER(LEN=*), PARAMETER :: routinen = 'reorder_3c_ints'
3878
3879 INTEGER :: handle, i_img, idx, idx_empty, idx_full, &
3880 nimg
3881 INTEGER(int_8) :: nze
3882 REAL(dp) :: occ
3883 TYPE(dbt_type), ALLOCATABLE, DIMENSION(:) :: t_3c_tmp
3884
3885 CALL timeset(routinen, handle)
3886
3887 nimg = ri_data%nimg
3888 ALLOCATE (t_3c_tmp(nimg))
3889 DO i_img = 1, nimg
3890 CALL dbt_create(t_3c_ints(i_img), t_3c_tmp(i_img))
3891 CALL dbt_copy(t_3c_ints(i_img), t_3c_tmp(i_img), move_data=.true.)
3892 END DO
3893
3894 !Loop over the images, check if ints have NZE == 0, and put them at the start or end of the
3895 !initial tensor array. Keep the mapping in an array
3896 ALLOCATE (ri_data%idx_to_img(nimg))
3897 idx_full = 0
3898 idx_empty = nimg + 1
3899
3900 DO i_img = 1, nimg
3901 CALL get_tensor_occupancy(t_3c_tmp(i_img), nze, occ)
3902 IF (nze == 0) THEN
3903 idx_empty = idx_empty - 1
3904 CALL dbt_copy(t_3c_tmp(i_img), t_3c_ints(idx_empty), move_data=.true.)
3905 ri_data%idx_to_img(idx_empty) = i_img
3906 ELSE
3907 idx_full = idx_full + 1
3908 CALL dbt_copy(t_3c_tmp(i_img), t_3c_ints(idx_full), move_data=.true.)
3909 ri_data%idx_to_img(idx_full) = i_img
3910 END IF
3911 CALL dbt_destroy(t_3c_tmp(i_img))
3912 END DO
3913
3914 !store the highest image index with non-zero integrals
3915 ri_data%nimg_nze = idx_full
3916
3917 ALLOCATE (ri_data%img_to_idx(nimg))
3918 DO idx = 1, nimg
3919 ri_data%img_to_idx(ri_data%idx_to_img(idx)) = idx
3920 END DO
3921
3922 CALL timestop(handle)
3923
3924 END SUBROUTINE reorder_3c_ints
3925
3926! **************************************************************************************************
3927!> \brief A routine that reorders the 3c derivatives, the same way that the integrals are, also to
3928!> increase efficiency of screening
3929!> \param t_3c_derivs ...
3930!> \param ri_data ...
3931! **************************************************************************************************
3932 SUBROUTINE reorder_3c_derivs(t_3c_derivs, ri_data)
3933 TYPE(dbt_type), DIMENSION(:, :), INTENT(INOUT) :: t_3c_derivs
3934 TYPE(hfx_ri_type), INTENT(INOUT) :: ri_data
3935
3936 CHARACTER(LEN=*), PARAMETER :: routinen = 'reorder_3c_derivs'
3937
3938 INTEGER :: handle, i_img, i_xyz, idx, nimg
3939 INTEGER(int_8) :: nze
3940 REAL(dp) :: occ
3941 TYPE(dbt_type), ALLOCATABLE, DIMENSION(:) :: t_3c_tmp
3942
3943 CALL timeset(routinen, handle)
3944
3945 nimg = ri_data%nimg
3946 ALLOCATE (t_3c_tmp(nimg))
3947 DO i_img = 1, nimg
3948 CALL dbt_create(t_3c_derivs(1, 1), t_3c_tmp(i_img))
3949 END DO
3950
3951 DO i_xyz = 1, 3
3952 DO i_img = 1, nimg
3953 CALL dbt_copy(t_3c_derivs(i_img, i_xyz), t_3c_tmp(i_img), move_data=.true.)
3954 END DO
3955 DO i_img = 1, nimg
3956 idx = ri_data%img_to_idx(i_img)
3957 CALL dbt_copy(t_3c_tmp(i_img), t_3c_derivs(idx, i_xyz), move_data=.true.)
3958 CALL get_tensor_occupancy(t_3c_derivs(idx, i_xyz), nze, occ)
3959 IF (nze > 0) ri_data%nimg_nze = max(idx, ri_data%nimg_nze)
3960 END DO
3961 END DO
3962
3963 DO i_img = 1, nimg
3964 CALL dbt_destroy(t_3c_tmp(i_img))
3965 END DO
3966
3967 CALL timestop(handle)
3968
3969 END SUBROUTINE reorder_3c_derivs
3970
3971! **************************************************************************************************
3972!> \brief Get the sparsity pattern related to the non-symmetric AO basis overlap neighbor list
3973!> \param pattern ...
3974!> \param ri_data ...
3975!> \param qs_env ...
3976! **************************************************************************************************
3977 SUBROUTINE get_sparsity_pattern(pattern, ri_data, qs_env)
3978 INTEGER, DIMENSION(:, :, :), INTENT(INOUT) :: pattern
3979 TYPE(hfx_ri_type), INTENT(INOUT) :: ri_data
3980 TYPE(qs_environment_type), POINTER :: qs_env
3981
3982 INTEGER :: iatom, j_img, jatom, mj_img, natom, nimg
3983 INTEGER, ALLOCATABLE, DIMENSION(:) :: bins
3984 INTEGER, ALLOCATABLE, DIMENSION(:, :, :) :: tmp_pattern
3985 INTEGER, DIMENSION(3) :: cell_j
3986 INTEGER, DIMENSION(:, :), POINTER :: index_to_cell
3987 INTEGER, DIMENSION(:, :, :), POINTER :: cell_to_index
3988 TYPE(dft_control_type), POINTER :: dft_control
3989 TYPE(kpoint_type), POINTER :: kpoints
3990 TYPE(mp_para_env_type), POINTER :: para_env
3992 DIMENSION(:), POINTER :: nl_iterator
3993 TYPE(neighbor_list_set_p_type), DIMENSION(:), &
3994 POINTER :: nl_2c
3995
3996 NULLIFY (nl_2c, nl_iterator, kpoints, cell_to_index, dft_control, index_to_cell, para_env)
3997
3998 CALL get_qs_env(qs_env, kpoints=kpoints, dft_control=dft_control, para_env=para_env, natom=natom)
3999 CALL get_kpoint_info(kpoints, cell_to_index=cell_to_index, index_to_cell=index_to_cell, sab_nl=nl_2c)
4000
4001 nimg = ri_data%nimg
4002 pattern(:, :, :) = 0
4003
4004 !We use the symmetric nl for all images that have an opposite cell
4005 CALL neighbor_list_iterator_create(nl_iterator, nl_2c)
4006 DO WHILE (neighbor_list_iterate(nl_iterator) == 0)
4007 CALL get_iterator_info(nl_iterator, iatom=iatom, jatom=jatom, cell=cell_j)
4008
4009 j_img = cell_to_index(cell_j(1), cell_j(2), cell_j(3))
4010 IF (j_img > nimg .OR. j_img < 1) cycle
4011
4012 mj_img = get_opp_index(j_img, qs_env)
4013 IF (mj_img > nimg .OR. mj_img < 1) cycle
4014
4015 IF (ri_data%present_images(j_img) == 0) cycle
4016
4017 pattern(iatom, jatom, j_img) = 1
4018 END DO
4019 CALL neighbor_list_iterator_release(nl_iterator)
4020
4021 !If there is no opposite cell present, then we take into account the non-symmetric nl
4022 CALL get_kpoint_info(kpoints, sab_nl_nosym=nl_2c)
4023
4024 CALL neighbor_list_iterator_create(nl_iterator, nl_2c)
4025 DO WHILE (neighbor_list_iterate(nl_iterator) == 0)
4026 CALL get_iterator_info(nl_iterator, iatom=iatom, jatom=jatom, cell=cell_j)
4027
4028 j_img = cell_to_index(cell_j(1), cell_j(2), cell_j(3))
4029 IF (j_img > nimg .OR. j_img < 1) cycle
4030
4031 mj_img = get_opp_index(j_img, qs_env)
4032 IF (mj_img <= nimg .AND. mj_img > 0) cycle
4033
4034 IF (ri_data%present_images(j_img) == 0) cycle
4035
4036 pattern(iatom, jatom, j_img) = 1
4037 END DO
4038 CALL neighbor_list_iterator_release(nl_iterator)
4039
4040 CALL para_env%sum(pattern)
4041
4042 !If the opposite image is considered, then there is no need to compute diagonal twice
4043 DO j_img = 2, nimg
4044 DO iatom = 1, natom
4045 IF (pattern(iatom, iatom, j_img) /= 0) THEN
4046 mj_img = get_opp_index(j_img, qs_env)
4047 IF (mj_img > nimg .OR. mj_img < 1) cycle
4048 pattern(iatom, iatom, mj_img) = 0
4049 END IF
4050 END DO
4051 END DO
4052
4053 ! We want to equilibrate the sparsity pattern such that there are same amount of blocks
4054 ! for each atom i of i,j pairs
4055 ALLOCATE (bins(natom))
4056 bins(:) = 0
4057
4058 ALLOCATE (tmp_pattern(natom, natom, nimg))
4059 tmp_pattern(:, :, :) = 0
4060 DO j_img = 1, nimg
4061 DO jatom = 1, natom
4062 DO iatom = 1, natom
4063 IF (pattern(iatom, jatom, j_img) == 0) cycle
4064 mj_img = get_opp_index(j_img, qs_env)
4065
4066 !Should we take the i,j,b or th j,i,-b atomic block?
4067 IF (mj_img > nimg .OR. mj_img < 1) THEN
4068 !No opposite image, no choice
4069 bins(iatom) = bins(iatom) + 1
4070 tmp_pattern(iatom, jatom, j_img) = 1
4071 ELSE
4072
4073 IF (bins(iatom) > bins(jatom)) THEN
4074 bins(jatom) = bins(jatom) + 1
4075 tmp_pattern(jatom, iatom, mj_img) = 1
4076 ELSE
4077 bins(iatom) = bins(iatom) + 1
4078 tmp_pattern(iatom, jatom, j_img) = 1
4079 END IF
4080 END IF
4081 END DO
4082 END DO
4083 END DO
4084
4085 ! -1 => unoccupied, 0 => occupied
4086 pattern(:, :, :) = tmp_pattern(:, :, :) - 1
4087
4088 END SUBROUTINE get_sparsity_pattern
4089
4090! **************************************************************************************************
4091!> \brief Distribute the iatom, jatom, b_img triplet over the subgroupd to spread the load
4092!> the group id for each triplet is passed as the value of sparsity_pattern(i, j, b),
4093!> with -1 being an unoccupied block
4094!> \param sparsity_pattern ...
4095!> \param ngroups ...
4096!> \param ri_data ...
4097! **************************************************************************************************
4098 SUBROUTINE get_sub_dist(sparsity_pattern, ngroups, ri_data)
4099 INTEGER, DIMENSION(:, :, :), INTENT(INOUT) :: sparsity_pattern
4100 INTEGER, INTENT(IN) :: ngroups
4101 TYPE(hfx_ri_type), INTENT(INOUT) :: ri_data
4102
4103 INTEGER :: b_img, ctr, iat, iatom, igroup, jatom, &
4104 natom, nimg, ub
4105 INTEGER, ALLOCATABLE, DIMENSION(:) :: max_at_per_group
4106 REAL(dp) :: cost
4107 REAL(dp), ALLOCATABLE, DIMENSION(:) :: bins
4108
4109 natom = SIZE(sparsity_pattern, 2)
4110 nimg = SIZE(sparsity_pattern, 3)
4111
4112 !To avoid unnecessary data replication accross the subgroups, we want to have a limited number
4113 !of subgroup with the data of a given iatom. At the minimum, all groups have 1 atom
4114 !We assume that the cost associated to each iatom is roughly the same
4115 IF (.NOT. ALLOCATED(ri_data%iatom_to_subgroup)) THEN
4116 ALLOCATE (ri_data%iatom_to_subgroup(natom), max_at_per_group(ngroups))
4117 DO iatom = 1, natom
4118 NULLIFY (ri_data%iatom_to_subgroup(iatom)%array)
4119 ALLOCATE (ri_data%iatom_to_subgroup(iatom)%array(ngroups))
4120 ri_data%iatom_to_subgroup(iatom)%array(:) = .false.
4121 END DO
4122
4123 ub = natom/ngroups
4124 IF (ub*ngroups < natom) ub = ub + 1
4125 max_at_per_group(:) = max(1, ub)
4126
4127 !We want each atom to be present the same amount of times. Some groups might have more atoms
4128 !than other to achieve this.
4129 ctr = 0
4130 DO WHILE (modulo(sum(max_at_per_group), natom) /= 0)
4131 igroup = modulo(ctr, ngroups) + 1
4132 max_at_per_group(igroup) = max_at_per_group(igroup) + 1
4133 ctr = ctr + 1
4134 END DO
4135
4136 ctr = 0
4137 DO igroup = 1, ngroups
4138 DO iat = 1, max_at_per_group(igroup)
4139 iatom = modulo(ctr, natom) + 1
4140 ri_data%iatom_to_subgroup(iatom)%array(igroup) = .true.
4141 ctr = ctr + 1
4142 END DO
4143 END DO
4144 END IF
4145
4146 ALLOCATE (bins(ngroups))
4147 bins = 0.0_dp
4148 DO b_img = 1, nimg
4149 DO jatom = 1, natom
4150 DO iatom = 1, natom
4151 IF (sparsity_pattern(iatom, jatom, b_img) == -1) cycle
4152 igroup = minloc(bins, 1, mask=ri_data%iatom_to_subgroup(iatom)%array) - 1
4153
4154 !Use cost information from previous SCF if available
4155 IF (any(ri_data%kp_cost > epsilon(0.0_dp))) THEN
4156 cost = ri_data%kp_cost(iatom, jatom, b_img)
4157 ELSE
4158 cost = real(ri_data%bsizes_AO(iatom)*ri_data%bsizes_AO(jatom), dp)
4159 END IF
4160 bins(igroup + 1) = bins(igroup + 1) + cost
4161 sparsity_pattern(iatom, jatom, b_img) = igroup
4162 END DO
4163 END DO
4164 END DO
4165
4166 END SUBROUTINE get_sub_dist
4167
4168! **************************************************************************************************
4169!> \brief A rouine that updates the sparsity pattern for force calculation, where all i,j,b combinations
4170!> are visited.
4171!> \param force_pattern ...
4172!> \param scf_pattern ...
4173!> \param ngroups ...
4174!> \param ri_data ...
4175!> \param qs_env ...
4176! **************************************************************************************************
4177 SUBROUTINE update_pattern_to_forces(force_pattern, scf_pattern, ngroups, ri_data, qs_env)
4178 INTEGER, DIMENSION(:, :, :), INTENT(INOUT) :: force_pattern, scf_pattern
4179 INTEGER, INTENT(IN) :: ngroups
4180 TYPE(hfx_ri_type), INTENT(INOUT) :: ri_data
4181 TYPE(qs_environment_type), POINTER :: qs_env
4182
4183 INTEGER :: b_img, iatom, igroup, jatom, mb_img, &
4184 natom, nimg
4185 REAL(dp), ALLOCATABLE, DIMENSION(:) :: bins
4186
4187 natom = SIZE(scf_pattern, 2)
4188 nimg = SIZE(scf_pattern, 3)
4189
4190 ALLOCATE (bins(ngroups))
4191 bins = 0.0_dp
4192
4193 DO b_img = 1, nimg
4194 mb_img = get_opp_index(b_img, qs_env)
4195 DO jatom = 1, natom
4196 DO iatom = 1, natom
4197 !Important: same distribution as KS matrix, because reuse t_3c_apc
4198 igroup = minloc(bins, 1, mask=ri_data%iatom_to_subgroup(iatom)%array) - 1
4199
4200 !check that block not already treated
4201 IF (scf_pattern(iatom, jatom, b_img) > -1) cycle
4202
4203 !If not, take the cost of block j, i, -b (same energy contribution)
4204 IF (mb_img > 0 .AND. mb_img <= nimg) THEN
4205 IF (scf_pattern(jatom, iatom, mb_img) == -1) cycle
4206 bins(igroup + 1) = bins(igroup + 1) + ri_data%kp_cost(jatom, iatom, mb_img)
4207 force_pattern(iatom, jatom, b_img) = igroup
4208 END IF
4209 END DO
4210 END DO
4211 END DO
4212
4213 END SUBROUTINE update_pattern_to_forces
4214
4215! **************************************************************************************************
4216!> \brief A routine that determines the extend of the KP RI-HFX periodic images, including for the
4217!> extension of the RI basis
4218!> \param ri_data ...
4219!> \param qs_env ...
4220! **************************************************************************************************
4221 SUBROUTINE get_kp_and_ri_images(ri_data, qs_env)
4222 TYPE(hfx_ri_type), INTENT(INOUT) :: ri_data
4223 TYPE(qs_environment_type), POINTER :: qs_env
4224
4225 CHARACTER(LEN=*), PARAMETER :: routinen = 'get_kp_and_ri_images'
4226
4227 CHARACTER(LEN=512) :: warning_msg
4228 INTEGER :: cell_j(3), cell_k(3), handle, i_img, iatom, ikind, j_img, jatom, jcell, katom, &
4229 kcell, kp_index_lbounds(3), kp_index_ubounds(3), natom, ngroups, nimg, nkind, pcoord(3), &
4230 pdims(3)
4231 INTEGER, ALLOCATABLE, DIMENSION(:) :: dist_ao_1, dist_ao_2, dist_ri, &
4232 nri_per_atom, present_img, ri_cells
4233 INTEGER, DIMENSION(:, :, :), POINTER :: cell_to_index
4234 REAL(dp) :: bump_fact, dij, dik, image_range, &
4235 ri_range, rij(3), rik(3)
4236 TYPE(dbt_type) :: t_dummy
4237 TYPE(dft_control_type), POINTER :: dft_control
4238 TYPE(distribution_2d_type), POINTER :: dist_2d
4239 TYPE(distribution_3d_type) :: dist_3d
4240 TYPE(gto_basis_set_p_type), ALLOCATABLE, &
4241 DIMENSION(:), TARGET :: basis_set_ao, basis_set_ri
4242 TYPE(kpoint_type), POINTER :: kpoints
4243 TYPE(mp_cart_type) :: mp_comm_t3c
4244 TYPE(mp_para_env_type), POINTER :: para_env
4245 TYPE(neighbor_list_3c_iterator_type) :: nl_3c_iter
4246 TYPE(neighbor_list_3c_type) :: nl_3c
4248 DIMENSION(:), POINTER :: nl_iterator
4249 TYPE(neighbor_list_set_p_type), DIMENSION(:), &
4250 POINTER :: nl_2c
4251 TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
4252 TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
4253 TYPE(section_vals_type), POINTER :: hfx_section
4254
4255 NULLIFY (qs_kind_set, dist_2d, nl_2c, nl_iterator, dft_control, &
4256 particle_set, kpoints, para_env, cell_to_index, hfx_section)
4257
4258 CALL timeset(routinen, handle)
4259
4260 CALL get_qs_env(qs_env, nkind=nkind, qs_kind_set=qs_kind_set, distribution_2d=dist_2d, &
4261 dft_control=dft_control, particle_set=particle_set, kpoints=kpoints, &
4262 para_env=para_env, natom=natom)
4263 nimg = dft_control%nimages
4264 CALL get_kpoint_info(kpoints, cell_to_index=cell_to_index)
4265 kp_index_lbounds = lbound(cell_to_index)
4266 kp_index_ubounds = ubound(cell_to_index)
4267
4268 hfx_section => section_vals_get_subs_vals(qs_env%input, "DFT%XC%HF%RI")
4269 CALL section_vals_val_get(hfx_section, "KP_NGROUPS", i_val=ngroups)
4270
4271 ALLOCATE (basis_set_ri(nkind), basis_set_ao(nkind))
4272 CALL basis_set_list_setup(basis_set_ri, ri_data%ri_basis_type, qs_kind_set)
4273 CALL basis_set_list_setup(basis_set_ao, ri_data%orb_basis_type, qs_kind_set)
4274
4275 !In case of shortrange HFX potential, it is imprtant to be consistent with the rest of the KP
4276 !code, and use EPS_SCHWARZ to determine the range (rather than eps_filter_2c in normal RI-HFX)
4277 IF (ri_data%hfx_pot%potential_type == do_potential_short) THEN
4278 CALL erfc_cutoff(ri_data%eps_schwarz, ri_data%hfx_pot%omega, ri_data%hfx_pot%cutoff_radius)
4279 WRITE (warning_msg, '(A)') &
4280 "The SHORTANGE HFX potential typically extends over many periodic images, "// &
4281 "possibly slowing down the calculation. Consider using the TRUNCATED "// &
4282 "potential for better computational performance."
4283 cpwarn(warning_msg)
4284 END IF
4285
4286 !Determine the range for contributing periodic images, and for the RI basis extension
4287 ri_data%kp_RI_range = 0.0_dp
4288 ri_data%kp_image_range = 0.0_dp
4289 DO ikind = 1, nkind
4290
4291 CALL init_interaction_radii_orb_basis(basis_set_ao(ikind)%gto_basis_set, ri_data%eps_pgf_orb)
4292 CALL get_gto_basis_set(basis_set_ao(ikind)%gto_basis_set, kind_radius=ri_range)
4293 ri_data%kp_RI_range = max(ri_range, ri_data%kp_RI_range)
4294
4295 CALL init_interaction_radii_orb_basis(basis_set_ao(ikind)%gto_basis_set, ri_data%eps_pgf_orb)
4296 CALL init_interaction_radii_orb_basis(basis_set_ri(ikind)%gto_basis_set, ri_data%eps_pgf_orb)
4297 CALL get_gto_basis_set(basis_set_ri(ikind)%gto_basis_set, kind_radius=image_range)
4298
4299 image_range = 2.0_dp*image_range + cutoff_screen_factor*ri_data%hfx_pot%cutoff_radius
4300 ri_data%kp_image_range = max(image_range, ri_data%kp_image_range)
4301 END DO
4302
4303 CALL section_vals_val_get(hfx_section, "KP_RI_BUMP_FACTOR", r_val=bump_fact)
4304 ri_data%kp_bump_rad = bump_fact*ri_data%kp_RI_range
4305
4306 !For the extent of the KP RI-HFX images, we are limited by the RI-HFX potential in
4307 !(mu^0 sigma^a|P^0) (P^0|Q^b) (Q^b|nu^b lambda^a+c), if there is no contact between
4308 !any P^0 and Q^b, then image b does not contribute
4309 CALL build_2c_neighbor_lists(nl_2c, basis_set_ri, basis_set_ri, ri_data%hfx_pot, &
4310 "HFX_2c_nl_RI", qs_env, sym_ij=.false., dist_2d=dist_2d)
4311
4312 ALLOCATE (present_img(nimg))
4313 present_img = 0
4314 ri_data%nimg = 0
4315 CALL neighbor_list_iterator_create(nl_iterator, nl_2c)
4316 DO WHILE (neighbor_list_iterate(nl_iterator) == 0)
4317 CALL get_iterator_info(nl_iterator, r=rij, cell=cell_j)
4318
4319 dij = norm2(rij)
4320
4321 j_img = cell_to_index(cell_j(1), cell_j(2), cell_j(3))
4322 IF (j_img > nimg .OR. j_img < 1) cycle
4323
4324 IF (dij > ri_data%kp_image_range) cycle
4325
4326 ri_data%nimg = max(j_img, ri_data%nimg)
4327 present_img(j_img) = 1
4328
4329 END DO
4330 CALL neighbor_list_iterator_release(nl_iterator)
4331 CALL release_neighbor_list_sets(nl_2c)
4332 CALL para_env%max(ri_data%nimg)
4333 IF (ri_data%nimg > nimg) THEN
4334 cpabort("Make sure the smallest exponent of the RI-HFX basis is larger than that of the ORB basis.")
4335 END IF
4336
4337 !Keep track of which images will not contribute, so that can be ignored before calculation
4338 CALL para_env%sum(present_img)
4339 ALLOCATE (ri_data%present_images(ri_data%nimg))
4340 ri_data%present_images = 0
4341 DO i_img = 1, ri_data%nimg
4342 IF (present_img(i_img) > 0) ri_data%present_images(i_img) = 1
4343 END DO
4344
4345 CALL create_3c_tensor(t_dummy, dist_ao_1, dist_ao_2, dist_ri, &
4346 ri_data%pgrid, ri_data%bsizes_AO, ri_data%bsizes_AO, ri_data%bsizes_RI, &
4347 map1=[1, 2], map2=[3], name="(AO AO | RI)")
4348
4349 CALL dbt_mp_environ_pgrid(ri_data%pgrid, pdims, pcoord)
4350 CALL mp_comm_t3c%create(ri_data%pgrid%mp_comm_2d, 3, pdims)
4351 CALL distribution_3d_create(dist_3d, dist_ao_1, dist_ao_2, dist_ri, &
4352 nkind, particle_set, mp_comm_t3c, own_comm=.true.)
4353 DEALLOCATE (dist_ri, dist_ao_1, dist_ao_2)
4354 CALL dbt_destroy(t_dummy)
4355
4356 !For the extension of the RI basis P in (mu^0 sigma^a |P^i), we consider an atom if the distance,
4357 !between mu^0 and P^i if smaller or equal to the kind radius of mu^0
4358 CALL build_3c_neighbor_lists(nl_3c, basis_set_ao, basis_set_ao, basis_set_ri, dist_3d, &
4359 ri_data%ri_metric, "HFX_3c_nl", qs_env, op_pos=2, sym_ij=.false., &
4360 own_dist=.true.)
4361
4362 ALLOCATE (ri_cells(nimg))
4363 ri_cells = 0
4364
4365 ALLOCATE (nri_per_atom(natom))
4366 nri_per_atom = 0
4367
4368 CALL neighbor_list_3c_iterator_create(nl_3c_iter, nl_3c)
4369 DO WHILE (neighbor_list_3c_iterate(nl_3c_iter) == 0)
4370 CALL get_3c_iterator_info(nl_3c_iter, cell_k=cell_k, rik=rik, cell_j=cell_j, &
4371 iatom=iatom, jatom=jatom, katom=katom)
4372 dik = norm2(rik)
4373
4374 IF (any([cell_j(1), cell_j(2), cell_j(3)] < kp_index_lbounds) .OR. &
4375 any([cell_j(1), cell_j(2), cell_j(3)] > kp_index_ubounds)) cycle
4376
4377 jcell = cell_to_index(cell_j(1), cell_j(2), cell_j(3))
4378 IF (jcell > nimg .OR. jcell < 1) cycle
4379
4380 IF (any([cell_k(1), cell_k(2), cell_k(3)] < kp_index_lbounds) .OR. &
4381 any([cell_k(1), cell_k(2), cell_k(3)] > kp_index_ubounds)) cycle
4382
4383 kcell = cell_to_index(cell_k(1), cell_k(2), cell_k(3))
4384 IF (kcell > nimg .OR. kcell < 1) cycle
4385
4386 IF (dik > ri_data%kp_RI_range) cycle
4387 ri_cells(kcell) = 1
4388
4389 IF (jcell == 1 .AND. iatom == jatom) nri_per_atom(iatom) = nri_per_atom(iatom) + ri_data%bsizes_RI(katom)
4390 END DO
4391 CALL neighbor_list_3c_iterator_destroy(nl_3c_iter)
4392 CALL neighbor_list_3c_destroy(nl_3c)
4393 CALL para_env%sum(ri_cells)
4394 CALL para_env%sum(nri_per_atom)
4395
4396 ALLOCATE (ri_data%img_to_RI_cell(nimg))
4397 ri_data%ncell_RI = 0
4398 ri_data%img_to_RI_cell = 0
4399 DO i_img = 1, nimg
4400 IF (ri_cells(i_img) > 0) THEN
4401 ri_data%ncell_RI = ri_data%ncell_RI + 1
4402 ri_data%img_to_RI_cell(i_img) = ri_data%ncell_RI
4403 END IF
4404 END DO
4405
4406 ALLOCATE (ri_data%RI_cell_to_img(ri_data%ncell_RI))
4407 DO i_img = 1, nimg
4408 IF (ri_data%img_to_RI_cell(i_img) > 0) ri_data%RI_cell_to_img(ri_data%img_to_RI_cell(i_img)) = i_img
4409 END DO
4410
4411 !Print some info
4412 IF (ri_data%unit_nr > 0) THEN
4413 WRITE (ri_data%unit_nr, fmt="(/T3,A,I29)") &
4414 "KP-HFX_RI_INFO| Number of RI-KP parallel groups:", ngroups
4415 WRITE (ri_data%unit_nr, fmt="(T3,A,I29)") &
4416 "KP-HFX_RI_INFO| Tensor stack size: ", ri_data%kp_stack_size
4417 WRITE (ri_data%unit_nr, fmt="(T3,A,F31.3,A)") &
4418 "KP-HFX_RI_INFO| RI basis extension radius:", ri_data%kp_RI_range*angstrom, " Ang"
4419 WRITE (ri_data%unit_nr, fmt="(T3,A,F12.3,A, F6.3, A)") &
4420 "KP-HFX_RI_INFO| RI basis bump factor and bump radius:", bump_fact, " /", &
4421 ri_data%kp_bump_rad*angstrom, " Ang"
4422 WRITE (ri_data%unit_nr, fmt="(T3,A,I16,A)") &
4423 "KP-HFX_RI_INFO| The extended RI bases cover up to ", ri_data%ncell_RI, " unit cells"
4424 WRITE (ri_data%unit_nr, fmt="(T3,A,I18)") &
4425 "KP-HFX_RI_INFO| Average number of sgf in extended RI bases:", sum(nri_per_atom)/natom
4426 WRITE (ri_data%unit_nr, fmt="(T3,A,F13.3,A)") &
4427 "KP-HFX_RI_INFO| Consider all image cells within a radius of ", ri_data%kp_image_range*angstrom, " Ang"
4428 WRITE (ri_data%unit_nr, fmt="(T3,A,I27/)") &
4429 "KP-HFX_RI_INFO| Number of image cells considered: ", ri_data%nimg
4430 CALL m_flush(ri_data%unit_nr)
4431 END IF
4432
4433 CALL timestop(handle)
4434
4435 END SUBROUTINE get_kp_and_ri_images
4436
4437! **************************************************************************************************
4438!> \brief A routine that creates tensors structure for rho_ao and 3c_ints in a stacked format for
4439!> the efficient contractions of rho_sigma^0,lambda^c * (mu^0 sigam^a | P) => TAS tensors
4440!> \param res_stack ...
4441!> \param rho_stack ...
4442!> \param ints_stack ...
4443!> \param rho_template ...
4444!> \param ints_template ...
4445!> \param stack_size ...
4446!> \param ri_data ...
4447!> \param qs_env ...
4448!> \note The result tensor has the exact same shape and distribution as the integral tensor
4449! **************************************************************************************************
4450 SUBROUTINE get_stack_tensors(res_stack, rho_stack, ints_stack, rho_template, ints_template, &
4451 stack_size, ri_data, qs_env)
4452 TYPE(dbt_type), DIMENSION(:), INTENT(INOUT) :: res_stack, rho_stack, ints_stack
4453 TYPE(dbt_type), INTENT(INOUT) :: rho_template, ints_template
4454 INTEGER, INTENT(IN) :: stack_size
4455 TYPE(hfx_ri_type), INTENT(INOUT) :: ri_data
4456 TYPE(qs_environment_type), POINTER :: qs_env
4457
4458 INTEGER :: is, nblks, nblks_3c(3), pdims_3d(3)
4459 INTEGER, ALLOCATABLE, DIMENSION(:) :: bsizes_ri_ext, bsizes_stack, dist1, &
4460 dist2, dist3, dist_stack1, &
4461 dist_stack2, dist_stack3
4462 TYPE(dbt_distribution_type) :: t_dist
4463 TYPE(dbt_pgrid_type) :: pgrid
4464 TYPE(mp_para_env_type), POINTER :: para_env
4465
4466 NULLIFY (para_env)
4467
4468 CALL get_qs_env(qs_env, para_env=para_env)
4469
4470 nblks = SIZE(ri_data%bsizes_AO_split)
4471 ALLOCATE (bsizes_stack(stack_size*nblks))
4472 DO is = 1, stack_size
4473 bsizes_stack((is - 1)*nblks + 1:is*nblks) = ri_data%bsizes_AO_split(:)
4474 END DO
4475
4476 ALLOCATE (dist1(nblks), dist2(nblks), dist_stack1(stack_size*nblks), dist_stack2(stack_size*nblks))
4477 CALL dbt_get_info(rho_template, proc_dist_1=dist1, proc_dist_2=dist2)
4478 DO is = 1, stack_size
4479 dist_stack1((is - 1)*nblks + 1:is*nblks) = dist1(:)
4480 dist_stack2((is - 1)*nblks + 1:is*nblks) = dist2(:)
4481 END DO
4482
4483 !First 2c tensor matches the distribution of template
4484 !It is stacked in both directions
4485 CALL dbt_distribution_new(t_dist, ri_data%pgrid_2d, dist_stack1, dist_stack2)
4486 CALL dbt_create(rho_stack(1), "RHO_stack", t_dist, [1], [2], bsizes_stack, bsizes_stack)
4487 CALL dbt_distribution_destroy(t_dist)
4488 DEALLOCATE (dist1, dist2, dist_stack1, dist_stack2)
4489
4490 !Second 2c tensor has optimal distribution on the 2d pgrid
4491 CALL create_2c_tensor(rho_stack(2), dist1, dist2, ri_data%pgrid_2d, bsizes_stack, bsizes_stack, name="RHO_stack")
4492 DEALLOCATE (dist1, dist2)
4493
4494 CALL dbt_get_info(ints_template, nblks_total=nblks_3c)
4495 ALLOCATE (dist1(nblks_3c(1)), dist2(nblks_3c(2)), dist3(nblks_3c(3)))
4496 ALLOCATE (dist_stack3(stack_size*nblks_3c(3)), bsizes_ri_ext(nblks_3c(2)))
4497 CALL dbt_get_info(ints_template, proc_dist_1=dist1, proc_dist_2=dist2, &
4498 proc_dist_3=dist3, blk_size_2=bsizes_ri_ext)
4499 DO is = 1, stack_size
4500 dist_stack3((is - 1)*nblks_3c(3) + 1:is*nblks_3c(3)) = dist3(:)
4501 END DO
4502
4503 !First 3c tensor matches the distribution of template
4504 CALL dbt_distribution_new(t_dist, ri_data%pgrid_1, dist1, dist2, dist_stack3)
4505 CALL dbt_create(ints_stack(1), "ints_stack", t_dist, [1, 2], [3], ri_data%bsizes_AO_split, &
4506 bsizes_ri_ext, bsizes_stack)
4507 CALL dbt_distribution_destroy(t_dist)
4508 DEALLOCATE (dist1, dist2, dist3, dist_stack3)
4509
4510 !Second 3c tensor has optimal pgrid
4511 pdims_3d = 0
4512 CALL dbt_pgrid_create(para_env, pdims_3d, pgrid, tensor_dims=[nblks_3c(1), nblks_3c(2), stack_size*nblks_3c(3)])
4513 CALL create_3c_tensor(ints_stack(2), dist1, dist2, dist3, pgrid, ri_data%bsizes_AO_split, &
4514 bsizes_ri_ext, bsizes_stack, [1, 2], [3], name="ints_stack")
4515 DEALLOCATE (dist1, dist2, dist3)
4516 CALL dbt_pgrid_destroy(pgrid)
4517
4518 !The result tensor has the same shape and dist as the integral tensor
4519 CALL dbt_create(ints_stack(1), res_stack(1))
4520 CALL dbt_create(ints_stack(2), res_stack(2))
4521
4522 END SUBROUTINE get_stack_tensors
4523
4524! **************************************************************************************************
4525!> \brief Fill the stack of 3c tensors accrding to the order in the images input
4526!> \param t_3c_stack ...
4527!> \param t_3c_in ...
4528!> \param images ...
4529!> \param stack_dim ...
4530!> \param ri_data ...
4531!> \param filter_at ...
4532!> \param filter_dim ...
4533!> \param idx_to_at ...
4534!> \param img_bounds ...
4535! **************************************************************************************************
4536 SUBROUTINE fill_3c_stack(t_3c_stack, t_3c_in, images, stack_dim, ri_data, filter_at, filter_dim, &
4537 idx_to_at, img_bounds)
4538 TYPE(dbt_type), INTENT(INOUT) :: t_3c_stack
4539 TYPE(dbt_type), DIMENSION(:), INTENT(INOUT) :: t_3c_in
4540 INTEGER, DIMENSION(:), INTENT(INOUT) :: images
4541 INTEGER, INTENT(IN) :: stack_dim
4542 TYPE(hfx_ri_type), INTENT(INOUT) :: ri_data
4543 INTEGER, INTENT(IN), OPTIONAL :: filter_at, filter_dim
4544 INTEGER, DIMENSION(:), INTENT(INOUT), OPTIONAL :: idx_to_at
4545 INTEGER, INTENT(IN), OPTIONAL :: img_bounds(2)
4546
4547 INTEGER :: dest(3), i_img, idx, ind(3), lb, nblks, &
4548 nimg, offset, ub
4549 LOGICAL :: do_filter
4550 TYPE(dbt_iterator_type) :: iter
4551
4552 ! Source and stack have matching block layouts and MPI owners by construction.
4553 nimg = ri_data%nimg
4554 nblks = SIZE(ri_data%bsizes_AO_split)
4555
4556 do_filter = .false.
4557 IF (PRESENT(filter_at) .AND. PRESENT(filter_dim) .AND. PRESENT(idx_to_at)) do_filter = .true.
4558
4559 lb = 1
4560 ub = nimg
4561 offset = 0
4562 IF (PRESENT(img_bounds)) THEN
4563 lb = img_bounds(1)
4564 ub = img_bounds(2) - 1
4565 offset = lb - 1
4566 END IF
4567
4568 ! Each image fills a distinct stack slice, so the team can span the whole batch.
4569!$OMP PARALLEL DEFAULT(NONE) &
4570!$OMP SHARED(images,t_3c_in,t_3c_stack,nblks,nimg,lb,ub,stack_dim,filter_at,filter_dim,idx_to_at,do_filter,offset) &
4571!$OMP PRIVATE(iter,ind,dest,i_img,idx)
4572 DO idx = lb, ub
4573 i_img = images(idx)
4574 IF (i_img == 0 .OR. i_img > nimg) cycle
4575 CALL dbt_iterator_start(iter, t_3c_in(i_img))
4576 DO WHILE (dbt_iterator_blocks_left(iter))
4577 CALL dbt_iterator_next_block(iter, ind)
4578 IF (do_filter) THEN
4579 IF (.NOT. idx_to_at(ind(filter_dim)) == filter_at) cycle
4580 END IF
4581 dest(:) = ind(:)
4582 dest(stack_dim) = ind(stack_dim) + (idx - offset - 1)*nblks
4583 CALL dbt_copy_block(t_3c_in(i_img), t_3c_stack, ind, ind_out=dest)
4584 END DO
4585 CALL dbt_iterator_stop(iter)
4586 END DO
4587!$OMP END PARALLEL
4588 CALL dbt_finalize(t_3c_stack)
4589
4590 END SUBROUTINE fill_3c_stack
4591
4592! **************************************************************************************************
4593!> \brief Fill a density-matrix stack, with images indexing both stack dimensions
4594!> \param t_2c_stack ...
4595!> \param t_2c_in ...
4596!> \param images ...
4597!> \param ri_data ...
4598! **************************************************************************************************
4599 SUBROUTINE fill_2c_stack(t_2c_stack, t_2c_in, images, ri_data)
4600 TYPE(dbt_type), INTENT(INOUT) :: t_2c_stack
4601 TYPE(dbt_type), DIMENSION(:), INTENT(INOUT) :: t_2c_in
4602 INTEGER, DIMENSION(:, :), INTENT(IN) :: images
4603 TYPE(hfx_ri_type), INTENT(INOUT) :: ri_data
4604
4605 CHARACTER(len=*), PARAMETER :: routinen = 'fill_2c_stack'
4606
4607 INTEGER :: dest(2), handle, i_img, icol, ind(2), &
4608 irow, nblks, nimg
4609 TYPE(dbt_iterator_type) :: iter
4610
4611 CALL timeset(routinen, handle)
4612
4613 ! Source and stack have matching block layouts and MPI owners by construction.
4614 nimg = ri_data%nimg
4615 nblks = SIZE(ri_data%bsizes_AO_split)
4616
4617 ! Each image pair fills a distinct slice; keep one team for the whole batch.
4618!$OMP PARALLEL DEFAULT(NONE) SHARED(images,t_2c_in,t_2c_stack,nblks,nimg) &
4619!$OMP PRIVATE(iter,ind,dest,i_img,irow,icol)
4620 DO icol = 1, SIZE(images, 2)
4621 DO irow = 1, SIZE(images, 1)
4622 i_img = images(irow, icol)
4623 IF (i_img < 1 .OR. i_img > nimg) cycle
4624 CALL dbt_iterator_start(iter, t_2c_in(i_img))
4625 DO WHILE (dbt_iterator_blocks_left(iter))
4626 CALL dbt_iterator_next_block(iter, ind)
4627 dest(:) = [(irow - 1)*nblks + ind(1), (icol - 1)*nblks + ind(2)]
4628 CALL dbt_copy_block(t_2c_in(i_img), t_2c_stack, ind, ind_out=dest)
4629 END DO
4630 CALL dbt_iterator_stop(iter)
4631 END DO
4632 END DO
4633!$OMP END PARALLEL
4634 CALL dbt_finalize(t_2c_stack)
4635 CALL timestop(handle)
4636
4637 END SUBROUTINE fill_2c_stack
4638
4639! **************************************************************************************************
4640!> \brief Accumulate a result stack into its image tensors in a single traversal
4641!> \param t_3c_apc Destination images in stack order, with matching blocks and MPI owners
4642!> \param t_stacked Result tensor stacked along the third dimension
4643!> \param img_filter Zero entries skip the corresponding destination image
4644! **************************************************************************************************
4645 SUBROUTINE accumulate_3c_stack(t_3c_apc, t_stacked, img_filter)
4646 TYPE(dbt_type), DIMENSION(:), INTENT(INOUT) :: t_3c_apc
4647 TYPE(dbt_type), INTENT(INOUT) :: t_stacked
4648 INTEGER, DIMENSION(:), INTENT(IN) :: img_filter
4649
4650 CHARACTER(len=*), PARAMETER :: routinen = 'accumulate_3c_stack'
4651
4652 INTEGER :: handle, idx, ndata
4653 INTEGER, DIMENSION(3) :: dest, ind, nblks_3c, sizes
4654 LOGICAL :: found
4655 REAL(dp), ALLOCATABLE, DIMENSION(:) :: blk
4656 TYPE(dbt_iterator_type) :: iter
4657
4658 CALL timeset(routinen, handle)
4659 CALL dbt_get_info(t_3c_apc(1), nblks_total=nblks_3c)
4660
4661 ! get_stack_tensors repeats the image distribution. Buffer reuse also supports
4662 ! the different matrix layouts of res_stack(1) and t_3c_apc.
4663!$OMP PARALLEL DEFAULT(NONE) SHARED(t_3c_apc,t_stacked,img_filter,nblks_3c) &
4664!$OMP PRIVATE(iter,ind,dest,sizes,blk,found,idx,ndata)
4665 ALLOCATE (blk(0))
4666 CALL dbt_iterator_start(iter, t_stacked)
4667 DO WHILE (dbt_iterator_blocks_left(iter))
4668 CALL dbt_iterator_next_block(iter, ind, blk_size=sizes)
4669 idx = (ind(3) - 1)/nblks_3c(3) + 1
4670 IF (idx > SIZE(t_3c_apc)) cycle
4671 IF (img_filter(idx) == 0) cycle
4672 ndata = product(sizes)
4673 IF (SIZE(blk) < ndata) THEN
4674 DEALLOCATE (blk)
4675 ALLOCATE (blk(ndata))
4676 END IF
4677 CALL dbt_get_block_data(t_stacked, ind, sizes, blk(:ndata), found)
4678 IF (.NOT. found) cycle
4679 dest(:) = [ind(1), ind(2), ind(3) - (idx - 1)*nblks_3c(3)]
4680 CALL dbt_put_block_data(t_3c_apc(idx), dest, sizes, blk(:ndata), summation=.true.)
4681 END DO
4682 CALL dbt_iterator_stop(iter)
4683 DEALLOCATE (blk)
4684!$OMP END PARALLEL
4685 DO idx = 1, SIZE(t_3c_apc)
4686 IF (img_filter(idx) /= 0) CALL dbt_finalize(t_3c_apc(idx))
4687 END DO
4688 CALL timestop(handle)
4689
4690 END SUBROUTINE accumulate_3c_stack
4691
4692! **************************************************************************************************
4693!> \brief Unstacks a stacked 3c tensor containing t_3c_apc
4694!> \param t_3c_apc ...
4695!> \param t_stacked ...
4696!> \param idx ...
4697! **************************************************************************************************
4698 SUBROUTINE unstack_t_3c_apc(t_3c_apc, t_stacked, idx)
4699 TYPE(dbt_type), INTENT(INOUT) :: t_3c_apc, t_stacked
4700 INTEGER, INTENT(IN) :: idx
4701
4702 INTEGER :: current_idx
4703 INTEGER, DIMENSION(3) :: ind, nblks_3c
4704 LOGICAL :: found
4705 REAL(dp), ALLOCATABLE, DIMENSION(:, :, :) :: blk
4706 TYPE(dbt_iterator_type) :: iter
4707
4708 !Note: t_3c_apc and t_stacked must have the same ditribution
4709 CALL dbt_get_info(t_3c_apc, nblks_total=nblks_3c)
4710
4711!$OMP PARALLEL DEFAULT(NONE) SHARED(t_3c_apc,t_stacked,idx,nblks_3c) PRIVATE(iter,ind,blk,found,current_idx)
4712 CALL dbt_iterator_start(iter, t_stacked)
4713 DO WHILE (dbt_iterator_blocks_left(iter))
4714 CALL dbt_iterator_next_block(iter, ind)
4715
4716 !tensor is stacked along the 3rd dimension
4717 current_idx = (ind(3) - 1)/nblks_3c(3) + 1
4718 IF (.NOT. idx == current_idx) cycle
4719
4720 CALL dbt_get_block(t_stacked, ind, blk, found)
4721 IF (.NOT. found) cycle
4722
4723 CALL dbt_put_block(t_3c_apc, [ind(1), ind(2), ind(3) - (idx - 1)*nblks_3c(3)], shape(blk), blk)
4724 DEALLOCATE (blk)
4725 END DO
4726 CALL dbt_iterator_stop(iter)
4727!$OMP END PARALLEL
4728
4729 END SUBROUTINE unstack_t_3c_apc
4730
4731! **************************************************************************************************
4732!> \brief copies the 3c integrals correspoinding to a single atom mu from the general (P^0| mu^0 sigam^a)
4733!> \param t_3c_at ...
4734!> \param t_3c_ints ...
4735!> \param iatom ...
4736!> \param dim_at ...
4737!> \param idx_to_at ...
4738! **************************************************************************************************
4739 SUBROUTINE get_atom_3c_ints(t_3c_at, t_3c_ints, iatom, dim_at, idx_to_at)
4740 TYPE(dbt_type), INTENT(INOUT) :: t_3c_at, t_3c_ints
4741 INTEGER, INTENT(IN) :: iatom, dim_at
4742 INTEGER, DIMENSION(:), INTENT(IN) :: idx_to_at
4743
4744 INTEGER, DIMENSION(3) :: ind
4745 LOGICAL :: found
4746 REAL(dp), ALLOCATABLE, DIMENSION(:, :, :) :: blk
4747 TYPE(dbt_iterator_type) :: iter
4748
4749!$OMP PARALLEL DEFAULT(NONE) SHARED(t_3c_ints,t_3c_at,iatom,idx_to_at,dim_at) PRIVATE(iter,ind,blk,found)
4750 CALL dbt_iterator_start(iter, t_3c_ints)
4751 DO WHILE (dbt_iterator_blocks_left(iter))
4752 CALL dbt_iterator_next_block(iter, ind)
4753 IF (.NOT. idx_to_at(ind(dim_at)) == iatom) cycle
4754
4755 CALL dbt_get_block(t_3c_ints, ind, blk, found)
4756 IF (.NOT. found) cycle
4757
4758 CALL dbt_put_block(t_3c_at, ind, shape(blk), blk)
4759 DEALLOCATE (blk)
4760 END DO
4761 CALL dbt_iterator_stop(iter)
4762!$OMP END PARALLEL
4763 CALL dbt_finalize(t_3c_at)
4764
4765 END SUBROUTINE get_atom_3c_ints
4766
4767! **************************************************************************************************
4768!> \brief Precalculate the 3c and 2c derivatives tensors
4769!> \param t_3c_der_RI ...
4770!> \param t_3c_der_AO ...
4771!> \param mat_der_pot ...
4772!> \param t_2c_der_metric ...
4773!> \param ri_data ...
4774!> \param qs_env ...
4775! **************************************************************************************************
4776 SUBROUTINE precalc_derivatives(t_3c_der_RI, t_3c_der_AO, mat_der_pot, t_2c_der_metric, ri_data, qs_env)
4777 TYPE(dbt_type), DIMENSION(:, :), INTENT(INOUT) :: t_3c_der_ri, t_3c_der_ao
4778 TYPE(dbcsr_type), DIMENSION(:, :), INTENT(INOUT) :: mat_der_pot
4779 TYPE(dbt_type), DIMENSION(:, :), INTENT(INOUT) :: t_2c_der_metric
4780 TYPE(hfx_ri_type), INTENT(INOUT) :: ri_data
4781 TYPE(qs_environment_type), POINTER :: qs_env
4782
4783 CHARACTER(LEN=*), PARAMETER :: routinen = 'precalc_derivatives'
4784
4785 INTEGER :: handle, handle2, i_img, i_mem, i_ri, &
4786 i_xyz, iatom, n_mem, natom, nblks_ri, &
4787 ncell_ri, nimg, nkind, nthreads
4788 INTEGER(int_8) :: nze
4789 INTEGER, ALLOCATABLE, DIMENSION(:) :: bsizes_ri_ext, bsizes_ri_ext_split, dist_ao_1, &
4790 dist_ao_2, dist_ri, dist_ri_ext, dummy_end, dummy_start, end_blocks, start_blocks
4791 INTEGER, DIMENSION(3) :: pcoord, pdims
4792 INTEGER, DIMENSION(:), POINTER :: col_bsize, row_bsize
4793 REAL(dp) :: occ
4794 TYPE(dbcsr_distribution_type) :: dbcsr_dist
4795 TYPE(dbcsr_type) :: dbcsr_template
4796 TYPE(dbcsr_type), ALLOCATABLE, DIMENSION(:, :) :: mat_der_metric
4797 TYPE(dbt_distribution_type) :: t_dist
4798 TYPE(dbt_pgrid_type) :: pgrid
4799 TYPE(dbt_type) :: t_3c_template
4800 TYPE(dbt_type), ALLOCATABLE, DIMENSION(:, :, :) :: t_3c_der_ao_prv, t_3c_der_ri_prv
4801 TYPE(dft_control_type), POINTER :: dft_control
4802 TYPE(distribution_2d_type), POINTER :: dist_2d
4803 TYPE(distribution_3d_type) :: dist_3d
4804 TYPE(gto_basis_set_p_type), ALLOCATABLE, &
4805 DIMENSION(:), TARGET :: basis_set_ao, basis_set_ri
4806 TYPE(mp_cart_type) :: mp_comm_t3c
4807 TYPE(mp_para_env_type), POINTER :: para_env
4808 TYPE(neighbor_list_3c_type) :: nl_3c
4809 TYPE(neighbor_list_set_p_type), DIMENSION(:), &
4810 POINTER :: nl_2c
4811 TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
4812 TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
4813
4814 NULLIFY (qs_kind_set, dist_2d, nl_2c, particle_set, dft_control, para_env, row_bsize, col_bsize)
4815
4816 CALL timeset(routinen, handle)
4817
4818 CALL get_qs_env(qs_env, nkind=nkind, qs_kind_set=qs_kind_set, distribution_2d=dist_2d, natom=natom, &
4819 particle_set=particle_set, dft_control=dft_control, para_env=para_env)
4820
4821 nimg = ri_data%nimg
4822 ncell_ri = ri_data%ncell_RI
4823
4824 ALLOCATE (basis_set_ri(nkind), basis_set_ao(nkind))
4825 CALL basis_set_list_setup(basis_set_ri, ri_data%ri_basis_type, qs_kind_set)
4826 CALL get_particle_set(particle_set, qs_kind_set, basis=basis_set_ri)
4827 CALL basis_set_list_setup(basis_set_ao, ri_data%orb_basis_type, qs_kind_set)
4828 CALL get_particle_set(particle_set, qs_kind_set, basis=basis_set_ao)
4829
4830 !Dealing with the 3c derivatives
4831 nthreads = 1
4832!$ nthreads = omp_get_num_threads()
4833 pdims = 0
4834 CALL dbt_pgrid_create(para_env, pdims, pgrid, tensor_dims=[max(1, natom/(ri_data%n_mem*nthreads)), natom, natom])
4835
4836 CALL create_3c_tensor(t_3c_template, dist_ao_1, dist_ao_2, dist_ri, pgrid, &
4837 ri_data%bsizes_AO, ri_data%bsizes_AO, ri_data%bsizes_RI, &
4838 map1=[1, 2], map2=[3], name="tmp")
4839 CALL dbt_destroy(t_3c_template)
4840
4841 !We stack the RI basis images. Keep consistent distribution
4842 nblks_ri = SIZE(ri_data%bsizes_RI_split)
4843 ALLOCATE (dist_ri_ext(natom*ncell_ri))
4844 ALLOCATE (bsizes_ri_ext(natom*ncell_ri))
4845 ALLOCATE (bsizes_ri_ext_split(nblks_ri*ncell_ri))
4846 DO i_ri = 1, ncell_ri
4847 bsizes_ri_ext((i_ri - 1)*natom + 1:i_ri*natom) = ri_data%bsizes_RI(:)
4848 dist_ri_ext((i_ri - 1)*natom + 1:i_ri*natom) = dist_ri(:)
4849 bsizes_ri_ext_split((i_ri - 1)*nblks_ri + 1:i_ri*nblks_ri) = ri_data%bsizes_RI_split(:)
4850 END DO
4851
4852 CALL dbt_distribution_new(t_dist, pgrid, dist_ao_1, dist_ao_2, dist_ri_ext)
4853 CALL dbt_create(t_3c_template, "KP_3c_der", t_dist, [1, 2], [3], &
4854 ri_data%bsizes_AO, ri_data%bsizes_AO, bsizes_ri_ext)
4855 CALL dbt_distribution_destroy(t_dist)
4856
4857 ALLOCATE (t_3c_der_ri_prv(nimg, 1, 3), t_3c_der_ao_prv(nimg, 1, 3))
4858 DO i_xyz = 1, 3
4859 DO i_img = 1, nimg
4860 CALL dbt_create(t_3c_template, t_3c_der_ri_prv(i_img, 1, i_xyz))
4861 CALL dbt_create(t_3c_template, t_3c_der_ao_prv(i_img, 1, i_xyz))
4862 END DO
4863 END DO
4864 CALL dbt_destroy(t_3c_template)
4865
4866 CALL dbt_mp_environ_pgrid(pgrid, pdims, pcoord)
4867 CALL mp_comm_t3c%create(pgrid%mp_comm_2d, 3, pdims)
4868 CALL distribution_3d_create(dist_3d, dist_ao_1, dist_ao_2, dist_ri, &
4869 nkind, particle_set, mp_comm_t3c, own_comm=.true.)
4870 DEALLOCATE (dist_ri, dist_ao_1, dist_ao_2)
4871 CALL dbt_pgrid_destroy(pgrid)
4872
4873 CALL build_3c_neighbor_lists(nl_3c, basis_set_ao, basis_set_ao, basis_set_ri, dist_3d, ri_data%ri_metric, &
4874 "HFX_3c_nl", qs_env, op_pos=2, sym_jk=.false., own_dist=.true.)
4875
4876 n_mem = ri_data%n_mem
4877 CALL create_tensor_batches(ri_data%bsizes_RI, n_mem, dummy_start, dummy_end, &
4878 start_blocks, end_blocks)
4879 DEALLOCATE (dummy_start, dummy_end)
4880
4881 CALL create_3c_tensor(t_3c_template, dist_ri, dist_ao_1, dist_ao_2, ri_data%pgrid_2, &
4882 bsizes_ri_ext_split, ri_data%bsizes_AO_split, ri_data%bsizes_AO_split, &
4883 map1=[1], map2=[2, 3], name="der (RI | AO AO)")
4884 DO i_xyz = 1, 3
4885 DO i_img = 1, nimg
4886 CALL dbt_create(t_3c_template, t_3c_der_ri(i_img, i_xyz))
4887 CALL dbt_create(t_3c_template, t_3c_der_ao(i_img, i_xyz))
4888 END DO
4889 END DO
4890
4891 DO i_mem = 1, n_mem
4892 CALL build_3c_derivatives(t_3c_der_ao_prv, t_3c_der_ri_prv, ri_data%filter_eps, qs_env, &
4893 nl_3c, basis_set_ao, basis_set_ao, basis_set_ri, &
4894 ri_data%ri_metric, der_eps=ri_data%eps_schwarz_forces, op_pos=2, &
4895 do_kpoints=.true., do_hfx_kpoints=.true., &
4896 bounds_k=[start_blocks(i_mem), end_blocks(i_mem)], &
4897 ri_range=ri_data%kp_RI_range, img_to_ri_cell=ri_data%img_to_RI_cell)
4898
4899 CALL timeset(routinen//"_cpy", handle2)
4900 !We go from (mu^0 sigma^i | P^j) to (P^i| sigma^j mu^0) and finally to (P^i| mu^0 sigma^j)
4901 DO i_img = 1, nimg
4902 DO i_xyz = 1, 3
4903 !derivative wrt to mu^0
4904 CALL get_tensor_occupancy(t_3c_der_ao_prv(i_img, 1, i_xyz), nze, occ)
4905 IF (nze > 0) THEN
4906 CALL dbt_copy(t_3c_der_ao_prv(i_img, 1, i_xyz), t_3c_template, &
4907 order=[3, 2, 1], move_data=.true.)
4908 CALL dbt_filter(t_3c_template, ri_data%filter_eps)
4909 CALL dbt_copy(t_3c_template, t_3c_der_ao(i_img, i_xyz), &
4910 order=[1, 3, 2], move_data=.true., summation=.true.)
4911 END IF
4912
4913 !derivative wrt to P^i
4914 CALL get_tensor_occupancy(t_3c_der_ri_prv(i_img, 1, i_xyz), nze, occ)
4915 IF (nze > 0) THEN
4916 CALL dbt_copy(t_3c_der_ri_prv(i_img, 1, i_xyz), t_3c_template, &
4917 order=[3, 2, 1], move_data=.true.)
4918 CALL dbt_filter(t_3c_template, ri_data%filter_eps)
4919 CALL dbt_copy(t_3c_template, t_3c_der_ri(i_img, i_xyz), &
4920 order=[1, 3, 2], move_data=.true., summation=.true.)
4921 END IF
4922 END DO
4923 END DO
4924 CALL timestop(handle2)
4925 END DO
4926 CALL dbt_destroy(t_3c_template)
4927
4928 CALL neighbor_list_3c_destroy(nl_3c)
4929 DO i_xyz = 1, 3
4930 DO i_img = 1, nimg
4931 CALL dbt_destroy(t_3c_der_ri_prv(i_img, 1, i_xyz))
4932 CALL dbt_destroy(t_3c_der_ao_prv(i_img, 1, i_xyz))
4933 END DO
4934 END DO
4935 DEALLOCATE (t_3c_der_ri_prv, t_3c_der_ao_prv)
4936
4937 !Reorder 3c derivatives to be consistant with ints
4938 CALL reorder_3c_derivs(t_3c_der_ri, ri_data)
4939 CALL reorder_3c_derivs(t_3c_der_ao, ri_data)
4940
4941 CALL timeset(routinen//"_2c", handle2)
4942 !The 2-center derivatives
4943 CALL cp_dbcsr_dist2d_to_dist(dist_2d, dbcsr_dist)
4944 ALLOCATE (row_bsize(SIZE(ri_data%bsizes_RI)))
4945 ALLOCATE (col_bsize(SIZE(ri_data%bsizes_RI)))
4946 row_bsize(:) = ri_data%bsizes_RI
4947 col_bsize(:) = ri_data%bsizes_RI
4948
4949 CALL dbcsr_create(dbcsr_template, "2c_der", dbcsr_dist, dbcsr_type_no_symmetry, &
4950 row_bsize, col_bsize)
4951 CALL dbcsr_distribution_release(dbcsr_dist)
4952 DEALLOCATE (col_bsize, row_bsize)
4953
4954 ALLOCATE (mat_der_metric(nimg, 3))
4955 DO i_xyz = 1, 3
4956 DO i_img = 1, nimg
4957 CALL dbcsr_create(mat_der_pot(i_img, i_xyz), template=dbcsr_template)
4958 CALL dbcsr_create(mat_der_metric(i_img, i_xyz), template=dbcsr_template)
4959 END DO
4960 END DO
4961 CALL dbcsr_release(dbcsr_template)
4962
4963 !HFX potential derivatives
4964 CALL build_2c_neighbor_lists(nl_2c, basis_set_ri, basis_set_ri, ri_data%hfx_pot, &
4965 "HFX_2c_nl_pot", qs_env, sym_ij=.false., dist_2d=dist_2d)
4966 CALL build_2c_derivatives(mat_der_pot, ri_data%filter_eps_2c, qs_env, nl_2c, &
4967 basis_set_ri, basis_set_ri, ri_data%hfx_pot, do_kpoints=.true.)
4968 CALL release_neighbor_list_sets(nl_2c)
4969
4970 !RI metric derivatives
4971 CALL build_2c_neighbor_lists(nl_2c, basis_set_ri, basis_set_ri, ri_data%ri_metric, &
4972 "HFX_2c_nl_pot", qs_env, sym_ij=.false., dist_2d=dist_2d)
4973 CALL build_2c_derivatives(mat_der_metric, ri_data%filter_eps_2c, qs_env, nl_2c, &
4974 basis_set_ri, basis_set_ri, ri_data%ri_metric, do_kpoints=.true.)
4975 CALL release_neighbor_list_sets(nl_2c)
4976
4977 !Get into extended RI basis and tensor format
4978 DO i_xyz = 1, 3
4979 DO iatom = 1, natom
4980 CALL dbt_create(ri_data%t_2c_inv(1, 1), t_2c_der_metric(iatom, i_xyz))
4981 CALL get_ext_2c_int(t_2c_der_metric(iatom, i_xyz), mat_der_metric(:, i_xyz), &
4982 iatom, iatom, 1, ri_data, qs_env)
4983 END DO
4984 DO i_img = 1, nimg
4985 CALL dbcsr_release(mat_der_metric(i_img, i_xyz))
4986 END DO
4987 END DO
4988 CALL timestop(handle2)
4989
4990 CALL timestop(handle)
4991
4992 END SUBROUTINE precalc_derivatives
4993
4994! **************************************************************************************************
4995!> \brief Update the forces due to the derivative of the a 2-center product d/dR (Q|R)
4996!> \param force ...
4997!> \param t_2c_contr A precontracted tensor containing sum_abcdPS (ab|P)(P|Q)^-1 (R|S)^-1 (S|cd) P_ac P_bd
4998!> \param t_2c_der the d/dR (Q|R) tensor, in all 3 cartesian directions
4999!> \param atom_of_kind ...
5000!> \param kind_of ...
5001!> \param img in which periodic image the second center of the tensor is
5002!> \param pref ...
5003!> \param ri_data ...
5004!> \param qs_env ...
5005!> \param work_virial ...
5006!> \param cell ...
5007!> \param particle_set ...
5008!> \param diag ...
5009!> \param offdiag ...
5010!> \note IMPORTANT: t_tc_contr and t_2c_der need to have the same distribution. Atomic block sizes are
5011!> assumed
5012! **************************************************************************************************
5013 SUBROUTINE get_2c_der_force(force, t_2c_contr, t_2c_der, atom_of_kind, kind_of, img, pref, &
5014 ri_data, qs_env, work_virial, cell, particle_set, diag, offdiag)
5015
5016 TYPE(qs_force_type), DIMENSION(:), POINTER :: force
5017 TYPE(dbt_type), INTENT(INOUT) :: t_2c_contr
5018 TYPE(dbt_type), DIMENSION(:), INTENT(INOUT) :: t_2c_der
5019 INTEGER, DIMENSION(:), INTENT(IN) :: atom_of_kind, kind_of
5020 INTEGER, INTENT(IN) :: img
5021 REAL(dp), INTENT(IN) :: pref
5022 TYPE(hfx_ri_type), INTENT(INOUT) :: ri_data
5023 TYPE(qs_environment_type), POINTER :: qs_env
5024 REAL(dp), DIMENSION(3, 3), INTENT(INOUT), OPTIONAL :: work_virial
5025 TYPE(cell_type), OPTIONAL, POINTER :: cell
5026 TYPE(particle_type), DIMENSION(:), OPTIONAL, &
5027 POINTER :: particle_set
5028 LOGICAL, INTENT(IN), OPTIONAL :: diag, offdiag
5029
5030 CHARACTER(LEN=*), PARAMETER :: routinen = 'get_2c_der_force'
5031
5032 INTEGER :: handle, i_img, i_ri, i_xyz, iat, &
5033 iat_of_kind, ikind, j_img, j_ri, &
5034 j_xyz, jat, jat_of_kind, jkind, natom
5035 INTEGER, DIMENSION(2) :: ind
5036 INTEGER, DIMENSION(:, :), POINTER :: index_to_cell
5037 LOGICAL :: found, my_diag, my_offdiag, use_virial
5038 REAL(dp) :: new_force
5039 REAL(dp), ALLOCATABLE, DIMENSION(:, :), TARGET :: contr_blk, der_blk
5040 REAL(dp), DIMENSION(3) :: scoord
5041 TYPE(dbt_iterator_type) :: iter
5042 TYPE(kpoint_type), POINTER :: kpoints
5043
5044 NULLIFY (kpoints, index_to_cell)
5045
5046 !Loop over the blocks of d/dR (Q|R), contract with the corresponding block of t_2c_contr and
5047 !update the relevant force
5048
5049 CALL timeset(routinen, handle)
5050
5051 use_virial = .false.
5052 IF (PRESENT(work_virial) .AND. PRESENT(cell) .AND. PRESENT(particle_set)) use_virial = .true.
5053
5054 my_diag = .false.
5055 IF (PRESENT(diag)) my_diag = diag
5056
5057 my_offdiag = .false.
5058 IF (PRESENT(diag)) my_offdiag = offdiag
5059
5060 CALL get_qs_env(qs_env, kpoints=kpoints, natom=natom)
5061 CALL get_kpoint_info(kpoints, index_to_cell=index_to_cell)
5062
5063!$OMP PARALLEL DEFAULT(NONE) &
5064!$OMP SHARED(t_2c_der,t_2c_contr,work_virial,force,use_virial,natom,index_to_cell,ri_data,img) &
5065!$OMP SHARED(pref,atom_of_kind,kind_of,particle_set,cell,my_diag,my_offdiag) &
5066!$OMP PRIVATE(i_xyz,j_xyz,iter,ind,der_blk,contr_blk,found,new_force,i_RI,i_img,j_RI,j_img) &
5067!$OMP PRIVATE(iat,jat,iat_of_kind,jat_of_kind,ikind,jkind,scoord)
5068 DO i_xyz = 1, 3
5069 CALL dbt_iterator_start(iter, t_2c_der(i_xyz))
5070 DO WHILE (dbt_iterator_blocks_left(iter))
5071 CALL dbt_iterator_next_block(iter, ind)
5072
5073 !Only take forecs due to block diagonal or block off-diagonal, depending on arguments
5074 IF ((my_diag .AND. .NOT. my_offdiag) .OR. (.NOT. my_diag .AND. my_offdiag)) THEN
5075 IF (my_diag .AND. (ind(1) /= ind(2))) cycle
5076 IF (my_offdiag .AND. (ind(1) == ind(2))) cycle
5077 END IF
5078
5079 CALL dbt_get_block(t_2c_der(i_xyz), ind, der_blk, found)
5080 cpassert(found)
5081 CALL dbt_get_block(t_2c_contr, ind, contr_blk, found)
5082
5083 IF (found) THEN
5084
5085 !an element of d/dR (Q|R) corresponds to 2 things because of translational invariance
5086 !(Q'| R) = - (Q| R'), once wrt the center on Q, and once on R
5087 new_force = pref*sum(der_blk(:, :)*contr_blk(:, :))
5088
5089 i_ri = (ind(1) - 1)/natom + 1
5090 i_img = ri_data%RI_cell_to_img(i_ri)
5091 iat = ind(1) - (i_ri - 1)*natom
5092 iat_of_kind = atom_of_kind(iat)
5093 ikind = kind_of(iat)
5094
5095 j_ri = (ind(2) - 1)/natom + 1
5096 j_img = ri_data%RI_cell_to_img(j_ri)
5097 jat = ind(2) - (j_ri - 1)*natom
5098 jat_of_kind = atom_of_kind(jat)
5099 jkind = kind_of(jat)
5100
5101 !Force on iatom (first center)
5102!$OMP ATOMIC
5103 force(ikind)%fock_4c(i_xyz, iat_of_kind) = force(ikind)%fock_4c(i_xyz, iat_of_kind) &
5104 + new_force
5105
5106 IF (use_virial) THEN
5107
5108 CALL real_to_scaled(scoord, pbc(particle_set(iat)%r, cell), cell)
5109 scoord(:) = scoord(:) + real(index_to_cell(:, i_img), dp)
5110
5111 DO j_xyz = 1, 3
5112!$OMP ATOMIC
5113 work_virial(i_xyz, j_xyz) = work_virial(i_xyz, j_xyz) + new_force*scoord(j_xyz)
5114 END DO
5115 END IF
5116
5117 !Force on jatom (second center)
5118!$OMP ATOMIC
5119 force(jkind)%fock_4c(i_xyz, jat_of_kind) = force(jkind)%fock_4c(i_xyz, jat_of_kind) &
5120 - new_force
5121
5122 IF (use_virial) THEN
5123
5124 CALL real_to_scaled(scoord, pbc(particle_set(jat)%r, cell), cell)
5125 scoord(:) = scoord(:) + real(index_to_cell(:, j_img) + index_to_cell(:, img), dp)
5126
5127 DO j_xyz = 1, 3
5128!$OMP ATOMIC
5129 work_virial(i_xyz, j_xyz) = work_virial(i_xyz, j_xyz) - new_force*scoord(j_xyz)
5130 END DO
5131 END IF
5132
5133 DEALLOCATE (contr_blk)
5134 END IF
5135
5136 DEALLOCATE (der_blk)
5137 END DO !iter
5138 CALL dbt_iterator_stop(iter)
5139
5140 END DO !i_xyz
5141!$OMP END PARALLEL
5142 CALL timestop(handle)
5143
5144 END SUBROUTINE get_2c_der_force
5145
5146! **************************************************************************************************
5147!> \brief This routines calculates the force contribution from a trace over 3D tensors, i.e.
5148!> force = sum_ijk A_ijk B_ijk., the B tensor is (P^0| sigma^0 lambda^img), with P in the
5149!> extended RI basis. Note that all tensors are stacked along the 3rd dimension
5150!> \param force ...
5151!> \param t_3c_contr ...
5152!> \param t_3c_der_1 ...
5153!> \param t_3c_der_2 ...
5154!> \param atom_of_kind ...
5155!> \param kind_of ...
5156!> \param idx_to_at_RI ...
5157!> \param idx_to_at_AO ...
5158!> \param i_images ...
5159!> \param lb_img ...
5160!> \param pref ...
5161!> \param ri_data ...
5162!> \param qs_env ...
5163!> \param work_virial ...
5164!> \param cell ...
5165!> \param particle_set ...
5166! **************************************************************************************************
5167 SUBROUTINE get_force_from_3c_trace(force, t_3c_contr, t_3c_der_1, t_3c_der_2, atom_of_kind, kind_of, &
5168 idx_to_at_RI, idx_to_at_AO, i_images, lb_img, pref, &
5169 ri_data, qs_env, work_virial, cell, particle_set)
5170
5171 TYPE(qs_force_type), DIMENSION(:), POINTER :: force
5172 TYPE(dbt_type), INTENT(INOUT) :: t_3c_contr
5173 TYPE(dbt_type), DIMENSION(3), INTENT(INOUT) :: t_3c_der_1, t_3c_der_2
5174 INTEGER, DIMENSION(:), INTENT(IN) :: atom_of_kind, kind_of, idx_to_at_ri, &
5175 idx_to_at_ao, i_images
5176 INTEGER, INTENT(IN) :: lb_img
5177 REAL(dp), INTENT(IN) :: pref
5178 TYPE(hfx_ri_type), INTENT(INOUT) :: ri_data
5179 TYPE(qs_environment_type), POINTER :: qs_env
5180 REAL(dp), DIMENSION(3, 3), INTENT(INOUT), OPTIONAL :: work_virial
5181 TYPE(cell_type), OPTIONAL, POINTER :: cell
5182 TYPE(particle_type), DIMENSION(:), OPTIONAL, &
5183 POINTER :: particle_set
5184
5185 CHARACTER(LEN=*), PARAMETER :: routinen = 'get_force_from_3c_trace'
5186
5187 INTEGER :: handle, i_ri, i_xyz, iat, iat_of_kind, idx, ikind, j_xyz, jat, jat_of_kind, &
5188 jkind, kat, kat_of_kind, kkind, nblks_ao, nblks_ri, ri_img
5189 INTEGER, DIMENSION(3) :: ind
5190 INTEGER, DIMENSION(:, :), POINTER :: index_to_cell
5191 LOGICAL :: found, found_1, found_2, use_virial
5192 REAL(dp) :: new_force
5193 REAL(dp), ALLOCATABLE, DIMENSION(:, :, :), TARGET :: contr_blk, der_blk_1, der_blk_2, &
5194 der_blk_3
5195 REAL(dp), DIMENSION(3) :: scoord
5196 TYPE(dbt_iterator_type) :: iter
5197 TYPE(kpoint_type), POINTER :: kpoints
5198
5199 NULLIFY (kpoints, index_to_cell)
5200
5201 CALL timeset(routinen, handle)
5202
5203 CALL get_qs_env(qs_env, kpoints=kpoints)
5204 CALL get_kpoint_info(kpoints, index_to_cell=index_to_cell)
5205
5206 nblks_ri = SIZE(ri_data%bsizes_RI_split)
5207 nblks_ao = SIZE(ri_data%bsizes_AO_split)
5208
5209 use_virial = .false.
5210 IF (PRESENT(work_virial) .AND. PRESENT(cell) .AND. PRESENT(particle_set)) use_virial = .true.
5211
5212!$OMP PARALLEL DEFAULT(NONE) &
5213!$OMP SHARED(t_3c_der_1, t_3c_der_2,t_3c_contr,work_virial,force,use_virial,index_to_cell,i_images,lb_img) &
5214!$OMP SHARED(pref,idx_to_at_AO,atom_of_kind,kind_of,particle_set,cell,idx_to_at_RI,ri_data,nblks_RI,nblks_AO) &
5215!$OMP PRIVATE(i_xyz,j_xyz,iter,ind,der_blk_1,contr_blk,found,new_force,iat,iat_of_kind,ikind,scoord) &
5216!$OMP PRIVATE(jat,kat,jat_of_kind,kat_of_kind,jkind,kkind,i_RI,RI_img,der_blk_2,der_blk_3,found_1,found_2,idx)
5217 CALL dbt_iterator_start(iter, t_3c_contr)
5218 DO WHILE (dbt_iterator_blocks_left(iter))
5219 CALL dbt_iterator_next_block(iter, ind)
5220
5221 CALL dbt_get_block(t_3c_contr, ind, contr_blk, found)
5222 IF (found) THEN
5223
5224 DO i_xyz = 1, 3
5225 CALL dbt_get_block(t_3c_der_1(i_xyz), ind, der_blk_1, found_1)
5226 IF (.NOT. found_1) THEN
5227 DEALLOCATE (der_blk_1)
5228 ALLOCATE (der_blk_1(SIZE(contr_blk, 1), SIZE(contr_blk, 2), SIZE(contr_blk, 3)))
5229 der_blk_1(:, :, :) = 0.0_dp
5230 END IF
5231 CALL dbt_get_block(t_3c_der_2(i_xyz), ind, der_blk_2, found_2)
5232 IF (.NOT. found_2) THEN
5233 DEALLOCATE (der_blk_2)
5234 ALLOCATE (der_blk_2(SIZE(contr_blk, 1), SIZE(contr_blk, 2), SIZE(contr_blk, 3)))
5235 der_blk_2(:, :, :) = 0.0_dp
5236 END IF
5237
5238 ALLOCATE (der_blk_3(SIZE(contr_blk, 1), SIZE(contr_blk, 2), SIZE(contr_blk, 3)))
5239 der_blk_3(:, :, :) = -(der_blk_1(:, :, :) + der_blk_2(:, :, :))
5240
5241 !We assume the tensors are in the format (P^0| sigma^0 mu^a+c-b), with P a member of the
5242 !extended RI basis set
5243
5244 !Force for the first center (RI extended basis, zero cell)
5245 new_force = pref*sum(der_blk_1(:, :, :)*contr_blk(:, :, :))
5246
5247 i_ri = (ind(1) - 1)/nblks_ri + 1
5248 ri_img = ri_data%RI_cell_to_img(i_ri)
5249 iat = idx_to_at_ri(ind(1) - (i_ri - 1)*nblks_ri)
5250 iat_of_kind = atom_of_kind(iat)
5251 ikind = kind_of(iat)
5252
5253!$OMP ATOMIC
5254 force(ikind)%fock_4c(i_xyz, iat_of_kind) = force(ikind)%fock_4c(i_xyz, iat_of_kind) &
5255 + new_force
5256
5257 IF (use_virial) THEN
5258
5259 CALL real_to_scaled(scoord, pbc(particle_set(iat)%r, cell), cell)
5260 scoord(:) = scoord(:) + real(index_to_cell(:, ri_img), dp)
5261
5262 DO j_xyz = 1, 3
5263!$OMP ATOMIC
5264 work_virial(i_xyz, j_xyz) = work_virial(i_xyz, j_xyz) + new_force*scoord(j_xyz)
5265 END DO
5266 END IF
5267
5268 !Force with respect to the second center (AO basis, zero cell)
5269 new_force = pref*sum(der_blk_2(:, :, :)*contr_blk(:, :, :))
5270 jat = idx_to_at_ao(ind(2))
5271 jat_of_kind = atom_of_kind(jat)
5272 jkind = kind_of(jat)
5273
5274!$OMP ATOMIC
5275 force(jkind)%fock_4c(i_xyz, jat_of_kind) = force(jkind)%fock_4c(i_xyz, jat_of_kind) &
5276 + new_force
5277
5278 IF (use_virial) THEN
5279
5280 CALL real_to_scaled(scoord, pbc(particle_set(jat)%r, cell), cell)
5281
5282 DO j_xyz = 1, 3
5283!$OMP ATOMIC
5284 work_virial(i_xyz, j_xyz) = work_virial(i_xyz, j_xyz) + new_force*scoord(j_xyz)
5285 END DO
5286 END IF
5287
5288 !Force with respect to the third center (AO basis, apc_img - b_img)
5289 !Note: tensors are stacked along the 3rd direction
5290 new_force = pref*sum(der_blk_3(:, :, :)*contr_blk(:, :, :))
5291 idx = (ind(3) - 1)/nblks_ao + 1
5292 kat = idx_to_at_ao(ind(3) - (idx - 1)*nblks_ao)
5293 kat_of_kind = atom_of_kind(kat)
5294 kkind = kind_of(kat)
5295
5296!$OMP ATOMIC
5297 force(kkind)%fock_4c(i_xyz, kat_of_kind) = force(kkind)%fock_4c(i_xyz, kat_of_kind) &
5298 + new_force
5299
5300 IF (use_virial) THEN
5301 CALL real_to_scaled(scoord, pbc(particle_set(kat)%r, cell), cell)
5302 scoord(:) = scoord(:) + real(index_to_cell(:, i_images(lb_img - 1 + idx)), dp)
5303
5304 DO j_xyz = 1, 3
5305!$OMP ATOMIC
5306 work_virial(i_xyz, j_xyz) = work_virial(i_xyz, j_xyz) + new_force*scoord(j_xyz)
5307 END DO
5308 END IF
5309
5310 DEALLOCATE (der_blk_1, der_blk_2, der_blk_3)
5311 END DO !i_xyz
5312 DEALLOCATE (contr_blk)
5313 END IF !found
5314 END DO !iter
5315 CALL dbt_iterator_stop(iter)
5316!$OMP END PARALLEL
5317 CALL timestop(handle)
5318
5319 END SUBROUTINE get_force_from_3c_trace
5320
5321END MODULE hfx_ri_kp
static GRID_HOST_DEVICE int modulo(int a, int m)
Equivalent of Fortran's MODULO, which always return a positive number. https://gcc....
static GRID_HOST_DEVICE double fac(const int i)
Factorial function, e.g. fac(5) = 5! = 120.
Definition grid_common.h:57
static GRID_HOST_DEVICE int idx(const orbital a)
Return coset index of given orbital angular momentum.
Types and set/get functions for auxiliary density matrix methods.
Definition admm_types.F:15
subroutine, public get_admm_env(admm_env, mo_derivs_aux_fit, mos_aux_fit, sab_aux_fit, sab_aux_fit_asymm, sab_aux_fit_vs_orb, matrix_s_aux_fit, matrix_s_aux_fit_kp, matrix_s_aux_fit_vs_orb, matrix_s_aux_fit_vs_orb_kp, task_list_aux_fit, matrix_ks_aux_fit, matrix_ks_aux_fit_kp, matrix_ks_aux_fit_im, matrix_ks_aux_fit_dft, matrix_ks_aux_fit_hfx, matrix_ks_aux_fit_dft_kp, matrix_ks_aux_fit_hfx_kp, rho_aux_fit, rho_aux_fit_buffer, admm_dm)
Get routine for the ADMM env.
Definition admm_types.F:599
Define the atomic kind types and their sub types.
subroutine, public get_atomic_kind_set(atomic_kind_set, atom_of_kind, kind_of, natom_of_kind, maxatom, natom, nshell, fist_potential_present, shell_present, shell_adiabatic, shell_check_distance, damping_present)
Get attributes of an atomic kind set.
subroutine, public get_gto_basis_set(gto_basis_set, name, aliases, norm_type, kind_radius, ncgf, nset, nsgf, cgf_symbol, sgf_symbol, norm_cgf, set_radius, lmax, lmin, lx, ly, lz, m, ncgf_set, npgf, nsgf_set, nshell, cphi, pgf_radius, sphi, scon, zet, first_cgf, first_sgf, l, last_cgf, last_sgf, n, gcc, maxco, maxl, maxpgf, maxsgf_set, maxshell, maxso, nco_sum, npgf_sum, nshell_sum, maxder, short_kind_radius, npgf_seg_sum, ccon)
...
collects all references to literature in CP2K as new algorithms / method are included from literature...
integer, save, public bussy2024
Handles all functions related to the CELL.
Definition cell_types.F:15
subroutine, public scaled_to_real(r, s, cell)
Transform scaled cell coordinates real coordinates. r=h*s.
Definition cell_types.F:661
subroutine, public real_to_scaled(s, r, cell)
Transform real to scaled cell coordinates. s=h_inv*r.
Definition cell_types.F:631
various utilities that regard array of different kinds: output, allocation,... maybe it is not a good...
methods related to the blacs parallel environment
subroutine, public cp_blacs_env_release(blacs_env)
releases the given blacs_env
subroutine, public cp_blacs_env_create(blacs_env, para_env, blacs_grid_layout, blacs_repeatable, row_major, grid_2d)
allocates and initializes a type that represent a blacs context
Defines control structures, which contain the parameters and the settings for the DFT-based calculati...
subroutine, public dbcsr_distribution_release(dist)
...
subroutine, public dbcsr_distribution_new(dist, template, group, pgrid, row_dist, col_dist, reuse_arrays)
...
logical function, public dbcsr_iterator_blocks_left(iterator)
...
subroutine, public dbcsr_iterator_stop(iterator)
...
subroutine, public dbcsr_copy(matrix_b, matrix_a, name, keep_sparsity, keep_imaginary)
...
subroutine, public dbcsr_get_block_p(matrix, row, col, block, found, row_size, col_size)
...
subroutine, public dbcsr_get_info(matrix, nblkrows_total, nblkcols_total, nfullrows_total, nfullcols_total, nblkrows_local, nblkcols_local, nfullrows_local, nfullcols_local, my_prow, my_pcol, local_rows, local_cols, proc_row_dist, proc_col_dist, row_blk_size, col_blk_size, row_blk_offset, col_blk_offset, distribution, name, matrix_type, group)
...
subroutine, public dbcsr_iterator_next_block(iterator, row, column, block, block_number_argument_has_been_removed, row_size, col_size, row_offset, col_offset, transposed)
...
subroutine, public dbcsr_filter(matrix, eps)
...
subroutine, public dbcsr_finalize(matrix)
...
subroutine, public dbcsr_iterator_start(iterator, matrix, shared, dynamic, dynamic_byrows)
...
subroutine, public dbcsr_release(matrix)
...
subroutine, public dbcsr_clear(matrix)
...
subroutine, public dbcsr_put_block(matrix, row, col, block, summation)
...
subroutine, public dbcsr_add(matrix_a, matrix_b, alpha_scalar, beta_scalar)
...
subroutine, public dbcsr_distribution_get(dist, row_dist, col_dist, nrows, ncols, has_threads, group, mynode, numnodes, nprows, npcols, myprow, mypcol, pgrid, subgroups_defined, prow_group, pcol_group)
...
Interface to (sca)lapack for the Cholesky based procedures.
subroutine, public cp_dbcsr_cholesky_decompose(matrix, n, para_env, blacs_env)
used to replace a symmetric positive def. matrix M with its cholesky decomposition U: M = U^T * U,...
subroutine, public cp_dbcsr_cholesky_invert(matrix, n, para_env, blacs_env, uplo_to_full)
used to replace the cholesky decomposition by the inverse
subroutine, public dbcsr_dot(matrix_a, matrix_b, trace)
Computes the dot product of two matrices, also known as the trace of their matrix product.
Interface to (sca)lapack for the Cholesky based procedures.
subroutine, public cp_dbcsr_power(matrix, exponent, threshold, n_dependent, para_env, blacs_env, verbose, eigenvectors, eigenvalues)
...
DBCSR operations in CP2K.
subroutine, public cp_dbcsr_dist2d_to_dist(dist2d, dist)
Creates a DBCSR distribution from a distribution_2d.
This is the start of a dbt_api, all publically needed functions are exported here....
Definition dbt_api.F:17
stores a mapping of 2D info (e.g. matrix) on a 2D processor distribution (i.e. blacs grid) where cpus...
subroutine, public distribution_2d_create(distribution_2d, blacs_env, local_rows_ptr, n_local_rows, local_cols_ptr, row_distribution_ptr, col_distribution_ptr, n_local_cols, n_row_distribution, n_col_distribution)
initializes the distribution_2d
subroutine, public distribution_2d_release(distribution_2d)
...
RI-methods for HFX and K-points. \auhtor Augustin Bussy (01.2023).
Definition hfx_ri_kp.F:13
subroutine, public hfx_ri_update_forces_kp(qs_env, ri_data, nspins, hf_fraction, rho_ao, use_virial)
Update the K-points RI-HFX forces.
Definition hfx_ri_kp.F:833
subroutine, public hfx_ri_update_ks_kp(qs_env, ri_data, ks_matrix, ehfx, rho_ao, geometry_did_change, nspins, hf_fraction)
Update the KS matrices for each real-space image.
Definition hfx_ri_kp.F:469
RI-methods for HFX.
Definition hfx_ri.F:12
subroutine, public get_force_from_3c_trace(force, t_3c_contr, t_3c_der, atom_of_kind, kind_of, idx_to_at, pref, do_mp2, deriv_dim)
This routines calculates the force contribution from a trace over 3D tensors, i.e....
Definition hfx_ri.F:3370
subroutine, public get_idx_to_atom(idx_to_at, bsizes_split, bsizes_orig)
a small utility function that returns the atom corresponding to a block of a split tensor
Definition hfx_ri.F:4241
subroutine, public hfx_ri_pre_scf_calc_tensors(qs_env, ri_data, t_2c_int_ri, t_2c_int_pot, t_3c_int, do_kpoints)
Calculate 2-center and 3-center integrals.
Definition hfx_ri.F:449
subroutine, public get_2c_der_force(force, t_2c_contr, t_2c_der, atom_of_kind, kind_of, idx_to_at, pref, do_mp2, do_ovlp)
Update the forces due to the derivative of the a 2-center product d/dR (Q|R).
Definition hfx_ri.F:3456
Types and set/get functions for HFX.
Definition hfx_types.F:16
collects all constants needed in input so that they can be used without circular dependencies
integer, parameter, public hfx_ri_do_2c_cholesky
integer, parameter, public hfx_ri_do_2c_diag
integer, parameter, public hfx_ri_do_2c_iter
integer, parameter, public do_potential_short
function that builds the hartree fock exchange section of the input
integer, parameter, public ri_pmat
objects that represent the structure of input sections and the data contained in an input section
subroutine, public section_vals_val_set(section_vals, keyword_name, i_rep_section, i_rep_val, val, l_val, i_val, r_val, c_val, l_vals_ptr, i_vals_ptr, r_vals_ptr, c_vals_ptr)
sets the requested value
recursive type(section_vals_type) function, pointer, public section_vals_get_subs_vals(section_vals, subsection_name, i_rep_section, can_return_null)
returns the values of the requested subsection
subroutine, public section_vals_val_get(section_vals, keyword_name, i_rep_section, i_rep_val, n_rep_val, val, l_val, i_val, r_val, c_val, l_vals, i_vals, r_vals, c_vals, explicit)
returns the requested value
Library choices for electronic integral APIs.
real(dp), parameter, public cutoff_screen_factor
Routines useful for iterative matrix calculations.
subroutine, public invert_hotelling(matrix_inverse, matrix, threshold, use_inv_as_guess, norm_convergence, filter_eps, accelerator_order, max_iter_lanczos, eps_lanczos, silent)
invert a symmetric positive definite matrix by Hotelling's method explicit symmetrization makes this ...
Defines the basic variable types.
Definition kinds.F:23
integer, parameter, public int_8
Definition kinds.F:54
integer, parameter, public dp
Definition kinds.F:34
integer, parameter, public default_string_length
Definition kinds.F:57
Types and basic routines needed for a kpoint calculation.
subroutine, public get_kpoint_info(kpoint, kp_scheme, nkp_grid, kp_shift, symmetry, verbose, full_grid, use_real_wfn, eps_geo, parallel_group_size, kp_range, nkp, xkp, wkp, para_env, blacs_env_all, para_env_kp, para_env_inter_kp, blacs_env, kp_env, kp_aux_env, mpools, iogrp, nkp_groups, kp_dist, cell_to_index, index_to_cell, sab_nl, sab_nl_nosym, inversion_symmetry_only, symmetry_backend, symmetry_reduction_method, gamma_centered, lattice_fft)
Retrieve information from a kpoint environment.
Machine interface based on Fortran 2003 and POSIX.
Definition machine.F:17
subroutine, public m_memory(mem)
Returns the total amount of memory [bytes] in use, if known, zero otherwise.
Definition machine.F:443
subroutine, public m_flush(lunit)
flushes units if the &GLOBAL flag is set accordingly
Definition machine.F:127
real(kind=dp) function, public m_walltime()
returns time from a real-time clock, protected against rolling early/easily
Definition machine.F:144
Collection of simple mathematical functions and subroutines.
Definition mathlib.F:15
subroutine, public diag(n, a, d, v)
Diagonalize matrix a. The eigenvalues are returned in vector d and the eigenvectors are returned in m...
Definition mathlib.F:1660
subroutine, public erfc_cutoff(eps, omg, r_cutoff)
compute a truncation radius for the shortrange operator
Definition mathlib.F:1854
Utility routines for the memory handling.
Interface to the message passing library MPI.
Define methods related to particle_type.
subroutine, public get_particle_set(particle_set, qs_kind_set, first_sgf, last_sgf, nsgf, nmao, basis, ncgf)
Get the components of a particle set.
Define the data structure for the particle information.
Definition of physical constants:
Definition physcon.F:68
real(kind=dp), parameter, public angstrom
Definition physcon.F:144
subroutine, public get_qs_env(qs_env, atomic_kind_set, qs_kind_set, cell, super_cell, cell_ref, use_ref_cell, kpoints, dft_control, mos, sab_orb, sab_all, qmmm, qmmm_periodic, mimic, sac_ae, sac_ppl, sac_lri, sap_ppnl, sab_vdw, sab_scp, sap_oce, sab_lrc, sab_se, sab_xtbe, sab_tbe, sab_core, sab_xb, sab_xtb_pp, sab_xtb_nonbond, sab_almo, sab_kp, sab_kp_nosym, sab_cneo, particle_set, energy, force, matrix_h, matrix_h_im, matrix_ks, matrix_ks_im, matrix_vxc, run_rtp, rtp, matrix_h_kp, matrix_h_im_kp, matrix_ks_kp, matrix_ks_im_kp, matrix_vxc_kp, kinetic_kp, matrix_s_kp, matrix_w_kp, matrix_s_ri_aux_kp, matrix_s, matrix_s_ri_aux, matrix_w, matrix_p_mp2, matrix_p_mp2_admm, matrix_vhxc, rho, rho_xc, pw_env, ewald_env, ewald_pw, active_space, mpools, input, para_env, blacs_env, scf_control, rel_control, kinetic, qs_charges, vppl, xcint_weights, rho_core, rho_nlcc, rho_nlcc_g, ks_env, ks_qmmm_env, wf_history, scf_env, local_particles, local_molecules, distribution_2d, dbcsr_dist, molecule_kind_set, molecule_set, subsys, cp_subsys, oce, local_rho_set, rho_atom_set, task_list, task_list_soft, rho0_atom_set, rho0_mpole, rhoz_set, rhoz_cneo_set, ecoul_1c, rho0_s_rs, rho0_s_gs, rhoz_cneo_s_rs, rhoz_cneo_s_gs, do_kpoints, has_unit_metric, requires_mo_derivs, mo_derivs, mo_loc_history, nkind, natom, nelectron_total, nelectron_spin, efield, neighbor_list_id, linres_control, xas_env, virial, cp_ddapc_env, cp_ddapc_ewald, outer_scf_history, outer_scf_ihistory, x_data, et_coupling, dftb_potential, results, se_taper, se_store_int_env, se_nddo_mpole, se_nonbond_env, admm_env, lri_env, lri_density, exstate_env, ec_env, harris_env, dispersion_env, gcp_env, vee, rho_external, external_vxc, mask, mp2_env, bs_env, kg_env, wanniercentres, atprop, ls_scf_env, do_transport, transport_env, v_hartree_rspace, s_mstruct_changed, rho_changed, potential_changed, forces_up_to_date, mscfg_env, almo_scf_env, gradient_history, variable_history, embed_pot, spin_embed_pot, polar_env, mos_last_converged, eeq, rhs, do_rixs, tb_tblite)
Get the QUICKSTEP environment.
Some utility functions for the calculation of integrals.
subroutine, public basis_set_list_setup(basis_set_list, basis_type, qs_kind_set)
Set up an easy accessible list of the basis sets for all kinds.
Calculate the interaction radii for the operator matrix calculation.
subroutine, public init_interaction_radii_orb_basis(orb_basis_set, eps_pgf_orb, eps_pgf_short)
...
Define the quickstep kind type and their sub types.
Define the neighbor list data types and the corresponding functionality.
subroutine, public release_neighbor_list_sets(nlists)
releases an array of neighbor_list_sets
subroutine, public neighbor_list_iterator_create(iterator_set, nl, search, nthread)
Neighbor list iterator functions.
subroutine, public neighbor_list_iterator_release(iterator_set)
...
integer function, public neighbor_list_iterate(iterator_set, mepos)
...
subroutine, public get_iterator_info(iterator_set, mepos, ikind, jkind, nkind, ilist, nlist, inode, nnode, iatom, jatom, r, cell)
...
module that contains the definitions of the scf types
Utility methods to build 3-center integral tensors of various types.
subroutine, public distribution_3d_create(dist_3d, dist1, dist2, dist3, nkind, particle_set, mp_comm_3d, own_comm)
Create a 3d distribution.
subroutine, public create_2c_tensor(t2c, dist_1, dist_2, pgrid, sizes_1, sizes_2, order, name)
...
subroutine, public create_tensor_batches(sizes, nbatches, starts_array, ends_array, starts_array_block, ends_array_block)
...
subroutine, public create_3c_tensor(t3c, dist_1, dist_2, dist_3, pgrid, sizes_1, sizes_2, sizes_3, map1, map2, name)
...
Utility methods to build 3-center integral tensors of various types.
Definition qs_tensors.F:11
subroutine, public build_3c_derivatives(t3c_der_i, t3c_der_k, filter_eps, qs_env, nl_3c, basis_i, basis_j, basis_k, potential_parameter, der_eps, op_pos, do_kpoints, do_hfx_kpoints, bounds_i, bounds_j, bounds_k, ri_range, img_to_ri_cell)
Build 3-center derivative tensors.
Definition qs_tensors.F:932
subroutine, public build_2c_neighbor_lists(ij_list, basis_i, basis_j, potential_parameter, name, qs_env, sym_ij, molecular, dist_2d, pot_to_rad)
Build 2-center neighborlists adapted to different operators This mainly wraps build_neighbor_lists fo...
Definition qs_tensors.F:138
subroutine, public neighbor_list_3c_iterator_destroy(iterator)
Destroy 3c-nl iterator.
Definition qs_tensors.F:440
subroutine, public neighbor_list_3c_destroy(ijk_list)
Destroy 3c neighborlist.
Definition qs_tensors.F:375
subroutine, public build_2c_derivatives(t2c_der, filter_eps, qs_env, nl_2c, basis_i, basis_j, potential_parameter, do_kpoints)
Calculates the derivatives of 2-center integrals, wrt to the first center.
subroutine, public get_tensor_occupancy(tensor, nze, occ)
...
subroutine, public build_3c_neighbor_lists(ijk_list, basis_i, basis_j, basis_k, dist_3d, potential_parameter, name, qs_env, sym_ij, sym_jk, sym_ik, molecular, op_pos, own_dist)
Build a 3-center neighbor list.
Definition qs_tensors.F:274
integer function, public neighbor_list_3c_iterate(iterator, next_pair)
Iterate 3c-nl iterator.
Definition qs_tensors.F:464
subroutine, public neighbor_list_3c_iterator_create(iterator, ijk_nl)
Create a 3-center neighborlist iterator.
Definition qs_tensors.F:392
subroutine, public get_3c_iterator_info(iterator, ikind, jkind, kkind, nkind, iatom, jatom, katom, rij, rjk, rik, cell_j, cell_k)
Get info of current iteration.
Definition qs_tensors.F:568
Provides all information about an atomic kind.
Type defining parameters related to the simulation cell.
Definition cell_types.F:60
represent a pointer to a 1d array
represent a pointer to a 2d array
represent a blacs multidimensional parallel environment (for the mpi corrispective see cp_paratypes/m...
distributes pairs on a 2d grid of processors
Contains information about kpoints.
stores all the informations relevant to an mpi environment
Provides all information about a quickstep kind.