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