(git:27c966b)
Loading...
Searching...
No Matches
gw_ri_rs_small_cell_full_kp_gamma.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 Gamma-point (q = 0) correction of W and Σ for periodic RI-RS GW.
10!> \author Jan Wilhelm (with help of ChatGPT, GPT-6 Sol and GPT-6 Astra)
11! **************************************************************************************************
13 USE cell_types, ONLY: cell_type,&
21 USE cp_cfm_diag, ONLY: cp_cfm_heevd
22 USE cp_cfm_types, ONLY: cp_cfm_create,&
28 USE cp_dbcsr_api, ONLY: dbcsr_copy,&
32 dbcsr_type_antisymmetric,&
33 dbcsr_type_no_symmetry
39 USE cp_fm_types, ONLY: cp_fm_get_info,&
56 USE kinds, ONLY: dp
62 USE kpoint_types, ONLY: get_kpoint_info,&
66 USE mathconstants, ONLY: fourpi,&
67 gaussi,&
68 twopi,&
69 z_one,&
70 z_zero
71 USE mathlib, ONLY: gauss_legendre,&
72 inv_3x3,&
81#include "./base/base_uses.f90"
82
83 IMPLICIT NONE
84 PRIVATE
85
86 CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'gw_ri_rs_small_cell_full_kp_gamma'
87
93
94 ! Below |a*L| = taylor_switch, radial phase moments use their Taylor series.
95 REAL(KIND=dp), PARAMETER :: taylor_switch = 1.0e-4_dp
96
97CONTAINS
98
99! **************************************************************************************************
100!> \brief Preserve distributed AO position, Hamiltonian and overlap images before QS release.
101!> Only the sparse operators and their immutable image traversal survive; SCF grids and
102!> neighbor lists can be released by the common GW initialization path.
103!> \param qs_env Converged Quickstep state.
104!> \param bs_env Band-structure settings.
105! **************************************************************************************************
106 SUBROUTINE prepare_ri_rs_gamma_correction(qs_env, bs_env)
107 TYPE(qs_environment_type), POINTER :: qs_env
108 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
109
110 CHARACTER(LEN=*), PARAMETER :: routinen = 'prepare_ri_rs_gamma_correction'
111
112 INTEGER :: handle, i_image, i_spin, n_img
113 LOGICAL :: created_scf_image_map
114 TYPE(dbcsr_p_type), POINTER :: matrix_h(:, :), matrix_s(:, :)
115 TYPE(gwkp_type), POINTER :: gwkp_env
116 TYPE(kpoint_type), POINTER :: kpoints_all, kpoints_scf
117 TYPE(neighbor_list_set_p_type), POINTER :: sab_all(:), sab_kp(:)
118
119 CALL timeset(routinen, handle)
120
121 cpassert(.NOT. ALLOCATED(bs_env%gwkp_env))
122 ALLOCATE (bs_env%gwkp_env)
123 gwkp_env => bs_env%gwkp_env
124 ALLOCATE (gwkp_env%eigenvalues_G(bs_env%n_ao, bs_env%nkp_scf_desymm, bs_env%n_spin))
125 gwkp_env%eigenvalues_G(:, :, :) = bs_env%eigenval_scf(:, 1:bs_env%nkp_scf_desymm, :)
126
127 NULLIFY (kpoints_all)
128
129 CALL get_qs_env(qs_env, kpoints=kpoints_scf, matrix_ks_kp=matrix_h, matrix_s_kp=matrix_s, &
130 sab_all=sab_all, sab_kp=sab_kp)
131 cpassert(ASSOCIATED(sab_all) .AND. ASSOCIATED(sab_kp))
132 cpassert(ASSOCIATED(matrix_h) .AND. ASSOCIATED(matrix_s))
133 cpassert(.NOT. ASSOCIATED(gwkp_env%gamma_corr_d))
134 created_scf_image_map = .NOT. ASSOCIATED(kpoints_scf)
135 IF (created_scf_image_map) THEN
136 CALL kpoint_create(kpoints_scf)
137 CALL kpoint_init_cell_index(kpoints_scf, sab_kp, bs_env%para_env, n_img)
138 END IF
139 CALL kpoint_create(kpoints_all)
140 CALL kpoint_init_cell_index(kpoints_all, sab_all, bs_env%para_env, n_img)
141 CALL gw_gamma_cell_index_union(kpoints_all, kpoints_scf, &
142 gwkp_env%gamma_corr_index_to_cell, gwkp_env%gamma_corr_cell_to_index)
143 CALL dbcsr_allocate_matrix_set(gwkp_env%gamma_corr_d, 3, SIZE(kpoints_all%index_to_cell, 2))
144 CALL build_local_moment_matrix_rs_img(qs_env, gwkp_env%gamma_corr_d)
145 CALL kp_transform_plan_create(gwkp_env%gamma_corr_plan, kpoints_all%sab_nl, &
146 kpoints_all%cell_to_index, SIZE(gwkp_env%gamma_corr_d, 2))
147 CALL kp_transform_plan_create(gwkp_env%gamma_corr_scf_plan, kpoints_scf%sab_nl, &
148 kpoints_scf%cell_to_index, SIZE(matrix_h, 2))
149 CALL dbcsr_allocate_matrix_set(gwkp_env%gamma_corr_h, SIZE(matrix_h, 1), SIZE(matrix_h, 2))
150 CALL dbcsr_allocate_matrix_set(gwkp_env%gamma_corr_s, SIZE(matrix_s, 1), SIZE(matrix_s, 2))
151 DO i_image = 1, SIZE(matrix_h, 2)
152 DO i_spin = 1, SIZE(matrix_h, 1)
153 ALLOCATE (gwkp_env%gamma_corr_h(i_spin, i_image)%matrix)
154 CALL dbcsr_copy(gwkp_env%gamma_corr_h(i_spin, i_image)%matrix, matrix_h(i_spin, i_image)%matrix)
155 END DO
156 END DO
157 DO i_image = 1, SIZE(matrix_s, 2)
158 DO i_spin = 1, SIZE(matrix_s, 1)
159 ALLOCATE (gwkp_env%gamma_corr_s(i_spin, i_image)%matrix)
160 CALL dbcsr_copy(gwkp_env%gamma_corr_s(i_spin, i_image)%matrix, matrix_s(i_spin, i_image)%matrix)
161 END DO
162 END DO
163 CALL kpoint_release(kpoints_all)
164 IF (created_scf_image_map) CALL kpoint_release(kpoints_scf)
165
166 CALL timestop(handle)
167
168 END SUBROUTINE prepare_ri_rs_gamma_correction
169
170! **************************************************************************************************
171!> \brief Gamma-correction head and wing coefficients for the near-Gamma cells.
172!> Construct the q->0 dielectric limit along face-cubature and mesh-sample directions
173!> from SCF dipole transitions and the directly fitted Gamma Gaussian body.
174!> \param bs_env ...
175!> \param qs_env ...
176!> \param gwkp_env RI-RS work data and Gamma correction.
177! **************************************************************************************************
178 SUBROUTINE gamma_point_correction(bs_env, qs_env, gwkp_env)
179 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
180 TYPE(qs_environment_type), POINTER :: qs_env
181 TYPE(gwkp_type), INTENT(INOUT) :: gwkp_env
182
183 CHARACTER(LEN=*), PARAMETER :: routinen = 'gamma_point_correction'
184
185 COMPLEX(KIND=dp) :: eps_00, pi_00, pi_q(3), &
186 q_direction_complex(3), &
187 schur_contribution, schur_denom
188 COMPLEX(KIND=dp), ALLOCATABLE :: dipole(:, :, :, :, :), eps_p_cartesian(:, :), &
189 f_external(:, :), f_response_cartesian(:, :), f_zero(:, :), metric_ref_zero(:, :), &
190 metric_rows(:, :), metric_zero(:, :), pi_cartesian(:, :), response_cartesian(:, :), &
191 schur_cartesian(:, :), time_chi(:, :, :), time_head(:, :, :), time_wing(:, :, :), &
192 v_sqrt_zero(:, :)
193 INTEGER :: a, first_row, handle, i_cartesian, &
194 i_direction, ikp, j_cartesian, j_w, &
195 n_angular, n_directions, n_near, &
196 n_w_kpoints
197 INTEGER, ALLOCATABLE :: metric_cells(:, :)
198 LOGICAL :: is_slab
199 REAL(kind=dp) :: near_q(3, 8), q_cart(3), q_direction(3), &
200 reciprocal(3, 3), volume
201 REAL(kind=dp), ALLOCATABLE :: angular_directions(:, :), angular_radii(:), &
202 angular_weights(:), directions(:, :), head_response(:, :), radii(:), weights(:)
203 TYPE(cp_fm_type), ALLOCATABLE :: fm_metric(:)
204
205 CALL timeset(routinen, handle)
206
207 is_slab = all(bs_env%periodic == [1, 1, 0])
208 volume = bs_env%ri_rs%cell%deth
209 n_w_kpoints = product(bs_env%nkp_grid_chi_eps_W_orig)
210 reciprocal = twopi*transpose(inv_3x3(bs_env%hmat))
211 IF (is_slab) THEN
212 CALL slab_reciprocal_vectors(bs_env, reciprocal(:, 1:2), volume)
213 CALL slab_cell_face_directions(bs_env, angular_directions, angular_weights, angular_radii)
214 ELSE
215 CALL gamma_cell_face_directions(bs_env, angular_directions, angular_weights, angular_radii)
216 END IF
217 n_angular = SIZE(angular_weights)
218 IF (bs_env%unit_nr > 0) THEN
219 WRITE (bs_env%unit_nr, '(T2,A,T72,I10)') &
220 'Number of angular points for the Γ-point correction of W:', n_angular
221 END IF
222 n_near = product(min(2, bs_env%nkp_grid_chi_eps_W_orig))
223 n_directions = n_angular + n_near
224 a = 0
225 DO ikp = 1, n_w_kpoints
226 q_direction = bs_env%kpoints_chi_eps_W%xkp(1:3, ikp) - &
227 anint(bs_env%kpoints_chi_eps_W%xkp(1:3, ikp))
228 IF (is_slab) THEN
229 IF (.NOT. is_gamma_neighbor(q_direction(1:2), bs_env%nkp_grid_chi_eps_W_orig(1:2))) cycle
230 ELSE
231 IF (.NOT. is_gamma_neighbor(q_direction, bs_env%nkp_grid_chi_eps_W_orig)) cycle
232 END IF
233 a = a + 1
234 IF (a > n_near) cpabort('Too many Gamma-neighbor transfer points')
235 near_q(:, a) = q_direction
236 END DO
237 IF (a /= n_near) cpabort('Inconsistent Gamma-neighbor transfer points')
238 ALLOCATE (directions(3, n_directions), weights(n_directions), radii(n_directions))
239 directions(:, 1:n_angular) = angular_directions
240 weights(1:n_angular) = angular_weights
241 radii(1:n_angular) = angular_radii
242 DEALLOCATE (angular_directions, angular_weights, angular_radii)
243 DO i_direction = 1, n_near
244 q_cart = matmul(reciprocal, near_q(:, i_direction))
245 radii(n_angular + i_direction) = norm2(q_cart)
246 directions(:, n_angular + i_direction) = q_cart/radii(n_angular + i_direction)
247 weights(n_angular + i_direction) = 0.0_dp
248 END DO
249 ALLOCATE (head_response(n_directions, bs_env%num_time_freq_points))
250 ! The finite RI metric at q=0 retains its complete Gaussian channel.
251 ! The singular full-Coulomb Gamma-correction head is integrated separately.
252 ALLOCATE (metric_zero(bs_env%n_RI, bs_env%n_RI), f_zero(bs_env%n_RI, bs_env%n_RI))
253 CALL build_ri_2c_lattice(fm_metric, metric_cells, bs_env%ri_metric, &
254 0.0_dp, bs_env, qs_env, reference_basis=bs_env%auto_ri%enabled)
255 IF (bs_env%auto_ri%enabled) THEN
256 CALL gamma_metric_contract_atoms(fm_metric, bs_env%auto_ri, metric_zero)
257 IF (.NOT. is_slab) THEN
258 ALLOCATE (metric_ref_zero(sum(bs_env%auto_ri%sizes_ref_RI), sum(bs_env%auto_ri%sizes_ref_RI)))
259 CALL ri_2c_lattice_to_kp(fm_metric, metric_cells, [0.0_dp, 0.0_dp, 0.0_dp], metric_ref_zero)
260 END IF
261 ELSE
262 CALL ri_2c_lattice_to_kp(fm_metric, metric_cells, [0.0_dp, 0.0_dp, 0.0_dp], metric_zero)
263 IF (.NOT. is_slab) ALLOCATE (metric_ref_zero, source=metric_zero)
264 END IF
265 CALL cp_fm_release(fm_metric)
266 DEALLOCATE (metric_cells)
267 IF (is_slab) THEN
268 CALL slab_gamma_factors(qs_env, bs_env, metric_zero, f_zero, f_external)
269 ELSE
270 CALL coulomb_matrix_gamma(qs_env, bs_env, v_sqrt_zero, metric_ref_zero)
271 DEALLOCATE (metric_ref_zero)
272 CALL gamma_metric_factor_distributed(bs_env%para_env, bs_env%regularization_RI, &
273 metric_zero, v_sqrt_zero, f_zero)
274 DEALLOCATE (v_sqrt_zero)
275 END IF
276 DEALLOCATE (metric_zero)
277 ! In 3D the external-potential map equals the density map.
278 IF (.NOT. is_slab) ALLOCATE (f_external, source=f_zero)
279
280 ! Gamma-correction matrix elements in the actual SCF MO gauge. q_x cancels analytically against
281 ! the 1/q and 1/q^2 factors, so q_x=1 is used after the limit has been taken.
282 CALL gw_gamma_transition_moments(bs_env, gwkp_env, bs_env%kpoints_scf_desymm, &
283 bs_env%cfm_mo_coeff_kp(1:bs_env%nkp_scf_desymm, :), gwkp_env%eigenvalues_G, &
284 bs_env%n_occ(1:bs_env%n_spin), dipole)
285
286 ALLOCATE (eps_p_cartesian(bs_env%n_RI, 3), pi_cartesian(3, 3), &
287 response_cartesian(bs_env%n_RI, 3), schur_cartesian(3, 3), &
288 f_response_cartesian(bs_env%n_RI, 3))
289 ALLOCATE (gwkp_env%gamma_correction%wing_cartesian(bs_env%ri_rs%n_grid_points, 3, &
290 bs_env%num_time_freq_points))
291 CALL accumulate_time_state(bs_env, gwkp_env%psi_lm_k, dipole, volume, first_row, &
292 time_chi, time_wing, time_head)
293 CALL metric_grid_rows(gwkp_env%Z_lP_zero(:, 2:, 1), f_zero, first_row, SIZE(time_chi, 1), metric_rows)
294 DO j_w = 1, bs_env%num_time_freq_points
295 CALL gamma_ao_time_response(bs_env, time_chi, time_wing, time_head, metric_rows, first_row, &
296 j_w, eps_p_cartesian, pi_cartesian, response_cartesian)
297 CALL zgemm('C', 'N', 3, 3, bs_env%n_RI, cmplx(fourpi, 0.0_dp, dp), &
298 eps_p_cartesian, bs_env%n_RI, response_cartesian, bs_env%n_RI, &
299 z_zero, schur_cartesian, 3)
300 ! The 2D Coulomb kernel is 2π/q, the 3D one 4π/q².
301 CALL zgemm('N', 'N', bs_env%n_RI, 3, bs_env%n_RI, z_one, &
302 f_external, bs_env%n_RI, response_cartesian, bs_env%n_RI, &
303 z_zero, f_response_cartesian, bs_env%n_RI)
304 CALL zgemm('N', 'N', bs_env%ri_rs%n_grid_points, 3, bs_env%n_RI, &
305 cmplx(merge(twopi, fourpi, is_slab)/ri_metric_head_zero(bs_env), 0.0_dp, dp), &
306 gwkp_env%Z_lP_zero(:, 2:, 1), bs_env%ri_rs%n_grid_points, f_response_cartesian, bs_env%n_RI, &
307 z_zero, gwkp_env%gamma_correction%wing_cartesian(:, :, j_w), bs_env%ri_rs%n_grid_points)
308 DO i_direction = 1, n_directions
309 q_direction_complex(:) = cmplx(directions(:, i_direction), 0.0_dp, dp)
310 pi_q(:) = matmul(pi_cartesian, q_direction_complex)
311 pi_00 = dot_product(q_direction_complex, pi_q)
312 eps_00 = z_one + fourpi*pi_00
313 schur_contribution = z_zero
314 DO j_cartesian = 1, 3
315 DO i_cartesian = 1, 3
316 schur_contribution = schur_contribution + &
317 conjg(q_direction_complex(i_cartesian))* &
318 schur_cartesian(i_cartesian, j_cartesian)*q_direction_complex(j_cartesian)
319 END DO
320 END DO
321 IF (is_slab) THEN
322 ! A(d,ω) = d†[α - E† B⁻¹ E]d is the screened 2D polarizability.
323 head_response(i_direction, j_w) = max(0.0_dp, real(pi_00 - schur_contribution/fourpi, dp))
324 cycle
325 END IF
326 schur_denom = eps_00 - schur_contribution
327 IF (real(schur_denom, dp) <= 0.0_dp) THEN
328 cpabort("Gamma dielectric Schur complement is not positive")
329 END IF
330 ! The Schur complement is a Hermitian quadratic form in the real d, so ε⁻¹_00(d) is real.
331 head_response(i_direction, j_w) = real(z_one/schur_denom, dp)
332 END DO
333
334 END DO
335
336 CALL gamma_corr_cell_integrals(bs_env, directions, weights, radii, n_angular, head_response, &
337 gwkp_env%gamma_correction)
338 DEALLOCATE (directions, head_response, radii, weights)
339 DEALLOCATE (time_chi, time_wing, time_head, metric_rows)
340 DEALLOCATE (f_external)
341
342 DEALLOCATE (dipole, &
343 eps_p_cartesian, f_zero, &
344 pi_cartesian, response_cartesian, schur_cartesian, &
345 f_response_cartesian)
346
347 CALL timestop(handle)
348
349 END SUBROUTINE gamma_point_correction
350
351! **************************************************************************************************
352!> \brief Coulomb matrix V_PQ(q=0) in the (AUTO_RI contracted) RI basis.
353!> In 3D, V(0) is the erfc RI metric M(0) plus the reciprocal Ewald sum, whose q=0 term
354!> -pi/(Omega omega^2) F(0) F(0)^H leaves V without its singular G=0 Fourier coefficient.
355!> The 2D Ewald sum has no q=0 limit; slabs sum the lattice directly and project the RI charge
356!> out afterwards.
357!> \param qs_env ...
358!> \param bs_env ...
359!> \param V_zero ...
360!> \param metric_ref_zero M(0) in the reference RI basis, required in 3D
361! **************************************************************************************************
362 SUBROUTINE coulomb_matrix_gamma(qs_env, bs_env, V_zero, metric_ref_zero)
363 TYPE(qs_environment_type), POINTER :: qs_env
364 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
365 COMPLEX(KIND=dp), ALLOCATABLE, INTENT(OUT) :: v_zero(:, :)
366 COMPLEX(KIND=dp), INTENT(IN), OPTIONAL :: metric_ref_zero(:, :)
367
368 CHARACTER(LEN=*), PARAMETER :: routinen = 'coulomb_matrix_gamma'
369
370 COMPLEX(KIND=dp), ALLOCATABLE :: u(:, :), v_contracted(:, :), &
371 v_owned(:, :, :)
372 INTEGER :: handle, n_local, n_ref
373 TYPE(kpoint_type), POINTER :: gamma_kp
374
375 CALL timeset(routinen, handle)
376
377 n_ref = bs_env%n_RI
378 IF (bs_env%auto_ri%enabled) n_ref = sum(bs_env%auto_ri%sizes_ref_RI)
379 n_local = 0
380 IF (modulo(1, bs_env%para_env%num_pe) == bs_env%para_env%mepos) n_local = 1
381 ALLOCATE (gamma_kp, v_owned(n_ref, n_ref, n_local), v_zero(n_ref, n_ref))
382 ALLOCATE (gamma_kp%xkp(3, 1))
383 gamma_kp%xkp = 0.0_dp
384 gamma_kp%nkp = 1
385 gamma_kp%nkp_grid = bs_env%nkp_grid_chi_eps_W_orig
386 v_owned = z_zero
387 IF (all(bs_env%periodic == [1, 1, 0])) THEN
388 CALL build_2c_coulomb_matrix_kp_dense(v_owned, qs_env, gamma_kp, &
389 bs_env%size_lattice_sum_V, 'RI_AUX', 1, 1)
390 ELSE
391 cpassert(PRESENT(metric_ref_zero))
392 IF (n_local == 1) v_owned(:, :, 1) = metric_ref_zero
393 CALL add_ewald_reciprocal(v_owned, qs_env, gamma_kp, 'RI_AUX', bs_env%ri_metric%omega, 1, 1)
394 END IF
395 v_zero(:, :) = z_zero
396 IF (n_local == 1) v_zero(:, :) = v_owned(:, :, 1)
397 CALL bs_env%para_env%sum(v_zero)
398 DEALLOCATE (gamma_kp%xkp)
399 DEALLOCATE (gamma_kp, v_owned)
400 IF (bs_env%auto_ri%enabled) THEN
401 CALL assemble_aa_contraction(bs_env, u)
402 ALLOCATE (v_contracted(bs_env%n_RI, bs_env%n_RI))
403 v_contracted(:, :) = matmul(conjg(transpose(u)), matmul(v_zero, u))
404 CALL move_alloc(v_contracted, v_zero)
405 DEALLOCATE (u)
406 END IF
407
408 CALL timestop(handle)
409
410 END SUBROUTINE coulomb_matrix_gamma
411
412! **************************************************************************************************
413!> \brief Angular cubature over the cells adjacent to Gamma.
414!> With mesh dimensions n_i, the fractional half-widths are h_i=min(1/n_i,1/2).
415!> The face Jacobian is Omega_BZ*PRODUCT(h_i)/r**3. The region contains
416!> PRODUCT(min(2,n_i)) distinct mesh cells; a one-point direction covers its
417!> entire Brillouin-zone interval exactly once, not two periodic copies.
418!> \param bs_env ...
419!> \param directions ...
420!> \param weights ...
421!> \param radii ...
422! **************************************************************************************************
423 SUBROUTINE gamma_cell_face_directions(bs_env, directions, weights, radii)
424 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
425 REAL(kind=dp), ALLOCATABLE, INTENT(OUT) :: directions(:, :), weights(:), radii(:)
426
427 INTEGER, PARAMETER :: n = 6
428
429 INTEGER :: face, i, ia, ib, j, k, n_angular, nk, &
430 sign_index
431 REAL(kind=dp) :: bmat(3, 3), jacobian_scale, q(3), &
432 qfrac(3), volume
433 REAL(kind=dp), ALLOCATABLE :: nodes(:), rule_weights(:)
434
435 ! Six faces with a 6-by-6 Gauss-Legendre rule give 216 angular points.
436 n_angular = 6*n*n
437 nk = product(bs_env%nkp_grid_chi_eps_W_orig)
438 jacobian_scale = real(product(min(2, bs_env%nkp_grid_chi_eps_W_orig)), dp)/8.0_dp
439 volume = bs_env%ri_rs%cell%deth
440 bmat = twopi*transpose(inv_3x3(bs_env%hmat))
441 ALLOCATE (nodes(n), rule_weights(n))
442 CALL gauss_legendre(n, nodes, rule_weights)
443 ALLOCATE (directions(3, n_angular), weights(n_angular), radii(n_angular))
444 face = 0
445 DO i = 1, 3
446 j = modulo(i, 3) + 1
447 k = modulo(i + 1, 3) + 1
448 DO sign_index = -1, 1, 2
449 DO ia = 1, n
450 DO ib = 1, n
451 face = face + 1
452 qfrac(i) = real(sign_index, dp)/real(max(2, bs_env%nkp_grid_chi_eps_W_orig(i)), dp)
453 qfrac(j) = nodes(ia)/real(max(2, bs_env%nkp_grid_chi_eps_W_orig(j)), dp)
454 qfrac(k) = nodes(ib)/real(max(2, bs_env%nkp_grid_chi_eps_W_orig(k)), dp)
455 q = matmul(bmat, qfrac)
456 radii(face) = norm2(q)
457 directions(:, face) = q/radii(face)
458 weights(face) = (twopi**3/volume)/(fourpi*real(nk, dp)*radii(face)**3)* &
459 rule_weights(ia)*rule_weights(ib)*jacobian_scale
460 END DO
461 END DO
462 END DO
463 END DO
464 cpassert(face == n_angular)
465 DEALLOCATE (nodes, rule_weights)
466 END SUBROUTINE gamma_cell_face_directions
467
468! **************************************************************************************************
469!> \brief Whether a point of the half-shifted q mesh is one of the transfer points next to Gamma.
470!> On a half-shifted mesh, 2*|q_i|*N_i is an odd integer; it equals one next to Gamma.
471!> \param xkp fractional q point
472!> \param nkp_grid q mesh along the same directions
473!> \return ...
474! **************************************************************************************************
475 PURE FUNCTION is_gamma_neighbor(xkp, nkp_grid) RESULT(neighbor)
476 REAL(kind=dp), INTENT(IN) :: xkp(:)
477 INTEGER, INTENT(IN) :: nkp_grid(:)
478 LOGICAL :: neighbor
479
480 neighbor = all(nint(2.0_dp*abs(xkp - anint(xkp))*real(nkp_grid, dp)) == 1)
481
482 END FUNCTION is_gamma_neighbor
483
484! **************************************************************************************************
485!> \brief Radial moments f0 = int_0^L exp(-i a q) dq and f1 = int_0^L q exp(-i a q) dq.
486!> A Taylor series replaces the closed forms where they cancel for small |a L|.
487!> \param a ...
488!> \param L ...
489!> \param f0 ...
490!> \param f1 ...
491! **************************************************************************************************
492 PURE SUBROUTINE radial_phase_moments(a, L, f0, f1)
493 REAL(kind=dp), INTENT(IN) :: a, l
494 COMPLEX(KIND=dp), INTENT(OUT) :: f0
495 COMPLEX(KIND=dp), INTENT(OUT), OPTIONAL :: f1
496
497 COMPLEX(KIND=dp) :: phase
498
499 IF (abs(a*l) < taylor_switch) THEN
500 f0 = cmplx(l - a*a*l**3/6.0_dp + a**4*l**5/120.0_dp, &
501 -a*l*l/2.0_dp + a**3*l**4/24.0_dp, dp)
502 IF (PRESENT(f1)) f1 = cmplx(l*l/2.0_dp - a*a*l**4/8.0_dp, &
503 -a*l**3/3.0_dp + a**3*l**5/30.0_dp, dp)
504 ELSE
505 phase = exp(-gaussi*a*l)
506 f0 = (z_one - phase)/(gaussi*a)
507 IF (PRESENT(f1)) f1 = (phase*(z_one + gaussi*a*l) - z_one)/(a*a)
508 END IF
509
510 END SUBROUTINE radial_phase_moments
511
512! **************************************************************************************************
513!> \brief Integrate 1/q**2 (3D) or 1/|q| (2D slab) over the distinct shifted-mesh cells around Gamma.
514!> The regular mesh sum already contains the near-Gamma model samples, which are
515!> subtracted here. In 3D the returned coefficient multiplies the factored head
516!> density 4*pi*Z_l0(0)*conjg(Z_l'0(0))/M_0(0)**2; in 2D it has units of inverse
517!> in-plane momentum. The face-to-polar Jacobian resolves the integrable singularity
518!> without sampling q=0.
519!> \param bs_env ...
520!> \param cells ...
521!> \param factors ...
522! **************************************************************************************************
523 SUBROUTINE singular_head_cell_factors(bs_env, cells, factors)
524 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
525 INTEGER, INTENT(IN) :: cells(:, :)
526 COMPLEX(KIND=dp), ALLOCATABLE, INTENT(OUT) :: factors(:)
527
528 COMPLEX(KIND=dp) :: f0
529 INTEGER :: icell, idir, iq, n_dim, nk
530 REAL(kind=dp) :: area, bmat(3, 3), q(3), qnorm, rcart(3), &
531 scale
532 REAL(kind=dp), ALLOCATABLE :: directions(:, :), radii(:), weights(:)
533
534 nk = product(bs_env%nkp_grid_chi_eps_W_orig)
535 IF (all(bs_env%periodic == [1, 1, 0])) THEN
536 n_dim = 2
537 CALL slab_cell_face_directions(bs_env, directions, weights, radii)
538 bmat(:, 3) = 0.0_dp
539 CALL slab_reciprocal_vectors(bs_env, bmat(:, 1:2), area)
540 scale = area/twopi**2
541 ELSE
542 n_dim = 3
543 CALL gamma_cell_face_directions(bs_env, directions, weights, radii)
544 bmat = twopi*transpose(inv_3x3(bs_env%hmat))
545 scale = bs_env%ri_rs%cell%deth*fourpi/(twopi**3)
546 END IF
547 ALLOCATE (factors(SIZE(cells, 2)))
548 DO icell = 1, SIZE(cells, 2)
549 rcart = matmul(bs_env%hmat, real(cells(:, icell), dp))
550 factors(icell) = z_zero
551 DO idir = 1, SIZE(weights)
552 CALL radial_phase_moments(dot_product(directions(:, idir), rcart), radii(idir), f0)
553 factors(icell) = factors(icell) + weights(idir)*f0
554 END DO
555 factors(icell) = factors(icell)*scale
556 DO iq = 1, nk
557 IF (.NOT. is_gamma_neighbor(bs_env%kpoints_chi_eps_W%xkp(1:n_dim, iq), &
558 bs_env%nkp_grid_chi_eps_W_orig(1:n_dim))) cycle
559 q = matmul(bmat, bs_env%kpoints_chi_eps_W%xkp(1:3, iq) - &
560 anint(bs_env%kpoints_chi_eps_W%xkp(1:3, iq)))
561 qnorm = norm2(q)
562 factors(icell) = factors(icell) - exp(-gaussi*dot_product(q, rcart))/ &
563 (real(nk, dp)*qnorm**(n_dim - 1))
564 END DO
565 END DO
566 DEALLOCATE (directions, weights, radii)
567 END SUBROUTINE singular_head_cell_factors
568
569! **************************************************************************************************
570!> \brief Separate the 2D charge mode and factor the neutral Γ Coulomb body.
571!> For charge vector f, P=I-ff†/(f†f) and V_b=P V_reg(0) P. The Gamma correction
572!> density map is M⁻¹ P V_b¹ᐟ²; the external-potential map is M⁻¹ V_reg P V_b⁻¹ᐟ².
573!> Keeping the second map distinct retains the finite charge/body couplings.
574!> \param qs_env ...
575!> \param bs_env ...
576!> \param metric ...
577!> \param F_density ...
578!> \param F_external ...
579! **************************************************************************************************
580 SUBROUTINE slab_gamma_factors(qs_env, bs_env, metric, F_density, F_external)
581 TYPE(qs_environment_type), POINTER :: qs_env
582 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
583 COMPLEX(KIND=dp), INTENT(INOUT) :: metric(:, :)
584 COMPLEX(KIND=dp), INTENT(OUT) :: f_density(:, :)
585 COMPLEX(KIND=dp), ALLOCATABLE, INTENT(OUT) :: f_external(:, :)
586
587 CHARACTER(LEN=*), PARAMETER :: routinen = 'slab_gamma_factors'
588
589 COMPLEX(KIND=dp), ALLOCATABLE :: body_inverse(:, :), body_sqrt(:, :), charge(:), &
590 charge_contracted(:), projector(:, :), u(:, :), v_regular(:, :)
591 INTEGER :: handle, i_col, i_row, n_ref, n_ri
592
593 CALL timeset(routinen, handle)
594
595 n_ri = bs_env%n_RI
596 n_ref = n_ri
597 IF (bs_env%auto_ri%enabled) n_ref = sum(bs_env%auto_ri%sizes_ref_RI)
598 CALL coulomb_matrix_gamma(qs_env, bs_env, v_regular)
599 ALLOCATE (charge(n_ref))
600 CALL gamma_auxiliary_charges(qs_env, charge)
601 IF (bs_env%auto_ri%enabled) THEN
602 CALL assemble_aa_contraction(bs_env, u)
603 ALLOCATE (charge_contracted(n_ri))
604 charge_contracted(:) = matmul(conjg(transpose(u)), charge)
605 CALL move_alloc(charge_contracted, charge)
606 DEALLOCATE (u)
607 END IF
608 cpassert(real(dot_product(charge, charge), dp) > 0.0_dp)
609 charge(:) = charge/sqrt(real(dot_product(charge, charge), dp))
610 ALLOCATE (projector(n_ri, n_ri), body_sqrt(n_ri, n_ri), body_inverse(n_ri, n_ri))
611 DO i_col = 1, n_ri
612 DO i_row = 1, n_ri
613 projector(i_row, i_col) = -charge(i_row)*conjg(charge(i_col))
614 END DO
615 projector(i_col, i_col) = projector(i_col, i_col) + z_one
616 END DO
617 body_sqrt(:, :) = matmul(projector, matmul(v_regular, projector))
618 body_inverse(:, :) = body_sqrt
619 CALL local_complex_power(body_sqrt, 0.5_dp, 0.0_dp, relative_eps=bs_env%regularization_RI)
620 CALL local_complex_power(body_inverse, -0.5_dp, 0.0_dp, relative_eps=bs_env%regularization_RI)
621 CALL local_complex_power(metric, -1.0_dp, 0.0_dp, relative_eps=bs_env%regularization_RI)
622 f_density = matmul(metric, matmul(projector, body_sqrt))
623 ALLOCATE (f_external(n_ri, n_ri))
624 f_external(:, :) = matmul(metric, matmul(v_regular, matmul(projector, body_inverse)))
625 DEALLOCATE (v_regular, charge, projector, body_sqrt, body_inverse)
626
627 CALL timestop(handle)
628
629 END SUBROUTINE slab_gamma_factors
630
631! **************************************************************************************************
632!> \brief In-plane reciprocal vectors and area for an arbitrary XY cell.
633!> \param bs_env ...
634!> \param b ...
635!> \param area ...
636! **************************************************************************************************
637 SUBROUTINE slab_reciprocal_vectors(bs_env, b, area)
638 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
639 REAL(kind=dp), INTENT(OUT) :: b(3, 2), area
640
641 REAL(kind=dp) :: normal(3)
642
643 normal = vector_product(bs_env%hmat(:, 1), bs_env%hmat(:, 2))
644 area = norm2(normal)
645 cpassert(area > 0.0_dp)
646 normal = normal/area
647 b(:, 1) = twopi*vector_product(bs_env%hmat(:, 2), normal)/area
648 b(:, 2) = twopi*vector_product(normal, bs_env%hmat(:, 1))/area
649 END SUBROUTINE slab_reciprocal_vectors
650
651! **************************************************************************************************
652!> \brief Polar boundary quadrature of the four shifted Γ-neighbor cells.
653!> Directions d, radii L and weights approximate dθ on the bounding parallelogram.
654!> As in 3D, the fractional half-widths are min(1/n_i,1/2).
655!> \param bs_env ...
656!> \param directions ...
657!> \param weights ...
658!> \param radii ...
659! **************************************************************************************************
660 SUBROUTINE slab_cell_face_directions(bs_env, directions, weights, radii)
661 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
662 REAL(kind=dp), ALLOCATABLE, INTENT(OUT) :: directions(:, :), weights(:), radii(:)
663
664 INTEGER, PARAMETER :: n_face = 24
665
666 INTEGER :: i_axis, i_direction, i_node, i_sign, &
667 j_axis
668 REAL(kind=dp) :: area, b(3, 2), dq(3), q(3)
669 REAL(kind=dp), ALLOCATABLE :: nodes(:), rule(:)
670
671 CALL slab_reciprocal_vectors(bs_env, b, area)
672 ALLOCATE (nodes(n_face), rule(n_face))
673 CALL gauss_legendre(n_face, nodes, rule)
674 ALLOCATE (directions(3, 4*n_face), weights(4*n_face), radii(4*n_face))
675 i_direction = 0
676 DO i_axis = 1, 2
677 j_axis = 3 - i_axis
678 dq = b(:, j_axis)/real(max(2, bs_env%nkp_grid_chi_eps_W_orig(j_axis)), dp)
679 DO i_sign = -1, 1, 2
680 DO i_node = 1, n_face
681 i_direction = i_direction + 1
682 q = real(i_sign, dp)*b(:, i_axis)/real(max(2, bs_env%nkp_grid_chi_eps_W_orig(i_axis)), dp) &
683 + nodes(i_node)*dq
684 radii(i_direction) = norm2(q)
685 directions(:, i_direction) = q/radii(i_direction)
686 weights(i_direction) = rule(i_node)*norm2(vector_product(q, dq))/sum(q*q)
687 END DO
688 END DO
689 END DO
690 DEALLOCATE (nodes, rule)
691 END SUBROUTINE slab_cell_face_directions
692
693! **************************************************************************************************
694!> \brief Integrate the Gamma-correction head and wings over the near-Gamma cells, in 2D and 3D.
695!> The regular mesh sum already contains the near-Gamma mesh samples (directions after
696!> n_angular), which are subtracted here; exp(-iqR) is retained per real-space cell R.
697!> Per cell only the head and one Cartesian wing coefficient per frequency are stored; the wing
698!> on the grid is wing_cartesian(ω) times that coefficient.
699!> 3D: ε⁻¹_00(d,ω) does not depend on |q|, so Wc_00 = 4π(ε⁻¹_00-1)/q² and Wc_l0 = -4π ε⁻¹_00 t_l·d/q
700!> have time-independent radial moments.
701!> 2D: with A(d,ω)=d†[α-E†B⁻¹E]d and v=2π/q, Wc_00=-4π²A/(1+2πqA) and Wc_l0=-2π t_l·d/(1+2πqA)
702!> are integrated radially by Gauss-Legendre.
703!> \param bs_env ...
704!> \param directions unit directions d around Gamma
705!> \param weights angular weights, zero for the mesh samples
706!> \param radii extent of the near-Gamma cell along d, or |q| of a mesh sample
707!> \param n_angular number of angular directions, followed by the mesh samples
708!> \param head_response ε⁻¹_00(d,ω) in 3D, A(d,ω) in 2D
709!> \param model Gamma correction per real-space cell
710! **************************************************************************************************
711 SUBROUTINE gamma_corr_cell_integrals(bs_env, directions, weights, radii, n_angular, head_response, model)
712 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
713 REAL(kind=dp), INTENT(IN) :: directions(:, :), weights(:), radii(:)
714 INTEGER, INTENT(IN) :: n_angular
715 REAL(kind=dp), INTENT(IN) :: head_response(:, :)
716 TYPE(gamma_correction_type), INTENT(INOUT) :: model
717
718 CHARACTER(LEN=*), PARAMETER :: routinen = 'gamma_corr_cell_integrals'
719 INTEGER, PARAMETER :: n_radial = 32
720
721 COMPLEX(KIND=dp) :: f0, f1, head, integral, phase, wing
722 COMPLEX(KIND=dp), ALLOCATABLE :: head_frequency(:, :)
723 INTEGER :: handle, i_cell, i_direction, &
724 i_frequency, i_node, n_cells, &
725 n_frequency, nk
726 INTEGER, ALLOCATABLE :: cells(:, :)
727 LOGICAL :: is_slab
728 REAL(kind=dp) :: area, b(3, 2), head_scale, length, &
729 measure, projection, q, rcart(3), &
730 response
731 REAL(kind=dp), ALLOCATABLE :: nodes(:), rule(:)
732
733 CALL timeset(routinen, handle)
734
735 is_slab = all(bs_env%periodic == [1, 1, 0])
736 CALL build_full_cell_grid(bs_env, cells)
737 nk = product(bs_env%nkp_grid_chi_eps_W_orig)
738 n_cells = SIZE(cells, 2)
739 n_frequency = bs_env%num_time_freq_points
740 head_scale = 1.0_dp/ri_metric_head_zero(bs_env)**2
741 ! Volume element of the angular rule: Ω/(2π)³ times the 4π of the solid angle in 3D,
742 ! A/(2π)² in 2D.
743 IF (is_slab) THEN
744 CALL slab_reciprocal_vectors(bs_env, b, area)
745 measure = area/twopi**2
746 ALLOCATE (nodes(n_radial), rule(n_radial))
747 CALL gauss_legendre(n_radial, nodes, rule)
748 ELSE
749 measure = bs_env%ri_rs%cell%deth*fourpi/twopi**3
750 END IF
751 ALLOCATE (head_frequency(n_cells, n_frequency), source=z_zero)
752 ALLOCATE (model%wing_cell_frequency(3, n_frequency, n_cells), source=z_zero)
753 DO i_cell = bs_env%para_env%mepos + 1, n_cells, bs_env%para_env%num_pe
754 rcart = matmul(bs_env%hmat, real(cells(:, i_cell), dp))
755 DO i_direction = 1, SIZE(radii)
756 length = radii(i_direction)
757 projection = dot_product(directions(:, i_direction), rcart)
758 phase = exp(-gaussi*length*projection)
759 IF (.NOT. is_slab) THEN
760 IF (i_direction <= n_angular) THEN
761 CALL radial_phase_moments(projection, length, f0, f1)
762 f0 = measure*weights(i_direction)*f0
763 f1 = measure*weights(i_direction)*f1
764 ELSE
765 f0 = -phase/(real(nk, dp)*length**2)
766 f1 = -phase/(real(nk, dp)*length)
767 END IF
768 END IF
769 DO i_frequency = 1, n_frequency
770 response = head_response(i_direction, i_frequency)
771 IF (is_slab) THEN
772 IF (i_direction <= n_angular) THEN
773 integral = z_zero
774 DO i_node = 1, n_radial
775 q = 0.5_dp*length*(nodes(i_node) + 1.0_dp)
776 integral = integral + 0.5_dp*length*rule(i_node)*q/(1.0_dp + twopi*q*response)* &
777 exp(-gaussi*q*projection)
778 END DO
779 integral = measure*weights(i_direction)*integral
780 ELSE
781 integral = -phase/(real(nk, dp)*(1.0_dp + twopi*length*response))
782 END IF
783 head = -twopi**2*response*integral
784 wing = -integral
785 ELSE
786 head = fourpi*(response - 1.0_dp)*f0
787 wing = -response*f1
788 END IF
789 head_frequency(i_cell, i_frequency) = head_frequency(i_cell, i_frequency) + head_scale*head
790 model%wing_cell_frequency(:, i_frequency, i_cell) = &
791 model%wing_cell_frequency(:, i_frequency, i_cell) + wing*directions(:, i_direction)
792 END DO
793 END DO
794 END DO
795 CALL bs_env%para_env%sum(head_frequency)
796 CALL bs_env%para_env%sum(model%wing_cell_frequency)
797 ALLOCATE (model%head_cell_tau(n_cells, n_frequency))
798 model%head_cell_tau(:, :) = matmul(head_frequency, &
799 transpose(bs_env%time_frequency_grid%cosine_frequency_to_time))
800 DEALLOCATE (cells, head_frequency)
801 IF (is_slab) DEALLOCATE (nodes, rule)
802
803 CALL timestop(handle)
804
805 END SUBROUTINE gamma_corr_cell_integrals
806
807! **************************************************************************************************
808!> \brief Add the Gamma-correction head and wings of one time point to the local grid pairs.
809!> The wing of cell R is w_l(R,τ) = Σ_ω cos(τ,ω) Σ_c wing_cartesian_lc(ω) c_c(R,ω); one GEMM per cell
810!> forms it together with its conjugate-side partner.
811!> \param bs_env ...
812!> \param cells ...
813!> \param model ...
814!> \param i_t ...
815!> \param Z_head Grid coefficients Z_l0(0) of the Gamma head function
816!> \param first_pair ...
817!> \param W_grid_R ...
818! **************************************************************************************************
819 SUBROUTINE add_gamma_corr_cell_correction(bs_env, cells, model, i_t, Z_head, first_pair, W_grid_R)
820 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
821 INTEGER, INTENT(IN) :: cells(:, :)
822 TYPE(gamma_correction_type), INTENT(IN) :: model
823 INTEGER, INTENT(IN) :: i_t
824 COMPLEX(KIND=dp), INTENT(IN) :: z_head(:)
825 INTEGER, INTENT(IN) :: first_pair
826 REAL(kind=dp), INTENT(INOUT) :: w_grid_r(:, :, :)
827
828 CHARACTER(LEN=*), PARAMETER :: routinen = 'add_gamma_corr_cell_correction'
829
830 COMPLEX(KIND=dp), ALLOCATABLE :: coefficient(:, :, :), wing(:, :)
831 INTEGER :: handle, i_cell, j_w, n_frequency, n_grid
832
833 CALL timeset(routinen, handle)
834
835 n_grid = SIZE(model%wing_cartesian, 1)
836 n_frequency = SIZE(model%wing_cartesian, 3)
837 ALLOCATE (coefficient(3, n_frequency, 2), wing(n_grid, 2))
838 DO i_cell = 1, SIZE(cells, 2)
839 DO j_w = 1, n_frequency
840 coefficient(:, j_w, 1) = bs_env%time_frequency_grid%cosine_frequency_to_time(i_t, j_w)* &
841 model%wing_cell_frequency(:, j_w, i_cell)
842 END DO
843 ! conj(wing_cartesian) c = conj(wing_cartesian conj(c))
844 coefficient(:, :, 2) = conjg(coefficient(:, :, 1))
845 CALL zgemm('N', 'N', n_grid, 2, 3*n_frequency, z_one, model%wing_cartesian, n_grid, &
846 coefficient, 3*n_frequency, z_zero, wing, n_grid)
847 wing(:, 2) = conjg(wing(:, 2))
848 CALL add_gamma_corr_pair_cell(bs_env, cells, i_cell, model%head_cell_tau(i_cell, i_t), z_head, &
849 first_pair, w_grid_r, wing(:, 1), wing(:, 2))
850 END DO
851 DEALLOCATE (coefficient, wing)
852
853 CALL timestop(handle)
854
855 END SUBROUTINE add_gamma_corr_cell_correction
856
857! **************************************************************************************************
858!> \brief Transform cached response and solve (I+F^H Z^H B Z F) X = F^H Z^H E.
859!> Z_lP and F are existing metric maps. All three right sides use the same
860!> row-distributed frequency matrix. Head and wings use the same time transform.
861!> \param bs_env ...
862!> \param chi_tau Owned grid rows of B(tau).
863!> \param wing_tau Owned grid rows of E(tau).
864!> \param head_tau P(tau).
865!> \param metric_rows Owned grid rows of T = Z F.
866!> \param first_row First owned grid row.
867!> \param j_w Frequency index.
868!> \param E ...
869!> \param P ...
870!> \param X ...
871! **************************************************************************************************
872 SUBROUTINE gamma_ao_time_response(bs_env, chi_tau, wing_tau, head_tau, metric_rows, first_row, &
873 j_w, E, P, X)
874 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
875 COMPLEX(KIND=dp), INTENT(IN) :: chi_tau(:, :, :), wing_tau(:, :, :), &
876 head_tau(:, :, :), metric_rows(:, :)
877 INTEGER, INTENT(IN) :: first_row, j_w
878 COMPLEX(KIND=dp), INTENT(OUT) :: e(:, :), p(:, :), x(:, :)
879
880 CHARACTER(LEN=*), PARAMETER :: routinen = 'gamma_ao_time_response'
881
882 COMPLEX(KIND=dp), ALLOCATABLE :: chi_frequency(:, :), wing_frequency(:, :)
883 INTEGER :: handle, nr, t
884
885 CALL timeset(routinen, handle)
886
887 ! Apply the existing cosine coefficients to the local cache, without rebuilding Green matrices.
888 nr = SIZE(chi_tau, 1)
889 ALLOCATE (chi_frequency(nr, SIZE(chi_tau, 2)), wing_frequency(nr, 3), source=z_zero)
890 p = z_zero
891 DO t = 1, bs_env%num_time_freq_points
892 chi_frequency(:, :) = chi_frequency + &
893 bs_env%time_frequency_grid%cosine_time_to_frequency(j_w, t)*chi_tau(:, :, t)
894 wing_frequency(:, :) = wing_frequency + &
895 bs_env%time_frequency_grid%cosine_time_to_frequency(j_w, t)*wing_tau(:, :, t)
896 p = p + bs_env%time_frequency_grid%cosine_time_to_frequency(j_w, t)*head_tau(:, :, t)
897 END DO
898 ! Project the wing as E_Qc = sum_l conjg(T_lQ) E_lc over disjoint rows.
899 e = z_zero
900 IF (nr > 0) CALL zgemm('C', 'N', SIZE(metric_rows, 2), 3, nr, z_one, metric_rows, nr, &
901 wing_frequency, nr, z_zero, e, SIZE(e, 1))
902 CALL bs_env%para_env%sum(e)
903 CALL solve_gamma_corr_body(bs_env, metric_rows, chi_frequency, first_row, e, x)
904
905 CALL timestop(handle)
906
907 END SUBROUTINE gamma_ao_time_response
908
909! **************************************************************************************************
910!> \brief Warn if sampled transform errors over the physical transition interval exceed the tolerance.
911!> The interval spans the smallest gap and the largest transition energy of all k points.
912!> \param bs_env ...
913! **************************************************************************************************
914 SUBROUTINE validate_time_transform(bs_env)
915 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
916
917 INTEGER, PARAMETER :: n_sampled_gaps = 32
918 REAL(kind=dp), PARAMETER :: eps_gamma_time_transform = 1.0e-5_dp
919
920 CHARACTER(LEN=256) :: error_message
921 INTEGER :: i, no, nv, spin, t, w
922 REAL(kind=dp) :: approximate, delta, exact, hi, lo, &
923 omega, worst
924 TYPE(gwkp_type), POINTER :: gwkp_env
925
926 gwkp_env => bs_env%gwkp_env
927
928 lo = huge(1.0_dp)
929 hi = 0.0_dp
930 DO spin = 1, bs_env%n_spin
931 no = bs_env%n_occ(spin)
932 nv = bs_env%n_mo_retained - no
933 cpassert(nv >= 0)
934 IF (no == 0 .OR. nv == 0) cycle
935 lo = min(lo, minval(gwkp_env%eigenvalues_G(no + 1, 1:bs_env%nkp_scf_desymm, spin) - &
936 gwkp_env%eigenvalues_G(no, 1:bs_env%nkp_scf_desymm, spin)))
937 hi = max(hi, maxval(gwkp_env%eigenvalues_G(no + nv, 1:bs_env%nkp_scf_desymm, spin) - &
938 gwkp_env%eigenvalues_G(1, 1:bs_env%nkp_scf_desymm, spin)))
939 END DO
940 IF (hi == 0.0_dp) RETURN
941 IF (lo <= 0.0_dp) THEN
942 cpabort('Gamma response requires a positive transition gap')
943 END IF
944 worst = 0.0_dp
945 DO i = 1, n_sampled_gaps
946 delta = lo*(hi/lo)**(real(i - 1, dp)/real(n_sampled_gaps - 1, dp))
947 DO w = 1, bs_env%num_time_freq_points
948 omega = bs_env%time_frequency_grid%frequency(w)
949 exact = 2.0_dp*delta/(delta*delta + omega*omega)
950 approximate = 0.0_dp
951 DO t = 1, bs_env%num_time_freq_points
952 approximate = approximate + bs_env%time_frequency_grid%cosine_time_to_frequency(w, t)* &
953 exp(-delta*bs_env%time_frequency_grid%imaginary_time(t))
954 END DO
955 worst = max(worst, abs(approximate - exact)/exact)
956 END DO
957 END DO
958 IF (worst > eps_gamma_time_transform) THEN
959 WRITE (error_message, '(A,ES12.4,A,ES12.4,A)') &
960 'Gamma time transform relative error ', worst, &
961 ' exceeds tolerance ', eps_gamma_time_transform, &
962 '. Increase NUM_TIME_FREQ_POINTS and check convergence.'
963 cpwarn(trim(error_message))
964 END IF
965 END SUBROUTINE validate_time_transform
966
967! **************************************************************************************************
968!> \brief Cache Gamma-correction response in imaginary time, distributed by RI-RS grid rows.
969!>
970!> Let psi_li=sum_mu phi_mu(r_l) C_mu,i(k), and let m be the midpoint
971!> between the highest occupied and lowest virtual eigenvalue at each k/spin.
972!> Gocc_ll'(tau)=sum_i psi_li conjg(psi_l'i) exp[(epsilon_i-m)tau].
973!> Gvir_ll'(tau)=sum_a psi_la conjg(psi_l'a) exp[-(epsilon_a-m)tau].
974!> These are the projected AO Green matrices, evaluated associatively.
975!> With f_k=spin_degeneracy*w_k and d_ia,c=-i*dipole_ia,c/sqrt(volume):
976!> B_ll'(tau)=sum_k f_k conjg(Gocc_ll') Gvir_ll'.
977!> E_lc(tau)=sum_kia f_k conjg(psi_li) psi_la d_ia,c exp[-Delta_ia*tau].
978!> P_cd(tau)=sum_kia f_k conjg(d_ia,c) d_ia,d exp[-Delta_ia*tau].
979!> Each rank owns disjoint rows of B and E; only the small P is replicated.
980!> A k-owner broadcasts its orbitals once, then all ranks process owned rows.
981!> No RI-pair by transition tensor or replicated grid-pair cache is formed.
982!> The cache is created once and reused for all frequencies. The existing
983!> minimax cosine transform approximates 2*Delta/(Delta**2+omega**2);
984!> its relative kernel error is checked independently of the iterative solve.
985!> \param bs_env ...
986!> \param psi ...
987!> \param dipole ...
988!> \param volume ...
989!> \param first_row First owned grid row.
990!> \param chi_tau Owned grid rows of B(tau).
991!> \param wing_tau Owned grid rows of E(tau).
992!> \param head_tau P(tau).
993! **************************************************************************************************
994 SUBROUTINE accumulate_time_state(bs_env, psi, dipole, volume, first_row, chi_tau, wing_tau, head_tau)
995 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
996 COMPLEX(KIND=dp), INTENT(IN) :: psi(:, :, :, :), dipole(:, :, :, :, :)
997 REAL(kind=dp), INTENT(IN) :: volume
998 INTEGER, INTENT(OUT) :: first_row
999 COMPLEX(KIND=dp), ALLOCATABLE, INTENT(OUT) :: chi_tau(:, :, :), wing_tau(:, :, :), &
1000 head_tau(:, :, :)
1001
1002 CHARACTER(LEN=*), PARAMETER :: routinen = 'accumulate_time_state'
1003
1004 COMPLEX(KIND=dp), ALLOCATABLE :: gamma_corr(:, :, :), orbitals(:, :)
1005 INTEGER :: c, handle, k, local_k, n_grid, n_rows, &
1006 n_time, no, nv, owner, spin
1007
1008 CALL timeset(routinen, handle)
1009
1010 cpassert(volume > 0.0_dp)
1011 n_grid = bs_env%ri_rs%n_grid_points
1012 n_time = bs_env%num_time_freq_points
1013 CALL block_range(n_grid, bs_env%para_env%mepos, bs_env%para_env%num_pe, first_row, n_rows)
1014 ALLOCATE (chi_tau(n_rows, n_grid, n_time), wing_tau(n_rows, 3, n_time), &
1015 head_tau(3, 3, n_time), source=z_zero)
1016 CALL validate_time_transform(bs_env)
1017 DO spin = 1, bs_env%n_spin
1018 no = bs_env%n_occ(spin)
1019 nv = bs_env%n_mo_retained - no
1020 cpassert(nv >= 0)
1021 IF (no == 0 .OR. nv == 0) cycle
1022 ALLOCATE (orbitals(bs_env%ri_rs%n_grid_points, no + nv), gamma_corr(no, nv, 3))
1023 DO k = 1, bs_env%nkp_scf_desymm
1024 owner = modulo(k - 1, bs_env%para_env%num_pe)
1025 IF (bs_env%para_env%mepos == owner) THEN
1026 local_k = (k - 1)/bs_env%para_env%num_pe + 1
1027 cpassert(local_k <= SIZE(psi, 3))
1028 orbitals(:, :) = psi(:, 1:no + nv, local_k, spin)
1029 DO c = 1, 3
1030 gamma_corr(:, :, c) = -gaussi*dipole(spin, local_k, c, 1:no, 1:nv)/sqrt(volume)
1031 END DO
1032 END IF
1033 CALL bs_env%para_env%bcast(orbitals, owner)
1034 CALL bs_env%para_env%bcast(gamma_corr, owner)
1035 CALL accumulate_k_time_rows(bs_env, orbitals, gamma_corr, k, spin, owner, &
1036 first_row, chi_tau, wing_tau, head_tau)
1037 END DO
1038 DEALLOCATE (orbitals, gamma_corr)
1039 END DO
1040 CALL bs_env%para_env%sum(head_tau)
1041
1042 CALL timestop(handle)
1043
1044 END SUBROUTINE accumulate_time_state
1045
1046! **************************************************************************************************
1047!> \brief Weight only owned orbital rows in Green and Gamma-correction products; accumulate the tiny head once.
1048!> Gocc_ll'=sum_i [psi_li exp((epsilon_i-m)tau)] conjg(psi_l'i), and analogously for Gvir.
1049!> The wing uses both fully weighted local factors: conjg(psi_li) psi_la d_ia exp(-Delta_ia*tau).
1050!> Global orbitals remain unweighted, avoiding a replicated time-weighted orbital array.
1051!> \param bs_env ...
1052!> \param orbitals ...
1053!> \param gamma_corr Transition moments entering the Gamma correction.
1054!> \param k ...
1055!> \param spin ...
1056!> \param owner ...
1057!> \param first_row ...
1058!> \param chi_tau ...
1059!> \param wing_tau ...
1060!> \param head_tau ...
1061! **************************************************************************************************
1062 SUBROUTINE accumulate_k_time_rows(bs_env, orbitals, gamma_corr, k, spin, owner, &
1063 first_row, chi_tau, wing_tau, head_tau)
1064 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
1065 COMPLEX(KIND=dp), INTENT(IN) :: orbitals(:, :), gamma_corr(:, :, :)
1066 INTEGER, INTENT(IN) :: k, spin, owner, first_row
1067 COMPLEX(KIND=dp), INTENT(INOUT) :: chi_tau(:, :, :), wing_tau(:, :, :), &
1068 head_tau(:, :, :)
1069
1070 COMPLEX(KIND=dp), ALLOCATABLE :: block(:, :), go(:, :), gv(:, :), &
1071 scaled(:, :)
1072 INTEGER :: a, c, d, first, i, last, ng, no, nr, nv, &
1073 t
1074 REAL(kind=dp) :: head_weight, mid, tau, weight
1075 REAL(kind=dp), ALLOCATABLE :: occ_factor(:), vir_factor(:)
1076 TYPE(gwkp_type), POINTER :: gwkp_env
1077
1078 gwkp_env => bs_env%gwkp_env
1079
1080 no = bs_env%n_occ(spin)
1081 nv = bs_env%n_mo_retained - no
1082 cpassert(nv >= 0)
1083 ng = bs_env%ri_rs%n_grid_points
1084 nr = SIZE(chi_tau, 1)
1085 first = first_row
1086 last = first + nr - 1
1087 mid = 0.5_dp*(gwkp_env%eigenvalues_G(no, k, spin) + gwkp_env%eigenvalues_G(no + 1, k, spin))
1088 weight = bs_env%spin_degeneracy*bs_env%kpoints_scf_desymm%wkp(k)
1089 ALLOCATE (occ_factor(no), vir_factor(nv), scaled(nr, no + nv))
1090 ALLOCATE (go(nr, ng), gv(nr, ng), block(nr, no))
1091 DO t = 1, bs_env%num_time_freq_points
1092 tau = bs_env%time_frequency_grid%imaginary_time(t)
1093 DO i = 1, no
1094 occ_factor(i) = exp(0.5_dp*(gwkp_env%eigenvalues_G(i, k, spin) - mid)*tau)
1095 scaled(:, i) = orbitals(first:last, i)*occ_factor(i)**2
1096 END DO
1097 DO a = 1, nv
1098 vir_factor(a) = exp(-0.5_dp*(gwkp_env%eigenvalues_G(no + a, k, spin) - mid)*tau)
1099 scaled(:, no + a) = orbitals(first:last, no + a)*vir_factor(a)**2
1100 END DO
1101 IF (nr > 0) THEN
1102 CALL zgemm('N', 'C', nr, ng, no, z_one, scaled, nr, &
1103 orbitals, ng, z_zero, go, nr)
1104 CALL zgemm('N', 'C', nr, ng, nv, z_one, scaled(1, no + 1), nr, &
1105 orbitals(:, no + 1:), ng, z_zero, gv, nr)
1106 chi_tau(:, :, t) = chi_tau(:, :, t) + weight*conjg(go)*gv
1107 DO c = 1, 3
1108 CALL zgemm('N', 'T', nr, no, nv, z_one, scaled(1, no + 1), nr, &
1109 gamma_corr(:, :, c), no, z_zero, block, nr)
1110 wing_tau(:, c, t) = wing_tau(:, c, t) + &
1111 weight*sum(conjg(scaled(:, 1:no))*block, dim=2)
1112 END DO
1113 END IF
1114 IF (bs_env%para_env%mepos == owner) THEN
1115 DO a = 1, nv
1116 DO i = 1, no
1117 head_weight = weight*(occ_factor(i)*vir_factor(a))**2
1118 DO d = 1, 3
1119 DO c = 1, 3
1120 head_tau(c, d, t) = head_tau(c, d, t) + &
1121 head_weight*conjg(gamma_corr(i, a, c))*gamma_corr(i, a, d)
1122 END DO
1123 END DO
1124 END DO
1125 END DO
1126 END IF
1127 END DO
1128 DEALLOCATE (occ_factor, vir_factor, scaled, go, gv, block)
1129 END SUBROUTINE accumulate_k_time_rows
1130
1131! **************************************************************************************************
1132!> \brief Form T_lQ = sum_P Z_lP F_PQ once on the owned grid rows.
1133!> The same T is reused for all frequencies; no replicated grid-by-RI map is formed.
1134!> \param Z ...
1135!> \param F ...
1136!> \param first_row First owned grid row.
1137!> \param nr Number of owned grid rows.
1138!> \param metric_rows ...
1139! **************************************************************************************************
1140 SUBROUTINE metric_grid_rows(Z, F, first_row, nr, metric_rows)
1141 COMPLEX(KIND=dp), INTENT(IN) :: z(:, :), f(:, :)
1142 INTEGER, INTENT(IN) :: first_row, nr
1143 COMPLEX(KIND=dp), ALLOCATABLE, INTENT(OUT) :: metric_rows(:, :)
1144
1145 CHARACTER(LEN=*), PARAMETER :: routinen = 'metric_grid_rows'
1146
1147 COMPLEX(KIND=dp), ALLOCATABLE :: z_rows(:, :)
1148 INTEGER :: handle
1149
1150 CALL timeset(routinen, handle)
1151
1152 ALLOCATE (metric_rows(nr, SIZE(f, 2)))
1153 IF (nr > 0) THEN
1154 ALLOCATE (z_rows, source=z(first_row:first_row + nr - 1, :))
1155 CALL zgemm('N', 'N', nr, SIZE(f, 2), SIZE(f, 1), z_one, &
1156 z_rows, nr, f, SIZE(f, 1), z_zero, metric_rows, nr)
1157 DEALLOCATE (z_rows)
1158 END IF
1159
1160 CALL timestop(handle)
1161
1162 END SUBROUTINE metric_grid_rows
1163
1164! **************************************************************************************************
1165!> \brief Apply T^H chi T with distributed rows and reductions of three-column vectors.
1166!> The grid vector is assembled from disjoint row contributions, never from replicated T.
1167!> \param bs_env ...
1168!> \param T_rows ...
1169!> \param chi ...
1170!> \param first_row ...
1171!> \param x ...
1172!> \param y ...
1173!> \param aux_y ...
1174!> \param grid_x ...
1175!> \param grid_y ...
1176! **************************************************************************************************
1177 SUBROUTINE apply_gamma_corr_body(bs_env, T_rows, chi, first_row, x, y, aux_y, grid_x, grid_y)
1178 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
1179 COMPLEX(KIND=dp), INTENT(IN) :: t_rows(:, :), chi(:, :)
1180 INTEGER, INTENT(IN) :: first_row
1181 COMPLEX(KIND=dp), INTENT(IN) :: x(:, :)
1182 COMPLEX(KIND=dp), INTENT(OUT) :: y(:, :)
1183 COMPLEX(KIND=dp), INTENT(INOUT) :: aux_y(:, :), grid_x(:, :), grid_y(:, :)
1184
1185 INTEGER :: i_last, nc, nr
1186
1187 nr = SIZE(chi, 1)
1188 nc = SIZE(x, 2)
1189 i_last = first_row + nr - 1
1190 grid_x = z_zero
1191 IF (nr > 0) THEN
1192 CALL zgemm('N', 'N', nr, nc, SIZE(t_rows, 2), z_one, t_rows, nr, &
1193 x, SIZE(x, 1), z_zero, grid_y, nr)
1194 grid_x(first_row:i_last, :) = grid_y
1195 END IF
1196 CALL bs_env%para_env%sum(grid_x)
1197 aux_y = z_zero
1198 IF (nr > 0) THEN
1199 CALL zgemm('N', 'N', nr, nc, SIZE(chi, 2), z_one, chi, nr, &
1200 grid_x, SIZE(grid_x, 1), z_zero, grid_y, nr)
1201 CALL zgemm('C', 'N', SIZE(t_rows, 2), nc, nr, z_one, t_rows, nr, &
1202 grid_y, nr, z_zero, aux_y, SIZE(aux_y, 1))
1203 END IF
1204 CALL bs_env%para_env%sum(aux_y)
1205 y = aux_y
1206 END SUBROUTINE apply_gamma_corr_body
1207
1208! **************************************************************************************************
1209!> \brief Independent conjugate gradients for three directions, sharing matrix actions and reductions.
1210!> Each column c retains its own alpha_c, beta_c and convergence test; columns are not coupled.
1211!> \param bs_env ...
1212!> \param T_rows ...
1213!> \param chi ...
1214!> \param first_row ...
1215!> \param b ...
1216!> \param x ...
1217! **************************************************************************************************
1218 SUBROUTINE solve_gamma_corr_body(bs_env, T_rows, chi, first_row, b, x)
1219 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
1220 COMPLEX(KIND=dp), INTENT(IN) :: t_rows(:, :), chi(:, :)
1221 INTEGER, INTENT(IN) :: first_row
1222 COMPLEX(KIND=dp), INTENT(IN) :: b(:, :)
1223 COMPLEX(KIND=dp), INTENT(OUT) :: x(:, :)
1224
1225 CHARACTER(LEN=*), PARAMETER :: routinen = 'solve_gamma_corr_body'
1226 REAL(kind=dp), PARAMETER :: tolerance = 1.0e-12_dp
1227
1228 COMPLEX(KIND=dp) :: denominator
1229 COMPLEX(KIND=dp), ALLOCATABLE :: a_direction(:, :), aux_y(:, :), &
1230 direction(:, :), grid_x(:, :), &
1231 grid_y(:, :), r(:, :)
1232 INTEGER :: handle, i_c, i_iter, nc
1233 REAL(kind=dp) :: alpha, rr(SIZE(b, 2)), &
1234 rr_converged(SIZE(b, 2)), rr_new
1235
1236 CALL timeset(routinen, handle)
1237
1238 nc = SIZE(b, 2)
1239 x = z_zero
1240 DO i_c = 1, nc
1241 rr(i_c) = real(dot_product(b(:, i_c), b(:, i_c)), dp)
1242 END DO
1243 rr_converged = tolerance**2*rr
1244 ALLOCATE (grid_x(SIZE(chi, 2), nc), grid_y(SIZE(chi, 1), nc), aux_y(SIZE(t_rows, 2), nc))
1245 ALLOCATE (r, source=b)
1246 ALLOCATE (direction, source=b)
1247 ALLOCATE (a_direction(SIZE(b, 1), nc))
1248 DO i_iter = 1, 4*SIZE(b, 1)
1249 IF (all(rr <= rr_converged)) EXIT
1250 CALL apply_gamma_corr_body(bs_env, t_rows, chi, first_row, direction, a_direction, aux_y, grid_x, grid_y)
1251 a_direction(:, :) = a_direction + direction
1252 DO i_c = 1, nc
1253 IF (rr(i_c) <= rr_converged(i_c)) cycle
1254 denominator = dot_product(direction(:, i_c), a_direction(:, i_c))
1255 IF (real(denominator, dp) <= 0.0_dp) THEN
1256 cpabort('Gamma body action is not positive')
1257 END IF
1258 alpha = rr(i_c)/real(denominator, dp)
1259 x(:, i_c) = x(:, i_c) + alpha*direction(:, i_c)
1260 r(:, i_c) = r(:, i_c) - alpha*a_direction(:, i_c)
1261 rr_new = real(dot_product(r(:, i_c), r(:, i_c)), dp)
1262 direction(:, i_c) = r(:, i_c) + (rr_new/rr(i_c))*direction(:, i_c)
1263 rr(i_c) = rr_new
1264 END DO
1265 END DO
1266 IF (any(rr > rr_converged)) THEN
1267 CALL cp_warn(__location__, "Gamma-point body CG not converged after "// &
1268 trim(cp_to_string(4*SIZE(b, 1)))//" iterations")
1269 END IF
1270
1271 CALL timestop(handle)
1272
1273 END SUBROUTINE solve_gamma_corr_body
1274
1275! **************************************************************************************************
1276!> \brief Contract M_pq=Σ_ABPQ U^A_Pp [Σ_R S_AB,PQ(R)] U^B_Qq using atom-local U (1).
1277!> P,Q enumerate reference functions on atoms A,B; p,q enumerate their optimized
1278!> functions. Each distributed S element contributes once. No atom pair, image,
1279!> or coefficient is screened. Sum the resulting optimized columns over MPI (2).
1280!> Only atom-local transformation rows and one atom-pair intermediate are stored;
1281!> the final optimized M remains replicated. This is exactly the real q=0 metric.
1282!> \param images ...
1283!> \param auto_ri ...
1284!> \param metric ...
1285! **************************************************************************************************
1286 SUBROUTINE gamma_metric_contract_atoms(images, auto_ri, metric)
1287 TYPE(cp_fm_type), INTENT(IN) :: images(:)
1288 TYPE(auto_ri_type), INTENT(IN) :: auto_ri
1289 COMPLEX(KIND=dp), INTENT(OUT) :: metric(:, :)
1290
1291 CHARACTER(LEN=*), PARAMETER :: routinen = 'gamma_metric_contract_atoms'
1292
1293 INTEGER :: handle
1294 TYPE(gamma_atom_projection_type), ALLOCATABLE :: left(:), right(:)
1295
1296 CALL timeset(routinen, handle)
1297
1298 CALL prepare_gamma_atom_projections(images, auto_ri, left, right)
1299 CALL accumulate_gamma_atom_metric(images, left, right, metric)
1300 DEALLOCATE (left, right)
1301
1302 CALL timestop(handle)
1303
1304 END SUBROUTINE gamma_metric_contract_atoms
1305
1306! **************************************************************************************************
1307!> \brief Extract the distributed reference rows of each atom-local U in equation (1).
1308!> \param images ...
1309!> \param auto_ri ...
1310!> \param left ...
1311!> \param right ...
1312! **************************************************************************************************
1313 SUBROUTINE prepare_gamma_atom_projections(images, auto_ri, left, right)
1314 TYPE(cp_fm_type), INTENT(IN) :: images(:)
1315 TYPE(auto_ri_type), INTENT(IN) :: auto_ri
1316 TYPE(gamma_atom_projection_type), ALLOCATABLE, &
1317 INTENT(OUT) :: left(:), right(:)
1318
1319 INTEGER :: nc, ngc, ngr, nr
1320 INTEGER, POINTER :: cols(:), rows(:)
1321 LOGICAL :: valid_layout
1322
1323 cpassert(SIZE(images) > 0)
1324 cpassert(auto_ri%ready)
1325 cpassert(ALLOCATED(auto_ri%sizes_ref_RI))
1326 cpassert(ALLOCATED(auto_ri%sizes_opt_RI))
1327 cpassert(ALLOCATED(auto_ri%U_Pp_atom))
1328 cpassert(ALLOCATED(auto_ri%U_Pp_atom_offset))
1329 cpassert(SIZE(auto_ri%sizes_ref_RI) > 0)
1330 cpassert(SIZE(auto_ri%sizes_ref_RI) == SIZE(auto_ri%sizes_opt_RI))
1331 cpassert(SIZE(auto_ri%U_Pp_atom_offset) == SIZE(auto_ri%sizes_ref_RI))
1332 valid_layout = all(auto_ri%sizes_ref_RI > 0) .AND. all(auto_ri%sizes_opt_RI > 0)
1333 cpassert(valid_layout)
1334 CALL cp_fm_get_info(images(1), nrow_local=nr, ncol_local=nc, &
1335 nrow_global=ngr, ncol_global=ngc, row_indices=rows, col_indices=cols)
1336 cpassert(ngr == sum(auto_ri%sizes_ref_RI) .AND. ngc == ngr)
1337 CALL extract_gamma_atom_rows(auto_ri, rows(1:nr), left)
1338 CALL extract_gamma_atom_rows(auto_ri, cols(1:nc), right)
1339 END SUBROUTINE prepare_gamma_atom_projections
1340
1341! **************************************************************************************************
1342!> \brief Map local reference indices onto their atom blocks in equation (1).
1343!> \param auto_ri ...
1344!> \param indices ...
1345!> \param blocks ...
1346! **************************************************************************************************
1347 SUBROUTINE extract_gamma_atom_rows(auto_ri, indices, blocks)
1348 TYPE(auto_ri_type), INTENT(IN) :: auto_ri
1349 INTEGER, INTENT(IN) :: indices(:)
1350 TYPE(gamma_atom_projection_type), ALLOCATABLE, &
1351 INTENT(OUT) :: blocks(:)
1352
1353 INTEGER :: first_opt, first_ref, iatom, icol, &
1354 ilocal, ipos, irow, nopt, nref, offset
1355
1356 ALLOCATE (blocks(SIZE(auto_ri%sizes_ref_RI)))
1357 first_ref = 1
1358 first_opt = 1
1359 DO iatom = 1, SIZE(blocks)
1360 nref = auto_ri%sizes_ref_RI(iatom)
1361 nopt = auto_ri%sizes_opt_RI(iatom)
1362 offset = auto_ri%U_Pp_atom_offset(iatom)
1363 cpassert(offset > 0 .AND. nref <= huge(0)/nopt)
1364 cpassert(offset - 1 <= SIZE(auto_ri%U_Pp_atom) - nref*nopt)
1365 ALLOCATE (blocks(iatom)%positions(count(indices >= first_ref .AND. indices < first_ref + nref)))
1366 ALLOCATE (blocks(iatom)%values(SIZE(blocks(iatom)%positions), nopt))
1367 blocks(iatom)%first = first_opt
1368 ipos = 0
1369 DO ilocal = 1, SIZE(indices)
1370 irow = indices(ilocal) - first_ref + 1
1371 IF (irow < 1 .OR. irow > nref) cycle
1372 ipos = ipos + 1
1373 blocks(iatom)%positions(ipos) = ilocal
1374 DO icol = 1, nopt
1375 blocks(iatom)%values(ipos, icol) = auto_ri%U_Pp_atom(offset + irow - 1 + (icol - 1)*nref)
1376 END DO
1377 END DO
1378 first_ref = first_ref + nref
1379 first_opt = first_opt + nopt
1380 END DO
1381 END SUBROUTINE extract_gamma_atom_rows
1382
1383! **************************************************************************************************
1384!> \brief Evaluate atom-pair products (1) locally and reduce optimized columns (2).
1385!> \param images ...
1386!> \param left ...
1387!> \param right ...
1388!> \param metric ...
1389! **************************************************************************************************
1390 SUBROUTINE accumulate_gamma_atom_metric(images, left, right, metric)
1391 TYPE(cp_fm_type), INTENT(IN) :: images(:)
1392 TYPE(gamma_atom_projection_type), INTENT(IN) :: left(:), right(:)
1393 COMPLEX(KIND=dp), INTENT(OUT) :: metric(:, :)
1394
1395 INTEGER :: ia, ib, icell, n_opt_total, nc, noleft, &
1396 nopt, noright, nr, nref, pfirst, qfirst
1397 REAL(kind=dp), ALLOCATABLE :: block(:, :), block_right(:, :), &
1398 value(:, :)
1399
1400 nopt = maxval([(SIZE(left(ia)%values, 2), ia=1, SIZE(left))])
1401 nref = max(1, maxval([(SIZE(left(ia)%positions), ia=1, SIZE(left))]), &
1402 maxval([(SIZE(right(ib)%positions), ib=1, SIZE(right))]))
1403 n_opt_total = sum([(SIZE(left(ia)%values, 2), ia=1, SIZE(left))])
1404 cpassert(all(shape(metric) == n_opt_total))
1405 ALLOCATE (block(nref, nref), block_right(nref, nopt), value(nopt, nopt))
1406 metric = z_zero
1407 DO ib = 1, SIZE(right)
1408 nc = SIZE(right(ib)%positions)
1409 noright = SIZE(right(ib)%values, 2)
1410 qfirst = right(ib)%first
1411 DO ia = 1, SIZE(left)
1412 nr = SIZE(left(ia)%positions)
1413 IF (nr == 0 .OR. nc == 0) cycle
1414 noleft = SIZE(left(ia)%values, 2)
1415 pfirst = left(ia)%first
1416 block(1:nr, 1:nc) = 0.0_dp
1417 DO icell = 1, SIZE(images)
1418 block(1:nr, 1:nc) = block(1:nr, 1:nc) + &
1419 images(icell)%local_data(left(ia)%positions, right(ib)%positions)
1420 END DO
1421 CALL dgemm('N', 'N', nr, noright, nc, 1.0_dp, block, nref, &
1422 right(ib)%values, nc, 0.0_dp, block_right, nref)
1423 CALL dgemm('T', 'N', noleft, noright, nr, 1.0_dp, left(ia)%values, nr, &
1424 block_right, nref, 0.0_dp, value, nopt)
1425 metric(pfirst:pfirst + noleft - 1, qfirst:qfirst + noright - 1) = &
1426 cmplx(value(1:noleft, 1:noright), 0.0_dp, dp)
1427 END DO
1428 cpassert(noright <= huge(0)/SIZE(metric, 1))
1429 CALL images(1)%matrix_struct%para_env%sum(metric(:, qfirst:qfirst + noright - 1))
1430 END DO
1431 DEALLOCATE (block, block_right, value)
1432 END SUBROUTINE accumulate_gamma_atom_metric
1433
1434! **************************************************************************************************
1435!> \brief Construct F=M^-1 V^1/2 with distributed Hermitian eigensystems.
1436!> For each input A, ifirst set A=(A+Aᴴ)/2 (1), then diagonalize A=U diag(λ) Uᴴ.
1437!> Define A^a=U diag(f(λ)) Uᴴ (2), where f(λ)=λ^a only for λ>max(0,η λ_max),
1438!> and zero otherwise. M uses a=-1 and the existing relative cutoff η; V uses
1439!> a=1/2 and η=0. Finally F=M^-1 (V^1/2)ᴴ (3), matching the dense Γ path.
1440!> Inputs and the final F remain replicated; eigenvectors and matrix products
1441!> are block-cyclic distributed. No spectral threshold or physical input changes.
1442!> \param para_env ...
1443!> \param relative_cutoff ...
1444!> \param metric ...
1445!> \param coulomb ...
1446!> \param factor ...
1447! **************************************************************************************************
1448 SUBROUTINE gamma_metric_factor_distributed(para_env, relative_cutoff, metric, coulomb, factor)
1449 TYPE(mp_para_env_type), INTENT(INOUT), TARGET :: para_env
1450 REAL(kind=dp), INTENT(IN) :: relative_cutoff
1451 COMPLEX(KIND=dp), INTENT(IN) :: metric(:, :), coulomb(:, :)
1452 COMPLEX(KIND=dp), INTENT(OUT) :: factor(:, :)
1453
1454 CHARACTER(LEN=*), PARAMETER :: routinen = 'gamma_metric_factor_distributed'
1455
1456 INTEGER :: handle, n
1457 TYPE(cp_blacs_env_type), POINTER :: blacs
1458 TYPE(cp_cfm_type) :: coulomb_d, factor_d, metric_d, work_d
1459 TYPE(cp_fm_struct_type), POINTER :: structure
1460
1461 CALL timeset(routinen, handle)
1462
1463 n = SIZE(metric, 1)
1464 CALL cp_blacs_env_create(blacs, para_env)
1465 CALL cp_fm_struct_create(structure, context=blacs, nrow_global=n, ncol_global=n)
1466 CALL cp_cfm_create(metric_d, structure)
1467 CALL cp_cfm_create(coulomb_d, structure)
1468 CALL cp_cfm_create(work_d, structure)
1469 CALL cp_cfm_create(factor_d, structure)
1470 CALL cp_cfm_set_submatrix(metric_d, metric)
1471 CALL cp_cfm_set_submatrix(coulomb_d, coulomb)
1472 CALL retained_hermitian_power(metric_d, work_d, factor_d, n, -1.0_dp, relative_cutoff)
1473 CALL retained_hermitian_power(coulomb_d, work_d, factor_d, n, 0.5_dp, 0.0_dp)
1474 CALL parallel_gemm('N', 'C', n, n, n, z_one, metric_d, coulomb_d, z_zero, factor_d)
1475 CALL collect_factor_columns(factor_d, factor)
1476 CALL cp_cfm_release(metric_d)
1477 CALL cp_cfm_release(coulomb_d)
1478 CALL cp_cfm_release(work_d)
1479 CALL cp_cfm_release(factor_d)
1480 CALL cp_fm_struct_release(structure)
1481 CALL cp_blacs_env_release(blacs)
1482
1483 CALL timestop(handle)
1484
1485 END SUBROUTINE gamma_metric_factor_distributed
1486
1487! **************************************************************************************************
1488!> \brief Apply Hermitization (1) and the strictly retained spectral power (2).
1489!> \param matrix ...
1490!> \param eigenvectors ...
1491!> \param work ...
1492!> \param n ...
1493!> \param exponent ...
1494!> \param relative_cutoff ...
1495! **************************************************************************************************
1496 SUBROUTINE retained_hermitian_power(matrix, eigenvectors, work, n, exponent, relative_cutoff)
1497 TYPE(cp_cfm_type), INTENT(INOUT) :: matrix, eigenvectors, work
1498 INTEGER, INTENT(IN) :: n
1499 REAL(kind=dp), INTENT(IN) :: exponent, relative_cutoff
1500
1501 COMPLEX(KIND=dp), ALLOCATABLE :: scales(:)
1502 INTEGER :: i_mode
1503 REAL(kind=dp) :: cutoff
1504 REAL(kind=dp), ALLOCATABLE :: eigenvalues(:)
1505
1506 CALL cp_cfm_transpose(matrix, 'C', work)
1507 CALL cp_cfm_scale_and_add(cmplx(0.5_dp, 0.0_dp, dp), matrix, cmplx(0.5_dp, 0.0_dp, dp), work)
1508 ALLOCATE (eigenvalues(n), scales(n))
1509 CALL cp_cfm_heevd(matrix, eigenvectors, eigenvalues)
1510 cutoff = max(0.0_dp, relative_cutoff*maxval(eigenvalues))
1511 scales = z_zero
1512 DO i_mode = 1, n
1513 IF (eigenvalues(i_mode) > cutoff) scales(i_mode) = cmplx(eigenvalues(i_mode)**(0.5_dp*exponent), 0.0_dp, dp)
1514 END DO
1515 CALL cp_cfm_column_scale(eigenvectors, scales)
1516 CALL parallel_gemm('N', 'C', n, n, n, z_one, eigenvectors, eigenvectors, z_zero, matrix)
1517 DEALLOCATE (eigenvalues, scales)
1518 END SUBROUTINE retained_hermitian_power
1519
1520! **************************************************************************************************
1521!> \brief Collect factor (3) by BLACS column blocks with bounded MPI element counts.
1522!> \param matrix ...
1523!> \param factor ...
1524! **************************************************************************************************
1525 SUBROUTINE collect_factor_columns(matrix, factor)
1526 TYPE(cp_cfm_type), INTENT(IN) :: matrix
1527 COMPLEX(KIND=dp), INTENT(OUT) :: factor(:, :)
1528
1529 INTEGER :: i_column, i_last, n_columns, n_rows
1530
1531 n_rows = SIZE(factor, 1)
1532 cpassert(n_rows > 0)
1533 n_columns = min(matrix%matrix_struct%ncol_block, huge(0)/n_rows)
1534 cpassert(n_columns > 0)
1535 i_column = 1
1536 DO
1537 i_last = i_column + (min(n_columns, SIZE(factor, 2) - i_column + 1) - 1)
1538 CALL cp_cfm_get_submatrix(matrix, factor(:, i_column:i_last), start_col=i_column)
1539 IF (i_last == SIZE(factor, 2)) EXIT
1540 i_column = i_last + 1
1541 END DO
1542 END SUBROUTINE collect_factor_columns
1543
1544! **************************************************************************************************
1545!> \brief Form Gamma-correction d_ia^α(k) for cyclically owned SCF k points.
1546!> Reuses the existing AO integral builders and Bloch transformation.
1547!> \brief Interband Gamma-correction moments in the same SCF MO gauge as the GW body.
1548!> The SCF Hamiltonian contains the nonlocal pseudopotential; its k derivative
1549!> therefore includes the nonlocal contribution without a separate commutator.
1550!> \param bs_env ...
1551!> \param gwkp_env Gamma-correction AO operators, released after their contraction.
1552!> \param kpoints ...
1553!> \param mo_coeff ...
1554!> \param eigenvalues ...
1555!> \param nocc_spin ...
1556!> \param dipole ...
1557! **************************************************************************************************
1558 SUBROUTINE gw_gamma_transition_moments(bs_env, gwkp_env, kpoints, mo_coeff, eigenvalues, nocc_spin, &
1559 dipole)
1560 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
1561 TYPE(gwkp_type), INTENT(INOUT) :: gwkp_env
1562 TYPE(kpoint_type), INTENT(IN), POINTER :: kpoints
1563 TYPE(cp_cfm_type), DIMENSION(:, :), INTENT(IN) :: mo_coeff
1564 REAL(kind=dp), DIMENSION(:, :, :), INTENT(IN) :: eigenvalues
1565 INTEGER, DIMENSION(:), INTENT(IN) :: nocc_spin
1566 COMPLEX(KIND=dp), ALLOCATABLE, &
1567 DIMENSION(:, :, :, :, :), INTENT(OUT) :: dipole
1568
1569 CHARACTER(LEN=*), PARAMETER :: routinen = 'gw_gamma_transition_moments'
1570 REAL(kind=dp), PARAMETER :: eps_degenerate = 1.0e-10_dp
1571
1572 COMPLEX(KIND=dp), ALLOCATABLE :: bloch(:, :, :, :), coeff_owned(:, :, :, :), &
1573 operators_k(:, :, :), projected_d(:, :), projected_h(:, :), projected_s(:, :), &
1574 recv_buffer(:)
1575 INTEGER :: first_col, first_k, first_rank_col, handle, i_dir, i_k, i_k_local, i_mo_a, &
1576 i_mo_i, i_rank, i_spin, ib, n_ao, n_batch, n_col, n_img, n_k, n_k_local, n_mat, n_mo, &
1577 n_mo_matrix, n_rank_col, n_row_matrix, n_spin, n_virt, nocc
1578 INTEGER, ALLOCATABLE :: nmo_spin(:), recv_counts(:), &
1579 recv_displs(:), send_counts(:), &
1580 send_displs(:)
1581 INTEGER, DIMENSION(3) :: periodic
1582 INTEGER, DIMENSION(:, :), POINTER :: index_to_cell
1583 INTEGER, DIMENSION(:, :, :), POINTER :: cell_to_index
1584 LOGICAL :: valid_layout
1585 REAL(kind=dp) :: energy_diff, hmat(3, 3)
1586 REAL(kind=dp), ALLOCATABLE :: cell_cartesian(:, :), d_cols(:, :, :, :), h_cols(:, :, :, :), &
1587 phase_angle(:, :), phase_cos(:, :), phase_sin(:, :), s_cols(:, :, :, :), weight_im(:, :), &
1588 weight_re(:, :)
1589 TYPE(cell_type), POINTER :: cell
1590 TYPE(mp_para_env_type), POINTER :: para_env
1591
1592 CALL timeset(routinen, handle)
1593
1594 cell => bs_env%ri_rs%cell
1595 para_env => bs_env%para_env
1596 index_to_cell => gwkp_env%gamma_corr_index_to_cell
1597 cell_to_index => gwkp_env%gamma_corr_cell_to_index
1598 cpassert(ASSOCIATED(index_to_cell) .AND. ASSOCIATED(cell_to_index))
1599 CALL get_cell(cell=cell, h=hmat, periodic=periodic)
1600 CALL get_kpoint_info(kpoints, nkp=n_k)
1601 n_spin = bs_env%n_spin
1602 cpassert(SIZE(mo_coeff, 1) >= n_k .AND. SIZE(mo_coeff, 2) == n_spin)
1603 valid_layout = SIZE(eigenvalues, 2) >= n_k .AND. SIZE(eigenvalues, 3) == n_spin
1604 cpassert(valid_layout)
1605 cpassert(SIZE(nocc_spin) == n_spin)
1606 ALLOCATE (nmo_spin(n_spin))
1607 DO i_spin = 1, n_spin
1608 CALL cp_cfm_get_info(mo_coeff(1, i_spin), nrow_global=n_row_matrix, ncol_global=n_mo_matrix)
1609 IF (i_spin == 1) n_ao = n_row_matrix
1610 valid_layout = n_row_matrix == n_ao .AND. n_mo_matrix == SIZE(eigenvalues, 1)
1611 cpassert(valid_layout)
1612 nmo_spin(i_spin) = n_mo_matrix
1613 END DO
1614 cpassert(all(nocc_spin >= 0 .AND. nocc_spin <= nmo_spin))
1615
1616 ! Each rank receives all rows and images of its AO columns; no rank holds all images.
1617 n_img = SIZE(index_to_cell, 2)
1618 CALL gamma_corr_image_columns(gwkp_env%gamma_corr_s, gwkp_env%gamma_corr_scf_plan, cell_to_index, &
1619 n_ao, n_img, para_env, s_cols)
1620 CALL gamma_corr_image_columns(gwkp_env%gamma_corr_h, gwkp_env%gamma_corr_scf_plan, cell_to_index, &
1621 n_ao, n_img, para_env, h_cols)
1622 CALL gamma_corr_image_columns(gwkp_env%gamma_corr_d, gwkp_env%gamma_corr_plan, cell_to_index, &
1623 n_ao, n_img, para_env, d_cols)
1624 CALL dbcsr_deallocate_matrix_set(gwkp_env%gamma_corr_s)
1625 CALL dbcsr_deallocate_matrix_set(gwkp_env%gamma_corr_h)
1626 CALL dbcsr_deallocate_matrix_set(gwkp_env%gamma_corr_d)
1627 gwkp_env%gamma_corr_plan = kp_transform_plan_type()
1628 gwkp_env%gamma_corr_scf_plan = kp_transform_plan_type()
1629 CALL block_range(n_ao, para_env%mepos, para_env%num_pe, first_col, n_col)
1630 ALLOCATE (cell_cartesian(3, n_img))
1631 cell_cartesian(:, :) = matmul(hmat, real(index_to_cell, dp))
1632 ALLOCATE (phase_angle(n_img, n_k))
1633 phase_angle(:, :) = twopi*matmul(transpose(real(index_to_cell, dp)), kpoints%xkp(:, 1:n_k))
1634
1635 n_k_local = (n_k + para_env%num_pe - 1 - para_env%mepos)/para_env%num_pe
1636 ALLOCATE (dipole(n_spin, n_k_local, 3, maxval(nocc_spin), maxval(nmo_spin - nocc_spin)), source=z_zero)
1637 ! Each rank works only on its own k points, with their MO coefficients summed to it.
1638 CALL owned_mo_coefficients(bs_env, mo_coeff(1:n_k, :), coeff_owned)
1639 ! Per direction α: ∂S/∂k_α, the moment d_α and ∂H/∂k_α of each spin.
1640 n_mat = 3*(2 + n_spin)
1641 ALLOCATE (bloch(n_ao, n_col, n_mat, para_env%num_pe), operators_k(n_ao, n_ao, n_mat), &
1642 phase_cos(n_img, para_env%num_pe), phase_sin(n_img, para_env%num_pe), &
1643 weight_re(n_img, para_env%num_pe), weight_im(n_img, para_env%num_pe), &
1644 recv_buffer(n_ao*n_ao*n_mat), &
1645 send_counts(0:para_env%num_pe - 1), send_displs(0:para_env%num_pe - 1), &
1646 recv_counts(0:para_env%num_pe - 1), recv_displs(0:para_env%num_pe - 1))
1647 ! k points are owned cyclically: batch k_first..k_first+P-1 gives rank r the k point k_first+r.
1648 DO first_k = 1, n_k, para_env%num_pe
1649 n_batch = min(para_env%num_pe, n_k - first_k + 1)
1650 DO ib = 1, n_batch
1651 phase_cos(:, ib) = cos(phase_angle(:, first_k + ib - 1))
1652 phase_sin(:, ib) = sin(phase_angle(:, first_k + ib - 1))
1653 END DO
1654 bloch(:, :, :, 1:n_batch) = z_zero
1655 DO i_dir = 1, 3
1656 IF (periodic(i_dir) == 0) cycle
1657 ! ∂X(k)/∂k_α = Σ_R i R_α e^{ikR} X(R)
1658 DO ib = 1, n_batch
1659 weight_re(:, ib) = -cell_cartesian(i_dir, :)*phase_sin(:, ib)
1660 weight_im(:, ib) = cell_cartesian(i_dir, :)*phase_cos(:, ib)
1661 END DO
1662 CALL bloch_sum_columns(s_cols(:, :, :, 1), weight_re, weight_im, n_batch, bloch(:, :, i_dir, :))
1663 CALL bloch_sum_columns(d_cols(:, :, :, i_dir), phase_cos, phase_sin, n_batch, &
1664 bloch(:, :, 3 + i_dir, :))
1665 DO i_spin = 1, n_spin
1666 CALL bloch_sum_columns(h_cols(:, :, :, i_spin), weight_re, weight_im, n_batch, &
1667 bloch(:, :, 3*(1 + i_spin) + i_dir, :))
1668 END DO
1669 END DO
1670 ! Transpose the column slices to the owner of each k point.
1671 DO i_rank = 0, para_env%num_pe - 1
1672 send_displs(i_rank) = n_ao*n_col*n_mat*i_rank
1673 send_counts(i_rank) = merge(n_ao*n_col*n_mat, 0, i_rank < n_batch)
1674 CALL block_range(n_ao, i_rank, para_env%num_pe, first_rank_col, n_rank_col)
1675 recv_displs(i_rank) = n_ao*(first_rank_col - 1)*n_mat
1676 recv_counts(i_rank) = merge(n_ao*n_rank_col*n_mat, 0, para_env%mepos < n_batch)
1677 END DO
1678 CALL para_env%alltoall(reshape(bloch, [SIZE(bloch)]), send_counts, send_displs, &
1679 recv_buffer, recv_counts, recv_displs)
1680 IF (para_env%mepos >= n_batch) cycle
1681 DO i_rank = 0, para_env%num_pe - 1
1682 CALL block_range(n_ao, i_rank, para_env%num_pe, first_rank_col, n_rank_col)
1683 operators_k(:, first_rank_col:first_rank_col + n_rank_col - 1, :) = &
1684 reshape(recv_buffer(recv_displs(i_rank) + 1:recv_displs(i_rank) + recv_counts(i_rank)), &
1685 [n_ao, n_rank_col, n_mat])
1686 END DO
1687 i_k = first_k + para_env%mepos
1688 i_k_local = (first_k - 1)/para_env%num_pe + 1
1689 DO i_spin = 1, n_spin
1690 n_mo = nmo_spin(i_spin)
1691 nocc = nocc_spin(i_spin)
1692 n_virt = n_mo - nocc
1693 IF (nocc == 0 .OR. n_virt == 0) cycle
1694 ALLOCATE (projected_h(n_virt, nocc), projected_s(n_virt, nocc), projected_d(n_virt, nocc))
1695 DO i_dir = 1, 3
1696 IF (periodic(i_dir) == 0) cycle
1697 CALL project_virtual_occupied(operators_k(:, :, 3*(1 + i_spin) + i_dir), &
1698 coeff_owned(:, 1:n_mo, i_k_local, i_spin), nocc, projected_h)
1699 CALL project_virtual_occupied(operators_k(:, :, i_dir), &
1700 coeff_owned(:, 1:n_mo, i_k_local, i_spin), nocc, projected_s)
1701 CALL project_virtual_occupied(operators_k(:, :, 3 + i_dir), &
1702 coeff_owned(:, 1:n_mo, i_k_local, i_spin), nocc, projected_d)
1703 DO i_mo_i = 1, nocc
1704 DO i_mo_a = 1, n_virt
1705 energy_diff = eigenvalues(nocc + i_mo_a, i_k, i_spin) - eigenvalues(i_mo_i, i_k, i_spin)
1706 IF (abs(energy_diff) <= eps_degenerate) cycle
1707 dipole(i_spin, i_k_local, i_dir, i_mo_i, i_mo_a) = &
1708 (-gaussi*projected_h(i_mo_a, i_mo_i) + &
1709 gaussi*eigenvalues(nocc + i_mo_a, i_k, i_spin)* &
1710 projected_s(i_mo_a, i_mo_i))/energy_diff + projected_d(i_mo_a, i_mo_i)
1711 END DO
1712 END DO
1713 END DO
1714 DEALLOCATE (projected_h, projected_s, projected_d)
1715 END DO
1716 END DO
1717 DEALLOCATE (bloch, cell_cartesian, coeff_owned, d_cols, h_cols, nmo_spin, operators_k, phase_angle, phase_cos, &
1718 phase_sin, recv_buffer, recv_counts, recv_displs, s_cols, send_counts, send_displs, &
1719 weight_im, weight_re)
1720 DEALLOCATE (gwkp_env%gamma_corr_index_to_cell, gwkp_env%gamma_corr_cell_to_index)
1721 NULLIFY (gwkp_env%gamma_corr_index_to_cell, gwkp_env%gamma_corr_cell_to_index)
1722
1723 CALL timestop(handle)
1724
1725 END SUBROUTINE gw_gamma_transition_moments
1726
1727! **************************************************************************************************
1728!> \brief Scatter one low-rank Gamma correction into the centred grid-pair images.
1729!> For s=nearest_integer[H^{-1}(r_l-r_l')], C_ll'(R)=C0_ll'(R-s).
1730!> R-s is wrapped into the finite FFT cell with its transfer-mesh Bloch twist. Half-cell
1731!> ties average adjacent integer images, retaining Hermiticity and cell-wrapping covariance.
1732!> The mirrored conjugate-transpose contribution enforces reciprocity also on Nyquist faces.
1733!> No grid-pair angular tensor is formed. values_R holds flattened column-major pairs
1734!> beginning at first_pair.
1735!> \param bs_env ...
1736!> \param cells ...
1737!> \param icell ...
1738!> \param head_scalar ...
1739!> \param head_vector ...
1740!> \param first_pair ...
1741!> \param values_R ...
1742!> \param wing_left ...
1743!> \param wing_right ...
1744! **************************************************************************************************
1745 SUBROUTINE add_gamma_corr_pair_cell(bs_env, cells, icell, head_scalar, head_vector, &
1746 first_pair, values_R, wing_left, wing_right)
1747 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
1748 INTEGER, INTENT(IN) :: cells(:, :), icell
1749 COMPLEX(KIND=dp), INTENT(IN) :: head_scalar, head_vector(:)
1750 INTEGER, INTENT(IN) :: first_pair
1751 REAL(kind=dp), INTENT(INOUT) :: values_r(:, :, :)
1752 COMPLEX(KIND=dp), INTENT(IN), OPTIONAL :: wing_left(:), wing_right(:)
1753
1754 COMPLEX(KIND=dp) :: amplitude(2), twist(27, 2)
1755 INTEGER :: destination(27, 2), high(3), idir, iimage, il, ilocal, ilp, imirror, ipair, ix, &
1756 iy, iz, jlocal, low(3), lower(3), ngrid, nmesh(3), shift(3), translated(3), wrapped(3)
1757 REAL(kind=dp) :: delta(3), half_boundary, qoffset(3), &
1758 tolerance, weight
1759 REAL(kind=dp), ALLOCATABLE :: fractional_grid(:, :)
1760
1761 ngrid = bs_env%ri_rs%n_grid_points
1762 nmesh = bs_env%kpoints_scf_desymm%nkp_grid
1763 lower = -nmesh/2
1764 qoffset = bs_env%kpoints_chi_eps_W%xkp(1:3, 1)
1765 ALLOCATE (fractional_grid(3, ngrid))
1766 fractional_grid(:, :) = matmul(bs_env%ri_rs%cell%h_inv, bs_env%ri_rs%grid_points)
1767 DO idir = 1, 3
1768 IF (bs_env%periodic(idir) == 0) fractional_grid(idir, :) = 0.0_dp
1769 END DO
1770 DO iz = -1, 1
1771 DO iy = -1, 1
1772 DO ix = -1, 1
1773 shift = [ix, iy, iz]
1774 iimage = 1 + ix + 1 + 3*(iy + 1) + 9*(iz + 1)
1775 DO imirror = 1, 2
1776 translated = (3 - 2*imirror)*cells(:, icell) + shift
1777 wrapped = modulo(translated - lower, nmesh) + lower
1778 destination(iimage, imirror) = 1 + (wrapped(3) - lower(3)) + &
1779 nmesh(3)*(wrapped(2) - lower(2) + nmesh(2)*(wrapped(1) - lower(1)))
1780 twist(iimage, imirror) = exp(cmplx(0.0_dp, &
1781 twopi*dot_product(qoffset, real(translated - wrapped, dp)), dp))
1782 END DO
1783 END DO
1784 END DO
1785 END DO
1786 ! Every local pair row receives its own images, so threads never share an element.
1787 !$OMP PARALLEL DO DEFAULT(NONE) COLLAPSE(2) SCHEDULE(STATIC) &
1788 !$OMP SHARED(values_R, first_pair, ngrid, fractional_grid, head_scalar, head_vector, &
1789 !$OMP wing_left, wing_right, destination, twist) &
1790 !$OMP PRIVATE(jlocal, ilocal, ipair, il, ilp, delta, low, high, idir, half_boundary, &
1791 !$OMP tolerance, weight, amplitude, iz, iy, ix, iimage, imirror)
1792 DO jlocal = 1, SIZE(values_r, 2)
1793 DO ilocal = 1, SIZE(values_r, 1)
1794 ipair = first_pair + ilocal - 1
1795 il = modulo(ipair - 1, ngrid) + 1
1796 ilp = (ipair - 1)/ngrid + 1
1797 delta = fractional_grid(:, il) - fractional_grid(:, ilp)
1798 low = nint(delta)
1799 high = low
1800 DO idir = 1, 3
1801 half_boundary = anint(delta(idir) - 0.5_dp) + 0.5_dp
1802 tolerance = 64.0_dp*epsilon(1.0_dp)*max(1.0_dp, abs(delta(idir)))
1803 IF (abs(delta(idir) - half_boundary) <= tolerance) THEN
1804 low(idir) = nint(half_boundary - 0.5_dp)
1805 high(idir) = low(idir) + 1
1806 END IF
1807 END DO
1808 weight = 0.5_dp/real(product(high - low + 1), dp)
1809 amplitude(1) = head_scalar*head_vector(il)*conjg(head_vector(ilp))
1810 amplitude(2) = conjg(head_scalar)*head_vector(il)*conjg(head_vector(ilp))
1811 IF (PRESENT(wing_left)) THEN
1812 amplitude(1) = amplitude(1) + &
1813 head_vector(il)*wing_right(ilp) + wing_left(il)*conjg(head_vector(ilp))
1814 amplitude(2) = amplitude(2) + &
1815 conjg(head_vector(ilp)*wing_right(il) + wing_left(ilp)*conjg(head_vector(il)))
1816 END IF
1817 DO iz = low(3), high(3)
1818 DO iy = low(2), high(2)
1819 DO ix = low(1), high(1)
1820 iimage = 1 + ix + 1 + 3*(iy + 1) + 9*(iz + 1)
1821 DO imirror = 1, 2
1822 values_r(ilocal, jlocal, destination(iimage, imirror)) = &
1823 values_r(ilocal, jlocal, destination(iimage, imirror)) + &
1824 weight*real(twist(iimage, imirror)*amplitude(imirror), dp)
1825 END DO
1826 END DO
1827 END DO
1828 END DO
1829 END DO
1830 END DO
1831 !$OMP END PARALLEL DO
1832 END SUBROUTINE add_gamma_corr_pair_cell
1833
1834! **************************************************************************************************
1835!> \brief Gather the sparse Gamma-correction AO images as column slices of their dense form.
1836!> Rank r receives all rows and images of its AO columns block_range(n_ao, r), so no rank holds
1837!> all images. Blocks keep the block and mirror conventions of replicate_rs_matrices.
1838!> \param matrices Distributed sparse AO image matrices, indexed (component, source image).
1839!> \param plan Prepared source image and atom-pair traversal.
1840!> \param cell_to_index Common Gamma-correction image map.
1841!> \param n_ao ...
1842!> \param n_img ...
1843!> \param para_env ...
1844!> \param values values(μ, ν, image, component) for the owned AO columns ν
1845! **************************************************************************************************
1846 SUBROUTINE gamma_corr_image_columns(matrices, plan, cell_to_index, n_ao, n_img, para_env, values)
1847 TYPE(dbcsr_p_type), POINTER :: matrices(:, :)
1848 TYPE(kp_transform_plan_type), INTENT(IN) :: plan
1849 INTEGER, POINTER :: cell_to_index(:, :, :)
1850 INTEGER, INTENT(IN) :: n_ao, n_img
1851 TYPE(mp_para_env_type), POINTER :: para_env
1852 REAL(dp), ALLOCATABLE, INTENT(OUT) :: values(:, :, :, :)
1853
1854 CHARACTER(LEN=*), PARAMETER :: routinen = 'gamma_corr_image_columns'
1855
1856 CHARACTER :: symmetry
1857 INTEGER :: cell(3), first, first_col, handle, i_col, i_comp, i_entry, i_header, i_image, &
1858 i_mirror, i_pass, i_rank, i_row, i_source, i_swap, i_value, n_col, n_local, n_rank
1859 INTEGER, ALLOCATABLE :: col_owner(:), header_counts(:), header_displs(:), header_recv(:), &
1860 header_recv_counts(:), header_recv_displs(:), header_send(:), value_counts(:), &
1861 value_displs(:), value_recv_counts(:), value_recv_displs(:)
1862 INTEGER, POINTER :: col_offsets(:), row_offsets(:)
1863 LOGICAL :: found
1864 REAL(dp) :: mirror_sign
1865 REAL(dp), ALLOCATABLE :: value_recv(:), value_send(:)
1866 REAL(dp), POINTER :: block(:, :)
1867
1868 CALL timeset(routinen, handle)
1869
1870 n_rank = para_env%num_pe
1871 ALLOCATE (col_owner(n_ao))
1872 DO i_rank = 0, n_rank - 1
1873 CALL block_range(n_ao, i_rank, n_rank, first, n_local)
1874 col_owner(first:first + n_local - 1) = i_rank
1875 END DO
1876 CALL block_range(n_ao, para_env%mepos, n_rank, first_col, n_col)
1877 CALL dbcsr_get_info(matrices(1, 1)%matrix, matrix_type=symmetry, &
1878 row_blk_offset=row_offsets, col_blk_offset=col_offsets)
1879 mirror_sign = merge(-1.0_dp, 1.0_dp, symmetry == dbcsr_type_antisymmetric)
1880 ALLOCATE (header_counts(0:n_rank - 1), header_displs(0:n_rank - 1), &
1881 value_counts(0:n_rank - 1), value_displs(0:n_rank - 1), &
1882 header_recv_counts(0:n_rank - 1), header_recv_displs(0:n_rank - 1), &
1883 value_recv_counts(0:n_rank - 1), value_recv_displs(0:n_rank - 1))
1884 ! The first pass counts the pieces for each column owner, the second packs them.
1885 DO i_pass = 1, 2
1886 IF (i_pass == 2) THEN
1887 header_displs(0) = 0
1888 value_displs(0) = 0
1889 DO i_rank = 1, n_rank - 1
1890 header_displs(i_rank) = header_displs(i_rank - 1) + header_counts(i_rank - 1)
1891 value_displs(i_rank) = value_displs(i_rank - 1) + value_counts(i_rank - 1)
1892 END DO
1893 ALLOCATE (header_send(max(1, sum(header_counts))), value_send(max(1, sum(value_counts))))
1894 END IF
1895 header_counts(:) = 0
1896 value_counts(:) = 0
1897 DO i_comp = 1, SIZE(matrices, 1)
1898 DO i_entry = 1, plan%nentry
1899 i_source = plan%image(i_entry)
1900 i_row = plan%row(i_entry)
1901 i_col = plan%col(i_entry)
1902 cell = plan%cell(:, i_entry)
1903 IF (plan%symmetry_sign(i_entry) < 0.0_dp) THEN
1904 i_swap = i_row
1905 i_row = i_col
1906 i_col = i_swap
1907 END IF
1908 IF (symmetry /= dbcsr_type_no_symmetry .AND. i_row > i_col) THEN
1909 i_swap = i_row
1910 i_row = i_col
1911 i_col = i_swap
1912 cell = -cell
1913 END IF
1914 CALL dbcsr_get_readonly_block_p(matrices(i_comp, i_source)%matrix, row=i_row, col=i_col, &
1915 block=block, found=found)
1916 IF (.NOT. found) cycle
1917 i_image = cell_to_index(cell(1), cell(2), cell(3))
1918 cpassert(i_image > 0)
1919 CALL pack_image_piece(block, [i_comp, i_image, row_offsets(i_row)], col_offsets(i_col), &
1920 col_owner, i_pass == 2, header_counts, value_counts, header_displs, &
1921 value_displs, header_send, value_send)
1922 IF (symmetry /= dbcsr_type_no_symmetry .AND. i_row /= i_col) THEN
1923 i_mirror = cell_to_index(-cell(1), -cell(2), -cell(3))
1924 cpassert(i_mirror > 0)
1925 CALL pack_image_piece(mirror_sign*transpose(block), [i_comp, i_mirror, col_offsets(i_col)], &
1926 row_offsets(i_row), col_owner, i_pass == 2, header_counts, &
1927 value_counts, header_displs, value_displs, header_send, value_send)
1928 END IF
1929 END DO
1930 END DO
1931 END DO
1932 CALL para_env%alltoall(header_counts, header_recv_counts, 1)
1933 CALL para_env%alltoall(value_counts, value_recv_counts, 1)
1934 header_recv_displs(0) = 0
1935 value_recv_displs(0) = 0
1936 DO i_rank = 1, n_rank - 1
1937 header_recv_displs(i_rank) = header_recv_displs(i_rank - 1) + header_recv_counts(i_rank - 1)
1938 value_recv_displs(i_rank) = value_recv_displs(i_rank - 1) + value_recv_counts(i_rank - 1)
1939 END DO
1940 ALLOCATE (header_recv(max(1, sum(header_recv_counts))), value_recv(max(1, sum(value_recv_counts))))
1941 CALL para_env%alltoall(header_send, header_counts, header_displs, header_recv, header_recv_counts, &
1942 header_recv_displs)
1943 CALL para_env%alltoall(value_send, value_counts, value_displs, value_recv, value_recv_counts, &
1944 value_recv_displs)
1945 DEALLOCATE (header_send, value_send)
1946
1947 ! Header (component, image, first row, rows, first column, columns); values column-major.
1948 ALLOCATE (values(n_ao, n_col, n_img, SIZE(matrices, 1)), source=0.0_dp)
1949 i_value = 0
1950 DO i_header = 1, sum(header_recv_counts), 6
1951 associate(h => header_recv(i_header:i_header + 5))
1952 values(h(3):h(3) + h(4) - 1, h(5) - first_col + 1:h(5) - first_col + h(6), h(2), h(1)) = &
1953 reshape(value_recv(i_value + 1:i_value + h(4)*h(6)), [h(4), h(6)])
1954 i_value = i_value + h(4)*h(6)
1955 END associate
1956 END DO
1957 DEALLOCATE (col_owner, header_counts, header_displs, header_recv, header_recv_counts, &
1958 header_recv_displs, value_counts, value_displs, value_recv, value_recv_counts, &
1959 value_recv_displs)
1960
1961 CALL timestop(handle)
1962
1963 END SUBROUTINE gamma_corr_image_columns
1964
1965! **************************************************************************************************
1966!> \brief Count or pack one dense AO-image piece for the owners of its columns.
1967!> \param piece block values, rows varying fastest
1968!> \param label component, image and first AO row of the piece
1969!> \param first_col first AO column of the piece
1970!> \param col_owner rank owning each AO column
1971!> \param fill pack into the buffers; otherwise only count
1972!> \param header_counts ...
1973!> \param value_counts ...
1974!> \param header_displs ...
1975!> \param value_displs ...
1976!> \param header_send ...
1977!> \param value_send ...
1978! **************************************************************************************************
1979 SUBROUTINE pack_image_piece(piece, label, first_col, col_owner, fill, header_counts, value_counts, &
1980 header_displs, value_displs, header_send, value_send)
1981 REAL(dp), INTENT(IN) :: piece(:, :)
1982 INTEGER, INTENT(IN) :: label(3), first_col, col_owner(:)
1983 LOGICAL, INTENT(IN) :: fill
1984 INTEGER, INTENT(INOUT) :: header_counts(0:), value_counts(0:)
1985 INTEGER, INTENT(IN) :: header_displs(0:), value_displs(0:)
1986 INTEGER, ALLOCATABLE, INTENT(INOUT) :: header_send(:)
1987 REAL(dp), ALLOCATABLE, INTENT(INOUT) :: value_send(:)
1988
1989 INTEGER :: i_first, n_cols, n_rows, owner
1990
1991 n_rows = SIZE(piece, 1)
1992 i_first = 1
1993 DO WHILE (i_first <= SIZE(piece, 2))
1994 owner = col_owner(first_col + i_first - 1)
1995 n_cols = 1
1996 DO WHILE (i_first + n_cols <= SIZE(piece, 2))
1997 IF (col_owner(first_col + i_first + n_cols - 1) /= owner) EXIT
1998 n_cols = n_cols + 1
1999 END DO
2000 IF (fill) THEN
2001 header_send(header_displs(owner) + header_counts(owner) + 1: &
2002 header_displs(owner) + header_counts(owner) + 6) = &
2003 [label, n_rows, first_col + i_first - 1, n_cols]
2004 value_send(value_displs(owner) + value_counts(owner) + 1: &
2005 value_displs(owner) + value_counts(owner) + n_rows*n_cols) = &
2006 reshape(piece(:, i_first:i_first + n_cols - 1), [n_rows*n_cols])
2007 END IF
2008 header_counts(owner) = header_counts(owner) + 6
2009 value_counts(owner) = value_counts(owner) + n_rows*n_cols
2010 i_first = i_first + n_cols
2011 END DO
2012 END SUBROUTINE pack_image_piece
2013
2014! **************************************************************************************************
2015!> \brief Bloch sums of AO-image column slices for a batch of k points with one GEMM each,
2016!> X(k) = Σ_R (w_re(R,k) + i w_im(R,k)) X(R).
2017!> \param images X(R), indexed (row, owned column, image)
2018!> \param weight_re ...
2019!> \param weight_im ...
2020!> \param n_batch number of k points
2021!> \param bloch X(k), indexed (row, owned column, k)
2022! **************************************************************************************************
2023 SUBROUTINE bloch_sum_columns(images, weight_re, weight_im, n_batch, bloch)
2024 REAL(dp), INTENT(IN) :: images(:, :, :), weight_re(:, :), &
2025 weight_im(:, :)
2026 INTEGER, INTENT(IN) :: n_batch
2027 COMPLEX(dp), INTENT(INOUT) :: bloch(:, :, :)
2028
2029 INTEGER :: n_img, n_values
2030 REAL(dp), ALLOCATABLE :: sum_im(:, :), sum_re(:, :)
2031
2032 n_values = SIZE(images, 1)*SIZE(images, 2)
2033 n_img = SIZE(images, 3)
2034 IF (n_values == 0) RETURN
2035 ALLOCATE (sum_re(n_values, n_batch), sum_im(n_values, n_batch))
2036 CALL dgemm('N', 'N', n_values, n_batch, n_img, 1.0_dp, images, n_values, weight_re, SIZE(weight_re, 1), &
2037 0.0_dp, sum_re, n_values)
2038 CALL dgemm('N', 'N', n_values, n_batch, n_img, 1.0_dp, images, n_values, weight_im, SIZE(weight_im, 1), &
2039 0.0_dp, sum_im, n_values)
2040 bloch(:, :, 1:n_batch) = reshape(cmplx(sum_re, sum_im, kind=dp), [SIZE(images, 1), SIZE(images, 2), n_batch])
2041 DEALLOCATE (sum_re, sum_im)
2042 END SUBROUTINE bloch_sum_columns
2043
2044! **************************************************************************************************
2045!> \brief Project an AO operator between virtual and occupied MOs, C_virt^H X C_occ.
2046!> \param operator_ao ...
2047!> \param coeff MO coefficients, occupied first
2048!> \param nocc ...
2049!> \param projected ...
2050! **************************************************************************************************
2051 SUBROUTINE project_virtual_occupied(operator_ao, coeff, nocc, projected)
2052 COMPLEX(dp), INTENT(IN) :: operator_ao(:, :), coeff(:, :)
2053 INTEGER, INTENT(IN) :: nocc
2054 COMPLEX(dp), INTENT(OUT) :: projected(:, :)
2055
2056 COMPLEX(dp), ALLOCATABLE :: work(:, :)
2057 INTEGER :: n_ao, n_virt
2058
2059 n_ao = SIZE(operator_ao, 1)
2060 n_virt = SIZE(coeff, 2) - nocc
2061 ALLOCATE (work(n_ao, nocc))
2062 CALL zgemm('N', 'N', n_ao, nocc, n_ao, z_one, operator_ao, n_ao, coeff, SIZE(coeff, 1), &
2063 z_zero, work, n_ao)
2064 CALL zgemm('C', 'N', n_virt, nocc, n_ao, z_one, coeff(:, nocc + 1:), SIZE(coeff, 1), work, n_ao, &
2065 z_zero, projected, n_virt)
2066 DEALLOCATE (work)
2067 END SUBROUTINE project_virtual_occupied
2068
2069! **************************************************************************************************
2070!> \brief Collect SCF and position-operator image cells, including mirrored images.
2071!> \param kp_a ...
2072!> \param kp_b ...
2073!> \param index_to_cell ...
2074!> \param cell_to_index ...
2075! **************************************************************************************************
2076 SUBROUTINE gw_gamma_cell_index_union(kp_a, kp_b, index_to_cell, cell_to_index)
2077 TYPE(kpoint_type), POINTER :: kp_a, kp_b
2078 INTEGER, DIMENSION(:, :), POINTER :: index_to_cell
2079 INTEGER, DIMENSION(:, :, :), POINTER :: cell_to_index
2080
2081 INTEGER :: i, ic, isgn, n
2082 INTEGER, ALLOCATABLE, DIMENSION(:, :) :: list
2083 INTEGER, DIMENSION(3) :: image, max_image
2084 INTEGER, DIMENSION(:, :), POINTER :: itc_a, itc_b
2085
2086 NULLIFY (itc_a, itc_b)
2087 CALL get_kpoint_info(kp_a, index_to_cell=itc_a)
2088 CALL get_kpoint_info(kp_b, index_to_cell=itc_b)
2089 cpassert(ASSOCIATED(itc_a) .AND. ASSOCIATED(itc_b))
2090 DO i = 1, 3
2091 max_image(i) = max(maxval(abs(itc_a(i, :))), maxval(abs(itc_b(i, :))))
2092 END DO
2093 ALLOCATE (cell_to_index(-max_image(1):max_image(1), -max_image(2):max_image(2), &
2094 -max_image(3):max_image(3)), source=0)
2095 ALLOCATE (list(3, 2*(SIZE(itc_a, 2) + SIZE(itc_b, 2))))
2096 n = 0
2097 DO isgn = 1, -1, -2
2098 DO ic = 1, SIZE(itc_a, 2)
2099 image = isgn*itc_a(:, ic)
2100 IF (cell_to_index(image(1), image(2), image(3)) /= 0) cycle
2101 n = n + 1
2102 cell_to_index(image(1), image(2), image(3)) = n
2103 list(:, n) = image
2104 END DO
2105 END DO
2106 DO isgn = 1, -1, -2
2107 DO ic = 1, SIZE(itc_b, 2)
2108 image = isgn*itc_b(:, ic)
2109 IF (cell_to_index(image(1), image(2), image(3)) /= 0) cycle
2110 n = n + 1
2111 cell_to_index(image(1), image(2), image(3)) = n
2112 list(:, n) = image
2113 END DO
2114 END DO
2115 ALLOCATE (index_to_cell(3, n))
2116 index_to_cell = list(:, 1:n)
2117 DEALLOCATE (list)
2118 END SUBROUTINE gw_gamma_cell_index_union
2119
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.
Handles all functions related to the CELL.
Definition cell_types.F:15
subroutine, public get_cell(cell, alpha, beta, gamma, deth, orthorhombic, abc, periodic, h, h_inv, symmetry_id, tag)
Get informations about a simulation cell.
Definition cell_types.F:234
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_column_scale(matrix_a, scaling, first_col, source)
Scales columns of the full matrix by corresponding factors.
used for collecting diagonalization schemes available for cp_cfm_type
Definition cp_cfm_diag.F:14
subroutine, public cp_cfm_heevd(matrix, eigenvectors, eigenvalues)
Perform a diagonalisation of a complex matrix.
Definition cp_cfm_diag.F:94
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_get_info(matrix, name, nrow_global, ncol_global, nrow_block, ncol_block, nrow_local, ncol_local, row_indices, col_indices, local_data, context, matrix_struct, para_env)
Returns information about a full matrix.
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) = ...
subroutine, public dbcsr_get_readonly_block_p(matrix, row, col, block, found, row_size, col_size)
Like dbcsr_get_block_p() but with matrix being INTENT(IN). When invoking this routine,...
subroutine, public dbcsr_copy(matrix_b, matrix_a, name, keep_sparsity, keep_imaginary)
...
subroutine, public dbcsr_get_info(matrix, nblkrows_total, nblkcols_total, nfullrows_total, nfullcols_total, nblkrows_local, nblkcols_local, nfullrows_local, nfullcols_local, my_prow, my_pcol, local_rows, local_cols, proc_row_dist, proc_col_dist, row_blk_size, col_blk_size, row_blk_offset, col_blk_offset, distribution, name, matrix_type, group)
...
DBCSR operations in CP2K.
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
various routines to log and control the output. The idea is that decisions about where to log should ...
Input and persistent data for automatic RI basis optimization.
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 prepare_ri_rs_gamma_correction(qs_env, bs_env)
Preserve distributed AO position, Hamiltonian and overlap images before QS release....
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.
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...
Defines the basic variable types.
Definition kinds.F:23
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 build_2c_coulomb_matrix_kp_dense(v_k, qs_env, kpoints, size_lattice_sum, basis_type, ikp_start, ikp_end)
Dense 2-center Coulomb matrix V_PQ(k) from a direct real-space lattice sum.
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...
Routines needed for kpoint calculation.
subroutine, public kpoint_init_cell_index(kpoint, sab_nl, para_env, nimages)
Generates the mapping of cell indices and linear RS index CELL (0,0,0) is always mapped to index 1.
subroutine, public kp_transform_plan_create(plan, sab_nl, cell_to_index, nimg, block_template, group_entries)
Build the immutable neighbor-list traversal shared by R-to-K and K-to-R transforms.
Types and basic routines needed for a kpoint calculation.
subroutine, public kpoint_release(kpoint)
Release a kpoint environment, deallocate all data.
subroutine, public get_kpoint_info(kpoint, kp_scheme, nkp_grid, kp_shift, symmetry, verbose, full_grid, use_real_wfn, eps_geo, parallel_group_size, kp_range, nkp, xkp, wkp, para_env, blacs_env_all, para_env_kp, para_env_inter_kp, blacs_env, kp_env, kp_aux_env, mpools, iogrp, nkp_groups, kp_dist, cell_to_index, index_to_cell, sab_nl, sab_nl_nosym, inversion_symmetry_only, symmetry_backend, symmetry_reduction_method, gamma_centered, lattice_fft)
Retrieve information from a kpoint environment.
subroutine, public kpoint_create(kpoint)
Create a kpoint environment.
An array-based list which grows on demand. When the internal array is full, a new array of twice the ...
Definition list.F:24
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
Collection of simple mathematical functions and subroutines.
Definition mathlib.F:15
pure real(kind=dp) function, dimension(3, 3), public inv_3x3(a)
Returns the inverse of the 3 x 3 matrix a.
Definition mathlib.F:526
subroutine, public gauss_legendre(n, nodes, weights)
Gauss-Legendre nodes and weights on [-1,1], with nodes in ascending order. The Legendre roots are ref...
Definition mathlib.F:2295
pure real(kind=dp) function, dimension(3), public vector_product(a, b)
Calculation of the vector product c = a x b.
Definition mathlib.F:1274
Interface to the message passing library MPI.
basic linear algebra operations for full matrixes
subroutine, public get_qs_env(qs_env, atomic_kind_set, qs_kind_set, cell, super_cell, cell_ref, use_ref_cell, kpoints, dft_control, mos, sab_orb, sab_all, qmmm, qmmm_periodic, mimic, sac_ae, sac_ppl, sac_lri, sap_ppnl, sab_vdw, sab_scp, sap_oce, sab_lrc, sab_se, sab_xtbe, sab_tbe, sab_core, sab_xb, sab_xtb_pp, sab_xtb_nonbond, sab_almo, sab_kp, sab_kp_nosym, sab_cneo, particle_set, energy, force, matrix_h, matrix_h_im, matrix_ks, matrix_ks_im, matrix_vxc, run_rtp, rtp, matrix_h_kp, matrix_h_im_kp, matrix_ks_kp, matrix_ks_im_kp, matrix_vxc_kp, kinetic_kp, matrix_s_kp, matrix_w_kp, matrix_s_ri_aux_kp, matrix_s, matrix_s_ri_aux, matrix_w, matrix_p_mp2, matrix_p_mp2_admm, matrix_vhxc, rho, rho_xc, pw_env, ewald_env, ewald_pw, active_space, mpools, input, para_env, blacs_env, scf_control, rel_control, kinetic, qs_charges, vppl, xcint_weights, rho_core, rho_nlcc, rho_nlcc_g, ks_env, ks_qmmm_env, wf_history, scf_env, local_particles, local_molecules, distribution_2d, dbcsr_dist, molecule_kind_set, molecule_set, subsys, cp_subsys, oce, local_rho_set, rho_atom_set, task_list, task_list_soft, rho0_atom_set, rho0_mpole, rhoz_set, rhoz_cneo_set, ecoul_1c, rho0_s_rs, rho0_s_gs, rhoz_cneo_s_rs, rhoz_cneo_s_gs, do_kpoints, has_unit_metric, requires_mo_derivs, mo_derivs, mo_loc_history, nkind, natom, nelectron_total, nelectron_spin, efield, neighbor_list_id, linres_control, xas_env, virial, cp_ddapc_env, cp_ddapc_ewald, outer_scf_history, outer_scf_ihistory, x_data, et_coupling, dftb_potential, results, se_taper, se_store_int_env, se_nddo_mpole, se_nonbond_env, admm_env, lri_env, lri_density, exstate_env, ec_env, harris_env, dispersion_env, gcp_env, vee, rho_external, external_vxc, mask, mp2_env, bs_env, kg_env, wanniercentres, atprop, ls_scf_env, do_transport, transport_env, v_hartree_rspace, s_mstruct_changed, rho_changed, potential_changed, forces_up_to_date, mscfg_env, almo_scf_env, gradient_history, variable_history, embed_pot, spin_embed_pot, polar_env, mos_last_converged, eeq, rhs, do_rixs, tb_tblite)
Get the QUICKSTEP environment.
Calculates the moment integrals <a|r^m|b> and <a|r x d/dr|b>.
Definition qs_moments.F:14
subroutine, public build_local_moment_matrix_rs_img(qs_env, moments_rs_img, rcc)
Calculate local moment matrix for a periodic system for all image cells.
Define the neighbor list data types and the corresponding functionality.
Type defining parameters related to the simulation cell.
Definition cell_types.F:60
represent a blacs multidimensional parallel environment (for the mpi corrispective see cp_paratypes/m...
Represent a complex full matrix.
keeps the information about the structure of a full matrix
represent a full matrix
Contains information about kpoints.
stores all the informations relevant to an mpi environment