32 dbcsr_type_antisymmetric,&
33 dbcsr_type_no_symmetry
81#include "./base/base_uses.f90"
86 CHARACTER(len=*),
PARAMETER,
PRIVATE :: moduleN =
'gw_ri_rs_small_cell_full_kp_gamma'
95 REAL(KIND=
dp),
PARAMETER :: taylor_switch = 1.0e-4_dp
110 CHARACTER(LEN=*),
PARAMETER :: routinen =
'prepare_ri_rs_gamma_correction'
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(:, :)
116 TYPE(
kpoint_type),
POINTER :: kpoints_all, kpoints_scf
119 CALL timeset(routinen, handle)
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, :)
127 NULLIFY (kpoints_all)
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
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)
146 kpoints_all%cell_to_index,
SIZE(gwkp_env%gamma_corr_d, 2))
148 kpoints_scf%cell_to_index,
SIZE(matrix_h, 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)
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)
166 CALL timestop(handle)
181 TYPE(
gwkp_type),
INTENT(INOUT) :: gwkp_env
183 CHARACTER(LEN=*),
PARAMETER :: routinen =
'gamma_point_correction'
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(:, :, :), &
193 INTEGER :: a, first_row, handle, i_cartesian, &
194 i_direction, ikp, j_cartesian, j_w, &
195 n_angular, n_directions, n_near, &
197 INTEGER,
ALLOCATABLE :: metric_cells(:, :)
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(:)
205 CALL timeset(routinen, handle)
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)
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)
215 CALL gamma_cell_face_directions(bs_env, angular_directions, angular_weights, angular_radii)
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
222 n_near = product(min(2, bs_env%nkp_grid_chi_eps_W_orig))
223 n_directions = n_angular + n_near
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))
229 IF (.NOT. is_gamma_neighbor(q_direction(1:2), bs_env%nkp_grid_chi_eps_W_orig(1:2))) cycle
231 IF (.NOT. is_gamma_neighbor(q_direction, bs_env%nkp_grid_chi_eps_W_orig)) cycle
234 IF (a > n_near) cpabort(
'Too many Gamma-neighbor transfer points')
235 near_q(:, a) = q_direction
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
249 ALLOCATE (head_response(n_directions, bs_env%num_time_freq_points))
252 ALLOCATE (metric_zero(bs_env%n_RI, bs_env%n_RI), f_zero(bs_env%n_RI, bs_env%n_RI))
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)
263 IF (.NOT. is_slab)
ALLOCATE (metric_ref_zero, source=metric_zero)
266 DEALLOCATE (metric_cells)
268 CALL slab_gamma_factors(qs_env, bs_env, metric_zero, f_zero, f_external)
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)
276 DEALLOCATE (metric_zero)
278 IF (.NOT. is_slab)
ALLOCATE (f_external, source=f_zero)
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)
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)
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, &
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)
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)
323 head_response(i_direction, j_w) = max(0.0_dp, real(pi_00 - schur_contribution/
fourpi,
dp))
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")
331 head_response(i_direction, j_w) = real(
z_one/schur_denom,
dp)
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)
342 DEALLOCATE (dipole, &
343 eps_p_cartesian, f_zero, &
344 pi_cartesian, response_cartesian, schur_cartesian, &
345 f_response_cartesian)
347 CALL timestop(handle)
362 SUBROUTINE coulomb_matrix_gamma(qs_env, bs_env, V_zero, metric_ref_zero)
365 COMPLEX(KIND=dp),
ALLOCATABLE,
INTENT(OUT) :: v_zero(:, :)
366 COMPLEX(KIND=dp),
INTENT(IN),
OPTIONAL :: metric_ref_zero(:, :)
368 CHARACTER(LEN=*),
PARAMETER :: routinen =
'coulomb_matrix_gamma'
370 COMPLEX(KIND=dp),
ALLOCATABLE :: u(:, :), v_contracted(:, :), &
372 INTEGER :: handle, n_local, n_ref
375 CALL timeset(routinen, handle)
378 IF (bs_env%auto_ri%enabled) n_ref = sum(bs_env%auto_ri%sizes_ref_RI)
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
385 gamma_kp%nkp_grid = bs_env%nkp_grid_chi_eps_W_orig
387 IF (all(bs_env%periodic == [1, 1, 0]))
THEN
389 bs_env%size_lattice_sum_V,
'RI_AUX', 1, 1)
391 cpassert(
PRESENT(metric_ref_zero))
392 IF (n_local == 1) v_owned(:, :, 1) = metric_ref_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
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)
408 CALL timestop(handle)
410 END SUBROUTINE coulomb_matrix_gamma
423 SUBROUTINE gamma_cell_face_directions(bs_env, directions, weights, radii)
425 REAL(kind=
dp),
ALLOCATABLE,
INTENT(OUT) :: directions(:, :), weights(:), radii(:)
427 INTEGER,
PARAMETER :: n = 6
429 INTEGER :: face, i, ia, ib, j, k, n_angular, nk, &
431 REAL(kind=
dp) :: bmat(3, 3), jacobian_scale, q(3), &
433 REAL(kind=
dp),
ALLOCATABLE :: nodes(:), rule_weights(:)
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
441 ALLOCATE (nodes(n), rule_weights(n))
443 ALLOCATE (directions(3, n_angular), weights(n_angular), radii(n_angular))
448 DO sign_index = -1, 1, 2
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
464 cpassert(face == n_angular)
465 DEALLOCATE (nodes, rule_weights)
466 END SUBROUTINE gamma_cell_face_directions
475 PURE FUNCTION is_gamma_neighbor(xkp, nkp_grid)
RESULT(neighbor)
476 REAL(kind=
dp),
INTENT(IN) :: xkp(:)
477 INTEGER,
INTENT(IN) :: nkp_grid(:)
480 neighbor = all(nint(2.0_dp*abs(xkp - anint(xkp))*real(nkp_grid,
dp)) == 1)
482 END FUNCTION is_gamma_neighbor
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
497 COMPLEX(KIND=dp) :: phase
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)
510 END SUBROUTINE radial_phase_moments
525 INTEGER,
INTENT(IN) :: cells(:, :)
526 COMPLEX(KIND=dp),
ALLOCATABLE,
INTENT(OUT) :: factors(:)
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), &
532 REAL(kind=
dp),
ALLOCATABLE :: directions(:, :), radii(:), weights(:)
534 nk = product(bs_env%nkp_grid_chi_eps_W_orig)
535 IF (all(bs_env%periodic == [1, 1, 0]))
THEN
537 CALL slab_cell_face_directions(bs_env, directions, weights, radii)
539 CALL slab_reciprocal_vectors(bs_env, bmat(:, 1:2), area)
540 scale = area/
twopi**2
543 CALL gamma_cell_face_directions(bs_env, directions, weights, radii)
547 ALLOCATE (factors(
SIZE(cells, 2)))
548 DO icell = 1,
SIZE(cells, 2)
549 rcart = matmul(bs_env%hmat, real(cells(:, icell),
dp))
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
555 factors(icell) = factors(icell)*scale
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)))
562 factors(icell) = factors(icell) - exp(-
gaussi*dot_product(q, rcart))/ &
563 (real(nk,
dp)*qnorm**(n_dim - 1))
566 DEALLOCATE (directions, weights, radii)
580 SUBROUTINE slab_gamma_factors(qs_env, bs_env, metric, F_density, F_external)
583 COMPLEX(KIND=dp),
INTENT(INOUT) :: metric(:, :)
584 COMPLEX(KIND=dp),
INTENT(OUT) :: f_density(:, :)
585 COMPLEX(KIND=dp),
ALLOCATABLE,
INTENT(OUT) :: f_external(:, :)
587 CHARACTER(LEN=*),
PARAMETER :: routinen =
'slab_gamma_factors'
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
593 CALL timeset(routinen, handle)
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))
601 IF (bs_env%auto_ri%enabled)
THEN
603 ALLOCATE (charge_contracted(n_ri))
604 charge_contracted(:) = matmul(conjg(transpose(u)), charge)
605 CALL move_alloc(charge_contracted, charge)
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))
613 projector(i_row, i_col) = -charge(i_row)*conjg(charge(i_col))
615 projector(i_col, i_col) = projector(i_col, i_col) +
z_one
617 body_sqrt(:, :) = matmul(projector, matmul(v_regular, projector))
618 body_inverse(:, :) = body_sqrt
620 CALL local_complex_power(body_inverse, -0.5_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)
627 CALL timestop(handle)
629 END SUBROUTINE slab_gamma_factors
637 SUBROUTINE slab_reciprocal_vectors(bs_env, b, area)
639 REAL(kind=
dp),
INTENT(OUT) :: b(3, 2), area
641 REAL(kind=
dp) :: normal(3)
645 cpassert(area > 0.0_dp)
649 END SUBROUTINE slab_reciprocal_vectors
660 SUBROUTINE slab_cell_face_directions(bs_env, directions, weights, radii)
662 REAL(kind=
dp),
ALLOCATABLE,
INTENT(OUT) :: directions(:, :), weights(:), radii(:)
664 INTEGER,
PARAMETER :: n_face = 24
666 INTEGER :: i_axis, i_direction, i_node, i_sign, &
668 REAL(kind=
dp) :: area, b(3, 2), dq(3), q(3)
669 REAL(kind=
dp),
ALLOCATABLE :: nodes(:), rule(:)
671 CALL slab_reciprocal_vectors(bs_env, b, area)
672 ALLOCATE (nodes(n_face), rule(n_face))
674 ALLOCATE (directions(3, 4*n_face), weights(4*n_face), radii(4*n_face))
678 dq = b(:, j_axis)/real(max(2, bs_env%nkp_grid_chi_eps_W_orig(j_axis)),
dp)
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) &
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)
690 DEALLOCATE (nodes, rule)
691 END SUBROUTINE slab_cell_face_directions
711 SUBROUTINE gamma_corr_cell_integrals(bs_env, directions, weights, radii, n_angular, head_response, model)
713 REAL(kind=
dp),
INTENT(IN) :: directions(:, :), weights(:), radii(:)
714 INTEGER,
INTENT(IN) :: n_angular
715 REAL(kind=
dp),
INTENT(IN) :: head_response(:, :)
718 CHARACTER(LEN=*),
PARAMETER :: routinen =
'gamma_corr_cell_integrals'
719 INTEGER,
PARAMETER :: n_radial = 32
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, &
726 INTEGER,
ALLOCATABLE :: cells(:, :)
728 REAL(kind=
dp) :: area, b(3, 2), head_scale, length, &
729 measure, projection, q, rcart(3), &
731 REAL(kind=
dp),
ALLOCATABLE :: nodes(:), rule(:)
733 CALL timeset(routinen, handle)
735 is_slab = all(bs_env%periodic == [1, 1, 0])
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
744 CALL slab_reciprocal_vectors(bs_env, b, area)
745 measure = area/
twopi**2
746 ALLOCATE (nodes(n_radial), rule(n_radial))
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
765 f0 = -phase/(real(nk,
dp)*length**2)
766 f1 = -phase/(real(nk,
dp)*length)
769 DO i_frequency = 1, n_frequency
770 response = head_response(i_direction, i_frequency)
772 IF (i_direction <= n_angular)
THEN
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)* &
779 integral = measure*weights(i_direction)*integral
781 integral = -phase/(real(nk,
dp)*(1.0_dp +
twopi*length*response))
783 head = -
twopi**2*response*integral
786 head =
fourpi*(response - 1.0_dp)*f0
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)
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)
803 CALL timestop(handle)
805 END SUBROUTINE gamma_corr_cell_integrals
821 INTEGER,
INTENT(IN) :: cells(:, :)
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(:, :, :)
828 CHARACTER(LEN=*),
PARAMETER :: routinen =
'add_gamma_corr_cell_correction'
830 COMPLEX(KIND=dp),
ALLOCATABLE :: coefficient(:, :, :), wing(:, :)
831 INTEGER :: handle, i_cell, j_w, n_frequency, n_grid
833 CALL timeset(routinen, handle)
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)
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))
849 first_pair, w_grid_r, wing(:, 1), wing(:, 2))
851 DEALLOCATE (coefficient, wing)
853 CALL timestop(handle)
872 SUBROUTINE gamma_ao_time_response(bs_env, chi_tau, wing_tau, head_tau, metric_rows, first_row, &
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(:, :)
880 CHARACTER(LEN=*),
PARAMETER :: routinen =
'gamma_ao_time_response'
882 COMPLEX(KIND=dp),
ALLOCATABLE :: chi_frequency(:, :), wing_frequency(:, :)
883 INTEGER :: handle, nr, t
885 CALL timeset(routinen, handle)
888 nr =
SIZE(chi_tau, 1)
889 ALLOCATE (chi_frequency(nr,
SIZE(chi_tau, 2)), wing_frequency(nr, 3), source=
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)
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)
905 CALL timestop(handle)
907 END SUBROUTINE gamma_ao_time_response
914 SUBROUTINE validate_time_transform(bs_env)
917 INTEGER,
PARAMETER :: n_sampled_gaps = 32
918 REAL(kind=
dp),
PARAMETER :: eps_gamma_time_transform = 1.0e-5_dp
920 CHARACTER(LEN=256) :: error_message
921 INTEGER :: i, no, nv, spin, t, w
922 REAL(kind=
dp) :: approximate, delta, exact, hi, lo, &
926 gwkp_env => bs_env%gwkp_env
930 DO spin = 1, bs_env%n_spin
931 no = bs_env%n_occ(spin)
932 nv = bs_env%n_mo_retained - no
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)))
940 IF (hi == 0.0_dp)
RETURN
941 IF (lo <= 0.0_dp)
THEN
942 cpabort(
'Gamma response requires a positive transition gap')
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)
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))
955 worst = max(worst, abs(approximate - exact)/exact)
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))
965 END SUBROUTINE validate_time_transform
994 SUBROUTINE accumulate_time_state(bs_env, psi, dipole, volume, first_row, chi_tau, wing_tau, head_tau)
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(:, :, :), &
1002 CHARACTER(LEN=*),
PARAMETER :: routinen =
'accumulate_time_state'
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
1008 CALL timeset(routinen, handle)
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
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)
1030 gamma_corr(:, :, c) = -
gaussi*dipole(spin, local_k, c, 1:no, 1:nv)/sqrt(volume)
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)
1038 DEALLOCATE (orbitals, gamma_corr)
1040 CALL bs_env%para_env%sum(head_tau)
1042 CALL timestop(handle)
1044 END SUBROUTINE accumulate_time_state
1062 SUBROUTINE accumulate_k_time_rows(bs_env, orbitals, gamma_corr, k, spin, owner, &
1063 first_row, chi_tau, wing_tau, head_tau)
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(:, :, :), &
1070 COMPLEX(KIND=dp),
ALLOCATABLE :: block(:, :), go(:, :), gv(:, :), &
1072 INTEGER :: a, c, d, first, i, last, ng, no, nr, nv, &
1074 REAL(kind=
dp) :: head_weight, mid, tau, weight
1075 REAL(kind=
dp),
ALLOCATABLE :: occ_factor(:), vir_factor(:)
1078 gwkp_env => bs_env%gwkp_env
1080 no = bs_env%n_occ(spin)
1081 nv = bs_env%n_mo_retained - no
1083 ng = bs_env%ri_rs%n_grid_points
1084 nr =
SIZE(chi_tau, 1)
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)
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
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
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
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)
1114 IF (bs_env%para_env%mepos == owner)
THEN
1117 head_weight = weight*(occ_factor(i)*vir_factor(a))**2
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)
1128 DEALLOCATE (occ_factor, vir_factor, scaled, go, gv, block)
1129 END SUBROUTINE accumulate_k_time_rows
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(:, :)
1145 CHARACTER(LEN=*),
PARAMETER :: routinen =
'metric_grid_rows'
1147 COMPLEX(KIND=dp),
ALLOCATABLE :: z_rows(:, :)
1150 CALL timeset(routinen, handle)
1152 ALLOCATE (metric_rows(nr,
SIZE(f, 2)))
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)
1160 CALL timestop(handle)
1162 END SUBROUTINE metric_grid_rows
1177 SUBROUTINE apply_gamma_corr_body(bs_env, T_rows, chi, first_row, x, y, aux_y, grid_x, grid_y)
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(:, :)
1185 INTEGER :: i_last, nc, nr
1189 i_last = first_row + nr - 1
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
1196 CALL bs_env%para_env%sum(grid_x)
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))
1204 CALL bs_env%para_env%sum(aux_y)
1206 END SUBROUTINE apply_gamma_corr_body
1218 SUBROUTINE solve_gamma_corr_body(bs_env, T_rows, chi, first_row, b, x)
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(:, :)
1225 CHARACTER(LEN=*),
PARAMETER :: routinen =
'solve_gamma_corr_body'
1226 REAL(kind=
dp),
PARAMETER :: tolerance = 1.0e-12_dp
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
1236 CALL timeset(routinen, handle)
1241 rr(i_c) = real(dot_product(b(:, i_c), b(:, i_c)),
dp)
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
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')
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)
1266 IF (any(rr > rr_converged))
THEN
1267 CALL cp_warn(__location__,
"Gamma-point body CG not converged after "// &
1271 CALL timestop(handle)
1273 END SUBROUTINE solve_gamma_corr_body
1286 SUBROUTINE gamma_metric_contract_atoms(images, auto_ri, metric)
1289 COMPLEX(KIND=dp),
INTENT(OUT) :: metric(:, :)
1291 CHARACTER(LEN=*),
PARAMETER :: routinen =
'gamma_metric_contract_atoms'
1296 CALL timeset(routinen, handle)
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)
1302 CALL timestop(handle)
1304 END SUBROUTINE gamma_metric_contract_atoms
1313 SUBROUTINE prepare_gamma_atom_projections(images, auto_ri, left, right)
1317 INTENT(OUT) :: left(:), right(:)
1319 INTEGER :: nc, ngc, ngr, nr
1320 INTEGER,
POINTER :: cols(:), rows(:)
1321 LOGICAL :: valid_layout
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)
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
1347 SUBROUTINE extract_gamma_atom_rows(auto_ri, indices, blocks)
1349 INTEGER,
INTENT(IN) :: indices(:)
1351 INTENT(OUT) :: blocks(:)
1353 INTEGER :: first_opt, first_ref, iatom, icol, &
1354 ilocal, ipos, irow, nopt, nref, offset
1356 ALLOCATE (blocks(
SIZE(auto_ri%sizes_ref_RI)))
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
1369 DO ilocal = 1,
SIZE(indices)
1370 irow = indices(ilocal) - first_ref + 1
1371 IF (irow < 1 .OR. irow > nref) cycle
1373 blocks(iatom)%positions(ipos) = ilocal
1375 blocks(iatom)%values(ipos, icol) = auto_ri%U_Pp_atom(offset + irow - 1 + (icol - 1)*nref)
1378 first_ref = first_ref + nref
1379 first_opt = first_opt + nopt
1381 END SUBROUTINE extract_gamma_atom_rows
1390 SUBROUTINE accumulate_gamma_atom_metric(images, left, right, metric)
1393 COMPLEX(KIND=dp),
INTENT(OUT) :: metric(:, :)
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(:, :), &
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))
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)
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)
1428 cpassert(noright <= huge(0)/
SIZE(metric, 1))
1429 CALL images(1)%matrix_struct%para_env%sum(metric(:, qfirst:qfirst + noright - 1))
1431 DEALLOCATE (block, block_right,
value)
1432 END SUBROUTINE accumulate_gamma_atom_metric
1448 SUBROUTINE gamma_metric_factor_distributed(para_env, relative_cutoff, metric, coulomb, factor)
1450 REAL(kind=
dp),
INTENT(IN) :: relative_cutoff
1451 COMPLEX(KIND=dp),
INTENT(IN) :: metric(:, :), coulomb(:, :)
1452 COMPLEX(KIND=dp),
INTENT(OUT) :: factor(:, :)
1454 CHARACTER(LEN=*),
PARAMETER :: routinen =
'gamma_metric_factor_distributed'
1456 INTEGER :: handle, n
1458 TYPE(
cp_cfm_type) :: coulomb_d, factor_d, metric_d, work_d
1461 CALL timeset(routinen, handle)
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)
1475 CALL collect_factor_columns(factor_d, factor)
1483 CALL timestop(handle)
1485 END SUBROUTINE gamma_metric_factor_distributed
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
1501 COMPLEX(KIND=dp),
ALLOCATABLE :: scales(:)
1503 REAL(kind=
dp) :: cutoff
1504 REAL(kind=
dp),
ALLOCATABLE :: eigenvalues(:)
1508 ALLOCATE (eigenvalues(n), scales(n))
1510 cutoff = max(0.0_dp, relative_cutoff*maxval(eigenvalues))
1513 IF (eigenvalues(i_mode) > cutoff) scales(i_mode) = cmplx(eigenvalues(i_mode)**(0.5_dp*exponent), 0.0_dp,
dp)
1517 DEALLOCATE (eigenvalues, scales)
1518 END SUBROUTINE retained_hermitian_power
1525 SUBROUTINE collect_factor_columns(matrix, factor)
1527 COMPLEX(KIND=dp),
INTENT(OUT) :: factor(:, :)
1529 INTEGER :: i_column, i_last, n_columns, n_rows
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)
1537 i_last = i_column + (min(n_columns,
SIZE(factor, 2) - i_column + 1) - 1)
1539 IF (i_last ==
SIZE(factor, 2))
EXIT
1540 i_column = i_last + 1
1542 END SUBROUTINE collect_factor_columns
1558 SUBROUTINE gw_gamma_transition_moments(bs_env, gwkp_env, kpoints, mo_coeff, eigenvalues, nocc_spin, &
1561 TYPE(
gwkp_type),
INTENT(INOUT) :: gwkp_env
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
1569 CHARACTER(LEN=*),
PARAMETER :: routinen =
'gw_gamma_transition_moments'
1570 REAL(kind=
dp),
PARAMETER :: eps_degenerate = 1.0e-10_dp
1572 COMPLEX(KIND=dp),
ALLOCATABLE :: bloch(:, :, :, :), coeff_owned(:, :, :, :), &
1573 operators_k(:, :, :), projected_d(:, :), projected_h(:, :), projected_s(:, :), &
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(:), &
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(:, :), &
1592 CALL timeset(routinen, handle)
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)
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
1614 cpassert(all(nocc_spin >= 0 .AND. nocc_spin <= nmo_spin))
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)
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))
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)
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))
1648 DO first_k = 1, n_k, para_env%num_pe
1649 n_batch = min(para_env%num_pe, n_k - first_k + 1)
1651 phase_cos(:, ib) = cos(phase_angle(:, first_k + ib - 1))
1652 phase_sin(:, ib) = sin(phase_angle(:, first_k + ib - 1))
1654 bloch(:, :, :, 1:n_batch) =
z_zero
1656 IF (periodic(i_dir) == 0) cycle
1659 weight_re(:, ib) = -cell_cartesian(i_dir, :)*phase_sin(:, ib)
1660 weight_im(:, ib) = cell_cartesian(i_dir, :)*phase_cos(:, ib)
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, :))
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)
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])
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))
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)
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)
1714 DEALLOCATE (projected_h, projected_s, projected_d)
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)
1723 CALL timestop(handle)
1725 END SUBROUTINE gw_gamma_transition_moments
1746 first_pair, values_R, wing_left, wing_right)
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(:)
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), &
1759 REAL(kind=
dp),
ALLOCATABLE :: fractional_grid(:, :)
1761 ngrid = bs_env%ri_rs%n_grid_points
1762 nmesh = bs_env%kpoints_scf_desymm%nkp_grid
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)
1768 IF (bs_env%periodic(idir) == 0) fractional_grid(idir, :) = 0.0_dp
1773 shift = [ix, iy, iz]
1774 iimage = 1 + ix + 1 + 3*(iy + 1) + 9*(iz + 1)
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))
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)
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
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)))
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)
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)
1846 SUBROUTINE gamma_corr_image_columns(matrices, plan, cell_to_index, n_ao, n_img, para_env, values)
1849 INTEGER,
POINTER :: cell_to_index(:, :, :)
1850 INTEGER,
INTENT(IN) :: n_ao, n_img
1852 REAL(
dp),
ALLOCATABLE,
INTENT(OUT) :: values(:, :, :, :)
1854 CHARACTER(LEN=*),
PARAMETER :: routinen =
'gamma_corr_image_columns'
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(:)
1864 REAL(
dp) :: mirror_sign
1865 REAL(
dp),
ALLOCATABLE :: value_recv(:), value_send(:)
1866 REAL(
dp),
POINTER :: block(:, :)
1868 CALL timeset(routinen, handle)
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
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))
1886 IF (i_pass == 2)
THEN
1887 header_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)
1893 ALLOCATE (header_send(max(1, sum(header_counts))), value_send(max(1, sum(value_counts))))
1895 header_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
1908 IF (symmetry /= dbcsr_type_no_symmetry .AND. i_row > i_col)
THEN
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)
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)
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, &
1943 CALL para_env%alltoall(value_send, value_counts, value_displs, value_recv, value_recv_counts, &
1945 DEALLOCATE (header_send, value_send)
1948 ALLOCATE (values(n_ao, n_col, n_img,
SIZE(matrices, 1)), source=0.0_dp)
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)
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, &
1961 CALL timestop(handle)
1963 END SUBROUTINE gamma_corr_image_columns
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(:)
1989 INTEGER :: i_first, n_cols, n_rows, owner
1991 n_rows =
SIZE(piece, 1)
1993 DO WHILE (i_first <=
SIZE(piece, 2))
1994 owner = col_owner(first_col + i_first - 1)
1996 DO WHILE (i_first + n_cols <=
SIZE(piece, 2))
1997 IF (col_owner(first_col + i_first + n_cols - 1) /= owner)
EXIT
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])
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
2012 END SUBROUTINE pack_image_piece
2023 SUBROUTINE bloch_sum_columns(images, weight_re, weight_im, n_batch, bloch)
2024 REAL(dp),
INTENT(IN) :: images(:, :, :), weight_re(:, :), &
2026 INTEGER,
INTENT(IN) :: n_batch
2027 COMPLEX(dp),
INTENT(INOUT) :: bloch(:, :, :)
2029 INTEGER :: n_img, n_values
2030 REAL(dp),
ALLOCATABLE :: sum_im(:, :), sum_re(:, :)
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
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(:, :)
2056 COMPLEX(dp),
ALLOCATABLE :: work(:, :)
2057 INTEGER :: n_ao, n_virt
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), &
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)
2067 END SUBROUTINE project_virtual_occupied
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
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
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))
2091 max_image(i) = max(maxval(abs(itc_a(i, :))), maxval(abs(itc_b(i, :))))
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))))
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
2102 cell_to_index(image(1), image(2), image(3)) = n
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
2111 cell_to_index(image(1), image(2), image(3)) = n
2115 ALLOCATE (index_to_cell(3, n))
2116 index_to_cell =
list(:, 1:n)
2118 END SUBROUTINE gw_gamma_cell_index_union
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.
subroutine, public get_cell(cell, alpha, beta, gamma, deth, orthorhombic, abc, periodic, h, h_inv, symmetry_id, tag)
Get informations about a simulation cell.
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
subroutine, public cp_cfm_heevd(matrix, eigenvectors, eigenvalues)
Perform a diagonalisation of a complex matrix.
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
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.
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.
integer, parameter, public dp
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 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.
pure real(kind=dp) function, dimension(3, 3), public inv_3x3(a)
Returns the inverse of the 3 x 3 matrix a.
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...
pure real(kind=dp) function, dimension(3), public vector_product(a, b)
Calculation of the vector product c = a x b.
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>.
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.
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
Contains information about kpoints.
stores all the informations relevant to an mpi environment