(git:27c966b)
Loading...
Searching...
No Matches
gw_ri_rs_small_cell_full_kp_utils.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 Green functions, screening and self-energy for periodic RI-RS GW.
10!> \author Jan Wilhelm (with help of ChatGPT, GPT-6 Sol and GPT-6 Astra)
11! **************************************************************************************************
22 USE cp_cfm_diag, ONLY: cp_cfm_power
23 USE cp_cfm_types, ONLY: cp_cfm_create,&
31 USE cp_fm_types, ONLY: cp_fm_get_info,&
34 USE fft_lib, ONLY: fft_3d,&
37 USE fft_plan, ONLY: fft_plan_type
38 USE fft_tools, ONLY: fwfft,&
39 fft_alloc,&
40 fft_dealloc
52 USE input_constants, ONLY: evgw0
53 USE kinds, ONLY: dp,&
54 int_8
64 USE machine, ONLY: m_flush,&
66 USE mathconstants, ONLY: fourpi,&
67 gaussi,&
68 twopi,&
69 z_one,&
70 z_zero
71 USE message_passing, ONLY: mp_comm_type,&
84
85!$ USE omp_lib, ONLY: omp_get_max_threads, omp_get_thread_num, omp_set_num_threads
86#include "./base/base_uses.f90"
87
88 IMPLICIT NONE
89 PRIVATE
90
91 CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'gw_ri_rs_small_cell_full_kp_utils'
92
93 PUBLIC :: compute_sigma_x, &
95 compute_w0, &
99
100 ! Tolerance for a sum of fractional k-point coordinates to be a reciprocal lattice vector.
101 REAL(KIND=dp), PARAMETER :: eps_kpoint = 1.0e-8_dp
102
103 ! Blocking limits and named slots in the six existing CP2K matrix workspaces.
104 INTEGER, PARAMETER :: column_block_cols = 32
105 INTEGER, PARAMETER :: band_kpoints_per_pass = 64, pair_batch_size = 256
106 INTEGER, PARAMETER :: w_chi = 1, w_metric = 2, w_tmp = 3, w_dielectric = 4, &
107 w_output = 5, n_w_matrices = 5
108
109 ! Memory estimates: bytes per MiB and per GB (mp_mem_avail_per_rank_GB).
110 REAL(KIND=dp), PARAMETER :: bytes_per_mib = 1024.0_dp**2, bytes_per_gb = 1.0e9_dp
111 ! A single stage may use stage_memory_share of the available memory. Stages that keep earlier
112 ! data alive reserve reserve_share of it (at least min_reserve_GB) and add library_bytes.
113 REAL(KIND=dp), PARAMETER :: stage_memory_share = 0.5_dp, reserve_share = 0.1_dp, &
114 min_reserve_gb = 1.0_dp, library_bytes = 256.0_dp*bytes_per_mib
115 ! Fixed workspace of the band projection and the Σ(k) of one projection round.
116 REAL(KIND=dp), PARAMETER :: projection_workspace = 64.0_dp*bytes_per_mib, &
117 projection_round_bytes = 512.0_dp*bytes_per_mib
118
119CONTAINS
120
121! **************************************************************************************************
122!> \brief Prepare RI-RS radial data, grid values, and Bloch orbitals.
123!> \param bs_env Band-structure settings and physical results.
124!> \param gwkp_env Temporary periodic GW work data.
125! **************************************************************************************************
126 SUBROUTINE prepare_ri_rs(bs_env, gwkp_env)
127 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
128 TYPE(gwkp_type), INTENT(INOUT) :: gwkp_env
129
130 CHARACTER(LEN=*), PARAMETER :: routinen = 'prepare_ri_rs'
131
132 COMPLEX(dp), ALLOCATABLE :: phi_lmu_k(:, :, :)
133 INTEGER :: handle
134
135 CALL timeset(routinen, handle)
136
137 CALL precompute_ri_rs_radii(bs_env%ri_rs, bs_env%basis_set_AO, bs_env%basis_set_RI, &
138 bs_env%eps_filter, bs_env%unit_nr)
139 CALL setup_ri_rs_grid(bs_env, bs_env%ri_rs%grid_points)
140 CALL compute_z_lp_q(bs_env, bs_env%ri_rs%grid_points, &
141 z_lp_q=gwkp_env%Z_lP_q, z_lp_zero=gwkp_env%Z_lP_zero)
142 CALL build_all_phi_k(bs_env, phi_lmu_k)
143 CALL build_all_psi_k(bs_env, phi_lmu_k, gwkp_env%psi_lm_k)
144 DEALLOCATE (phi_lmu_k)
145 IF (bs_env%gw_flavour == evgw0) THEN
146 cpassert(ALLOCATED(gwkp_env%eigenvalues_G))
147 cpassert(ALLOCATED(bs_env%eigenval_evGW0))
148 cpassert(SIZE(bs_env%eigenval_scf, 2) >= bs_env%nkp_scf_desymm)
149 bs_env%eigenval_evGW0(:, :, :) = bs_env%eigenval_scf
150 END IF
151
152 CALL timestop(handle)
153
154 END SUBROUTINE prepare_ri_rs
155
156! **************************************************************************************************
157!> \brief Form the grid Green functions and RI polarizability.
158!> \param qs_env Quickstep data required by existing integral/lifecycle interfaces.
159!> \param bs_env Band-structure settings and physical results.
160!> \param gwkp_env Temporary periodic GW work data.
161! **************************************************************************************************
162 SUBROUTINE compute_g_and_chi(qs_env, bs_env, gwkp_env)
163 TYPE(qs_environment_type), POINTER :: qs_env
164 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
165 TYPE(gwkp_type), INTENT(INOUT) :: gwkp_env
166
167 CHARACTER(LEN=*), PARAMETER :: routinen = 'compute_G_and_chi'
168
169 INTEGER :: handle
170
171 CALL timeset(routinen, handle)
172
173 cpassert(.NOT. ASSOCIATED(gwkp_env%group_env))
174 gwkp_env%group_env => bs_env%para_env
175 CALL gwkp_env%group_env%retain()
176 gwkp_env%ngroups = 1
177 gwkp_env%my_group = 0
178
179 CALL gamma_point_correction(bs_env, qs_env, gwkp_env)
180 cpassert(.NOT. ALLOCATED(gwkp_env%band_orbitals))
181 CALL compute_chi(bs_env, gwkp_env)
182
183 CALL timestop(handle)
184
185 END SUBROUTINE compute_g_and_chi
186
187! **************************************************************************************************
188!> \brief Compute ε, Wᶜ and its imaginary-time transform, Eqs. (7)-(8).
189!> \param qs_env Required by the existing two-centre integral interfaces.
190!> \param bs_env Band-structure settings and physical results.
191!> \param gwkp_env Periodic GW work data.
192! **************************************************************************************************
193 SUBROUTINE compute_w0(qs_env, bs_env, gwkp_env)
194 TYPE(qs_environment_type), POINTER :: qs_env
195 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
196 TYPE(gwkp_type), INTENT(INOUT) :: gwkp_env
197
198 CHARACTER(LEN=*), PARAMETER :: routinen = 'compute_W0'
199
200 COMPLEX(KIND=dp), ALLOCATABLE :: chi_k_w(:, :), mwm_k_w(:, :)
201 INTEGER :: handle, i_batch, i_group_size, ikp, &
202 ikp_local, j_w, my_qgroup, n_qgroups, &
203 n_ri, nkp, nkp_local, q_group, &
204 q_owner, q_root
205 INTEGER, ALLOCATABLE :: group_load(:), q_batch(:, :), &
206 q_partner(:), q_roots(:)
207 LOGICAL, ALLOCATABLE :: solves_q(:)
208 REAL(kind=dp) :: available_gb, required_bytes, step_start
209 REAL(kind=dp), ALLOCATABLE :: frequency_elapsed(:)
210 TYPE(cp_blacs_env_type), POINTER :: w_blacs
211 TYPE(cp_cfm_type) :: w_matrices(n_w_matrices)
212 TYPE(cp_fm_struct_type), POINTER :: w_matrix_struct
213 TYPE(mp_para_env_type), POINTER :: w_group_env
214
215 CALL timeset(routinen, handle)
216
217 CALL compute_metric_coulomb_factor(bs_env, qs_env, gwkp_env)
218 ! Σˣ and Σᶜ use the band-path orbitals, which include the SCF mesh.
219 DEALLOCATE (gwkp_env%psi_lm_k)
220 n_ri = bs_env%n_RI
221 nkp = bs_env%nkp_scf_desymm
222 IF (bs_env%nkp_chi_eps_W_orig_plus_extra /= nkp) THEN
223 cpabort("Periodic RI-RS GW requires the complete SCF transfer mesh")
224 END IF
225 IF (any(bs_env%nkp_grid_chi_eps_W_orig /= &
226 bs_env%kpoints_scf_desymm%nkp_grid)) THEN
227 cpabort("Periodic RI-RS GW requires the SCF mesh dimensions for W")
228 END IF
229 nkp_local = SIZE(gwkp_env%metric_coulomb_factor, 3)
230 ! Distributed solver matrices plus equal workspace reserve; owner buffers remain local.
231 ! Select the smallest feasible divisor, independently of the number of q points.
232 CALL mp_mem_avail_per_rank_gb(bs_env%para_env, available_gb)
233 DO i_group_size = 1, bs_env%para_env%num_pe
234 IF (modulo(bs_env%para_env%num_pe, i_group_size) /= 0) cycle
235 required_bytes = real(n_ri, dp)**2*(2.0_dp*complex_bytes + &
236 2.0_dp*complex_bytes*n_w_matrices/real(i_group_size, dp)) + &
237 complex_bytes*real(n_ri, dp)*real(column_block_cols, dp)
238 IF (required_bytes <= stage_memory_share*max(0.0_dp, available_gb)*bytes_per_gb) EXIT
239 END DO
240 IF (i_group_size > bs_env%para_env%num_pe) THEN
241 cpabort('Insufficient node memory for the distributed GW W(q) solver')
242 END IF
243 n_qgroups = bs_env%para_env%num_pe/i_group_size
244 CALL grouped_w_partition(bs_env%para_env, n_qgroups, my_qgroup, q_roots)
245 ! Time reversal gives W(-q) = W(q)*: compute_chi provides χ for the first q of each ±q pair,
246 ! and the owner of -q receives the conjugate block after the last batch.
247 CALL minus_q_partners(bs_env%kpoints_chi_eps_W%xkp(1:3, 1:nkp), q_partner)
248 ALLOCATE (solves_q(nkp))
249 solves_q(:) = [(q_partner(ikp) >= ikp, ikp=1, nkp)]
250 ALLOCATE (group_load(0:n_qgroups - 1), source=0)
251 DO ikp = 1, nkp
252 IF (solves_q(ikp)) group_load(modulo(ikp, n_qgroups)) = group_load(modulo(ikp, n_qgroups)) + 1
253 END DO
254 ALLOCATE (q_batch(maxval(group_load), 0:n_qgroups - 1), source=0)
255 group_load(:) = 0
256 DO ikp = 1, nkp
257 IF (.NOT. solves_q(ikp)) cycle
258 q_group = modulo(ikp, n_qgroups)
259 group_load(q_group) = group_load(q_group) + 1
260 q_batch(group_load(q_group), q_group) = ikp
261 END DO
262 ALLOCATE (gwkp_env%W_PQ_tau(nkp_local))
263 IF (nkp_local > 0 .OR. bs_env%para_env%mepos == q_roots(my_qgroup + 1)) THEN
264 ALLOCATE (chi_k_w(n_ri, n_ri), mwm_k_w(n_ri, n_ri))
265 ELSE
266 ALLOCATE (chi_k_w(0, 0), mwm_k_w(0, 0))
267 END IF
268
269 ALLOCATE (w_group_env)
270 CALL w_group_env%from_split(bs_env%para_env, my_qgroup)
271 CALL grouped_w_create(w_matrices, w_blacs, w_matrix_struct, w_group_env, n_ri)
272 ALLOCATE (frequency_elapsed(bs_env%num_time_freq_points), source=0.0_dp)
273 ! Each group holds one q at a time, reusing K(q) for all frequencies. Group MODULO(q, n_qgroups)
274 ! handles q, so with one rank per group the owner of q solves it without communication.
275 ! Ordered world transfers also cover incomplete batches and idle groups.
276 DO i_batch = 1, SIZE(q_batch, 1)
277 DO q_group = 0, n_qgroups - 1
278 ikp = q_batch(i_batch, q_group)
279 IF (ikp == 0) cycle
280 q_root = q_roots(q_group + 1)
281 q_owner = modulo(ikp, bs_env%para_env%num_pe)
282 ikp_local = 1 + (ikp - 1)/bs_env%para_env%num_pe
283 IF (bs_env%para_env%mepos == q_owner) THEN
284 CALL grouped_w_copy_matrix(bs_env%para_env, n_ri, q_owner, q_root, &
285 gwkp_env%metric_coulomb_factor(:, :, ikp_local), chi_k_w)
286 ALLOCATE (gwkp_env%W_PQ_tau(ikp_local)%values( &
287 n_ri, n_ri, bs_env%num_time_freq_points), source=z_zero)
288 ELSE
289 CALL grouped_w_copy_matrix(bs_env%para_env, n_ri, q_owner, q_root, mwm_k_w, chi_k_w)
290 END IF
291 IF (q_group == my_qgroup) THEN
292 CALL set_owner_matrix(w_matrices(w_metric), chi_k_w, w_group_env, 0, n_ri)
293 END IF
294 END DO
295 DO j_w = 1, bs_env%num_time_freq_points
296 step_start = m_walltime()
297 DO q_group = 0, n_qgroups - 1
298 ikp = q_batch(i_batch, q_group)
299 IF (ikp == 0) cycle
300 q_owner = modulo(ikp, bs_env%para_env%num_pe)
301 ikp_local = 1 + (ikp - 1)/bs_env%para_env%num_pe
302 IF (bs_env%para_env%mepos == q_owner) THEN
303 mwm_k_w(:, :) = gwkp_env%chi_q_frequency(ikp_local)%values(:, :, j_w)
304 ! The RI fit gives occupied-virtual pair densities a spurious charge m_ia that makes
305 ! ε_00 - 1 ~ C(ω)/q and the q sum log-divergent. As in the Γ model (P = 1 - c c^H),
306 ! remove it from the fitted coefficients, d' = d - (c/|c|) m, i.e. Z^H ρ' = Z^H ρ - e m:
307 ! χ' = χ - e X^H - X e^H + L0 e e^H. m and d are taken at q = 0, so the physical
308 ! O(q) dipole charge is kept; the error is O(q).
309 CALL zgerc(n_ri, n_ri, cmplx(gwkp_env%leak_l0(j_w), 0.0_dp, dp), &
310 gwkp_env%leak_e(:, ikp_local), 1, gwkp_env%leak_e(:, ikp_local), 1, &
311 mwm_k_w, n_ri)
312 CALL zgerc(n_ri, n_ri, -z_one, gwkp_env%leak_e(:, ikp_local), 1, &
313 gwkp_env%leak_x(:, j_w, ikp_local), 1, mwm_k_w, n_ri)
314 CALL zgerc(n_ri, n_ri, -z_one, gwkp_env%leak_x(:, j_w, ikp_local), 1, &
315 gwkp_env%leak_e(:, ikp_local), 1, mwm_k_w, n_ri)
316 END IF
317 CALL grouped_w_copy_matrix(bs_env%para_env, n_ri, q_owner, q_roots(q_group + 1), mwm_k_w, chi_k_w)
318 END DO
319 IF (q_batch(i_batch, my_qgroup) > 0) THEN
320 CALL grouped_w_solve(w_matrices, w_group_env, n_ri, 0, chi_k_w, &
321 bs_env%eps_eigval_mat_RI, mwm_k_w)
322 END IF
323 DO q_group = 0, n_qgroups - 1
324 ikp = q_batch(i_batch, q_group)
325 IF (ikp == 0) cycle
326 q_owner = modulo(ikp, bs_env%para_env%num_pe)
327 ikp_local = 1 + (ikp - 1)/bs_env%para_env%num_pe
328 CALL grouped_w_copy_matrix(bs_env%para_env, n_ri, q_roots(q_group + 1), q_owner, mwm_k_w, chi_k_w)
329 IF (bs_env%para_env%mepos == q_owner) THEN
330 gwkp_env%W_PQ_tau(ikp_local)%values(:, :, j_w) = chi_k_w
331 IF (j_w == bs_env%num_time_freq_points) DEALLOCATE (gwkp_env%chi_q_frequency(ikp_local)%values)
332 END IF
333 END DO
334 frequency_elapsed(j_w) = frequency_elapsed(j_w) + m_walltime() - step_start
335 END DO
336 DO q_group = 0, n_qgroups - 1
337 ikp = q_batch(i_batch, q_group)
338 IF (ikp == 0) cycle
339 IF (bs_env%para_env%mepos /= modulo(ikp, bs_env%para_env%num_pe)) cycle
340 CALL cosine_transform_ri_q(gwkp_env%W_PQ_tau(1 + (ikp - 1)/bs_env%para_env%num_pe), &
341 bs_env%time_frequency_grid%cosine_frequency_to_time)
342 END DO
343 END DO
344 CALL receive_minus_q_blocks(bs_env%para_env, q_partner, solves_q, n_ri, &
345 bs_env%num_time_freq_points, gwkp_env%W_PQ_tau)
346 DO j_w = 1, bs_env%num_time_freq_points
347 CALL bs_env%para_env%max(frequency_elapsed(j_w))
348 IF (bs_env%unit_nr > 0) THEN
349 WRITE (bs_env%unit_nr, '(T2,A,I3,A,I0,T56,A,T70,F10.1,A)') &
350 'GW W(iω) point ', j_w, ' / ', bs_env%num_time_freq_points, &
351 'Exec. time:', frequency_elapsed(j_w), ' s'
352 CALL m_flush(bs_env%unit_nr)
353 END IF
354 END DO
355 DEALLOCATE (frequency_elapsed)
356
357 CALL grouped_w_release(w_matrices, w_matrix_struct, w_blacs)
358 CALL mp_para_env_release(w_group_env)
359
360 ! Release frequency-domain matrices after constructing W(q,tau).
361 DEALLOCATE (chi_k_w, mwm_k_w)
362
363 cpassert(gwkp_env%ngroups == 1 .AND. gwkp_env%my_group == 0)
364 cpassert(gwkp_env%group_env%num_pe == bs_env%para_env%num_pe)
365 cpassert(gwkp_env%group_env%mepos == bs_env%para_env%mepos)
366
367 DEALLOCATE (group_load, q_batch, q_partner, solves_q, gwkp_env%leak_e, gwkp_env%leak_x, gwkp_env%leak_l0)
368 IF (ALLOCATED(gwkp_env%chi_q_frequency)) DEALLOCATE (gwkp_env%chi_q_frequency)
369
370 CALL timestop(handle)
371
372 END SUBROUTINE compute_w0
373
374! **************************************************************************************************
375!> \brief Evaluate exchange and prepare band orbitals for correlation.
376!> \param bs_env Band-structure settings and physical results.
377!> \param gwkp_env Temporary periodic GW work data.
378! **************************************************************************************************
379 SUBROUTINE compute_sigma_x(bs_env, gwkp_env)
380 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
381 TYPE(gwkp_type), INTENT(INOUT) :: gwkp_env
382
383 CHARACTER(LEN=*), PARAMETER :: routinen = 'compute_Sigma_x'
384
385 INTEGER :: handle
386
387 CALL timeset(routinen, handle)
388
389 CALL collect_band_bloch_orbitals(bs_env, gwkp_env%band_orbitals)
390 CALL compute_sigma_x_from_v(bs_env, gwkp_env)
391 CALL prepare_self_energy_groups(bs_env, gwkp_env)
392 IF (gwkp_env%ngroups > 1) CALL band_orbitals_to_groups(bs_env, gwkp_env)
393
394 CALL timestop(handle)
395
396 END SUBROUTINE compute_sigma_x
397
398! **************************************************************************************************
399!> \brief Retain Z and fixed W in the smallest memory-feasible time groups.
400!>
401!> Each group owns τ = group + 1, group + 1 + ngroups, ... . Within a group,
402!> Z and W use owner MOD(q,g). W remains in the auxiliary basis. A single q block is broadcast during this
403!> one-time redistribution; each auxiliary W(q,τ) goes only to its owning rank.
404!> \param bs_env Band-structure settings.
405!> \param gwkp_env Work arrays and their MPI distribution.
406! **************************************************************************************************
407 SUBROUTINE prepare_self_energy_groups(bs_env, gwkp_env)
408 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
409 TYPE(gwkp_type), INTENT(INOUT) :: gwkp_env
410
411 CHARACTER(LEN=*), PARAMETER :: routinen = 'prepare_self_energy_groups'
412
413 COMPLEX(KIND=dp), ALLOCATABLE :: z_group(:, :, :)
414 INTEGER :: handle, i_dest, i_group_size, i_root, i_source, i_tag, iq, iq_local, it, &
415 it_local, n_ao, n_aux, n_grid, n_k, n_local_k, n_local_q, n_local_time, n_spin, &
416 n_stored_q, n_threads, n_time, n_world
417 REAL(kind=dp) :: available_gb, block_bytes, budget_bytes, grid_bytes, input_bytes, n_pairs, &
418 peak_bytes, persistent_bytes, result_bytes
419 TYPE(mp_comm_type) :: column_env
420 TYPE(ri_q_matrix_block_type), ALLOCATABLE :: w_group(:)
421
422 CALL timeset(routinen, handle)
423
424 n_threads = 1
425!$ n_threads = omp_get_max_threads()
426 n_world = bs_env%para_env%num_pe
427 n_grid = bs_env%ri_rs%n_grid_points
428 n_ao = bs_env%n_ao
429 n_spin = bs_env%n_spin
430 n_aux = bs_env%n_RI
431 n_k = bs_env%nkp_scf_desymm
432 n_time = bs_env%num_time_freq_points
433 CALL mp_mem_avail_per_rank_gb(bs_env%para_env, available_gb)
434 CALL bs_env%para_env%min(available_gb)
435 input_bytes = complex_bytes*(real(SIZE(gwkp_env%Z_lP_q, kind=int_8), dp) + &
436 REAL(ri_q_storage_elements(gwkp_env%w_pq_tau), dp))
437 CALL bs_env%para_env%min(input_bytes)
438 ! World inputs remain alive during redistribution. Count the complete new
439 ! group storage in addition to measured usage; retain a library reserve.
440 budget_bytes = budget_after_reserve(available_gb)
441 block_bytes = complex_bytes*real(n_grid, dp)*real(n_aux, dp)
442 result_bytes = 2.0_dp*complex_bytes*real(n_ao, dp)*real(SIZE(bs_env%eigenval_scf, 2), dp)* &
443 REAL(n_time*n_spin, dp)
444 DO i_group_size = min(2, max(1, n_world/2)), n_world
445 IF (modulo(n_world, i_group_size) /= 0) cycle
446 gwkp_env%ngroups = n_world/i_group_size
447 IF (gwkp_env%ngroups > n_time) cycle
448 n_local_k = (n_k + i_group_size - 1)/i_group_size
449 n_local_time = (n_time + gwkp_env%ngroups - 1)/gwkp_env%ngroups
450 n_stored_q = (n_k + i_group_size - 1)/i_group_size
451 persistent_bytes = complex_bytes*(real(n_stored_q, dp)*real(n_grid, dp)*real(n_aux, dp) + &
452 REAL(n_aux, dp)**2*REAL(n_stored_q*n_local_time, dp))
453 n_pairs = real(ceiling(real(n_grid, dp)**2/real(i_group_size, dp), kind=int_8), dp)
454 ! G(k), gwkp_env%W_PQ_tau(R), and Σ(R) coexist: 16 + 8 + 8 bytes per pair and mesh point,
455 ! next to the band orbitals of the owned band k points.
456 ! q matrices, band projection, and Green blocks belong to separate stages.
457 grid_bytes = n_pairs*(complex_bytes + 2.0_dp*real_bytes)*real(n_k, dp) + &
458 complex_bytes*real(n_grid, dp)*real(n_ao, dp)*real(n_spin, dp)* &
459 REAL((bs_env%nkp_bs_and_dos + i_group_size - 1)/i_group_size, dp) + &
460 max(complex_bytes*REAL(n_grid, dp)**2*REAL(n_local_k, dp) + &
461 complex_bytes*REAL(n_aux, dp)**2*REAL(n_local_k, dp) + &
462 complex_bytes*REAL(n_threads, dp)*REAL(n_grid, dp)*REAL(n_ao + n_aux, dp), &
463 projection_workspace_bytes(n_grid, i_group_size, &
464 product(bs_env%kpoints_scf_desymm%nkp_grid + 2), n_ao, n_threads)) + &
465 2.0_dp*complex_bytes*REAL(n_threads*n_k, dp) + library_bytes
466 peak_bytes = max(persistent_bytes + block_bytes, &
467 persistent_bytes - input_bytes + grid_bytes + result_bytes)
468 IF (gwkp_env%ngroups == 1) peak_bytes = grid_bytes + result_bytes
469 IF (peak_bytes <= budget_bytes) EXIT
470 END DO
471 IF (i_group_size > n_world) THEN
472 cpabort('Insufficient memory per MPI rank for GW self-energy time groups')
473 END IF
474 gwkp_env%ngroups = n_world/i_group_size
475 gwkp_env%my_group = bs_env%para_env%mepos/i_group_size
476 CALL mp_para_env_release(gwkp_env%group_env)
477 ALLOCATE (gwkp_env%group_env)
478 CALL gwkp_env%group_env%from_split(bs_env%para_env, gwkp_env%my_group)
479 cpassert(gwkp_env%group_env%num_pe == i_group_size)
480 IF (gwkp_env%ngroups == 1) THEN
481 CALL timestop(handle)
482 RETURN
483 END IF
484 n_local_q = count([(modulo(iq, i_group_size) == gwkp_env%group_env%mepos, iq=1, n_k)])
485 n_local_time = (n_time - gwkp_env%my_group + gwkp_env%ngroups - 1)/gwkp_env%ngroups
486 ALLOCATE (w_group(n_local_q))
487 ! Ranks with the same within-group index need the same k/q slices.
488 ! Communicate only along this column, independently of the other columns.
489 CALL column_env%from_split(bs_env%para_env, gwkp_env%group_env%mepos)
490 CALL gather_group_columns(bs_env, gwkp_env, column_env, i_group_size, gwkp_env%group_env%mepos, z_group)
491 iq_local = 0
492 DO iq = modulo(gwkp_env%group_env%mepos - 1, i_group_size) + 1, n_k, i_group_size
493 i_root = modulo(iq, n_world)/i_group_size
494 iq_local = iq_local + 1
495 ALLOCATE (w_group(iq_local)%values(n_aux, n_aux, n_local_time))
496 DO it = 1, n_time
497 i_dest = modulo(it - 1, gwkp_env%ngroups)
498 IF (column_env%mepos == i_root) THEN
499 IF (i_dest == i_root) THEN
500 it_local = (it - 1)/gwkp_env%ngroups + 1
501 w_group(iq_local)%values(:, :, it_local) = gwkp_env%W_PQ_tau((iq - 1)/n_world + 1)%values(:, :, it)
502 ELSE
503 CALL column_env%send(gwkp_env%W_PQ_tau((iq - 1)/n_world + 1)%values(:, :, it), i_dest, 0)
504 END IF
505 ELSE IF (column_env%mepos == i_dest) THEN
506 i_source = i_root
507 i_tag = 0
508 it_local = (it - 1)/gwkp_env%ngroups + 1
509 CALL column_env%recv(w_group(iq_local)%values(:, :, it_local), i_source, i_tag)
510 END IF
511 END DO
512 IF (column_env%mepos == i_root) DEALLOCATE (gwkp_env%W_PQ_tau((iq - 1)/n_world + 1)%values)
513 END DO
514 CALL column_env%free()
515 CALL move_alloc(z_group, gwkp_env%Z_lP_q)
516 CALL move_alloc(w_group, gwkp_env%W_PQ_tau)
517
518 CALL timestop(handle)
519
520 END SUBROUTINE prepare_self_energy_groups
521
522! **************************************************************************************************
523!> \brief Copy the Z(q), and optionally the orbitals ψ(k), needed by one rank of a time group
524!> from their world owners.
525!> World rank r owns k with MOD(k-1,n_world)=r and q with MOD(q,n_world)=r. Group rank i
526!> receives k with MOD(k-1,g)=i and q with MOD(q,g)=i. column_env connects the ranks with
527!> the same group rank; its rank is the time-group index.
528!> \param bs_env Band-structure settings.
529!> \param gwkp_env World-distributed ψ(k) and Z(q).
530!> \param column_env Ranks with the same within-group index.
531!> \param group_size Ranks per time group g.
532!> \param group_rank Within-group index i.
533!> \param Z_group Z(q) of this group rank, indexed (grid, RI, local q).
534!> \param psi_group ψ(k) of this group rank, indexed (grid, AO, local k, spin).
535! **************************************************************************************************
536 SUBROUTINE gather_group_columns(bs_env, gwkp_env, column_env, group_size, group_rank, Z_group, psi_group)
537 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
538 TYPE(gwkp_type), INTENT(IN) :: gwkp_env
539 TYPE(mp_comm_type), INTENT(IN) :: column_env
540 INTEGER, INTENT(IN) :: group_size, group_rank
541 COMPLEX(KIND=dp), ALLOCATABLE, INTENT(OUT) :: z_group(:, :, :)
542 COMPLEX(KIND=dp), ALLOCATABLE, INTENT(OUT), &
543 OPTIONAL :: psi_group(:, :, :, :)
544
545 CHARACTER(LEN=*), PARAMETER :: routinen = 'gather_group_columns'
546
547 COMPLEX(KIND=dp), ALLOCATABLE :: psi_block(:, :, :), z_block(:, :)
548 INTEGER :: handle, i_root, ik, ik_local, iq, &
549 iq_local, n_grid, n_k, n_world
550
551 CALL timeset(routinen, handle)
552
553 n_world = bs_env%para_env%num_pe
554 n_grid = bs_env%ri_rs%n_grid_points
555 n_k = bs_env%nkp_scf_desymm
556 IF (PRESENT(psi_group)) THEN
557 ALLOCATE (psi_group(n_grid, bs_env%n_ao, count([(modulo(ik - 1, group_size) == group_rank, ik=1, n_k)]), &
558 bs_env%n_spin))
559 ALLOCATE (psi_block(n_grid, bs_env%n_ao, bs_env%n_spin))
560 ik_local = 0
561 DO ik = group_rank + 1, n_k, group_size
562 i_root = modulo(ik - 1, n_world)/group_size
563 IF (column_env%mepos == i_root) psi_block(:, :, :) = gwkp_env%psi_lm_k(:, :, (ik - 1)/n_world + 1, :)
564 CALL column_env%bcast(psi_block, i_root)
565 ik_local = ik_local + 1
566 psi_group(:, :, ik_local, :) = psi_block
567 END DO
568 DEALLOCATE (psi_block)
569 END IF
570 ALLOCATE (z_group(n_grid, bs_env%n_RI, count([(modulo(iq, group_size) == group_rank, iq=1, n_k)])))
571 ALLOCATE (z_block(n_grid, bs_env%n_RI))
572 iq_local = 0
573 DO iq = modulo(group_rank - 1, group_size) + 1, n_k, group_size
574 i_root = modulo(iq, n_world)/group_size
575 IF (column_env%mepos == i_root) z_block(:, :) = gwkp_env%Z_lP_q(:, :, (iq - 1)/n_world + 1)
576 CALL column_env%bcast(z_block, i_root)
577 iq_local = iq_local + 1
578 z_group(:, :, iq_local) = z_block
579 END DO
580 DEALLOCATE (z_block)
581
582 CALL timestop(handle)
583
584 END SUBROUTINE gather_group_columns
585
586! **************************************************************************************************
587!> \brief Compute chi_PQ^R(i tau) through the periodic real-space grid representation.
588!> \param bs_env Band-structure settings and physical results.
589!> \param gwkp_env Periodic GW work data.
590! **************************************************************************************************
591 SUBROUTINE compute_chi(bs_env, gwkp_env)
592 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
593 TYPE(gwkp_type), INTENT(INOUT), TARGET :: gwkp_env
594
595 CHARACTER(LEN=*), PARAMETER :: routinen = 'compute_chi'
596
597 COMPLEX(KIND=dp), ALLOCATABLE :: chi_grid_local_q(:, :, :), &
598 chi_grid_q(:, :, :), chi_q(:, :, :), &
599 g_grid_k(:, :, :), work(:, :)
600 COMPLEX(KIND=dp), ALLOCATABLE, TARGET :: psi_time(:, :, :, :), z_group(:, :, :)
601 COMPLEX(KIND=dp), POINTER :: psi_values(:, :, :, :), z_time(:, :, :)
602 INTEGER :: first_pair, handle, i_group, i_needed, i_q_world, i_rank, i_root, i_size, &
603 i_source, i_t, i_tag, i_time_group, i_time_local, iq, iq_local, ispin, n_aux, n_cells, &
604 n_grid, n_groups, n_local, n_needed_local, n_pairs_local, n_q_local, n_threads, &
605 n_time_local, n_world
606 INTEGER, ALLOCATABLE :: full_cells(:, :), needed_local(:), &
607 q_partner(:)
608 LOGICAL, ALLOCATABLE :: needed(:)
609 REAL(kind=dp) :: available_gb, budget, input_bytes, live_bytes, n_pair_max, n_q_max, peak, &
610 q_bytes, rows_bytes, step_start, tau, time_bytes
611 REAL(kind=dp), ALLOCATABLE :: chi_grid_r(:, :, :), g_occ_r(:, :, :), &
612 qpoints(:, :), step_times(:)
613 TYPE(mp_comm_type) :: column_env
614 TYPE(mp_para_env_type), POINTER :: chi_env
615 TYPE(ri_q_matrix_block_type), ALLOCATABLE :: chi_partial(:)
616
617 CALL timeset(routinen, handle)
618
619 cpassert(gwkp_env%ngroups == 1 .AND. gwkp_env%my_group == 0)
620 CALL build_full_cell_grid(bs_env, full_cells)
621 n_grid = bs_env%ri_rs%n_grid_points
622 n_aux = bs_env%n_RI
623 n_world = gwkp_env%group_env%num_pe
624 ALLOCATE (qpoints(3, bs_env%nkp_scf_desymm))
625 qpoints(:, :) = bs_env%kpoints_chi_eps_W%xkp(:, 1:bs_env%nkp_scf_desymm)
626 ! χ(-q) = χ(q)*: compute_W0 solves the first q of every ±q pair and conjugates W for -q.
627 CALL minus_q_partners(qpoints, q_partner)
628 ALLOCATE (needed(bs_env%nkp_scf_desymm))
629 needed(:) = [(q_partner(iq) >= iq, iq=1, bs_env%nkp_scf_desymm)]
630 CALL mp_mem_avail_per_rank_gb(gwkp_env%group_env, available_gb)
631 budget = budget_after_reserve(available_gb)
632 n_cells = SIZE(full_cells, 2)
633 n_threads = 1
634!$ n_threads = omp_get_max_threads()
635 DO i_size = min(2, max(1, n_world/2)), n_world
636 IF (modulo(n_world, i_size) /= 0) cycle
637 n_groups = n_world/i_size
638 IF (n_groups > bs_env%num_time_freq_points) cycle
639 i_rank = modulo(gwkp_env%group_env%mepos, i_size)
640 i_group = gwkp_env%group_env%mepos/i_size
641 n_time_local = (bs_env%num_time_freq_points + n_groups - 1 - i_group)/n_groups
642 n_pair_max = real(int(n_grid, int_8)**2*(i_rank + 1)/i_size - int(n_grid, int_8)**2*i_rank/i_size, dp)
643 n_q_max = real(count([(modulo(iq, i_size) == i_rank .AND. needed(iq), &
644 iq=1, bs_env%nkp_scf_desymm)]), dp)
645 input_bytes = 0.0_dp
646 IF (n_groups > 1) THEN
647 input_bytes = complex_bytes*real(n_grid, dp)*real(n_aux, dp)*n_q_max
648 input_bytes = input_bytes + &
649 complex_bytes*real(n_grid, dp)*real(bs_env%n_ao*bs_env%n_spin, dp)* &
650 REAL((bs_env%nkp_scf_desymm + i_size - 1 - i_rank)/i_size, dp)
651 END IF
652 time_bytes = complex_bytes*real(n_aux, dp)**2*n_q_max*real(n_time_local, dp)
653 ! Keep only this group's time slices, not a full frequency set per group.
654 ! Lattice FFT work holds two mesh vectors per thread, not another grid matrix.
655 rows_bytes = n_pair_max*complex_bytes*real(bs_env%nkp_scf_desymm + n_cells, dp)
656 q_bytes = complex_bytes*real(n_grid, dp)**2*n_q_max
657 ! G(k) and the pair transfers hold two full grid matrices.
658 live_bytes = max(rows_bytes + 2.0_dp*complex_bytes*real(n_grid, dp)**2 + &
659 complex_bytes*real(n_grid, dp)*real(bs_env%n_ao, dp), &
660 complex_bytes*n_pair_max*real(bs_env%nkp_scf_desymm, dp) + q_bytes + &
661 2.0_dp*complex_bytes*real(n_grid, dp)**2, &
662 q_bytes + complex_bytes*real(n_grid, dp)*real(n_aux, dp)* &
663 max(1.0_dp, min(real(n_threads, dp), n_q_max)))
664 peak = input_bytes + time_bytes + complex_bytes*real(n_aux, dp)**2*n_q_max + &
665 max(live_bytes, complex_bytes*real(n_aux, dp)**2*real(bs_env%num_time_freq_points, dp)) + &
666 2.0_dp*complex_bytes*real(n_threads*bs_env%nkp_scf_desymm, dp) + library_bytes
667 CALL gwkp_env%group_env%max(peak)
668 IF (peak <= budget) EXIT
669 END DO
670 IF (i_size > n_world) THEN
671 cpabort("Insufficient memory per MPI rank for GW time groups")
672 END IF
673 n_groups = n_world/i_size
674 i_group = gwkp_env%group_env%mepos/i_size
675 ALLOCATE (chi_env)
676 CALL chi_env%from_split(gwkp_env%group_env, i_group)
677 CALL column_env%from_split(gwkp_env%group_env, chi_env%mepos)
678 IF (n_groups > 1) THEN
679 CALL gather_group_columns(bs_env, gwkp_env, column_env, i_size, chi_env%mepos, z_group, psi_time)
680 psi_values => psi_time
681 z_time => z_group
682 ELSE
683 psi_values => gwkp_env%psi_lm_k
684 z_time => gwkp_env%Z_lP_q
685 END IF
686 n_time_local = (bs_env%num_time_freq_points + n_groups - 1 - i_group)/n_groups
687 CALL block_range(n_grid**2, chi_env%mepos, chi_env%num_pe, first_pair, n_pairs_local)
688 n_q_local = count([(modulo(iq, chi_env%num_pe) == chi_env%mepos, iq=1, bs_env%nkp_scf_desymm)])
689 ALLOCATE (chi_q(n_aux, n_aux, n_q_local), needed_local(n_q_local))
690 ALLOCATE (chi_partial(n_q_local))
691 iq_local = 0
692 n_needed_local = 0
693 DO iq = modulo(chi_env%mepos - 1, i_size) + 1, SIZE(qpoints, 2), i_size
694 iq_local = iq_local + 1
695 IF (.NOT. needed(iq)) cycle
696 n_needed_local = n_needed_local + 1
697 needed_local(n_needed_local) = iq_local
698 ALLOCATE (chi_partial(iq_local)%values(n_aux, n_aux, n_time_local), source=z_zero)
699 END DO
700
701 ALLOCATE (step_times(bs_env%num_time_freq_points), source=0.0_dp)
702 DO i_t = i_group + 1, bs_env%num_time_freq_points, n_groups
703 step_start = m_walltime()
704 tau = bs_env%time_frequency_grid%imaginary_time(i_t)
705 chi_q = z_zero
706 DO ispin = 1, bs_env%n_spin
707 ALLOCATE (g_grid_k(n_pairs_local, 1, bs_env%nkp_scf_desymm))
708 ALLOCATE (g_occ_r(n_pairs_local, 1, n_cells))
709 ALLOCATE (chi_grid_r, mold=g_occ_r)
710 CALL build_green_pairs(bs_env, psi_values(:, :, :, ispin), chi_env, tau, ispin, .true., g_grid_k)
711 CALL gw_k_grid_to_real_cells(g_grid_k, bs_env%kpoints_scf_desymm%xkp, &
712 bs_env%kpoints_scf_desymm%nkp_grid, full_cells, &
713 g_occ_r)
714 CALL build_green_pairs(bs_env, psi_values(:, :, :, ispin), chi_env, tau, ispin, .false., g_grid_k)
715 CALL gw_k_grid_to_real_cells(g_grid_k, bs_env%kpoints_scf_desymm%xkp, &
716 bs_env%kpoints_scf_desymm%nkp_grid, full_cells, &
717 chi_grid_r)
718 CALL filter_full_green_cells(bs_env, chi_env, g_occ_r)
719 CALL filter_full_green_cells(bs_env, chi_env, chi_grid_r)
720 DEALLOCATE (g_grid_k)
721 chi_grid_r(:, :, :) = bs_env%spin_degeneracy*g_occ_r*chi_grid_r
722 DEALLOCATE (g_occ_r)
723 ALLOCATE (chi_grid_q(n_pairs_local, 1, bs_env%nkp_scf_desymm))
724 CALL product_to_k_pair_centered(bs_env, full_cells, qpoints, chi_grid_r, &
725 chi_grid_q, first_pair)
726 DEALLOCATE (chi_grid_r)
727
728 ALLOCATE (chi_grid_local_q(n_grid, n_grid, n_needed_local))
729 CALL fft_pairs_to_q_matrices(chi_grid_q, chi_env, needed, chi_grid_local_q)
730 DEALLOCATE (chi_grid_q)
731 ! χ_PQ(q) = Σ_ll′ Z_lP(q)* χ_ll′(q) Z_l′Q(q).
732 ! Parallelize distinct q matrices; leave singleton GEMM threading to BLAS.
733 !$OMP PARALLEL DEFAULT(NONE) IF(n_needed_local > 1) &
734 !$OMP SHARED(n_grid, n_aux, n_needed_local, needed_local, chi_grid_local_q, Z_time, chi_q) &
735 !$OMP PRIVATE(work, i_needed, iq_local)
736 ALLOCATE (work(n_grid, n_aux))
737 !$OMP DO SCHEDULE(STATIC)
738 DO i_needed = 1, n_needed_local
739 iq_local = needed_local(i_needed)
740 CALL zgemm('N', 'N', n_grid, n_aux, n_grid, z_one, &
741 chi_grid_local_q(:, :, i_needed), n_grid, &
742 z_time(:, :, iq_local), n_grid, z_zero, work, n_grid)
743 CALL zgemm('C', 'N', n_aux, n_aux, n_grid, z_one, &
744 z_time(:, :, iq_local), n_grid, work, n_grid, z_one, &
745 chi_q(:, :, iq_local), n_aux)
746 END DO
747 !$OMP END DO
748 DEALLOCATE (work)
749 !$OMP END PARALLEL
750 DEALLOCATE (chi_grid_local_q)
751 END DO
752 i_time_local = (i_t - 1)/n_groups + 1
753 DO i_needed = 1, n_needed_local
754 iq_local = needed_local(i_needed)
755 chi_partial(iq_local)%values(:, :, i_time_local) = chi_q(:, :, iq_local)
756 END DO
757 IF (chi_env%mepos == 0) step_times(i_t) = m_walltime() - step_start
758 END DO
759
760 CALL bs_env%para_env%sum(step_times)
761 IF (bs_env%unit_nr > 0) THEN
762 WRITE (bs_env%unit_nr, '(A)') ' '
763 DO i_t = 1, bs_env%num_time_freq_points
764 WRITE (bs_env%unit_nr, '(T2,A,I3,A,I0,T57,A,T71,F10.1,A)') &
765 'GW χ(iτ) point ', i_t, ' / ', bs_env%num_time_freq_points, &
766 'Exec. time:', step_times(i_t), ' s'
767 END DO
768 CALL m_flush(bs_env%unit_nr)
769 END IF
770 DEALLOCATE (step_times)
771
772 IF (ALLOCATED(psi_time)) DEALLOCATE (psi_time)
773 n_local = count([(modulo(iq, n_world) == gwkp_env%group_env%mepos, iq=1, bs_env%nkp_scf_desymm)])
774 ALLOCATE (gwkp_env%chi_q_frequency(n_local))
775 iq_local = 0
776 DO iq = modulo(chi_env%mepos - 1, i_size) + 1, bs_env%nkp_scf_desymm, i_size
777 iq_local = iq_local + 1
778 IF (.NOT. needed(iq)) cycle
779 i_root = modulo(iq, n_world)/i_size
780 i_q_world = (iq - 1)/n_world + 1
781 IF (column_env%mepos == i_root) THEN
782 ALLOCATE (gwkp_env%chi_q_frequency(i_q_world)%values(n_aux, n_aux, &
783 bs_env%num_time_freq_points))
784 END IF
785 ! Send each computed time slice once, point to point, to the world q owner, which alone
786 ! forms the complete frequency result; slices and frequency sets are never replicated.
787 DO i_t = 1, bs_env%num_time_freq_points
788 i_time_group = modulo(i_t - 1, n_groups)
789 i_time_local = (i_t - 1)/n_groups + 1
790 IF (column_env%mepos == i_root) THEN
791 IF (i_time_group == i_root) THEN
792 gwkp_env%chi_q_frequency(i_q_world)%values(:, :, i_t) = &
793 chi_partial(iq_local)%values(:, :, i_time_local)
794 ELSE
795 i_source = i_time_group
796 i_tag = 0
797 CALL column_env%recv(gwkp_env%chi_q_frequency(i_q_world)%values(:, :, i_t), i_source, i_tag)
798 END IF
799 ELSE IF (column_env%mepos == i_time_group) THEN
800 CALL column_env%send(chi_partial(iq_local)%values(:, :, i_time_local), i_root, 0)
801 END IF
802 END DO
803 IF (column_env%mepos == i_root) THEN
804 CALL cosine_transform_ri_q(gwkp_env%chi_q_frequency(i_q_world), &
805 bs_env%time_frequency_grid%cosine_time_to_frequency)
806 END IF
807 DEALLOCATE (chi_partial(iq_local)%values)
808 END DO
809 DEALLOCATE (chi_partial)
810 NULLIFY (z_time)
811 IF (ALLOCATED(z_group)) DEALLOCATE (z_group)
812 CALL column_env%free()
813 CALL mp_para_env_release(chi_env)
814 DEALLOCATE (chi_q, full_cells, needed, needed_local, q_partner, qpoints)
815
816 CALL timestop(handle)
817
818 END SUBROUTINE compute_chi
819
820! **************************************************************************************************
821!> \brief Build K(q) = M(q)^-1 V(q)^1/2 from the Gaussian RI metric M and the Ewald Coulomb matrix V,
822!> and the charge-neutralisation vectors e(q) = M(q) c/|c| and X(q,iω) = M(q) M(0)^-1 X0(iω).
823!> The Gamma-cell head is corrected separately from these operators.
824!> \param bs_env ...
825!> \param qs_env ...
826!> \param gwkp_env K(q) and the neutralisation vectors for the q points owned by this rank
827! **************************************************************************************************
828 SUBROUTINE compute_metric_coulomb_factor(bs_env, qs_env, gwkp_env)
829 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
830 TYPE(qs_environment_type), POINTER :: qs_env
831 TYPE(gwkp_type), INTENT(INOUT) :: gwkp_env
832
833 CHARACTER(LEN=*), PARAMETER :: routinen = 'compute_metric_coulomb_factor'
834
835 COMPLEX(KIND=dp), ALLOCATABLE :: charge(:), charge_hat(:), m_inv(:, :), m_ref(:, :, :), &
836 m_zero(:, :), u_pp(:, :), v_ref(:, :, :), v_sqrt(:, :), x0(:, :), x_coef(:, :)
837 INTEGER :: handle, ikp, ilocal, n_ref, n_ri, nkp, &
838 nkp_local
839 INTEGER, ALLOCATABLE :: metric_cells(:, :)
840 TYPE(cp_fm_type), ALLOCATABLE :: fm_metric(:)
841
842 CALL timeset(routinen, handle)
843
844 n_ri = bs_env%n_RI
845 n_ref = n_ri
846 IF (bs_env%auto_ri%enabled) n_ref = sum(bs_env%auto_ri%sizes_ref_RI)
847 nkp = bs_env%nkp_scf_desymm
848 cpassert(bs_env%nkp_chi_eps_W_orig == nkp)
849 nkp_local = count([(modulo(ikp, bs_env%para_env%num_pe) == &
850 bs_env%para_env%mepos, ikp=1, nkp)])
851 ALLOCATE (m_ref(n_ref, n_ref, nkp_local), v_ref(n_ref, n_ref, nkp_local), source=z_zero)
852 ALLOCATE (gwkp_env%metric_coulomb_factor(n_ri, n_ri, nkp_local), m_inv(n_ri, n_ri), v_sqrt(n_ri, n_ri))
853 CALL build_ri_2c_lattice(fm_metric, metric_cells, bs_env%ri_metric, &
854 0.0_dp, bs_env, qs_env, reference_basis=bs_env%auto_ri%enabled)
855 CALL ri_2c_lattice_to_owned_q(fm_metric, metric_cells, &
856 bs_env%kpoints_chi_eps_W%xkp(1:3, 1:nkp), &
857 bs_env%kpoints_chi_eps_W%nkp_grid, m_ref)
858 ! The erfc(omega r)/r metric is the real-space Ewald part of the Coulomb matrix, which is
859 ! finite on the shifted 2D/3D q mesh. No G=0 auxiliary channel is removed at a transfer point.
860 v_ref(:, :, :) = m_ref
861 CALL add_ewald_reciprocal(v_ref, qs_env, bs_env%kpoints_chi_eps_W, 'RI_AUX', &
862 bs_env%ri_metric%omega, 1, nkp)
863 ALLOCATE (m_zero(n_ref, n_ref), charge(n_ref), charge_hat(n_ri))
864 CALL ri_2c_lattice_to_kp(fm_metric, metric_cells, [0.0_dp, 0.0_dp, 0.0_dp], m_zero)
865 CALL gamma_auxiliary_charges(qs_env, charge)
866 CALL cp_fm_release(fm_metric)
867 DEALLOCATE (metric_cells)
868 IF (bs_env%auto_ri%enabled) CALL assemble_aa_contraction(bs_env, u_pp)
869 IF (bs_env%auto_ri%enabled) THEN
870 m_inv(:, :) = matmul(conjg(transpose(u_pp)), matmul(m_zero, u_pp))
871 charge_hat(:) = matmul(conjg(transpose(u_pp)), charge)
872 ELSE
873 m_inv(:, :) = m_zero
874 charge_hat(:) = charge
875 END IF
876 DEALLOCATE (m_zero, charge)
877 charge_hat(:) = charge_hat/sqrt(real(dot_product(charge_hat, charge_hat), dp))
878 CALL local_complex_power(m_inv, -1.0_dp, 0.0_dp, relative_eps=bs_env%regularization_RI)
879 ! The pair densities enter χ as Z^H ρ = M d. Neutralise the fitted coefficients d, which are
880 ! continuous in q: X0 = Σ (Z(0)^H ρ) m* becomes M(0)^-1 X0 = Σ d m*, and M(q) M(0)^-1 X0 at q.
881 ! Carrying X0 itself to q would let M(q)^-1 amplify its O(q) mismatch in ill-conditioned metrics.
882 CALL compute_charge_leak_q0(bs_env, gwkp_env, matmul(m_inv, charge_hat), x0)
883 ALLOCATE (x_coef(n_ri, SIZE(x0, 2)))
884 x_coef(:, :) = matmul(m_inv, x0)
885 DEALLOCATE (x0)
886 ALLOCATE (gwkp_env%leak_e(n_ri, nkp_local), gwkp_env%leak_x(n_ri, SIZE(x_coef, 2), nkp_local))
887 DO ilocal = 1, nkp_local
888 IF (bs_env%auto_ri%enabled) THEN
889 m_inv(:, :) = matmul(conjg(transpose(u_pp)), matmul(m_ref(:, :, ilocal), u_pp))
890 v_sqrt(:, :) = matmul(conjg(transpose(u_pp)), matmul(v_ref(:, :, ilocal), u_pp))
891 ELSE
892 m_inv(:, :) = m_ref(:, :, ilocal)
893 v_sqrt(:, :) = v_ref(:, :, ilocal)
894 END IF
895 gwkp_env%leak_e(:, ilocal) = matmul(m_inv, charge_hat)
896 gwkp_env%leak_x(:, :, ilocal) = matmul(m_inv, x_coef)
897 CALL local_complex_power(m_inv, -1.0_dp, 0.0_dp, relative_eps=bs_env%regularization_RI)
898 CALL local_complex_power(v_sqrt, 0.5_dp, 0.0_dp)
899 gwkp_env%metric_coulomb_factor(:, :, ilocal) = matmul(m_inv, v_sqrt)
900 END DO
901 DEALLOCATE (m_ref, v_ref, m_inv, v_sqrt, charge_hat, x_coef)
902 IF (ALLOCATED(u_pp)) DEALLOCATE (u_pp)
903
904 CALL timestop(handle)
905
906 END SUBROUTINE compute_metric_coulomb_factor
907
908! **************************************************************************************************
909!> \brief Spurious charge of the fitted q = 0 occupied-virtual pair densities.
910!> The RI fit gives ρ_ia = ψ_ik* ψ_ak (exactly neutral) the charge m_ia(k) = Σ_l g_l* ψ_ik(r_l) ψ_ak(r_l)*
911!> with g = Z(0) v0. With the time dependence and cosine transform of compute_chi,
912!> L0(iω) = (s/N_k) Σ_k,ia |m_ia|² w_ia(iω) and X0(iω) = Z(0)^H T(iω), T_l = (s/N_k) Σ_k,ia ψ_i ψ_a* m_ia* w_ia.
913!> \param bs_env ...
914!> \param gwkp_env ...
915!> \param v0 M(0)^-1 c/|c|
916!> \param X0 X(iω) in the projection space Z(0)^H ρ
917! **************************************************************************************************
918 SUBROUTINE compute_charge_leak_q0(bs_env, gwkp_env, v0, X0)
919 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
920 TYPE(gwkp_type), INTENT(INOUT) :: gwkp_env
921 COMPLEX(KIND=dp), INTENT(IN) :: v0(:)
922 COMPLEX(KIND=dp), ALLOCATABLE, INTENT(OUT) :: x0(:, :)
923
924 CHARACTER(LEN=*), PARAMETER :: routinen = 'compute_charge_leak_q0'
925
926 COMPLEX(KIND=dp), ALLOCATABLE :: cross(:, :), g0(:), m(:, :), psi(:, :), &
927 t_tau(:, :), work(:, :), x_tau(:, :), &
928 y(:, :)
929 INTEGER :: a, handle, i, ikp, ispin, j_w, n_ao, &
930 n_grid, n_occ, n_t, n_vir, nkp, &
931 num_pe, t
932 REAL(kind=dp) :: prefactor, x
933 REAL(kind=dp), ALLOCATABLE :: l_tau(:)
934
935 CALL timeset(routinen, handle)
936
937 n_grid = bs_env%ri_rs%n_grid_points
938 n_ao = SIZE(gwkp_env%psi_lm_k, 2)
939 nkp = bs_env%nkp_scf_desymm
940 n_t = bs_env%num_time_freq_points
941 num_pe = bs_env%para_env%num_pe
942 ALLOCATE (g0(n_grid), psi(n_grid, n_ao), work(n_grid, n_ao), t_tau(n_grid, n_t), l_tau(n_t))
943 g0(:) = matmul(gwkp_env%Z_lP_zero(:, 2:, 1), v0)
944 t_tau(:, :) = z_zero
945 l_tau(:) = 0.0_dp
946 DO ispin = 1, bs_env%n_spin
947 n_occ = bs_env%n_occ(ispin)
948 n_vir = n_ao - n_occ
949 IF (n_occ == 0 .OR. n_vir == 0) cycle
950 ALLOCATE (m(n_occ, n_vir), cross(n_occ, n_vir), y(n_grid, n_occ))
951 DO ikp = 1, nkp
952 IF (modulo(ikp - 1, num_pe) /= bs_env%para_env%mepos) cycle
953 psi(:, :) = gwkp_env%psi_lm_k(:, :, (ikp - 1)/num_pe + 1, ispin)
954 DO i = 1, n_occ
955 work(:, i) = conjg(g0)*psi(:, i)
956 END DO
957 m(:, :) = matmul(transpose(work(:, 1:n_occ)), conjg(psi(:, n_occ + 1:n_ao)))
958 DO t = 1, n_t
959 cross(:, :) = z_zero
960 DO a = 1, n_vir
961 DO i = 1, n_occ
962 x = bs_env%time_frequency_grid%imaginary_time(t)* &
963 (gwkp_env%eigenvalues_G(n_occ + a, ikp, ispin) - gwkp_env%eigenvalues_G(i, ikp, ispin))
964 IF (x < 700.0_dp) THEN
965 cross(i, a) = conjg(m(i, a))*exp(-x)
966 l_tau(t) = l_tau(t) + abs(m(i, a))**2*exp(-x)
967 END IF
968 END DO
969 END DO
970 y(:, :) = matmul(conjg(psi(:, n_occ + 1:n_ao)), transpose(cross))
971 t_tau(:, t) = t_tau(:, t) + sum(psi(:, 1:n_occ)*y, dim=2)
972 END DO
973 END DO
974 DEALLOCATE (m, cross, y)
975 END DO
976 CALL bs_env%para_env%sum(t_tau)
977 CALL bs_env%para_env%sum(l_tau)
978 prefactor = bs_env%spin_degeneracy/real(nkp, dp)
979 ALLOCATE (x_tau(bs_env%n_RI, n_t))
980 x_tau(:, :) = prefactor*matmul(conjg(transpose(gwkp_env%Z_lP_zero(:, 2:, 1))), t_tau)
981 ALLOCATE (x0(bs_env%n_RI, n_t), source=z_zero)
982 ALLOCATE (gwkp_env%leak_l0(n_t), source=0.0_dp)
983 DO t = 1, n_t
984 DO j_w = 1, n_t
985 x0(:, j_w) = x0(:, j_w) + bs_env%time_frequency_grid%cosine_time_to_frequency(j_w, t)*x_tau(:, t)
986 gwkp_env%leak_l0(j_w) = gwkp_env%leak_l0(j_w) + &
987 bs_env%time_frequency_grid%cosine_time_to_frequency(j_w, t)*prefactor*l_tau(t)
988 END DO
989 END DO
990 DEALLOCATE (g0, psi, work, l_tau, t_tau, x_tau)
991
992 CALL timestop(handle)
993
994 END SUBROUTINE compute_charge_leak_q0
995
996! **************************************************************************************************
997!> \brief Contract the occupied density with the bare RI Coulomb operator.
998!> \param bs_env Band-structure settings and physical results.
999!> \param gwkp_env Periodic GW work data.
1000! **************************************************************************************************
1001 SUBROUTINE compute_sigma_x_from_v(bs_env, gwkp_env)
1002 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
1003 TYPE(gwkp_type), INTENT(INOUT), TARGET :: gwkp_env
1004
1005 CHARACTER(LEN=*), PARAMETER :: routinen = 'compute_Sigma_x_from_V'
1006
1007 COMPLEX(KIND=dp), ALLOCATABLE :: g_grid_k(:, :, :), head_cell_factor(:), &
1008 q_matrices(:, :, :), zk(:, :)
1009 INTEGER :: first_pair, handle, iq, iq_local, ispin, &
1010 n_grid, n_pairs_local, n_q_local, n_ri
1011 INTEGER, ALLOCATABLE :: full_cells(:, :)
1012 REAL(kind=dp) :: head_metric_zero
1013 REAL(kind=dp), ALLOCATABLE :: qpoints(:, :), sigma_grid_r(:, :, :), &
1014 v_grid_r(:, :, :)
1015
1016 CALL timeset(routinen, handle)
1017
1018 ALLOCATE (gwkp_env%sigma_x_bands(bs_env%n_ao, bs_env%nkp_bs_and_DOS, bs_env%n_spin))
1019 n_grid = bs_env%ri_rs%n_grid_points
1020 ! Same grid-pair distribution as Σc with q owners MOD(q,P).
1021 CALL block_range(n_grid**2, bs_env%para_env%mepos, bs_env%para_env%num_pe, first_pair, n_pairs_local)
1022 n_q_local = SIZE(gwkp_env%Z_lP_q, 3)
1023 IF (SIZE(gwkp_env%Z_lP_zero, 2) /= bs_env%n_RI + 1 .OR. SIZE(gwkp_env%Z_lP_zero, 3) /= 1) THEN
1024 cpabort("Periodic RI-RS GW requires the direct Gamma auxiliary fit")
1025 END IF
1026 ALLOCATE (qpoints(3, bs_env%nkp_scf_desymm))
1027 qpoints(:, :) = bs_env%kpoints_chi_eps_W%xkp(:, 1:bs_env%nkp_scf_desymm)
1028 cpassert(SIZE(gwkp_env%Z_lP_q, 2) == bs_env%n_RI)
1029 cpassert(ALLOCATED(gwkp_env%metric_coulomb_factor))
1030 n_ri = bs_env%n_RI
1031 ALLOCATE (q_matrices(n_grid, n_grid, n_q_local))
1032 ! V_ll'(q) = Z(q) K(q) K(q)† Z(q)† = (ZK)(ZK)† with the metric-Coulomb factor K.
1033 !$OMP PARALLEL DEFAULT(NONE) SHARED(gwkp_env, q_matrices, n_q_local, n_grid, n_RI) &
1034 !$OMP PRIVATE(iq_local, ZK)
1035 ALLOCATE (zk(n_grid, n_ri))
1036 !$OMP DO SCHEDULE(STATIC)
1037 DO iq_local = 1, n_q_local
1038 CALL zgemm('N', 'N', n_grid, n_ri, n_ri, z_one, gwkp_env%Z_lP_q(:, :, iq_local), n_grid, &
1039 gwkp_env%metric_coulomb_factor(:, :, iq_local), n_ri, z_zero, zk, n_grid)
1040 CALL zgemm('N', 'C', n_grid, n_grid, n_ri, z_one, zk, n_grid, zk, n_grid, &
1041 z_zero, q_matrices(:, :, iq_local), n_grid)
1042 END DO
1043 !$OMP END DO
1044 DEALLOCATE (zk)
1045 !$OMP END PARALLEL
1046 DEALLOCATE (gwkp_env%metric_coulomb_factor)
1047 ALLOCATE (g_grid_k(n_pairs_local, 1, bs_env%nkp_scf_desymm))
1048 CALL q_matrices_to_fft_pairs(q_matrices, bs_env%para_env, g_grid_k)
1049 DEALLOCATE (q_matrices)
1050 CALL build_full_cell_grid(bs_env, full_cells)
1051 ALLOCATE (v_grid_r(n_pairs_local, 1, SIZE(full_cells, 2)))
1052 CALL gw_k_grid_to_real_cells(g_grid_k, qpoints, bs_env%kpoints_scf_desymm%nkp_grid, &
1053 full_cells, v_grid_r)
1054 CALL singular_head_cell_factors(bs_env, full_cells, head_cell_factor)
1055 head_metric_zero = ri_metric_head_zero(bs_env)
1056 DO iq = 1, SIZE(full_cells, 2)
1057 IF (all(bs_env%periodic == [1, 1, 0])) THEN
1058 CALL add_gamma_corr_pair_cell(bs_env, full_cells, iq, &
1059 twopi/head_metric_zero**2*head_cell_factor(iq), &
1060 gwkp_env%Z_lP_zero(:, 1, 1), first_pair, v_grid_r)
1061 ELSE
1062 CALL add_gamma_corr_pair_cell(bs_env, full_cells, iq, &
1063 fourpi/head_metric_zero**2*head_cell_factor(iq), &
1064 gwkp_env%Z_lP_zero(:, 1, 1), first_pair, v_grid_r)
1065 END IF
1066 END DO
1067 DEALLOCATE (head_cell_factor)
1068
1069 ALLOCATE (sigma_grid_r, mold=v_grid_r)
1070 DO ispin = 1, bs_env%n_spin
1071 CALL build_green_pairs(bs_env, gwkp_env%band_orbitals(:, :, :, ispin), bs_env%para_env, &
1072 0.0_dp, ispin, .true., g_grid_k)
1073 CALL gw_k_grid_to_real_cells(g_grid_k, bs_env%kpoints_scf_desymm%xkp, &
1074 bs_env%kpoints_scf_desymm%nkp_grid, full_cells, sigma_grid_r)
1075 sigma_grid_r(:, :, :) = -sigma_grid_r*v_grid_r
1076 CALL self_energy_on_mesh(bs_env, full_cells, sigma_grid_r, first_pair, g_grid_k)
1077
1078 CALL self_energy_pairs_to_bloch(bs_env, g_grid_k, sigma_grid_r, first_pair, bs_env%para_env, &
1079 gwkp_env%band_orbitals(:, :, :, ispin), &
1080 gwkp_env%sigma_x_bands(:, :, ispin))
1081 END DO
1082
1083 DEALLOCATE (full_cells, g_grid_k, qpoints, sigma_grid_r, v_grid_r)
1084
1085 CALL timestop(handle)
1086
1087 END SUBROUTINE compute_sigma_x_from_v
1088
1089! **************************************************************************************************
1090!> \brief Form Σc with one owner per ordered (l,l') pair through both lattice FFTs.
1091!> \param bs_env Band-structure settings and physical results.
1092!> \param gwkp_env Periodic GW work data.
1093! **************************************************************************************************
1094 SUBROUTINE compute_sigma_c_from_w_pq(bs_env, gwkp_env)
1095 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
1096 TYPE(gwkp_type), INTENT(INOUT), TARGET :: gwkp_env
1097
1098 CHARACTER(LEN=*), PARAMETER :: routinen = 'compute_Sigma_c_from_W_PQ'
1099
1100 COMPLEX(KIND=dp), ALLOCATABLE :: g_grid_k(:, :, :)
1101 INTEGER :: first_pair, handle, i_group, i_t, ispin, &
1102 n_grid, n_pairs_local
1103 INTEGER, ALLOCATABLE :: full_cells(:, :)
1104 REAL(kind=dp) :: step_elapsed, step_start, tau
1105 REAL(kind=dp), ALLOCATABLE :: qpoints(:, :), sigma_band(:, :), sigma_grid_r(:, :, :), &
1106 sigma_result_buffer(:), step_times(:), w_grid_r(:, :, :)
1107
1108 CALL timeset(routinen, handle)
1109
1110 cpassert(ALLOCATED(gwkp_env%band_orbitals))
1111 IF (ALLOCATED(gwkp_env%sigma_c_bands)) DEALLOCATE (gwkp_env%sigma_c_bands)
1112 CALL build_full_cell_grid(bs_env, full_cells)
1113 n_grid = bs_env%ri_rs%n_grid_points
1114 CALL block_range(n_grid**2, gwkp_env%group_env%mepos, gwkp_env%group_env%num_pe, &
1115 first_pair, n_pairs_local)
1116 ALLOCATE (g_grid_k(n_pairs_local, 1, bs_env%nkp_scf_desymm))
1117 ALLOCATE (w_grid_r(n_pairs_local, 1, SIZE(full_cells, 2)))
1118 ALLOCATE (sigma_grid_r, mold=w_grid_r)
1119 ALLOCATE (qpoints(3, bs_env%nkp_scf_desymm))
1120 qpoints(:, :) = bs_env%kpoints_chi_eps_W%xkp(:, 1:bs_env%nkp_scf_desymm)
1121 ALLOCATE (sigma_band(bs_env%n_ao, bs_env%nkp_bs_and_DOS))
1122 ALLOCATE (gwkp_env%sigma_c_bands(bs_env%n_ao, bs_env%nkp_bs_and_DOS, &
1123 bs_env%num_time_freq_points, 2, bs_env%n_spin))
1124 ALLOCATE (step_times(bs_env%num_time_freq_points), source=0.0_dp)
1125 gwkp_env%sigma_c_bands = 0.0_dp
1126
1127 DO i_t = gwkp_env%my_group + 1, bs_env%num_time_freq_points, gwkp_env%ngroups
1128 step_start = m_walltime()
1129 i_group = (i_t - gwkp_env%my_group - 1)/gwkp_env%ngroups + 1
1130 CALL project_auxiliary_time_q_to_fft_pairs(gwkp_env%Z_lP_q, &
1131 gwkp_env%W_PQ_tau, i_group, &
1132 gwkp_env%group_env, g_grid_k)
1133
1134 CALL gw_k_grid_to_real_cells(g_grid_k, qpoints, &
1135 bs_env%kpoints_scf_desymm%nkp_grid, full_cells, &
1136 w_grid_r)
1137 CALL add_gamma_corr_cell_correction(bs_env, full_cells, gwkp_env%gamma_correction, i_t, &
1138 gwkp_env%Z_lP_zero(:, 1, 1), first_pair, w_grid_r)
1139 CALL truncate_grid_w_by_pair_distance_pairs(bs_env, full_cells, first_pair, w_grid_r)
1140 tau = bs_env%time_frequency_grid%imaginary_time(i_t)
1141 DO ispin = 1, bs_env%n_spin
1142 CALL build_green_pairs(bs_env, gwkp_env%band_orbitals(:, :, :, ispin), gwkp_env%group_env, &
1143 tau, ispin, .true., g_grid_k)
1144 CALL gw_k_grid_to_real_cells(g_grid_k, bs_env%kpoints_scf_desymm%xkp, &
1145 bs_env%kpoints_scf_desymm%nkp_grid, full_cells, sigma_grid_r)
1146 CALL filter_full_green_cells(bs_env, gwkp_env%group_env, sigma_grid_r)
1147 sigma_grid_r(:, :, :) = -sigma_grid_r*w_grid_r
1148 CALL self_energy_on_mesh(bs_env, full_cells, sigma_grid_r, first_pair, g_grid_k)
1149 CALL self_energy_pairs_to_bloch(bs_env, g_grid_k, sigma_grid_r, first_pair, gwkp_env%group_env, &
1150 gwkp_env%band_orbitals(:, :, :, ispin), sigma_band)
1151 IF (gwkp_env%group_env%mepos == 0) gwkp_env%sigma_c_bands(:, :, i_t, 2, ispin) = sigma_band
1152
1153 CALL build_green_pairs(bs_env, gwkp_env%band_orbitals(:, :, :, ispin), gwkp_env%group_env, &
1154 tau, ispin, .false., g_grid_k)
1155 CALL gw_k_grid_to_real_cells(g_grid_k, bs_env%kpoints_scf_desymm%xkp, &
1156 bs_env%kpoints_scf_desymm%nkp_grid, full_cells, sigma_grid_r)
1157 CALL filter_full_green_cells(bs_env, gwkp_env%group_env, sigma_grid_r)
1158 sigma_grid_r(:, :, :) = sigma_grid_r*w_grid_r
1159 CALL self_energy_on_mesh(bs_env, full_cells, sigma_grid_r, first_pair, g_grid_k)
1160 CALL self_energy_pairs_to_bloch(bs_env, g_grid_k, sigma_grid_r, first_pair, gwkp_env%group_env, &
1161 gwkp_env%band_orbitals(:, :, :, ispin), sigma_band)
1162 IF (gwkp_env%group_env%mepos == 0) gwkp_env%sigma_c_bands(:, :, i_t, 1, ispin) = sigma_band
1163 END DO
1164 step_elapsed = m_walltime() - step_start
1165 IF (gwkp_env%group_env%mepos == 0) step_times(i_t) = step_elapsed
1166 END DO
1167
1168 CALL bs_env%para_env%sum(step_times)
1169 IF (bs_env%unit_nr > 0) THEN
1170 DO i_t = 1, bs_env%num_time_freq_points
1171 WRITE (bs_env%unit_nr, '(T2,A,I3,A,I0,T57,A,T71,F10.1,A)') &
1172 'GW Σ_c(iτ) point ', i_t, ' / ', bs_env%num_time_freq_points, &
1173 'Exec. time:', step_times(i_t), ' s'
1174 END DO
1175 CALL m_flush(bs_env%unit_nr)
1176 END IF
1177
1178 ALLOCATE (sigma_result_buffer(SIZE(gwkp_env%sigma_c_bands)))
1179 sigma_result_buffer(:) = reshape(gwkp_env%sigma_c_bands, [SIZE(gwkp_env%sigma_c_bands)])
1180 CALL bs_env%para_env%sum(sigma_result_buffer)
1181 gwkp_env%sigma_c_bands(:, :, :, :, :) = reshape(sigma_result_buffer, shape(gwkp_env%sigma_c_bands))
1182
1183 DEALLOCATE (full_cells, g_grid_k, qpoints, &
1184 sigma_band, sigma_result_buffer, sigma_grid_r, &
1185 step_times, w_grid_r)
1186
1187 CALL timestop(handle)
1188
1189 END SUBROUTINE compute_sigma_c_from_w_pq
1190
1191! **************************************************************************************************
1192!> \brief Drop an entire real-space Green matrix only when its full Frobenius norm,
1193!> divided by the number of atom pairs, is below EPS_FILTER.
1194!> \param bs_env ...
1195!> \param para_env ...
1196!> \param G_R ...
1197! **************************************************************************************************
1198 SUBROUTINE filter_full_green_cells(bs_env, para_env, G_R)
1199 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
1200 TYPE(mp_para_env_type), POINTER :: para_env
1201 REAL(kind=dp), INTENT(INOUT) :: g_r(:, :, :)
1202
1203 INTEGER :: i_cell, n_cells
1204 REAL(kind=dp) :: atom_block_count
1205 REAL(kind=dp), ALLOCATABLE :: norm_squared(:)
1206
1207 cpassert(bs_env%n_atom > 0)
1208 n_cells = SIZE(g_r, 3)
1209 atom_block_count = real(bs_env%n_atom, dp)**2
1210 ALLOCATE (norm_squared(n_cells))
1211 DO i_cell = 1, n_cells
1212 norm_squared(i_cell) = sum(g_r(:, :, i_cell)**2)
1213 END DO
1214 CALL para_env%sum(norm_squared)
1215 DO i_cell = 1, n_cells
1216 IF (sqrt(norm_squared(i_cell))/atom_block_count < bs_env%eps_filter) THEN
1217 g_r(:, :, i_cell) = 0.0_dp
1218 END IF
1219 END DO
1220 DEALLOCATE (norm_squared)
1221 END SUBROUTINE filter_full_green_cells
1222
1223! **************************************************************************************************
1224!> \brief Apply the same real-space cutoff to ordered pairs owned by this rank.
1225!> \param bs_env ...
1226!> \param cells ...
1227!> \param first_pair First locally owned ordered grid pair.
1228!> \param W_grid_R ...
1229! **************************************************************************************************
1230 SUBROUTINE truncate_grid_w_by_pair_distance_pairs(bs_env, cells, first_pair, W_grid_R)
1231 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
1232 INTEGER, DIMENSION(:, :), INTENT(IN) :: cells
1233 INTEGER, INTENT(IN) :: first_pair
1234 REAL(kind=dp), DIMENSION(:, :, :), INTENT(INOUT) :: w_grid_r
1235
1236 INTEGER :: i_l, i_local, i_lp, i_pair, i_r, n_grid
1237 REAL(kind=dp) :: cell_index(3), cutoff_squared, &
1238 displacement(3), lattice_vector(3)
1239
1240 IF (bs_env%ri_rs%cutoff_radius_v_w <= 0.0_dp) RETURN
1241 cpassert(ALLOCATED(bs_env%ri_rs%grid_points))
1242 cpassert(SIZE(w_grid_r, 2) == 1)
1243 cpassert(SIZE(cells, 2) == SIZE(w_grid_r, 3))
1244 cutoff_squared = bs_env%ri_rs%cutoff_radius_v_w**2
1245 n_grid = bs_env%ri_rs%n_grid_points
1246 !$OMP PARALLEL DO DEFAULT(NONE) SCHEDULE(STATIC) &
1247 !$OMP SHARED(bs_env, cells, cutoff_squared, first_pair, n_grid, W_grid_R) &
1248 !$OMP PRIVATE(cell_index, displacement, i_l, i_local, i_lp, i_pair, i_R, lattice_vector)
1249 DO i_r = 1, SIZE(cells, 2)
1250 cell_index(:) = real(cells(:, i_r), dp)
1251 lattice_vector(:) = matmul(bs_env%hmat, cell_index)
1252 DO i_local = 1, SIZE(w_grid_r, 1)
1253 i_pair = first_pair + i_local - 1
1254 i_l = modulo(i_pair - 1, n_grid) + 1
1255 i_lp = (i_pair - 1)/n_grid + 1
1256 displacement = bs_env%ri_rs%grid_points(:, i_l) - &
1257 bs_env%ri_rs%grid_points(:, i_lp) - lattice_vector
1258 IF (sum(displacement**2) > cutoff_squared) w_grid_r(i_local, 1, i_r) = 0.0_dp
1259 END DO
1260 END DO
1261 !$OMP END PARALLEL DO
1262
1263 END SUBROUTINE truncate_grid_w_by_pair_distance_pairs
1264
1265! **************************************************************************************************
1266!> \brief Form G_ll′(k,τ) at the k owners and send each ordered grid pair to its owner.
1267!> Pair i=l+(l′-1)*n_grid follows Fortran column order, so every rank's pair block is a
1268!> contiguous section of the full matrix. In each round every rank forms one native k point.
1269!> \param bs_env ...
1270!> \param orbitals Band Bloch orbitals of one spin at the k points owned in env, MOD(k-1,P).
1271!> \param env Ranks that share the grid pairs and the band k points.
1272!> \param tau ...
1273!> \param ispin ...
1274!> \param occupied ...
1275!> \param G_pairs ...
1276! **************************************************************************************************
1277 SUBROUTINE build_green_pairs(bs_env, orbitals, env, tau, ispin, occupied, G_pairs)
1278 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
1279 COMPLEX(KIND=dp), INTENT(IN) :: orbitals(:, :, :)
1280 TYPE(mp_para_env_type), POINTER :: env
1281 REAL(kind=dp), INTENT(IN) :: tau
1282 INTEGER, INTENT(IN) :: ispin
1283 LOGICAL, INTENT(IN) :: occupied
1284 COMPLEX(KIND=dp), INTENT(OUT) :: g_pairs(:, :, :)
1285
1286 CHARACTER(LEN=*), PARAMETER :: routinen = 'build_green_pairs'
1287
1288 COMPLEX(KIND=dp), ALLOCATABLE :: green(:), received(:), scaled_coeff(:, :)
1289 INTEGER :: first_band, handle, i_band, i_col, &
1290 i_first, i_rank, ikp, n_bands, n_grid, &
1291 n_k, n_local, n_rank
1292 INTEGER, ALLOCATABLE :: pair_counts(:), pair_displs(:), &
1293 recv_counts(:), recv_displs(:), &
1294 send_counts(:)
1295 REAL(kind=dp) :: tau_e
1296 TYPE(gwkp_type), POINTER :: gwkp_env
1297
1298 CALL timeset(routinen, handle)
1299
1300 gwkp_env => bs_env%gwkp_env
1301
1302 n_grid = bs_env%ri_rs%n_grid_points
1303 n_k = bs_env%nkp_scf_desymm
1304 n_local = SIZE(g_pairs, 1)
1305 n_rank = env%num_pe
1306 first_band = 1
1307 n_bands = bs_env%n_occ(ispin)
1308 IF (.NOT. occupied) THEN
1309 first_band = n_bands + 1
1310 n_bands = bs_env%n_ao - n_bands
1311 END IF
1312 cpassert(n_bands >= 0 .AND. n_bands <= bs_env%n_ao)
1313 IF (n_bands == 0) THEN
1314 g_pairs = z_zero
1315 CALL timestop(handle)
1316 RETURN
1317 END IF
1318 CALL grid_pair_blocks(n_grid, n_rank, pair_counts, pair_displs)
1319 ALLOCATE (green(n_grid**2), received(n_local*n_rank), scaled_coeff(n_grid, n_bands), &
1320 recv_counts(0:n_rank - 1), recv_displs(0:n_rank - 1), send_counts(0:n_rank - 1))
1321 recv_displs(:) = [(i_rank*n_local, i_rank=0, n_rank - 1)]
1322 ! G_ll′(k,τ) = Σ_n [ψ_ln(k) exp(-|τ(ε_nk-ε_F)|/2)]
1323 ! [ψ_l′n(k) exp(-|τ(ε_nk-ε_F)|/2)]*.
1324 DO i_first = 1, n_k, n_rank
1325 ikp = i_first + env%mepos
1326 send_counts(:) = 0
1327 IF (ikp <= n_k) THEN
1328 scaled_coeff(:, :) = orbitals(:, first_band:first_band + n_bands - 1, (ikp - 1)/n_rank + 1)
1329 DO i_band = 1, n_bands
1330 tau_e = abs(0.5_dp*tau*(gwkp_env%eigenvalues_G(first_band + i_band - 1, ikp, ispin) - &
1331 bs_env%e_fermi(ispin)))
1332 IF (tau_e < bs_env%stabilize_exp) THEN
1333 scaled_coeff(:, i_band) = scaled_coeff(:, i_band)*exp(-tau_e)
1334 ELSE
1335 scaled_coeff(:, i_band) = z_zero
1336 END IF
1337 END DO
1338 !$OMP PARALLEL DO DEFAULT(NONE) SCHEDULE(STATIC) &
1339 !$OMP SHARED(green, n_bands, n_grid, scaled_coeff) PRIVATE(i_col)
1340 DO i_col = 1, n_grid, column_block_cols
1341 CALL zgemm('N', 'C', n_grid, min(column_block_cols, n_grid - i_col + 1), n_bands, z_one, &
1342 scaled_coeff, n_grid, scaled_coeff(i_col, 1), n_grid, z_zero, &
1343 green(1 + (i_col - 1)*n_grid), n_grid)
1344 END DO
1345 !$OMP END PARALLEL DO
1346 send_counts(:) = pair_counts
1347 END IF
1348 DO i_rank = 0, n_rank - 1
1349 recv_counts(i_rank) = merge(n_local, 0, i_first + i_rank <= n_k)
1350 END DO
1351 CALL env%alltoall(green, send_counts, pair_displs, received, recv_counts, recv_displs)
1352 DO i_rank = 0, min(n_rank, n_k - i_first + 1) - 1
1353 g_pairs(:, 1, i_first + i_rank) = received(i_rank*n_local + 1:(i_rank + 1)*n_local)
1354 END DO
1355 END DO
1356
1357 CALL timestop(handle)
1358
1359 END SUBROUTINE build_green_pairs
1360
1361! **************************************************************************************************
1362!> \brief Ordered grid-pair blocks of all ranks, as assigned by block_range.
1363!> Rank r owns the pairs displs(r)+1, ..., displs(r)+counts(r).
1364!> \param n_grid ...
1365!> \param n_rank ...
1366!> \param counts ...
1367!> \param displs ...
1368! **************************************************************************************************
1369 SUBROUTINE grid_pair_blocks(n_grid, n_rank, counts, displs)
1370 INTEGER, INTENT(IN) :: n_grid, n_rank
1371 INTEGER, ALLOCATABLE, INTENT(OUT) :: counts(:), displs(:)
1372
1373 INTEGER :: i_rank
1374
1375 ALLOCATE (counts(0:n_rank - 1), displs(0:n_rank - 1))
1376 DO i_rank = 0, n_rank - 1
1377 CALL block_range(n_grid**2, i_rank, n_rank, displs(i_rank), counts(i_rank))
1378 END DO
1379 displs(:) = displs - 1
1380
1381 END SUBROUTINE grid_pair_blocks
1382
1383! **************************************************************************************************
1384!> \brief Cache grid values of every Bloch molecular orbital without replicating all MO matrices.
1385!> \param bs_env ...
1386!> \param phi_lmu_k ...
1387!> \param psi_lm_k ...
1388! **************************************************************************************************
1389 SUBROUTINE build_all_psi_k(bs_env, phi_lmu_k, psi_lm_k)
1390 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
1391 COMPLEX(KIND=dp), DIMENSION(:, :, :), INTENT(IN) :: phi_lmu_k
1392 COMPLEX(KIND=dp), ALLOCATABLE, INTENT(OUT) :: psi_lm_k(:, :, :, :)
1393
1394 CHARACTER(LEN=*), PARAMETER :: routinen = 'build_all_psi_k'
1395
1396 COMPLEX(KIND=dp), ALLOCATABLE :: mo_coeff(:, :, :, :)
1397 INTEGER :: handle, ikp_local, ispin
1398
1399 CALL timeset(routinen, handle)
1400
1401 CALL owned_mo_coefficients(bs_env, bs_env%cfm_mo_coeff_kp(1:bs_env%nkp_scf_desymm, :), mo_coeff)
1402 ALLOCATE (psi_lm_k(bs_env%ri_rs%n_grid_points, bs_env%n_ao, SIZE(phi_lmu_k, 3), &
1403 bs_env%n_spin), source=z_zero)
1404 DO ispin = 1, bs_env%n_spin
1405 DO ikp_local = 1, SIZE(mo_coeff, 3)
1406 psi_lm_k(:, :, ikp_local, ispin) = matmul(phi_lmu_k(:, :, ikp_local), mo_coeff(:, :, ikp_local, ispin))
1407 END DO
1408 END DO
1409 DEALLOCATE (mo_coeff)
1410
1411 CALL timestop(handle)
1412
1413 END SUBROUTINE build_all_psi_k
1414
1415! **************************************************************************************************
1416!> \brief Cache Bloch sums of the RI-RS grid values for the k-points owned by this rank.
1417!> \param bs_env ...
1418!> \param phi_lmu_k ...
1419! **************************************************************************************************
1420 SUBROUTINE build_all_phi_k(bs_env, phi_lmu_k)
1421 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
1422 COMPLEX(dp), ALLOCATABLE, INTENT(OUT) :: phi_lmu_k(:, :, :)
1423
1424 CHARACTER(LEN=*), PARAMETER :: routinen = 'build_all_phi_k'
1425
1426 INTEGER :: handle, i_r, ikp, ikp_local, n_k_local
1427 REAL(dp), ALLOCATABLE :: phi_r(:, :)
1428
1429 CALL timeset(routinen, handle)
1430
1431 n_k_local = (bs_env%nkp_scf_desymm + bs_env%para_env%num_pe - 1 - &
1432 bs_env%para_env%mepos)/bs_env%para_env%num_pe
1433 ALLOCATE (phi_lmu_k(bs_env%ri_rs%n_grid_points, bs_env%n_ao, n_k_local))
1434 phi_lmu_k = z_zero
1435 IF (n_k_local > 0) THEN
1436 ALLOCATE (phi_r(bs_env%ri_rs%n_grid_points, bs_env%n_ao))
1437 DO i_r = 1, SIZE(bs_env%index_to_cell_3c, 2)
1438 CALL evaluate_mu_rl_r_values(bs_env, bs_env%ri_rs%grid_points, i_r, phi_r)
1439 ikp_local = 0
1440 DO ikp = bs_env%para_env%mepos + 1, bs_env%nkp_scf_desymm, bs_env%para_env%num_pe
1441 ikp_local = ikp_local + 1
1442 phi_lmu_k(:, :, ikp_local) = phi_lmu_k(:, :, ikp_local) + phi_r* &
1443 exp(gaussi*twopi*sum(bs_env%kpoints_scf_desymm%xkp(:, ikp)* &
1444 REAL(bs_env%index_to_cell_3c(:, i_r), dp)))
1445 END DO
1446 END DO
1447 END IF
1448
1449 CALL timestop(handle)
1450
1451 END SUBROUTINE build_all_phi_k
1452
1453! **************************************************************************************************
1454!> \brief Fourier transform distributed real RI lattice blocks to cyclic q owners in one exchange.
1455!>
1456!> I_PQ(q) = Σ_R exp(2πiq·R) I_PQ(R). Each sender transforms its own matrix block
1457!> for every q with the lattice FFT and sends the resulting blocks to their q owners, iq mod P.
1458!> \param fm_R ...
1459!> \param cells ...
1460!> \param qpoints ...
1461!> \param nkp_grid regular q mesh of qpoints
1462!> \param matrices_q ...
1463! **************************************************************************************************
1464 SUBROUTINE ri_2c_lattice_to_owned_q(fm_R, cells, qpoints, nkp_grid, matrices_q)
1465 TYPE(cp_fm_type), INTENT(IN) :: fm_r(:)
1466 INTEGER, INTENT(IN) :: cells(:, :)
1467 REAL(kind=dp), INTENT(IN) :: qpoints(:, :)
1468 INTEGER, INTENT(IN) :: nkp_grid(3)
1469 COMPLEX(KIND=dp), INTENT(OUT) :: matrices_q(:, :, :)
1470
1471 CHARACTER(LEN=*), PARAMETER :: routinen = 'ri_2c_lattice_to_owned_q'
1472
1473 COMPLEX(KIND=dp), ALLOCATABLE :: recv_buffer(:)
1474 COMPLEX(KIND=dp), ALLOCATABLE, TARGET :: send_buffer(:)
1475 COMPLEX(KIND=dp), CONTIGUOUS, POINTER :: values_q(:, :, :)
1476 INTEGER :: dest, handle, icell, icol, iq, iq_local, &
1477 irow, n_q, n_q_local, n_ri, ncols, &
1478 nprocs, nrecv, nrows, nsend, pos, &
1479 source
1480 INTEGER, ALLOCATABLE :: block_metadata(:), q_order(:), recv_counts(:), recv_displs(:), &
1481 send_counts(:), send_displs(:), source_cols(:, :), source_rows(:, :), source_sizes(:, :)
1482 INTEGER, POINTER :: cols(:), rows(:)
1483 REAL(kind=dp), ALLOCATABLE :: values_r(:, :, :)
1484 TYPE(mp_para_env_type), POINTER :: para_env
1485
1486 CALL timeset(routinen, handle)
1487
1488 cpassert(SIZE(fm_r) > 0)
1489 cpassert(SIZE(cells, 1) == 3 .AND. SIZE(cells, 2) == SIZE(fm_r))
1490 cpassert(SIZE(qpoints, 1) == 3)
1491 n_q = SIZE(qpoints, 2)
1492 n_ri = SIZE(matrices_q, 1)
1493 cpassert(SIZE(matrices_q, 2) == n_ri)
1494 para_env => fm_r(1)%matrix_struct%para_env
1495 nprocs = para_env%num_pe
1496 n_q_local = count([(modulo(iq, nprocs) == para_env%mepos, iq=1, n_q)])
1497 cpassert(SIZE(matrices_q, 3) == n_q_local)
1498 CALL cp_fm_get_info(fm_r(1), nrow_local=nrows, ncol_local=ncols, &
1499 row_indices=rows, col_indices=cols)
1500
1501 ALLOCATE (block_metadata(2 + 2*n_ri), source_sizes(2, 0:nprocs - 1), &
1502 source_rows(n_ri, 0:nprocs - 1), source_cols(n_ri, 0:nprocs - 1))
1503 DO source = 0, nprocs - 1
1504 block_metadata = 0
1505 IF (para_env%mepos == source) THEN
1506 block_metadata(1:2) = [nrows, ncols]
1507 block_metadata(3:2 + nrows) = rows(1:nrows)
1508 block_metadata(3 + n_ri:2 + n_ri + ncols) = cols(1:ncols)
1509 END IF
1510 CALL para_env%bcast(block_metadata, source)
1511 source_sizes(:, source) = block_metadata(1:2)
1512 source_rows(:, source) = block_metadata(3:2 + n_ri)
1513 source_cols(:, source) = block_metadata(3 + n_ri:2 + 2*n_ri)
1514 END DO
1515
1516 ! The q points sorted by owner make the transformed blocks the send buffer itself.
1517 ALLOCATE (q_order(n_q), send_counts(0:nprocs - 1), recv_counts(0:nprocs - 1), &
1518 send_displs(0:nprocs - 1), recv_displs(0:nprocs - 1))
1519 pos = 0
1520 DO dest = 0, nprocs - 1
1521 send_displs(dest) = pos*nrows*ncols
1522 DO iq = 1, n_q
1523 IF (modulo(iq, nprocs) /= dest) cycle
1524 pos = pos + 1
1525 q_order(pos) = iq
1526 END DO
1527 send_counts(dest) = pos*nrows*ncols - send_displs(dest)
1528 END DO
1529 nsend = n_q*nrows*ncols
1530 CALL para_env%alltoall(send_counts, recv_counts, 1)
1531 recv_displs(0) = 0
1532 DO source = 1, nprocs - 1
1533 recv_displs(source) = recv_displs(source - 1) + recv_counts(source - 1)
1534 END DO
1535 nrecv = recv_displs(nprocs - 1) + recv_counts(nprocs - 1)
1536 ALLOCATE (send_buffer(max(1, nsend)), recv_buffer(max(1, nrecv)))
1537 IF (nsend > 0) THEN
1538 ALLOCATE (values_r(nrows, ncols, SIZE(fm_r)))
1539 DO icell = 1, SIZE(fm_r)
1540 values_r(:, :, icell) = fm_r(icell)%local_data(1:nrows, 1:ncols)
1541 END DO
1542 values_q(1:nrows, 1:ncols, 1:n_q) => send_buffer(1:nsend)
1543 CALL cell_to_k_grid_fft(values_r, cells, qpoints, nkp_grid, values_q, &
1544 selected_kpoints=q_order)
1545 DEALLOCATE (values_r)
1546 END IF
1547 CALL para_env%alltoall(send_buffer, send_counts, send_displs, &
1548 recv_buffer, recv_counts, recv_displs)
1549 matrices_q = z_zero
1550 DO source = 0, nprocs - 1
1551 nrows = source_sizes(1, source)
1552 ncols = source_sizes(2, source)
1553 cpassert(recv_counts(source) == nrows*ncols*n_q_local)
1554 pos = recv_displs(source)
1555 DO iq_local = 1, n_q_local
1556 DO icol = 1, ncols
1557 DO irow = 1, nrows
1558 matrices_q(source_rows(irow, source), source_cols(icol, source), iq_local) = &
1559 recv_buffer(pos + (icol - 1)*nrows + irow)
1560 END DO
1561 END DO
1562 pos = pos + nrows*ncols
1563 END DO
1564 END DO
1565
1566 CALL timestop(handle)
1567
1568 END SUBROUTINE ri_2c_lattice_to_owned_q
1569
1570! **************************************************************************************************
1571!> \brief Evaluate the band Bloch orbitals of the k points owned by this rank, MOD(k-1,P), one AO
1572!> cell at a time. No rank stores the orbitals of other k points.
1573!> \param bs_env ...
1574!> \param orbitals orbitals(:, :, (k-1)/P+1, spin) on the RI-RS grid
1575! **************************************************************************************************
1576 SUBROUTINE collect_band_bloch_orbitals(bs_env, orbitals)
1577 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
1578 COMPLEX(dp), ALLOCATABLE, INTENT(OUT) :: orbitals(:, :, :, :)
1579
1580 CHARACTER(LEN=*), PARAMETER :: routinen = 'collect_band_bloch_orbitals'
1581
1582 COMPLEX(dp), ALLOCATABLE :: coefficients(:, :, :, :), phi_k(:, :)
1583 INTEGER :: handle, i_kp, i_local, i_r, ispin, &
1584 n_grid, n_own
1585 REAL(dp) :: available_gb, orbital_bytes
1586 REAL(dp), ALLOCATABLE :: phi_r(:, :)
1587 TYPE(mp_para_env_type), POINTER :: env
1588
1589 CALL timeset(routinen, handle)
1590
1591 env => bs_env%para_env
1592 n_grid = bs_env%ri_rs%n_grid_points
1593 n_own = (bs_env%nkp_bs_and_DOS - env%mepos + env%num_pe - 1)/env%num_pe
1594 CALL collect_band_mo_coefficients(bs_env, coefficients)
1595 orbital_bytes = real(n_grid, dp)*real(bs_env%n_ao, dp)* &
1596 (complex_bytes*real(n_own*bs_env%n_spin, dp) + complex_bytes + real_bytes)
1597 CALL mp_mem_avail_per_rank_gb(env, available_gb)
1598 CALL env%min(available_gb)
1599 CALL env%max(orbital_bytes)
1600 IF (orbital_bytes > stage_memory_share*max(0.0_dp, available_gb)*bytes_per_gb) THEN
1601 cpabort("Insufficient memory for GW band-path orbitals")
1602 END IF
1603 ALLOCATE (phi_r(n_grid, bs_env%n_ao))
1604 ALLOCATE (orbitals(n_grid, bs_env%n_ao, n_own, bs_env%n_spin))
1605 orbitals = z_zero
1606 DO i_r = 1, SIZE(bs_env%index_to_cell_3c, 2)
1607 IF (n_own == 0) EXIT
1608 CALL evaluate_mu_rl_r_values(bs_env, bs_env%ri_rs%grid_points, i_r, phi_r)
1609 !$OMP PARALLEL DO DEFAULT(NONE) SCHEDULE(STATIC) &
1610 !$OMP SHARED(bs_env, env, i_R, n_own, orbitals, phi_R) PRIVATE(i_kp, i_local)
1611 DO i_local = 1, n_own
1612 i_kp = env%mepos + 1 + (i_local - 1)*env%num_pe
1613 orbitals(:, :, i_local, 1) = orbitals(:, :, i_local, 1) + phi_r* &
1614 exp(gaussi*twopi*sum(bs_env%kpoints_DOS%xkp(:, i_kp)* &
1615 REAL(bs_env%index_to_cell_3c(:, i_r), dp)))
1616 END DO
1617 !$OMP END PARALLEL DO
1618 END DO
1619 !$OMP PARALLEL DEFAULT(NONE) SHARED(bs_env, coefficients, n_grid, n_own, orbitals) &
1620 !$OMP PRIVATE(i_local, ispin, phi_k)
1621 ALLOCATE (phi_k(n_grid, bs_env%n_ao))
1622 !$OMP DO SCHEDULE(STATIC)
1623 DO i_local = 1, n_own
1624 phi_k(:, :) = orbitals(:, :, i_local, 1)
1625 DO ispin = 1, bs_env%n_spin
1626 CALL zgemm('N', 'N', n_grid, bs_env%n_ao, bs_env%n_ao, z_one, &
1627 phi_k, n_grid, coefficients(:, :, i_local, ispin), &
1628 bs_env%n_ao, z_zero, orbitals(:, :, i_local, ispin), n_grid)
1629 END DO
1630 END DO
1631 !$OMP END DO
1632 DEALLOCATE (phi_k)
1633 !$OMP END PARALLEL
1634
1635 CALL timestop(handle)
1636
1637 END SUBROUTINE collect_band_bloch_orbitals
1638
1639! **************************************************************************************************
1640!> \brief Move the band orbitals from their owners in all ranks, MOD(k-1,P), to their owners in
1641!> the time groups of Σc, MOD(k-1,g). As g divides P, both owners have the same rank within their
1642!> group, so every k point is broadcast once among the ranks with this group rank.
1643!> \param bs_env ...
1644!> \param gwkp_env ...
1645! **************************************************************************************************
1646 SUBROUTINE band_orbitals_to_groups(bs_env, gwkp_env)
1647 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
1648 TYPE(gwkp_type), INTENT(INOUT) :: gwkp_env
1649
1650 CHARACTER(LEN=*), PARAMETER :: routinen = 'band_orbitals_to_groups'
1651
1652 COMPLEX(dp), ALLOCATABLE :: group_orbitals(:, :, :, :)
1653 INTEGER :: handle, i_kp, i_local, i_root, ispin, &
1654 n_group, n_own, n_rank
1655 TYPE(mp_comm_type) :: column_env
1656
1657 CALL timeset(routinen, handle)
1658
1659 n_rank = bs_env%para_env%num_pe
1660 n_group = gwkp_env%group_env%num_pe
1661 n_own = (bs_env%nkp_bs_and_DOS - gwkp_env%group_env%mepos + n_group - 1)/n_group
1662 ALLOCATE (group_orbitals(SIZE(gwkp_env%band_orbitals, 1), SIZE(gwkp_env%band_orbitals, 2), &
1663 n_own, bs_env%n_spin))
1664 CALL column_env%from_split(bs_env%para_env, gwkp_env%group_env%mepos)
1665 DO i_local = 1, n_own
1666 i_kp = gwkp_env%group_env%mepos + 1 + (i_local - 1)*n_group
1667 i_root = modulo(i_kp - 1, n_rank)/n_group
1668 DO ispin = 1, bs_env%n_spin
1669 IF (column_env%mepos == i_root) THEN
1670 group_orbitals(:, :, i_local, ispin) = gwkp_env%band_orbitals(:, :, (i_kp - 1)/n_rank + 1, ispin)
1671 END IF
1672 CALL column_env%bcast(group_orbitals(:, :, i_local, ispin), i_root)
1673 END DO
1674 END DO
1675 CALL column_env%free()
1676 CALL move_alloc(group_orbitals, gwkp_env%band_orbitals)
1677
1678 CALL timestop(handle)
1679
1680 END SUBROUTINE band_orbitals_to_groups
1681
1682! **************************************************************************************************
1683!> \brief Return the SCF-mesh index of a band k point, or 0 if it lies off the mesh.
1684!> The first nkp_scf_desymm band points are the SCF mesh in the same order.
1685!> \param bs_env ...
1686!> \param i_kp_eval Band k point.
1687!> \return ...
1688! **************************************************************************************************
1689 FUNCTION native_q_index(bs_env, i_kp_eval) RESULT(native_iq)
1690 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
1691 INTEGER, INTENT(IN) :: i_kp_eval
1692 INTEGER :: native_iq
1693
1694 INTEGER :: iq
1695 REAL(kind=dp) :: delta_k(3)
1696
1697 native_iq = 0
1698 DO iq = 1, bs_env%nkp_scf_desymm
1699 IF (i_kp_eval <= bs_env%nkp_scf_desymm .AND. iq /= i_kp_eval) cycle
1700 delta_k = bs_env%kpoints_DOS%xkp(:, i_kp_eval) - bs_env%kpoints_scf_desymm%xkp(:, iq)
1701 IF (maxval(abs(delta_k - anint(delta_k))) <= 64.0_dp*epsilon(1.0_dp)) THEN
1702 native_iq = iq
1703 RETURN
1704 END IF
1705 END DO
1706
1707 END FUNCTION native_q_index
1708
1709! **************************************************************************************************
1710!> \brief MO coefficients of the band k points owned by this rank, MOD(k-1,P).
1711!> \param bs_env ...
1712!> \param band_coefficients ...
1713! **************************************************************************************************
1714 SUBROUTINE collect_band_mo_coefficients(bs_env, band_coefficients)
1715 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
1716 COMPLEX(KIND=dp), ALLOCATABLE, INTENT(OUT) :: band_coefficients(:, :, :, :)
1717
1718 CHARACTER(LEN=*), PARAMETER :: routinen = 'collect_band_mo_coefficients'
1719
1720 COMPLEX(KIND=dp), ALLOCATABLE :: coefficients(:, :)
1721 INTEGER :: handle, i_kp_eval, ispin
1722 TYPE(mp_para_env_type), POINTER :: env
1723
1724 CALL timeset(routinen, handle)
1725
1726 env => bs_env%para_env
1727 ALLOCATE (coefficients(bs_env%n_ao, bs_env%n_ao))
1728 ALLOCATE (band_coefficients(bs_env%n_ao, bs_env%n_ao, &
1729 (bs_env%nkp_bs_and_DOS - env%mepos + env%num_pe - 1)/env%num_pe, &
1730 bs_env%n_spin))
1731 DO ispin = 1, bs_env%n_spin
1732 DO i_kp_eval = 1, bs_env%nkp_bs_and_DOS
1733 CALL cp_cfm_get_submatrix(bs_env%cfm_mo_coeff_kp(i_kp_eval, ispin), coefficients)
1734 IF (modulo(i_kp_eval - 1, env%num_pe) == env%mepos) THEN
1735 band_coefficients(:, :, (i_kp_eval - 1)/env%num_pe + 1, ispin) = coefficients
1736 END IF
1737 END DO
1738 END DO
1739
1740 CALL timestop(handle)
1741
1742 END SUBROUTINE collect_band_mo_coefficients
1743
1744! **************************************************************************************************
1745!> \brief Σ_ll′(k) = Σ_R exp(2πik·R) Σ_ll′(R) on the native k mesh for the ordered grid pairs of
1746!> this rank. The pair-centered lattice FFT consumes its input, so it transforms copies of at most
1747!> 64 MiB and Σ(R) stays available for the band k points.
1748!> \param bs_env ...
1749!> \param cells ...
1750!> \param sigma_R ...
1751!> \param first_pair First locally owned ordered grid pair.
1752!> \param sigma_mesh ...
1753! **************************************************************************************************
1754 SUBROUTINE self_energy_on_mesh(bs_env, cells, sigma_R, first_pair, sigma_mesh)
1755 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
1756 INTEGER, INTENT(IN) :: cells(:, :)
1757 REAL(kind=dp), INTENT(IN) :: sigma_r(:, :, :)
1758 INTEGER, INTENT(IN) :: first_pair
1759 COMPLEX(KIND=dp), INTENT(OUT) :: sigma_mesh(:, :, :)
1760
1761 INTEGER :: i_first, n_chunk, n_pairs
1762 REAL(kind=dp), ALLOCATABLE :: chunk(:, :, :)
1763
1764 n_chunk = max(1, min(SIZE(sigma_r, 1), int(64.0_dp*bytes_per_mib/real_bytes)/SIZE(cells, 2)))
1765 ALLOCATE (chunk(n_chunk, 1, SIZE(cells, 2)))
1766 DO i_first = 1, SIZE(sigma_r, 1), n_chunk
1767 n_pairs = min(n_chunk, SIZE(sigma_r, 1) - i_first + 1)
1768 chunk(1:n_pairs, :, :) = sigma_r(i_first:i_first + n_pairs - 1, :, :)
1769 CALL product_to_k_pair_centered(bs_env, cells, bs_env%kpoints_scf_desymm%xkp, chunk(1:n_pairs, :, :), &
1770 sigma_mesh(i_first:i_first + n_pairs - 1, :, :), &
1771 first_pair + i_first - 1)
1772 END DO
1773 DEALLOCATE (chunk)
1774 END SUBROUTINE self_energy_on_mesh
1775
1776! **************************************************************************************************
1777!> \brief Project Σ_ll′(k) onto the band Bloch orbitals, Σ_n(k) = Re[ψ_n(k)† Σ(k) ψ_n(k)].
1778!>
1779!> The pair owners evaluate Σ_ll′(k) = Σ_R exp(2πik·R) Σ_ll′(R) at the band k points. Every k
1780!> owner, MOD(k-1,P), receives its complete matrices and contracts them with dense kernels. A
1781!> round covers m k points per rank, so that one pass over Σ(R) serves up to
1782!> band_kpoints_per_pass k points.
1783!> \param bs_env ...
1784!> \param sigma_mesh Σ on the native k mesh for the ordered grid pairs of this rank.
1785!> \param sigma_R Σ in the cells of the native mesh for the same pairs.
1786!> \param first_pair First locally owned ordered grid pair.
1787!> \param env Ranks that share the grid pairs and the band k points.
1788!> \param orbitals Band Bloch orbitals of one spin at the k points owned in env.
1789!> \param sigma_band ...
1790! **************************************************************************************************
1791 SUBROUTINE self_energy_pairs_to_bloch(bs_env, sigma_mesh, sigma_R, first_pair, env, orbitals, sigma_band)
1792 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
1793 COMPLEX(KIND=dp), INTENT(IN) :: sigma_mesh(:, :, :)
1794 REAL(kind=dp), INTENT(IN) :: sigma_r(:, :, :)
1795 INTEGER, INTENT(IN) :: first_pair
1796 TYPE(mp_para_env_type), POINTER :: env
1797 COMPLEX(KIND=dp), CONTIGUOUS, INTENT(IN) :: orbitals(:, :, :)
1798 REAL(kind=dp), INTENT(OUT) :: sigma_band(:, :)
1799
1800 CHARACTER(LEN=*), PARAMETER :: routinen = 'self_energy_pairs_to_Bloch'
1801
1802 COMPLEX(KIND=dp), ALLOCATABLE :: sigma_k(:, :), work(:, :)
1803 COMPLEX(KIND=dp), ALLOCATABLE, TARGET :: send_buffer(:)
1804 COMPLEX(KIND=dp), CONTIGUOUS, POINTER :: sigma_columns(:, :)
1805 INTEGER :: handle, i_band, i_batch, i_first, i_kp, &
1806 i_rank, j, m, n_batch, n_grid, n_kp, &
1807 n_local, n_rank, n_round
1808 INTEGER, ALLOCATABLE :: pair_counts(:), pair_displs(:), &
1809 recv_counts(:), send_counts(:), &
1810 send_displs(:)
1811
1812 CALL timeset(routinen, handle)
1813
1814 n_grid = bs_env%ri_rs%n_grid_points
1815 n_kp = bs_env%nkp_bs_and_DOS
1816 n_local = SIZE(sigma_mesh, 1)
1817 n_rank = env%num_pe
1818 n_batch = min(16, bs_env%n_ao)
1819 m = projection_round_kpoints(n_grid, n_rank)
1820 CALL grid_pair_blocks(n_grid, n_rank, pair_counts, pair_displs)
1821 ALLOCATE (send_buffer(n_local*n_rank*m), sigma_k(n_grid**2, m), recv_counts(0:n_rank - 1), &
1822 send_counts(0:n_rank - 1), send_displs(0:n_rank - 1))
1823 sigma_columns(1:n_local, 1:n_rank*m) => send_buffer
1824 send_displs(:) = [(i_rank*n_local, i_rank=0, n_rank - 1)]
1825 sigma_band = 0.0_dp
1826 ! Column i_col of a round holds the band k point i_first+i_col-1, owned by MOD(i_col-1,P).
1827 DO i_first = 1, n_kp, n_rank*m
1828 n_round = min(n_rank*m, n_kp - i_first + 1)
1829 CALL band_self_energy(bs_env, sigma_mesh, sigma_r, first_pair, i_first, sigma_columns(:, 1:n_round))
1830 DO j = 1, m
1831 DO i_rank = 0, n_rank - 1
1832 send_counts(i_rank) = merge(n_local, 0, (j - 1)*n_rank + i_rank < n_round)
1833 END DO
1834 i_kp = i_first + (j - 1)*n_rank + env%mepos
1835 recv_counts(:) = merge(pair_counts, 0, i_kp <= n_kp)
1836 CALL env%alltoall(send_buffer((j - 1)*n_rank*n_local + 1:j*n_rank*n_local), send_counts, &
1837 send_displs, sigma_k(:, j), recv_counts, pair_displs)
1838 END DO
1839 !$OMP PARALLEL DEFAULT(NONE) &
1840 !$OMP SHARED(bs_env, env, i_first, m, n_batch, n_grid, n_kp, n_rank, orbitals, sigma_band, sigma_k) &
1841 !$OMP PRIVATE(i_band, i_batch, i_kp, j, work)
1842 ALLOCATE (work(n_grid, n_batch))
1843 !$OMP DO COLLAPSE(2) SCHEDULE(DYNAMIC)
1844 DO j = 1, m
1845 DO i_batch = 1, bs_env%n_ao, n_batch
1846 i_kp = i_first + (j - 1)*n_rank + env%mepos
1847 IF (i_kp > n_kp) cycle
1848 CALL zgemm('N', 'N', n_grid, min(n_batch, bs_env%n_ao - i_batch + 1), n_grid, z_one, &
1849 sigma_k(1, j), n_grid, &
1850 orbitals(:, i_batch:min(i_batch + n_batch - 1, bs_env%n_ao), (i_kp - 1)/n_rank + 1), n_grid, &
1851 z_zero, work, n_grid)
1852 DO i_band = i_batch, min(i_batch + n_batch - 1, bs_env%n_ao)
1853 sigma_band(i_band, i_kp) = real(sum(conjg(orbitals(:, i_band, (i_kp - 1)/n_rank + 1))* &
1854 work(:, i_band - i_batch + 1)), dp)
1855 END DO
1856 END DO
1857 END DO
1858 !$OMP END DO
1859 DEALLOCATE (work)
1860 !$OMP END PARALLEL
1861 END DO
1862 CALL env%sum(sigma_band)
1863
1864 CALL timestop(handle)
1865
1866 END SUBROUTINE self_energy_pairs_to_bloch
1867
1868! **************************************************************************************************
1869!> \brief Weight of the image offset c along one direction of an n_mesh-point k mesh: 1 inside
1870!> the Born-von Karman supercell, the given boundary weight on its boundary 2|c| = n_mesh, 0
1871!> outside. A pair on a half-cell tie averages the image sets centred at s and s+1.
1872!> \param c ...
1873!> \param n_mesh ...
1874!> \param tie ...
1875!> \param boundary Weight of each of the two boundary images.
1876!> \return ...
1877! **************************************************************************************************
1878 PURE REAL(KIND=dp) FUNCTION image_weight(c, n_mesh, tie, boundary) RESULT(weight)
1879 INTEGER, INTENT(IN) :: c, n_mesh
1880 LOGICAL, INTENT(IN) :: tie
1881 REAL(kind=dp), INTENT(IN) :: boundary
1882
1883 INTEGER :: i_center, n_centers
1884
1885 n_centers = merge(2, 1, tie)
1886 weight = 0.0_dp
1887 DO i_center = 0, n_centers - 1
1888 IF (2*abs(c - i_center) < n_mesh) THEN
1889 weight = weight + 1.0_dp/real(n_centers, dp)
1890 ELSE IF (2*abs(c - i_center) == n_mesh) THEN
1891 weight = weight + boundary/real(n_centers, dp)
1892 END IF
1893 END DO
1894 END FUNCTION image_weight
1895
1896! **************************************************************************************************
1897!> \brief Bound the band-projection and Green-function workspaces of one rank in bytes.
1898!> The projection holds the Σ(k) columns of one round, the complete Σ(k) of the owned k points,
1899!> the shared lattice phases, thread-private image, sum and output blocks, and the 64 MiB copy of
1900!> the mesh transform. The Green function holds one complete G(k), its received pair blocks and
1901!> the scaled orbitals.
1902!> \param n_grid ...
1903!> \param n_rank ...
1904!> \param n_images Bound of the image cells summed per pair.
1905!> \param n_ao ...
1906!> \param n_threads ...
1907!> \return ...
1908! **************************************************************************************************
1909 FUNCTION projection_workspace_bytes(n_grid, n_rank, n_images, n_ao, n_threads) RESULT(bytes)
1910 INTEGER, INTENT(IN) :: n_grid, n_rank, n_images, n_ao, n_threads
1911 REAL(dp) :: bytes
1912
1913 REAL(dp) :: m, n_round
1914
1915 m = real(projection_round_kpoints(n_grid, n_rank), dp)
1916 n_round = m*real(n_rank, dp)
1917 bytes = 2.0_dp*complex_bytes*m*real(n_grid, dp)**2 + complex_bytes*n_round*real(n_images, dp) + &
1918 projection_workspace + &
1919 REAL(n_threads, dp)*(complex_bytes*REAL(n_grid, dp)*REAL(MIN(16, n_ao), dp) + &
1920 real_bytes*REAL(pair_batch_size, dp)*REAL(n_images, dp) + &
1921 complex_bytes*REAL(pair_batch_size, dp)*n_round)
1922 bytes = max(bytes, complex_bytes*real(n_grid, dp)*(2.0_dp*real(n_grid, dp) + real(n_ao, dp)))
1923 END FUNCTION projection_workspace_bytes
1924
1925! **************************************************************************************************
1926!> \brief Band k points per rank and projection round: a round evaluates up to
1927!> band_kpoints_per_pass k points, while the complete Σ(k) of a round stay below 512 MiB.
1928!> \param n_grid ...
1929!> \param n_rank ...
1930!> \return ...
1931! **************************************************************************************************
1932 PURE INTEGER FUNCTION projection_round_kpoints(n_grid, n_rank) RESULT(m)
1933 INTEGER, INTENT(IN) :: n_grid, n_rank
1934
1935 m = max(1, min(band_kpoints_per_pass/n_rank, int(projection_round_bytes/(2.0_dp*complex_bytes*real(n_grid, dp)**2))))
1936 END FUNCTION projection_round_kpoints
1937
1938! **************************************************************************************************
1939!> \brief Bytes left for a stage after the reserve for data that stays alive and for libraries.
1940!> \param available_GB ...
1941!> \return ...
1942! **************************************************************************************************
1943 PURE REAL(KIND=dp) FUNCTION budget_after_reserve(available_GB) RESULT(budget)
1944 REAL(kind=dp), INTENT(IN) :: available_gb
1945
1946 budget = max(0.0_dp, available_gb - max(min_reserve_gb, reserve_share*available_gb))*bytes_per_gb
1947 END FUNCTION budget_after_reserve
1948
1949! **************************************************************************************************
1950!> \brief Σ_ll′(k) = Σ_R exp(2πik·R) Σ_ll′(R) at the band k points of one projection round.
1951!> Native k points are copied from the mesh transform. Elsewhere every pair sums its images
1952!> R = s+c around the lattice shift s nearest to H⁻¹(r_l-r_l′), weighted by image_weight in
1953!> each direction; on the native mesh this is the pair-centered lattice FFT. The two images on
1954!> the supercell boundary share (1+cos(2πk₀N))/2 for the mesh offset k₀: on a Γ-centered mesh
1955!> they are equivalent, on a half-shifted mesh G and W are antiperiodic there and the images
1956!> cancel. All pairs share the phases exp(2πik·c), so batches of pairs are summed by two real
1957!> GEMMs.
1958!> \param bs_env ...
1959!> \param sigma_mesh ...
1960!> \param sigma_R ...
1961!> \param first_pair ...
1962!> \param first_kp_eval Band k point of the first column of sigma_k.
1963!> \param sigma_k ...
1964! **************************************************************************************************
1965 SUBROUTINE band_self_energy(bs_env, sigma_mesh, sigma_R, first_pair, first_kp_eval, sigma_k)
1966 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
1967 COMPLEX(KIND=dp), INTENT(IN) :: sigma_mesh(:, :, :)
1968 REAL(kind=dp), INTENT(IN) :: sigma_r(:, :, :)
1969 INTEGER, INTENT(IN) :: first_pair, first_kp_eval
1970 COMPLEX(KIND=dp), INTENT(OUT) :: sigma_k(:, :)
1971
1972 COMPLEX(KIND=dp) :: phase
1973 INTEGER :: c1, c2, c3, first_c(3), i_c, i_col, i_first, i_local, i_off, i_pair, last_c(3), &
1974 lower(3), n_c, n_grid, n_off, n_pairs, native_iq, nmesh(3), shift(3), wrapped(3)
1975 INTEGER, ALLOCATABLE :: offmesh_col(:), pair_shift(:, :)
1976 LOGICAL :: any_tie(3), tie(3)
1977 REAL(kind=dp) :: angle, boundary(3)
1978 REAL(kind=dp), ALLOCATABLE :: cos_c(:, :), fractional_grid(:, :), images(:, :), sin_c(:, :), &
1979 sum_im(:, :), sum_re(:, :), weight_1(:), weight_2(:), weight_3(:), xk(:, :)
1980
1981 ALLOCATE (offmesh_col(SIZE(sigma_k, 2)))
1982 n_off = 0
1983 DO i_col = 1, SIZE(sigma_k, 2)
1984 native_iq = native_q_index(bs_env, first_kp_eval + i_col - 1)
1985 IF (native_iq > 0) THEN
1986 sigma_k(:, i_col) = sigma_mesh(:, 1, native_iq)
1987 ELSE
1988 n_off = n_off + 1
1989 offmesh_col(n_off) = i_col
1990 END IF
1991 END DO
1992 IF (n_off == 0) RETURN
1993
1994 n_grid = bs_env%ri_rs%n_grid_points
1995 nmesh = bs_env%kpoints_scf_desymm%nkp_grid
1996 lower = -nmesh/2
1997 ALLOCATE (fractional_grid(3, n_grid))
1998 fractional_grid(:, :) = matmul(bs_env%ri_rs%cell%h_inv, bs_env%ri_rs%grid_points)
1999 DO i_c = 1, 3
2000 IF (bs_env%periodic(i_c) == 0) fractional_grid(i_c, :) = 0.0_dp
2001 END DO
2002 any_tie = .false.
2003 DO i_local = 1, SIZE(sigma_r, 1)
2004 CALL product_pair_shift(first_pair + i_local - 1, n_grid, fractional_grid, shift, tie)
2005 any_tie = any_tie .OR. tie
2006 END DO
2007 boundary = 0.25_dp*(1.0_dp + cos(twopi*bs_env%kpoints_scf_desymm%xkp(:, 1)*nmesh))
2008 first_c = lower
2009 last_c = nmesh/2 + merge(1, 0, any_tie)
2010 n_c = product(last_c - first_c + 1)
2011 ALLOCATE (xk(3, n_off), cos_c(n_c, n_off), sin_c(n_c, n_off))
2012 DO i_off = 1, n_off
2013 xk(:, i_off) = bs_env%kpoints_DOS%xkp(:, first_kp_eval + offmesh_col(i_off) - 1)
2014 END DO
2015 ! Image c of every pair is column i_c of the shared phases, c1 running fastest.
2016 !$OMP PARALLEL DO DEFAULT(NONE) SCHEDULE(STATIC) &
2017 !$OMP SHARED(cos_c, first_c, last_c, n_off, sin_c, xk) PRIVATE(c1, c2, c3, i_c, i_off, phase)
2018 DO i_off = 1, n_off
2019 i_c = 0
2020 DO c3 = first_c(3), last_c(3)
2021 DO c2 = first_c(2), last_c(2)
2022 DO c1 = first_c(1), last_c(1)
2023 i_c = i_c + 1
2024 phase = exp(cmplx(0.0_dp, twopi*(xk(1, i_off)*c1 + xk(2, i_off)*c2 + xk(3, i_off)*c3), kind=dp))
2025 cos_c(i_c, i_off) = real(phase, kind=dp)
2026 sin_c(i_c, i_off) = aimag(phase)
2027 END DO
2028 END DO
2029 END DO
2030 END DO
2031 !$OMP END PARALLEL DO
2032
2033 !$OMP PARALLEL DEFAULT(NONE) &
2034 !$OMP SHARED(boundary, cos_c, first_c, first_pair, fractional_grid, last_c, lower, n_c, n_grid, n_off, &
2035 !$OMP nmesh, offmesh_col, sigma_k, sigma_R, sin_c, xk) &
2036 !$OMP PRIVATE(angle, c1, c2, c3, i_c, i_first, i_off, i_pair, images, n_pairs, pair_shift, shift, &
2037 !$OMP sum_im, sum_re, tie, weight_1, weight_2, weight_3, wrapped)
2038 ALLOCATE (images(n_c, pair_batch_size), pair_shift(3, pair_batch_size), &
2039 sum_re(pair_batch_size, n_off), sum_im(pair_batch_size, n_off), &
2040 weight_1(first_c(1):last_c(1)), weight_2(first_c(2):last_c(2)), weight_3(first_c(3):last_c(3)))
2041 !$OMP DO SCHEDULE(DYNAMIC)
2042 DO i_first = 1, SIZE(sigma_r, 1), pair_batch_size
2043 n_pairs = min(pair_batch_size, SIZE(sigma_r, 1) - i_first + 1)
2044 DO i_pair = 1, n_pairs
2045 CALL product_pair_shift(first_pair + i_first + i_pair - 2, n_grid, fractional_grid, shift, tie)
2046 pair_shift(:, i_pair) = shift
2047 weight_1(:) = [(image_weight(c1, nmesh(1), tie(1), boundary(1)), c1=first_c(1), last_c(1))]
2048 weight_2(:) = [(image_weight(c2, nmesh(2), tie(2), boundary(2)), c2=first_c(2), last_c(2))]
2049 weight_3(:) = [(image_weight(c3, nmesh(3), tie(3), boundary(3)), c3=first_c(3), last_c(3))]
2050 i_c = 0
2051 DO c3 = first_c(3), last_c(3)
2052 DO c2 = first_c(2), last_c(2)
2053 DO c1 = first_c(1), last_c(1)
2054 i_c = i_c + 1
2055 IF (weight_1(c1)*weight_2(c2)*weight_3(c3) == 0.0_dp) THEN
2056 images(i_c, i_pair) = 0.0_dp
2057 ELSE
2058 wrapped = modulo(shift + [c1, c2, c3] - lower, nmesh) + lower
2059 images(i_c, i_pair) = weight_1(c1)*weight_2(c2)*weight_3(c3)* &
2060 sigma_r(i_first + i_pair - 1, 1, 1 + wrapped(3) - lower(3) + &
2061 nmesh(3)*(wrapped(2) - lower(2) + nmesh(2)*(wrapped(1) - lower(1))))
2062 END IF
2063 END DO
2064 END DO
2065 END DO
2066 END DO
2067 CALL dgemm('T', 'N', n_pairs, n_off, n_c, 1.0_dp, images, n_c, cos_c, n_c, 0.0_dp, sum_re, pair_batch_size)
2068 CALL dgemm('T', 'N', n_pairs, n_off, n_c, 1.0_dp, images, n_c, sin_c, n_c, 0.0_dp, sum_im, pair_batch_size)
2069 DO i_off = 1, n_off
2070 DO i_pair = 1, n_pairs
2071 angle = twopi*dot_product(xk(:, i_off), real(pair_shift(:, i_pair), dp))
2072 sigma_k(i_first + i_pair - 1, offmesh_col(i_off)) = &
2073 exp(cmplx(0.0_dp, angle, kind=dp))*cmplx(sum_re(i_pair, i_off), sum_im(i_pair, i_off), kind=dp)
2074 END DO
2075 END DO
2076 END DO
2077 !$OMP END DO
2078 DEALLOCATE (images, pair_shift, sum_re, sum_im, weight_1, weight_2, weight_3)
2079 !$OMP END PARALLEL
2080 DEALLOCATE (cos_c, fractional_grid, offmesh_col, sin_c, xk)
2081 END SUBROUTINE band_self_energy
2082
2083! **************************************************************************************************
2084!> \brief Transform a periodic lattice product in a cell centred on each physical grid pair.
2085!> Products of two half-shifted lattice transforms are periodic. For s nearest to
2086!> H^{-1}(r_l-r_l'), F_ll'(k)=exp(2πiks) Σ_R exp(2πikR) F_ll'(R+s).
2087!> The even-mesh Nyquist endpoints and half-cell choices of s are averaged symmetrically.
2088!> On integer/half-shifted band meshes these averages are real separable endpoint weights,
2089!> allowing one FFT and one cell-vector scratch. values_R is consumed in place.
2090!> The rank owns the flattened column-major pairs first_pair, first_pair+1, ...
2091!> \param bs_env ...
2092!> \param cells ...
2093!> \param xkp ...
2094!> \param values_R ...
2095!> \param values_k ...
2096!> \param first_pair ...
2097! **************************************************************************************************
2098 SUBROUTINE product_to_k_pair_centered(bs_env, cells, xkp, values_R, values_k, first_pair)
2099 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
2100 INTEGER, INTENT(IN) :: cells(:, :)
2101 REAL(kind=dp), INTENT(IN) :: xkp(:, :)
2102 REAL(kind=dp), INTENT(INOUT) :: values_r(:, :, :)
2103 COMPLEX(KIND=dp), INTENT(OUT) :: values_k(:, :, :)
2104 INTEGER, INTENT(IN) :: first_pair
2105
2106 CHARACTER(LEN=*), PARAMETER :: routinen = 'product_to_k_pair_centered'
2107
2108 INTEGER :: handle, icell, idir, ik, ilocal, &
2109 isource, lower(3), ngrid, nmesh(3), &
2110 shift(3), wrapped(3)
2111 LOGICAL :: tie(3)
2112 REAL(kind=dp) :: endpoint(3), weight
2113 REAL(kind=dp), ALLOCATABLE :: fractional_grid(:, :), scratch(:)
2114
2115 CALL timeset(routinen, handle)
2116
2117 ngrid = bs_env%ri_rs%n_grid_points
2118 nmesh = bs_env%kpoints_scf_desymm%nkp_grid
2119 lower = -nmesh/2
2120 endpoint = cos(twopi*xkp(1:3, 1)*real(nmesh, dp))
2121 ALLOCATE (fractional_grid(3, ngrid))
2122 fractional_grid(:, :) = matmul(bs_env%ri_rs%cell%h_inv, bs_env%ri_rs%grid_points)
2123 DO idir = 1, 3
2124 IF (bs_env%periodic(idir) == 0) fractional_grid(idir, :) = 0.0_dp
2125 END DO
2126 cpassert(SIZE(values_r, 2) == 1)
2127 !$OMP PARALLEL DEFAULT(NONE) &
2128 !$OMP SHARED(values_R, cells, nmesh, lower, endpoint, first_pair, ngrid, fractional_grid) &
2129 !$OMP PRIVATE(ilocal, shift, tie, scratch, icell, wrapped, isource, weight, idir)
2130 ALLOCATE (scratch(SIZE(cells, 2)))
2131 !$OMP DO SCHEDULE(STATIC)
2132 DO ilocal = 1, SIZE(values_r, 1)
2133 CALL product_pair_shift(first_pair + ilocal - 1, ngrid, fractional_grid, shift, tie)
2134 scratch(:) = values_r(ilocal, 1, :)
2135 DO icell = 1, SIZE(cells, 2)
2136 wrapped = modulo(cells(:, icell) + shift - lower, nmesh) + lower
2137 isource = 1 + wrapped(3) - lower(3) + &
2138 nmesh(3)*(wrapped(2) - lower(2) + nmesh(2)*(wrapped(1) - lower(1)))
2139 weight = 1.0_dp
2140 DO idir = 1, 3
2141 IF (modulo(nmesh(idir), 2) == 0) THEN
2142 IF (cells(idir, icell) == lower(idir)) THEN
2143 IF (tie(idir)) THEN
2144 weight = weight*(1.0_dp + 3.0_dp*endpoint(idir))/4.0_dp
2145 ELSE
2146 weight = weight*(1.0_dp + endpoint(idir))/2.0_dp
2147 END IF
2148 ELSE IF (tie(idir) .AND. cells(idir, icell) == lower(idir) + 1) THEN
2149 weight = weight*(3.0_dp + endpoint(idir))/4.0_dp
2150 END IF
2151 ELSE IF (tie(idir) .AND. cells(idir, icell) == lower(idir)) THEN
2152 weight = weight*(1.0_dp + endpoint(idir))/2.0_dp
2153 END IF
2154 END DO
2155 values_r(ilocal, 1, icell) = weight*scratch(isource)
2156 END DO
2157 END DO
2158 !$OMP END DO
2159 DEALLOCATE (scratch)
2160 !$OMP END PARALLEL
2161 CALL cell_to_k_grid_fft(values_r, cells, xkp, nmesh, values_k)
2162 !$OMP PARALLEL DO DEFAULT(NONE) SCHEDULE(STATIC) &
2163 !$OMP SHARED(values_k, xkp, first_pair, ngrid, fractional_grid) &
2164 !$OMP PRIVATE(ilocal, shift, tie, ik)
2165 DO ilocal = 1, SIZE(values_k, 1)
2166 CALL product_pair_shift(first_pair + ilocal - 1, ngrid, fractional_grid, shift, tie)
2167 DO ik = 1, SIZE(xkp, 2)
2168 values_k(ilocal, 1, ik) = values_k(ilocal, 1, ik)* &
2169 exp(cmplx(0.0_dp, twopi*dot_product(xkp(1:3, ik), real(shift, dp)), dp))
2170 END DO
2171 END DO
2172 !$OMP END PARALLEL DO
2173
2174 CALL timestop(handle)
2175
2176 END SUBROUTINE product_to_k_pair_centered
2177
2178! **************************************************************************************************
2179!> \brief Select the lattice image of a grid-point pair and identify half-cell ties.
2180!> \param ipair flattened column-major pair index l+(l'-1)*ngrid
2181!> \param ngrid ...
2182!> \param fractional_grid ...
2183!> \param pair_shift ...
2184!> \param pair_tie ...
2185! **************************************************************************************************
2186 SUBROUTINE product_pair_shift(ipair, ngrid, fractional_grid, pair_shift, pair_tie)
2187 INTEGER, INTENT(IN) :: ipair, ngrid
2188 REAL(kind=dp), INTENT(IN) :: fractional_grid(:, :)
2189 INTEGER, INTENT(OUT) :: pair_shift(3)
2190 LOGICAL, INTENT(OUT) :: pair_tie(3)
2191
2192 INTEGER :: idir, il, ilp
2193 REAL(kind=dp) :: delta(3), half_boundary, tolerance
2194
2195 il = modulo(ipair - 1, ngrid) + 1
2196 ilp = (ipair - 1)/ngrid + 1
2197 delta = fractional_grid(:, il) - fractional_grid(:, ilp)
2198 pair_shift = nint(delta)
2199 pair_tie = .false.
2200 DO idir = 1, 3
2201 half_boundary = anint(delta(idir) - 0.5_dp) + 0.5_dp
2202 tolerance = 64.0_dp*epsilon(1.0_dp)*max(1.0_dp, abs(delta(idir)))
2203 IF (abs(delta(idir) - half_boundary) <= tolerance) THEN
2204 pair_shift(idir) = nint(half_boundary - 0.5_dp)
2205 pair_tie(idir) = .true.
2206 END IF
2207 END DO
2208 END SUBROUTINE product_pair_shift
2209
2210! **************************************************************************************************
2211!> \brief Cosine transform of one q block between imaginary time and frequency,
2212!> X_PQ(q,j) = Σ_i T_ji Y_PQ(q,i), as one GEMM over all (P,Q).
2213!> \param block Y on input, X on output
2214!> \param transform T, the real minimax cosine transform in either direction
2215! **************************************************************************************************
2216 SUBROUTINE cosine_transform_ri_q(block, transform)
2217 TYPE(ri_q_matrix_block_type), INTENT(INOUT) :: block
2218 REAL(kind=dp), INTENT(IN) :: transform(:, :)
2219
2220 CHARACTER(LEN=*), PARAMETER :: routinen = 'cosine_transform_ri_q'
2221
2222 COMPLEX(KIND=dp), ALLOCATABLE :: values(:, :, :)
2223 INTEGER :: handle, n_elements
2224
2225 CALL timeset(routinen, handle)
2226
2227 cpassert(SIZE(transform, 2) == SIZE(block%values, 3))
2228 n_elements = SIZE(block%values, 1)*SIZE(block%values, 2)
2229 ALLOCATE (values(SIZE(block%values, 1), SIZE(block%values, 2), SIZE(transform, 1)))
2230 CALL zgemm('N', 'T', n_elements, SIZE(transform, 1), SIZE(transform, 2), z_one, block%values, &
2231 n_elements, cmplx(transform, kind=dp), SIZE(transform, 1), z_zero, values, n_elements)
2232 CALL move_alloc(values, block%values)
2233
2234 CALL timestop(handle)
2235
2236 END SUBROUTINE cosine_transform_ri_q
2237
2238! **************************************************************************************************
2239!> \brief Count live complex elements across independently allocated q blocks.
2240!> \param blocks ...
2241!> \return ...
2242! **************************************************************************************************
2243 FUNCTION ri_q_storage_elements(blocks) RESULT(nelements)
2244 TYPE(ri_q_matrix_block_type), INTENT(IN) :: blocks(:)
2245 INTEGER(KIND=int_8) :: nelements
2246
2247 INTEGER :: iq
2248
2249 nelements = 0_int_8
2250 DO iq = 1, SIZE(blocks)
2251 IF (ALLOCATED(blocks(iq)%values)) THEN
2252 nelements = nelements + SIZE(blocks(iq)%values, kind=int_8)
2253 END IF
2254 END DO
2255 END FUNCTION ri_q_storage_elements
2256
2257! **************************************************************************************************
2258!> \brief Real part of the inverse lattice transform X(R) = Σ_k e^(-i2πk·R) X(k)/N_k.
2259!> On a regular k grid, one FFT plan is shared and the matrix elements are distributed
2260!> over OpenMP workers with private buffers, as cell_to_k_grid_fft does for k ← R.
2261!> Other k sets use the serial inverse lattice transform row by row.
2262!> \param values_k Reciprocal-space matrices.
2263!> \param xkp Fractional k points.
2264!> \param nkp_grid Mesh dimensions.
2265!> \param cells Lattice vectors.
2266!> \param values_R Real lattice matrices.
2267! **************************************************************************************************
2268 SUBROUTINE gw_k_grid_to_real_cells(values_k, xkp, nkp_grid, cells, values_R)
2269 COMPLEX(KIND=dp), INTENT(IN) :: values_k(:, :, :)
2270 REAL(kind=dp), INTENT(IN) :: xkp(:, :)
2271 INTEGER, INTENT(IN) :: nkp_grid(3), cells(:, :)
2272 REAL(kind=dp), INTENT(OUT) :: values_r(:, :, :)
2273
2274 CHARACTER(LEN=*), PARAMETER :: routinen = 'gw_k_grid_to_real_cells'
2275
2276 TYPE fft_buffer_type
2277 COMPLEX(KIND=dp), CONTIGUOUS, POINTER :: zin(:, :, :), zout(:, :, :)
2278 END TYPE fft_buffer_type
2279
2280 COMPLEX(KIND=dp), ALLOCATABLE :: cell_phase(:), row_r(:, :, :)
2281 COMPLEX(KIND=dp), CONTIGUOUS, POINTER :: fft_in(:, :, :), fft_out(:, :, :)
2282 INTEGER :: d, handle, i, icell, ik, irow, ithread, &
2283 j, local_stat, nfft(3), nthreads, stat
2284 INTEGER, ALLOCATABLE :: cell_index(:, :), grid_index(:, :)
2285 LOGICAL :: regular
2286 REAL(kind=dp) :: k_offset(3), weight
2287 TYPE(fft_buffer_type), ALLOCATABLE :: buffers(:)
2288 TYPE(fft_plan_type) :: plan
2289 TYPE(k_grid_to_cell_work_type) :: work
2290
2291 CALL timeset(routinen, handle)
2292 ALLOCATE (grid_index(3, SIZE(xkp, 2)))
2293 regular = regular_kpoint_grid(xkp, nkp_grid, grid_index, k_offset)
2294 IF (regular) regular = lattice_fft_shape(nkp_grid, nfft)
2295 stat = 1
2296 IF (regular) THEN
2297 weight = 1.0_dp/real(SIZE(xkp, 2), dp)
2298 ALLOCATE (cell_index(3, SIZE(cells, 2)), cell_phase(SIZE(cells, 2)))
2299 DO icell = 1, SIZE(cells, 2)
2300 DO d = 1, 3
2301 cell_index(d, icell) = modulo(cells(d, icell), nkp_grid(d))*(nfft(d)/nkp_grid(d)) + 1
2302 END DO
2303 cell_phase(icell) = exp(-gaussi*twopi*sum(k_offset*real(cells(1:3, icell), dp)))
2304 END DO
2305 nthreads = lattice_fft_num_threads(nfft, SIZE(values_r, 1)*SIZE(values_r, 2))
2306 ALLOCATE (buffers(nthreads))
2307 DO ithread = 1, nthreads
2308 CALL fft_alloc(buffers(ithread)%zin, nfft)
2309 CALL fft_alloc(buffers(ithread)%zout, nfft)
2310 END DO
2311 stat = 0
2312!$OMP PARALLEL DEFAULT(NONE) NUM_THREADS(nthreads) &
2313!$OMP SHARED(buffers, plan, nfft, values_k, values_R, grid_index, cell_index, cell_phase, weight) &
2314!$OMP PRIVATE(ithread, fft_in, fft_out, i, j, ik, icell, local_stat) REDUCTION(MAX:stat)
2315 ! Each worker executes serial FFTs; this ICV change is scoped to its implicit task.
2316!$ CALL omp_set_num_threads(1)
2317!$OMP SINGLE
2318 CALL fft_create_plan_3d(plan, .false., fwfft, nfft, buffers(1)%zin, buffers(1)%zout)
2319!$OMP END SINGLE
2320 ithread = 1
2321!$ ithread = omp_get_thread_num() + 1
2322 fft_in => buffers(ithread)%zin
2323 fft_out => buffers(ithread)%zout
2324!$OMP DO COLLAPSE(2) SCHEDULE(STATIC)
2325 DO j = 1, SIZE(values_r, 2)
2326 DO i = 1, SIZE(values_r, 1)
2327 fft_in = z_zero
2328 DO ik = 1, SIZE(values_k, 3)
2329 fft_in(grid_index(1, ik), grid_index(2, ik), grid_index(3, ik)) = weight*values_k(i, j, ik)
2330 END DO
2331 CALL fft_3d(plan, 1.0_dp, fft_in, fft_out, local_stat)
2332 stat = max(stat, local_stat)
2333 IF (local_stat /= 0) cycle
2334 DO icell = 1, SIZE(values_r, 3)
2335 values_r(i, j, icell) = real(cell_phase(icell)* &
2336 fft_out(cell_index(1, icell), cell_index(2, icell), &
2337 cell_index(3, icell)), dp)
2338 END DO
2339 END DO
2340 END DO
2341!$OMP END DO
2342!$OMP END PARALLEL
2343 CALL fft_destroy_plan(plan)
2344 DO ithread = 1, nthreads
2345 CALL fft_dealloc(buffers(ithread)%zin)
2346 CALL fft_dealloc(buffers(ithread)%zout)
2347 END DO
2348 DEALLOCATE (buffers, cell_index, cell_phase)
2349 END IF
2350 IF (stat /= 0) THEN
2351 ALLOCATE (row_r(1, SIZE(values_r, 2), SIZE(values_r, 3)))
2352 CALL k_grid_to_cell_prepare(work, xkp, nkp_grid, cells)
2353 DO irow = 1, SIZE(values_r, 1)
2354 CALL k_grid_to_cell_execute(work, values_k(irow:irow, :, :), row_r)
2355 values_r(irow, :, :) = real(row_r(1, :, :), dp)
2356 END DO
2357 CALL k_grid_to_cell_release(work)
2358 DEALLOCATE (row_r)
2359 END IF
2360 DEALLOCATE (grid_index)
2361 CALL timestop(handle)
2362
2363 END SUBROUTINE gw_k_grid_to_real_cells
2364
2365! **************************************************************************************************
2366!> \brief Transfer q-local full matrices to owners of ordered grid pairs.
2367!> Ranks own q points cyclically, MOD(q,P), and ordered grid pairs as contiguous blocks. Round r
2368!> moves q = (r-1)P+1, ..., rP, the r-th local q of every rank, with one alltoall.
2369!> \param q_matrices ...
2370!> \param para_env ...
2371!> \param fft_pairs ...
2372! **************************************************************************************************
2373 SUBROUTINE q_matrices_to_fft_pairs(q_matrices, para_env, fft_pairs)
2374 COMPLEX(KIND=dp), INTENT(IN) :: q_matrices(:, :, :)
2375 TYPE(mp_para_env_type), POINTER :: para_env
2376 COMPLEX(KIND=dp), INTENT(OUT) :: fft_pairs(:, :, :)
2377
2378 CHARACTER(LEN=*), PARAMETER :: routinen = 'q_matrices_to_fft_pairs'
2379
2380 COMPLEX(KIND=dp), ALLOCATABLE :: received(:), sent(:)
2381 INTEGER :: handle, i_first, i_rank, i_round, &
2382 n_grid, n_local, n_q, n_rank, n_round
2383 INTEGER, ALLOCATABLE :: pair_counts(:), pair_displs(:), &
2384 recv_counts(:), recv_displs(:), &
2385 send_counts(:)
2386
2387 CALL timeset(routinen, handle)
2388
2389 n_rank = para_env%num_pe
2390 n_grid = SIZE(q_matrices, 1)
2391 n_local = SIZE(fft_pairs, 1)
2392 n_q = SIZE(fft_pairs, 3)
2393 cpassert(SIZE(fft_pairs, 2) == 1)
2394 CALL grid_pair_blocks(n_grid, n_rank, pair_counts, pair_displs)
2395 ALLOCATE (sent(n_grid**2), received(n_local*n_rank), recv_counts(0:n_rank - 1), &
2396 recv_displs(0:n_rank - 1), send_counts(0:n_rank - 1))
2397 recv_displs(:) = [(modulo(i_rank - 1, n_rank)*n_local, i_rank=0, n_rank - 1)]
2398 DO i_round = 1, (n_q + n_rank - 1)/n_rank
2399 i_first = (i_round - 1)*n_rank
2400 n_round = min(n_rank, n_q - i_first)
2401 DO i_rank = 0, n_rank - 1
2402 recv_counts(i_rank) = merge(n_local, 0, modulo(i_rank - 1, n_rank) < n_round)
2403 END DO
2404 send_counts(:) = 0
2405 IF (modulo(para_env%mepos - 1, n_rank) < n_round) THEN
2406 sent(:) = reshape(q_matrices(:, :, i_round), [n_grid**2])
2407 send_counts(:) = pair_counts
2408 END IF
2409 CALL para_env%alltoall(sent, send_counts, pair_displs, received, recv_counts, recv_displs)
2410 fft_pairs(:, 1, i_first + 1:i_first + n_round) = reshape(received(1:n_local*n_round), [n_local, n_round])
2411 END DO
2412
2413 CALL timestop(handle)
2414
2415 END SUBROUTINE q_matrices_to_fft_pairs
2416
2417! **************************************************************************************************
2418!> \brief Transfer ordered grid pairs to the q owners as full matrices, the reverse of
2419!> q_matrices_to_fft_pairs.
2420!> \param fft_pairs ...
2421!> \param para_env ...
2422!> \param needed q points to transfer
2423!> \param q_matrices needed q points of this rank, MOD(q,P), in ascending order
2424! **************************************************************************************************
2425 SUBROUTINE fft_pairs_to_q_matrices(fft_pairs, para_env, needed, q_matrices)
2426 COMPLEX(KIND=dp), INTENT(IN) :: fft_pairs(:, :, :)
2427 TYPE(mp_para_env_type), POINTER :: para_env
2428 LOGICAL, INTENT(IN) :: needed(:)
2429 COMPLEX(KIND=dp), INTENT(OUT) :: q_matrices(:, :, :)
2430
2431 CHARACTER(LEN=*), PARAMETER :: routinen = 'fft_pairs_to_q_matrices'
2432
2433 COMPLEX(KIND=dp), ALLOCATABLE :: received(:), sent(:)
2434 INTEGER :: handle, i_first, i_local, i_rank, &
2435 i_round, iq, n_grid, n_local, n_q, &
2436 n_rank, n_round
2437 INTEGER, ALLOCATABLE :: pair_counts(:), pair_displs(:), &
2438 recv_counts(:), send_counts(:), &
2439 send_displs(:)
2440 LOGICAL :: receiving
2441
2442 CALL timeset(routinen, handle)
2443
2444 n_rank = para_env%num_pe
2445 n_grid = SIZE(q_matrices, 1)
2446 n_local = SIZE(fft_pairs, 1)
2447 n_q = SIZE(fft_pairs, 3)
2448 cpassert(SIZE(fft_pairs, 2) == 1 .AND. SIZE(needed) == n_q)
2449 CALL grid_pair_blocks(n_grid, n_rank, pair_counts, pair_displs)
2450 ALLOCATE (sent(n_local*n_rank), received(n_grid**2), recv_counts(0:n_rank - 1), &
2451 send_counts(0:n_rank - 1), send_displs(0:n_rank - 1))
2452 send_displs(:) = [(modulo(i_rank - 1, n_rank)*n_local, i_rank=0, n_rank - 1)]
2453 i_local = 0
2454 DO i_round = 1, (n_q + n_rank - 1)/n_rank
2455 i_first = (i_round - 1)*n_rank
2456 n_round = min(n_rank, n_q - i_first)
2457 send_counts(:) = 0
2458 DO i_rank = 0, n_rank - 1
2459 iq = i_first + modulo(i_rank - 1, n_rank) + 1
2460 IF (iq > n_q) cycle
2461 IF (needed(iq)) send_counts(i_rank) = n_local
2462 END DO
2463 sent(1:n_local*n_round) = reshape(fft_pairs(:, 1, i_first + 1:i_first + n_round), [n_local*n_round])
2464 iq = i_first + modulo(para_env%mepos - 1, n_rank) + 1
2465 receiving = iq <= n_q
2466 IF (receiving) receiving = needed(iq)
2467 recv_counts(:) = 0
2468 IF (receiving) recv_counts(:) = pair_counts
2469 CALL para_env%alltoall(sent, send_counts, send_displs, received, recv_counts, pair_displs)
2470 IF (receiving) THEN
2471 i_local = i_local + 1
2472 q_matrices(:, :, i_local) = reshape(received, [n_grid, n_grid])
2473 END IF
2474 END DO
2475 cpassert(i_local == SIZE(q_matrices, 3))
2476
2477 CALL timestop(handle)
2478
2479 END SUBROUTINE fft_pairs_to_q_matrices
2480
2481! **************************************************************************************************
2482!> \brief Form W_ll'(q,iτ) = Σ_PQ Z_lP(q) W_PQ(q,iτ) Z_l'Q(q)* and transfer FFT pairs.
2483!> \param Z_q ...
2484!> \param interaction_q W_PQ(q,iτ) of the locally owned q
2485!> \param itime ...
2486!> \param para_env ...
2487!> \param fft_pairs ...
2488! **************************************************************************************************
2489 SUBROUTINE project_auxiliary_time_q_to_fft_pairs(Z_q, interaction_q, itime, para_env, fft_pairs)
2490 COMPLEX(KIND=dp), INTENT(IN) :: z_q(:, :, :)
2491 TYPE(ri_q_matrix_block_type), INTENT(IN) :: interaction_q(:)
2492 INTEGER, INTENT(IN) :: itime
2493 TYPE(mp_para_env_type), POINTER :: para_env
2494 COMPLEX(KIND=dp), INTENT(OUT) :: fft_pairs(:, :, :)
2495
2496 CHARACTER(LEN=*), PARAMETER :: routinen = 'project_auxiliary_time_q_to_fft_pairs'
2497
2498 COMPLEX(KIND=dp), ALLOCATABLE :: q_matrices(:, :, :), zw(:, :)
2499 INTEGER :: handle, i_q_local, n_grid, n_q_local, &
2500 n_ri
2501
2502 CALL timeset(routinen, handle)
2503
2504 n_grid = SIZE(z_q, 1)
2505 n_ri = SIZE(z_q, 2)
2506 n_q_local = SIZE(z_q, 3)
2507 cpassert(SIZE(interaction_q) == n_q_local)
2508 ALLOCATE (q_matrices(n_grid, n_grid, n_q_local))
2509 !$OMP PARALLEL DEFAULT(NONE) SHARED(Z_q, interaction_q, itime, q_matrices, n_q_local, n_grid, n_RI) &
2510 !$OMP PRIVATE(i_q_local, ZW)
2511 ALLOCATE (zw(n_grid, n_ri))
2512 !$OMP DO SCHEDULE(STATIC)
2513 DO i_q_local = 1, n_q_local
2514 CALL zgemm('N', 'N', n_grid, n_ri, n_ri, z_one, z_q(:, :, i_q_local), n_grid, &
2515 interaction_q(i_q_local)%values(:, :, itime), n_ri, z_zero, zw, n_grid)
2516 CALL zgemm('N', 'C', n_grid, n_grid, n_ri, z_one, zw, n_grid, z_q(:, :, i_q_local), n_grid, &
2517 z_zero, q_matrices(:, :, i_q_local), n_grid)
2518 END DO
2519 !$OMP END DO
2520 DEALLOCATE (zw)
2521 !$OMP END PARALLEL
2522 CALL q_matrices_to_fft_pairs(q_matrices, para_env, fft_pairs)
2523
2524 CALL timestop(handle)
2525
2526 END SUBROUTINE project_auxiliary_time_q_to_fft_pairs
2527
2528! **************************************************************************************************
2529!> \brief ...
2530!> \param distributed ...
2531!> \param owner_matrix ...
2532!> \param group_env ...
2533!> \param root ...
2534!> \param n ...
2535! **************************************************************************************************
2536 SUBROUTINE set_owner_matrix(distributed, owner_matrix, group_env, root, n)
2537 TYPE(cp_cfm_type), INTENT(IN) :: distributed
2538 COMPLEX(KIND=dp), INTENT(IN) :: owner_matrix(:, :)
2539 TYPE(mp_para_env_type), INTENT(INOUT) :: group_env
2540 INTEGER, INTENT(IN) :: root, n
2541
2542 COMPLEX(KIND=dp), ALLOCATABLE :: column_block(:, :)
2543 INTEGER :: first, width
2544
2545 IF (group_env%mepos == root) THEN
2546 cpassert(all(shape(owner_matrix) == [n, n]))
2547 END IF
2548 ALLOCATE (column_block(n, column_block_cols))
2549 DO first = 1, n, column_block_cols
2550 width = min(column_block_cols, n - first + 1)
2551 IF (group_env%mepos == root) column_block(:, 1:width) = owner_matrix(:, first:first + width - 1)
2552 CALL group_env%bcast(column_block(:, 1:width), root)
2553 CALL cp_cfm_set_submatrix(distributed, column_block(:, 1:width), &
2554 start_row=1, start_col=first)
2555 END DO
2556 DEALLOCATE (column_block)
2557 END SUBROUTINE set_owner_matrix
2558
2559! **************************************************************************************************
2560!> \brief ...
2561!> \param distributed ...
2562!> \param owner_matrix ...
2563!> \param group_env ...
2564!> \param root ...
2565!> \param n ...
2566! **************************************************************************************************
2567 SUBROUTINE get_owner_matrix(distributed, owner_matrix, group_env, root, n)
2568 TYPE(cp_cfm_type), INTENT(IN) :: distributed
2569 COMPLEX(KIND=dp), INTENT(OUT) :: owner_matrix(:, :)
2570 TYPE(mp_para_env_type), INTENT(INOUT) :: group_env
2571 INTEGER, INTENT(IN) :: root, n
2572
2573 COMPLEX(KIND=dp), ALLOCATABLE :: column_block(:, :)
2574 INTEGER :: first, width
2575
2576 IF (group_env%mepos == root) THEN
2577 cpassert(all(shape(owner_matrix) == [n, n]))
2578 END IF
2579 ALLOCATE (column_block(n, column_block_cols))
2580 DO first = 1, n, column_block_cols
2581 width = min(column_block_cols, n - first + 1)
2582 CALL cp_cfm_get_submatrix(distributed, column_block(:, 1:width), &
2583 start_row=1, start_col=first)
2584 IF (group_env%mepos == root) owner_matrix(:, first:first + width - 1) = column_block(:, 1:width)
2585 END DO
2586 DEALLOCATE (column_block)
2587 END SUBROUTINE get_owner_matrix
2588
2589! **************************************************************************************************
2590!> \brief Find -q in the same mesh for every q; q is its own partner where -q ≡ q.
2591!> \param xkp q points in fractional coordinates
2592!> \param q_partner index of -q for every q
2593! **************************************************************************************************
2594 SUBROUTINE minus_q_partners(xkp, q_partner)
2595 REAL(kind=dp), INTENT(IN) :: xkp(:, :)
2596 INTEGER, ALLOCATABLE, INTENT(OUT) :: q_partner(:)
2597
2598 INTEGER :: ikp, jkp
2599 REAL(kind=dp) :: q_sum(3)
2600
2601 ALLOCATE (q_partner(SIZE(xkp, 2)), source=0)
2602 DO ikp = 1, SIZE(xkp, 2)
2603 DO jkp = 1, SIZE(xkp, 2)
2604 q_sum(:) = xkp(:, ikp) + xkp(:, jkp)
2605 IF (all(abs(q_sum - anint(q_sum)) < eps_kpoint)) THEN
2606 q_partner(ikp) = jkp
2607 EXIT
2608 END IF
2609 END DO
2610 cpassert(q_partner(ikp) > 0)
2611 END DO
2612 END SUBROUTINE minus_q_partners
2613
2614! **************************************************************************************************
2615!> \brief Give the owner of every unsolved q the block of its partner, W(q,τ) = W(-q,τ)*.
2616!> \param para_env ...
2617!> \param q_partner index of -q for every q
2618!> \param solves_q whether W(q) was solved; then the owner of q holds its block
2619!> \param n_RI ...
2620!> \param n_time ...
2621!> \param blocks W(q,τ) of the q points owned by this rank
2622! **************************************************************************************************
2623 SUBROUTINE receive_minus_q_blocks(para_env, q_partner, solves_q, n_RI, n_time, blocks)
2624 TYPE(mp_para_env_type), POINTER :: para_env
2625 INTEGER, INTENT(IN) :: q_partner(:)
2626 LOGICAL, INTENT(IN) :: solves_q(:)
2627 INTEGER, INTENT(IN) :: n_ri, n_time
2628 TYPE(ri_q_matrix_block_type), INTENT(INOUT) :: blocks(:)
2629
2630 INTEGER :: destination, ikp, n_requests, &
2631 partner_local, q_local, source
2632 TYPE(mp_request_type), ALLOCATABLE :: requests(:)
2633
2634 ! A rank sends at most its solved and receives at most its unsolved blocks.
2635 ALLOCATE (requests(SIZE(blocks)))
2636 n_requests = 0
2637 DO ikp = 1, SIZE(q_partner)
2638 IF (.NOT. solves_q(ikp) .OR. q_partner(ikp) == ikp) cycle
2639 source = modulo(ikp, para_env%num_pe)
2640 destination = modulo(q_partner(ikp), para_env%num_pe)
2641 q_local = 1 + (ikp - 1)/para_env%num_pe
2642 partner_local = 1 + (q_partner(ikp) - 1)/para_env%num_pe
2643 IF (para_env%mepos == destination) THEN
2644 ALLOCATE (blocks(partner_local)%values(n_ri, n_ri, n_time))
2645 IF (source == destination) THEN
2646 blocks(partner_local)%values(:, :, :) = blocks(q_local)%values
2647 ELSE
2648 n_requests = n_requests + 1
2649 CALL para_env%irecv(blocks(partner_local)%values, source, requests(n_requests), tag=q_local)
2650 END IF
2651 ELSE IF (para_env%mepos == source) THEN
2652 n_requests = n_requests + 1
2653 CALL para_env%isend(blocks(q_local)%values, destination, requests(n_requests), tag=q_local)
2654 END IF
2655 END DO
2656 CALL mp_waitall(requests(1:n_requests))
2657 DO ikp = para_env%mepos, SIZE(q_partner), para_env%num_pe
2658 IF (ikp == 0) cycle
2659 IF (.NOT. solves_q(ikp)) THEN
2660 q_local = 1 + (ikp - 1)/para_env%num_pe
2661 blocks(q_local)%values(:, :, :) = conjg(blocks(q_local)%values)
2662 END IF
2663 END DO
2664 END SUBROUTINE receive_minus_q_blocks
2665
2666! **************************************************************************************************
2667!> \brief Partition MPI ranks into balanced contiguous q groups.
2668!> \param para_env ...
2669!> \param n_qgroups ...
2670!> \param my_qgroup ...
2671!> \param q_roots ...
2672! **************************************************************************************************
2673 SUBROUTINE grouped_w_partition(para_env, n_qgroups, my_qgroup, q_roots)
2674 TYPE(mp_para_env_type), POINTER :: para_env
2675 INTEGER, INTENT(IN) :: n_qgroups
2676 INTEGER, INTENT(OUT) :: my_qgroup
2677 INTEGER, ALLOCATABLE, INTENT(OUT) :: q_roots(:)
2678
2679 INTEGER :: i_first, i_group, i_last
2680
2681 cpassert(n_qgroups > 0 .AND. n_qgroups <= para_env%num_pe)
2682 ALLOCATE (q_roots(n_qgroups))
2683 DO i_group = 0, n_qgroups - 1
2684 i_first = (para_env%num_pe*i_group)/n_qgroups
2685 i_last = (para_env%num_pe*(i_group + 1))/n_qgroups - 1
2686 q_roots(i_group + 1) = i_first
2687 IF (para_env%mepos >= i_first .AND. para_env%mepos <= i_last) my_qgroup = i_group
2688 END DO
2689
2690 END SUBROUTINE grouped_w_partition
2691
2692! **************************************************************************************************
2693!> \brief ...
2694!> \param matrices ...
2695!> \param blacs ...
2696!> \param matrix_struct ...
2697!> \param group_env ...
2698!> \param n ...
2699!> Construct block-cyclic matrices on the MPI ranks assigned to a q group.
2700! **************************************************************************************************
2701 SUBROUTINE grouped_w_create(matrices, blacs, matrix_struct, group_env, n)
2702 TYPE(cp_cfm_type), INTENT(INOUT) :: matrices(n_w_matrices)
2703 TYPE(cp_blacs_env_type), POINTER :: blacs
2704 TYPE(cp_fm_struct_type), POINTER :: matrix_struct
2705 TYPE(mp_para_env_type), INTENT(INOUT), TARGET :: group_env
2706 INTEGER, INTENT(IN) :: n
2707
2708 cpassert(n > 0)
2709 CALL cp_blacs_env_create(blacs, group_env)
2710 CALL cp_fm_struct_create(matrix_struct, context=blacs, &
2711 nrow_global=n, ncol_global=n)
2712 CALL cp_cfm_create(matrices(w_chi), matrix_struct)
2713 CALL cp_cfm_create(matrices(w_metric), matrix_struct)
2714 CALL cp_cfm_create(matrices(w_tmp), matrix_struct)
2715 CALL cp_cfm_create(matrices(w_dielectric), matrix_struct)
2716 CALL cp_cfm_create(matrices(w_output), matrix_struct)
2717 END SUBROUTINE grouped_w_create
2718
2719! **************************************************************************************************
2720!> \brief ...
2721!> \param world ...
2722!> \param n ...
2723!> \param source_rank ...
2724!> \param destination_rank ...
2725!> \param source ...
2726!> \param destination ...
2727!> Transfer a matrix between world owners without a full-size communication buffer.
2728! **************************************************************************************************
2729 SUBROUTINE grouped_w_copy_matrix(world, n, source_rank, destination_rank, source, destination)
2730 TYPE(mp_para_env_type), INTENT(INOUT) :: world
2731 INTEGER, INTENT(IN) :: n, source_rank, destination_rank
2732 COMPLEX(KIND=dp), INTENT(IN) :: source(:, :)
2733 COMPLEX(KIND=dp), INTENT(INOUT) :: destination(:, :)
2734
2735 INTEGER :: first, last, sender, tag
2736
2737 IF (source_rank == destination_rank) THEN
2738 IF (world%mepos == source_rank) destination = source
2739 RETURN
2740 END IF
2741 DO first = 1, n, column_block_cols
2742 last = min(first + column_block_cols - 1, n)
2743 IF (world%mepos == source_rank) THEN
2744 CALL world%send(source(:, first:last), destination_rank, 0)
2745 ELSE IF (world%mepos == destination_rank) THEN
2746 sender = source_rank
2747 tag = 0
2748 CALL world%recv(destination(:, first:last), sender, tag)
2749 END IF
2750 END DO
2751 END SUBROUTINE grouped_w_copy_matrix
2752
2753! **************************************************************************************************
2754!> \brief Evaluate screened W with a distributed positive-definite dielectric inverse.
2755!> For ε=I+BᴴχB after the unchanged positive χ filter, Cholesky gives ε^-1.
2756!> Reuse K=M^-1 V^1/2 to form W=K(ε^-1-I)Kᴴ.
2757!> \param matrices ...
2758!> \param group_env ...
2759!> \param n ...
2760!> \param root ...
2761!> \param chi_owner ...
2762!> \param cutoff ...
2763!> \param output_owner ...
2764! **************************************************************************************************
2765 SUBROUTINE grouped_w_solve(matrices, group_env, n, root, chi_owner, cutoff, output_owner)
2766 TYPE(cp_cfm_type), INTENT(INOUT) :: matrices(n_w_matrices)
2767 TYPE(mp_para_env_type), INTENT(INOUT) :: group_env
2768 INTEGER, INTENT(IN) :: n, root
2769 COMPLEX(KIND=dp), INTENT(IN) :: chi_owner(:, :)
2770 REAL(kind=dp), INTENT(IN) :: cutoff
2771 COMPLEX(KIND=dp), INTENT(OUT) :: output_owner(:, :)
2772
2773 INTEGER :: info
2774
2775 cpassert(root >= 0 .AND. root < group_env%num_pe)
2776 CALL set_owner_matrix(matrices(w_chi), chi_owner, group_env, root, n)
2777
2778 ! Match the dense reference: symmetrize before each Hermitian eigensolve.
2779 CALL cp_cfm_transpose(matrices(w_chi), 'C', matrices(w_output))
2780 CALL cp_cfm_scale_and_add(cmplx(0.5_dp, 0.0_dp, kind=dp), matrices(w_chi), &
2781 cmplx(0.5_dp, 0.0_dp, kind=dp), matrices(w_output))
2782 CALL cp_cfm_power(matrices(w_chi), exponent=1.0_dp, threshold=cutoff)
2783 CALL parallel_gemm('C', 'N', n, n, n, z_one, matrices(w_metric), &
2784 matrices(w_chi), z_zero, matrices(w_tmp))
2785 CALL parallel_gemm('N', 'N', n, n, n, z_one, matrices(w_tmp), &
2786 matrices(w_metric), z_zero, matrices(w_dielectric))
2787 CALL cp_cfm_add_on_diag(matrices(w_dielectric), z_one)
2788 CALL cp_cfm_transpose(matrices(w_dielectric), 'C', matrices(w_output))
2789 CALL cp_cfm_scale_and_add(cmplx(0.5_dp, 0.0_dp, kind=dp), matrices(w_dielectric), &
2790 cmplx(0.5_dp, 0.0_dp, kind=dp), matrices(w_output))
2791 ! χ is positive semidefinite after filtering: ε=I+BᴴχB has λ_min>=1.
2792 CALL cp_cfm_cholesky_decompose(matrices(w_dielectric), info_out=info)
2793 IF (info /= 0) THEN
2794 cpabort("Screened dielectric Cholesky factorization failed")
2795 END IF
2796 CALL cp_cfm_cholesky_invert(matrices(w_dielectric), info_out=info)
2797 IF (info /= 0) cpabort("Screened dielectric Cholesky inversion failed")
2798 CALL cp_cfm_uplo_to_full(matrices(w_dielectric), matrices(w_output), uplo='U')
2799 CALL cp_cfm_add_on_diag(matrices(w_dielectric), -z_one)
2800 ! With K=M^-1 V^1/2 and Hermitian M^-1, W=K(ε^-1-I)Kᴴ.
2801 ! The existing factor removes two matrix products at every later frequency.
2802 CALL parallel_gemm('N', 'N', n, n, n, z_one, matrices(w_metric), &
2803 matrices(w_dielectric), z_zero, matrices(w_tmp))
2804 CALL parallel_gemm('N', 'C', n, n, n, z_one, matrices(w_tmp), &
2805 matrices(w_metric), z_zero, matrices(w_output))
2806 CALL get_owner_matrix(matrices(w_output), output_owner, group_env, root, n)
2807 END SUBROUTINE grouped_w_solve
2808
2809! **************************************************************************************************
2810!> \brief Release the distributed dielectric workspaces for a q-point group.
2811!> \param matrices Distributed matrix storage.
2812!> \param matrix_struct ...
2813!> \param blacs ...
2814! **************************************************************************************************
2815 SUBROUTINE grouped_w_release(matrices, matrix_struct, blacs)
2816 TYPE(cp_cfm_type), INTENT(INOUT) :: matrices(n_w_matrices)
2817 TYPE(cp_fm_struct_type), POINTER :: matrix_struct
2818 TYPE(cp_blacs_env_type), POINTER :: blacs
2819
2820 CALL cp_cfm_release(matrices(w_chi))
2821 CALL cp_cfm_release(matrices(w_metric))
2822 CALL cp_cfm_release(matrices(w_tmp))
2823 CALL cp_cfm_release(matrices(w_dielectric))
2824 CALL cp_cfm_release(matrices(w_output))
2825 CALL cp_fm_struct_release(matrix_struct)
2826 CALL cp_blacs_env_release(blacs)
2827 END SUBROUTINE grouped_w_release
2828
2829! **************************************************************************************************
2830!> \brief Release all periodic RI-RS work data after the last quasiparticle iteration.
2831!> \param bs_env Band-structure environment owning the work data.
2832! **************************************************************************************************
2833 SUBROUTINE release_gw_work(bs_env)
2834 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
2835
2836 CHARACTER(LEN=*), PARAMETER :: routinen = 'release_gw_work'
2837
2838 INTEGER :: handle
2839 TYPE(gwkp_type), POINTER :: gwkp_env
2840
2841 CALL timeset(routinen, handle)
2842
2843 gwkp_env => bs_env%gwkp_env
2844 CALL mp_para_env_release(gwkp_env%group_env)
2845 IF (ALLOCATED(gwkp_env%eigenvalues_G)) DEALLOCATE (gwkp_env%eigenvalues_G)
2846 ! AO image pointers are released immediately after the Gamma contraction.
2847 cpassert(.NOT. ASSOCIATED(gwkp_env%gamma_corr_d))
2848 cpassert(.NOT. ASSOCIATED(gwkp_env%gamma_corr_h))
2849 cpassert(.NOT. ASSOCIATED(gwkp_env%gamma_corr_s))
2850 cpassert(.NOT. ASSOCIATED(gwkp_env%gamma_corr_index_to_cell))
2851 cpassert(.NOT. ASSOCIATED(gwkp_env%gamma_corr_cell_to_index))
2852 DEALLOCATE (bs_env%gwkp_env)
2853 NULLIFY (gwkp_env)
2854
2855 CALL timestop(handle)
2856
2857 END SUBROUTINE release_gw_work
2858
static GRID_HOST_DEVICE int modulo(int a, int m)
Equivalent of Fortran's MODULO, which always return a positive number. https://gcc....
static void dgemm(const char transa, const char transb, const int m, const int n, const int k, const double alpha, const double *a, const int lda, const double *b, const int ldb, const double beta, double *c, const int ldc)
Convenient wrapper to hide Fortran nature of dgemm_, swapping a and b.
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
Basic linear algebra operations for complex full matrices.
subroutine, public cp_cfm_scale_and_add(alpha, matrix_a, beta, matrix_b)
Scale and add two BLACS matrices (a = alpha*a + beta*b).
subroutine, public cp_cfm_transpose(matrix, trans, matrixt)
Transposes a BLACS distributed complex matrix.
subroutine, public cp_cfm_uplo_to_full(matrix, workspace, uplo)
...
subroutine, public cp_cfm_add_on_diag(matrix, alpha, n_active)
Adds a scalar to the diagonal of a distributed complex full matrix.
various cholesky decomposition related routines
subroutine, public cp_cfm_cholesky_decompose(matrix, n, info_out)
Used to replace a symmetric positive definite matrix M with its Cholesky decomposition U: M = U^T * U...
subroutine, public cp_cfm_cholesky_invert(matrix, n, info_out)
Used to replace Cholesky decomposition by the inverse.
used for collecting diagonalization schemes available for cp_cfm_type
Definition cp_cfm_diag.F:14
subroutine, public cp_cfm_power(matrix, work, exponent, threshold, n_dependent, verbose, eigvals)
Raise the positive spectral part of a Hermitian matrix to a real power. Eigenmodes with eigenvalues b...
Represents a complex full matrix distributed on many processors.
subroutine, public cp_cfm_get_submatrix(fm, target_m, start_row, start_col, n_rows, n_cols, transpose)
Extract a sub-matrix from the full matrix: op(target_m)(1:n_rows,1:n_cols) = fm(start_row:start_row+n...
subroutine, public cp_cfm_release(matrix)
Releases a full matrix.
subroutine, public cp_cfm_create(matrix, matrix_struct, name, nrow, ncol, set_zero)
Creates a new full matrix with the given structure.
subroutine, public cp_cfm_set_submatrix(matrix, new_values, start_row, start_col, n_rows, n_cols, alpha, beta, transpose)
Set a sub-matrix of the full matrix: matrix(start_row:start_row+n_rows,start_col:start_col+n_cols) = ...
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 fft_3d(plan, scale, zin, zout, stat)
...
Definition fft_lib.F:172
subroutine, public fft_destroy_plan(plan)
...
Definition fft_lib.F:256
subroutine, public fft_create_plan_3d(plan, fft_in_place, fsign, n, zin, zout)
...
Definition fft_lib.F:139
Type to store data about a (1D or 3D) FFT, including FFTW plan.
Definition fft_plan.F:18
integer, parameter, public fwfft
Definition fft_tools.F:146
Gamma-point (q = 0) correction of W and Σ for periodic RI-RS GW.
subroutine, public add_gamma_corr_cell_correction(bs_env, cells, model, i_t, z_head, first_pair, w_grid_r)
Add the Gamma-correction head and wings of one time point to the local grid pairs....
subroutine, public singular_head_cell_factors(bs_env, cells, factors)
Integrate 1/q**2 (3D) or 1/|q| (2D slab) over the distinct shifted-mesh cells around Gamma....
subroutine, public gamma_point_correction(bs_env, qs_env, gwkp_env)
Gamma-correction head and wing coefficients for the near-Gamma cells. Construct the q->0 dielectric l...
subroutine, public add_gamma_corr_pair_cell(bs_env, cells, icell, head_scalar, head_vector, first_pair, values_r, wing_left, wing_right)
Scatter one low-rank Gamma correction into the centred grid-pair images. For s=nearest_integer[H^{-1}...
RI lattice matrices, real-space cells and data distribution shared by χ, W, Σ and the Gamma correctio...
real(kind=dp) function, public ri_metric_head_zero(bs_env)
Zero-wave-vector Fourier value of the selected RI metric kernel.
subroutine, public build_ri_2c_lattice(fm_r, cells, potential, regularization, bs_env, qs_env, reference_basis)
Form distributed I_PQ(R) = (φ_P,0 | v | φ_Q,R) over every screened neighbor cell.
subroutine, public gamma_auxiliary_charges(qs_env, charge)
Integrate each auxiliary function for the Gamma charge-mode projection. For a primitive x^(2m) exp(-α...
pure subroutine, public block_range(n, rank, n_rank, first, n_local)
Return the contiguous part of 1..n owned by one rank of a block distribution.
subroutine, public assemble_aa_contraction(bs_env, u_pp)
Assembles the q-independent, atom-diagonal AA contraction from stored coefficients.
subroutine, public owned_mo_coefficients(bs_env, mo_coeff, coeff)
Dense MO coefficients of the cyclically owned SCF k points (owner of k: MODULO(k-1,...
subroutine, public ri_2c_lattice_to_kp(fm_r, cells, qpoint, matrix_q)
Evaluate I_PQ(q) = sum_R exp(i q.R) I_PQ(R) collectively into an existing dense matrix.
subroutine, public build_full_cell_grid(bs_env, index_to_cell)
Construct a complete cell grid dual to the uniform SCF k-point mesh.
Work data of periodic RI-RS GW with full k-point sampling.
Green functions, screening and self-energy for periodic RI-RS GW.
subroutine, public release_gw_work(bs_env)
Release all periodic RI-RS work data after the last quasiparticle iteration.
subroutine, public compute_w0(qs_env, bs_env, gwkp_env)
Compute ε, Wᶜ and its imaginary-time transform, Eqs. (7)-(8).
subroutine, public prepare_ri_rs(bs_env, gwkp_env)
Prepare RI-RS radial data, grid values, and Bloch orbitals.
subroutine, public compute_g_and_chi(qs_env, bs_env, gwkp_env)
Form the grid Green functions and RI polarizability.
subroutine, public compute_sigma_c_from_w_pq(bs_env, gwkp_env)
Form Σc with one owner per ordered (l,l') pair through both lattice FFTs.
subroutine, public compute_sigma_x(bs_env, gwkp_env)
Evaluate exchange and prepare band orbitals for correlation.
Full-matrix operations not provided by the CP2K FM packages.
Definition gw_utils_fm.F:13
subroutine, public local_complex_power(matrix, exponent, eps, cond_nr, min_ev, max_ev, relative_eps)
Compute a spectral power of a local complex Hermitian matrix. Eigenvalues not larger than eps (or rel...
collects all constants needed in input so that they can be used without circular dependencies
integer, parameter, public evgw0
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
Routines to compute the Coulomb integral V_(alpha beta)(k) for a k-point k using lattice summation in...
subroutine, public add_ewald_reciprocal(v_k, qs_env, kpoints, basis_type, omega, ikp_start, ikp_end)
Adds the reciprocal Ewald part sum_L exp(ik*L) (P 0 | erf(omega r)/r | Q L) to the dense V_PQ(k) of t...
Batched lattice Fourier transforms on regular k-point grids.
subroutine, public k_grid_to_cell_prepare(work, xkp, nkp_grid, index_to_cell, weights, allow_incomplete)
Prepare one inverse lattice transform for repeated matrix batches.
logical function, public regular_kpoint_grid(xkp, nkp_grid, grid_index, k_offset, allow_incomplete)
Test and map a uniformly shifted reciprocal grid or an explicitly allowed subset.
subroutine, public k_grid_to_cell_execute(work, values_k, values_rs, used_fft)
Execute a matrix batch using the prepared inverse lattice transform. Call outside OpenMP worker regio...
integer function, public lattice_fft_num_threads(nfft, nentries)
Bound concurrent forward transforms to 64 MiB of scratch, or one grid if larger.
subroutine, public k_grid_to_cell_release(work)
Release call-scoped inverse lattice maps and FFT buffers.
subroutine, public cell_to_k_grid_fft(values_rs, index_to_cell, xkp, nkp_grid, values_k, used_fft, deriv_direction, hmat, selected_kpoints)
Transform real matrices to a regular reciprocal grid or any subset of that grid.
logical function, public lattice_fft_shape(n, nfft, allow_arbitrary)
Find backend-supported FFT dimensions that preserve the original lattice grid. Sample the padded tran...
Machine interface based on Fortran 2003 and POSIX.
Definition machine.F:17
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
Definition of mathematical constants and functions.
complex(kind=dp), parameter, public z_one
complex(kind=dp), parameter, public gaussi
real(kind=dp), parameter, public fourpi
real(kind=dp), parameter, public twopi
complex(kind=dp), parameter, public z_zero
Interface to the message passing library MPI.
subroutine, public mp_para_env_release(para_env)
releases the para object (to be called when you don't want anymore the shared copy of this object)
subroutine, public mp_mem_avail_per_rank_gb(comm, mem_avail_gb)
Memory that is currently free on the node, per MPI rank of that node, in GB.
basic linear algebra operations for full matrixes
Main setup file for RI-RS grids {r_l}.
subroutine, public setup_ri_rs_grid(bs_env, grid_points)
Get RI-RS grid points {r_l}, either by on-the-fly optimization or reading pretabulated atomic grids.
AO sampling, D_llp(R), and the periodic RI-RS fit Z_lP(q).
subroutine, public compute_z_lp_q(bs_env, grid_points, z_lp_q, z_lp_zero)
Build explicit-cell AO values and periodic RI-RS coefficients Z_lP(q).
subroutine, public evaluate_mu_rl_r_values(bs_env, grid_points, i_cell, ao_values)
Evaluate ϕ_μ(r_l-R) locally for one cell R and the supplied points r_l.
Geometry and AO evaluation for real-space resolution of the identity.
Definition ri_rs_utils.F:11
subroutine, public precompute_ri_rs_radii(ri_rs, basis_set_ao, basis_set_ri, eps, unit_nr)
Compute per-atom AO and RI basis radii from the most diffuse Gaussian primitive in the AO ("ORB") and...
Definition ri_rs_utils.F:55
represent a blacs multidimensional parallel environment (for the mpi corrispective see cp_paratypes/m...
Represent a complex full matrix.
keeps the information about the structure of a full matrix
represent a full matrix
values(P,Q,j) stores one q point, with j an imaginary frequency or time. Each q allocation can be rel...
stores all the informations relevant to an mpi environment