(git:27c966b)
Loading...
Searching...
No Matches
ri_rs_kp_fitting.F
Go to the documentation of this file.
1!--------------------------------------------------------------------------------------------------!
2! CP2K: A general program to perform molecular dynamics simulations !
3! Copyright 2000-2026 CP2K developers group <https://cp2k.org> !
4! !
5! SPDX-License-Identifier: GPL-2.0-or-later !
6!--------------------------------------------------------------------------------------------------!
7
8! **************************************************************************************************
9!> \brief AO sampling, D_llp(R), and the periodic RI-RS fit Z_lP(q).
10!> \author Jan Wilhelm (with help of ChatGPT, GPT-6 Sol and GPT-6 Astra)
11! **************************************************************************************************
14 USE cell_types, ONLY: cell_type,&
15 pbc
17 USE kinds, ONLY: dp,&
18 int_8
19 USE machine, ONLY: m_walltime
20 USE mathconstants, ONLY: pi,&
21 twopi,&
22 z_one,&
23 z_zero
28 USE physcon, ONLY: angstrom
35#include "./base/base_uses.f90"
36
37 IMPLICIT NONE
38 PRIVATE
39
40 CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'ri_rs_kp_fitting'
41
42 ! Complex numbers in the 64 MiB batches of q points.
43 INTEGER(KIND=int_8), PARAMETER :: q_batch_elements = 64_int_8*1024_int_8**2/16_int_8
44
45 PUBLIC :: compute_z_lp_q, &
47
48CONTAINS
49
50! **************************************************************************************************
51!> \brief Build explicit-cell AO values and periodic RI-RS coefficients Z_lP(q).
52!> \param bs_env Band-structure environment.
53!> \param grid_points Reference-cell RI-RS points r_l.
54!> \param Z_lP_q Gaussian RI-RS coefficients on the shifted SCF mesh.
55!> \param Z_lP_zero ...
56! **************************************************************************************************
57 SUBROUTINE compute_z_lp_q(bs_env, grid_points, &
58 Z_lP_q, Z_lP_zero)
59 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
60 REAL(kind=dp), DIMENSION(:, :), INTENT(IN) :: grid_points
61 COMPLEX(KIND=dp), ALLOCATABLE, INTENT(OUT) :: z_lp_q(:, :, :), z_lp_zero(:, :, :)
62
63 CHARACTER(LEN=*), PARAMETER :: routinen = 'compute_Z_lP_q'
64
65 COMPLEX(KIND=dp), ALLOCATABLE :: charge_lq(:, :)
66 INTEGER :: first_l, handle, last_l, n_d_cells, &
67 n_d_lp_cells, n_grid
68 INTEGER, ALLOCATABLE :: active_cells(:), cells_r_d_llp(:, :), &
69 cells_r_d_lp(:, :)
70 REAL(kind=dp) :: elapsed, start_time
71 REAL(kind=dp), ALLOCATABLE :: d_llp_r(:, :, :), d_lp_r(:, :, :), &
72 qpoints_head(:, :), qpoints_scf(:, :)
73
74 CALL timeset(routinen, handle)
75
76 start_time = m_walltime()
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"
78
79 IF (bs_env%ri_rs%tikhonov < 0.0_dp) THEN
80 cpabort("RI_RS%TIKHONOV must not be negative")
81 END IF
82
83 n_grid = bs_env%ri_rs%n_grid_points
84 cpassert(n_grid > 0)
85 cpassert(SIZE(grid_points, 2) == n_grid)
86 cpassert(bs_env%nkp_scf_desymm > 0)
87
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)
98 END IF
99
100 ! Each rank stores a rectangular (l,l') block for every translation A.
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)
104 CALL mp_print_mem_per_rank(bs_env%para_env, bs_env%unit_nr, "After distribution of D_ll'^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)
108 END IF
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')
114
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)
119
120 DEALLOCATE (cells_r_d_lp, cells_r_d_llp, d_llp_r, qpoints_scf, d_lp_r)
121 DEALLOCATE (qpoints_head)
122
123 elapsed = m_walltime() - start_time
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)
128
129 CALL timestop(handle)
130
131 END SUBROUTINE compute_z_lp_q
132
133! **************************************************************************************************
134!> \brief Evaluate ϕ_μ(r_l-R) locally for one cell R and the supplied points r_l.
135!> \param bs_env ...
136!> \param grid_points ...
137!> \param i_cell Index of R in index_to_cell_3c.
138!> \param ao_values AO values indexed (l,μ), without MPI communication.
139! **************************************************************************************************
140 SUBROUTINE evaluate_mu_rl_r_values(bs_env, grid_points, i_cell, ao_values)
141 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
142 REAL(dp), INTENT(IN) :: grid_points(:, :)
143 INTEGER, INTENT(IN) :: i_cell
144 REAL(dp), INTENT(OUT) :: ao_values(:, :)
145
146 CHARACTER(LEN=*), PARAMETER :: routinen = 'evaluate_mu_rl_R_values'
147
148 INTEGER :: first_ao, handle, i_atom, i_kind, last_ao
149 REAL(dp) :: cutoff_squared, source_position(3)
150 TYPE(cell_type), POINTER :: cell
151 TYPE(gto_basis_set_type), POINTER :: basis
152 TYPE(particle_type), POINTER :: particle_set(:)
153
154 CALL timeset(routinen, handle)
155
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)
160 ao_values = 0.0_dp
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
171 END IF
172 CALL evaluate_ao_basis_on_points(ao_values(:, first_ao:last_ao), &
173 grid_points, basis, source_position, cell, &
174 cutoff_squared=cutoff_squared, minimum_image=.false.)
175 END DO
176
177 CALL timestop(handle)
178
179 END SUBROUTINE evaluate_mu_rl_r_values
180
181! **************************************************************************************************
182!> \brief Solve the periodic RI-RS equations at the supplied momentum-transfer points.
183!> At zero Tikhonov parameter the Hermitian D matrix is solved in the
184!> Moore-Penrose sense, yielding the minimum-norm unregularized fit.
185!> \param bs_env ...
186!> \param cells_R_D_llp ...
187!> \param cells_R_d_lP ...
188!> \param D_llp_R ...
189!> \param d_lP_R ...
190!> \param first_l ...
191!> \param last_l ...
192!> \param qpoints ...
193!> \param Z_lP_q ...
194!> \param distributed ...
195!> \param charge_lq ...
196! **************************************************************************************************
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)
199 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
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(:, :)
207
208 CHARACTER(LEN=*), PARAMETER :: routinen = 'solve_for_Z_lP_q'
209
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, &
215 next_progress
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, &
220 volume
221 REAL(kind=dp), ALLOCATABLE :: scale(:)
222
223 CALL timeset(routinen, handle)
224
225 n_grid = bs_env%ri_rs%n_grid_points
226 n_ri = bs_env%n_RI
227 n_ri_solve = n_ri
228 IF (PRESENT(charge_lq)) THEN
229 IF (bs_env%ri_metric%potential_type /= do_potential_short) THEN
230 cpabort('Unsupported RI metric')
231 END IF
232 n_ri_solve = n_ri + 1
233 END IF
234 n_q = SIZE(qpoints, 2)
235 ! q points are cyclic: rank MOD(q,P) for the SCF mesh, MOD(q-1,P) otherwise.
236 iq = modulo(bs_env%para_env%mepos - merge(1, 0, distributed), bs_env%para_env%num_pe) + 1
237 n_q_local = n_q
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))
240 ! Only ranks solving a q point need a complete D(q).
241 IF (iq <= n_q) THEN
242 ALLOCATE (d_llp_q(n_grid, n_grid))
243 ELSE
244 ALLOCATE (d_llp_q(0, 0))
245 END IF
246 ! D(q) of up to n_sub q points at a time, bounded to 64 MiB unless one q is larger.
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))
257 END DO
258 ALLOCATE (scale(n_grid), active_grid_point(n_grid))
259 z_lp_q = z_zero
260
261 progress_start = m_walltime()
262 next_progress = 1
263 n_done = 0_int_8
264 n_local = 0_int_8
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
268 END DO
269 END IF
270 iq_local = 0
271 DO i_batch = 0, (n_q - 1)/bs_env%para_env%num_pe
272 ! D_ll'(q) = sum_A exp(i q.A) D_ll'^A; gather only on the q rank.
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)
282 ! Gather conjugate-transposed row blocks directly into contiguous columns.
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)
285 ELSE
286 CALL bs_env%para_env%gatherv(d_llp_q_local, empty_matrix, recvcounts, displs, i_rank)
287 END IF
288 END DO
289 END DO
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)
293 ELSE
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)
296 END IF
297 IF (iq > n_q) cycle
298 iq_local = iq_local + 1
299 IF (distributed) THEN
300 iq_store = iq_local
301 ELSE
302 iq_store = iq
303 END IF
304 IF (PRESENT(charge_lq)) THEN
305 ! The Gamma-correction scalar head is separate. Keep the full Gaussian RI.
306 volume = bs_env%ri_rs%cell%deth
307 IF (all(bs_env%periodic == [1, 1, 0])) THEN
308 ! The separated 2D charge channel is normalized by layer area,
309 ! independent of the vacuum-cell height.
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)])
316 END IF
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)
320 END IF
321
322 DO lp = 1, n_grid
323 DO l = 1, lp
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)
327 END DO
328 END DO
329
330 DO l = 1, n_grid
331 scale(l) = sqrt(max(real(d_llp_q(l, l), kind=dp), tiny(1.0_dp)))
332 END DO
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")
338 END IF
339
340 ! Form D'_ll'(q) = D_ll'(q)/(s_l s_l') + eta delta_ll'.
341 DO lp = 1, n_grid
342 DO l = 1, n_grid
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))
345 ELSE
346 d_llp_q(l, lp) = z_zero
347 END IF
348 END DO
349 END DO
350 DO l = 1, n_grid
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)
354 ELSE
355 d_lp_q(l, :) = z_zero
356 scale(l) = 1.0_dp
357 END IF
358 END DO
359
360 IF (bs_env%ri_rs%tikhonov <= 0.0_dp) THEN
361 CALL solve_unregularized_d_llp(d_llp_q, d_lp_q)
362 ELSE
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")
366 END IF
367
368 DO l = 1, n_grid
369 z_lp_q(l, :, iq_store) = d_lp_q(l, :)/scale(l)
370 END DO
371 IF (bs_env%para_env%mepos == 0 .AND. n_local > 1) THEN
372 n_done = n_done + 1_int_8
373 CALL write_ri_rs_progress(bs_env%unit_nr, 'Z_lP(q) q-point', &
374 n_done, n_local, next_progress, progress_start)
375 END IF
376 END DO
377 IF (distributed) THEN
378 cpassert(iq_local == n_q_local)
379 END IF
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)
382
383 CALL timestop(handle)
384
385 END SUBROUTINE solve_for_z_lp_q
386
387! **************************************************************************************************
388!> \brief Solve a possibly rank-deficient Hermitian D matrix without Tikhonov regularization.
389!> Only eigenvalues indistinguishable from zero at matrix-roundoff precision
390!> are excluded from the Moore-Penrose inverse.
391!> \param D_llp_q ...
392!> \param rhs ...
393! **************************************************************************************************
394 SUBROUTINE solve_unregularized_d_llp(D_llp_q, rhs)
395 COMPLEX(KIND=dp), DIMENSION(:, :), INTENT(INOUT) :: d_llp_q, rhs
396
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(:)
401
402 ngrid = SIZE(d_llp_q, 1)
403 cpassert(ngrid > 0)
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')
413 END IF
414
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)
421 ELSE
422 projected_rhs(ieigenvalue, :) = z_zero
423 END IF
424 END DO
425 CALL zgemm('N', 'N', ngrid, SIZE(rhs, 2), ngrid, z_one, d_llp_q, ngrid, &
426 projected_rhs, ngrid, z_zero, rhs, ngrid)
427
428 END SUBROUTINE solve_unregularized_d_llp
429
430! **************************************************************************************************
431!> \brief Enumerate translations T that survive the R_cut^(lP) criterion for at least one (l,P).
432!> \param bs_env ...
433!> \param grid_points ...
434!> \param cells_R_d_lP ...
435! **************************************************************************************************
436 SUBROUTINE build_cells_r_for_d_lp_r(bs_env, grid_points, cells_R_d_lP)
437 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
438 REAL(kind=dp), DIMENSION(:, :), INTENT(IN) :: grid_points
439 INTEGER, ALLOCATABLE, INTENT(OUT) :: cells_r_d_lp(:, :)
440
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(:, :)
444 LOGICAL :: keep
445 REAL(kind=dp) :: center(3), cutoff, max_cutoff
446 REAL(kind=dp), ALLOCATABLE :: automatic_cutoffs(:)
447 TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
448
449 particle_set => bs_env%ri_rs%particle_set
450 CALL compute_d_lp_cutoffs(bs_env, automatic_cutoffs)
451 max_cutoff = maxval(automatic_cutoffs)
452 DO ix = 1, 3
453 IF (bs_env%periodic(ix) == 1) THEN
454 ! If |h*T + delta| <= R, then every fractional component obeys
455 ! |T_i| <= R*|(h^-1)_i| + |(h^-1 delta)_i|. Both the atom and the
456 ! grid point are mapped into the reference cell, so the last term
457 ! is smaller than one. This reciprocal-vector bound remains safe
458 ! for skewed cells, unlike a bound based on direct-vector lengths.
459 n_max(ix) = ceiling(max_cutoff*norm2(bs_env%ri_rs%cell%h_inv(ix, :))) + 1
460 ELSE
461 n_max(ix) = 0
462 END IF
463 END DO
464
465 n_work = product(2*n_max + 1)
466 ALLOCATE (cells_work(3, n_work))
467 n_ri = 0
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]
472 keep = .false.
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
479 keep = .true.
480 EXIT
481 END IF
482 END DO
483 IF (keep) EXIT
484 END DO
485 IF (.NOT. keep) cycle
486 n_ri = n_ri + 1
487 cells_work(:, n_ri) = cell_index
488 END DO
489 END DO
490 END DO
491
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):', &
497 max_cutoff*angstrom, ' Å'
498 END IF
499 DEALLOCATE (automatic_cutoffs, cells_work)
500
501 END SUBROUTINE build_cells_r_for_d_lp_r
502
503! **************************************************************************************************
504!> \brief Enumerate the translations A required by the AO-product D matrix D_ll'^A.
505!>
506!> Unlike the right-hand side d_lP^T, D_ll'^A is independent of R_cut^(lP). A translation can
507!> contribute only if it is the difference of two image cells with nonzero AO values on the
508!> reference-cell grid. Keeping a separate cell set prevents Eq. (10) screening from truncating
509!> the least-squares metric and avoids enumerating empty three-center image cells.
510!> \param bs_env ...
511!> \param grid_points Reference-cell RI-RS points.
512!> \param cells_R_D_llp ...
513!> \param active_cells Indices of cells with nonzero AO values.
514! **************************************************************************************************
515 SUBROUTINE build_cells_r_for_d_llp(bs_env, grid_points, cells_R_D_llp, active_cells)
516 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
517 REAL(dp), INTENT(IN) :: grid_points(:, :)
518 INTEGER, ALLOCATABLE, INTENT(OUT) :: cells_r_d_llp(:, :), active_cells(:)
519
520 INTEGER :: cell_index(3), i, i_r, i_r_prime, &
521 n_active, n_d_cells
522 INTEGER, ALLOCATABLE :: cells_work(:, :), nonzero_cells(:)
523 LOGICAL :: found
524 REAL(dp), ALLOCATABLE :: phi(:, :)
525
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))
528 n_active = 0
529 DO i_r = 1, SIZE(bs_env%index_to_cell_3c, 2)
530 CALL evaluate_mu_rl_r_values(bs_env, grid_points, i_r, phi)
531 IF (.NOT. any(abs(phi) > 0.0_dp)) cycle
532 n_active = n_active + 1
533 nonzero_cells(n_active) = i_r
534 END DO
535 cpassert(n_active > 0)
536 ALLOCATE (active_cells(n_active))
537 active_cells(:) = nonzero_cells(:n_active)
538
539 ALLOCATE (cells_work(3, n_active*n_active))
540 n_d_cells = 0
541 DO i_r = 1, 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))
545 found = .false.
546 DO i = 1, n_d_cells
547 IF (all(cell_index == cells_work(:, i))) THEN
548 found = .true.
549 EXIT
550 END IF
551 END DO
552 IF (.NOT. found) THEN
553 n_d_cells = n_d_cells + 1
554 cells_work(:, n_d_cells) = cell_index
555 END IF
556 END DO
557 END DO
558
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)
562
563 END SUBROUTINE build_cells_r_for_d_llp
564
565! **************************************************************************************************
566!> \brief Construct D_ll'^R = [Σ_{S-S'=R,μ} ϕ_μ^S(r_L) ϕ_μ^S'(r_L')]².
567!> The progress count enumerates pairs of active cell images (S,S'), not Gaussian pairs.
568!> \param bs_env ...
569!> \param grid_points Reference-cell RI-RS points.
570!> \param cells_R_D_llp ...
571!> \param active_cells Indices of cells with nonzero AO values.
572!> \param first_l ...
573!> \param last_l ...
574!> \param D_llp_R ...
575! **************************************************************************************************
576 SUBROUTINE compute_d_llp(bs_env, grid_points, cells_R_D_llp, active_cells, first_l, last_l, D_llp_R)
577 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
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(:, :, :)
582
583 CHARACTER(LEN=*), PARAMETER :: routinen = 'compute_D_llp'
584
585 INTEGER :: cell_a(3), handle, i_a, i_active, &
586 i_active_prime, i_r, i_r_prime, &
587 next_progress, nrows
588 INTEGER(KIND=int_8) :: n_done, n_total
589 LOGICAL :: found
590 REAL(kind=dp) :: progress_start
591 REAL(kind=dp), ALLOCATABLE :: phi_full(:, :), phi_local(:, :)
592
593 CALL timeset(routinen, handle)
594
595 progress_start = m_walltime()
596 next_progress = 1
597 n_done = 0_int_8
598 n_total = int(SIZE(active_cells), int_8)**2
599 ! Sum the AO images for each owned (l,l') rectangle before squaring.
600 d_llp_r = 0.0_dp
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)
606 CALL evaluate_mu_rl_r_values(bs_env, grid_points, i_r_prime, phi_full)
607 DO i_active = 1, SIZE(active_cells)
608 i_r = active_cells(i_active)
609 CALL evaluate_mu_rl_r_values(bs_env, grid_points(:, first_l:last_l), i_r, phi_local)
610 IF (n_done > 0) CALL write_ri_rs_progress(bs_env%unit_nr, "D_ll'^R Cell-image pairs", &
611 n_done, n_total, next_progress, progress_start)
612 n_done = n_done + 1
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), &
620 max(1, nrows))
621 END DO
622 END DO
623 END IF
624 IF (first_l <= last_l) CALL write_ri_rs_progress(bs_env%unit_nr, "D_ll'^R Cell-image pairs", &
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)
628 END DO
629 IF (ALLOCATED(phi_local)) DEALLOCATE (phi_local, phi_full)
630
631 CALL timestop(handle)
632
633 END SUBROUTINE compute_d_llp
634
635! **************************************************************************************************
636!> \brief Find a lattice triplet in an explicit image-cell list.
637!> \param cell ...
638!> \param index_to_cell ...
639!> \param found ...
640!> \param index ...
641! **************************************************************************************************
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
647
648 INTEGER :: i
649
650 found = .false.
651 index = 0
652 DO i = 1, SIZE(index_to_cell, 2)
653 IF (all(cell == index_to_cell(:, i))) THEN
654 found = .true.
655 index = i
656 RETURN
657 END IF
658 END DO
659
660 END SUBROUTINE find_cell
661
662! **************************************************************************************************
663!> \brief Fourier-transform d_lP(R) to d_lP(q) and gather l rows on each q owner.
664!> d_lP(q)=Σ_T exp(iqT)d_lP(T); two send buffers overlap transfer with the next q-point transform.
665!> Each rank holds all P and T for its l interval; each destination receives one complete q.
666!> \param bs_env ...
667!> \param cells_R_d_lP ...
668!> \param d_lP_R ...
669!> \param qpoints ...
670!> \param i_batch ...
671!> \param distributed ...
672!> \param d_lP_q ...
673!> \param iq ...
674! **************************************************************************************************
675 SUBROUTINE fourier_transform_d_lp_r_to_q(bs_env, cells_R_d_lP, d_lP_R, qpoints, i_batch, distributed, &
676 d_lP_q, iq)
677 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
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
685
686 CHARACTER(LEN=*), PARAMETER :: routinen = 'fourier_transform_d_lP_R_to_q'
687
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, &
692 q_column, q_offset
693 INTEGER, ALLOCATABLE :: q_dest(:), q_indices(:)
694 LOGICAL :: receiving, sending(2)
695 TYPE(mp_request_type) :: send_requests(2)
696 TYPE(mp_request_type), ALLOCATABLE :: recv_requests(:)
697
698 CALL timeset(routinen, handle)
699
700 n_ranks = bs_env%para_env%num_pe
701 i_rank = bs_env%para_env%mepos
702 q_offset = 0
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))
709 n_p = bs_env%n_RI
710 ALLOCATE (recv_requests(n_ranks), send_q(SIZE(d_lp_r, 1), n_p, 2))
711 d_lp_q = z_zero
712 n_recv = 0
713 receiving = iq <= SIZE(qpoints, 2)
714 IF (receiving) THEN
715 ! Packed rank blocks: offset=(first_l-1)*n_P, followed by local (l,P) order.
716 ! Receive into contiguous storage before assigning the noncontiguous output rows.
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
722 n_recv = n_recv + 1
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)
725 END DO
726 END IF
727 n_active_q = 0
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
735 END DO
736 sending = .false.
737 IF (SIZE(d_lp_r, 1) > 0 .AND. n_p > 0) THEN
738 ! Sums of up to n_sub q points at a time, bounded to 64 MiB unless one q is larger.
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)
754 ELSE
755 CALL bs_env%para_env%isend(send_q(:, :, i_slot), i_dest, send_requests(i_slot), tag=29571)
756 sending(i_slot) = .true.
757 END IF
758 END DO
759 END DO
760 DEALLOCATE (result_q)
761 END IF
762 DO i_slot = 1, 2
763 IF (sending(i_slot)) CALL mp_waitall(send_requests(i_slot:i_slot))
764 END DO
765 IF (n_recv > 0) CALL mp_waitall(recv_requests(:n_recv))
766 IF (receiving) THEN
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
771 DO i_p = 1, n_p
772 position = (first_l - 1)*n_p + (i_p - 1)*n_l
773 DO i_l = 1, n_l
774 d_lp_q(first_l + i_l - 1, i_p) = receive_q(position + i_l)
775 END DO
776 END DO
777 END DO
778 DEALLOCATE (receive_q)
779 END IF
780 DEALLOCATE (send_q, recv_requests)
781
782 CALL timestop(handle)
783
784 END SUBROUTINE fourier_transform_d_lp_r_to_q
785
786! **************************************************************************************************
787!> \brief Lattice sums X_q(:, q) = Σ_R exp(±2πi q·R) X_R(:, R) for several q points, as two real
788!> GEMMs against cosine and sine tables over row blocks of X_R on all OpenMP threads.
789!> \param n number of rows of X_R and X_q
790!> \param values_R X_R, one column per lattice vector
791!> \param cells lattice vectors of the columns of X_R
792!> \param qpoints q points in reciprocal lattice coordinates
793!> \param sign +1 or -1, the sign of the phase
794!> \param values_q X_q, one column per q point
795! **************************************************************************************************
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)), &
799 qpoints(:, :), sign
800 COMPLEX(KIND=dp), INTENT(OUT) :: values_q(n, size(qpoints, 2))
801
802 INTEGER, PARAMETER :: row_block = 4096
803
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(:, :)
808
809 n_r = SIZE(cells, 2)
810 n_q = SIZE(qpoints, 2)
811 IF (n_r == 0) THEN
812 values_q = z_zero
813 RETURN
814 END IF
815 ALLOCATE (phase_cos(n_r, n_q), phase_sin(n_r, n_q))
816 DO iq = 1, n_q
817 DO i_r = 1, n_r
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)
821 END DO
822 END DO
823 !$OMP PARALLEL DEFAULT(NONE) SHARED(n, n_q, n_R, values_R, values_q, phase_cos, phase_sin) &
824 !$OMP PRIVATE(first, nrows, work_cos, work_sin)
825 ALLOCATE (work_cos(row_block, n_q), work_sin(row_block, n_q))
826 !$OMP DO SCHEDULE(DYNAMIC)
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)
834 END DO
835 !$OMP END DO
836 DEALLOCATE (work_cos, work_sin)
837 !$OMP END PARALLEL
838
839 END SUBROUTINE lattice_sum_to_q
840
841END MODULE ri_rs_kp_fitting
static GRID_HOST_DEVICE int modulo(int a, int m)
Equivalent of Fortran's MODULO, which always return a positive number. https://gcc....
static void dgemm(const char transa, const char transb, const int m, const int n, const int k, const double alpha, const double *a, const int lda, const double *b, const int ldb, const double beta, double *c, const int ldc)
Convenient wrapper to hide Fortran nature of dgemm_, swapping a and b.
Handles all functions related to the CELL.
Definition cell_types.F:15
collects all constants needed in input so that they can be used without circular dependencies
integer, parameter, public do_potential_short
Defines the basic variable types.
Definition kinds.F:23
integer, parameter, public int_8
Definition kinds.F:54
integer, parameter, public dp
Definition kinds.F:34
Machine interface based on Fortran 2003 and POSIX.
Definition machine.F:17
real(kind=dp) function, public m_walltime()
returns time from a real-time clock, protected against rolling early/easily
Definition machine.F:144
Definition of mathematical constants and functions.
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:
Definition physcon.F:68
real(kind=dp), parameter, public angstrom
Definition physcon.F:144
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.
Definition ri_rs_utils.F:11
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.
Definition cell_types.F:60