35#include "./base/base_uses.f90"
40 CHARACTER(len=*),
PARAMETER,
PRIVATE :: moduleN =
'ri_rs_kp_fitting'
43 INTEGER(KIND=int_8),
PARAMETER :: q_batch_elements = 64_int_8*1024_int_8**2/16_int_8
60 REAL(kind=
dp),
DIMENSION(:, :),
INTENT(IN) :: grid_points
61 COMPLEX(KIND=dp),
ALLOCATABLE,
INTENT(OUT) :: z_lp_q(:, :, :), z_lp_zero(:, :, :)
63 CHARACTER(LEN=*),
PARAMETER :: routinen =
'compute_Z_lP_q'
65 COMPLEX(KIND=dp),
ALLOCATABLE :: charge_lq(:, :)
66 INTEGER :: first_l, handle, last_l, n_d_cells, &
68 INTEGER,
ALLOCATABLE :: active_cells(:), cells_r_d_llp(:, :), &
70 REAL(kind=
dp) :: elapsed, start_time
71 REAL(kind=
dp),
ALLOCATABLE :: d_llp_r(:, :, :), d_lp_r(:, :, :), &
72 qpoints_head(:, :), qpoints_scf(:, :)
74 CALL timeset(routinen, handle)
77 IF (bs_env%unit_nr > 0)
WRITE (bs_env%unit_nr,
'(T2,A)')
"RI-RS: Compute Z_lP(k); required: D_ll'^R and d_lP^R"
79 IF (bs_env%ri_rs%tikhonov < 0.0_dp)
THEN
80 cpabort(
"RI_RS%TIKHONOV must not be negative")
83 n_grid = bs_env%ri_rs%n_grid_points
85 cpassert(
SIZE(grid_points, 2) == n_grid)
86 cpassert(bs_env%nkp_scf_desymm > 0)
88 CALL build_cells_r_for_d_lp_r(bs_env, grid_points, cells_r_d_lp)
89 CALL build_cells_r_for_d_llp(bs_env, grid_points, cells_r_d_llp, active_cells)
90 n_d_lp_cells =
SIZE(cells_r_d_lp, 2)
91 n_d_cells =
SIZE(cells_r_d_llp, 2)
92 cpassert(n_d_lp_cells > 0)
93 cpassert(n_d_cells > 0)
94 IF (bs_env%unit_nr > 0)
THEN
95 WRITE (bs_env%unit_nr,
'(T2,A,T72,I9)')
'RI-RS d_lP^R cells R:', n_d_lp_cells
96 WRITE (bs_env%unit_nr,
'(T2,A,T72,I9)')
"RI-RS D_ll'^R cells R:", n_d_cells
97 FLUSH (bs_env%unit_nr)
101 CALL get_local_l_range(n_grid, bs_env%para_env%num_pe, bs_env%para_env%mepos, first_l, last_l)
102 ALLOCATE (d_llp_r(last_l - first_l + 1, n_grid, n_d_cells))
103 CALL compute_d_llp(bs_env, grid_points, cells_r_d_llp, active_cells, first_l, last_l, d_llp_r)
105 IF (bs_env%unit_nr > 0)
THEN
106 WRITE (bs_env%unit_nr,
'(T2,A)')
'RI-RS three-center projection d_lP^R: starting'
107 FLUSH (bs_env%unit_nr)
109 ALLOCATE (qpoints_scf(3, bs_env%nkp_scf_desymm))
110 qpoints_scf(:, :) = bs_env%kpoints_chi_eps_W%xkp(:, 1:bs_env%nkp_scf_desymm)
111 ALLOCATE (qpoints_head(3, 1), source=0.0_dp)
112 CALL compute_d_lp_r(bs_env, grid_points, cells_r_d_lp, d_lp_r, qpoints_head, charge_lq)
113 CALL mp_print_mem_per_rank(bs_env%para_env, bs_env%unit_nr,
'After RI-RS three-center projection d_lP^R')
115 CALL solve_for_z_lp_q(bs_env, cells_r_d_llp, cells_r_d_lp, d_llp_r, d_lp_r, first_l, last_l, qpoints_scf, &
116 z_lp_q, distributed=.true.)
117 CALL solve_for_z_lp_q(bs_env, cells_r_d_llp, cells_r_d_lp, d_llp_r, d_lp_r, first_l, last_l, &
118 qpoints_head, z_lp_zero, distributed=.false., charge_lq=charge_lq)
120 DEALLOCATE (cells_r_d_lp, cells_r_d_llp, d_llp_r, qpoints_scf, d_lp_r)
121 DEALLOCATE (qpoints_head)
124 CALL bs_env%para_env%max(elapsed)
125 IF (bs_env%unit_nr > 0)
WRITE (bs_env%unit_nr,
'(T2,A,T69,F10.1,A)') &
126 'Completed computing Z_lP(q), Exec. time:', elapsed,
' s'
127 IF (bs_env%unit_nr > 0)
FLUSH (bs_env%unit_nr)
129 CALL timestop(handle)
142 REAL(
dp),
INTENT(IN) :: grid_points(:, :)
143 INTEGER,
INTENT(IN) :: i_cell
144 REAL(
dp),
INTENT(OUT) :: ao_values(:, :)
146 CHARACTER(LEN=*),
PARAMETER :: routinen =
'evaluate_mu_rl_R_values'
148 INTEGER :: first_ao, handle, i_atom, i_kind, last_ao
149 REAL(
dp) :: cutoff_squared, source_position(3)
154 CALL timeset(routinen, handle)
156 cell => bs_env%ri_rs%cell
157 particle_set => bs_env%ri_rs%particle_set
158 cpassert(
SIZE(ao_values, 1) ==
SIZE(grid_points, 2))
159 cpassert(
SIZE(ao_values, 2) == bs_env%n_ao)
161 DO i_atom = 1, bs_env%n_atom
162 i_kind = particle_set(i_atom)%atomic_kind%kind_number
163 basis => bs_env%basis_set_AO(i_kind)%gto_basis_set
164 first_ao = bs_env%i_ao_start_from_atom(i_atom)
165 last_ao = bs_env%i_ao_end_from_atom(i_atom)
166 source_position =
pbc(particle_set(i_atom)%r, cell) + &
167 matmul(bs_env%hmat, real(bs_env%index_to_cell_3c(1:3, i_cell),
dp))
168 cutoff_squared = bs_env%ri_rs%radius_ao_per_atom(i_atom)**2
169 IF (bs_env%ri_rs%cutoff_radius_ri_ao > 0.0_dp)
THEN
170 cutoff_squared = bs_env%ri_rs%cutoff_radius_ri_ao**2
173 grid_points, basis, source_position, cell, &
174 cutoff_squared=cutoff_squared, minimum_image=.false.)
177 CALL timestop(handle)
197 SUBROUTINE solve_for_z_lp_q(bs_env, cells_R_D_llp, cells_R_d_lP, D_llp_R, d_lP_R, first_l, last_l, qpoints, &
198 Z_lP_q, distributed, charge_lq)
200 INTEGER,
DIMENSION(:, :),
INTENT(IN) :: cells_r_d_llp, cells_r_d_lp
201 REAL(kind=
dp),
CONTIGUOUS,
INTENT(IN) :: d_llp_r(:, :, :), d_lp_r(:, :, :)
202 INTEGER,
INTENT(IN) :: first_l, last_l
203 REAL(kind=
dp),
DIMENSION(:, :),
INTENT(IN) :: qpoints
204 COMPLEX(KIND=dp),
ALLOCATABLE,
INTENT(OUT) :: z_lp_q(:, :, :)
205 LOGICAL,
INTENT(IN) :: distributed
206 COMPLEX(KIND=dp),
INTENT(IN),
OPTIONAL :: charge_lq(:, :)
208 CHARACTER(LEN=*),
PARAMETER :: routinen =
'solve_for_Z_lP_q'
210 COMPLEX(KIND=dp) :: empty_matrix(0, 0), phase
211 COMPLEX(KIND=dp),
ALLOCATABLE :: d_llp_q(:, :), d_llp_q_batch(:, :, :), &
212 d_llp_q_local(:, :), d_lp_q(:, :)
213 INTEGER :: first_l_rank, first_q, handle, i_batch, i_rank, info, iq, iq_batch, iq_local, &
214 iq_store, l, last_l_rank, last_q, lp, n_grid, n_q, n_q_local, n_ri, n_ri_solve, n_sub, &
216 INTEGER(KIND=int_8) :: n_done, n_local
217 INTEGER,
ALLOCATABLE :: displs(:), recvcounts(:)
218 LOGICAL,
ALLOCATABLE :: active_grid_point(:)
219 REAL(kind=
dp) :: max_scale, metric_head, progress_start, &
221 REAL(kind=
dp),
ALLOCATABLE :: scale(:)
223 CALL timeset(routinen, handle)
225 n_grid = bs_env%ri_rs%n_grid_points
228 IF (
PRESENT(charge_lq))
THEN
230 cpabort(
'Unsupported RI metric')
232 n_ri_solve = n_ri + 1
234 n_q =
SIZE(qpoints, 2)
236 iq =
modulo(bs_env%para_env%mepos - merge(1, 0, distributed), bs_env%para_env%num_pe) + 1
238 IF (distributed) n_q_local = (n_q + bs_env%para_env%num_pe - iq)/bs_env%para_env%num_pe
239 ALLOCATE (z_lp_q(n_grid, n_ri_solve, n_q_local))
242 ALLOCATE (d_llp_q(n_grid, n_grid))
244 ALLOCATE (d_llp_q(0, 0))
247 n_sub = int(max(1_int_8, min(int(bs_env%para_env%num_pe,
int_8), q_batch_elements/ &
248 max(1_int_8,
SIZE(d_llp_r, 1, kind=
int_8)*n_grid))))
249 ALLOCATE (d_llp_q_local(n_grid, last_l - first_l + 1), &
250 d_llp_q_batch(last_l - first_l + 1, n_grid, n_sub), &
251 recvcounts(bs_env%para_env%num_pe), displs(bs_env%para_env%num_pe), &
252 d_lp_q(n_grid, n_ri_solve))
253 DO i_rank = 0, bs_env%para_env%num_pe - 1
254 CALL get_local_l_range(n_grid, bs_env%para_env%num_pe, i_rank, first_l_rank, last_l_rank)
255 recvcounts(i_rank + 1) = (last_l_rank - first_l_rank + 1)*n_grid
256 displs(i_rank + 1) = sum(recvcounts(1:i_rank))
258 ALLOCATE (scale(n_grid), active_grid_point(n_grid))
265 IF (bs_env%para_env%mepos == 0)
THEN
266 DO iq = merge(bs_env%para_env%num_pe, 1, distributed), n_q, bs_env%para_env%num_pe
267 n_local = n_local + 1_int_8
271 DO i_batch = 0, (n_q - 1)/bs_env%para_env%num_pe
273 DO first_q = i_batch*bs_env%para_env%num_pe + 1, &
274 min(n_q, (i_batch + 1)*bs_env%para_env%num_pe), n_sub
275 last_q = min(first_q + n_sub - 1, n_q, (i_batch + 1)*bs_env%para_env%num_pe)
276 CALL lattice_sum_to_q(
SIZE(d_llp_q_batch, 1)*n_grid, d_llp_r, cells_r_d_llp, &
277 qpoints(:, first_q:last_q), -1.0_dp, d_llp_q_batch)
278 DO iq_batch = first_q, last_q
279 d_llp_q_local(:, :) = transpose(d_llp_q_batch(:, :, iq_batch - first_q + 1))
280 i_rank =
modulo(iq_batch - 1, bs_env%para_env%num_pe)
281 IF (distributed) i_rank =
modulo(iq_batch, bs_env%para_env%num_pe)
283 IF (bs_env%para_env%mepos == i_rank)
THEN
284 CALL bs_env%para_env%gatherv(d_llp_q_local, d_llp_q, recvcounts, displs, i_rank)
286 CALL bs_env%para_env%gatherv(d_llp_q_local, empty_matrix, recvcounts, displs, i_rank)
290 IF (
PRESENT(charge_lq))
THEN
291 CALL fourier_transform_d_lp_r_to_q(bs_env, cells_r_d_lp, d_lp_r, qpoints, i_batch, &
292 distributed, d_lp_q(:, 2:), iq)
294 CALL fourier_transform_d_lp_r_to_q(bs_env, cells_r_d_lp, d_lp_r, qpoints, i_batch, &
295 distributed, d_lp_q, iq)
298 iq_local = iq_local + 1
299 IF (distributed)
THEN
304 IF (
PRESENT(charge_lq))
THEN
306 volume = bs_env%ri_rs%cell%deth
307 IF (all(bs_env%periodic == [1, 1, 0]))
THEN
310 volume = norm2([bs_env%hmat(2, 1)*bs_env%hmat(3, 2) - &
311 bs_env%hmat(3, 1)*bs_env%hmat(2, 2), &
312 bs_env%hmat(3, 1)*bs_env%hmat(1, 2) - &
313 bs_env%hmat(1, 1)*bs_env%hmat(3, 2), &
314 bs_env%hmat(1, 1)*bs_env%hmat(2, 2) - &
315 bs_env%hmat(2, 1)*bs_env%hmat(1, 2)])
317 cpassert(volume > 0.0_dp)
318 metric_head =
pi/bs_env%ri_metric%omega**2
319 d_lp_q(:, 1) = metric_head/sqrt(volume)*charge_lq(:, iq)
324 phase = 0.5_dp*(d_llp_q(l, lp) + conjg(d_llp_q(lp, l)))
325 d_llp_q(l, lp) = phase
326 d_llp_q(lp, l) = conjg(phase)
331 scale(l) = sqrt(max(real(d_llp_q(l, l), kind=
dp), tiny(1.0_dp)))
333 max_scale = maxval(scale)
334 active_grid_point(:) = scale > &
335 sqrt(max(bs_env%eps_filter, epsilon(1.0_dp)))*max_scale
336 IF (.NOT. any(active_grid_point))
THEN
337 cpabort(
"Periodic RI-RS D(q) has no active grid points")
343 IF (active_grid_point(l) .AND. active_grid_point(lp))
THEN
344 d_llp_q(l, lp) = d_llp_q(l, lp)/(scale(l)*scale(lp))
351 d_llp_q(l, l) = d_llp_q(l, l) + bs_env%ri_rs%tikhonov*
z_one
352 IF (active_grid_point(l))
THEN
353 d_lp_q(l, :) = d_lp_q(l, :)/scale(l)
360 IF (bs_env%ri_rs%tikhonov <= 0.0_dp)
THEN
361 CALL solve_unregularized_d_llp(d_llp_q, d_lp_q)
363 CALL zpotrf(
'L', n_grid, d_llp_q, n_grid, info)
364 IF (info == 0)
CALL zpotrs(
'L', n_grid, n_ri_solve, d_llp_q, n_grid, d_lp_q, n_grid, info)
365 IF (info /= 0) cpabort(
"Periodic RI-RS regularized solve failed")
369 z_lp_q(l, :, iq_store) = d_lp_q(l, :)/scale(l)
371 IF (bs_env%para_env%mepos == 0 .AND. n_local > 1)
THEN
372 n_done = n_done + 1_int_8
374 n_done, n_local, next_progress, progress_start)
377 IF (distributed)
THEN
378 cpassert(iq_local == n_q_local)
380 IF (.NOT. distributed)
CALL bs_env%para_env%sum(z_lp_q)
381 DEALLOCATE (d_llp_q, d_llp_q_batch, d_llp_q_local, recvcounts, displs, d_lp_q, scale, active_grid_point)
383 CALL timestop(handle)
385 END SUBROUTINE solve_for_z_lp_q
394 SUBROUTINE solve_unregularized_d_llp(D_llp_q, rhs)
395 COMPLEX(KIND=dp),
DIMENSION(:, :),
INTENT(INOUT) :: d_llp_q, rhs
397 COMPLEX(KIND=dp),
ALLOCATABLE :: projected_rhs(:, :), work(:)
398 INTEGER :: ieigenvalue, info, ngrid
399 REAL(kind=
dp) :: rank_tolerance
400 REAL(kind=
dp),
ALLOCATABLE :: eigenvalues(:), rwork(:)
402 ngrid =
SIZE(d_llp_q, 1)
404 cpassert(
SIZE(d_llp_q, 2) == ngrid)
405 cpassert(
SIZE(rhs, 1) == ngrid)
406 ALLOCATE (eigenvalues(ngrid), rwork(max(1, 3*ngrid - 2)), &
407 work(max(1, 2*ngrid - 1)), projected_rhs(ngrid,
SIZE(rhs, 2)))
408 CALL zheev(
'V',
'L', ngrid, d_llp_q, ngrid, eigenvalues, work,
SIZE(work), rwork, info)
409 IF (info /= 0) cpabort(
'Unregularized periodic RI-RS eigensolver failed')
410 rank_tolerance = epsilon(1.0_dp)*real(ngrid,
dp)*maxval(abs(eigenvalues))
411 IF (.NOT. any(eigenvalues > rank_tolerance))
THEN
412 cpabort(
'Unregularized RI-RS D matrix has zero numerical rank')
415 CALL zgemm(
'C',
'N', ngrid,
SIZE(rhs, 2), ngrid,
z_one, d_llp_q, ngrid, &
416 rhs, ngrid,
z_zero, projected_rhs, ngrid)
417 DO ieigenvalue = 1, ngrid
418 IF (eigenvalues(ieigenvalue) > rank_tolerance)
THEN
419 projected_rhs(ieigenvalue, :) = &
420 projected_rhs(ieigenvalue, :)/eigenvalues(ieigenvalue)
422 projected_rhs(ieigenvalue, :) =
z_zero
425 CALL zgemm(
'N',
'N', ngrid,
SIZE(rhs, 2), ngrid,
z_one, d_llp_q, ngrid, &
426 projected_rhs, ngrid,
z_zero, rhs, ngrid)
428 END SUBROUTINE solve_unregularized_d_llp
436 SUBROUTINE build_cells_r_for_d_lp_r(bs_env, grid_points, cells_R_d_lP)
438 REAL(kind=
dp),
DIMENSION(:, :),
INTENT(IN) :: grid_points
439 INTEGER,
ALLOCATABLE,
INTENT(OUT) :: cells_r_d_lp(:, :)
441 INTEGER :: cell_index(3), i_atom, i_l, ix, iy, iz, &
442 n_max(3), n_ri, n_work
443 INTEGER,
ALLOCATABLE :: cells_work(:, :)
445 REAL(kind=
dp) :: center(3), cutoff, max_cutoff
446 REAL(kind=
dp),
ALLOCATABLE :: automatic_cutoffs(:)
449 particle_set => bs_env%ri_rs%particle_set
451 max_cutoff = maxval(automatic_cutoffs)
453 IF (bs_env%periodic(ix) == 1)
THEN
459 n_max(ix) = ceiling(max_cutoff*norm2(bs_env%ri_rs%cell%h_inv(ix, :))) + 1
465 n_work = product(2*n_max + 1)
466 ALLOCATE (cells_work(3, n_work))
468 DO ix = -n_max(1), n_max(1)
469 DO iy = -n_max(2), n_max(2)
470 DO iz = -n_max(3), n_max(3)
471 cell_index = [ix, iy, iz]
473 DO i_atom = 1, bs_env%n_atom
474 cutoff = automatic_cutoffs(i_atom)
475 center =
pbc(particle_set(i_atom)%r, bs_env%ri_rs%cell) + &
476 matmul(bs_env%hmat, real(cell_index,
dp))
477 DO i_l = 1, bs_env%ri_rs%n_grid_points
478 IF (norm2(center - grid_points(:, i_l)) <= cutoff)
THEN
485 IF (.NOT. keep) cycle
487 cells_work(:, n_ri) = cell_index
492 ALLOCATE (cells_r_d_lp(3, n_ri))
493 cells_r_d_lp(:, :) = cells_work(:, 1:n_ri)
494 IF (bs_env%unit_nr > 0)
THEN
495 WRITE (bs_env%unit_nr,
'(T2,A,T69,F10.1,A)') &
496 'Periodic RI-RS automatic R_cut^(lP):', &
499 DEALLOCATE (automatic_cutoffs, cells_work)
501 END SUBROUTINE build_cells_r_for_d_lp_r
515 SUBROUTINE build_cells_r_for_d_llp(bs_env, grid_points, cells_R_D_llp, active_cells)
517 REAL(
dp),
INTENT(IN) :: grid_points(:, :)
518 INTEGER,
ALLOCATABLE,
INTENT(OUT) :: cells_r_d_llp(:, :), active_cells(:)
520 INTEGER :: cell_index(3), i, i_r, i_r_prime, &
522 INTEGER,
ALLOCATABLE :: cells_work(:, :), nonzero_cells(:)
524 REAL(
dp),
ALLOCATABLE :: phi(:, :)
526 ALLOCATE (nonzero_cells(
SIZE(bs_env%index_to_cell_3c, 2)))
527 ALLOCATE (phi(bs_env%ri_rs%n_grid_points, bs_env%n_ao))
529 DO i_r = 1,
SIZE(bs_env%index_to_cell_3c, 2)
531 IF (.NOT. any(abs(phi) > 0.0_dp)) cycle
532 n_active = n_active + 1
533 nonzero_cells(n_active) = i_r
535 cpassert(n_active > 0)
536 ALLOCATE (active_cells(n_active))
537 active_cells(:) = nonzero_cells(:n_active)
539 ALLOCATE (cells_work(3, n_active*n_active))
542 DO i_r_prime = 1, n_active
543 cell_index = bs_env%index_to_cell_3c(:, active_cells(i_r)) - &
544 bs_env%index_to_cell_3c(:, active_cells(i_r_prime))
547 IF (all(cell_index == cells_work(:, i)))
THEN
552 IF (.NOT. found)
THEN
553 n_d_cells = n_d_cells + 1
554 cells_work(:, n_d_cells) = cell_index
559 ALLOCATE (cells_r_d_llp(3, n_d_cells))
560 cells_r_d_llp(:, :) = cells_work(:, 1:n_d_cells)
561 DEALLOCATE (cells_work, phi)
563 END SUBROUTINE build_cells_r_for_d_llp
576 SUBROUTINE compute_d_llp(bs_env, grid_points, cells_R_D_llp, active_cells, first_l, last_l, D_llp_R)
578 REAL(
dp),
INTENT(IN) :: grid_points(:, :)
579 INTEGER,
DIMENSION(:, :),
INTENT(IN) :: cells_r_d_llp
580 INTEGER,
INTENT(IN) :: active_cells(:), first_l, last_l
581 REAL(kind=
dp),
CONTIGUOUS,
INTENT(OUT) :: d_llp_r(:, :, :)
583 CHARACTER(LEN=*),
PARAMETER :: routinen =
'compute_D_llp'
585 INTEGER :: cell_a(3), handle, i_a, i_active, &
586 i_active_prime, i_r, i_r_prime, &
588 INTEGER(KIND=int_8) :: n_done, n_total
590 REAL(kind=
dp) :: progress_start
591 REAL(kind=
dp),
ALLOCATABLE :: phi_full(:, :), phi_local(:, :)
593 CALL timeset(routinen, handle)
598 n_total = int(
SIZE(active_cells),
int_8)**2
601 nrows = max(0, last_l - first_l + 1)
602 IF (first_l <= last_l)
THEN
603 ALLOCATE (phi_local(nrows, bs_env%n_ao), phi_full(bs_env%ri_rs%n_grid_points, bs_env%n_ao))
604 DO i_active_prime = 1,
SIZE(active_cells)
605 i_r_prime = active_cells(i_active_prime)
607 DO i_active = 1,
SIZE(active_cells)
608 i_r = active_cells(i_active)
611 n_done, n_total, next_progress, progress_start)
613 cell_a = bs_env%index_to_cell_3c(:, i_r) - &
614 bs_env%index_to_cell_3c(:, i_r_prime)
615 CALL find_cell(cell_a, cells_r_d_llp, found, i_a)
616 IF (.NOT. found) cycle
617 CALL dgemm(
'N',
'T', nrows, bs_env%ri_rs%n_grid_points, bs_env%n_ao, &
618 1.0_dp, phi_local, max(1, nrows), phi_full, &
619 max(1, bs_env%ri_rs%n_grid_points), 1.0_dp, d_llp_r(:, :, i_a), &
625 n_done, n_total, next_progress, progress_start)
626 DO i_a = 1,
SIZE(d_llp_r, 3)
627 d_llp_r(:, :, i_a) = d_llp_r(:, :, i_a)*d_llp_r(:, :, i_a)
629 IF (
ALLOCATED(phi_local))
DEALLOCATE (phi_local, phi_full)
631 CALL timestop(handle)
633 END SUBROUTINE compute_d_llp
642 SUBROUTINE find_cell(cell, index_to_cell, found, index)
643 INTEGER,
DIMENSION(3),
INTENT(IN) :: cell
644 INTEGER,
DIMENSION(:, :),
INTENT(IN) :: index_to_cell
645 LOGICAL,
INTENT(OUT) :: found
646 INTEGER,
INTENT(OUT) :: index
652 DO i = 1,
SIZE(index_to_cell, 2)
653 IF (all(cell == index_to_cell(:, i)))
THEN
660 END SUBROUTINE find_cell
675 SUBROUTINE fourier_transform_d_lp_r_to_q(bs_env, cells_R_d_lP, d_lP_R, qpoints, i_batch, distributed, &
678 INTEGER,
INTENT(IN) :: cells_r_d_lp(:, :)
679 REAL(
dp),
CONTIGUOUS,
INTENT(IN) :: d_lp_r(:, :, :)
680 REAL(
dp),
INTENT(IN) :: qpoints(:, :)
681 INTEGER,
INTENT(IN) :: i_batch
682 LOGICAL,
INTENT(IN) :: distributed
683 COMPLEX(dp), asynchronous,
CONTIGUOUS,
INTENT(OUT) :: d_lp_q(:, :)
684 INTEGER,
INTENT(OUT) :: iq
686 CHARACTER(LEN=*),
PARAMETER :: routinen =
'fourier_transform_d_lP_R_to_q'
688 COMPLEX(dp),
ALLOCATABLE :: result_q(:, :, :)
689 COMPLEX(dp),
ALLOCATABLE, asynchronous :: receive_q(:), send_q(:, :, :)
690 INTEGER :: first_l, first_q, handle, i_dest, i_l, i_p, i_q, i_rank, i_slot, last_l, last_q, &
691 local_first_l, local_last_l, n_active_q, n_l, n_p, n_ranks, n_recv, n_sub, position, &
693 INTEGER,
ALLOCATABLE :: q_dest(:), q_indices(:)
694 LOGICAL :: receiving, sending(2)
698 CALL timeset(routinen, handle)
700 n_ranks = bs_env%para_env%num_pe
701 i_rank = bs_env%para_env%mepos
703 IF (distributed) q_offset = 1
704 iq = i_batch*n_ranks +
modulo(i_rank - q_offset, n_ranks) + 1
705 CALL get_local_l_range(bs_env%ri_rs%n_grid_points, n_ranks, i_rank, local_first_l, local_last_l)
706 cpassert(
SIZE(d_lp_r, 1) == local_last_l - local_first_l + 1)
707 cpassert(
SIZE(d_lp_r, 2) ==
SIZE(d_lp_q, 2))
708 cpassert(
SIZE(d_lp_q, kind=
int_8) <= int(huge(0),
int_8))
710 ALLOCATE (recv_requests(n_ranks), send_q(
SIZE(d_lp_r, 1), n_p, 2))
713 receiving = iq <=
SIZE(qpoints, 2)
717 ALLOCATE (receive_q(
SIZE(d_lp_q)))
718 DO i_dest = 0, n_ranks - 1
719 IF (i_dest == i_rank) cycle
720 CALL get_local_l_range(bs_env%ri_rs%n_grid_points, n_ranks, i_dest, first_l, last_l)
721 IF (last_l < first_l .OR. n_p == 0) cycle
723 CALL bs_env%para_env%irecv(receive_q((first_l - 1)*n_p + 1:last_l*n_p), &
724 i_dest, recv_requests(n_recv), tag=29571)
728 ALLOCATE (q_indices(n_ranks), q_dest(n_ranks))
729 DO i_dest = 0, n_ranks - 1
730 i_q = i_batch*n_ranks +
modulo(i_dest - q_offset, n_ranks) + 1
731 IF (i_q >
SIZE(qpoints, 2)) cycle
732 n_active_q = n_active_q + 1
733 q_indices(n_active_q) = i_q
734 q_dest(n_active_q) = i_dest
737 IF (
SIZE(d_lp_r, 1) > 0 .AND. n_p > 0)
THEN
739 n_sub = int(max(1_int_8, min(int(n_active_q,
int_8), q_batch_elements/ &
740 (
SIZE(d_lp_r, 1, kind=
int_8)*n_p))))
741 ALLOCATE (result_q(
SIZE(d_lp_r, 1), n_p, n_sub))
742 DO first_q = 1, n_active_q, n_sub
743 last_q = min(first_q + n_sub - 1, n_active_q)
744 CALL lattice_sum_to_q(
SIZE(d_lp_r, 1)*n_p, d_lp_r, cells_r_d_lp, &
745 qpoints(:, q_indices(first_q:last_q)), 1.0_dp, result_q)
746 DO q_column = first_q, last_q
747 i_dest = q_dest(q_column)
748 i_slot =
modulo(i_dest, 2) + 1
749 IF (sending(i_slot))
CALL mp_waitall(send_requests(i_slot:i_slot))
750 sending(i_slot) = .false.
751 send_q(:, :, i_slot) = result_q(:, :, q_column - first_q + 1)
752 IF (i_dest == i_rank)
THEN
753 d_lp_q(local_first_l:local_last_l, :) = send_q(:, :, i_slot)
755 CALL bs_env%para_env%isend(send_q(:, :, i_slot), i_dest, send_requests(i_slot), tag=29571)
756 sending(i_slot) = .true.
760 DEALLOCATE (result_q)
763 IF (sending(i_slot))
CALL mp_waitall(send_requests(i_slot:i_slot))
765 IF (n_recv > 0)
CALL mp_waitall(recv_requests(:n_recv))
767 DO i_dest = 0, n_ranks - 1
768 IF (i_dest == i_rank) cycle
769 CALL get_local_l_range(bs_env%ri_rs%n_grid_points, n_ranks, i_dest, first_l, last_l)
770 n_l = last_l - first_l + 1
772 position = (first_l - 1)*n_p + (i_p - 1)*n_l
774 d_lp_q(first_l + i_l - 1, i_p) = receive_q(position + i_l)
778 DEALLOCATE (receive_q)
780 DEALLOCATE (send_q, recv_requests)
782 CALL timestop(handle)
784 END SUBROUTINE fourier_transform_d_lp_r_to_q
796 SUBROUTINE lattice_sum_to_q(n, values_R, cells, qpoints, sign, values_q)
797 INTEGER,
INTENT(IN) :: n, cells(:, :)
798 REAL(kind=
dp),
INTENT(IN) :: values_r(n,
SIZE(cells, 2)), &
800 COMPLEX(KIND=dp),
INTENT(OUT) :: values_q(n, size(qpoints, 2))
802 INTEGER,
PARAMETER :: row_block = 4096
804 COMPLEX(KIND=dp) :: phase
805 INTEGER :: first, i_r, iq, n_q, n_r, nrows
806 REAL(kind=
dp),
ALLOCATABLE :: phase_cos(:, :), phase_sin(:, :), &
807 work_cos(:, :), work_sin(:, :)
810 n_q =
SIZE(qpoints, 2)
815 ALLOCATE (phase_cos(n_r, n_q), phase_sin(n_r, n_q))
818 phase = exp(cmplx(0.0_dp, sign*
twopi*sum(qpoints(1:3, iq)*real(cells(1:3, i_r),
dp)), kind=
dp))
819 phase_cos(i_r, iq) = real(phase, kind=
dp)
820 phase_sin(i_r, iq) = aimag(phase)
825 ALLOCATE (work_cos(row_block, n_q), work_sin(row_block, n_q))
827 DO first = 1, n, row_block
828 nrows = min(row_block, n - first + 1)
829 CALL dgemm(
'N',
'N', nrows, n_q, n_r, 1.0_dp, values_r(first, 1), n, phase_cos, n_r, &
830 0.0_dp, work_cos, row_block)
831 CALL dgemm(
'N',
'N', nrows, n_q, n_r, 1.0_dp, values_r(first, 1), n, phase_sin, n_r, &
832 0.0_dp, work_sin, row_block)
833 values_q(first:first + nrows - 1, :) = cmplx(work_cos(1:nrows, :), work_sin(1:nrows, :), kind=
dp)
836 DEALLOCATE (work_cos, work_sin)
839 END SUBROUTINE lattice_sum_to_q
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.
Defines the basic variable types.
integer, parameter, public int_8
integer, parameter, public dp
Machine interface based on Fortran 2003 and POSIX.
real(kind=dp) function, public m_walltime()
returns time from a real-time clock, protected against rolling early/easily
Definition of mathematical constants and functions.
real(kind=dp), parameter, public pi
complex(kind=dp), parameter, public z_one
real(kind=dp), parameter, public twopi
complex(kind=dp), parameter, public z_zero
Interface to the message passing library MPI.
subroutine, public mp_print_mem_per_rank(comm, unit_nr, label)
Prints the memory available to and used by a single MPI rank at the moment of the call....
Define the data structure for the particle information.
Definition of physical constants:
real(kind=dp), parameter, public angstrom
AO sampling, D_llp(R), and the periodic RI-RS fit Z_lP(q).
subroutine, public compute_z_lp_q(bs_env, grid_points, z_lp_q, z_lp_zero)
Build explicit-cell AO values and periodic RI-RS coefficients Z_lP(q).
subroutine, public evaluate_mu_rl_r_values(bs_env, grid_points, i_cell, ao_values)
Evaluate ϕ_μ(r_l-R) locally for one cell R and the supplied points r_l.
Numerical integrals d_lP(R) for periodic RI-RS fitting.
subroutine, public compute_d_lp_r(bs_env, grid_points, cells_r_d_lp, d_lp_r, qpoints, charge_lq)
Allocate d_lP(R), prepare its integration grid, and evaluate the integrals.
subroutine, public get_local_l_range(n_grid, n_ranks, i_rank, first_l, last_l)
Distribute individual l rows evenly across MPI ranks; empty ranges are valid.
subroutine, public compute_d_lp_cutoffs(bs_env, cutoffs)
Automatic l/P cutoff from the overlap of the diffuse AO-field square and RI potential.
subroutine, public write_ri_rs_progress(unit_nr, phase, n_done, n_total, next_progress, phase_start)
Print one rank-local RI-RS work milestone without synchronizing MPI ranks. Percentages count complete...
Geometry and AO evaluation for real-space resolution of the identity.
subroutine, public evaluate_ao_basis_on_points(phi, grid_points, basis, source_position, cell, dphi, cutoff_squared, periodic_images, minimum_image)
Evaluate one explicitly supplied contracted Gaussian basis on Cartesian points.
Type defining parameters related to the simulation cell.