(git:27c966b)
Loading...
Searching...
No Matches
ri_rs_kp_integrals.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 Numerical integrals d_lP(R) for periodic RI-RS fitting.
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
16 USE ieee_arithmetic, ONLY: ieee_is_finite
18 USE kinds, ONLY: dp,&
19 dp_size,&
20 int_8,&
22 USE machine, ONLY: m_walltime
23 USE mathconstants, ONLY: pi,&
24 twopi
25 USE mathlib, ONLY: diamat_all,&
27 USE message_passing, ONLY: mp_comm_type,&
30 USE omp_lib, ONLY: omp_get_max_threads,&
31 omp_in_parallel
32 USE orbital_pointers, ONLY: indco,&
33 ncoset,&
34 nso
36 USE physcon, ONLY: angstrom
38 USE ri_rs_kp_types, ONLY: ao_image_type,&
44#include "./base/base_uses.f90"
45
46 IMPLICIT NONE
47 PRIVATE
48
49 CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'ri_rs_kp_integrals'
50
51 ! Uniform d_lP integration grid: spacing prefactor calibrated on the AO exponents
52 ! (including the matched reference RI basis) and safety factor of the Gaussian padding.
53 REAL(dp), PARAMETER :: spacing_prefactor = 0.4_dp*sqrt(2.693604_dp*log(1.0e8_dp)), &
54 padding_safety = 1.1_dp
55 ! Largest Gaussian phase error alpha*|delta r**2| accepted for a tabulated radial moment.
56 REAL(dp), PARAMETER :: eps_mesh_phase = 1.0e-8_dp
57 ! Tolerance for integer lattice-metric ratios that allow tabulated radial moments.
58 REAL(dp), PARAMETER :: eps_integer = 1.0e-10_dp
59 ! Radial intervals of the tabulated bounds of the erfc RI potentials [bohr] and their number.
60 REAL(dp), PARAMETER :: short_bound_spacing = 0.125_dp
61 INTEGER, PARAMETER :: n_short_bounds = 1024
62 ! Largest edge of the quadrature blocks of integration points.
63 INTEGER, PARAMETER :: quadrature_block_edge = 8, quadrature_block_size = quadrature_block_edge**3
64 ! Relative roundoff inflation of the tabulated potential bounds.
65 REAL(dp), PARAMETER :: eps_bound_roundoff = 1.0e-10_dp
66 ! Short-range radial grid in x = a*r**2: uniform spacing up to short_grid_switch, then
67 ! geometric with ratio short_grid_ratio up to short_grid_xmax.
68 REAL(dp), PARAMETER :: short_grid_spacing = 0.02_dp, short_grid_switch = 2.0_dp, &
69 short_grid_ratio = 1.01_dp, short_grid_xmax = 512.0_dp
70 INTEGER, PARAMETER :: n_short_uniform = nint(short_grid_switch/short_grid_spacing)
71 ! Below this b = omega**2/(a+omega**2), far-field radial moments use the upward recurrence.
72 REAL(dp), PARAMETER :: recurrence_b_max = 0.9_dp
73 ! Radius [bohr] covered by the uniform-mesh radial moment tables.
74 REAL(dp), PARAMETER :: mesh_table_radius = 40.0_dp
75 ! Memory estimates: bytes per real, per MiB and per GB (mp_mem_avail_per_rank_GB).
76 REAL(dp), PARAMETER :: real_bytes = real(dp_size, dp), bytes_per_mib = 1024.0_dp**2, &
77 bytes_per_gb = 1.0e9_dp
78 ! Fixed workspaces: library calls, OpenMP threads, MPI message batches, radial tables of all atoms.
79 REAL(dp), PARAMETER :: library_workspace_bytes = 96.0_dp*bytes_per_mib, &
80 thread_workspace_bytes = 128.0_dp*bytes_per_mib, &
81 message_batch_bytes = 32.0_dp*bytes_per_mib, &
82 radial_table_budget = 256.0_dp*bytes_per_mib
83 ! Safety factor on the estimated d_lP workspaces and the shares of the available memory
84 ! for the cached RI potentials and AO values of the quadrature blocks.
85 REAL(dp), PARAMETER :: workspace_safety = 1.3_dp, ri_cache_share = 0.4_dp, ao_cache_share = 0.1_dp
86
87 PUBLIC :: compute_d_lp_r, &
91
92CONTAINS
93
94! **************************************************************************************************
95!> \brief Allocate d_lP(R), prepare its integration grid, and evaluate the integrals.
96!> \param bs_env ...
97!> \param grid_points ...
98!> \param cells_R_d_lP Retained integer cell vectors R, indexed (3,R).
99!> \param d_lP_R Numerical integrals indexed by local point l, RI function P and cell R.
100!> \param qpoints ...
101!> \param charge_lq ...
102! **************************************************************************************************
103 SUBROUTINE compute_d_lp_r(bs_env, grid_points, cells_R_d_lP, d_lP_R, &
104 qpoints, charge_lq)
105 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
106 REAL(dp), INTENT(IN) :: grid_points(:, :)
107 INTEGER, INTENT(IN) :: cells_r_d_lp(:, :)
108 REAL(dp), ALLOCATABLE, INTENT(OUT) :: d_lp_r(:, :, :)
109 REAL(dp), INTENT(IN) :: qpoints(:, :)
110 COMPLEX(dp), ALLOCATABLE, INTENT(OUT) :: charge_lq(:, :)
111
112 CHARACTER(LEN=*), PARAMETER :: routinen = 'compute_d_lP_R'
113
114 INTEGER :: first_l, handle, last_l
115 TYPE(uniform_settings_type) :: settings
116
117 IF (bs_env%unit_nr > 0) THEN
118 WRITE (bs_env%unit_nr, '(T2,A,T72,A9)') 'd_lP method', 'NUMERICAL'
119 END IF
120 CALL get_local_l_range(bs_env%ri_rs%n_grid_points, bs_env%para_env%num_pe, &
121 bs_env%para_env%mepos, first_l, last_l)
122 ALLOCATE (d_lp_r(last_l - first_l + 1, bs_env%n_RI, SIZE(cells_r_d_lp, 2)))
123
124 CALL timeset(routinen, handle)
125
126 CALL prepare_d_lp_integration_grid(bs_env, grid_points, settings)
127 ALLOCATE (charge_lq(bs_env%ri_rs%n_grid_points, SIZE(qpoints, 2)))
128 CALL integrate_d_lp_r(bs_env, grid_points, cells_r_d_lp, settings, d_lp_r, first_l, &
129 qpoints, charge_lq)
130
131 IF (.NOT. all(ieee_is_finite(d_lp_r))) cpabort("Nonfinite DLP cell value")
132
133 CALL timestop(handle)
134
135 END SUBROUTINE compute_d_lp_r
136
137! **************************************************************************************************
138!> \brief Automatic l/P cutoff from the overlap of the diffuse AO-field square and RI potential.
139!>
140!> Outside the metric cutoff sphere, conservative Gaussian envelopes are
141!> f_l(r)^2 ~ exp[-alpha_AO |r-r_l|^2] and
142!> v_PT(r) ~ exp[-alpha_RI (|r-R_PT|-r_c)^2/2]. Maximizing their product over r gives
143!> exp[-gamma (|r_l-R_PT|-r_c)^2], with 1/gamma=1/alpha_AO+2/alpha_RI.
144!> Both methods use the same EPS_FILTER-dependent lattice domain.
145!> \param bs_env ...
146!> \param cutoffs ...
147! **************************************************************************************************
148 SUBROUTINE compute_d_lp_cutoffs(bs_env, cutoffs)
149 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
150 REAL(dp), ALLOCATABLE, INTENT(OUT) :: cutoffs(:)
151
152 CHARACTER(LEN=*), PARAMETER :: routinen = 'compute_d_lP_cutoffs'
153
154 INTEGER :: handle, iatom, ikind
155 REAL(dp) :: accuracy, ao_max, ao_min, ri_max, ri_min
156 TYPE(gto_basis_set_type), POINTER :: basis
157 TYPE(particle_type), POINTER :: particles(:)
158
159 CALL timeset(routinen, handle)
160
161 particles => bs_env%ri_rs%particle_set
162 accuracy = d_lp_integration_accuracy(bs_env)
163 ALLOCATE (cutoffs(bs_env%n_atom))
164 DO iatom = 1, bs_env%n_atom
165 ikind = particles(iatom)%atomic_kind%kind_number
166 basis => bs_env%basis_set_AO(ikind)%gto_basis_set
167 CALL basis_exponent_range(basis, ao_min, ao_max)
168 basis => bs_env%basis_set_RI(ikind)%gto_basis_set
169 CALL basis_exponent_range(basis, ri_min, ri_max)
170 cutoffs(iatom) = max(0.0_dp, bs_env%ri_metric%cutoff_radius) + &
171 sqrt(log(1.0_dp/accuracy)*(1.0_dp/ao_min + 2.0_dp/ri_min))
172 END DO
173
174 CALL timestop(handle)
175
176 END SUBROUTINE compute_d_lp_cutoffs
177
178! **************************************************************************************************
179!> \brief Distribute individual l rows evenly across MPI ranks; empty ranges are valid.
180!> \param n_grid Number of RI-RS grid points.
181!> \param n_ranks Number of MPI ranks.
182!> \param i_rank MPI rank.
183!> \param first_l First locally owned grid row.
184!> \param last_l Last locally owned grid row.
185! **************************************************************************************************
186 SUBROUTINE get_local_l_range(n_grid, n_ranks, i_rank, first_l, last_l)
187 INTEGER, INTENT(IN) :: n_grid, n_ranks, i_rank
188 INTEGER, INTENT(OUT) :: first_l, last_l
189
190 cpassert(i_rank >= 0 .AND. i_rank < n_ranks)
191 first_l = int(int(n_grid, int_8)*int(i_rank, int_8)/int(n_ranks, int_8)) + 1
192 last_l = int(int(n_grid, int_8)*int(i_rank + 1, int_8)/int(n_ranks, int_8))
193
194 END SUBROUTINE get_local_l_range
195
196! **************************************************************************************************
197!> \brief Print one rank-local RI-RS work milestone without synchronizing MPI ranks.
198!> Percentages count completed local units; time is the elapsed wall time since the phase began.
199!> \param unit_nr ...
200!> \param phase ...
201!> \param n_done ...
202!> \param n_total ...
203!> \param next_progress ...
204!> \param phase_start ...
205! **************************************************************************************************
206 SUBROUTINE write_ri_rs_progress(unit_nr, phase, n_done, n_total, next_progress, phase_start)
207 INTEGER, INTENT(IN) :: unit_nr
208 CHARACTER(LEN=*), INTENT(IN) :: phase
209 INTEGER(KIND=int_8), INTENT(IN) :: n_done, n_total
210 INTEGER, INTENT(INOUT) :: next_progress
211 REAL(kind=dp), INTENT(IN) :: phase_start
212
213 REAL(kind=dp), PARAMETER :: milestones(9) = [0.1_dp, 0.3_dp, 1.0_dp, 3.0_dp, 10.0_dp, 30.0_dp&
214 , 60.0_dp, 90.0_dp, 100.0_dp]
215
216 CHARACTER(LEN=100) :: progress_line
217 CHARACTER(LEN=20) :: count_text
218 CHARACTER(LEN=32) :: phase_text
219 CHARACTER(LEN=64) :: progress_format
220 CHARACTER(LEN=8) :: time_text
221 INTEGER :: count_width, previous_progress
222 REAL(kind=dp) :: percent_done, phase_time
223
224 IF (unit_nr <= 0 .OR. n_total <= 0) RETURN
225 percent_done = 100.0_dp*real(n_done, dp)/real(n_total, dp)
226 previous_progress = next_progress
227 DO WHILE (next_progress <= SIZE(milestones))
228 IF (percent_done < milestones(next_progress)) EXIT
229 next_progress = next_progress + 1
230 END DO
231 IF (next_progress == previous_progress) RETURN
232 cpassert(len_trim(phase) <= 32)
233 WRITE (count_text, '(I0)') n_total
234 count_width = len_trim(count_text)
235 WRITE (progress_format, '(A,I0,A,I0,A)') &
236 '(A32,1X,I', count_width, ',A,I', count_width, ',1X,F5.1,A)'
237 phase_text = phase
238 WRITE (progress_line, progress_format) &
239 phase_text, n_done, ' / ', n_total, percent_done, ' %'
240 phase_time = m_walltime() - phase_start
241 WRITE (time_text, '(F8.1)') phase_time
242 IF (index(time_text, '*') > 0) WRITE (time_text, '(ES8.1E3)') phase_time
243 IF (len_trim(progress_line) > 56) THEN
244 WRITE (unit_nr, '(T2,A)') trim(phase)
245 WRITE (unit_nr, '(T2,A)') trim(adjustl(progress_line(34:)))
246 progress_line = ''
247 END IF
248 WRITE (unit_nr, '(T2,A,T60,A,T71,A,A)') trim(progress_line), 'Total time:', time_text, ' s'
249 FLUSH (unit_nr)
250
251 END SUBROUTINE write_ri_rs_progress
252
253! **************************************************************************************************
254!> \brief Choose the smallest fitting divisor of MPI ranks with 30 percent reserve.
255!> A memory group has ngroups ranks and jointly holds all T. Corresponding T slices
256!> across memory groups divide g. Equal integration ranks
257!> share their uniquely evaluated fields across T groups. The caller's P result is already live.
258!> Use the existing node-memory estimate to bound temporary workspaces.
259!> Budget the maximum simultaneous integration, reduction and redistribution storage.
260!> The final redistribution needs an extra receive buffer as large as the live result.
261!> Selection masks remain live throughout; block masks have at most nl rows.
262!> \param para_env ...
263!> \param nl ...
264!> \param bs_env ...
265!> \param nt ...
266!> \param nresult ...
267!> \param integration_comm ...
268!> \param field_comm ...
269!> \param ngroups ...
270! **************************************************************************************************
271 SUBROUTINE prepare_d_lp_mpi_groups(para_env, nl, bs_env, nt, nresult, integration_comm, field_comm, ngroups)
272 TYPE(mp_para_env_type), POINTER :: para_env
273 INTEGER, INTENT(IN) :: nl
274 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
275 INTEGER, INTENT(IN) :: nt
276 INTEGER(int_8), INTENT(IN) :: nresult
277 TYPE(mp_comm_type), INTENT(OUT) :: integration_comm, field_comm
278 INTEGER, INTENT(OUT) :: ngroups
279
280 INTEGER :: igroup, integration_rank, maxnp, &
281 nlocal_t, np
282 REAL(dp) :: available_gb, cell_bytes, contraction_bytes, cyclic_bytes, local_bytes, &
283 reduction_bytes, required_bytes, result_bytes, selection_bytes, workspace_bytes
284
285 np = bs_env%n_RI
286 CALL mp_mem_avail_per_rank_gb(para_env, available_gb)
287 result_bytes = real_bytes*real(nresult, dp)
288 CALL para_env%max(result_bytes)
289 cell_bytes = real_bytes*real(nl, dp)*real(np, dp)
290 cyclic_bytes = cell_bytes*real((nt + para_env%num_pe - 1)/para_env%num_pe, dp)
291 maxnp = maxval(bs_env%i_RI_end_from_atom - bs_env%i_RI_start_from_atom + 1)
292 contraction_bytes = real_bytes*(real(nl, dp)*real(maxnp + quadrature_block_size, dp) + &
293 REAL(quadrature_block_size, dp)*REAL(maxnp, dp))
294 workspace_bytes = library_workspace_bytes + &
295 5.0_dp*real_bytes*real(nl + 3, dp)*real(quadrature_block_size, dp) + &
296 max(thread_workspace_bytes, contraction_bytes)
297 DO ngroups = 1, para_env%num_pe
298 IF (modulo(para_env%num_pe, ngroups) /= 0) cycle
299 nlocal_t = (nt + ngroups - 1)/ngroups
300 ! The caller allocated d_lP_R before this call. Its bytes are already
301 ! reflected in the measured free memory and must not be charged twice.
302 local_bytes = cell_bytes*real(nlocal_t, dp)
303 selection_bytes = 2.0_dp*real(storage_size(.true.)/8, dp)* &
304 REAL(nl, dp)*REAL(bs_env%n_atom, dp)*REAL(nlocal_t, dp)
305 selection_bytes = selection_bytes + 3.0_dp*real(int_size, dp)*real(nlocal_t, dp)
306 reduction_bytes = max(message_batch_bytes, cell_bytes)
307 required_bytes = workspace_safety*(selection_bytes + &
308 max(local_bytes + workspace_bytes, &
309 local_bytes + cyclic_bytes + reduction_bytes, &
310 2.0_dp*cyclic_bytes + result_bytes + reduction_bytes))
311 IF (required_bytes <= available_gb*bytes_per_gb) EXIT
312 END DO
313 IF (ngroups > para_env%num_pe) THEN
314 ngroups = para_env%num_pe
315 cpwarn('d_lP workspaces exceed memory with all MPI ranks')
316 END IF
317 igroup = modulo(para_env%mepos, ngroups)
318 integration_rank = para_env%mepos/ngroups
319 CALL integration_comm%from_split(para_env, igroup, key=integration_rank)
320 CALL field_comm%from_split(para_env, integration_rank, key=igroup)
321 IF (bs_env%unit_nr > 0) THEN
322 WRITE (bs_env%unit_nr, '(T2,A,T66,F12.1,A)') 'd_lP PSS-aware rank headroom:', available_gb, ' GB'
323 WRITE (bs_env%unit_nr, '(T2,A,T61,I20)') 'd_lP memory group size:', ngroups
324 WRITE (bs_env%unit_nr, '(T2,A,T61,I20)') 'd_lP independent integration groups:', integration_comm%num_pe
325 WRITE (bs_env%unit_nr, '(T2,A,T66,F12.1,A)') 'd_lP existing output storage:', result_bytes*1.0e-9_dp, ' GB'
326 WRITE (bs_env%unit_nr, '(T2,A,T66,F12.1,A)') 'd_lP additional memory budget:', required_bytes*1.0e-9_dp, ' GB'
327 FLUSH (bs_env%unit_nr)
328 END IF
329
330 END SUBROUTINE prepare_d_lp_mpi_groups
331
332! **************************************************************************************************
333!> \brief Sum Eq. (2) directly onto ranks owning cyclic T cells, T=r+1+j*N.
334!> The first two indices are (L,P); contiguous T blocks are pointer views, with no packing copy.
335!> Reduce-scatter retains only each rank's output cells instead of replicating the full sum.
336!> \param comm ...
337!> \param partial ...
338!> \param cells ...
339! **************************************************************************************************
340 SUBROUTINE sum_d_lp_r_over_mpi_ranks(comm, partial, cells)
341 TYPE(mp_comm_type), INTENT(IN) :: comm
342 REAL(dp), CONTIGUOUS, INTENT(IN), TARGET :: partial(:, :, :)
343 REAL(dp), ALLOCATABLE, INTENT(OUT), TARGET :: cells(:, :, :)
344
345 CHARACTER(LEN=*), PARAMETER :: routinen = 'sum_d_lP_R_over_mpi_ranks'
346
347 INTEGER :: block_cells, first_rank, first_t, handle, icycle, last_rank, last_t, nl, nlocal, &
348 np, nranks, nt, rank, round_first, round_last
349 INTEGER(int_8) :: iround, plane_count
350 REAL(dp), CONTIGUOUS, POINTER :: receive_block(:), send_block(:, :)
351 REAL(dp), TARGET :: empty(0)
352 INTEGER :: recv_counts(comm%num_pe)
353
354 CALL timeset(routinen, handle)
355
356 nl = SIZE(partial, 1)
357 np = SIZE(partial, 2)
358 nt = SIZE(partial, 3)
359 nranks = comm%num_pe
360 rank = comm%mepos
361 nlocal = 0
362 IF (nt > rank) nlocal = 1 + (nt - 1 - rank)/nranks
363 ALLOCATE (cells(nl, np, nlocal))
364 IF (nl == 0 .OR. np == 0 .OR. nt == 0) THEN
365 CALL timestop(handle)
366 RETURN
367 END IF
368 IF (nranks == 1) THEN
369 cells(:, :, :) = partial(:, :, :)
370 CALL timestop(handle)
371 RETURN
372 END IF
373 plane_count = int(nl, int_8)*int(np, int_8)
374 IF (plane_count > huge(0)) THEN
375 cpabort('DLP integration block exceeds the MPI count range')
376 END IF
377 block_cells = int(max(1_int_8, int(message_batch_bytes/real_bytes, int_8)/plane_count))
378 block_cells = min(block_cells, int(int(huge(0), int_8)/plane_count))
379 icycle = 0
380 DO iround = 1_int_8, int(nt, int_8), int(nranks, int_8)
381 icycle = icycle + 1
382 round_first = int(iround)
383 round_last = int(min(int(nt, int_8), iround + int(nranks, int_8) - 1_int_8))
384 first_t = round_first
385 DO WHILE (first_t <= round_last)
386 last_t = int(min(int(round_last, int_8), int(first_t, int_8) + int(block_cells, int_8) - 1_int_8))
387 first_rank = first_t - round_first
388 last_rank = last_t - round_first
389 recv_counts = 0
390 recv_counts(first_rank + 1:last_rank + 1) = int(plane_count)
391 send_block(1:int(plane_count), 1:last_t - first_t + 1) => partial(:, :, first_t:last_t)
392 receive_block => empty
393 IF (rank >= first_rank .AND. rank <= last_rank) THEN
394 receive_block(1:int(plane_count)) => cells(:, :, icycle)
395 END IF
396 CALL comm%sum_scatter(send_block, receive_block, recv_counts)
397 IF (last_t == round_last) EXIT
398 first_t = last_t + 1
399 END DO
400 END DO
401
402 CALL timestop(handle)
403
404 END SUBROUTINE sum_d_lp_r_over_mpi_ranks
405
406! **************************************************************************************************
407!> \brief Redistribute Eq. (2) from cyclic T cells to disjoint contiguous l rows.
408!> Rank r initially owns T=r+1+j*N. The caller owns first_l:first_l+size(d_lP_R,1)-1.
409!> Only the local T slice and local l slice are materialized, never a global DLP replica.
410!> \param para_env ...
411!> \param cell_values ...
412!> \param first_l ...
413!> \param d_lP_R Numerical integrals indexed by local point l, RI function P and cell R.
414! **************************************************************************************************
415 SUBROUTINE distribute_d_lp_r_by_l(para_env, cell_values, first_l, d_lP_R)
416 TYPE(mp_para_env_type), POINTER :: para_env
417 REAL(dp), INTENT(IN) :: cell_values(:, :, :)
418 INTEGER, INTENT(IN) :: first_l
419 REAL(dp), INTENT(OUT) :: d_lp_r(:, :, :)
420
421 CHARACTER(LEN=*), PARAMETER :: routinen = 'distribute_d_lP_R_by_l'
422
423 INTEGER :: handle, il, ip, irank, it, local_it, &
424 nlocal_l, nlocal_t, np, nranks, &
425 position
426 INTEGER(int_8) :: count_wide
427 INTEGER, ALLOCATABLE :: cell_counts(:), first_rows(:), recv_counts(:), recv_offsets(:), &
428 row_counts(:), send_counts(:), send_offsets(:)
429 REAL(dp), ALLOCATABLE :: receive_values(:), send_values(:)
430
431 CALL timeset(routinen, handle)
432
433 nranks = para_env%num_pe
434 np = SIZE(d_lp_r, 2)
435 nlocal_l = SIZE(d_lp_r, 1)
436 nlocal_t = SIZE(cell_values, 3)
437 ALLOCATE (first_rows(nranks), row_counts(nranks), cell_counts(nranks), &
438 send_counts(nranks), send_offsets(nranks), recv_counts(nranks), recv_offsets(nranks))
439 CALL para_env%allgather(first_l, first_rows)
440 CALL para_env%allgather(nlocal_l, row_counts)
441 CALL para_env%allgather(nlocal_t, cell_counts)
442 DO irank = 1, nranks
443 count_wide = int(np, int_8)*int(row_counts(irank), int_8)*int(nlocal_t, int_8)
444 cpassert(count_wide <= huge(0))
445 count_wide = int(np, int_8)*int(nlocal_l, int_8)*int(cell_counts(irank), int_8)
446 cpassert(count_wide <= huge(0))
447 send_counts(irank) = np*row_counts(irank)*nlocal_t
448 recv_counts(irank) = np*nlocal_l*cell_counts(irank)
449 END DO
450 cpassert(sum(int(send_counts, int_8)) <= huge(0))
451 cpassert(sum(int(recv_counts, int_8)) <= huge(0))
452 position = 0
453 DO irank = 1, nranks
454 send_offsets(irank) = position
455 position = position + send_counts(irank)
456 END DO
457 ALLOCATE (send_values(position))
458 position = 0
459 DO irank = 1, nranks
460 recv_offsets(irank) = position
461 position = position + recv_counts(irank)
462 END DO
463 ALLOCATE (receive_values(position))
464 DO irank = 1, nranks
465 position = send_offsets(irank)
466 DO local_it = 1, nlocal_t
467 DO ip = 1, np
468 DO il = first_rows(irank), first_rows(irank) + row_counts(irank) - 1
469 position = position + 1
470 send_values(position) = cell_values(il, ip, local_it)
471 END DO
472 END DO
473 END DO
474 END DO
475 CALL para_env%alltoall(send_values, send_counts, send_offsets, &
476 receive_values, recv_counts, recv_offsets)
477 DO irank = 1, nranks
478 position = recv_offsets(irank)
479 DO local_it = 1, cell_counts(irank)
480 it = irank + (local_it - 1)*nranks
481 DO ip = 1, np
482 DO il = 1, nlocal_l
483 position = position + 1
484 d_lp_r(il, ip, it) = receive_values(position)
485 END DO
486 END DO
487 END DO
488 END DO
489
490 CALL timestop(handle)
491
492 END SUBROUTINE distribute_d_lp_r_by_l
493
494! **************************************************************************************************
495!> \brief Accumulate Q_lq += w SUM_g f_l(r_g)^2 exp(i q.r_g) with real BLAS products.
496!> The real field is the existing f_l(r_g)^2 on one quadrature integration-point block. Cosine and sine
497!> columns share a DGEMM; OpenMP partitions disjoint l rows without a reduction.
498!> The grid, phase convention, weights, and retained quadrature points are unchanged.
499!> \param points ...
500!> \param field ...
501!> \param q_cart ...
502!> \param weight ...
503!> \param charge_lq ...
504! **************************************************************************************************
505 SUBROUTINE accumulate_fourier_charge(points, field, q_cart, weight, charge_lq)
506 REAL(dp), INTENT(IN) :: points(:, :)
507 REAL(dp), CONTIGUOUS, INTENT(IN) :: field(:, :)
508 REAL(dp), INTENT(IN) :: q_cart(:, :), weight
509 COMPLEX(dp), INTENT(INOUT) :: charge_lq(:, :)
510
511 CHARACTER(LEN=*), PARAMETER :: routinen = 'accumulate_fourier_charge'
512 INTEGER, PARAMETER :: row_block = 64
513
514 COMPLEX(dp) :: phase
515 INTEGER :: first_l, handle, ig, il, iq, nblock, ng, &
516 nl, nq
517 REAL(dp), ALLOCATABLE :: charge_parts(:, :), phase_parts(:, :)
518
519 CALL timeset(routinen, handle)
520
521 ng = SIZE(points, 2)
522 nl = SIZE(field, 2)
523 nq = SIZE(q_cart, 2)
524 IF (ng == 0 .OR. nl == 0 .OR. nq == 0) THEN
525 CALL timestop(handle)
526 RETURN
527 END IF
528 ALLOCATE (phase_parts(ng, 2*nq))
529 DO iq = 1, nq
530 DO ig = 1, ng
531 phase = exp(cmplx(0.0_dp, sum(q_cart(:, iq)*points(:, ig)), dp))
532 phase_parts(ig, iq) = real(phase, dp)
533 phase_parts(ig, nq + iq) = aimag(phase)
534 END DO
535 END DO
536 !$OMP PARALLEL DEFAULT(NONE) IF(nl > row_block) &
537 !$OMP SHARED(field, phase_parts, charge_lq, ng, nl, nq, weight) &
538 !$OMP PRIVATE(charge_parts, first_l, il, iq, nblock)
539 ALLOCATE (charge_parts(row_block, 2*nq))
540 !$OMP DO SCHEDULE(STATIC)
541 DO first_l = 1, nl, row_block
542 nblock = min(row_block, nl - first_l + 1)
543 CALL dgemm('T', 'N', nblock, 2*nq, ng, weight, &
544 field(:, first_l:first_l + nblock - 1), SIZE(field, 1), &
545 phase_parts, ng, 0.0_dp, charge_parts, row_block)
546 DO iq = 1, nq
547 DO il = 1, nblock
548 charge_lq(first_l + il - 1, iq) = charge_lq(first_l + il - 1, iq) + &
549 cmplx(charge_parts(il, iq), charge_parts(il, nq + iq), dp)
550 END DO
551 END DO
552 END DO
553 !$OMP END DO
554 DEALLOCATE (charge_parts)
555 !$OMP END PARALLEL
556 DEALLOCATE (phase_parts)
557
558 CALL timestop(handle)
559
560 END SUBROUTINE accumulate_fourier_charge
561
562! **************************************************************************************************
563!> \brief Validate the operator and define the integration box and mesh in Eq. (3).
564!> \param bs_env ...
565!> \param grid_points ...
566!> \param settings ...
567! **************************************************************************************************
568 SUBROUTINE prepare_d_lp_integration_grid(bs_env, grid_points, settings)
569 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
570 REAL(dp), INTENT(IN) :: grid_points(:, :)
571 TYPE(uniform_settings_type), INTENT(OUT) :: settings
572
573 INTEGER(int_8) :: npoints
574 REAL(dp) :: exponent_max, exponent_min
575
576 settings%accuracy = d_lp_integration_accuracy(bs_env)
577 ! The full-k RI-RS path always uses the erfc(omega r)/r RI metric with omega > 0.
578 IF (bs_env%ri_metric%potential_type /= do_potential_short .OR. &
579 .NOT. (bs_env%ri_metric%omega > 0.0_dp)) THEN
580 cpabort('Uniform DLP requires the short-range erfc RI metric with positive omega')
581 END IF
582 IF (all(bs_env%ri_rs%cell%perd == 0)) cpabort('Uniform DLP requires a periodic cell')
583 CALL ao_exponent_range(bs_env, exponent_min, exponent_max)
584 settings%l_block_edge = 1.0_dp/angstrom
585 settings%spacing = spacing_prefactor/ &
586 sqrt(exponent_max*log(1.0_dp/settings%accuracy))
587 settings%padding = padding_safety*sqrt(log(1.0_dp/settings%accuracy)/exponent_min)
588 CALL build_d_lp_integration_grid(bs_env, grid_points, settings)
589 npoints = product(int(settings%counts, int_8))
590 IF (bs_env%unit_nr > 0) THEN
591 WRITE (bs_env%unit_nr, '(T2,A,T72,ES9.1E2)') 'd_lP uniform quadrature points', real(npoints, dp)
592 WRITE (bs_env%unit_nr, '(T2,A,T72,ES9.1)') 'd_lP integration threshold', settings%accuracy
593 WRITE (bs_env%unit_nr, '(T2,A,T69,F12.3)') 'd_lP largest AO exponent [bohr^-2]', exponent_max
594 WRITE (bs_env%unit_nr, '(T2,A,T69,F12.3)') 'd_lP uniform spacing [bohr]', settings%spacing
595 END IF
596
597 END SUBROUTINE prepare_d_lp_integration_grid
598
599! **************************************************************************************************
600!> \brief Build Eq. (3) on a cell-commensurate lattice without coarsening its largest spacing.
601!> The integration parallelepiped contains the original padded Cartesian box.
602!> \param bs_env Band-structure environment with RI-RS cell and atoms.
603!> \param grid_points ...
604!> \param settings ...
605! **************************************************************************************************
606 SUBROUTINE build_d_lp_integration_grid(bs_env, grid_points, settings)
607 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
608 REAL(dp), INTENT(IN) :: grid_points(:, :)
609 TYPE(uniform_settings_type), INTENT(INOUT) :: settings
610
611 INTEGER :: idir, subdivisions(3)
612 REAL(dp) :: box_center(3), box_extent(3), &
613 eigenvalues(3), inverse_step(3, 3), &
614 metric(3, 3), scale, upper(3)
615 TYPE(cell_type), POINTER :: cell
616 TYPE(particle_type), POINTER :: particles(:)
617
618 cell => bs_env%ri_rs%cell
619 particles => bs_env%ri_rs%particle_set
620 settings%origin = pbc(particles(1)%r, cell)
621 DO idir = 1, 3
622 subdivisions(idir) = max(1, ceiling(norm2(cell%hmat(:, idir))/settings%spacing))
623 settings%step(:, idir) = cell%hmat(:, idir)/real(subdivisions(idir), dp)
624 END DO
625 metric = matmul(transpose(settings%step), settings%step)
626 CALL diamat_all(metric, eigenvalues, dac=.true.)
627 scale = max(1.0_dp, sqrt(maxval(eigenvalues))/settings%spacing)
628 subdivisions = ceiling(scale*real(subdivisions, dp))
629 settings%subdivisions = subdivisions
630 DO idir = 1, 3
631 settings%step(:, idir) = cell%hmat(:, idir)/real(subdivisions(idir), dp)
632 inverse_step(idir, :) = real(subdivisions(idir), dp)*cell%h_inv(idir, :)
633 END DO
634 settings%weight = abs(cell%deth)/product(real(subdivisions, dp))
635 box_center = 0.5_dp*(minval(grid_points, dim=2) + maxval(grid_points, dim=2)) - settings%origin
636 box_extent = 0.5_dp*(maxval(grid_points, dim=2) - minval(grid_points, dim=2)) + settings%padding
637 settings%lower = real(ceiling(matmul(inverse_step, box_center) - &
638 matmul(abs(inverse_step), box_extent)), dp)
639 upper = real(floor(matmul(inverse_step, box_center) + &
640 matmul(abs(inverse_step), box_extent)), dp)
641 settings%counts = int(upper - settings%lower) + 1
642 cpassert(all(settings%counts > 0))
643
644 END SUBROUTINE build_d_lp_integration_grid
645
646! **************************************************************************************************
647!> \brief Partition the RI-RS points r_l into Cartesian cubes used by Eq. (4).
648!> \param grid_points ...
649!> \param edge ...
650!> \param blocks ...
651! **************************************************************************************************
652 SUBROUTINE group_points_l_by_position(grid_points, edge, blocks)
653 REAL(dp), INTENT(IN) :: grid_points(:, :), edge
654 TYPE(l_block_partition_type), INTENT(OUT) :: blocks
655
656 INTEGER :: cube(3), iblock, idir, il, insert
657 INTEGER, ALLOCATABLE :: block_of_l(:), counts(:), cubes(:, :), &
658 next(:)
659 REAL(dp) :: origin(3)
660
661 origin = minval(grid_points, dim=2)
662 ALLOCATE (block_of_l(SIZE(grid_points, 2)), cubes(3, SIZE(grid_points, 2)))
663 blocks%nblocks = 0
664 DO il = 1, SIZE(grid_points, 2)
665 cube = floor((grid_points(:, il) - origin)/edge)
666 iblock = 0
667 DO insert = 1, blocks%nblocks
668 IF (all(cubes(:, insert) == cube)) THEN
669 iblock = insert
670 EXIT
671 END IF
672 END DO
673 IF (iblock == 0) THEN
674 blocks%nblocks = blocks%nblocks + 1
675 iblock = blocks%nblocks
676 cubes(:, iblock) = cube
677 END IF
678 block_of_l(il) = iblock
679 END DO
680 ALLOCATE (counts(blocks%nblocks), next(blocks%nblocks), &
681 blocks%first(blocks%nblocks + 1), blocks%indices(SIZE(grid_points, 2)), &
682 blocks%lower(3, blocks%nblocks), blocks%upper(3, blocks%nblocks))
683 counts = 0
684 DO il = 1, SIZE(grid_points, 2)
685 counts(block_of_l(il)) = counts(block_of_l(il)) + 1
686 END DO
687 blocks%first(1) = 1
688 DO iblock = 1, blocks%nblocks
689 blocks%first(iblock + 1) = blocks%first(iblock) + counts(iblock)
690 END DO
691 next(:) = blocks%first(1:blocks%nblocks)
692 blocks%lower = huge(1.0_dp)
693 blocks%upper = -huge(1.0_dp)
694 DO il = 1, SIZE(grid_points, 2)
695 iblock = block_of_l(il)
696 blocks%indices(next(iblock)) = il
697 next(iblock) = next(iblock) + 1
698 DO idir = 1, 3
699 blocks%lower(idir, iblock) = min(blocks%lower(idir, iblock), grid_points(idir, il))
700 blocks%upper(idir, iblock) = max(blocks%upper(idir, iblock), grid_points(idir, il))
701 END DO
702 END DO
703
704 END SUBROUTINE group_points_l_by_position
705
706! **************************************************************************************************
707!> \brief Return the smallest and largest positive AO primitive exponents for the grid heuristic.
708!> \param bs_env ...
709!> \param exponent_min ...
710!> \param exponent_max ...
711! **************************************************************************************************
712 SUBROUTINE ao_exponent_range(bs_env, exponent_min, exponent_max)
713 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
714 REAL(dp), INTENT(OUT) :: exponent_min, exponent_max
715
716 INTEGER :: ikind, ipgf, iset
717 REAL(dp) :: exponent
718 TYPE(gto_basis_set_type), POINTER :: basis
719
720 exponent_min = huge(1.0_dp)
721 exponent_max = 0.0_dp
722 DO ikind = 1, SIZE(bs_env%basis_set_AO)
723 basis => bs_env%basis_set_AO(ikind)%gto_basis_set
724 IF (.NOT. ASSOCIATED(basis)) cycle
725 DO iset = 1, basis%nset
726 DO ipgf = 1, basis%npgf(iset)
727 exponent = basis%zet(ipgf, iset)
728 IF (exponent <= 0.0_dp) cycle
729 exponent_min = min(exponent_min, exponent)
730 exponent_max = max(exponent_max, exponent)
731 END DO
732 END DO
733 END DO
734 IF (exponent_max <= 0.0_dp .OR. exponent_min == huge(1.0_dp)) THEN
735 cpabort('Uniform DLP found no positive AO primitive exponent')
736 END IF
737
738 END SUBROUTINE ao_exponent_range
739
740! **************************************************************************************************
741!> \brief Use the sparse-filter threshold EPS_FILTER as the DLP threshold.
742!> \param bs_env ...
743!> \return ...
744! **************************************************************************************************
745 FUNCTION d_lp_integration_accuracy(bs_env) RESULT(accuracy)
746 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
747 REAL(dp) :: accuracy
748
749 IF (.NOT. ieee_is_finite(bs_env%eps_filter) .OR. &
750 bs_env%eps_filter <= 0.0_dp .OR. bs_env%eps_filter >= 1.0_dp) THEN
751 cpabort('EPS_FILTER must be between zero and one for periodic RI-RS DLP')
752 END IF
753 accuracy = bs_env%eps_filter
754 END FUNCTION d_lp_integration_accuracy
755
756! **************************************************************************************************
757!> \brief Return the smallest and largest positive primitive exponent of one Gaussian basis.
758!> \param basis ...
759!> \param exponent_min ...
760!> \param exponent_max ...
761! **************************************************************************************************
762 SUBROUTINE basis_exponent_range(basis, exponent_min, exponent_max)
763 TYPE(gto_basis_set_type), POINTER :: basis
764 REAL(dp), INTENT(OUT) :: exponent_min, exponent_max
765
766 INTEGER :: ipgf, iset
767 REAL(dp) :: exponent
768
769 cpassert(ASSOCIATED(basis))
770 exponent_min = huge(1.0_dp)
771 exponent_max = 0.0_dp
772 DO iset = 1, basis%nset
773 DO ipgf = 1, basis%npgf(iset)
774 exponent = basis%zet(ipgf, iset)
775 IF (exponent <= 0.0_dp) cycle
776 exponent_min = min(exponent_min, exponent)
777 exponent_max = max(exponent_max, exponent)
778 END DO
779 END DO
780 IF (exponent_max <= 0.0_dp .OR. exponent_min == huge(1.0_dp)) THEN
781 cpabort('Automatic DLP cutoff found no positive primitive exponent')
782 END IF
783
784 END SUBROUTINE basis_exponent_range
785
786! **************************************************************************************************
787!> \brief Construct d_lP(T) by shared uniform quadrature with analytic auxiliary potentials.
788!> (1) f_l(r)=sum_mu,R ϕ_mu,R(r_l) ϕ_mu,R(r), using the explicit AO cell set.
789!> (2) d_lP(T)=integral f_l(r)^2 v_P,T(r) dr is approximated by |det S| sum_g f_l(r_g)^2 v_P,T(r_g).
790!> (3) r_g=r_0+S n covers the bounding box of r_l enlarged by padding. The spacing follows
791!> h=C/[sqrt(alpha_max log(1/epsilon))], calibrated to h=0.4 bohr for the validated Si basis
792!> at epsilon=1e-8. The padding is 1.1 sqrt[log(1/epsilon)/alpha_min].
793!> Periodic cells use r_0 at atom 1 and S=H diag(1/N_i), with integer N_i and ||S||_2<=h.
794!> Translation and cell wrapping then only shift the quadrature lattice or relabel its points.
795!> (4) Spatial blocks of r_l are screened separately for every P-atom/T pair immediately before
796!> contraction. A triangle-inequality product bound controls discarded discrete terms, not
797!> quadrature error.
798!> The usual l/P/T cutoff removes individual l rows before contraction.
799!> Eq. (1) retains all AO pairs in the explicit cell domain; cell-domain and screening errors
800!> must be converged together with the numerical quadrature.
801!> Integration-point blocks are MPI distributed and each AO field is evaluated once. Bounded batches
802!> share these fields within memory groups whose ranks divide T; independent groups divide g.
803!> Group sums assemble disjoint cyclic T cells. A final all-to-all
804!> redistributes the completed d(T) from T ownership to the caller's l ownership.
805!> Each memory group holds one full partial d(T), subject to the memory budget.
806!> No global AO-pair matrix is formed.
807!> EPS_FILTER controls the grid, finite box and screening threshold together.
808!> \param bs_env ...
809!> \param grid_points ...
810!> \param cells_R_d_lP Retained integer cell vectors R, indexed (3,R).
811!> \param settings ...
812!> \param d_lP_R Numerical integrals indexed by local point l, RI function P and cell R.
813!> \param first_l ...
814!> \param qpoints ...
815!> \param charge_lq ...
816! **************************************************************************************************
817 SUBROUTINE integrate_d_lp_r(bs_env, grid_points, cells_R_d_lP, settings, d_lP_R, first_l, &
818 qpoints, charge_lq)
819 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
820 REAL(dp), INTENT(IN) :: grid_points(:, :)
821 INTEGER, INTENT(IN) :: cells_r_d_lp(:, :)
822 TYPE(uniform_settings_type), INTENT(IN) :: settings
823 REAL(dp), INTENT(OUT) :: d_lp_r(:, :, :)
824 INTEGER, INTENT(IN) :: first_l
825 REAL(dp), INTENT(IN) :: qpoints(:, :)
826 COMPLEX(dp), INTENT(OUT) :: charge_lq(:, :)
827
828 CHARACTER(LEN=*), PARAMETER :: routinen = 'integrate_d_lP_R'
829
830 INTEGER :: block_class, block_point(3), copy(3), counts(3), first_block(3), handle, iatom, &
831 ibatch, iblock, ig, igroup, il, indices(quadrature_block_size), irank, it, last_rank, &
832 n_blocks(3), n_class(3), next_progress, ngroups, nkeep, nl, np, npoints, nranks, &
833 nreceived, point_index(quadrature_block_size), position, quadrature_block_first(3), &
834 quadrature_block_points(3), rank, ranks_per_batch
835 INTEGER(int_8) :: contraction_products, iround, item, n_completed_quadrature_blocks, &
836 n_items, n_local_quadrature_blocks, nrounds, ntotal, potential_points
837 INTEGER(int_8), ALLOCATABLE :: chunk_first(:)
838 INTEGER, ALLOCATABLE :: chunk_class(:), chunk_part(:), chunk_parts(:), kept_per_rank(:), &
839 local_cells(:, :), recv_counts(:), recv_offsets(:)
840 LOGICAL, ALLOCATABLE :: include_l_blocks_p_r(:, :, :), &
841 include_lp_r(:, :, :)
842 REAL(dp) :: available_gb, bound, elapsed, field_bound(quadrature_block_size), lower(3), &
843 mesh_index(3), points(3, quadrature_block_size), prep_start, ri_center_r(3), threshold, &
844 weight
845 REAL(dp), ALLOCATABLE :: cutoffs_lp(:), cyclic_d_lp_r(:, :, :), envelopes(:, :), &
846 field(:, :), local_d_lp_r(:, :, :), q_cart(:, :), receive_fields(:), send_fields(:)
847 TYPE(ao_image_type), ALLOCATABLE :: images(:)
848 TYPE(block_class_cache_type) :: ao_cache, cache
849 TYPE(cell_type), POINTER :: cell
850 TYPE(l_block_partition_type) :: blocks
851 TYPE(mp_comm_type) :: field_comm, integration_comm
852 TYPE(particle_type), POINTER :: particles(:)
853 TYPE(radial_interpolation_type), ALLOCATABLE :: aux_potentials(:)
854
855 CALL timeset(routinen, handle)
856
857 nl = bs_env%ri_rs%n_grid_points
858 cell => bs_env%ri_rs%cell
859 particles => bs_env%ri_rs%particle_set
860 ALLOCATE (field(quadrature_block_size, nl), q_cart(3, SIZE(qpoints, 2)))
861 q_cart(:, :) = matmul(twopi*transpose(cell%h_inv), qpoints)
862 charge_lq = cmplx(0.0_dp, 0.0_dp, dp)
863 elapsed = m_walltime()
864 prep_start = elapsed
865 IF (bs_env%unit_nr > 0) THEN
866 WRITE (bs_env%unit_nr, '(T2,A)') 'd_lP quadrature: evaluating AO image values'
867 FLUSH (bs_env%unit_nr)
868 END IF
869 CALL prepare_ao_images(bs_env, grid_points, images)
870 IF (bs_env%unit_nr > 0) THEN
871 WRITE (bs_env%unit_nr, '(T2,A,T67,F12.1,A)') &
872 'd_lP AO image values, rank 0 time:', m_walltime() - prep_start, ' s'
873 FLUSH (bs_env%unit_nr)
874 END IF
875 CALL prepare_aux_potentials(bs_env, settings, aux_potentials)
876 CALL auxiliary_potential_bound(bs_env, aux_potentials, envelopes)
877 CALL group_points_l_by_position(grid_points, settings%l_block_edge, blocks)
878 bound = maxval(envelopes(1, :))
879 d_lp_r = 0.0_dp
880 CALL prepare_d_lp_mpi_groups(bs_env%para_env, nl, bs_env, SIZE(cells_r_d_lp, 2), SIZE(d_lp_r, kind=int_8), &
881 integration_comm, field_comm, ngroups)
882 igroup = field_comm%mepos
883 nranks = field_comm%num_pe
884 rank = field_comm%mepos
885 np = bs_env%n_RI
886 ALLOCATE (local_cells(3, SIZE(cells_r_d_lp(:, igroup + 1::ngroups), 2)))
887 local_cells(:, :) = cells_r_d_lp(:, igroup + 1::ngroups)
888
889 CALL compute_d_lp_cutoffs(bs_env, cutoffs_lp)
890 ALLOCATE (include_lp_r(nl, bs_env%n_atom, SIZE(local_cells, 2)))
891 ALLOCATE (include_l_blocks_p_r(blocks%nblocks, bs_env%n_atom, SIZE(local_cells, 2)), source=.false.)
892 DO it = 1, SIZE(local_cells, 2)
893 DO iatom = 1, bs_env%n_atom
894 ri_center_r = pbc(particles(iatom)%r, cell) + matmul(bs_env%hmat, real(local_cells(:, it), dp))
895 DO il = 1, nl
896 include_lp_r(il, iatom, it) = sum((grid_points(:, il) - ri_center_r)**2) <= cutoffs_lp(iatom)**2
897 END DO
898 DO iblock = 1, blocks%nblocks
899 IF (distance_to_box_squared(ri_center_r, blocks%lower(:, iblock), &
900 blocks%upper(:, iblock)) > cutoffs_lp(iatom)**2) cycle
901 include_l_blocks_p_r(iblock, iatom, it) = &
902 any(include_lp_r(blocks%indices(blocks%first(iblock):blocks%first(iblock + 1) - 1), iatom, it))
903 END DO
904 END DO
905 END DO
906 DEALLOCATE (cutoffs_lp)
907 IF (bs_env%unit_nr > 0) THEN
908 FLUSH (bs_env%unit_nr)
909 END IF
910
911 ALLOCATE (local_d_lp_r(nl, np, SIZE(local_cells, 2)), source=0.0_dp)
912 ALLOCATE (kept_per_rank(nranks), recv_counts(nranks), recv_offsets(nranks))
913 ! At most 32 MiB of shared fields per rank, independent of the MPI size.
914 ranks_per_batch = max(1, int(message_batch_bytes/ &
915 (real_bytes*real(nl + 4, dp)*real(quadrature_block_size, dp))))
916 ranks_per_batch = min(nranks, ranks_per_batch)
917 ALLOCATE (send_fields((nl + 4)*quadrature_block_size), &
918 receive_fields((nl + 4)*quadrature_block_size*ranks_per_batch))
919 contraction_products = 0_int_8
920 potential_points = 0_int_8
921 weight = settings%weight
922 lower = settings%lower
923 counts = settings%counts
924 ntotal = product(int(counts, int_8))
925 ! Each integration group handles parts of classes of cell-aligned blocks, its ranks share the
926 ! fields of each block, and every rank keeps the potentials of the current class.
927 CALL plan_quadrature_blocks(settings, cell%perd, integration_comm%num_pe, integration_comm%mepos, &
928 n_class, first_block, n_blocks, chunk_class, chunk_part, chunk_parts, &
929 chunk_first)
930 n_items = chunk_first(SIZE(chunk_class))
931 nrounds = (n_items + int(nranks, int_8) - 1_int_8)/int(nranks, int_8)
932 CALL mp_mem_avail_per_rank_gb(bs_env%para_env, available_gb)
933 CALL prepare_block_class_cache(cache, local_cells, bs_env%n_atom, quadrature_block_size, &
934 maxval(bs_env%i_RI_end_from_atom - bs_env%i_RI_start_from_atom + 1), &
935 n_class, first_block, n_blocks, ri_cache_share*bytes_per_gb*available_gb, .true.)
936 CALL prepare_block_class_cache(ao_cache, bs_env%index_to_cell_3c, bs_env%n_atom, quadrature_block_size, &
937 maxval(bs_env%i_ao_end_from_atom - bs_env%i_ao_start_from_atom + 1), &
938 n_class, first_block, n_blocks, ao_cache_share*bytes_per_gb*available_gb, .false.)
939 next_progress = 1
940 n_local_quadrature_blocks = nrounds
941 threshold = 0.5_dp*settings%accuracy/(real(ntotal, dp)*weight)
942 DO iround = 0_int_8, nrounds - 1_int_8
943 item = iround*int(nranks, int_8) + int(rank, int_8)
944 nkeep = 0
945 IF (item < n_items) THEN
946 CALL locate_quadrature_block(item, settings, cell%perd, n_class, first_block, n_blocks, &
947 chunk_class, chunk_part, chunk_parts, chunk_first, block_class, &
948 copy, quadrature_block_first, quadrature_block_points)
949 ! Points are indexed in the complete block, so all copies address the same potentials.
950 npoints = 0
951 DO ig = 1, quadrature_block_size
952 block_point(1) = modulo(ig - 1, quadrature_block_edge)
953 block_point(2) = modulo((ig - 1)/quadrature_block_edge, quadrature_block_edge)
954 block_point(3) = (ig - 1)/quadrature_block_edge**2
955 IF (any(block_point >= quadrature_block_points)) cycle
956 IF (any(quadrature_block_first + block_point < 0 .OR. &
957 quadrature_block_first + block_point >= counts)) cycle
958 npoints = npoints + 1
959 point_index(npoints) = ig
960 mesh_index = lower + real(quadrature_block_first + block_point, dp)
961 points(:, npoints) = settings%origin + matmul(settings%step, mesh_index)
962 END DO
963 IF (block_class /= ao_cache%block_class) CALL reset_block_class_cache(ao_cache, block_class)
964 CALL evaluate_rho_l_squared(images, points(:, 1:npoints), point_index(1:npoints), copy, ao_cache, &
965 field(1:npoints, :), &
966 settings%accuracy/(8.0_dp*weight*real(ntotal, dp)*max(1.0_dp, bound)))
967 CALL accumulate_fourier_charge(points(:, 1:npoints), field, q_cart, weight, charge_lq)
968 nkeep = 0
969 field_bound(1:npoints) = 0.0_dp
970 DO il = 1, nl
971 !$OMP SIMD
972 DO ig = 1, npoints
973 field_bound(ig) = max(field_bound(ig), abs(field(ig, il)))
974 END DO
975 END DO
976 DO ig = 1, npoints
977 IF (field_bound(ig)*bound > threshold) THEN
978 nkeep = nkeep + 1
979 indices(nkeep) = ig
980 END IF
981 END DO
982 DO ig = 1, nkeep
983 send_fields(4*ig - 3:4*ig - 1) = points(:, indices(ig))
984 send_fields(4*ig) = real(point_index(indices(ig)), dp)
985 END DO
986 DO il = 1, nl
987 DO ig = 1, nkeep
988 send_fields(4*nkeep + (il - 1)*nkeep + ig) = field(indices(ig), il)
989 END DO
990 END DO
991 END IF
992 CALL field_comm%allgather(nkeep, kept_per_rank)
993 DO ibatch = 1, nranks, ranks_per_batch
994 last_rank = min(nranks, ibatch + ranks_per_batch - 1)
995 recv_counts = 0
996 recv_counts(ibatch:last_rank) = (nl + 4)*kept_per_rank(ibatch:last_rank)
997 nreceived = 0
998 DO irank = 1, nranks
999 recv_offsets(irank) = nreceived
1000 nreceived = nreceived + recv_counts(irank)
1001 END DO
1002 IF (nreceived == 0) cycle
1003 cpassert(nreceived <= SIZE(receive_fields))
1004 CALL field_comm%allgatherv(send_fields(1:recv_counts(rank + 1)), &
1005 receive_fields(1:nreceived), recv_counts, recv_offsets)
1006 DO irank = ibatch, last_rank
1007 nkeep = kept_per_rank(irank)
1008 IF (nkeep == 0 .OR. SIZE(local_cells, 2) == 0) cycle
1009 position = recv_offsets(irank)
1010 DO ig = 1, nkeep
1011 points(:, ig) = receive_fields(position + 4*ig - 3:position + 4*ig - 1)
1012 point_index(ig) = nint(receive_fields(position + 4*ig))
1013 END DO
1014 DO il = 1, nl
1015 DO ig = 1, nkeep
1016 field(ig, il) = receive_fields(position + 4*nkeep + (il - 1)*nkeep + ig)
1017 END DO
1018 END DO
1019 CALL locate_quadrature_block(iround*int(nranks, int_8) + int(irank - 1, int_8), settings, &
1020 cell%perd, n_class, first_block, n_blocks, chunk_class, &
1021 chunk_part, chunk_parts, chunk_first, block_class, copy, &
1022 quadrature_block_first, quadrature_block_points)
1023 IF (block_class /= cache%block_class) CALL reset_block_class_cache(cache, block_class)
1024 CALL integrate_potentials(bs_env, local_cells, include_lp_r, include_l_blocks_p_r, &
1025 points(:, 1:nkeep), point_index(1:nkeep), field(1:nkeep, :), &
1026 weight, envelopes, blocks, threshold, copy, cache, potential_points, &
1027 contraction_products, local_d_lp_r, aux_potentials)
1028 END DO
1029 END DO
1030 n_completed_quadrature_blocks = iround + 1_int_8
1031 CALL write_ri_rs_progress(bs_env%unit_nr, 'd_lP Integration batch done', &
1032 n_completed_quadrature_blocks, n_local_quadrature_blocks, next_progress, elapsed)
1033 END DO
1034
1035 DEALLOCATE (field, send_fields, receive_fields, kept_per_rank, recv_counts, recv_offsets, &
1036 chunk_class, chunk_part, chunk_parts, chunk_first)
1037 CALL sum_d_lp_r_over_mpi_ranks(integration_comm, local_d_lp_r, cyclic_d_lp_r)
1038 DEALLOCATE (local_d_lp_r, local_cells)
1039 CALL distribute_d_lp_r_by_l(bs_env%para_env, cyclic_d_lp_r, first_l, d_lp_r)
1040 DEALLOCATE (cyclic_d_lp_r)
1041 CALL integration_comm%free()
1042 CALL field_comm%free()
1043 CALL bs_env%para_env%sum(charge_lq)
1044 CALL bs_env%para_env%sum(potential_points)
1045 CALL bs_env%para_env%sum(contraction_products)
1046
1047 IF (bs_env%unit_nr > 0) THEN
1048 WRITE (bs_env%unit_nr, '(T2,A,T72,ES9.1E2)') &
1049 'Potential evaluations V_P^R(r_g) (point, atom image):', real(potential_points, dp)
1050 WRITE (bs_env%unit_nr, '(T2,A,T72,ES9.1E2)') &
1051 'd_lP products f_l(r_g)^2 V_P^R(r_g):', real(contraction_products, dp)
1052 WRITE (bs_env%unit_nr, '(T2,A)') &
1053 'f_l(r) = SUM_(μ,R) ϕ_μ^R(r_l) ϕ_μ^R(r)'
1054 WRITE (bs_env%unit_nr, '(T2,A)') &
1055 'd_lP^R = SUM_g w_g f_l(r_g)^2 V_P^R(r_g)'
1056 END IF
1057 elapsed = m_walltime() - elapsed
1058 CALL bs_env%para_env%max(elapsed)
1059 IF (bs_env%unit_nr > 0) WRITE (bs_env%unit_nr, '(T2,A,T69,F10.1,A)') &
1060 'd_lP completed numerical integration, Exec. time:', elapsed, ' s'
1061 IF (bs_env%unit_nr > 0) FLUSH (bs_env%unit_nr)
1062
1063 CALL timestop(handle)
1064
1065 END SUBROUTINE integrate_d_lp_r
1066
1067! **************************************************************************************************
1068!> \brief Distribute the cell-aligned quadrature blocks over the integration groups by class.
1069!> In a periodic direction, blocks of up to quadrature_block_edge points tile each cell period of
1070!> N subdivisions. Block j = c + M t is block c of the M blocks of period t, so all lattice copies t
1071!> of the class c see the same RI potentials. In a non-periodic direction every block is a class.
1072!> A group takes whole classes; a class with more blocks than a group share is split into strided
1073!> parts, each of which spans the whole integration box.
1074!> \param settings ...
1075!> \param perd periodicity of the cell
1076!> \param n_groups ...
1077!> \param my_group ...
1078!> \param n_class classes M per direction
1079!> \param first_block index j of the first box block per direction
1080!> \param n_blocks box blocks per direction
1081!> \param chunk_class class of each part of this group (from 0)
1082!> \param chunk_part ...
1083!> \param chunk_parts ...
1084!> \param chunk_first number of blocks of this group before each part, indexed from 0
1085! **************************************************************************************************
1086 SUBROUTINE plan_quadrature_blocks(settings, perd, n_groups, my_group, n_class, first_block, &
1087 n_blocks, chunk_class, chunk_part, chunk_parts, chunk_first)
1088 TYPE(uniform_settings_type), INTENT(IN) :: settings
1089 INTEGER, INTENT(IN) :: perd(3), n_groups, my_group
1090 INTEGER, INTENT(OUT) :: n_class(3), first_block(3), n_blocks(3)
1091 INTEGER, ALLOCATABLE, INTENT(OUT) :: chunk_class(:), chunk_part(:), &
1092 chunk_parts(:)
1093 INTEGER(int_8), ALLOCATABLE, INTENT(OUT) :: chunk_first(:)
1094
1095 INTEGER :: block_class, copy_lower(3), group, ichunk, idir, ipart, last_point, n_chunks, &
1096 n_copies(3), n_mine, n_parts, period, position(3)
1097 INTEGER(int_8) :: class_blocks, part_length
1098 INTEGER(int_8), ALLOCATABLE :: load(:), part_blocks(:)
1099 INTEGER, ALLOCATABLE :: owner(:), part_class(:), part_count(:), &
1100 part_index(:)
1101
1102 DO idir = 1, 3
1103 IF (perd(idir) == 1) THEN
1104 n_class(idir) = (settings%subdivisions(idir) + quadrature_block_edge - 1)/quadrature_block_edge
1105 period = floor(settings%lower(idir)/real(settings%subdivisions(idir), dp))
1106 first_block(idir) = n_class(idir)*period + &
1107 (nint(settings%lower(idir)) - settings%subdivisions(idir)*period)/quadrature_block_edge
1108 last_point = nint(settings%lower(idir)) + settings%counts(idir) - 1
1109 period = floor(real(last_point, dp)/real(settings%subdivisions(idir), dp))
1110 n_blocks(idir) = n_class(idir)*period + &
1111 (last_point - settings%subdivisions(idir)*period)/quadrature_block_edge - &
1112 first_block(idir) + 1
1113 ELSE
1114 n_blocks(idir) = (settings%counts(idir) + quadrature_block_edge - 1)/quadrature_block_edge
1115 n_class(idir) = n_blocks(idir)
1116 first_block(idir) = 0
1117 END IF
1118 END DO
1119 ! About 64 parts per group balance the load and still reuse each potential many times.
1120 part_length = max(1_int_8, product(int(n_blocks, int_8))/(64_int_8*int(n_groups, int_8)))
1121 n_chunks = 0
1122 DO block_class = 0, product(n_class) - 1
1123 CALL class_copy_range(block_class, n_class, first_block, n_blocks, position, copy_lower, n_copies)
1124 class_blocks = product(int(n_copies, int_8))
1125 n_chunks = n_chunks + int((class_blocks + part_length - 1_int_8)/part_length)
1126 END DO
1127 ALLOCATE (part_class(n_chunks), part_index(n_chunks), part_count(n_chunks), &
1128 part_blocks(n_chunks), owner(n_chunks), load(0:n_groups - 1))
1129 load = 0_int_8
1130 ichunk = 0
1131 DO block_class = 0, product(n_class) - 1
1132 CALL class_copy_range(block_class, n_class, first_block, n_blocks, position, copy_lower, n_copies)
1133 class_blocks = product(int(n_copies, int_8))
1134 n_parts = int((class_blocks + part_length - 1_int_8)/part_length)
1135 DO ipart = 0, n_parts - 1
1136 ichunk = ichunk + 1
1137 part_class(ichunk) = block_class
1138 part_index(ichunk) = ipart
1139 part_count(ichunk) = n_parts
1140 part_blocks(ichunk) = (class_blocks - int(ipart, int_8) + int(n_parts - 1, int_8))/int(n_parts, int_8)
1141 group = minloc(load, 1) - 1
1142 owner(ichunk) = group
1143 load(group) = load(group) + part_blocks(ichunk)
1144 END DO
1145 END DO
1146 n_mine = count(owner == my_group)
1147 ALLOCATE (chunk_class(n_mine), chunk_part(n_mine), chunk_parts(n_mine), chunk_first(0:n_mine))
1148 chunk_first(0) = 0_int_8
1149 n_mine = 0
1150 DO ichunk = 1, n_chunks
1151 IF (owner(ichunk) /= my_group) cycle
1152 n_mine = n_mine + 1
1153 chunk_class(n_mine) = part_class(ichunk)
1154 chunk_part(n_mine) = part_index(ichunk)
1155 chunk_parts(n_mine) = part_count(ichunk)
1156 chunk_first(n_mine) = chunk_first(n_mine - 1) + part_blocks(ichunk)
1157 END DO
1158 DEALLOCATE (part_class, part_index, part_count, part_blocks, owner, load)
1159
1160 END SUBROUTINE plan_quadrature_blocks
1161
1162! **************************************************************************************************
1163!> \brief Position of a block class and the range of its lattice copies in the box.
1164!> \param block_class class index from 0
1165!> \param n_class ...
1166!> \param first_block ...
1167!> \param n_blocks ...
1168!> \param position c per direction
1169!> \param copy_lower first copy t per direction
1170!> \param n_copies copies per direction
1171! **************************************************************************************************
1172 PURE SUBROUTINE class_copy_range(block_class, n_class, first_block, n_blocks, position, &
1173 copy_lower, n_copies)
1174 INTEGER, INTENT(IN) :: block_class, n_class(3), first_block(3), &
1175 n_blocks(3)
1176 INTEGER, INTENT(OUT) :: position(3), copy_lower(3), n_copies(3)
1177
1178 INTEGER :: idir
1179
1180 position(1) = modulo(block_class, n_class(1))
1181 position(2) = modulo(block_class/n_class(1), n_class(2))
1182 position(3) = block_class/(n_class(1)*n_class(2))
1183 DO idir = 1, 3
1184 copy_lower(idir) = ceiling(real(first_block(idir) - position(idir), dp)/real(n_class(idir), dp))
1185 n_copies(idir) = max(0, floor(real(first_block(idir) + n_blocks(idir) - 1 - position(idir), dp)/ &
1186 REAL(n_class(idir), dp)) - copy_lower(idir) + 1)
1187 END DO
1188
1189 END SUBROUTINE class_copy_range
1190
1191! **************************************************************************************************
1192!> \brief Class, lattice copy and box points of the item-th block of this integration group.
1193!> \param item block of the group, from 0
1194!> \param settings ...
1195!> \param perd ...
1196!> \param n_class ...
1197!> \param first_block ...
1198!> \param n_blocks ...
1199!> \param chunk_class ...
1200!> \param chunk_part ...
1201!> \param chunk_parts ...
1202!> \param chunk_first ...
1203!> \param block_class ...
1204!> \param copy lattice vector t of the copy
1205!> \param block_first first point of the block in the box per direction, may lie before the box
1206!> \param block_points points of the block per direction
1207! **************************************************************************************************
1208 SUBROUTINE locate_quadrature_block(item, settings, perd, n_class, first_block, n_blocks, &
1209 chunk_class, chunk_part, chunk_parts, chunk_first, &
1210 block_class, copy, block_first, block_points)
1211 INTEGER(int_8), INTENT(IN) :: item
1212 TYPE(uniform_settings_type), INTENT(IN) :: settings
1213 INTEGER, INTENT(IN) :: perd(3), n_class(3), first_block(3), &
1214 n_blocks(3), chunk_class(:), &
1215 chunk_part(:), chunk_parts(:)
1216 INTEGER(int_8), INTENT(IN) :: chunk_first(0:)
1217 INTEGER, INTENT(OUT) :: block_class, copy(3), block_first(3), &
1218 block_points(3)
1219
1220 INTEGER :: copy_lower(3), ichunk, idir, &
1221 lower_chunk, n_copies(3), position(3), &
1222 upper_chunk
1223 INTEGER(int_8) :: k
1224
1225 lower_chunk = 1
1226 upper_chunk = SIZE(chunk_class)
1227 DO WHILE (lower_chunk < upper_chunk)
1228 ichunk = (lower_chunk + upper_chunk)/2
1229 IF (chunk_first(ichunk) <= item) THEN
1230 lower_chunk = ichunk + 1
1231 ELSE
1232 upper_chunk = ichunk
1233 END IF
1234 END DO
1235 ichunk = lower_chunk
1236 block_class = chunk_class(ichunk)
1237 CALL class_copy_range(block_class, n_class, first_block, n_blocks, position, copy_lower, n_copies)
1238 k = int(chunk_part(ichunk), int_8) + int(chunk_parts(ichunk), int_8)*(item - chunk_first(ichunk - 1))
1239 copy(1) = copy_lower(1) + int(modulo(k, int(n_copies(1), int_8)))
1240 copy(2) = copy_lower(2) + int(modulo(k/int(n_copies(1), int_8), int(n_copies(2), int_8)))
1241 copy(3) = copy_lower(3) + int(k/int(n_copies(1)*n_copies(2), int_8))
1242 DO idir = 1, 3
1243 IF (perd(idir) == 1) THEN
1244 block_first(idir) = settings%subdivisions(idir)*copy(idir) + &
1245 quadrature_block_edge*position(idir) - nint(settings%lower(idir))
1246 block_points(idir) = min(quadrature_block_edge, &
1247 settings%subdivisions(idir) - quadrature_block_edge*position(idir))
1248 ELSE
1249 block_first(idir) = quadrature_block_edge*position(idir)
1250 block_points(idir) = quadrature_block_edge
1251 END IF
1252 END DO
1253
1254 END SUBROUTINE locate_quadrature_block
1255
1256! **************************************************************************************************
1257!> \brief Allocate the slot map over all U = T - t of this rank's cells T and the box copies t.
1258!> \param cache ...
1259!> \param cells ...
1260!> \param n_atom ...
1261!> \param n_points points of a complete block
1262!> \param maxnp functions per atom
1263!> \param n_class ...
1264!> \param first_block ...
1265!> \param n_blocks ...
1266!> \param budget bytes the values may occupy
1267!> \param point_bounds whether the slots also keep point bounds
1268! **************************************************************************************************
1269 SUBROUTINE prepare_block_class_cache(cache, cells, n_atom, n_points, maxnp, n_class, first_block, &
1270 n_blocks, budget, point_bounds)
1271 TYPE(block_class_cache_type), INTENT(OUT) :: cache
1272 INTEGER, INTENT(IN) :: cells(:, :), n_atom, n_points, maxnp, &
1273 n_class(3), first_block(3), n_blocks(3)
1274 REAL(dp), INTENT(IN) :: budget
1275 LOGICAL, INTENT(IN) :: point_bounds
1276
1277 INTEGER :: copy_lower(3), copy_upper(3), idir, &
1278 u_lower(3), u_upper(3)
1279
1280 copy_lower = floor(real(first_block, dp)/real(n_class, dp))
1281 copy_upper = floor(real(first_block + n_blocks - 1, dp)/real(n_class, dp))
1282 u_lower = 0
1283 u_upper = 0
1284 IF (SIZE(cells, 2) > 0) THEN
1285 DO idir = 1, 3
1286 u_lower(idir) = minval(cells(idir, :)) - copy_upper(idir)
1287 u_upper(idir) = maxval(cells(idir, :)) - copy_lower(idir)
1288 END DO
1289 END IF
1290 ALLOCATE (cache%slot(u_lower(1):u_upper(1), u_lower(2):u_upper(2), u_lower(3):u_upper(3), n_atom), &
1291 source=0)
1292 ! Only the pages of used slots are touched, so the budget is reserved, not occupied.
1293 cache%n_slots = int(min(real(SIZE(cache%slot), dp), &
1294 budget/(real_bytes*real(n_points, dp)*real(maxnp + 2, dp))))
1295 ALLOCATE (cache%keys(4, cache%n_slots), cache%evaluated(n_points, cache%n_slots), &
1296 cache%values(maxnp, n_points, cache%n_slots))
1297 IF (point_bounds) ALLOCATE (cache%point_bound(n_points, cache%n_slots))
1298
1299 END SUBROUTINE prepare_block_class_cache
1300
1301! **************************************************************************************************
1302!> \brief Start the values of a new block class.
1303!> \param cache ...
1304!> \param block_class ...
1305! **************************************************************************************************
1306 SUBROUTINE reset_block_class_cache(cache, block_class)
1307 TYPE(block_class_cache_type), INTENT(INOUT) :: cache
1308 INTEGER, INTENT(IN) :: block_class
1309
1310 INTEGER :: islot
1311
1312 DO islot = 1, cache%n_used
1313 cache%slot(cache%keys(1, islot), cache%keys(2, islot), cache%keys(3, islot), cache%keys(4, islot)) = 0
1314 END DO
1315 cache%n_used = 0
1316 cache%block_class = block_class
1317
1318 END SUBROUTINE reset_block_class_cache
1319
1320! **************************************************************************************************
1321!> \brief Form each atom-local auxiliary potential's spherical coefficients just once.
1322!> Without AUTO_RI, the contraction U_Pp is the identity.
1323!> \param bs_env ...
1324!> \param settings ...
1325!> \param aux_potentials ...
1326! **************************************************************************************************
1327 SUBROUTINE prepare_aux_potentials(bs_env, settings, aux_potentials)
1328 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
1329 TYPE(uniform_settings_type), INTENT(IN) :: settings
1330 TYPE(radial_interpolation_type), ALLOCATABLE, &
1331 INTENT(OUT) :: aux_potentials(:)
1332
1333 CHARACTER(LEN=12) :: spacing_text
1334 INTEGER :: first_u, iatom, ikind, ip, nmesh, np, &
1335 nref
1336 REAL(dp) :: mesh_bytes, mesh_displacement(3), &
1337 mesh_spacing
1338 REAL(dp), ALLOCATABLE :: u_pp(:, :)
1339 TYPE(cell_type), POINTER :: cell
1340 TYPE(gto_basis_set_type), POINTER :: basis
1341 TYPE(particle_type), POINTER :: particles(:)
1342
1343 particles => bs_env%ri_rs%particle_set
1344 cell => bs_env%ri_rs%cell
1345 ALLOCATE (aux_potentials(bs_env%n_atom))
1346 nmesh = 0
1347 mesh_bytes = 0.0_dp
1348 mesh_spacing = 0.0_dp
1349 DO iatom = 1, bs_env%n_atom
1350 ikind = particles(iatom)%atomic_kind%kind_number
1351 basis => bs_env%basis_set_RI(ikind)%gto_basis_set
1352 IF (bs_env%auto_ri%enabled) THEN
1353 nref = bs_env%auto_ri%sizes_ref_RI(iatom)
1354 np = bs_env%auto_ri%sizes_opt_RI(iatom)
1355 first_u = bs_env%auto_ri%U_Pp_atom_offset(iatom)
1356 ALLOCATE (u_pp(nref, np))
1357 u_pp(:, :) = reshape(bs_env%auto_ri%U_Pp_atom(first_u:first_u + nref*np - 1), [nref, np])
1358 ELSE
1359 ALLOCATE (u_pp(basis%nsgf, basis%nsgf), source=0.0_dp)
1360 DO ip = 1, basis%nsgf
1361 u_pp(ip, ip) = 1.0_dp
1362 END DO
1363 END IF
1364 mesh_displacement = settings%origin - pbc(particles(iatom)%r, cell)
1365 CALL prepare_contracted_potential(basis, u_pp, aux_potentials(iatom), &
1366 max(0.0_dp, bs_env%ri_metric%omega), &
1367 radial_table_bytes=radial_table_budget/real(max(1, bs_env%n_atom), dp), &
1368 mesh_step=settings%step, &
1369 mesh_displacement=mesh_displacement)
1370 IF (ALLOCATED(aux_potentials(iatom)%mesh_moments)) THEN
1371 nmesh = nmesh + 1
1372 mesh_bytes = mesh_bytes + real_bytes*real(SIZE(aux_potentials(iatom)%mesh_moments, kind=int_8), dp)
1373 mesh_spacing = aux_potentials(iatom)%mesh_spacing
1374 END IF
1375 DEALLOCATE (u_pp)
1376 END DO
1377 IF (bs_env%unit_nr > 0) THEN
1378 IF (nmesh > 0) THEN
1379 WRITE (bs_env%unit_nr, '(T2,A,T69,F12.1)') 'd_lP radial mesh storage [MiB/rank]', mesh_bytes/bytes_per_mib
1380 WRITE (spacing_text, '(ES12.1E1)') mesh_spacing
1381 IF (index(spacing_text, '*') > 0) WRITE (spacing_text, '(ES12.1E2)') mesh_spacing
1382 IF (index(spacing_text, '*') > 0) WRITE (spacing_text, '(ES12.1E3)') mesh_spacing
1383 WRITE (bs_env%unit_nr, '(T2,A,T69,A12)') 'd_lP radial mesh squared spacing [bohr^2]', spacing_text
1384 END IF
1385 END IF
1386
1387 END SUBROUTINE prepare_aux_potentials
1388
1389! **************************************************************************************************
1390!> \brief Store nonzero atom-image coefficients ϕ_mu,R(r_l) in Eq. (1) once.
1391!> \param bs_env ...
1392!> \param grid_points ...
1393!> \param images ...
1394! **************************************************************************************************
1395 SUBROUTINE prepare_ao_images(bs_env, grid_points, images)
1396 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
1397 REAL(dp), INTENT(IN) :: grid_points(:, :)
1398 TYPE(ao_image_type), ALLOCATABLE, INTENT(OUT) :: images(:)
1399
1400 INTEGER :: iatom, icell, iimage, ikind, maxao, nao
1401 REAL(dp) :: center(3), cutoff2
1402 REAL(dp), ALLOCATABLE :: primary(:, :)
1403 TYPE(cell_type), POINTER :: cell
1404 TYPE(gto_basis_set_type), POINTER :: basis
1405 TYPE(particle_type), POINTER :: particles(:)
1406
1407 cell => bs_env%ri_rs%cell
1408 particles => bs_env%ri_rs%particle_set
1409 maxao = maxval(bs_env%i_ao_end_from_atom - bs_env%i_ao_start_from_atom + 1)
1410 ALLOCATE (primary(bs_env%ri_rs%n_grid_points, maxao))
1411 ALLOCATE (images(bs_env%n_atom*SIZE(bs_env%index_to_cell_3c, 2)))
1412 iimage = 0
1413 DO icell = 1, SIZE(bs_env%index_to_cell_3c, 2)
1414 DO iatom = 1, bs_env%n_atom
1415 ikind = particles(iatom)%atomic_kind%kind_number
1416 basis => bs_env%basis_set_AO(ikind)%gto_basis_set
1417 nao = bs_env%i_ao_end_from_atom(iatom) - bs_env%i_ao_start_from_atom(iatom) + 1
1418 center = pbc(particles(iatom)%r, cell) + matmul(bs_env%hmat, real(bs_env%index_to_cell_3c(:, icell), dp))
1419 cutoff2 = bs_env%ri_rs%radius_ao_per_atom(iatom)**2
1420 IF (bs_env%ri_rs%cutoff_radius_ri_ao > 0.0_dp) cutoff2 = bs_env%ri_rs%cutoff_radius_ri_ao**2
1421 primary = 0.0_dp
1422 CALL evaluate_ao_basis_on_points(primary(:, 1:nao), grid_points, basis, center, cell, &
1423 cutoff_squared=cutoff2, minimum_image=.false.)
1424 IF (.NOT. any(primary(:, 1:nao) /= 0.0_dp)) cycle
1425 iimage = iimage + 1
1426 images(iimage)%basis => basis
1427 images(iimage)%atom = iatom
1428 images(iimage)%cell = bs_env%index_to_cell_3c(:, icell)
1429 images(iimage)%center = center
1430 ALLOCATE (images(iimage)%primary(bs_env%ri_rs%n_grid_points, nao))
1431 images(iimage)%primary(:, :) = primary(:, 1:nao)
1432 CALL pack_nonzero_ao_rows(images(iimage))
1433 CALL bound_ao_image(images(iimage))
1434 END DO
1435 END DO
1436
1437 END SUBROUTINE prepare_ao_images
1438
1439! **************************************************************************************************
1440!> \brief Store only exactly nonzero l rows of Eq. (1) when at least half the rows are zero.
1441!> primary_l maps packed rows back to the original r_l; no finite AO coefficient is discarded.
1442!> \param image ...
1443! **************************************************************************************************
1444 SUBROUTINE pack_nonzero_ao_rows(image)
1445 TYPE(ao_image_type), INTENT(INOUT) :: image
1446
1447 INTEGER :: il, ipacked, nkeep, nl
1448 LOGICAL, ALLOCATABLE :: nonzero(:)
1449 REAL(dp), ALLOCATABLE :: primary(:, :)
1450
1451 nl = SIZE(image%primary, 1)
1452 ALLOCATE (nonzero(nl))
1453 DO il = 1, nl
1454 nonzero(il) = any(abs(image%primary(il, :)) > 0.0_dp)
1455 END DO
1456 nkeep = count(nonzero)
1457 IF (nkeep == 0 .OR. nkeep > nl/2) RETURN
1458 ALLOCATE (primary(nkeep, SIZE(image%primary, 2)), image%primary_l(nkeep))
1459 ipacked = 0
1460 DO il = 1, nl
1461 IF (.NOT. nonzero(il)) cycle
1462 ipacked = ipacked + 1
1463 primary(ipacked, :) = image%primary(il, :)
1464 image%primary_l(ipacked) = il
1465 END DO
1466 CALL move_alloc(primary, image%primary)
1467
1468 END SUBROUTINE pack_nonzero_ao_rows
1469
1470! **************************************************************************************************
1471!> \brief Bound each image contribution to f_l(r) in Eq. (1) by B exp(-a_min r^2/2).
1472!> |x^n exp(-a x^2/2)| <= (n/(a e))^(n/2) bounds the remaining polynomial and Gaussian.
1473!> \param image ...
1474! **************************************************************************************************
1475 SUBROUTINE bound_ao_image(image)
1476 TYPE(ao_image_type), INTENT(INOUT) :: image
1477
1478 INTEGER :: ico, idir, il, ipgf, irow, iset, isgf, &
1479 ncart, power
1480 REAL(dp) :: amplitude, exponent, factor
1481 REAL(dp), ALLOCATABLE :: ao_bound(:)
1482 TYPE(gto_basis_set_type), POINTER :: basis
1483
1484 basis => image%basis
1485 image%decay = huge(0.0_dp)
1486 ALLOCATE (ao_bound(SIZE(image%primary, 2)), source=0.0_dp)
1487 DO iset = 1, basis%nset
1488 ncart = ncoset(basis%lmax(iset))
1489 DO ipgf = 1, basis%npgf(iset)
1490 exponent = basis%zet(ipgf, iset)
1491 cpassert(exponent > 0.0_dp)
1492 image%decay = min(image%decay, 0.5_dp*exponent)
1493 DO ico = 1, ncart
1494 factor = 1.0_dp
1495 DO idir = 1, 3
1496 power = indco(idir, ico)
1497 IF (power > 0) factor = factor*(real(power, dp)/(exponent*exp(1.0_dp)))** &
1498 (0.5_dp*real(power, dp))
1499 END DO
1500 irow = (ipgf - 1)*ncart + ico
1501 DO isgf = basis%first_sgf(1, iset), basis%last_sgf(basis%nshell(iset), iset)
1502 ao_bound(isgf) = ao_bound(isgf) + abs(basis%sphi(irow, isgf))*factor
1503 END DO
1504 END DO
1505 END DO
1506 END DO
1507 image%bound = 0.0_dp
1508 DO il = 1, SIZE(image%primary, 1)
1509 amplitude = 0.0_dp
1510 DO isgf = 1, SIZE(ao_bound)
1511 amplitude = amplitude + abs(image%primary(il, isgf))*ao_bound(isgf)
1512 END DO
1513 image%bound = max(image%bound, amplitude)
1514 END DO
1515 CALL prepare_ao_radial_envelope(image)
1516
1517 END SUBROUTINE bound_ao_image
1518
1519! **************************************************************************************************
1520!> \brief Bound each Cartesian primitive in Eq. (1) by its exact angular maximum at fixed radius.
1521!> For degree n, |x^i y^j z^k| <= r^n product_d (n_d/n)^(n_d/2).
1522!> Positive coefficient sums bound every l row without changing the field error budget.
1523!> \param image ...
1524! **************************************************************************************************
1525 SUBROUTINE prepare_ao_radial_envelope(image)
1526 TYPE(ao_image_type), INTENT(INOUT) :: image
1527
1528 INTEGER :: ico, idegree, idir, iexponent, il, ipgf, &
1529 irow, iset, isgf, ncart, power
1530 REAL(dp) :: amplitude, angular_factor
1531 REAL(dp), ALLOCATABLE :: ao_weight(:)
1532 TYPE(gto_basis_set_type), POINTER :: basis
1533
1534 basis => image%basis
1535 ALLOCATE (image%envelope_exponents(sum(basis%npgf(:basis%nset))), &
1536 image%radial_envelope(0:maxval(basis%lmax(:basis%nset)), sum(basis%npgf(:basis%nset))), &
1537 ao_weight(SIZE(image%primary, 2)))
1538 ao_weight = 0.0_dp
1539 DO isgf = 1, SIZE(ao_weight)
1540 DO il = 1, SIZE(image%primary, 1)
1541 ao_weight(isgf) = max(ao_weight(isgf), abs(image%primary(il, isgf)))
1542 END DO
1543 END DO
1544 image%radial_envelope = 0.0_dp
1545 iexponent = 0
1546 DO iset = 1, basis%nset
1547 ncart = ncoset(basis%lmax(iset))
1548 DO ipgf = 1, basis%npgf(iset)
1549 iexponent = iexponent + 1
1550 image%envelope_exponents(iexponent) = basis%zet(ipgf, iset)
1551 DO ico = 1, ncart
1552 idegree = sum(indco(:, ico))
1553 angular_factor = 1.0_dp
1554 DO idir = 1, 3
1555 power = indco(idir, ico)
1556 IF (power > 0) angular_factor = angular_factor* &
1557 (real(power, dp)/real(idegree, dp))**(0.5_dp*real(power, dp))
1558 END DO
1559 irow = (ipgf - 1)*ncart + ico
1560 amplitude = 0.0_dp
1561 DO isgf = basis%first_sgf(1, iset), basis%last_sgf(basis%nshell(iset), iset)
1562 amplitude = amplitude + ao_weight(isgf)*abs(basis%sphi(irow, isgf))
1563 END DO
1564 image%radial_envelope(idegree, iexponent) = &
1565 image%radial_envelope(idegree, iexponent) + angular_factor*amplitude
1566 END DO
1567 END DO
1568 END DO
1569 image%radial_envelope(:, :) = image%radial_envelope* &
1570 (1.0_dp + epsilon(1.0_dp)* &
1571 (64.0_dp + 4.0_dp*real(SIZE(image%radial_envelope), dp)* &
1572 REAL(size(ao_weight), dp)))
1573
1574 END SUBROUTINE prepare_ao_radial_envelope
1575
1576! **************************************************************************************************
1577!> \brief Bound Eq. (1) outside a sphere using max_{r>=r_min} r^n exp(-alpha*r²).
1578!> The maximum is at r²=max(r_min²,n/(2*alpha)); far-field polynomials share one exponential.
1579!> \param image ...
1580!> \param distance_squared ...
1581!> \return ...
1582! **************************************************************************************************
1583 PURE FUNCTION bound_ao_image_distance(image, distance_squared) RESULT(bound)
1584 TYPE(ao_image_type), INTENT(IN) :: image
1585 REAL(dp), INTENT(IN) :: distance_squared
1586 REAL(dp) :: bound
1587
1588 INTEGER :: idegree, iexponent, lmax
1589 REAL(dp) :: exponent, polynomial, radius, &
1590 radius_squared
1591
1592 bound = 0.0_dp
1593 lmax = ubound(image%radial_envelope, 1)
1594 radius = sqrt(distance_squared)
1595 DO iexponent = 1, SIZE(image%envelope_exponents)
1596 exponent = image%envelope_exponents(iexponent)
1597 IF (exponent*distance_squared >= 0.5_dp*real(lmax, dp)) THEN
1598 polynomial = image%radial_envelope(lmax, iexponent)
1599 DO idegree = lmax - 1, 0, -1
1600 polynomial = image%radial_envelope(idegree, iexponent) + radius*polynomial
1601 END DO
1602 bound = bound + polynomial*exp(-exponent*distance_squared)
1603 ELSE
1604 DO idegree = 0, lmax
1605 radius_squared = max(distance_squared, 0.5_dp*real(idegree, dp)/exponent)
1606 bound = bound + image%radial_envelope(idegree, iexponent)* &
1607 radius_squared**(0.5_dp*real(idegree, dp))*exp(-exponent*radius_squared)
1608 END DO
1609 END IF
1610 END DO
1611 END FUNCTION bound_ao_image_distance
1612
1613! **************************************************************************************************
1614!> \brief Evaluate Eq. (1), then square, on one quadrature block.
1615!> The AO image (A, R) on the lattice copy t of a block equals the image (A, R - t) on the class
1616!> block, so its values come from the class cache. The threads first fill the cache, one image
1617!> each, then sum Eq. (1) on separate point slices.
1618!> \param images ...
1619!> \param points ...
1620!> \param point_index index of each point in its complete quadrature block
1621!> \param copy lattice copy t of the quadrature block
1622!> \param cache AO values of the block class, shared by its copies
1623!> \param field_squared ...
1624!> \param tolerance ...
1625! **************************************************************************************************
1626 SUBROUTINE evaluate_rho_l_squared(images, points, point_index, copy, cache, field_squared, tolerance)
1627 TYPE(ao_image_type), INTENT(IN) :: images(:)
1628 REAL(dp), INTENT(IN) :: points(:, :)
1629 INTEGER, INTENT(IN) :: point_index(:), copy(3)
1630 TYPE(block_class_cache_type), INTENT(INOUT) :: cache
1631 REAL(dp), INTENT(OUT) :: field_squared(:, :)
1632 REAL(dp), INTENT(IN) :: tolerance
1633
1634 CHARACTER(LEN=*), PARAMETER :: routinen = 'evaluate_rho_l_squared'
1635
1636 INTEGER :: first, handle, ig, iimage, islot, last, &
1637 nao, ng, nimages, nmissing, nslice, &
1638 nthreads, row, u(3)
1639 INTEGER, ALLOCATABLE :: missing_indices(:)
1640 REAL(dp), ALLOCATABLE :: values(:, :)
1641 LOGICAL :: image_needed(size(images))
1642 INTEGER :: image_slot(size(images))
1643
1644 CALL timeset(routinen, handle)
1645
1646 ng = SIZE(points, 2)
1647 nimages = 0
1648 DO iimage = 1, SIZE(images)
1649 IF (.NOT. ALLOCATED(images(iimage)%primary)) EXIT
1650 nimages = nimages + 1
1651 END DO
1652 CALL screen_ao_images(images(1:nimages), points, tolerance, image_needed(1:nimages))
1653 IF (.NOT. any(image_needed(1:nimages))) THEN
1654 field_squared = 0.0_dp
1655 CALL timestop(handle)
1656 RETURN
1657 END IF
1658 image_slot = 0
1659 !$OMP PARALLEL DEFAULT(NONE) &
1660 !$OMP SHARED(images, points, point_index, copy, cache, ng, nimages, image_needed, image_slot) &
1661 !$OMP PRIVATE(ig, iimage, islot, nao, nmissing, row, u, missing_indices, values)
1662 ALLOCATE (missing_indices(ng))
1663 !$OMP DO SCHEDULE(DYNAMIC)
1664 DO iimage = 1, nimages
1665 IF (.NOT. image_needed(iimage)) cycle
1666 u = images(iimage)%cell - copy
1667 ! Only this thread handles the image, so only the slot counter is shared.
1668 islot = cache%slot(u(1), u(2), u(3), images(iimage)%atom)
1669 IF (islot == 0) THEN
1670 !$OMP CRITICAL(d_lP_ao_cache)
1671 IF (cache%n_used < cache%n_slots) THEN
1672 cache%n_used = cache%n_used + 1
1673 islot = cache%n_used
1674 cache%keys(:, islot) = [u, images(iimage)%atom]
1675 END IF
1676 !$OMP END CRITICAL(d_lP_ao_cache)
1677 IF (islot == 0) cycle
1678 cache%slot(u(1), u(2), u(3), images(iimage)%atom) = islot
1679 cache%evaluated(:, islot) = .false.
1680 END IF
1681 image_slot(iimage) = islot
1682 nmissing = 0
1683 DO ig = 1, ng
1684 IF (.NOT. cache%evaluated(point_index(ig), islot)) THEN
1685 nmissing = nmissing + 1
1686 missing_indices(nmissing) = ig
1687 END IF
1688 END DO
1689 IF (nmissing == 0) cycle
1690 nao = SIZE(images(iimage)%primary, 2)
1691 ALLOCATE (values(nmissing, nao))
1692 CALL evaluate_mu_r_values(values, points(:, missing_indices(1:nmissing)), &
1693 images(iimage)%basis, images(iimage)%center)
1694 DO row = 1, nmissing
1695 cache%values(1:nao, point_index(missing_indices(row)), islot) = values(row, :)
1696 cache%evaluated(point_index(missing_indices(row)), islot) = .true.
1697 END DO
1698 DEALLOCATE (values)
1699 END DO
1700 !$OMP END DO
1701 DEALLOCATE (missing_indices)
1702 !$OMP END PARALLEL
1703 nthreads = 1
1704!$ nthreads = omp_get_max_threads()
1705 nslice = max(1, (ng + nthreads - 1)/nthreads)
1706 !$OMP PARALLEL DO DEFAULT(NONE) SCHEDULE(STATIC) &
1707 !$OMP SHARED(images, points, point_index, cache, field_squared, ng, nslice, nimages, image_slot, tolerance) &
1708 !$OMP PRIVATE(first, last)
1709 DO first = 1, ng, nslice
1710 last = min(ng, first + nslice - 1)
1711 CALL evaluate_rho_l_squared_slice(images(1:nimages), points(:, first:last), point_index(first:last), &
1712 image_slot(1:nimages), cache, field_squared(first:last, :), tolerance)
1713 END DO
1714 !$OMP END PARALLEL DO
1715
1716 CALL timestop(handle)
1717
1718 END SUBROUTINE evaluate_rho_l_squared
1719
1720! **************************************************************************************************
1721!> \brief Select the AO images a set of points needs for the squared-field error bound.
1722!> For image bounds b_i and A=sum_i b_i, omit at most delta=sqrt(A²+epsilon)-A in amplitude;
1723!> then 2A*delta+delta²<=epsilon. Points with A²<=epsilon need no image. A nonpositive tolerance
1724!> keeps every image. A subset of the points needs a subset of the images.
1725!> \param images ...
1726!> \param points ...
1727!> \param tolerance ...
1728!> \param image_needed ...
1729! **************************************************************************************************
1730 SUBROUTINE screen_ao_images(images, points, tolerance, image_needed)
1731 TYPE(ao_image_type), INTENT(IN) :: images(:)
1732 REAL(dp), INTENT(IN) :: points(:, :), tolerance
1733 LOGICAL, INTENT(OUT) :: image_needed(:)
1734
1735 INTEGER :: iimage, nimages
1736 REAL(dp) :: amplitude, delta, lower(3), &
1737 root_tolerance, upper(3), &
1738 image_bounds(size(images))
1739
1740 image_needed = .true.
1741 IF (tolerance <= 0.0_dp) RETURN
1742 nimages = SIZE(images)
1743 lower = minval(points, dim=2)
1744 upper = maxval(points, dim=2)
1745 DO iimage = 1, nimages
1746 image_bounds(iimage) = images(iimage)%bound* &
1747 exp(-images(iimage)%decay*distance_to_box_squared(images(iimage)%center, lower, upper))
1748 END DO
1749 amplitude = sum(image_bounds)*(1.0_dp + real(nimages, dp)*epsilon(1.0_dp))
1750 root_tolerance = sqrt(tolerance)
1751 IF (amplitude <= root_tolerance) THEN
1752 image_needed = .false.
1753 RETURN
1754 END IF
1755 delta = (tolerance/amplitude)/(sqrt(1.0_dp + (root_tolerance/amplitude)**2) + 1.0_dp)
1756 delta = delta/real(max(1, nimages), dp)
1757 DO iimage = 1, nimages
1758 IF (image_bounds(iimage) <= delta) THEN
1759 image_needed(iimage) = .false.
1760 ELSE
1761 image_needed(iimage) = bound_ao_image_distance( &
1762 images(iimage), distance_to_box_squared(images(iimage)%center, lower, upper)) &
1763 > delta
1764 END IF
1765 END DO
1766
1767 END SUBROUTINE screen_ao_images
1768
1769! **************************************************************************************************
1770!> \brief Evaluate Eq. (1), then square, on a slice of a quadrature block.
1771!> Images with a cache slot take their values from the class cache, the others are evaluated.
1772!> \param images ...
1773!> \param points ...
1774!> \param point_index index of each point in its complete quadrature block
1775!> \param image_slot cache slot of each image, or 0
1776!> \param cache AO values of the block class
1777!> \param field_squared ...
1778!> \param tolerance ...
1779! **************************************************************************************************
1780 SUBROUTINE evaluate_rho_l_squared_slice(images, points, point_index, image_slot, cache, field_squared, &
1781 tolerance)
1782 TYPE(ao_image_type), INTENT(IN) :: images(:)
1783 REAL(dp), INTENT(IN) :: points(:, :)
1784 INTEGER, INTENT(IN) :: point_index(:), image_slot(:)
1785 TYPE(block_class_cache_type), INTENT(IN) :: cache
1786 REAL(dp), INTENT(OUT) :: field_squared(:, :)
1787 REAL(dp), INTENT(IN) :: tolerance
1788
1789 INTEGER :: ig, iimage, il, nao, ng, nl, nprimary
1790 REAL(dp), ALLOCATABLE :: field(:, :), image_field(:, :), &
1791 values(:, :)
1792 LOGICAL :: image_needed(size(images))
1793
1794 ng = SIZE(points, 2)
1795 nl = SIZE(field_squared, 2)
1796 CALL screen_ao_images(images, points, tolerance, image_needed)
1797 ALLOCATE (field(ng, nl), source=0.0_dp)
1798 DO iimage = 1, SIZE(images)
1799 IF (.NOT. image_needed(iimage)) cycle
1800 nao = SIZE(images(iimage)%primary, 2)
1801 ALLOCATE (values(ng, nao))
1802 IF (image_slot(iimage) > 0) THEN
1803 DO ig = 1, ng
1804 values(ig, :) = cache%values(1:nao, point_index(ig), image_slot(iimage))
1805 END DO
1806 ELSE
1807 CALL evaluate_mu_r_values(values, points, images(iimage)%basis, images(iimage)%center)
1808 END IF
1809 nprimary = SIZE(images(iimage)%primary, 1)
1810 IF (ALLOCATED(images(iimage)%primary_l)) THEN
1811 IF (.NOT. ALLOCATED(image_field)) ALLOCATE (image_field(ng, nl))
1812 CALL dgemm('N', 'T', ng, nprimary, nao, 1.0_dp, values, ng, &
1813 images(iimage)%primary, nprimary, 0.0_dp, image_field, ng)
1814 DO il = 1, nprimary
1815 field(:, images(iimage)%primary_l(il)) = &
1816 field(:, images(iimage)%primary_l(il)) + image_field(:, il)
1817 END DO
1818 ELSE
1819 CALL dgemm('N', 'T', ng, nl, nao, 1.0_dp, values, ng, &
1820 images(iimage)%primary, nl, 1.0_dp, field, ng)
1821 END IF
1822 DEALLOCATE (values)
1823 END DO
1824 field_squared = field**2
1825
1826 END SUBROUTINE evaluate_rho_l_squared_slice
1827
1828! **************************************************************************************************
1829!> \brief Evaluate the AO values in Eq. (1), sharing primitive factors before contraction.
1830!> Columns of primitive are Cartesian Gaussian primitives, grouped by basis set and exponent.
1831!> The original spherical contraction coefficients define exactly the same unscreened AO values.
1832!> \param values ...
1833!> \param points ...
1834!> \param basis ...
1835!> \param center ...
1836! **************************************************************************************************
1837 SUBROUTINE evaluate_mu_r_values(values, points, basis, center)
1838 REAL(dp), INTENT(OUT) :: values(:, :)
1839 REAL(dp), INTENT(IN) :: points(:, :)
1840 TYPE(gto_basis_set_type), POINTER :: basis
1841 REAL(dp), INTENT(IN) :: center(3)
1842
1843 INTEGER :: first_sgf, ico, ig, ipgf, ipower, iset, &
1844 last_sgf, ncart, ng, nprimitive
1845 INTEGER, ALLOCATABLE :: offsets(:)
1846 LOGICAL :: parallel_points
1847 REAL(dp) :: relative(3)
1848 REAL(dp), ALLOCATABLE :: coordinate_power(:, :), exponential(:), &
1849 monomial(:, :), primitive(:, :), &
1850 radius2(:)
1851
1852 ng = SIZE(points, 2)
1853 IF (ng == 0) RETURN
1854 ALLOCATE (offsets(basis%nset), monomial(ng, ncoset(maxval(basis%lmax))), &
1855 coordinate_power(3, 0:maxval(basis%lmax)), radius2(ng), exponential(ng))
1856 nprimitive = 0
1857 DO iset = 1, basis%nset
1858 offsets(iset) = nprimitive
1859 nprimitive = nprimitive + basis%npgf(iset)*ncoset(basis%lmax(iset))
1860 END DO
1861 ALLOCATE (primitive(ng, nprimitive))
1862 parallel_points = .true.
1863!$ parallel_points = .NOT. omp_in_parallel()
1864 !$OMP PARALLEL DEFAULT(NONE) IF(parallel_points) &
1865 !$OMP SHARED(ng, points, center, basis, primitive, offsets, ncoset, indco, radius2, monomial) &
1866 !$OMP PRIVATE(ig, relative, ico, coordinate_power, ipower, iset, ipgf, ncart, exponential)
1867 !$OMP DO SCHEDULE(STATIC)
1868 DO ig = 1, ng
1869 relative = points(:, ig) - center
1870 radius2(ig) = dot_product(relative, relative)
1871 coordinate_power(:, 0) = 1.0_dp
1872 DO ipower = 1, ubound(coordinate_power, 2)
1873 coordinate_power(:, ipower) = coordinate_power(:, ipower - 1)*relative
1874 END DO
1875 DO ico = 1, SIZE(monomial, 2)
1876 monomial(ig, ico) = coordinate_power(1, indco(1, ico))* &
1877 coordinate_power(2, indco(2, ico))*coordinate_power(3, indco(3, ico))
1878 END DO
1879 END DO
1880 !$OMP END DO
1881 ! Fill primitive columns contiguously in g, reusing each Cartesian monomial.
1882 DO iset = 1, basis%nset
1883 ncart = ncoset(basis%lmax(iset))
1884 !$OMP DO SCHEDULE(STATIC)
1885 DO ipgf = 1, basis%npgf(iset)
1886 !$OMP SIMD
1887 DO ig = 1, ng
1888 exponential(ig) = exp(-basis%zet(ipgf, iset)*radius2(ig))
1889 END DO
1890 DO ico = 1, ncart
1891 !$OMP SIMD
1892 DO ig = 1, ng
1893 primitive(ig, offsets(iset) + (ipgf - 1)*ncart + ico) = monomial(ig, ico)*exponential(ig)
1894 END DO
1895 END DO
1896 END DO
1897 !$OMP END DO
1898 END DO
1899 !$OMP END PARALLEL
1900 values = 0.0_dp
1901 DO iset = 1, basis%nset
1902 nprimitive = basis%npgf(iset)*ncoset(basis%lmax(iset))
1903 first_sgf = basis%first_sgf(1, iset)
1904 last_sgf = basis%last_sgf(basis%nshell(iset), iset)
1905 CALL dgemm('N', 'N', ng, last_sgf - first_sgf + 1, nprimitive, &
1906 1.0_dp, primitive(:, offsets(iset) + 1:), ng, &
1907 basis%sphi(:, first_sgf:last_sgf), SIZE(basis%sphi, 1), &
1908 1.0_dp, values(:, first_sgf:last_sgf), ng)
1909 END DO
1910
1911 END SUBROUTINE evaluate_mu_r_values
1912
1913! **************************************************************************************************
1914!> \brief Bound every auxiliary potential in Eq. (2), including contracted angular functions.
1915!> |x^n exp(-a x²/2)| <= (n/(a e))^(n/2); the remaining Gaussian potential is <=4 pi/a.
1916!> The triangle inequality uses the contracted primitive coefficients of each function.
1917!> Truncating the positive kernel preserves the bound; envelopes are indexed (bound/exponents,atom).
1918!> \param bs_env ...
1919!> \param aux_potentials ...
1920!> \param envelopes ...
1921! **************************************************************************************************
1922 SUBROUTINE auxiliary_potential_bound(bs_env, aux_potentials, envelopes)
1923 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
1924 TYPE(radial_interpolation_type), ALLOCATABLE, &
1925 INTENT(IN) :: aux_potentials(:)
1926 REAL(dp), ALLOCATABLE, INTENT(OUT) :: envelopes(:, :)
1927
1928 INTEGER :: iatom, ico, idir, ikind, ipgf, &
1929 irow_packed, iset, ishell, l, power
1930 LOGICAL, ALLOCATABLE :: angular_used(:)
1931 REAL(dp) :: exponent, factor
1932 REAL(dp), ALLOCATABLE :: bounds(:)
1933 TYPE(gto_basis_set_type), POINTER :: basis
1934 TYPE(particle_type), POINTER :: particles(:)
1935
1936 particles => bs_env%ri_rs%particle_set
1937 ALLOCATE (envelopes(3, bs_env%n_atom))
1938 envelopes = 0.0_dp
1939 DO iatom = 1, bs_env%n_atom
1940 ikind = particles(iatom)%atomic_kind%kind_number
1941 basis => bs_env%basis_set_RI(ikind)%gto_basis_set
1942 cpassert(ASSOCIATED(basis))
1943 ALLOCATE (bounds(SIZE(aux_potentials(iatom)%coefficients, 2)))
1944 ALLOCATE (angular_used(0:maxval(basis%lmax)))
1945 bounds = 0.0_dp
1946 irow_packed = 0
1947 envelopes(2, iatom) = huge(1.0_dp)
1948 DO iset = 1, basis%nset
1949 angular_used = .false.
1950 DO ishell = 1, basis%nshell(iset)
1951 angular_used(basis%l(ishell, iset)) = .true.
1952 END DO
1953 DO ipgf = 1, basis%npgf(iset)
1954 exponent = basis%zet(ipgf, iset)
1955 envelopes(2, iatom) = min(envelopes(2, iatom), exponent)
1956 envelopes(3, iatom) = max(envelopes(3, iatom), exponent)
1957 DO l = 0, basis%lmax(iset)
1958 IF (.NOT. angular_used(l)) cycle
1959 DO ico = ncoset(l - 1) + 1, ncoset(l)
1960 irow_packed = irow_packed + 1
1961 factor = 4.0_dp*pi/exponent
1962 DO idir = 1, 3
1963 power = indco(idir, ico)
1964 IF (power > 0) factor = factor*(real(power, dp)/(exponent*exp(1.0_dp)))** &
1965 (0.5_dp*real(power, dp))
1966 END DO
1967 bounds(:) = bounds(:) + abs(aux_potentials(iatom)%coefficients(irow_packed, :))*factor
1968 END DO
1969 END DO
1970 END DO
1971 END DO
1972 envelopes(1, iatom) = maxval(bounds)
1973 DEALLOCATE (bounds, angular_used)
1974 END DO
1975
1976 END SUBROUTINE auxiliary_potential_bound
1977
1978! **************************************************************************************************
1979!> \brief Contract Eqs. (2),(4), screening every spatial l/P-atom/T block separately.
1980!> B bounds every selected potential on this atom; a_min/max bound its primitive exponents.
1981!> For erfc(ωr)/r, |v_P(r)|<=B exp[-a_min ω² |r-center|²/(a_min+2ω²)].
1982!> This follows from erfc(x)<=exp(-x²), completing the square, and the Gaussian Coulomb bound.
1983!> The tabulated radial bounds of prepare_short_potential_bounds tighten it where smaller.
1984!> Half the absolute budget screens common points. The other half is applied independently to
1985!> every d_lP(T): a point is contracted for one l block only when its local product bound exceeds
1986!> the per-point threshold. The exact l/P/T distance criterion is applied before each GEMM.
1987!> \param bs_env ...
1988!> \param cells_R_d_lP Retained integer cell vectors R, indexed (3,R).
1989!> \param include_lP_R ...
1990!> \param include_l_blocks_P_R ...
1991!> \param points ...
1992!> \param point_index index of each point in its complete quadrature block
1993!> \param field_squared ...
1994!> \param weight ...
1995!> \param envelopes ...
1996!> \param blocks ...
1997!> \param threshold ...
1998!> \param copy lattice copy t of the quadrature block
1999!> \param cache potentials of the block class, shared by its copies
2000!> \param potential_points ...
2001!> \param contraction_products ...
2002!> \param d_lP_R Numerical integrals indexed by local point l, RI function P and cell R.
2003!> \param aux_potentials ...
2004! **************************************************************************************************
2005 SUBROUTINE integrate_potentials(bs_env, cells_R_d_lP, include_lP_R, include_l_blocks_P_R, &
2006 points, point_index, field_squared, weight, envelopes, blocks, &
2007 threshold, copy, cache, potential_points, contraction_products, &
2008 d_lP_R, aux_potentials)
2009 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
2010 INTEGER, INTENT(IN) :: cells_r_d_lp(:, :)
2011 LOGICAL, INTENT(IN) :: include_lp_r(:, :, :), &
2012 include_l_blocks_p_r(:, :, :)
2013 REAL(dp), INTENT(IN) :: points(:, :)
2014 INTEGER, INTENT(IN) :: point_index(:)
2015 REAL(dp), INTENT(IN) :: field_squared(:, :), weight, &
2016 envelopes(:, :)
2017 TYPE(l_block_partition_type), INTENT(IN) :: blocks
2018 REAL(dp), INTENT(IN) :: threshold
2019 INTEGER, INTENT(IN) :: copy(3)
2020 TYPE(block_class_cache_type), INTENT(INOUT) :: cache
2021 INTEGER(int_8), INTENT(INOUT) :: potential_points, contraction_products
2022 REAL(dp), INTENT(INOUT) :: d_lp_r(:, :, :)
2023 TYPE(radial_interpolation_type), ALLOCATABLE, &
2024 INTENT(IN) :: aux_potentials(:)
2025
2026 CHARACTER(LEN=*), PARAMETER :: routinen = 'integrate_potentials'
2027
2028 INTEGER :: firstp, handle, iatom, iblock, ig, ikind, ilocal, iradial, islot, it, lastp, &
2029 maxnp, nactive, nmissing, np, nthreads, nunion, row, u(3)
2030 REAL(dp) :: center(3), decay, field_max, &
2031 points_lower(3), points_upper(3)
2032 REAL(dp), ALLOCATABLE :: block_field_bound(:, :), block_ri(:, :), field_work(:, :), &
2033 maximum_field(:), point_bound(:), potential(:, :), potential_t(:, :)
2034 LOGICAL :: point_needed(size(points, 2))
2035 INTEGER :: union_indices(size(points, 2)), missing_indices(size(points, 2))
2036 TYPE(cell_type), POINTER :: cell
2037 TYPE(gto_basis_set_type), POINTER :: basis
2038 LOGICAL :: block_needed(blocks%nblocks)
2039 INTEGER :: block_active_last(blocks%nblocks), block_active_first(blocks%nblocks)
2040 TYPE(particle_type), POINTER :: particles(:)
2041 INTEGER :: active_l(bs_env%ri_rs%n_grid_points)
2042
2043 CALL timeset(routinen, handle)
2044
2045 cell => bs_env%ri_rs%cell
2046 particles => bs_env%ri_rs%particle_set
2047 maxnp = maxval(bs_env%i_RI_end_from_atom - bs_env%i_RI_start_from_atom + 1)
2048
2049 ALLOCATE (block_field_bound(SIZE(points, 2), blocks%nblocks))
2050
2051 DO iblock = 1, blocks%nblocks
2052 block_field_bound(:, iblock) = 0.0_dp
2053 DO ilocal = blocks%first(iblock), blocks%first(iblock + 1) - 1
2054 DO ig = 1, SIZE(points, 2)
2055 block_field_bound(ig, iblock) = max(block_field_bound(ig, iblock), &
2056 abs(field_squared(ig, blocks%indices(ilocal))))
2057 END DO
2058 END DO
2059 END DO
2060
2061 field_max = maxval(block_field_bound)
2062 points_lower = minval(points, dim=2)
2063 points_upper = maxval(points, dim=2)
2064
2065 ! Bound the additional thread workspaces to 128 MiB per MPI rank.
2066 ! A single thread retains the original memory bound for large global l grids.
2067 nthreads = 1
2068!$ nthreads = omp_get_max_threads()
2069 nthreads = min(nthreads, max(1, int(thread_workspace_bytes/ &
2070 (real_bytes*(real(bs_env%ri_rs%n_grid_points, dp)*(maxnp + SIZE(points, 2)) + &
2071 2.0_dp*real(SIZE(points, 2), dp)*maxnp)))))
2072 ! Each atom/image owns disjoint P/T columns of Eq. (2).
2073
2074 !$OMP PARALLEL DEFAULT(NONE) NUM_THREADS(nthreads) &
2075 !$OMP SHARED(bs_env, cells_R_d_lP, points, point_index, field_squared, weight, envelopes, &
2076 !$OMP blocks, threshold, copy, cache, d_lP_R, aux_potentials, cell, particles, &
2077 !$OMP maxnp, block_field_bound, field_max, points_lower, points_upper, include_lP_R, &
2078 !$OMP include_l_blocks_P_R) &
2079 !$OMP PRIVATE(firstp, iatom, iblock, ig, ikind, ilocal, iradial, islot, it, lastp, &
2080 !$OMP nactive, nmissing, np, nunion, row, u, center, decay, basis, &
2081 !$OMP block_RI, field_work, maximum_field, point_bound, potential, potential_t, point_needed, &
2082 !$OMP union_indices, missing_indices, &
2083 !$OMP active_l, block_needed, block_active_last, block_active_first) &
2084 !$OMP REDUCTION(+:potential_points, contraction_products)
2085
2086 ALLOCATE (block_ri(bs_env%ri_rs%n_grid_points, maxnp), &
2087 field_work(SIZE(points, 2), bs_env%ri_rs%n_grid_points), point_bound(SIZE(points, 2)), &
2088 maximum_field(SIZE(points, 2)), potential(SIZE(points, 2), maxnp), &
2089 potential_t(maxnp, SIZE(points, 2)))
2090
2091 !$OMP DO COLLAPSE(2) SCHEDULE(DYNAMIC)
2092 DO it = 1, SIZE(cells_r_d_lp, 2)
2093 DO iatom = 1, bs_env%n_atom
2094 ikind = particles(iatom)%atomic_kind%kind_number
2095 basis => bs_env%basis_set_RI(ikind)%gto_basis_set
2096 firstp = bs_env%i_RI_start_from_atom(iatom)
2097 lastp = bs_env%i_RI_end_from_atom(iatom)
2098 np = lastp - firstp + 1
2099 center = pbc(particles(iatom)%r, cell) + matmul(bs_env%hmat, real(cells_r_d_lp(:, it), dp))
2100 IF (.NOT. any(include_l_blocks_p_r(:, iatom, it))) cycle
2101 ! The block of the lattice copy T' sees V_P around R_A + hT at U = T - T'.
2102 u = cells_r_d_lp(:, it) - copy
2103 islot = cache%slot(u(1), u(2), u(3), iatom)
2104 decay = envelopes(2, iatom)*bs_env%ri_metric%omega**2/ &
2105 (envelopes(2, iatom) + 2.0_dp*bs_env%ri_metric%omega**2)
2106 ! No point bound exceeds the envelope at the block point nearest to the atom image.
2107 IF (field_max*envelopes(1, iatom)* &
2108 exp(-decay*distance_to_box_squared(center, points_lower, points_upper)) <= threshold) THEN
2109 cycle
2110 END IF
2111 DO ig = 1, SIZE(points, 2)
2112 IF (islot > 0) THEN
2113 IF (cache%point_bound(point_index(ig), islot) >= 0.0_dp) THEN
2114 point_bound(ig) = cache%point_bound(point_index(ig), islot)
2115 cycle
2116 END IF
2117 END IF
2118 point_bound(ig) = envelopes(1, iatom)
2119 IF (decay > 0.0_dp) point_bound(ig) = point_bound(ig)* &
2120 exp(-decay*sum((points(:, ig) - center)**2))
2121 IF (ALLOCATED(aux_potentials(iatom)%short_bounds)) THEN
2122 iradial = int(norm2(points(:, ig) - center)/short_bound_spacing)
2123 IF (iradial < n_short_bounds) THEN
2124 point_bound(ig) = min(point_bound(ig), aux_potentials(iatom)%short_bounds(iradial))
2125 END IF
2126 END IF
2127 IF (islot > 0) cache%point_bound(point_index(ig), islot) = point_bound(ig)
2128 END DO
2129 ! A point is needed if it passes for one needed l block, i.e. for their largest field.
2130 block_needed = .false.
2131 maximum_field = 0.0_dp
2132 DO iblock = 1, blocks%nblocks
2133 IF (.NOT. include_l_blocks_p_r(iblock, iatom, it)) cycle
2134 block_needed(iblock) = any(block_field_bound(:, iblock)*point_bound > threshold)
2135 IF (block_needed(iblock)) maximum_field(:) = max(maximum_field, block_field_bound(:, iblock))
2136 END DO
2137 point_needed = maximum_field*point_bound > threshold
2138 nunion = 0
2139 DO ig = 1, SIZE(points, 2)
2140 IF (point_needed(ig)) THEN
2141 nunion = nunion + 1
2142 union_indices(nunion) = ig
2143 END IF
2144 END DO
2145 IF (nunion == 0) cycle
2146 IF (islot == 0) THEN
2147 !$OMP CRITICAL(d_lP_potential_cache)
2148 IF (cache%n_used < cache%n_slots) THEN
2149 cache%n_used = cache%n_used + 1
2150 islot = cache%n_used
2151 cache%keys(:, islot) = [u, iatom]
2152 cache%slot(u(1), u(2), u(3), iatom) = islot
2153 END IF
2154 !$OMP END CRITICAL(d_lP_potential_cache)
2155 IF (islot > 0) THEN
2156 cache%evaluated(:, islot) = .false.
2157 cache%point_bound(:, islot) = -1.0_dp
2158 cache%point_bound(point_index, islot) = point_bound(1:SIZE(points, 2))
2159 END IF
2160 END IF
2161 IF (islot > 0) THEN
2162 ! Evaluate only the points that no earlier copy of the block needed.
2163 nmissing = 0
2164 DO row = 1, nunion
2165 IF (.NOT. cache%evaluated(point_index(union_indices(row)), islot)) THEN
2166 nmissing = nmissing + 1
2167 missing_indices(nmissing) = union_indices(row)
2168 END IF
2169 END DO
2170 IF (nmissing > 0) THEN
2171 CALL contract_selected_potentials(potential(:, 1:np), &
2172 points(:, missing_indices(1:nmissing)), &
2173 basis, center, aux_potentials(iatom))
2174 DO row = 1, nmissing
2175 cache%values(1:np, point_index(missing_indices(row)), islot) = potential(row, 1:np)
2176 cache%evaluated(point_index(missing_indices(row)), islot) = .true.
2177 END DO
2178 END IF
2179 DO row = 1, nunion
2180 potential_t(1:np, row) = cache%values(1:np, point_index(union_indices(row)), islot)
2181 END DO
2182 potential_points = potential_points + int(nmissing, int_8)
2183 ELSE
2184 CALL contract_selected_potentials(potential(:, 1:np), &
2185 points(:, union_indices(1:nunion)), &
2186 basis, center, aux_potentials(iatom))
2187 potential_t(1:np, 1:nunion) = transpose(potential(1:nunion, 1:np))
2188 potential_points = potential_points + int(nunion, int_8)
2189 END IF
2190 nactive = 0
2191 block_active_first = 0
2192 block_active_last = -1
2193 DO iblock = 1, blocks%nblocks
2194 IF (.NOT. block_needed(iblock)) cycle
2195 block_active_first(iblock) = nactive + 1
2196 DO ilocal = blocks%first(iblock), blocks%first(iblock + 1) - 1
2197 IF (include_lp_r(blocks%indices(ilocal), iatom, it)) THEN
2198 nactive = nactive + 1
2199 active_l(nactive) = blocks%indices(ilocal)
2200 END IF
2201 END DO
2202 block_active_last(iblock) = nactive
2203 END DO
2204 DO iblock = 1, blocks%nblocks
2205 IF (.NOT. block_needed(iblock)) cycle
2206 DO row = 1, nunion
2207 ig = union_indices(row)
2208 point_needed(row) = block_field_bound(ig, iblock)*point_bound(ig) > threshold
2209 END DO
2210 DO ilocal = block_active_first(iblock), block_active_last(iblock)
2211 DO row = 1, nunion
2212 field_work(row, ilocal) = merge(field_squared(union_indices(row), active_l(ilocal)), &
2213 0.0_dp, point_needed(row))
2214 END DO
2215 END DO
2216 END DO
2217 CALL dgemm('T', 'T', nactive, np, nunion, weight, field_work, SIZE(field_work, 1), &
2218 potential_t, SIZE(potential_t, 1), 0.0_dp, block_ri, SIZE(block_ri, 1))
2219 DO ilocal = 1, nactive
2220 d_lp_r(active_l(ilocal), firstp:lastp, it) = &
2221 d_lp_r(active_l(ilocal), firstp:lastp, it) + block_ri(ilocal, 1:np)
2222 END DO
2223 contraction_products = contraction_products + &
2224 int(nactive, int_8)*int(np, int_8)*int(nunion, int_8)
2225 END DO
2226 END DO
2227 !$OMP END DO NOWAIT
2228
2229 !$OMP BARRIER
2230
2231 DEALLOCATE (block_ri, field_work, maximum_field, point_bound, potential, potential_t)
2232
2233 !$OMP END PARALLEL
2234
2235 CALL timestop(handle)
2236
2237 END SUBROUTINE integrate_potentials
2238
2239! **************************************************************************************************
2240!> \brief Squared distance from a point to an axis-aligned spatial l block.
2241!> \param point ...
2242!> \param lower ...
2243!> \param upper ...
2244!> \return ...
2245! **************************************************************************************************
2246 PURE FUNCTION distance_to_box_squared(point, lower, upper) RESULT(distance_squared)
2247 REAL(dp), INTENT(IN) :: point(3), lower(3), upper(3)
2248 REAL(dp) :: distance_squared
2249
2250 INTEGER :: idir
2251 REAL(dp) :: delta
2252
2253 distance_squared = 0.0_dp
2254 DO idir = 1, 3
2255 delta = max(lower(idir) - point(idir), point(idir) - upper(idir), 0.0_dp)
2257 END DO
2258 END FUNCTION distance_to_box_squared
2259
2260! **************************************************************************************************
2261!> \brief Transform spherical coefficients once; bound the radial tables by radial_table_bytes.
2262!> \param basis ...
2263!> \param U_Pp ...
2264!> \param contracted ...
2265!> \param attenuation ...
2266!> \param radial_table_bytes ...
2267!> \param mesh_step ...
2268!> \param mesh_displacement ...
2269! **************************************************************************************************
2270 SUBROUTINE prepare_contracted_potential(basis, U_Pp, contracted, attenuation, radial_table_bytes, &
2271 mesh_step, mesh_displacement)
2272 TYPE(gto_basis_set_type), POINTER :: basis
2273 REAL(dp), INTENT(IN) :: u_pp(:, :)
2274 TYPE(radial_interpolation_type), INTENT(OUT) :: contracted
2275 REAL(dp), INTENT(IN) :: attenuation, radial_table_bytes, &
2276 mesh_step(3, 3), mesh_displacement(3)
2277
2278 INTEGER :: ico, iopt, ipgf, irow, irow_packed, &
2279 iset, isgf, ishell, l, ncart, &
2280 ninterval, np, nrows
2281 LOGICAL, ALLOCATABLE :: angular_used(:)
2282 REAL(dp) :: bytes_per_node, table_bytes
2283 REAL(dp), ALLOCATABLE :: full_abscissae(:), moments(:, :)
2284
2285 ALLOCATE (angular_used(0:maxval(basis%lmax)))
2286 cpassert(SIZE(u_pp, 1) == basis%nsgf)
2287 np = SIZE(u_pp, 2)
2288 nrows = 0
2289 DO iset = 1, basis%nset
2290 angular_used = .false.
2291 DO ishell = 1, basis%nshell(iset)
2292 angular_used(basis%l(ishell, iset)) = .true.
2293 END DO
2294 DO l = 0, basis%lmax(iset)
2295 IF (angular_used(l)) nrows = nrows + basis%npgf(iset)*(ncoset(l) - ncoset(l - 1))
2296 END DO
2297 END DO
2298 ! W_g,ia is independent of the contracted shell: sum its coefficients before evaluating W.
2299 ! Rows enumerate (set, primitive, distinct angular momentum, Cartesian monomial).
2300 ALLOCATE (contracted%coefficients(nrows, np), source=0.0_dp)
2301 irow_packed = 0
2302 DO iset = 1, basis%nset
2303 ncart = ncoset(basis%lmax(iset))
2304 angular_used = .false.
2305 DO ishell = 1, basis%nshell(iset)
2306 angular_used(basis%l(ishell, iset)) = .true.
2307 END DO
2308 DO ipgf = 1, basis%npgf(iset)
2309 DO l = 0, basis%lmax(iset)
2310 IF (.NOT. angular_used(l)) cycle
2311 DO ico = ncoset(l - 1) + 1, ncoset(l)
2312 irow = (ipgf - 1)*ncart + ico
2313 irow_packed = irow_packed + 1
2314 DO ishell = 1, basis%nshell(iset)
2315 IF (basis%l(ishell, iset) /= l) cycle
2316 DO isgf = basis%first_sgf(ishell, iset), basis%last_sgf(ishell, iset)
2317 DO iopt = 1, np
2318 contracted%coefficients(irow_packed, iopt) = &
2319 contracted%coefficients(irow_packed, iopt) + basis%sphi(irow, isgf)*u_pp(isgf, iopt)
2320 END DO
2321 END DO
2322 END DO
2323 END DO
2324 END DO
2325 END DO
2326 END DO
2327 DEALLOCATE (angular_used)
2328 IF (basis%nsgf < nrows) CALL prepare_spherical_potential_factorization(basis, u_pp, contracted)
2329 contracted%attenuation = max(0.0_dp, attenuation)
2330 IF (contracted%attenuation > 0.0_dp) THEN
2331 CALL prepare_short_potential_bounds(basis, contracted%coefficients, contracted%attenuation, &
2332 contracted%short_bounds)
2333 END IF
2334 CALL prepare_radial_series(basis, contracted%attenuation, contracted%series, contracted%short_series)
2335 table_bytes = max(0.0_dp, radial_table_bytes)
2336 ! Budget the actual logarithmic nodes, including one primitive preparation buffer.
2337 bytes_per_node = real_bytes*real(maxval(basis%lmax) + 7, dp)* &
2338 REAL(maxval(basis%npgf)*basis%nset + 1, dp)
2339 CALL prepare_short_radial_grid(short_grid_xmax, contracted%short_abscissae)
2340 table_bytes = max(0.0_dp, table_bytes - real_bytes*real(SIZE(contracted%short_abscissae), dp))
2341 ninterval = int(min(real(SIZE(contracted%short_abscissae), dp), &
2342 table_bytes/bytes_per_node)) - 1
2343 IF (ninterval < 1) THEN
2344 DEALLOCATE (contracted%short_abscissae)
2345 RETURN
2346 END IF
2347 IF (ninterval < ubound(contracted%short_abscissae, 1)) THEN
2348 CALL move_alloc(contracted%short_abscissae, full_abscissae)
2349 ALLOCATE (contracted%short_abscissae(0:ninterval))
2350 contracted%short_abscissae(:) = full_abscissae(0:ninterval)
2351 DEALLOCATE (full_abscissae)
2352 END IF
2353 ALLOCATE (contracted%short_moments(0:maxval(basis%lmax) + 6, &
2354 0:ninterval, maxval(basis%npgf), basis%nset))
2355 DO iset = 1, basis%nset
2356 DO ipgf = 1, basis%npgf(iset)
2357 CALL prepare_short_radial_table(basis%zet(ipgf, iset), contracted%attenuation, &
2358 maxval(basis%lmax), moments, ninterval, abscissae=contracted%short_abscissae)
2359 contracted%short_moments(:, :, ipgf, iset) = moments
2360 DEALLOCATE (moments)
2361 END DO
2362 END DO
2363 ! Keep the generic radial lookup at all radii before using remaining storage for mesh nodes.
2364 table_bytes = max(0.0_dp, table_bytes - real_bytes*real(SIZE(contracted%short_moments, kind=int_8), dp))
2365 CALL prepare_mesh_radial_moments(basis, contracted, mesh_step, mesh_displacement, table_bytes)
2366
2367 END SUBROUTINE prepare_contracted_potential
2368
2369! **************************************************************************************************
2370!> \brief Bound every contracted erfc potential over radial intervals without cancellation.
2371!> H_l(r) = (2 pi/a) int_sqrt(b)^1 t^(2l) exp(-a r^2 t^2) dt, b = omega^2/(a+omega^2), decreases with r.
2372!> On [r_lo, r_hi], |x^i y^j z^k| <= r_hi^l prod_d (n_d/l)^(n_d/2), l = i+j+k. Upper rectangle sums
2373!> at the maxima of the integrand bound H_l(r_lo); summing absolute contracted coefficients then
2374!> bounds every P and every direction. For each homogeneous polynomial, Cauchy also gives
2375!> |sum_alpha c_alpha r^alpha| <= r^l sqrt(sum_alpha c_alpha^2/multinomial(l, alpha)); the smaller
2376!> angular bound is taken. Points beyond the last interval keep the Gaussian envelope bound.
2377!> \param basis ...
2378!> \param coefficients contracted Cartesian primitive rows of the RI functions P
2379!> \param omega attenuation of the erfc kernel
2380!> \param radial_bounds bound on |V_P(r)| for all P and r in each interval
2381! **************************************************************************************************
2382 SUBROUTINE prepare_short_potential_bounds(basis, coefficients, omega, radial_bounds)
2383 TYPE(gto_basis_set_type), POINTER :: basis
2384 REAL(dp), INTENT(IN) :: coefficients(:, :), omega
2385 REAL(dp), ALLOCATABLE, INTENT(OUT) :: radial_bounds(:)
2386
2387 INTEGER, PARAMETER :: n_rectangles = 32
2388
2389 INTEGER :: ico, idir, ifactor, il, ipgf, ipower, &
2390 iq, ir, irow, iset, ishell, istart
2391 REAL(dp) :: a, angular, factor, integral, lower, &
2392 multinomial, peak, rhi, rlo, thi, tlo, &
2393 width, x
2394 REAL(dp), DIMENSION(SIZE(coefficients, 2)) :: bounds, cscale, sumsq, triangle
2395 LOGICAL :: angular_used(0:maxval(basis%lmax))
2396
2397 ALLOCATE (radial_bounds(0:n_short_bounds - 1))
2398 DO ir = 0, n_short_bounds - 1
2399 rlo = real(ir, dp)*short_bound_spacing
2400 rhi = real(ir + 1, dp)*short_bound_spacing
2401 bounds = 0.0_dp
2402 irow = 0
2403 DO iset = 1, basis%nset
2404 angular_used = .false.
2405 DO ishell = 1, basis%nshell(iset)
2406 angular_used(basis%l(ishell, iset)) = .true.
2407 END DO
2408 DO ipgf = 1, basis%npgf(iset)
2409 a = basis%zet(ipgf, iset)
2410 lower = sqrt(omega**2/(a + omega**2))
2411 width = 1.0_dp - lower
2412 x = a*rlo**2
2413 DO il = 0, basis%lmax(iset)
2414 IF (.NOT. angular_used(il)) cycle
2415 integral = 0.0_dp
2416 DO iq = 0, n_rectangles - 1
2417 tlo = lower + width*real(iq, dp)/real(n_rectangles, dp)
2418 thi = lower + width*real(iq + 1, dp)/real(n_rectangles, dp)
2419 peak = thi
2420 IF (x > 0.0_dp) peak = max(tlo, min(thi, sqrt(real(il, dp)/x)))
2421 integral = integral + width/real(n_rectangles, dp)*peak**(2*il)*exp(-x*peak**2)
2422 END DO
2423 factor = 2.0_dp*pi/a*integral*rhi**il
2424 istart = irow
2425 cscale = 0.0_dp
2426 DO ico = 1, ncoset(il) - ncoset(il - 1)
2427 cscale = max(cscale, abs(coefficients(istart + ico, :)))
2428 END DO
2429 triangle = 0.0_dp
2430 sumsq = 0.0_dp
2431 DO ico = ncoset(il - 1) + 1, ncoset(il)
2432 angular = 1.0_dp
2433 multinomial = 1.0_dp
2434 DO ifactor = 2, il
2435 multinomial = multinomial*real(ifactor, dp)
2436 END DO
2437 DO idir = 1, 3
2438 ipower = indco(idir, ico)
2439 IF (ipower > 0) angular = angular*(real(ipower, dp)/real(il, dp))**(0.5_dp*real(ipower, dp))
2440 DO ifactor = 2, ipower
2441 multinomial = multinomial/real(ifactor, dp)
2442 END DO
2443 END DO
2444 irow = irow + 1
2445 triangle = triangle + abs(coefficients(irow, :))*angular
2446 sumsq = sumsq + (coefficients(irow, :)/max(tiny(1.0_dp), cscale))**2/multinomial
2447 END DO
2448 WHERE (cscale >= tiny(1.0_dp)) triangle = min(triangle, cscale*sqrt(sumsq))
2449 bounds = bounds + triangle*max(tiny(1.0_dp), factor)
2450 END DO
2451 END DO
2452 END DO
2453 cpassert(irow == SIZE(coefficients, 1))
2454 ! Inflate by roundoff.
2455 radial_bounds(ir) = max(tiny(1.0_dp), maxval(bounds)*(1.0_dp + eps_bound_roundoff))
2456 END DO
2457
2458 END SUBROUTINE prepare_short_potential_bounds
2459
2460! **************************************************************************************************
2461!> \brief Factor sphi into a common angular map and shell/primitive radial scales when exact.
2462!> This is checked against every spherical coefficient; arbitrary contracted or modified bases
2463!> retain the original Cartesian path if the factorization does not hold.
2464!> \param basis ...
2465!> \param U_Pp ...
2466!> \param contracted ...
2467! **************************************************************************************************
2468 SUBROUTINE prepare_spherical_potential_factorization(basis, U_Pp, contracted)
2469 TYPE(gto_basis_set_type), POINTER :: basis
2470 REAL(dp), INTENT(IN) :: u_pp(:, :)
2471 TYPE(radial_interpolation_type), INTENT(INOUT) :: contracted
2472
2473 INTEGER :: c, first, ico, ipgf, iset, ishell, l, &
2474 lmax, m, ncart, ref_c, ref_m, ref_p, &
2475 ref_shell, row
2476 LOGICAL, ALLOCATABLE :: used(:)
2477 REAL(dp) :: actual, expected, ref_abs, ref_value, &
2478 scale
2479 REAL(dp), ALLOCATABLE :: angular_coeff(:, :, :, :), &
2480 primitive_scale(:, :, :)
2481
2482 IF (SIZE(u_pp, 1) /= basis%nsgf) RETURN
2483 ALLOCATE (used(basis%nsgf), source=.false.)
2484 ALLOCATE (angular_coeff(maxval(ncoset(0:maxval(basis%lmax)) - &
2485 ncoset(-1:maxval(basis%lmax) - 1)), &
2486 maxval(nso(0:maxval(basis%lmax))), &
2487 0:maxval(basis%lmax), basis%nset), source=0.0_dp)
2488 ALLOCATE (primitive_scale(maxval(basis%npgf), maxval(basis%nshell), basis%nset), &
2489 source=0.0_dp)
2490 DO iset = 1, basis%nset
2491 DO ishell = 1, basis%nshell(iset)
2492 l = basis%l(ishell, iset)
2493 first = basis%first_sgf(ishell, iset)
2494 IF (first < 1 .OR. basis%last_sgf(ishell, iset) /= first + nso(l) - 1) RETURN
2495 IF (basis%last_sgf(ishell, iset) > basis%nsgf) RETURN
2496 IF (any(used(first:first + nso(l) - 1))) RETURN
2497 used(first:first + nso(l) - 1) = .true.
2498 END DO
2499 END DO
2500 IF (.NOT. all(used)) RETURN
2501 DO iset = 1, basis%nset
2502 lmax = basis%lmax(iset)
2503 DO l = 0, lmax
2504 ref_abs = 0.0_dp
2505 ref_shell = 0
2506 ncart = ncoset(l) - ncoset(l - 1)
2507 DO ishell = 1, basis%nshell(iset)
2508 IF (basis%l(ishell, iset) /= l) cycle
2509 first = basis%first_sgf(ishell, iset)
2510 DO ipgf = 1, basis%npgf(iset)
2511 DO m = 1, nso(l)
2512 DO c = 1, ncart
2513 ico = ncoset(l - 1) + c
2514 row = (ipgf - 1)*ncoset(lmax) + ico
2515 actual = basis%sphi(row, first + m - 1)
2516 IF (abs(actual) > ref_abs) THEN
2517 ref_abs = abs(actual)
2518 ref_value = actual
2519 ref_shell = ishell
2520 ref_p = ipgf
2521 ref_c = c
2522 ref_m = m
2523 END IF
2524 END DO
2525 END DO
2526 END DO
2527 END DO
2528 IF (ref_shell == 0) cycle
2529 first = basis%first_sgf(ref_shell, iset)
2530 DO m = 1, nso(l)
2531 DO c = 1, ncart
2532 ico = ncoset(l - 1) + c
2533 row = (ref_p - 1)*ncoset(lmax) + ico
2534 angular_coeff(c, m, l, iset) = basis%sphi(row, first + m - 1)
2535 END DO
2536 END DO
2537 DO ishell = 1, basis%nshell(iset)
2538 IF (basis%l(ishell, iset) /= l) cycle
2539 first = basis%first_sgf(ishell, iset)
2540 DO ipgf = 1, basis%npgf(iset)
2541 row = (ipgf - 1)*ncoset(lmax) + ncoset(l - 1) + ref_c
2542 scale = basis%sphi(row, first + ref_m - 1)/ref_value
2543 primitive_scale(ipgf, ishell, iset) = scale
2544 DO m = 1, nso(l)
2545 DO c = 1, ncart
2546 ico = ncoset(l - 1) + c
2547 row = (ipgf - 1)*ncoset(lmax) + ico
2548 actual = basis%sphi(row, first + m - 1)
2549 expected = scale*angular_coeff(c, m, l, iset)
2550 IF (abs(actual - expected) > 64.0_dp*epsilon(1.0_dp)* &
2551 max(ref_abs, abs(actual), abs(expected))) RETURN
2552 END DO
2553 END DO
2554 END DO
2555 END DO
2556 END DO
2557 END DO
2558 CALL move_alloc(angular_coeff, contracted%spherical_angular)
2559 CALL move_alloc(primitive_scale, contracted%spherical_primitive)
2560 ALLOCATE (contracted%spherical_selection(SIZE(u_pp, 1), SIZE(u_pp, 2)))
2561 contracted%spherical_selection(:, :) = u_pp
2562 contracted%spherical_factorized = .true.
2563
2564 END SUBROUTINE prepare_spherical_potential_factorization
2565
2566! **************************************************************************************************
2567!> \brief Prepare the origin series in Eq. (2) once for a fixed basis and erfc(omega*r)/r kernel.
2568!> The coefficients depend on neither the integration point nor the translated atomic center.
2569!> \param basis ...
2570!> \param omega ...
2571!> \param series ...
2572!> \param short_series ...
2573! **************************************************************************************************
2574 SUBROUTINE prepare_radial_series(basis, omega, series, short_series)
2575 TYPE(gto_basis_set_type), POINTER :: basis
2576 REAL(dp), INTENT(IN) :: omega
2577 REAL(dp), ALLOCATABLE, INTENT(OUT) :: series(:, :, :), short_series(:, :, :)
2578
2579 INTEGER :: in, ipgf, iset, lmax
2580 REAL(dp) :: beta_ratio, exponent
2581
2582 lmax = maxval(basis%lmax)
2583 ALLOCATE (series(0:lmax + 24, maxval(basis%npgf), basis%nset), &
2584 short_series(0:lmax + 24, maxval(basis%npgf), basis%nset))
2585 DO iset = 1, basis%nset
2586 DO ipgf = 1, basis%npgf(iset)
2587 exponent = basis%zet(ipgf, iset)
2588 CALL origin_series(series(:, ipgf, iset))
2589 beta_ratio = omega**2/(exponent + omega**2)
2590 DO in = 0, ubound(series, 1)
2591 short_series(in, ipgf, iset) = series(in, ipgf, iset)* &
2592 (1.0_dp - beta_ratio**(real(in, dp) + 0.5_dp))
2593 END DO
2594 END DO
2595 END DO
2596
2597 END SUBROUTINE prepare_radial_series
2598
2599! **************************************************************************************************
2600!> \brief Evaluate the auxiliary potentials v_P(r) for the erfc(omega*r)/r kernel.
2601!> For a primitive H_l(r) exp(-a r²), H_l is a homogeneous harmonic polynomial.
2602!> (1) Its potential is H_l(r) (-1/a)^l d^l F(s)/ds^l, s=r².
2603!> (2) For the full Coulomb kernel, F(r²)=pi^(3/2) erf(sqrt(a)r)/(a^(3/2) r);
2604!> radial_potential_short subtracts the long-range part of erfc(omega*r)/r.
2605!> The potential columns are the contracted functions P of contracted%coefficients
2606!> (AUTO_RI functions or the plain RI basis); a final GEMM applies the contraction.
2607!> The erfc kernel and its origin series are those prepared in contracted.
2608!> \param values ...
2609!> \param points ...
2610!> \param basis ...
2611!> \param center ...
2612!> \param contracted ...
2613! **************************************************************************************************
2614 SUBROUTINE contract_selected_potentials(values, points, basis, center, contracted)
2615 REAL(dp), CONTIGUOUS, INTENT(OUT) :: values(:, :)
2616 REAL(dp), INTENT(IN) :: points(:, :)
2617 TYPE(gto_basis_set_type), POINTER :: basis
2618 REAL(dp), INTENT(IN) :: center(3)
2619 TYPE(radial_interpolation_type), INTENT(IN), &
2620 TARGET :: contracted
2621
2622 INTEGER :: iangular, ico, ig, imesh, ipgf, irow, &
2623 iset, isgf, ishell, l, lmax, m, &
2624 nchannels, ng, np, nrows, nwork
2625 INTEGER, ALLOCATABLE :: angular_count(:, :, :), &
2626 angular_index(:), &
2627 angular_term(:, :, :, :), &
2628 mesh_channel(:), minimum_l(:)
2629 LOGICAL :: factorized, in_table, parallel_points, &
2630 tabulated, use_mesh
2631 LOGICAL, ALLOCATABLE :: angular_used(:, :)
2632 REAL(dp) :: mesh_delta, mesh_position, omega, &
2633 radial_sum, radius, radius2, &
2634 relative(3), spherical_value, x
2635 REAL(dp), ALLOCATABLE :: angular_values(:, :, :), coordinate_power(:, :), long_radial(:), &
2636 monomial(:), radial(:), radial_channels(:, :, :), radial_mesh(:), work(:, :)
2637 REAL(dp), CONTIGUOUS, POINTER :: coefficient_matrix(:, :)
2638 REAL(dp), POINTER :: series(:, :, :), short_series(:, :, :)
2639
2640 ng = SIZE(points, 2)
2641 np = SIZE(values, 2)
2642 lmax = maxval(basis%lmax)
2643 nrows = SIZE(contracted%coefficients, 1)
2644 factorized = contracted%spherical_factorized
2645 nwork = nrows
2646 IF (factorized) nwork = basis%nsgf
2647 ALLOCATE (angular_used(0:lmax, basis%nset), source=.false.)
2648 ALLOCATE (minimum_l(basis%nset))
2649 DO iset = 1, basis%nset
2650 minimum_l(iset) = minval(basis%l(1:basis%nshell(iset), iset))
2651 DO ishell = 1, basis%nshell(iset)
2652 angular_used(basis%l(ishell, iset), iset) = .true.
2653 END DO
2654 END DO
2655 omega = contracted%attenuation
2656 ALLOCATE (work(nwork, ng))
2657 series => contracted%series
2658 short_series => contracted%short_series
2659 ! Primitive/angular row labels are invariant over the integration points.
2660 ! Evaluate each tabulated radial channel once, then apply its Cartesian monomial.
2661 nchannels = 0
2662 ALLOCATE (mesh_channel(nrows), angular_index(nrows))
2663 IF (ALLOCATED(contracted%mesh_moments)) THEN
2664 nchannels = SIZE(contracted%mesh_moments, 2)
2665 irow = 0
2666 DO iset = 1, basis%nset
2667 DO ipgf = 1, basis%npgf(iset)
2668 DO l = 0, basis%lmax(iset)
2669 IF (.NOT. angular_used(l, iset)) cycle
2670 DO ico = ncoset(l - 1) + 1, ncoset(l)
2671 irow = irow + 1
2672 mesh_channel(irow) = contracted%mesh_first(ipgf, iset) + l - minimum_l(iset)
2673 angular_index(irow) = ico
2674 END DO
2675 END DO
2676 END DO
2677 END DO
2678 END IF
2679 ! Preserve the order of the nonzero polynomial terms; omit only exact zeros.
2680 IF (factorized) THEN
2681 ALLOCATE (angular_count(maxval(nso(0:lmax)), 0:lmax, basis%nset), source=0)
2682 ALLOCATE (angular_term(SIZE(contracted%spherical_angular, 1), maxval(nso(0:lmax)), 0:lmax, basis%nset))
2683 DO iset = 1, basis%nset
2684 DO l = 0, basis%lmax(iset)
2685 IF (.NOT. angular_used(l, iset)) cycle
2686 DO m = 1, nso(l)
2687 DO ico = ncoset(l - 1) + 1, ncoset(l)
2688 IF (abs(contracted%spherical_angular(ico - ncoset(l - 1), m, l, iset)) <= 0.0_dp) cycle
2689 angular_count(m, l, iset) = angular_count(m, l, iset) + 1
2690 angular_term(angular_count(m, l, iset), m, l, iset) = ico
2691 END DO
2692 END DO
2693 END DO
2694 END DO
2695 END IF
2696 tabulated = ALLOCATED(contracted%short_moments)
2697
2698 parallel_points = .true.
2699!$ parallel_points = .NOT. omp_in_parallel()
2700 !$OMP PARALLEL DEFAULT(NONE) IF(parallel_points) &
2701 !$OMP SHARED(ng, points, center, basis, series, short_series, omega, &
2702 !$OMP work, indco, ncoset, lmax, angular_used, minimum_l, contracted, tabulated, &
2703 !$OMP nchannels, nrows, nwork, factorized, mesh_channel, angular_index, nso, angular_count, angular_term) &
2704 !$OMP PRIVATE(ig, ico, relative, radius, radius2, iset, ipgf, radial, monomial, l, irow, long_radial, &
2705 !$OMP x, in_table, coordinate_power, imesh, mesh_delta, mesh_position, use_mesh, radial_mesh, &
2706 !$OMP radial_channels, angular_values, isgf, m, radial_sum, spherical_value, iangular)
2707 ALLOCATE (radial(0:lmax), long_radial(0:lmax))
2708 ALLOCATE (monomial(ncoset(lmax)), coordinate_power(3, 0:lmax), radial_mesh(nchannels))
2709 IF (factorized) ALLOCATE (radial_channels(0:lmax, maxval(basis%npgf), basis%nset), &
2710 angular_values(maxval(nso(0:lmax)), 0:lmax, basis%nset))
2711 !$OMP DO SCHEDULE(STATIC)
2712 DO ig = 1, ng
2713 relative = points(:, ig) - center
2714 radius2 = sum(relative**2)
2715 coordinate_power(:, 0) = 1.0_dp
2716 DO l = 1, lmax
2717 coordinate_power(:, l) = coordinate_power(:, l - 1)*relative
2718 END DO
2719 DO ico = 1, SIZE(monomial)
2720 monomial(ico) = coordinate_power(1, indco(1, ico))* &
2721 coordinate_power(2, indco(2, ico))*coordinate_power(3, indco(3, ico))
2722 END DO
2723 use_mesh = .false.
2724 mesh_delta = 0.0_dp
2725 imesh = 0
2726 IF (ALLOCATED(contracted%mesh_moments)) THEN
2727 mesh_position = radius2/contracted%mesh_spacing - contracted%mesh_offset
2728 IF (mesh_position >= -0.5_dp .AND. mesh_position <= real(ubound(contracted%mesh_moments, 3), dp) + 0.5_dp) THEN
2729 imesh = nint(mesh_position)
2730 IF (imesh >= 0 .AND. imesh <= ubound(contracted%mesh_moments, 3)) THEN
2731 mesh_delta = radius2 - contracted%mesh_spacing*(real(imesh, dp) + contracted%mesh_offset)
2732 use_mesh = abs(mesh_delta) <= 64.0_dp*epsilon(1.0_dp)*max(1.0_dp, radius2)
2733 use_mesh = use_mesh .AND. contracted%mesh_exponent*abs(mesh_delta) <= eps_mesh_phase
2734 END IF
2735 END IF
2736 END IF
2737 IF (use_mesh) THEN
2738 IF (factorized .AND. ALLOCATED(contracted%spherical_mesh_first)) THEN
2739 ! The radial shell contraction was formed once for each squared-radius node.
2740 ELSE
2741 radial_mesh(:) = contracted%mesh_moments(1, :, imesh) + &
2742 mesh_delta*contracted%mesh_moments(2, :, imesh)
2743 END IF
2744 IF (factorized .AND. .NOT. ALLOCATED(contracted%spherical_mesh_first)) THEN
2745 DO iset = 1, basis%nset
2746 DO ipgf = 1, basis%npgf(iset)
2747 DO l = 0, basis%lmax(iset)
2748 IF (.NOT. angular_used(l, iset)) cycle
2749 radial_channels(l, ipgf, iset) = &
2750 radial_mesh(contracted%mesh_first(ipgf, iset) + l - minimum_l(iset))
2751 END DO
2752 END DO
2753 END DO
2754 ELSE IF (.NOT. factorized) THEN
2755 DO irow = 1, nrows
2756 work(irow, ig) = monomial(angular_index(irow))*radial_mesh(mesh_channel(irow))
2757 END DO
2758 cycle
2759 END IF
2760 ELSE
2761 radius = sqrt(radius2)
2762 irow = 0
2763 DO iset = 1, basis%nset
2764 DO ipgf = 1, basis%npgf(iset)
2765 x = basis%zet(ipgf, iset)*radius2
2766 in_table = .false.
2767 IF (tabulated) in_table = x <= contracted%short_abscissae(ubound(contracted%short_abscissae, 1))
2768 IF (in_table) THEN
2769 CALL evaluate_short_radial_table(x, contracted%short_abscissae, &
2770 contracted%short_moments(minimum_l(iset):, :, ipgf, iset), &
2771 radial(minimum_l(iset):basis%lmax(iset)))
2772 ELSE
2773 CALL radial_potential_short(radius, basis%zet(ipgf, iset), omega, &
2774 series(:, ipgf, iset), short_series(:, ipgf, iset), &
2775 radial(0:basis%lmax(iset)), long_radial)
2776 END IF
2777 IF (factorized) THEN
2778 DO l = 0, basis%lmax(iset)
2779 IF (angular_used(l, iset)) radial_channels(l, ipgf, iset) = radial(l)
2780 END DO
2781 ELSE
2782 DO l = 0, basis%lmax(iset)
2783 IF (.NOT. angular_used(l, iset)) cycle
2784 DO ico = ncoset(l - 1) + 1, ncoset(l)
2785 irow = irow + 1
2786 work(irow, ig) = monomial(ico)*radial(l)
2787 END DO
2788 END DO
2789 END IF
2790 END DO
2791 END DO
2792 END IF
2793 IF (factorized) THEN
2794 work(:, ig) = 0.0_dp
2795 DO iset = 1, basis%nset
2796 DO l = 0, basis%lmax(iset)
2797 IF (.NOT. angular_used(l, iset)) cycle
2798 DO m = 1, nso(l)
2799 spherical_value = 0.0_dp
2800 DO iangular = 1, angular_count(m, l, iset)
2801 ico = angular_term(iangular, m, l, iset)
2802 spherical_value = spherical_value + monomial(ico)* &
2803 contracted%spherical_angular(ico - ncoset(l - 1), m, l, iset)
2804 END DO
2805 angular_values(m, l, iset) = spherical_value
2806 END DO
2807 END DO
2808 DO ishell = 1, basis%nshell(iset)
2809 l = basis%l(ishell, iset)
2810 IF (use_mesh .AND. ALLOCATED(contracted%spherical_mesh_first)) THEN
2811 irow = contracted%spherical_mesh_first(ishell, iset)
2812 radial_sum = contracted%mesh_moments(1, irow, imesh) + &
2813 mesh_delta*contracted%mesh_moments(2, irow, imesh)
2814 ELSE
2815 radial_sum = sum(radial_channels(l, 1:basis%npgf(iset), iset)* &
2816 contracted%spherical_primitive(1:basis%npgf(iset), ishell, iset))
2817 END IF
2818 DO m = 1, nso(l)
2819 isgf = basis%first_sgf(ishell, iset) + m - 1
2820 work(isgf, ig) = radial_sum*angular_values(m, l, iset)
2821 END DO
2822 END DO
2823 END DO
2824 END IF
2825 END DO
2826 !$OMP END DO
2827 DEALLOCATE (radial, long_radial)
2828 DEALLOCATE (monomial, coordinate_power, radial_mesh)
2829 IF (factorized) DEALLOCATE (radial_channels, angular_values)
2830 !$OMP END PARALLEL
2831
2832 ! The caller supplies the full leading dimension; write only the active point rows.
2833 IF (factorized) THEN
2834 coefficient_matrix => contracted%spherical_selection
2835 ELSE
2836 coefficient_matrix => contracted%coefficients
2837 END IF
2838 CALL dgemm('T', 'N', ng, np, nwork, 1.0_dp, work, nwork, &
2839 coefficient_matrix, nwork, 0.0_dp, values, SIZE(values, 1))
2840 IF (factorized) DEALLOCATE (angular_count, angular_term)
2841 DEALLOCATE (angular_used, minimum_l, mesh_channel, angular_index)
2842
2843 END SUBROUTINE contract_selected_potentials
2844
2845! **************************************************************************************************
2846!> \brief Gaussian potential for erfc(omega*r)/r via two full-Coulomb potentials.
2847!> With beta=a*omega²/(a+omega²), the long-range part equals the beta potential
2848!> multiplied in angular channel l by (beta/a)^(l+3/2). The origin coefficients
2849!> combine both terms before evaluation to avoid subtraction at r=0.
2850!> \param radius ...
2851!> \param exponent ...
2852!> \param omega ...
2853!> \param coefficients ...
2854!> \param short_coefficients ...
2855!> \param radial ...
2856!> \param long_radial ...
2857! **************************************************************************************************
2858 SUBROUTINE radial_potential_short(radius, exponent, omega, coefficients, short_coefficients, radial, long_radial)
2859 REAL(dp), INTENT(IN) :: radius, exponent, omega, &
2860 coefficients(0:), &
2861 short_coefficients(0:)
2862 REAL(dp), INTENT(OUT) :: radial(0:)
2863 REAL(dp), INTENT(INOUT) :: long_radial(0:)
2864
2865 INTEGER :: il
2866 REAL(dp) :: beta, beta_ratio, factor, lower_exp, &
2867 ratio_power, root_x, upper_exp, x
2868
2869 IF (exponent*radius**2 < 0.5_dp) THEN
2870 CALL radial_potential(radius, exponent, short_coefficients, radial)
2871 RETURN
2872 END IF
2873 beta_ratio = omega**2/(exponent + omega**2)
2874 beta = exponent*beta_ratio
2875 x = exponent*radius**2
2876 IF (x >= real(2*ubound(radial, 1) + 8, dp)) THEN
2877 ! H_l(x,b)=integral_{sqrt(b)}^1 t^(2l) exp(-x t^2) dt; radial_l=(2 pi/a) H_l.
2878 ! Its upward recurrence is stable here and avoids subtracting two Coulomb derivatives.
2879 root_x = sqrt(x)
2880 factor = 2.0_dp*pi/exponent
2881 radial(0) = factor*sqrt(pi)/(2.0_dp*root_x)* &
2882 (erfc(sqrt(beta_ratio)*root_x) - erfc(root_x))
2883 upper_exp = exp(-x)
2884 lower_exp = exp(-beta_ratio*x)
2885 ratio_power = sqrt(beta_ratio)
2886 DO il = 1, ubound(radial, 1)
2887 radial(il) = (real(2*il - 1, dp)*radial(il - 1) + &
2888 factor*(ratio_power*lower_exp - upper_exp))/(2.0_dp*x)
2889 ratio_power = ratio_power*beta_ratio
2890 END DO
2891 RETURN
2892 END IF
2893 CALL radial_potential(radius, exponent, coefficients, radial)
2894 CALL radial_potential(radius, beta, coefficients, long_radial(0:ubound(radial, 1)))
2895 DO il = 0, ubound(radial, 1)
2896 radial(il) = radial(il) - beta_ratio**(real(il, dp) + 1.5_dp)*long_radial(il)
2897 END DO
2898
2899 END SUBROUTINE radial_potential_short
2900
2901! **************************************************************************************************
2902!> \brief Expand the full-Coulomb F of Eq. (2) in x=a r²: F=(2 pi/a) sum_n c_n x^n near the origin.
2903!> The coefficients c_n are independent of the exponent a.
2904!> \param coefficients ...
2905! **************************************************************************************************
2906 SUBROUTINE origin_series(coefficients)
2907 REAL(dp), INTENT(OUT) :: coefficients(0:)
2908
2909 INTEGER :: ik, in
2910 REAL(dp) :: factorial, weight, terms(0:ubound(coefficients, 1))
2911
2912 weight = 1.0_dp
2913 DO ik = 0, ubound(terms, 1)
2914 terms(ik) = weight
2915 weight = weight*2.0_dp/real(2*ik + 3, dp)
2916 END DO
2917 DO in = 0, ubound(coefficients, 1)
2918 coefficients(in) = 0.0_dp
2919 factorial = 1.0_dp
2920 DO ik = in, 0, -1
2921 coefficients(in) = coefficients(in) + factorial*terms(ik)
2922 factorial = -factorial/real(in - ik + 1, dp)
2923 END DO
2924 END DO
2925
2926 END SUBROUTINE origin_series
2927
2928! **************************************************************************************************
2929!> \brief Evaluate the radial derivatives in Eq. (1) by series or Taylor differentiation of (2).
2930!> The full Coulomb kernel is used; radial_potential_short forms erfc(omega*r)/r from it.
2931!> \param radius ...
2932!> \param exponent ...
2933!> \param coefficients ...
2934!> \param radial ...
2935! **************************************************************************************************
2936 SUBROUTINE radial_potential(radius, exponent, coefficients, radial)
2937 REAL(dp), INTENT(IN) :: radius, exponent, coefficients(0:)
2938 REAL(dp), INTENT(OUT) :: radial(0:)
2939
2940 INTEGER :: ij, il, in, lmax
2941 REAL(dp) :: factor, power, prefactor, value, x, jet(0:ubound(radial, 1)), &
2942 gaussian(0:ubound(radial, 1)), derivative(0:ubound(radial, 1)), &
2943 bracket(0:ubound(radial, 1))
2944
2945 lmax = ubound(radial, 1)
2946 x = exponent*radius**2
2947 IF (x < 0.5_dp) THEN
2948 DO il = 0, lmax
2949 value = 0.0_dp
2950 power = 1.0_dp
2951 DO in = il, ubound(coefficients, 1)
2952 factor = 1.0_dp
2953 DO ij = in - il + 1, in
2954 factor = factor*real(ij, dp)
2955 END DO
2956 value = value + coefficients(in)*factor*power
2957 power = power*x
2958 END DO
2959 radial(il) = 2.0_dp*pi/exponent*(-1.0_dp)**il*value
2960 END DO
2961 RETURN
2962 END IF
2963 bracket(0:lmax) = 0.0_dp
2964 gaussian(0) = exp(-exponent*radius**2)
2965 IF (lmax > 0) gaussian(1) = -2.0_dp*exponent*radius*gaussian(0)
2966 DO in = 2, lmax
2967 gaussian(in) = -2.0_dp*exponent* &
2968 (radius*gaussian(in - 1) + gaussian(in - 2))/real(in, dp)
2969 END DO
2970 DO in = 1, lmax
2971 bracket(in) = bracket(in) + &
2972 4.0_dp*sqrt(exponent/pi)*gaussian(in - 1)/real(in, dp)
2973 END DO
2974 bracket(0) = 2.0_dp*erf(sqrt(exponent)*radius)
2975 prefactor = pi**1.5_dp/(2.0_dp*exponent**1.5_dp)
2976 jet(0) = bracket(0)/radius
2977 DO in = 1, lmax
2978 jet(in) = (bracket(in) - jet(in - 1))/radius
2979 END DO
2980 radial(0) = prefactor*jet(0)
2981 DO il = 1, lmax
2982 derivative(0:lmax) = 0.0_dp
2983 derivative(0) = jet(1)/(2.0_dp*radius)
2984 DO in = 1, lmax - il
2985 derivative(in) = (real(in + 1, dp)*jet(in + 1)/2.0_dp - derivative(in - 1))/radius
2986 END DO
2987 jet(0:lmax) = derivative(0:lmax)
2988 radial(il) = prefactor*(-1.0_dp/exponent)**il*jet(0)
2989 END DO
2990
2991 END SUBROUTINE radial_potential
2992
2993! **************************************************************************************************
2994!> \brief Place radial nodes at x=0.02*i up to x=2, then at x=2*1.01**(i-100).
2995!> The final node retains the original table extent. For a nearest-node displacement δ,
2996!> the sixth-order Taylor remainder of H_j is bounded by
2997!> |δ|**7/7! * integral_0^1 t**(2*j+14)*exp(-(x-|δ|)*t**2) dt.
2998!> On the geometric part |δ| <= 0.005*x, so this bound is below 1.1e-17 for j>=0.
2999!> This is an absolute bound before the Gaussian prefactor 2*pi/a; roundoff is separate.
3000!> \param xmax ...
3001!> \param abscissae ...
3002! **************************************************************************************************
3003 SUBROUTINE prepare_short_radial_grid(xmax, abscissae)
3004 REAL(dp), INTENT(IN) :: xmax
3005 REAL(dp), ALLOCATABLE, INTENT(OUT) :: abscissae(:)
3006
3007 INTEGER :: ix, last
3008
3009 cpassert(xmax > 0.0_dp)
3010 IF (xmax <= short_grid_switch) THEN
3011 last = ceiling(xmax/short_grid_spacing)
3012 ELSE
3013 last = n_short_uniform + ceiling(log(xmax/short_grid_switch)/log(short_grid_ratio))
3014 END IF
3015 ALLOCATE (abscissae(0:last))
3016 DO ix = 0, last - 1
3017 IF (ix <= n_short_uniform) THEN
3018 abscissae(ix) = short_grid_spacing*real(ix, dp)
3019 ELSE
3020 abscissae(ix) = short_grid_switch*exp(real(ix - n_short_uniform, dp)*log(short_grid_ratio))
3021 END IF
3022 END DO
3023 abscissae(last) = xmax
3024
3025 END SUBROUTINE prepare_short_radial_grid
3026
3027! **************************************************************************************************
3028!> \brief Tabulate H_j(x,b)=integral_sqrt(b)^1 t^(2j)*exp(-x*t²)dt for erfc potentials.
3029!> A 64-point positive quadrature supplies near-field moments; a stable upward recurrence
3030!> supplies far-field moments for b<0.9. Narrow intervals retain quadrature to avoid cancellation.
3031!> Uniform mesh tables select spacing and offset; nonuniform lookup tables supply their abscissae.
3032!> The caller bounds storage, including one primitive preparation buffer.
3033!> \param a ...
3034!> \param omega ...
3035!> \param lmax ...
3036!> \param table ...
3037!> \param ninterval ...
3038!> \param point_spacing ...
3039!> \param point_offset ...
3040!> \param last_moment ...
3041!> \param abscissae ...
3042! **************************************************************************************************
3043 SUBROUTINE prepare_short_radial_table(a, omega, lmax, table, ninterval, point_spacing, point_offset, &
3044 last_moment, abscissae)
3045 REAL(dp), INTENT(IN) :: a, omega
3046 INTEGER, INTENT(IN) :: lmax
3047 REAL(dp), ALLOCATABLE, INTENT(OUT) :: table(:, :)
3048 INTEGER, INTENT(IN) :: ninterval
3049 REAL(dp), INTENT(IN), OPTIONAL :: point_spacing, point_offset
3050 INTEGER, INTENT(IN), OPTIONAL :: last_moment
3051 REAL(dp), INTENT(IN), OPTIONAL :: abscissae(0:)
3052
3053 INTEGER, PARAMETER :: n_legendre = 64
3054
3055 INTEGER :: ig, il, ix, last, order, quadrature_last
3056 REAL(dp) :: b, factor, legendre_nodes(n_legendre), legendre_weights(n_legendre), lower, &
3057 lower_exp, offset, ratio_power, root_x, spacing, t, t2, term, upper_exp, width, x
3058
3059 cpassert(PRESENT(point_spacing) .OR. PRESENT(abscissae))
3060 last = ninterval
3061 spacing = 0.0_dp
3062 offset = 0.0_dp
3063 order = lmax + 6
3064 IF (PRESENT(point_spacing)) spacing = point_spacing
3065 IF (PRESENT(point_offset)) offset = point_offset
3066 IF (PRESENT(last_moment)) order = last_moment
3067 ALLOCATE (table(0:order, 0:last), source=0.0_dp)
3068 b = omega**2/(a + omega**2)
3069 lower = sqrt(b)
3070 width = (a/(a + omega**2))/(1.0_dp + lower)
3071 quadrature_last = last
3072 IF (b < recurrence_b_max) THEN
3073 IF (PRESENT(abscissae)) THEN
3074 quadrature_last = count(abscissae <= real(2*order + 8, dp)) - 1
3075 ELSE
3076 quadrature_last = int(min(real(last, dp), max(0.0_dp, real(2*order + 8, dp)/spacing - offset)))
3077 END IF
3078 END IF
3079 CALL gauss_legendre(n_legendre, legendre_nodes, legendre_weights)
3080 DO ig = 1, n_legendre
3081 t = lower + 0.5_dp*width*(1.0_dp + legendre_nodes(ig))
3082 t2 = t*t
3083 DO ix = 0, quadrature_last
3084 x = spacing*(real(ix, dp) + offset)
3085 IF (PRESENT(abscissae)) x = abscissae(ix)
3086 term = 0.5_dp*width*legendre_weights(ig)*exp(-x*t2)*2.0_dp*pi/a
3087 DO il = 0, ubound(table, 1)
3088 table(il, ix) = table(il, ix) + term
3089 term = term*t2
3090 END DO
3091 END DO
3092 END DO
3093 factor = 2.0_dp*pi/a
3094 DO ix = quadrature_last + 1, last
3095 x = spacing*(real(ix, dp) + offset)
3096 IF (PRESENT(abscissae)) x = abscissae(ix)
3097 root_x = sqrt(x)
3098 table(0, ix) = factor*sqrt(pi)/(2.0_dp*root_x)*(erfc(lower*root_x) - erfc(root_x))
3099 lower_exp = exp(-b*x)
3100 upper_exp = exp(-x)
3101 ratio_power = lower
3102 DO il = 1, ubound(table, 1)
3103 table(il, ix) = (real(2*il - 1, dp)*table(il - 1, ix) + &
3104 factor*(ratio_power*lower_exp - upper_exp))/(2.0_dp*x)
3105 ratio_power = ratio_power*b
3106 END DO
3107 END DO
3108
3109 END SUBROUTINE prepare_short_radial_table
3110
3111! **************************************************************************************************
3112!> \brief Evaluate H_l at the nearest table point with its first six derivatives.
3113!> Since d^k H_l/dx^k=(-1)^k H_(l+k), the stored higher moments provide the derivatives.
3114!> The nonuniform-node error bound is given in prepare_short_radial_grid.
3115!> \param x ...
3116!> \param abscissae ...
3117!> \param table ...
3118!> \param radial ...
3119! **************************************************************************************************
3120 SUBROUTINE evaluate_short_radial_table(x, abscissae, table, radial)
3121 REAL(dp), INTENT(IN) :: x, abscissae(0:), table(0:, 0:)
3122 REAL(dp), INTENT(OUT) :: radial(0:)
3123
3124 REAL(dp), PARAMETER :: &
3125 inverse(6) = [1.0_dp, 0.5_dp, 1.0_dp/3.0_dp, 0.25_dp, 0.2_dp, 1.0_dp/6.0_dp]
3126
3127 INTEGER :: ik, il, ileft, imiddle, iright, ix
3128 REAL(dp) :: delta, value
3129
3130 ileft = 0
3131 iright = ubound(abscissae, 1)
3132 DO WHILE (iright - ileft > 1)
3133 imiddle = (ileft + iright)/2
3134 IF (x < abscissae(imiddle)) THEN
3135 iright = imiddle
3136 ELSE
3137 ileft = imiddle
3138 END IF
3139 END DO
3140 ix = ileft
3141 IF (abscissae(iright) - x < x - abscissae(ileft)) ix = iright
3142 delta = abscissae(ix) - x
3143 DO il = 0, ubound(radial, 1)
3144 value = table(il + 6, ix)
3145 DO ik = 5, 0, -1
3146 value = table(il + ik, ix) + delta*value*inverse(ik + 1)
3147 END DO
3148 radial(il) = value
3149 END DO
3150
3151 END SUBROUTINE evaluate_short_radial_table
3152
3153! **************************************************************************************************
3154!> \brief Tabulate H_l(r²) only when squared mesh distances share a one-dimensional lattice.
3155!> With M=SᵀS and d=r_origin-r_atom, r²=nᵀMn+2nᵀSᵀd+d². Integer coefficients
3156!> in units q imply r²=q(i+offset). Whole-cell translations preserve this mesh.
3157!> H_l(s+δ)=H_l(s)-a δ H_(l+1)(s)+O((a δ)²); each lookup checks the residual.
3158!> Tables cover up to 40 bohr within the storage budget; other points use the generic lookup.
3159!> Account for all stored channels, their index map and the largest temporary moment table.
3160!> \param basis ...
3161!> \param contracted ...
3162!> \param step ...
3163!> \param displacement ...
3164!> \param table_bytes ...
3165! **************************************************************************************************
3166 SUBROUTINE prepare_mesh_radial_moments(basis, contracted, step, displacement, table_bytes)
3167 TYPE(gto_basis_set_type), POINTER :: basis
3168 TYPE(radial_interpolation_type), INTENT(INOUT) :: contracted
3169 REAL(dp), INTENT(IN) :: step(3, 3), displacement(3)
3170 REAL(dp), INTENT(INOUT) :: table_bytes
3171
3172 INTEGER :: idir, ifirst, il, ipgf, iset, ishell, jdir, last, max_l, max_primitive, &
3173 minimum_l, nchannels, nshell_total, nstored, shell_index
3174 LOGICAL :: direct_shells
3175 REAL(dp) :: bytes_per_node, index_bytes, linear(3), &
3176 metric(3, 3), offset, ratio, &
3177 required_bytes, spacing
3178 REAL(dp), ALLOCATABLE :: moments(:, :)
3179
3180 IF (basis%nset < 1) RETURN
3181 IF (any(basis%nshell(1:basis%nset) < 1)) RETURN
3182 IF (any(basis%npgf(1:basis%nset) < 1)) RETURN
3183 IF (contracted%attenuation <= 0.0_dp .OR. table_bytes <= 0.0_dp) RETURN
3184 metric = matmul(transpose(step), step)
3185 spacing = min(metric(1, 1), metric(2, 2), metric(3, 3))
3186 IF (spacing <= tiny(1.0_dp)) RETURN
3187 DO idir = 1, 3
3188 DO jdir = idir, 3
3189 ratio = metric(idir, jdir)/spacing
3190 IF (idir /= jdir) ratio = 2.0_dp*ratio
3191 IF (abs(ratio - anint(ratio)) > eps_integer) RETURN
3192 END DO
3193 END DO
3194 linear = 2.0_dp*matmul(transpose(step), displacement)/spacing
3195 IF (any(abs(linear - anint(linear)) > eps_integer)) RETURN
3196 offset = modulo(sum(displacement**2)/spacing, 1.0_dp)
3197 IF (mesh_table_radius**2/spacing > real(huge(last) - 1, dp)) RETURN
3198 last = ceiling(mesh_table_radius**2/spacing)
3199 IF (last < 1) RETURN
3200 nchannels = 0
3201 DO iset = 1, basis%nset
3202 minimum_l = minval(basis%l(1:basis%nshell(iset), iset))
3203 nchannels = nchannels + basis%npgf(iset)*(basis%lmax(iset) - minimum_l + 1)
3204 END DO
3205 nshell_total = sum(basis%nshell(1:basis%nset))
3206 max_l = maxval(basis%lmax(1:basis%nset))
3207 max_primitive = maxval(basis%npgf(1:basis%nset))
3208 direct_shells = contracted%spherical_factorized .AND. nshell_total <= nchannels
3209 nstored = nchannels
3210 IF (direct_shells) nstored = nshell_total
3211 bytes_per_node = real_bytes*(2.0_dp*real(nstored, dp) + real(max_l + 2, dp))
3212 index_bytes = real(int_size, dp)*real(max_primitive, dp)*real(basis%nset, dp)
3213 IF (direct_shells) THEN
3214 index_bytes = index_bytes + &
3215 REAL(int_size, dp)*REAL(maxval(basis%nshell)*basis%nset, dp)
3216 END IF
3217 ! A smaller table still accelerates nearby points; its upper bound selects the generic fallback.
3218 last = min(last, int(min(real(huge(last), dp), &
3219 max(0.0_dp, (table_bytes - index_bytes)/bytes_per_node))) - 1)
3220 IF (last < 1) RETURN
3221 required_bytes = bytes_per_node*real(last + 1, dp) + index_bytes
3222 IF (required_bytes > table_bytes) RETURN
3223 ALLOCATE (contracted%mesh_moments(2, nstored, 0:last))
3224 ALLOCATE (contracted%mesh_first(max_primitive, basis%nset), source=0)
3225 contracted%mesh_spacing = spacing
3226 contracted%mesh_offset = offset
3227 IF (direct_shells) THEN
3228 ALLOCATE (contracted%spherical_mesh_first(maxval(basis%nshell), basis%nset), source=0)
3229 shell_index = 0
3230 DO iset = 1, basis%nset
3231 DO ishell = 1, basis%nshell(iset)
3232 shell_index = shell_index + 1
3233 contracted%spherical_mesh_first(ishell, iset) = shell_index
3234 END DO
3235 END DO
3236 contracted%mesh_moments = 0.0_dp
3237 END IF
3238 ifirst = 1
3239 DO iset = 1, basis%nset
3240 minimum_l = minval(basis%l(1:basis%nshell(iset), iset))
3241 DO ipgf = 1, basis%npgf(iset)
3242 contracted%mesh_exponent = max(contracted%mesh_exponent, basis%zet(ipgf, iset))
3243 contracted%mesh_first(ipgf, iset) = ifirst
3244 CALL prepare_short_radial_table(basis%zet(ipgf, iset), contracted%attenuation, &
3245 basis%lmax(iset), moments, last, &
3246 basis%zet(ipgf, iset)*spacing, offset, basis%lmax(iset) + 1)
3247 IF (direct_shells) THEN
3248 ! Accumulate primitive contributions in their original order for each shell.
3249 ! The larger primitive-channel table is never materialized.
3250 DO ishell = 1, basis%nshell(iset)
3251 il = basis%l(ishell, iset)
3252 shell_index = contracted%spherical_mesh_first(ishell, iset)
3253 contracted%mesh_moments(1, shell_index, :) = &
3254 contracted%mesh_moments(1, shell_index, :) + &
3255 contracted%spherical_primitive(ipgf, ishell, iset)*moments(il, :)
3256 contracted%mesh_moments(2, shell_index, :) = &
3257 contracted%mesh_moments(2, shell_index, :) + &
3258 contracted%spherical_primitive(ipgf, ishell, iset)*(-basis%zet(ipgf, iset)*moments(il + 1, :))
3259 END DO
3260 ifirst = ifirst + basis%lmax(iset) - minimum_l + 1
3261 ELSE
3262 DO il = minimum_l, basis%lmax(iset)
3263 contracted%mesh_moments(1, ifirst, :) = moments(il, :)
3264 contracted%mesh_moments(2, ifirst, :) = -basis%zet(ipgf, iset)*moments(il + 1, :)
3265 ifirst = ifirst + 1
3266 END DO
3267 END IF
3268 DEALLOCATE (moments)
3269 END DO
3270 END DO
3271 table_bytes = table_bytes - required_bytes
3272
3273 END SUBROUTINE prepare_mesh_radial_moments
3274
3275END MODULE ri_rs_kp_integrals
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.
double distance_squared(double *A, double *B)
Definition grpp_utils.c:68
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 gaussian
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_size
Definition kinds.F:36
integer, parameter, public dp
Definition kinds.F:34
integer, parameter, public int_size
Definition kinds.F:36
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
real(kind=dp), parameter, public twopi
Collection of simple mathematical functions and subroutines.
Definition mathlib.F:15
subroutine, public diamat_all(a, eigval, dac)
Diagonalize the symmetric n by n matrix a using the LAPACK library. Only the upper triangle of matrix...
Definition mathlib.F:383
subroutine, public gauss_legendre(n, nodes, weights)
Gauss-Legendre nodes and weights on [-1,1], with nodes in ascending order. The Legendre roots are ref...
Definition mathlib.F:2295
pure real(kind=dp) function, public multinomial(n, k)
Calculates the multinomial coefficients.
Definition mathlib.F:265
Interface to the message passing library MPI.
subroutine, public mp_mem_avail_per_rank_gb(comm, mem_avail_gb)
Memory that is currently free on the node, per MPI rank of that node, in GB.
Provides Cartesian and spherical orbital pointers and indices.
integer, dimension(:), allocatable, public ncoset
integer, dimension(:, :), allocatable, public indco
integer, dimension(:), allocatable, public nso
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
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...
Integration grid, AO images and radial tables for the periodic RI-RS integrals d_lP(R).
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
stores all the informations relevant to an mpi environment