(git:27c966b)
Loading...
Searching...
No Matches
rpa_exchange.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 Auxiliary routines needed for RPA-exchange
10!> given blacs_env to another
11!> \par History
12!> 09.2016 created [Vladimir Rybkin]
13!> 03.2019 Renamed [Frederick Stein]
14!> 03.2019 Moved Functions from rpa_ri_gpw.F [Frederick Stein]
15!> 04.2024 Added open-shell calculations, SOSEX [Frederick Stein]
16!> \author Vladimir Rybkin
17! **************************************************************************************************
20 USE cell_types, ONLY: cell_type
23 USE cp_dbcsr_api, ONLY: &
25 dbcsr_release, dbcsr_set, dbcsr_type, dbcsr_type_no_symmetry
35 USE cp_fm_types, ONLY: cp_fm_create,&
46 maxsize,&
49 USE hfx_types, ONLY: hfx_create,&
57 USE kinds, ONLY: dp,&
58 int_8
60 USE mathconstants, ONLY: sqrthalf
63 USE mp2_types, ONLY: mp2_type
71 USE rpa_util, ONLY: calc_fm_mat_s_rpa,&
74#include "./base/base_uses.f90"
75
76 IMPLICIT NONE
77
78 PRIVATE
79
80 CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'rpa_exchange'
81
83
84 TYPE rpa_exchange_env_type
85 PRIVATE
86 TYPE(qs_environment_type), POINTER :: qs_env => null()
87 TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: mat_hfx => null()
88 TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: dbcsr_Gamma_munu_P => null()
89 TYPE(dbcsr_type), ALLOCATABLE, DIMENSION(:) :: dbcsr_Gamma_inu_P
90 ! Workaround GCC 8
91 TYPE(dbcsr_type), DIMENSION(:), POINTER :: mo_coeff_o => null()
92 TYPE(dbcsr_type), DIMENSION(:), POINTER :: mo_coeff_v => null()
93 TYPE(dbcsr_type) :: work_ao
94 TYPE(hfx_type), DIMENSION(:, :), POINTER :: x_data => null()
95 TYPE(mp_para_env_type), POINTER :: para_env => null()
96 TYPE(section_vals_type), POINTER :: hfx_sections => null()
97 LOGICAL :: my_recalc_hfx_integrals = .false.
98 REAL(KIND=dp) :: eps_filter = 0.0_dp
99 TYPE(cp_fm_struct_p_type), DIMENSION(:), ALLOCATABLE :: struct_Gamma
100 CONTAINS
101 PROCEDURE, PASS(exchange_env), NON_OVERRIDABLE :: create => hfx_create_subgroup
102 !PROCEDURE, PASS(exchange_env), NON_OVERRIDABLE :: integrate => integrate_exchange
103 PROCEDURE, PASS(exchange_env), NON_OVERRIDABLE :: release => hfx_release_subgroup
104 END TYPE rpa_exchange_env_type
105
106 TYPE dbcsr_matrix_p_set
107 TYPE(dbcsr_type), ALLOCATABLE, DIMENSION(:) :: matrix_set
108 END TYPE dbcsr_matrix_p_set
109
111 PRIVATE
112 INTEGER :: exchange_correction = rpa_exchange_none
113 TYPE(rpa_exchange_env_type) :: exchange_env
114 INTEGER, DIMENSION(:), ALLOCATABLE :: homo, virtual, dimen_ia
115 TYPE(group_dist_d1_type) :: aux_func_dist = group_dist_d1_type()
116 INTEGER, DIMENSION(:), ALLOCATABLE :: aux2send
117 INTEGER :: dimen_ri = 0
118 INTEGER :: block_size = 0
119 INTEGER :: color_sub = 0
120 INTEGER :: ngroup = 0
121 TYPE(cp_fm_type) :: fm_mat_q_tmp = cp_fm_type()
122 TYPE(cp_fm_type) :: fm_mat_r_half_gemm = cp_fm_type()
123 TYPE(cp_fm_type) :: fm_mat_u = cp_fm_type()
124 TYPE(mp_para_env_type), POINTER :: para_env_sub => null()
125 CONTAINS
126 PROCEDURE, PUBLIC, pass(exchange_work), non_overridable :: create => rpa_exchange_work_create
127 PROCEDURE, PUBLIC, pass(exchange_work), non_overridable :: compute => rpa_exchange_work_compute
128 PROCEDURE, PUBLIC, pass(exchange_work), non_overridable :: release => rpa_exchange_work_release
129 PROCEDURE, PRIVATE, pass(exchange_work), non_overridable :: redistribute_into_subgroups
130 PROCEDURE, PRIVATE, pass(exchange_work), non_overridable :: compute_fm => rpa_exchange_work_compute_fm
131 PROCEDURE, PRIVATE, pass(exchange_work), non_overridable :: compute_hfx => rpa_exchange_work_compute_hfx
133
134CONTAINS
135
136! **************************************************************************************************
137!> \brief ...
138!> \param mp2_env ...
139!> \param homo ...
140!> \param virtual ...
141!> \param dimen_RI ...
142!> \param para_env ...
143!> \param mem_per_rank ...
144!> \param mem_per_repl ...
145! **************************************************************************************************
146 SUBROUTINE rpa_exchange_needed_mem(mp2_env, homo, virtual, dimen_RI, para_env, mem_per_rank, mem_per_repl)
147 TYPE(mp2_type), INTENT(IN) :: mp2_env
148 INTEGER, DIMENSION(:), INTENT(IN) :: homo, virtual
149 INTEGER, INTENT(IN) :: dimen_ri
150 TYPE(mp_para_env_type), INTENT(IN) :: para_env
151 REAL(kind=dp), INTENT(INOUT) :: mem_per_rank, mem_per_repl
152
153 INTEGER :: block_size
154
155 ! We need the block size and if it is unknown, an upper bound
156 block_size = mp2_env%ri_rpa%exchange_block_size
157 IF (block_size <= 0) block_size = max(1, (dimen_ri + para_env%num_pe - 1)/para_env%num_pe)
158
159 ! storage of product matrix (upper bound only as it depends on the square of the potential still unknown block size)
160 mem_per_rank = mem_per_rank + real(maxval(homo), kind=dp)**2*block_size**2*8.0_dp/(1024_dp**2)
161
162 ! work arrays R (2x) and U, copies of Gamma (2x), communication buffer (as expensive as Gamma)
163 mem_per_repl = mem_per_repl + 3.0_dp*dimen_ri*dimen_ri*8.0_dp/(1024_dp**2) &
164 + 3.0_dp*maxval(homo*virtual)*dimen_ri*8.0_dp/(1024_dp**2)
165 END SUBROUTINE rpa_exchange_needed_mem
166
167! **************************************************************************************************
168!> \brief ...
169!> \param exchange_work ...
170!> \param qs_env ...
171!> \param para_env_sub ...
172!> \param mat_munu ...
173!> \param dimen_RI ...
174!> \param fm_mat_S ...
175!> \param fm_mat_Q ...
176!> \param fm_mat_Q_gemm ...
177!> \param homo ...
178!> \param virtual ...
179! **************************************************************************************************
180 SUBROUTINE rpa_exchange_work_create(exchange_work, qs_env, para_env_sub, mat_munu, dimen_RI, &
181 fm_mat_S, fm_mat_Q, fm_mat_Q_gemm, homo, virtual)
182 CLASS(rpa_exchange_work_type), INTENT(INOUT) :: exchange_work
183 TYPE(qs_environment_type), POINTER :: qs_env
184 TYPE(mp_para_env_type), POINTER, INTENT(IN) :: para_env_sub
185 TYPE(dbcsr_p_type), INTENT(IN) :: mat_munu
186 INTEGER, INTENT(IN) :: dimen_ri
187 TYPE(cp_fm_type), DIMENSION(:), INTENT(IN) :: fm_mat_s
188 TYPE(cp_fm_type), INTENT(IN) :: fm_mat_q, fm_mat_q_gemm
189 INTEGER, DIMENSION(SIZE(fm_mat_S)), INTENT(IN) :: homo, virtual
190
191 INTEGER :: nspins, aux_global, aux_local, my_process_row, proc, ispin
192 INTEGER, DIMENSION(:), POINTER :: row_indices, aux_distribution_fm
193 TYPE(cp_blacs_env_type), POINTER :: context
194
195 exchange_work%exchange_correction = qs_env%mp2_env%ri_rpa%exchange_correction
196
197 IF (exchange_work%exchange_correction == rpa_exchange_none) RETURN
198
199 associate(para_env => fm_mat_s(1)%matrix_struct%para_env)
200 exchange_work%para_env_sub => para_env_sub
201 exchange_work%ngroup = para_env%num_pe/para_env_sub%num_pe
202 exchange_work%color_sub = para_env%mepos/para_env_sub%num_pe
203 END associate
204
205 CALL cp_fm_get_info(fm_mat_s(1), row_indices=row_indices, nrow_locals=aux_distribution_fm, context=context)
206 CALL context%get(my_process_row=my_process_row)
207
208 CALL create_group_dist(exchange_work%aux_func_dist, exchange_work%ngroup, dimen_ri)
209 ALLOCATE (exchange_work%aux2send(0:exchange_work%ngroup - 1))
210 exchange_work%aux2send = 0
211 DO aux_local = 1, aux_distribution_fm(my_process_row)
212 aux_global = row_indices(aux_local)
213 proc = group_dist_proc(exchange_work%aux_func_dist, aux_global)
214 exchange_work%aux2send(proc) = exchange_work%aux2send(proc) + 1
215 END DO
216
217 nspins = SIZE(fm_mat_s)
218
219 ALLOCATE (exchange_work%homo(nspins), exchange_work%virtual(nspins), exchange_work%dimen_ia(nspins))
220 exchange_work%homo(:) = homo
221 exchange_work%virtual(:) = virtual
222 exchange_work%dimen_ia(:) = homo*virtual
223 exchange_work%dimen_RI = dimen_ri
224
225 exchange_work%block_size = qs_env%mp2_env%ri_rpa%exchange_block_size
226 IF (exchange_work%block_size <= 0) exchange_work%block_size = dimen_ri
227
228 CALL cp_fm_create(exchange_work%fm_mat_U, fm_mat_q%matrix_struct, name="fm_mat_U")
229 CALL cp_fm_create(exchange_work%fm_mat_Q_tmp, fm_mat_q%matrix_struct, name="fm_mat_Q_tmp")
230 CALL cp_fm_create(exchange_work%fm_mat_R_half_gemm, fm_mat_q_gemm%matrix_struct)
231
232 IF (qs_env%mp2_env%ri_rpa%use_hfx_implementation) THEN
233 CALL exchange_work%exchange_env%create(qs_env, mat_munu%matrix, para_env_sub, fm_mat_s)
234 END IF
235
236 IF (ASSOCIATED(qs_env%mp2_env%ri_rpa%mo_coeff_o)) THEN
237 DO ispin = 1, SIZE(qs_env%mp2_env%ri_rpa%mo_coeff_o)
238 CALL dbcsr_release(qs_env%mp2_env%ri_rpa%mo_coeff_o(ispin))
239 END DO
240 DEALLOCATE (qs_env%mp2_env%ri_rpa%mo_coeff_o)
241 END IF
242
243 IF (ASSOCIATED(qs_env%mp2_env%ri_rpa%mo_coeff_v)) THEN
244 DO ispin = 1, SIZE(qs_env%mp2_env%ri_rpa%mo_coeff_v)
245 CALL dbcsr_release(qs_env%mp2_env%ri_rpa%mo_coeff_v(ispin))
246 END DO
247 DEALLOCATE (qs_env%mp2_env%ri_rpa%mo_coeff_v)
248 END IF
249 END SUBROUTINE rpa_exchange_work_create
250
251! **************************************************************************************************
252!> \brief ... Initializes x_data on a subgroup
253!> \param exchange_env ...
254!> \param qs_env ...
255!> \param mat_munu ...
256!> \param para_env_sub ...
257!> \param fm_mat_S ...
258!> \author Vladimir Rybkin
259! **************************************************************************************************
260 SUBROUTINE hfx_create_subgroup(exchange_env, qs_env, mat_munu, para_env_sub, fm_mat_S)
261 CLASS(rpa_exchange_env_type), INTENT(INOUT) :: exchange_env
262 TYPE(dbcsr_type), INTENT(IN) :: mat_munu
263 TYPE(qs_environment_type), POINTER :: qs_env
264 TYPE(mp_para_env_type), POINTER, INTENT(IN) :: para_env_sub
265 TYPE(cp_fm_type), DIMENSION(:), INTENT(IN) :: fm_mat_s
266
267 CHARACTER(LEN=*), PARAMETER :: routinen = 'hfx_create_subgroup'
268
269 INTEGER :: handle, nelectron_total, ispin, &
270 number_of_aos, nspins, dimen_ri, dimen_ia
271 TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
272 TYPE(cell_type), POINTER :: my_cell
273 TYPE(dft_control_type), POINTER :: dft_control
274 TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
275 TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
276 TYPE(qs_subsys_type), POINTER :: subsys
277 TYPE(scf_control_type), POINTER :: scf_control
278 TYPE(section_vals_type), POINTER :: input
279
280 CALL timeset(routinen, handle)
281
282 exchange_env%mo_coeff_o => qs_env%mp2_env%ri_rpa%mo_coeff_o
283 exchange_env%mo_coeff_v => qs_env%mp2_env%ri_rpa%mo_coeff_v
284 NULLIFY (qs_env%mp2_env%ri_rpa%mo_coeff_o, qs_env%mp2_env%ri_rpa%mo_coeff_v)
285
286 nspins = SIZE(exchange_env%mo_coeff_o)
287
288 exchange_env%qs_env => qs_env
289 exchange_env%para_env => para_env_sub
290 exchange_env%eps_filter = qs_env%mp2_env%mp2_gpw%eps_filter
291
292 NULLIFY (my_cell, atomic_kind_set, particle_set, dft_control, qs_kind_set, scf_control)
293
294 CALL get_qs_env(qs_env, &
295 subsys=subsys, &
296 input=input, &
297 scf_control=scf_control, &
298 nelectron_total=nelectron_total)
299
300 CALL qs_subsys_get(subsys, &
301 cell=my_cell, &
302 atomic_kind_set=atomic_kind_set, &
303 qs_kind_set=qs_kind_set, &
304 particle_set=particle_set)
305
306 exchange_env%hfx_sections => section_vals_get_subs_vals(input, "DFT%XC%WF_CORRELATION%RI_RPA%HF")
307 CALL get_qs_env(qs_env, dft_control=dft_control)
308
309 ! Retrieve particle_set and atomic_kind_set
310 CALL hfx_create(exchange_env%x_data, para_env_sub, exchange_env%hfx_sections, atomic_kind_set, &
311 qs_kind_set, particle_set, dft_control, my_cell, orb_basis='ORB', &
312 nelectron_total=nelectron_total)
313
314 exchange_env%my_recalc_hfx_integrals = .true.
315
316 CALL dbcsr_allocate_matrix_set(exchange_env%mat_hfx, nspins)
317 DO ispin = 1, nspins
318 ALLOCATE (exchange_env%mat_hfx(ispin)%matrix)
319 CALL dbcsr_init_p(exchange_env%mat_hfx(ispin)%matrix)
320 CALL dbcsr_create(exchange_env%mat_hfx(ispin)%matrix, template=mat_munu, &
321 matrix_type=dbcsr_type_no_symmetry)
322 CALL dbcsr_copy(exchange_env%mat_hfx(ispin)%matrix, mat_munu)
323 END DO
324
325 CALL dbcsr_get_info(mat_munu, nfullcols_total=number_of_aos)
326
327 CALL dbcsr_create(exchange_env%work_ao, template=mat_munu, &
328 matrix_type=dbcsr_type_no_symmetry)
329
330 ALLOCATE (exchange_env%dbcsr_Gamma_inu_P(nspins))
331 CALL dbcsr_allocate_matrix_set(exchange_env%dbcsr_Gamma_munu_P, nspins)
332 DO ispin = 1, nspins
333 ALLOCATE (exchange_env%dbcsr_Gamma_munu_P(ispin)%matrix)
334 CALL dbcsr_create(exchange_env%dbcsr_Gamma_munu_P(ispin)%matrix, template=mat_munu, &
335 matrix_type=dbcsr_type_no_symmetry)
336 CALL dbcsr_copy(exchange_env%dbcsr_Gamma_munu_P(ispin)%matrix, mat_munu)
337 CALL dbcsr_set(exchange_env%dbcsr_Gamma_munu_P(ispin)%matrix, 0.0_dp)
338
339 CALL dbcsr_create(exchange_env%dbcsr_Gamma_inu_P(ispin), template=exchange_env%mo_coeff_o(ispin))
340 CALL dbcsr_copy(exchange_env%dbcsr_Gamma_inu_P(ispin), exchange_env%mo_coeff_o(ispin))
341 CALL dbcsr_set(exchange_env%dbcsr_Gamma_inu_P(ispin), 0.0_dp)
342 END DO
343
344 ALLOCATE (exchange_env%struct_Gamma(nspins))
345 DO ispin = 1, nspins
346 CALL cp_fm_get_info(fm_mat_s(ispin), nrow_global=dimen_ri, ncol_global=dimen_ia)
347 CALL cp_fm_struct_create(exchange_env%struct_Gamma(ispin)%struct, template_fmstruct=fm_mat_s(ispin)%matrix_struct, &
348 nrow_global=dimen_ia, ncol_global=dimen_ri)
349 END DO
350
351 CALL timestop(handle)
352
353 END SUBROUTINE hfx_create_subgroup
354
355! **************************************************************************************************
356!> \brief ...
357!> \param exchange_work ...
358! **************************************************************************************************
359 SUBROUTINE rpa_exchange_work_release(exchange_work)
360 CLASS(rpa_exchange_work_type), INTENT(INOUT) :: exchange_work
361
362 IF (ALLOCATED(exchange_work%homo)) DEALLOCATE (exchange_work%homo)
363 IF (ALLOCATED(exchange_work%virtual)) DEALLOCATE (exchange_work%virtual)
364 IF (ALLOCATED(exchange_work%dimen_ia)) DEALLOCATE (exchange_work%dimen_ia)
365 NULLIFY (exchange_work%para_env_sub)
366 CALL release_group_dist(exchange_work%aux_func_dist)
367 IF (ALLOCATED(exchange_work%aux2send)) DEALLOCATE (exchange_work%aux2send)
368 CALL cp_fm_release(exchange_work%fm_mat_Q_tmp)
369 CALL cp_fm_release(exchange_work%fm_mat_U)
370 CALL cp_fm_release(exchange_work%fm_mat_R_half_gemm)
371
372 CALL exchange_work%exchange_env%release()
373 END SUBROUTINE rpa_exchange_work_release
374
375! **************************************************************************************************
376!> \brief ...
377!> \param exchange_env ...
378! **************************************************************************************************
379 SUBROUTINE hfx_release_subgroup(exchange_env)
380 CLASS(rpa_exchange_env_type), INTENT(INOUT) :: exchange_env
381
382 INTEGER :: ispin
383
384 NULLIFY (exchange_env%para_env, exchange_env%hfx_sections)
385
386 IF (ASSOCIATED(exchange_env%x_data)) THEN
387 CALL hfx_release(exchange_env%x_data)
388 NULLIFY (exchange_env%x_data)
389 END IF
390
391 CALL dbcsr_release(exchange_env%work_ao)
392
393 IF (ASSOCIATED(exchange_env%dbcsr_Gamma_munu_P)) THEN
394 DO ispin = 1, SIZE(exchange_env%mat_hfx, 1)
395 CALL dbcsr_release(exchange_env%dbcsr_Gamma_munu_P(ispin)%matrix)
396 CALL dbcsr_release(exchange_env%mat_hfx(ispin)%matrix)
397 CALL dbcsr_release(exchange_env%dbcsr_Gamma_inu_P(ispin))
398 CALL dbcsr_release(exchange_env%mo_coeff_o(ispin))
399 CALL dbcsr_release(exchange_env%mo_coeff_v(ispin))
400 DEALLOCATE (exchange_env%mat_hfx(ispin)%matrix)
401 DEALLOCATE (exchange_env%dbcsr_Gamma_munu_P(ispin)%matrix)
402 END DO
403 DEALLOCATE (exchange_env%mat_hfx, exchange_env%dbcsr_Gamma_munu_P)
404 DEALLOCATE (exchange_env%dbcsr_Gamma_inu_P, exchange_env%mo_coeff_o, exchange_env%mo_coeff_v)
405 NULLIFY (exchange_env%mat_hfx, exchange_env%dbcsr_Gamma_munu_P)
406 END IF
407 IF (ALLOCATED(exchange_env%struct_Gamma)) THEN
408 DO ispin = 1, SIZE(exchange_env%struct_Gamma)
409 CALL cp_fm_struct_release(exchange_env%struct_Gamma(ispin)%struct)
410 END DO
411 DEALLOCATE (exchange_env%struct_Gamma)
412 END IF
413 END SUBROUTINE hfx_release_subgroup
414
415! **************************************************************************************************
416!> \brief Main driver for RPA-exchange energies
417!> \param exchange_work ...
418!> \param fm_mat_Q ...
419!> \param eig ...
420!> \param fm_mat_S ...
421!> \param omega ...
422!> \param e_exchange_corr exchange energy correction for a quadrature point
423!> \param mp2_env ...
424!> \author Vladimir Rybkin, 07/2016
425! **************************************************************************************************
426 SUBROUTINE rpa_exchange_work_compute(exchange_work, fm_mat_Q, eig, fm_mat_S, omega, &
427 e_exchange_corr, mp2_env)
428 CLASS(rpa_exchange_work_type), INTENT(INOUT) :: exchange_work
429 TYPE(cp_fm_type), INTENT(IN) :: fm_mat_q
430 REAL(kind=dp), DIMENSION(:, :), INTENT(IN) :: eig
431 TYPE(cp_fm_type), DIMENSION(:), INTENT(INOUT) :: fm_mat_s
432 REAL(kind=dp), INTENT(IN) :: omega
433 REAL(kind=dp), INTENT(INOUT) :: e_exchange_corr
434 TYPE(mp2_type), INTENT(INOUT) :: mp2_env
435
436 CHARACTER(LEN=*), PARAMETER :: routinen = 'rpa_exchange_work_compute'
437 REAL(kind=dp), PARAMETER :: thresh = 0.0000001_dp
438
439 INTEGER :: handle, nspins, dimen_ri, iib
440 REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: eigenval
441
442 IF (exchange_work%exchange_correction == rpa_exchange_none) RETURN
443
444 CALL timeset(routinen, handle)
445
446 CALL cp_fm_get_info(fm_mat_q, ncol_global=dimen_ri)
447
448 nspins = SIZE(fm_mat_s)
449
450 ! Eigenvalues
451 ALLOCATE (eigenval(dimen_ri))
452 eigenval = 0.0_dp
453
454 CALL cp_fm_set_all(matrix=exchange_work%fm_mat_Q_tmp, alpha=0.0_dp)
455 CALL cp_fm_set_all(matrix=exchange_work%fm_mat_U, alpha=0.0_dp)
456
457 ! Copy Q to Q_tmp
458 CALL cp_fm_to_fm(fm_mat_q, exchange_work%fm_mat_Q_tmp)
459 ! Diagonalize Q
460 CALL choose_eigv_solver(exchange_work%fm_mat_Q_tmp, exchange_work%fm_mat_U, eigenval)
461
462 ! Calculate diagonal matrix for R_half
463
464 ! Manipulate eigenvalues to get diagonal matrix
465 IF (exchange_work%exchange_correction == rpa_exchange_axk) THEN
466 DO iib = 1, dimen_ri
467 IF (abs(eigenval(iib)) >= thresh) THEN
468 eigenval(iib) = &
469 sqrt((1.0_dp/(eigenval(iib)**2))*log(1.0_dp + eigenval(iib)) &
470 - 1.0_dp/(eigenval(iib)*(eigenval(iib) + 1.0_dp)))
471 ELSE
472 eigenval(iib) = sqrthalf
473 END IF
474 END DO
475 ELSE IF (exchange_work%exchange_correction == rpa_exchange_sosex) THEN
476 DO iib = 1, dimen_ri
477 IF (abs(eigenval(iib)) >= thresh) THEN
478 eigenval(iib) = &
479 sqrt(-(1.0_dp/(eigenval(iib)**2))*log(1.0_dp + eigenval(iib)) &
480 + 1.0_dp/eigenval(iib))
481 ELSE
482 eigenval(iib) = sqrthalf
483 END IF
484 END DO
485 ELSE
486 cpabort("Unknown RPA exchange correction")
487 END IF
488
489 ! fm_mat_U now contains some sqrt of the required matrix-valued function
490 CALL cp_fm_column_scale(exchange_work%fm_mat_U, eigenval)
491
492 ! Release memory
493 DEALLOCATE (eigenval)
494
495 ! Redistribute fm_mat_U for "rectangular" multiplication: ia*P P*P
496 CALL cp_fm_set_all(matrix=exchange_work%fm_mat_R_half_gemm, alpha=0.0_dp)
497
498 CALL cp_fm_to_fm_submat_general(exchange_work%fm_mat_U, exchange_work%fm_mat_R_half_gemm, dimen_ri, &
499 dimen_ri, 1, 1, 1, 1, exchange_work%fm_mat_U%matrix_struct%context)
500
501 IF (mp2_env%ri_rpa%use_hfx_implementation) THEN
502 CALL exchange_work%compute_hfx(fm_mat_s, eig, omega, e_exchange_corr)
503 ELSE
504 CALL exchange_work%compute_fm(fm_mat_s, eig, omega, e_exchange_corr, mp2_env)
505 END IF
506
507 CALL timestop(handle)
508
509 END SUBROUTINE rpa_exchange_work_compute
510
511! **************************************************************************************************
512!> \brief Main driver for RPA-exchange energies
513!> \param exchange_work ...
514!> \param fm_mat_S ...
515!> \param eig ...
516!> \param omega ...
517!> \param e_exchange_corr exchange energy correction for a quadrature point
518!> \param mp2_env ...
519!> \author Frederick Stein, May-June 2024
520! **************************************************************************************************
521 SUBROUTINE rpa_exchange_work_compute_fm(exchange_work, fm_mat_S, eig, omega, &
522 e_exchange_corr, mp2_env)
523 CLASS(rpa_exchange_work_type), INTENT(INOUT) :: exchange_work
524 TYPE(cp_fm_type), DIMENSION(:), INTENT(IN) :: fm_mat_s
525 REAL(kind=dp), DIMENSION(:, :), INTENT(IN) :: eig
526 REAL(kind=dp), INTENT(IN) :: omega
527 REAL(kind=dp), INTENT(INOUT) :: e_exchange_corr
528 TYPE(mp2_type), INTENT(INOUT) :: mp2_env
529
530 CHARACTER(LEN=*), PARAMETER :: routinen = 'rpa_exchange_work_compute_fm'
531
532 INTEGER :: handle, ispin, nspins, p, q, l_size_gamma, hom, virt, i, &
533 send_proc, recv_proc, recv_size, max_aux_size, proc_shift, dimen_ia, &
534 block_size, p_start, p_end, p_size, q_start, q_size, q_end, handle2, my_aux_size, my_virt
535 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :, :), TARGET :: mat_gamma_3_3d
536 REAL(kind=dp), POINTER, DIMENSION(:), CONTIGUOUS :: mat_gamma_3_1d
537 REAL(kind=dp), POINTER, DIMENSION(:, :), CONTIGUOUS :: mat_gamma_3_2d
538 REAL(kind=dp), ALLOCATABLE, TARGET, DIMENSION(:) :: recv_buffer_1d
539 REAL(kind=dp), POINTER, DIMENSION(:, :), CONTIGUOUS :: recv_buffer_2d
540 REAL(kind=dp), POINTER, DIMENSION(:, :, :), CONTIGUOUS :: recv_buffer_3d
541 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :, :) :: mat_b_iap
542 REAL(kind=dp), ALLOCATABLE, DIMENSION(:), TARGET :: product_matrix_1d
543 REAL(kind=dp), POINTER, DIMENSION(:, :), CONTIGUOUS :: product_matrix_2d
544 REAL(kind=dp), POINTER, DIMENSION(:, :, :, :), CONTIGUOUS :: product_matrix_4d
545 TYPE(cp_fm_type) :: fm_mat_gamma_3
546 TYPE(mp_para_env_type), POINTER :: para_env
547 TYPE(group_dist_d1_type) :: virt_dist
548
549 CALL timeset(routinen, handle)
550
551 nspins = SIZE(fm_mat_s)
552
553 CALL get_group_dist(exchange_work%aux_func_dist, exchange_work%color_sub, sizes=my_aux_size)
554
555 e_exchange_corr = 0.0_dp
556 max_aux_size = maxsize(exchange_work%aux_func_dist)
557
558 ! local_gemm_ctx has a very large footprint the first time this routine is
559 ! called.
560 CALL mp2_env%local_gemm_ctx%create(local_gemm_pu_gpu)
561 CALL mp2_env%local_gemm_ctx%set_op_threshold_gpu(128*128*128*2)
562
563 DO ispin = 1, nspins
564 hom = exchange_work%homo(ispin)
565 virt = exchange_work%virtual(ispin)
566 dimen_ia = hom*virt
567 IF (hom < 1 .OR. virt < 1) cycle
568
569 CALL cp_fm_get_info(fm_mat_s(ispin), para_env=para_env)
570
571 CALL cp_fm_create(fm_mat_gamma_3, fm_mat_s(ispin)%matrix_struct)
572 CALL cp_fm_set_all(matrix=fm_mat_gamma_3, alpha=0.0_dp)
573
574 ! Update G with a new value of Omega: in practice, it is G*S
575
576 ! Scale fm_work_iaP
577 CALL calc_fm_mat_s_rpa(fm_mat_s(ispin), .true., virt, eig(:, ispin), &
578 hom, omega, 0.0_dp)
579
580 ! Calculate Gamma_3: Gamma_3 = G*S*R^(1/2) = G*S*R^(1/2)
581 CALL parallel_gemm(transa="T", transb="N", m=exchange_work%dimen_RI, n=dimen_ia, k=exchange_work%dimen_RI, alpha=1.0_dp, &
582 matrix_a=exchange_work%fm_mat_R_half_gemm, matrix_b=fm_mat_s(ispin), beta=0.0_dp, &
583 matrix_c=fm_mat_gamma_3)
584
585 CALL create_group_dist(virt_dist, exchange_work%para_env_sub%num_pe, virt)
586
587 ! Remove extra factor from S after the multiplication (to return to the original matrix)
588 CALL remove_scaling_factor_rpa(fm_mat_s(ispin), virt, eig(:, ispin), hom, omega)
589
590 CALL exchange_work%redistribute_into_subgroups(fm_mat_gamma_3, mat_gamma_3_3d, ispin, virt_dist)
591 CALL cp_fm_release(fm_mat_gamma_3)
592
593 ! We need only the pure matrix
594 CALL remove_scaling_factor_rpa(fm_mat_s(ispin), virt, eig(:, ispin), hom, omega)
595
596 ! Reorder matrix from (P, i*a) -> (a, i, P) with P being distributed within subgroups
597 CALL exchange_work%redistribute_into_subgroups(fm_mat_s(ispin), mat_b_iap, ispin, virt_dist)
598
599 ! Return to the original tensor
600 CALL calc_fm_mat_s_rpa(fm_mat_s(ispin), .true., virt, eig(:, ispin), hom, omega, 0.0_dp)
601
602 l_size_gamma = SIZE(mat_gamma_3_3d, 3)
603 my_virt = SIZE(mat_gamma_3_3d, 1)
604 block_size = exchange_work%block_size
605
606 mat_gamma_3_1d(1:int(my_virt, kind=int_8)*hom*my_aux_size) => mat_gamma_3_3d(:, :, 1:my_aux_size)
607 mat_gamma_3_2d(1:my_virt, 1:hom*my_aux_size) => mat_gamma_3_1d(1:int(my_virt, kind=int_8)*hom*my_aux_size)
608
609 ALLOCATE (product_matrix_1d(int(hom*min(block_size, l_size_gamma), kind=int_8)* &
610 int(hom*min(block_size, max_aux_size), kind=int_8)))
611 ALLOCATE (recv_buffer_1d(int(virt, kind=int_8)*hom*max_aux_size))
612 recv_buffer_2d(1:my_virt, 1:hom*max_aux_size) => recv_buffer_1d(1:int(virt, kind=int_8)*hom*max_aux_size)
613 recv_buffer_3d(1:my_virt, 1:hom, 1:max_aux_size) => recv_buffer_1d(1:int(virt, kind=int_8)*hom*max_aux_size)
614 DO proc_shift = 0, para_env%num_pe - 1, exchange_work%para_env_sub%num_pe
615 send_proc = modulo(para_env%mepos + proc_shift, para_env%num_pe)
616 recv_proc = modulo(para_env%mepos - proc_shift, para_env%num_pe)
617
618 CALL get_group_dist(exchange_work%aux_func_dist, recv_proc/exchange_work%para_env_sub%num_pe, sizes=recv_size)
619
620 IF (recv_size == 0) recv_proc = mp_proc_null
621
622 CALL para_env%sendrecv(mat_b_iap, send_proc, recv_buffer_3d(:, :, 1:recv_size), recv_proc)
623
624 IF (recv_size == 0) cycle
625
626 DO p_start = 1, l_size_gamma, block_size
627 p_end = min(l_size_gamma, p_start + block_size - 1)
628 p_size = p_end - p_start + 1
629 DO q_start = 1, recv_size, block_size
630 q_end = min(recv_size, q_start + block_size - 1)
631 q_size = q_end - q_start + 1
632
633 ! Reassign product_matrix pointers to enforce contiguity of target array
634 product_matrix_2d(1:hom*p_size, 1:hom*q_size) => &
635 product_matrix_1d(1:int(hom*p_size, kind=int_8)*int(hom*q_size, kind=int_8))
636 product_matrix_4d(1:hom, 1:p_size, 1:hom, 1:q_size) => &
637 product_matrix_1d(1:int(hom*p_size, kind=int_8)*int(hom*q_size, kind=int_8))
638
639 CALL timeset(routinen//"_gemm", handle2)
640 CALL mp2_env%local_gemm_ctx%gemm("T", "N", hom*p_size, hom*q_size, my_virt, 1.0_dp, &
641 mat_gamma_3_2d(:, hom*(p_start - 1) + 1:hom*p_end), my_virt, &
642 recv_buffer_2d(:, hom*(q_start - 1) + 1:hom*q_end), my_virt, &
643 0.0_dp, product_matrix_2d, hom*p_size)
644 CALL timestop(handle2)
645
646 CALL timeset(routinen//"_energy", handle2)
647!$OMP PARALLEL DO DEFAULT(NONE) SHARED(P_size, Q_size, hom, product_matrix_4D) &
648!$OMP COLLAPSE(3) REDUCTION(+: e_exchange_corr) PRIVATE(P, Q, i)
649 DO p = 1, p_size
650 DO q = 1, q_size
651 DO i = 1, hom
652 e_exchange_corr = e_exchange_corr + dot_product(product_matrix_4d(i, p, :, q), product_matrix_4d(:, p, i, q))
653 END DO
654 END DO
655 END DO
656 CALL timestop(handle2)
657 END DO
658 END DO
659 END DO
660
661 CALL release_group_dist(virt_dist)
662 IF (ALLOCATED(mat_b_iap)) DEALLOCATE (mat_b_iap)
663 IF (ALLOCATED(mat_gamma_3_3d)) DEALLOCATE (mat_gamma_3_3d)
664 IF (ALLOCATED(product_matrix_1d)) DEALLOCATE (product_matrix_1d)
665 IF (ALLOCATED(recv_buffer_1d)) DEALLOCATE (recv_buffer_1d)
666 END DO
667
668 CALL mp2_env%local_gemm_ctx%destroy()
669
670 IF (nspins == 2) e_exchange_corr = e_exchange_corr*2.0_dp
671 IF (nspins == 1) e_exchange_corr = e_exchange_corr*4.0_dp
672
673 CALL timestop(handle)
674
675 END SUBROUTINE rpa_exchange_work_compute_fm
676
677! **************************************************************************************************
678!> \brief Contract RPA-exchange density matrix with HF exchange integrals and evaluate the correction
679!> \param exchange_work ...
680!> \param fm_mat_S ...
681!> \param eig ...
682!> \param omega ...
683!> \param e_exchange_corr ...
684!> \author Vladimir Rybkin, 08/2016
685! **************************************************************************************************
686 SUBROUTINE rpa_exchange_work_compute_hfx(exchange_work, fm_mat_S, eig, omega, e_exchange_corr)
687 CLASS(rpa_exchange_work_type), INTENT(INOUT) :: exchange_work
688 TYPE(cp_fm_type), DIMENSION(:), INTENT(INOUT) :: fm_mat_s
689 REAL(kind=dp), DIMENSION(:, :), INTENT(IN) :: eig
690 REAL(kind=dp), INTENT(IN) :: omega
691 REAL(kind=dp), INTENT(OUT) :: e_exchange_corr
692
693 CHARACTER(LEN=*), PARAMETER :: routinen = 'rpa_exchange_work_compute_hfx'
694
695 INTEGER :: handle, ispin, my_aux_start, my_aux_end, &
696 my_aux_size, nspins, l_counter, dimen_ia, hom, virt
697 REAL(kind=dp) :: e_exchange_p
698 TYPE(dbcsr_matrix_p_set), DIMENSION(:), ALLOCATABLE :: dbcsr_gamma_3
699 TYPE(cp_fm_type) :: fm_mat_gamma_3
700 TYPE(mp_para_env_type), POINTER :: para_env
701
702 CALL timeset(routinen, handle)
703
704 e_exchange_corr = 0.0_dp
705
706 nspins = SIZE(fm_mat_s)
707
708 CALL get_group_dist(exchange_work%aux_func_dist, exchange_work%color_sub, my_aux_start, my_aux_end, my_aux_size)
709
710 ALLOCATE (dbcsr_gamma_3(nspins))
711 DO ispin = 1, nspins
712 hom = exchange_work%homo(ispin)
713 virt = exchange_work%virtual(ispin)
714 dimen_ia = hom*virt
715 IF (hom < 1 .OR. virt < 1) cycle
716
717 CALL cp_fm_get_info(fm_mat_s(ispin), para_env=para_env)
718
719 CALL cp_fm_create(fm_mat_gamma_3, exchange_work%exchange_env%struct_Gamma(ispin)%struct)
720 CALL cp_fm_set_all(matrix=fm_mat_gamma_3, alpha=0.0_dp)
721
722 ! Update G with a new value of Omega: in practice, it is G*S
723
724 ! Scale fm_work_iaP
725 CALL calc_fm_mat_s_rpa(fm_mat_s(ispin), .true., virt, eig(:, ispin), &
726 hom, omega, 0.0_dp)
727
728 ! Calculate Gamma_3: Gamma_3 = G*S*R^(1/2) = G*S*R^(1/2)
729 CALL parallel_gemm(transa="T", transb="N", m=dimen_ia, n=exchange_work%dimen_RI, &
730 k=exchange_work%dimen_RI, alpha=1.0_dp, &
731 matrix_a=fm_mat_s(ispin), matrix_b=exchange_work%fm_mat_R_half_gemm, beta=0.0_dp, &
732 matrix_c=fm_mat_gamma_3)
733
734 ! Remove extra factor from S after the multiplication (to return to the original matrix)
735 CALL remove_scaling_factor_rpa(fm_mat_s(ispin), virt, eig(:, ispin), hom, omega)
736
737 ! Copy Gamma_ia_P^3 to dbcsr matrix set
738 block
739 INTEGER :: first_row, last_row, nlocal
740 REAL(kind=dp), ALLOCATABLE :: slices(:, :)
741 TYPE(group_dist_d1_type) :: row_distribution
742
743 CALL create_group_dist(row_distribution, exchange_work%para_env_sub%num_pe, dimen_ia)
744 CALL get_group_dist(row_distribution, exchange_work%para_env_sub%mepos, first_row, last_row, nlocal)
745 CALL cp_fm_to_array(slices, first_row, last_row, my_aux_start, my_aux_end, fm_mat_gamma_3)
746 ALLOCATE (dbcsr_gamma_3(ispin)%matrix_set(my_aux_size))
747 CALL cp_array_to_dbcsr_slices(slices, hom, virt, exchange_work%para_env_sub, first_row, last_row, &
748 exchange_work%exchange_env%mo_coeff_o(ispin), &
749 matrices=dbcsr_gamma_3(ispin)%matrix_set)
750 CALL release_group_dist(row_distribution)
751 END block
752 END DO
753
754 DO l_counter = 1, my_aux_size
755 DO ispin = 1, nspins
756 ! Do dbcsr multiplication: transform the virtual index
757 CALL dbcsr_multiply("N", "T", 1.0_dp, exchange_work%exchange_env%mo_coeff_v(ispin), &
758 dbcsr_gamma_3(ispin)%matrix_set(l_counter), &
759 0.0_dp, exchange_work%exchange_env%dbcsr_Gamma_inu_P(ispin), &
760 filter_eps=exchange_work%exchange_env%eps_filter)
761
762 CALL dbcsr_release(dbcsr_gamma_3(ispin)%matrix_set(l_counter))
763
764 ! Do dbcsr multiplication: transform the occupied index
765 CALL dbcsr_multiply("N", "T", 0.5_dp, exchange_work%exchange_env%dbcsr_Gamma_inu_P(ispin), &
766 exchange_work%exchange_env%mo_coeff_o(ispin), &
767 0.0_dp, exchange_work%exchange_env%dbcsr_Gamma_munu_P(ispin)%matrix, &
768 filter_eps=exchange_work%exchange_env%eps_filter)
769 CALL dbcsr_multiply("N", "T", 0.5_dp, exchange_work%exchange_env%mo_coeff_o(ispin), &
770 exchange_work%exchange_env%dbcsr_Gamma_inu_P(ispin), &
771 1.0_dp, exchange_work%exchange_env%dbcsr_Gamma_munu_P(ispin)%matrix, &
772 filter_eps=exchange_work%exchange_env%eps_filter)
773
774 CALL dbcsr_set(exchange_work%exchange_env%mat_hfx(ispin)%matrix, 0.0_dp)
775 END DO
776
777 CALL tddft_hfx_matrix(exchange_work%exchange_env%mat_hfx, exchange_work%exchange_env%dbcsr_Gamma_munu_P, &
778 exchange_work%exchange_env%qs_env, .false., &
779 exchange_work%exchange_env%my_recalc_hfx_integrals, &
780 exchange_work%exchange_env%hfx_sections, exchange_work%exchange_env%x_data, &
781 exchange_work%exchange_env%para_env)
782
783 exchange_work%exchange_env%my_recalc_hfx_integrals = .false.
784 DO ispin = 1, nspins
785 CALL dbcsr_multiply("N", "T", 1.0_dp, exchange_work%exchange_env%mat_hfx(ispin)%matrix, &
786 exchange_work%exchange_env%dbcsr_Gamma_munu_P(ispin)%matrix, &
787 0.0_dp, exchange_work%exchange_env%work_ao, filter_eps=exchange_work%exchange_env%eps_filter)
788 CALL dbcsr_trace(exchange_work%exchange_env%work_ao, e_exchange_p)
789 e_exchange_corr = e_exchange_corr - e_exchange_p
790 END DO
791 END DO
792
793 IF (nspins == 2) e_exchange_corr = e_exchange_corr
794 IF (nspins == 1) e_exchange_corr = e_exchange_corr*4.0_dp
795
796 CALL timestop(handle)
797
798 END SUBROUTINE rpa_exchange_work_compute_hfx
799
800! **************************************************************************************************
801!> \brief ...
802!> \param exchange_work ...
803!> \param fm_mat ...
804!> \param mat ...
805!> \param ispin ...
806!> \param virt_dist ...
807! **************************************************************************************************
808 SUBROUTINE redistribute_into_subgroups(exchange_work, fm_mat, mat, ispin, virt_dist)
809 CLASS(rpa_exchange_work_type), INTENT(IN) :: exchange_work
810 TYPE(cp_fm_type), INTENT(IN) :: fm_mat
811 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :, :), &
812 INTENT(OUT) :: mat
813 INTEGER, INTENT(IN) :: ispin
814 TYPE(group_dist_d1_type), INTENT(IN) :: virt_dist
815
816 CHARACTER(LEN=*), PARAMETER :: routinen = 'redistribute_into_subgroups'
817
818 INTEGER :: aux_counter, aux_global, aux_local, aux_proc, avirt, dimen_ri, handle, handle2, &
819 ia_global, ia_local, iocc, max_number_recv, max_number_send, my_aux_end, my_aux_size, &
820 my_aux_start, my_process_column, my_process_row, my_virt_end, my_virt_size, &
821 my_virt_start, proc, proc_shift, recv_proc, send_proc, virt_counter, virt_proc, group_size
822 INTEGER, ALLOCATABLE, DIMENSION(:) :: data2send, recv_col_indices, &
823 recv_row_indices, send_aux_indices, send_virt_indices, virt2send
824 INTEGER, DIMENSION(2) :: recv_shape
825 INTEGER, DIMENSION(:), POINTER :: aux_distribution_fm, col_indices, &
826 ia_distribution_fm, row_indices
827 INTEGER, DIMENSION(:, :), POINTER :: mpi2blacs
828 REAL(kind=dp), ALLOCATABLE, DIMENSION(:), TARGET :: recv_buffer, send_buffer
829 REAL(kind=dp), CONTIGUOUS, DIMENSION(:, :), &
830 POINTER :: recv_ptr, send_ptr
831 TYPE(cp_blacs_env_type), POINTER :: context
832 TYPE(mp_para_env_type), POINTER :: para_env
833
834 CALL timeset(routinen, handle)
835
836 CALL cp_fm_get_info(matrix=fm_mat, &
837 nrow_locals=aux_distribution_fm, &
838 col_indices=col_indices, &
839 row_indices=row_indices, &
840 ncol_locals=ia_distribution_fm, &
841 context=context, &
842 nrow_global=dimen_ri, &
843 para_env=para_env)
844
845 IF (exchange_work%homo(ispin) <= 0 .OR. exchange_work%virtual(ispin) <= 0) THEN
846 CALL get_group_dist(virt_dist, exchange_work%para_env_sub%mepos, my_virt_start, my_virt_end, my_virt_size)
847 ALLOCATE (mat(exchange_work%homo(ispin), my_virt_size, dimen_ri))
848 CALL timestop(handle)
849 RETURN
850 END IF
851
852 group_size = exchange_work%para_env_sub%num_pe
853
854 CALL timeset(routinen//"_prep", handle2)
855 CALL get_group_dist(exchange_work%aux_func_dist, exchange_work%color_sub, my_aux_start, my_aux_end, my_aux_size)
856 CALL get_group_dist(virt_dist, exchange_work%para_env_sub%mepos, my_virt_start, my_virt_end, my_virt_size)
857 CALL context%get(my_process_column=my_process_column, my_process_row=my_process_row, mpi2blacs=mpi2blacs)
858
859 ! Determine the number of columns to send
860 ALLOCATE (send_aux_indices(maxval(exchange_work%aux2send)))
861 ALLOCATE (virt2send(0:group_size - 1))
862 virt2send = 0
863 DO ia_local = 1, ia_distribution_fm(my_process_column)
864 ia_global = col_indices(ia_local)
865 avirt = mod(ia_global - 1, exchange_work%virtual(ispin)) + 1
866 proc = group_dist_proc(virt_dist, avirt)
867 virt2send(proc) = virt2send(proc) + 1
868 END DO
869
870 ALLOCATE (data2send(0:para_env%num_pe - 1))
871 DO aux_proc = 0, exchange_work%ngroup - 1
872 DO virt_proc = 0, group_size - 1
873 data2send(aux_proc*group_size + virt_proc) = exchange_work%aux2send(aux_proc)*virt2send(virt_proc)
874 END DO
875 END DO
876
877 ALLOCATE (send_virt_indices(maxval(virt2send)))
878 max_number_send = maxval(data2send)
879
880 ALLOCATE (send_buffer(int(max_number_send, kind=int_8)*exchange_work%homo(ispin)))
881 max_number_recv = max_number_send
882 CALL para_env%max(max_number_recv)
883 ALLOCATE (recv_buffer(max_number_recv))
884
885 ALLOCATE (mat(my_virt_size, exchange_work%homo(ispin), my_aux_size))
886
887 CALL timestop(handle2)
888
889 CALL timeset(routinen//"_own", handle2)
890 ! Start with own data
891 DO aux_local = 1, aux_distribution_fm(my_process_row)
892 aux_global = row_indices(aux_local)
893 IF (aux_global < my_aux_start .OR. aux_global > my_aux_end) cycle
894 DO ia_local = 1, ia_distribution_fm(my_process_column)
895 ia_global = fm_mat%matrix_struct%col_indices(ia_local)
896
897 iocc = (ia_global - 1)/exchange_work%virtual(ispin) + 1
898 avirt = mod(ia_global - 1, exchange_work%virtual(ispin)) + 1
899
900 IF (my_virt_start > avirt .OR. my_virt_end < avirt) cycle
901
902 mat(avirt - my_virt_start + 1, iocc, aux_global - my_aux_start + 1) = fm_mat%local_data(aux_local, ia_local)
903 END DO
904 END DO
905 CALL timestop(handle2)
906
907 DO proc_shift = 1, para_env%num_pe - 1
908 send_proc = modulo(para_env%mepos + proc_shift, para_env%num_pe)
909 recv_proc = modulo(para_env%mepos - proc_shift, para_env%num_pe)
910
911 CALL timeset(routinen//"_pack_buffer", handle2)
912 send_ptr(1:virt2send(mod(send_proc, group_size)), &
913 1:exchange_work%aux2send(send_proc/group_size)) => &
914 send_buffer(1:int(virt2send(mod(send_proc, group_size)), kind=int_8)* &
915 exchange_work%aux2send(send_proc/group_size))
916! Pack send buffer
917 aux_counter = 0
918 DO aux_local = 1, aux_distribution_fm(my_process_row)
919 aux_global = row_indices(aux_local)
920 proc = group_dist_proc(exchange_work%aux_func_dist, aux_global)
921 IF (proc /= send_proc/group_size) cycle
922 aux_counter = aux_counter + 1
923 virt_counter = 0
924 DO ia_local = 1, ia_distribution_fm(my_process_column)
925 ia_global = col_indices(ia_local)
926 avirt = mod(ia_global - 1, exchange_work%virtual(ispin)) + 1
927
928 proc = group_dist_proc(virt_dist, avirt)
929 IF (proc /= mod(send_proc, group_size)) cycle
930 virt_counter = virt_counter + 1
931 send_ptr(virt_counter, aux_counter) = fm_mat%local_data(aux_local, ia_local)
932 send_virt_indices(virt_counter) = ia_global
933 END DO
934 send_aux_indices(aux_counter) = aux_global
935 END DO
936 CALL timestop(handle2)
937
938 CALL timeset(routinen//"_ex_size", handle2)
939 recv_shape = [1, 1]
940 CALL para_env%sendrecv(shape(send_ptr), send_proc, recv_shape, recv_proc)
941 CALL timestop(handle2)
942
943 IF (SIZE(send_ptr) == 0) send_proc = mp_proc_null
944 IF (product(recv_shape) == 0) recv_proc = mp_proc_null
945
946 CALL timeset(routinen//"_ex_idx", handle2)
947 ALLOCATE (recv_row_indices(recv_shape(1)), recv_col_indices(recv_shape(2)))
948 CALL para_env%sendrecv(send_virt_indices(1:virt_counter), send_proc, recv_row_indices, recv_proc)
949 CALL para_env%sendrecv(send_aux_indices(1:aux_counter), send_proc, recv_col_indices, recv_proc)
950 CALL timestop(handle2)
951
952 ! Prepare pointer to recv buffer (consider transposition while packing the send buffer)
953 recv_ptr(1:recv_shape(1), 1:max(1, recv_shape(2))) => recv_buffer(1:recv_shape(1)*max(1, recv_shape(2)))
954
955 CALL timeset(routinen//"_sendrecv", handle2)
956! Perform communication
957 CALL para_env%sendrecv(send_ptr, send_proc, recv_ptr, recv_proc)
958 CALL timestop(handle2)
959
960 IF (recv_proc == mp_proc_null) THEN
961 DEALLOCATE (recv_row_indices, recv_col_indices)
962 cycle
963 END IF
964
965 CALL timeset(routinen//"_unpack", handle2)
966! Unpack receive buffer
967 DO aux_local = 1, SIZE(recv_col_indices)
968 aux_global = recv_col_indices(aux_local)
969
970 DO ia_local = 1, SIZE(recv_row_indices)
971 ia_global = recv_row_indices(ia_local)
972
973 iocc = (ia_global - 1)/exchange_work%virtual(ispin) + 1
974 avirt = mod(ia_global - 1, exchange_work%virtual(ispin)) + 1
975
976 mat(avirt - my_virt_start + 1, iocc, aux_global - my_aux_start + 1) = recv_ptr(ia_local, aux_local)
977 END DO
978 END DO
979 CALL timestop(handle2)
980
981 IF (ALLOCATED(recv_row_indices)) DEALLOCATE (recv_row_indices)
982 IF (ALLOCATED(recv_col_indices)) DEALLOCATE (recv_col_indices)
983 END DO
984
985 DEALLOCATE (send_aux_indices, send_virt_indices)
986
987 CALL timestop(handle)
988
989 END SUBROUTINE redistribute_into_subgroups
990
991END MODULE rpa_exchange
static GRID_HOST_DEVICE int modulo(int a, int m)
Equivalent of Fortran's MODULO, which always return a positive number. https://gcc....
Define the atomic kind types and their sub types.
Handles all functions related to the CELL.
Definition cell_types.F:15
methods related to the blacs parallel environment
Defines control structures, which contain the parameters and the settings for the DFT-based calculati...
subroutine, public dbcsr_copy(matrix_b, matrix_a, name, keep_sparsity, keep_imaginary)
...
subroutine, public dbcsr_multiply(transa, transb, alpha, matrix_a, matrix_b, beta, matrix_c, first_row, last_row, first_column, last_column, first_k, last_k, retain_sparsity, filter_eps, flop)
...
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_init_p(matrix)
...
subroutine, public dbcsr_set(matrix, alpha)
...
subroutine, public dbcsr_release(matrix)
...
subroutine, public dbcsr_trace(matrix, trace)
Computes the trace of the given matrix, also known as the sum of its diagonal elements.
DBCSR operations in CP2K.
Redistribution of DBCSR blocks and distributed matrix slices.
subroutine, public cp_array_to_dbcsr_slices(array, nrows, ncols, para_env_sub, first_row, last_row, template, matrices, matrix_ptrs)
Consume a distributed flattened array and create a DBCSR matrix per slice.
Basic linear algebra operations for full matrices.
subroutine, public cp_fm_column_scale(matrixa, scaling, first_col, source)
Scales columns of the full matrix by corresponding factors.
used for collecting some of the diagonalization schemes available for cp_fm_type. cp_fm_power also mo...
Definition cp_fm_diag.F:17
subroutine, public choose_eigv_solver(matrix, eigenvectors, eigenvalues, info)
Choose the Eigensolver depending on which library is available ELPA seems to be unstable for small sy...
Definition cp_fm_diag.F:262
Redistribution of full matrices and distributed arrays.
subroutine, public cp_fm_to_array(mat2d, my_start_row, my_end_row, my_start_col, my_end_col, fm_mat)
Extract distributed rectangular array tiles from a full matrix.
represent the structure of a full matrix
subroutine, public cp_fm_struct_create(fmstruct, para_env, context, nrow_global, ncol_global, nrow_block, ncol_block, descriptor, first_p_pos, local_leading_dimension, template_fmstruct, square_blocks, force_block)
allocates and initializes a full matrix structure
subroutine, public cp_fm_struct_release(fmstruct)
releases a full matrix structure
represent a full matrix distributed on many processors
Definition cp_fm_types.F:15
subroutine, public cp_fm_get_info(matrix, name, nrow_global, ncol_global, nrow_block, ncol_block, nrow_local, ncol_local, row_indices, col_indices, local_data, context, nrow_locals, ncol_locals, matrix_struct, para_env)
returns all kind of information about the full matrix
subroutine, public cp_fm_to_fm_submat_general(source, destination, nrows, ncols, s_firstrow, s_firstcol, d_firstrow, d_firstcol, global_context)
General copy of a submatrix of fm matrix to a submatrix of another fm matrix. The two matrices can ha...
subroutine, public cp_fm_set_all(matrix, alpha, beta)
set all elements of a matrix to the same value, and optionally the diagonal to a different one
subroutine, public cp_fm_create(matrix, matrix_struct, name, nrow, ncol, set_zero)
creates a new full matrix with the given structure
Types to describe group distributions.
elemental integer function, public maxsize(this)
...
elemental integer function, public group_dist_proc(this, pos)
...
Utilities for hfx and admm methods.
subroutine, public tddft_hfx_matrix(matrix_ks, rho_ao, qs_env, update_energy, recalc_integrals, external_hfx_sections, external_x_data, external_para_env)
Add the hfx contributions to the Hamiltonian.
Types and set/get functions for HFX.
Definition hfx_types.F:16
subroutine, public hfx_create(x_data, para_env, hfx_section, atomic_kind_set, qs_kind_set, particle_set, dft_control, cell, orb_basis, ri_basis, nelectron_total, nkp_grid)
This routine allocates and initializes all types in hfx_data
Definition hfx_types.F:566
subroutine, public hfx_release(x_data)
This routine deallocates all data structures
Definition hfx_types.F:1924
collects all constants needed in input so that they can be used without circular dependencies
integer, parameter, public rpa_exchange_none
integer, parameter, public rpa_exchange_sosex
integer, parameter, public rpa_exchange_axk
objects that represent the structure of input sections and the data contained in an input section
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
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 local_gemm_pu_gpu
Definition of mathematical constants and functions.
real(kind=dp), parameter, public sqrthalf
Interface to the message passing library MPI.
integer, parameter, public mp_proc_null
Types needed for MP2 calculations.
Definition mp2_types.F:14
basic linear algebra operations for full matrixes
Define the data structure for the particle information.
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.
Define the quickstep kind type and their sub types.
types that represent a quickstep subsys
subroutine, public qs_subsys_get(subsys, atomic_kinds, atomic_kind_set, particles, particle_set, local_particles, molecules, molecule_set, molecule_kinds, molecule_kind_set, local_molecules, para_env, colvar_p, shell_particles, core_particles, gci, multipoles, natom, nparticle, ncore, nshell, nkind, atprop, virial, results, cell, cell_ref, use_ref_cell, energy, force, qs_kind_set, cp_subsys, nelectron_total, nelectron_spin)
...
Auxiliary routines needed for RPA-exchange given blacs_env to another.
subroutine, public rpa_exchange_needed_mem(mp2_env, homo, virtual, dimen_ri, para_env, mem_per_rank, mem_per_repl)
...
Utility functions for RPA calculations.
Definition rpa_util.F:13
subroutine, public remove_scaling_factor_rpa(fm_mat_s, virtual, eigenval_last, homo, omega_old)
...
Definition rpa_util.F:642
subroutine, public calc_fm_mat_s_rpa(fm_mat_s, first_cycle, virtual, eigenval, homo, omega, omega_old)
...
Definition rpa_util.F:692
parameters that control an scf iteration
Provides all information about an atomic kind.
Type defining parameters related to the simulation cell.
Definition cell_types.F:60
represent a blacs multidimensional parallel environment (for the mpi corrispective see cp_paratypes/m...
represent a full matrix
stores some data used in construction of Kohn-Sham matrix
Definition hfx_types.F:474
stores all the informations relevant to an mpi environment
Provides all information about a quickstep kind.