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 Shared numerical operations for computing the RI-RS matrix Z_lP.
10 : !> \par History
11 : !> 09.2026 created Jan Wilhelm
12 : ! **************************************************************************************************
13 : MODULE ri_rs_fitting
14 : USE cp_blacs_env, ONLY: cp_blacs_env_type
15 : USE cp_dbcsr_api, ONLY: dbcsr_put_block,&
16 : dbcsr_type
17 : USE cp_fm_cholesky, ONLY: cp_fm_cholesky_decompose,&
18 : cp_fm_cholesky_solve
19 : USE cp_fm_struct, ONLY: cp_fm_struct_create,&
20 : cp_fm_struct_release,&
21 : cp_fm_struct_type
22 : USE cp_fm_types, ONLY: cp_fm_create,&
23 : cp_fm_get_info,&
24 : cp_fm_get_submatrix,&
25 : cp_fm_release,&
26 : cp_fm_set_submatrix,&
27 : cp_fm_type
28 : USE kinds, ONLY: dp
29 : USE message_passing, ONLY: mp_para_env_type
30 : #include "./base/base_uses.f90"
31 :
32 : IMPLICIT NONE
33 : PRIVATE
34 :
35 : CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'ri_rs_fitting'
36 : REAL(KIND=dp), PARAMETER, PRIVATE :: jacobi_floor = 1.0E-16_dp
37 :
38 : PUBLIC :: evaluate_ri_rs_fit, &
39 : solve_ri_rs_fit, &
40 : store_Z_lP_columns
41 :
42 : CONTAINS
43 :
44 : ! **************************************************************************************************
45 : !> \brief Computes the regularized RI-RS fit from an AO collocation matrix and a replicated RHS.
46 : !>
47 : !> D_ll' = [Σ_μ ϕ_μ(r_l) ϕ_μ(r_l')]²,
48 : !> d_l = 1/sqrt(max(D_ll, jacobi_floor)),
49 : !> D'_ll' = d_l D_ll' d_l' + λ δ_ll',
50 : !> Σ_l' D'_ll' Z'_l'P = d_l d_lP,
51 : !> Z_lP = d_l Z'_lP.
52 : !>
53 : !> The RI-RS least-squares construction and balancing are described by Duchemin and
54 : !> Blase, arXiv:1902.00066, Sec. II.3. Here jacobi_floor bounds D_ll from below in
55 : !> both the LAPACK and ScaLAPACK paths. The regularization λ is added after Jacobi
56 : !> scaling; in the unscaled system its diagonal contribution is λ/d_l².
57 : !> \param phi_local AO values on the fitting grid, replicated within the solve subgroup
58 : !> \param d_lp replicated RHS d_lP on entry and fitted coefficients Z_lP on return
59 : !> \param tikhonov regularization parameter λ in the Jacobi-scaled space
60 : !> \param para_env_sub optional subgroup communicator for the distributed solve
61 : !> \param blacs_env_sub optional BLACS context, supplied together with para_env_sub
62 : ! **************************************************************************************************
63 59 : SUBROUTINE solve_ri_rs_fit(phi_local, d_lp, tikhonov, para_env_sub, blacs_env_sub)
64 : REAL(KIND=dp), DIMENSION(:, :), INTENT(IN) :: phi_local
65 : REAL(KIND=dp), DIMENSION(:, :), INTENT(INOUT) :: d_lp
66 : REAL(KIND=dp), INTENT(IN) :: tikhonov
67 : TYPE(mp_para_env_type), OPTIONAL, POINTER :: para_env_sub
68 : TYPE(cp_blacs_env_type), OPTIONAL, POINTER :: blacs_env_sub
69 :
70 : CHARACTER(LEN=*), PARAMETER :: routineN = 'solve_ri_rs_fit'
71 :
72 : INTEGER :: handle, handle2, i, info, n_grid, n_rhs
73 59 : REAL(KIND=dp), ALLOCATABLE :: gram(:, :), jacobi_scale(:)
74 :
75 59 : CALL timeset(routineN, handle)
76 :
77 59 : n_grid = SIZE(phi_local, 1)
78 59 : n_rhs = SIZE(d_lp, 2)
79 59 : CPASSERT(SIZE(d_lp, 1) == n_grid)
80 59 : CPASSERT(PRESENT(para_env_sub) .EQV. PRESENT(blacs_env_sub))
81 59 : IF (PRESENT(para_env_sub)) THEN
82 : ! Only the row norms are replicated; the distributed solve builds its own Gram tiles.
83 0 : ALLOCATE (jacobi_scale(n_grid))
84 : !$OMP PARALLEL DO DEFAULT(NONE) &
85 : !$OMP SHARED(n_grid, phi_local, jacobi_scale) &
86 0 : !$OMP PRIVATE(i) SCHEDULE(STATIC)
87 : DO i = 1, n_grid
88 : jacobi_scale(i) = 1.0_dp/MAX(DOT_PRODUCT(phi_local(i, :), phi_local(i, :)), SQRT(jacobi_floor))
89 : END DO
90 : !$OMP END PARALLEL DO
91 : ELSE
92 59 : CALL factor_ri_rs_fit(phi_local, tikhonov, gram, jacobi_scale, info)
93 59 : IF (info /= 0) CPABORT('RI-RS Cholesky factorization failed')
94 : END IF
95 :
96 : ! Pre-scale the right-hand side: d_lP <- d_l d_lP.
97 59 : CALL scale_rows_by_diag(d_lp, jacobi_scale)
98 59 : IF (PRESENT(para_env_sub)) THEN
99 : CALL solve_D_lp_distributed(phi_local, jacobi_scale, d_lp, tikhonov, &
100 0 : para_env_sub, blacs_env_sub)
101 : ELSE
102 : ! Solve Σ_l' D'_ll' Z'_l'P = d_l d_lP, overwriting d_lp with Z'_lP.
103 59 : CALL timeset(routineN//'_dpotrs', handle2)
104 59 : CALL dpotrs('L', n_grid, n_rhs, gram, n_grid, d_lp, n_grid, info)
105 59 : CALL timestop(handle2)
106 59 : IF (info /= 0) CPABORT('RI-RS Cholesky solve failed')
107 : END IF
108 : ! Undo the conditioning on the solution: Z_lP = d_l Z'_lP.
109 59 : CALL scale_rows_by_diag(d_lp, jacobi_scale)
110 :
111 59 : CALL timestop(handle)
112 :
113 59 : END SUBROUTINE solve_ri_rs_fit
114 :
115 : ! **************************************************************************************************
116 : !> \brief Build and factor the Jacobi-scaled squared AO Gram matrix, K = d D d + λ I.
117 : !> The lower triangle is used throughout; the factor is retained for subsequent solves.
118 : !> See solve_ri_rs_fit for the RI-RS balancing convention and reference.
119 : !> \param phi AO values indexed (grid point, AO)
120 : !> \param tikhonov regularization in the Jacobi-scaled space
121 : !> \param factor lower Cholesky factor of K on successful return
122 : !> \param jacobi_scale pair-feature scales d_l
123 : !> \param info LAPACK factorization status; the caller chooses how to handle failure
124 : !> \param overlap optional lower triangle of the scaled AO overlap V V^T, V_l = sqrt(d_l) phi_l
125 : ! **************************************************************************************************
126 268 : SUBROUTINE factor_ri_rs_fit(phi, tikhonov, factor, jacobi_scale, info, overlap)
127 : REAL(KIND=dp), DIMENSION(:, :), INTENT(IN) :: phi
128 : REAL(KIND=dp), INTENT(IN) :: tikhonov
129 : REAL(KIND=dp), ALLOCATABLE, INTENT(OUT) :: factor(:, :), jacobi_scale(:)
130 : INTEGER, INTENT(OUT) :: info
131 : REAL(KIND=dp), ALLOCATABLE, INTENT(OUT), OPTIONAL :: overlap(:, :)
132 :
133 : CHARACTER(LEN=*), PARAMETER :: routineN = 'factor_ri_rs_fit'
134 :
135 : INTEGER :: handle, handle2, i, j, n_grid
136 :
137 268 : CALL timeset(routineN, handle)
138 268 : n_grid = SIZE(phi, 1)
139 1608 : ALLOCATE (factor(n_grid, n_grid), jacobi_scale(n_grid))
140 : ! First form S_ll' = Σ_μ phi_lμ phi_l'μ; D_ll' = S_ll'^2.
141 268 : CALL timeset(routineN//'_dsyrk', handle2)
142 268 : CALL dsyrk('L', 'N', n_grid, SIZE(phi, 2), 1.0_dp, phi, n_grid, 0.0_dp, factor, n_grid)
143 268 : CALL timestop(handle2)
144 28553 : DO i = 1, n_grid
145 28553 : jacobi_scale(i) = 1.0_dp/MAX(factor(i, i), SQRT(jacobi_floor))
146 : END DO
147 268 : IF (PRESENT(overlap)) THEN
148 836 : ALLOCATE (overlap(n_grid, n_grid))
149 : ! Only the grid optimizer requests this overlap; leave the production solve path unchanged.
150 : !$OMP PARALLEL DO DEFAULT(NONE) &
151 : !$OMP SHARED(n_grid, overlap, factor, jacobi_scale) &
152 209 : !$OMP PRIVATE(j, i) SCHEDULE(DYNAMIC)
153 : DO j = 1, n_grid
154 : DO i = j, n_grid
155 : overlap(i, j) = factor(i, j)*SQRT(jacobi_scale(i))*SQRT(jacobi_scale(j))
156 : END DO
157 : END DO
158 : !$OMP END PARALLEL DO
159 : END IF
160 : ! Add λ after Jacobi scaling; LAPACK needs only the lower triangle.
161 : !$OMP PARALLEL DO DEFAULT(NONE) &
162 : !$OMP SHARED(n_grid, factor, jacobi_scale, tikhonov) &
163 268 : !$OMP PRIVATE(j, i) SCHEDULE(DYNAMIC)
164 : DO j = 1, n_grid
165 : DO i = j, n_grid
166 : factor(i, j) = factor(i, j)**2*jacobi_scale(i)*jacobi_scale(j)
167 : END DO
168 : factor(j, j) = factor(j, j) + tikhonov
169 : END DO
170 : !$OMP END PARALLEL DO
171 268 : CALL timeset(routineN//'_dpotrf', handle2)
172 268 : CALL dpotrf('L', n_grid, factor, n_grid, info)
173 268 : CALL timestop(handle2)
174 268 : CALL timestop(handle)
175 804 : END SUBROUTINE factor_ri_rs_fit
176 :
177 : ! **************************************************************************************************
178 : !> \brief Evaluate the normalized cluster residual and its exact derivative in AO space.
179 : !>
180 : !> V_l = sqrt(d_l) phi_l, S = V V^T, K = S∘S + λI, and K Z' = b, with
181 : !> b_lP = V_l^T B_P V_l with the symmetric AO matrices B_P.
182 : !> The objective is ||B - A Z'||²/||B||², A_:l = svec(V_l V_l^T), without a penalty term.
183 : !> For λ>0 the derivative includes the response of the regularized coefficients.
184 : !> Each rank of atom_env holds a contiguous range of RI columns. K does not depend on P
185 : !> and is factored on every rank; each RI-column panel reuses V B_P for the RHS and
186 : !> gradient. The error terms and the AO gradient are summed over atom_env.
187 : !> The balancing convention and RI-RS reference are given in solve_ri_rs_fit.
188 : !> \param phi unscaled AO values indexed (grid point, AO)
189 : !> \param int_3c three-center integrals of this rank's RI columns indexed (AO, AO, RI),
190 : !> with both AO triangles filled
191 : !> \param integral_norm2 ||B||² over all RI columns of the cluster
192 : !> \param tikhonov regularization in the Jacobi-scaled space
193 : !> \param atom_env MPI group sharing the RI columns of the cluster
194 : !> \param normalized_error normalized squared residual
195 : !> \param fit_successful false if the target is empty or the regularized fit cannot be solved
196 : !> \param ao_gradient derivative with respect to the unscaled AO values
197 : !> \param max_abs_error optional largest absolute unweighted integral error
198 : ! **************************************************************************************************
199 209 : SUBROUTINE evaluate_ri_rs_fit(phi, int_3c, integral_norm2, tikhonov, atom_env, &
200 209 : normalized_error, fit_successful, ao_gradient, max_abs_error)
201 : REAL(KIND=dp), DIMENSION(:, :), INTENT(IN) :: phi
202 : REAL(KIND=dp), DIMENSION(:, :, :), INTENT(IN) :: int_3c
203 : REAL(KIND=dp), INTENT(IN) :: integral_norm2, tikhonov
204 : TYPE(mp_para_env_type), POINTER :: atom_env
205 : REAL(KIND=dp), INTENT(OUT) :: normalized_error
206 : LOGICAL, INTENT(OUT) :: fit_successful
207 : REAL(KIND=dp), DIMENSION(:, :), INTENT(OUT) :: ao_gradient
208 : REAL(KIND=dp), INTENT(OUT), OPTIONAL :: max_abs_error
209 :
210 : CHARACTER(LEN=*), PARAMETER :: routineN = 'evaluate_ri_rs_fit'
211 : INTEGER, PARAMETER :: residual_block_size = 64, &
212 : ri_panel_size = 16
213 :
214 : INTEGER :: first, handle, handle2, i, icache, imu, &
215 : info, inu, ip, j, last, n_ao, n_grid, &
216 : n_ri, panel_first, panel_last
217 : REAL(KIND=dp) :: error_scale, projection, residual_max, &
218 : residual_norm2, roundoff_estimate, &
219 : row_norm2, value, weight
220 209 : REAL(KIND=dp), ALLOCATABLE :: coefficient_work(:, :), factor(:, :), jacobi_scale(:), &
221 209 : overlap(:, :), partial_norm2(:), products(:, :, :), residual(:, :), v(:, :), z_prime(:, :)
222 : REAL(KIND=dp), DIMENSION(2) :: fit_terms
223 :
224 209 : CALL timeset(routineN, handle)
225 209 : n_grid = SIZE(phi, 1)
226 209 : n_ao = SIZE(phi, 2)
227 209 : n_ri = SIZE(int_3c, 3)
228 209 : CPASSERT(SIZE(int_3c, 1) == n_ao .AND. SIZE(int_3c, 2) == n_ao)
229 627 : CPASSERT(ALL(SHAPE(ao_gradient) == SHAPE(phi)))
230 209 : CPASSERT(tikhonov >= 0.0_dp)
231 60411 : ao_gradient(:, :) = 0.0_dp
232 209 : normalized_error = HUGE(normalized_error)
233 209 : IF (PRESENT(max_abs_error)) max_abs_error = HUGE(max_abs_error)
234 209 : fit_successful = .FALSE.
235 209 : IF (n_grid < 1 .OR. integral_norm2 <= TINY(1.0_dp)) THEN
236 0 : CALL timestop(handle)
237 0 : RETURN
238 : END IF
239 :
240 : ! Every rank factors the same K, so all ranks of atom_env return together on failure.
241 209 : CALL factor_ri_rs_fit(phi, tikhonov, factor, jacobi_scale, info, overlap)
242 209 : IF (info /= 0) THEN
243 0 : CALL timestop(handle)
244 0 : RETURN
245 : END IF
246 0 : ALLOCATE (v(n_grid, n_ao), products(n_grid, n_ao, MAX(1, MIN(ri_panel_size, n_ri))), &
247 2717 : coefficient_work(n_grid, n_ri), z_prime(n_grid, n_ri))
248 : !$OMP PARALLEL DO DEFAULT(NONE) &
249 209 : !$OMP SHARED(n_ao, n_grid, v, jacobi_scale, phi) PRIVATE(imu, i) SCHEDULE(STATIC)
250 : DO imu = 1, n_ao
251 : DO i = 1, n_grid
252 : v(i, imu) = SQRT(jacobi_scale(i))*phi(i, imu)
253 : END DO
254 : END DO
255 : !$OMP END PARALLEL DO
256 : ! Keep Z' and the coefficient response for the final grid-space symmetric product.
257 209 : fit_terms(:) = 0.0_dp
258 209 : CALL timeset(routineN//'_ri_panels', handle2)
259 1650 : DO panel_first = 1, n_ri, ri_panel_size
260 1441 : panel_last = MIN(panel_first + ri_panel_size - 1, n_ri)
261 : ! Each panel caches one product V B_P per RI function. The functions are independent, so
262 : ! each thread forms whole products and immediately sums its b_lP over μ in a fixed order.
263 : ! BLAS is called from inside the parallel region.
264 : !$OMP PARALLEL DO DEFAULT(NONE) &
265 : !$OMP SHARED(panel_first, panel_last, n_grid, n_ao, int_3c, v, products, coefficient_work) &
266 1441 : !$OMP PRIVATE(ip, icache, imu) SCHEDULE(STATIC)
267 : DO ip = panel_first, panel_last
268 : icache = ip - panel_first + 1
269 : CALL dgemm('N', 'N', n_grid, n_ao, n_ao, 1.0_dp, v, n_grid, int_3c(:, :, ip), n_ao, &
270 : 0.0_dp, products(:, :, icache), n_grid)
271 : coefficient_work(:, ip) = 0.0_dp
272 : DO imu = 1, n_ao
273 : coefficient_work(:, ip) = coefficient_work(:, ip) + v(:, imu)*products(:, imu, icache)
274 : END DO
275 : END DO
276 : !$OMP END PARALLEL DO
277 603677 : z_prime(:, panel_first:panel_last) = coefficient_work(:, panel_first:panel_last)
278 : CALL dpotrs('L', n_grid, panel_last - panel_first + 1, factor, n_grid, &
279 1441 : z_prime(:, panel_first:panel_last), n_grid, info)
280 1441 : IF (info /= 0) CPABORT("RI-RS grid fit Cholesky solve failed")
281 22541 : DO ip = panel_first, panel_last
282 602236 : fit_terms(1) = fit_terms(1) + DOT_PRODUCT(z_prime(:, ip), coefficient_work(:, ip))
283 603677 : fit_terms(2) = fit_terms(2) + DOT_PRODUCT(z_prime(:, ip), z_prime(:, ip))
284 : END DO
285 : ! Reuse the RHS for Y = K^-1 Z' and then C = Z' + λY; λ=0 needs no response solve.
286 603677 : coefficient_work(:, panel_first:panel_last) = z_prime(:, panel_first:panel_last)
287 1441 : IF (tikhonov > 0.0_dp) THEN
288 : CALL dpotrs('L', n_grid, panel_last - panel_first + 1, factor, n_grid, &
289 1441 : coefficient_work(:, panel_first:panel_last), n_grid, info)
290 1441 : IF (info /= 0) CPABORT("RI-RS grid fit response solve failed")
291 : coefficient_work(:, panel_first:panel_last) = z_prime(:, panel_first:panel_last) + &
292 603677 : tikhonov*coefficient_work(:, panel_first:panel_last)
293 : END IF
294 : ! Accumulate -Σ_P C_lP (V B_P)_l while the panel products are still available.
295 : ! Own AO columns, keeping RI accumulation order within every gradient element.
296 : !$OMP PARALLEL DO DEFAULT(NONE) &
297 : !$OMP SHARED(n_ao, n_grid, panel_first, panel_last, ao_gradient, coefficient_work, products) &
298 1441 : !$OMP PRIVATE(imu, ip, icache, i) SCHEDULE(STATIC)
299 : DO imu = 1, n_ao
300 : DO ip = panel_first, panel_last
301 : icache = ip - panel_first + 1
302 : DO i = 1, n_grid
303 : ao_gradient(i, imu) = ao_gradient(i, imu) - coefficient_work(i, ip)*products(i, imu, icache)
304 : END DO
305 : END DO
306 : END DO
307 : !$OMP END PARALLEL DO
308 1650 : IF (tikhonov > 0.0_dp) THEN
309 : ! Retain T = C - Z'/2 = Z'/2 + λY for M = Z'T^T + TZ'^T below.
310 : coefficient_work(:, panel_first:panel_last) = coefficient_work(:, panel_first:panel_last) - &
311 603677 : 0.5_dp*z_prime(:, panel_first:panel_last)
312 : END IF
313 : END DO
314 209 : CALL timestop(handle2)
315 209 : CALL atom_env%sum(fit_terms)
316 : ! K Z' = b implies ||B-AZ'||² = ||B||² - tr(Z'^T b) - λ ||Z'||².
317 209 : value = fit_terms(1)
318 209 : weight = tikhonov*fit_terms(2)
319 209 : residual_norm2 = integral_norm2 - value - weight
320 209 : error_scale = integral_norm2 + ABS(value) + weight
321 : ! Large coefficients can also amplify the factorization's backward error. The scaled
322 : ! Gram has diagonal <= 1, hence ||K||_2 <= n_grid + λ; use a conservative roundoff estimate.
323 : roundoff_estimate = 8.0_dp*EPSILON(1.0_dp)*REAL(n_grid, dp)* &
324 209 : (REAL(n_grid, dp) + tikhonov)*fit_terms(2)
325 : ! Near an exact fit, use bounded AO-column panels to avoid cancellation. This path also
326 : ! supplies the final maximum-error diagnostic, without storing a pair-by-RI residual.
327 209 : IF (PRESENT(max_abs_error) .OR. &
328 : residual_norm2 <= MAX(SQRT(EPSILON(1.0_dp))*error_scale, roundoff_estimate)) THEN
329 102 : CALL timeset(routineN//'_residual', handle2)
330 612 : ALLOCATE (residual(n_ao, MIN(residual_block_size, n_ao)), partial_norm2(MIN(residual_block_size, n_ao)))
331 102 : residual_norm2 = 0.0_dp
332 102 : residual_max = 0.0_dp
333 7618 : DO ip = 1, n_ri
334 15134 : DO first = 1, n_ao, residual_block_size
335 7516 : last = MIN(first + residual_block_size - 1, n_ao)
336 : !$OMP PARALLEL DO DEFAULT(NONE) &
337 : !$OMP SHARED(first, last, n_grid, products, z_prime, ip, v) &
338 7516 : !$OMP PRIVATE(inu, i) SCHEDULE(STATIC)
339 : DO inu = first, last
340 : DO i = 1, n_grid
341 : products(i, inu - first + 1, 1) = z_prime(i, ip)*v(i, inu)
342 : END DO
343 : END DO
344 : !$OMP END PARALLEL DO
345 : CALL dgemm('T', 'N', n_ao, last - first + 1, n_grid, -1.0_dp, v, n_grid, products(:, :, 1), n_grid, &
346 7516 : 0.0_dp, residual, n_ao)
347 : !$OMP PARALLEL DO DEFAULT(NONE) &
348 : !$OMP SHARED(first, last, int_3c, ip, residual, partial_norm2) PRIVATE(inu, imu, value, weight) &
349 7516 : !$OMP REDUCTION(MAX:residual_max) SCHEDULE(STATIC)
350 : DO inu = first, last
351 : partial_norm2(inu - first + 1) = 0.0_dp
352 : DO imu = 1, inu
353 : value = int_3c(imu, inu, ip) + residual(imu, inu - first + 1)
354 : weight = REAL(2 - MERGE(1, 0, imu == inu), dp)
355 : partial_norm2(inu - first + 1) = partial_norm2(inu - first + 1) + weight*value**2
356 : residual_max = MAX(residual_max, ABS(value))
357 : END DO
358 : END DO
359 : !$OMP END PARALLEL DO
360 63912 : DO inu = first, last
361 56396 : residual_norm2 = residual_norm2 + partial_norm2(inu - first + 1)
362 : END DO
363 : END DO
364 : END DO
365 102 : CALL atom_env%sum(residual_norm2)
366 102 : IF (PRESENT(max_abs_error)) THEN
367 29 : max_abs_error = residual_max
368 29 : CALL atom_env%max(max_abs_error)
369 : END IF
370 204 : CALL timestop(handle2)
371 : END IF
372 209 : DEALLOCATE (products)
373 209 : normalized_error = residual_norm2/integral_norm2
374 :
375 209 : CALL timeset(routineN//'_gradient', handle2)
376 : ! All panel solves are complete. Reuse the factor for this rank's part of
377 : ! M = Z'Z'^T + λ(Z'Y^T + YZ'^T); (M∘S)V is linear in M and summed with the AO gradient.
378 209 : IF (tikhonov > 0.0_dp) THEN
379 : CALL dsyr2k('L', 'N', n_grid, n_ri, 1.0_dp, z_prime, n_grid, coefficient_work, n_grid, &
380 209 : 0.0_dp, factor, n_grid)
381 : ELSE
382 0 : CALL dsyrk('L', 'N', n_grid, n_ri, 1.0_dp, z_prime, n_grid, 0.0_dp, factor, n_grid)
383 : END IF
384 : !$OMP PARALLEL DO DEFAULT(NONE) &
385 209 : !$OMP SHARED(n_grid, factor, overlap) PRIVATE(j, i) SCHEDULE(DYNAMIC)
386 : DO j = 1, n_grid
387 : DO i = j, n_grid
388 : factor(i, j) = factor(i, j)*overlap(i, j)
389 : END DO
390 : END DO
391 : !$OMP END PARALLEL DO
392 : ! Add (M∘S)V to the accumulated negative term, then normalize the AO gradient.
393 : CALL dsymm('L', 'L', n_grid, n_ao, 1.0_dp, factor, n_grid, v, n_grid, &
394 209 : 1.0_dp, ao_gradient, n_grid)
395 120613 : CALL atom_env%sum(ao_gradient)
396 : ! V_l = sqrt(d_l) phi_l. Differentiate the normalization only above the floor;
397 : ! on the clamped branch d_l is constant, including at the branch boundary.
398 : !$OMP PARALLEL DO DEFAULT(NONE) &
399 : !$OMP SHARED(n_grid, n_ao, integral_norm2, phi, ao_gradient, jacobi_scale) &
400 209 : !$OMP PRIVATE(i, imu, row_norm2, projection) SCHEDULE(STATIC)
401 : DO i = 1, n_grid
402 : DO imu = 1, n_ao
403 : ao_gradient(i, imu) = (4.0_dp/integral_norm2)*ao_gradient(i, imu)
404 : END DO
405 : row_norm2 = DOT_PRODUCT(phi(i, :), phi(i, :))
406 : IF (row_norm2 > SQRT(jacobi_floor)) THEN
407 : projection = DOT_PRODUCT(phi(i, :), ao_gradient(i, :))/row_norm2
408 : DO imu = 1, n_ao
409 : ao_gradient(i, imu) = ao_gradient(i, imu) - projection*phi(i, imu)
410 : END DO
411 : END IF
412 : DO imu = 1, n_ao
413 : ao_gradient(i, imu) = SQRT(jacobi_scale(i))*ao_gradient(i, imu)
414 : END DO
415 : END DO
416 : !$OMP END PARALLEL DO
417 209 : CALL timestop(handle2)
418 209 : fit_successful = .TRUE.
419 209 : CALL timestop(handle)
420 836 : END SUBROUTINE evaluate_ri_rs_fit
421 :
422 : ! **************************************************************************************************
423 : !> \brief Multiplies every matrix row by the corresponding diagonal entry:
424 : !> A(l, :) <- d_l A(l, :).
425 : !> \param matrix ...
426 : !> \param diagonal ...
427 : ! **************************************************************************************************
428 118 : SUBROUTINE scale_rows_by_diag(matrix, diagonal)
429 :
430 : REAL(KIND=dp), DIMENSION(:, :), INTENT(INOUT) :: matrix
431 : REAL(KIND=dp), DIMENSION(:), INTENT(IN) :: diagonal
432 :
433 : CHARACTER(LEN=*), PARAMETER :: routineN = 'scale_rows_by_diag'
434 :
435 : INTEGER :: handle, i, j
436 :
437 118 : CALL timeset(routineN, handle)
438 118 : CPASSERT(SIZE(diagonal) == SIZE(matrix, 1))
439 :
440 : !$OMP PARALLEL DO DEFAULT(NONE) &
441 : !$OMP SHARED(matrix, diagonal) &
442 : !$OMP PRIVATE(j, i) &
443 118 : !$OMP SCHEDULE(STATIC)
444 : DO j = 1, SIZE(matrix, 2)
445 : DO i = 1, SIZE(matrix, 1)
446 : matrix(i, j) = matrix(i, j)*diagonal(i)
447 : END DO
448 : END DO
449 : !$OMP END PARALLEL DO
450 :
451 118 : CALL timestop(handle)
452 :
453 118 : END SUBROUTINE scale_rows_by_diag
454 :
455 : ! **************************************************************************************************
456 : !> \brief Stores dense Z_lP columns in the distributed block-sparse matrix.
457 : !> \param mat_Z_lP ...
458 : !> \param Z_local ...
459 : !> \param local_grid_idx ...
460 : !> \param n_local_grid ...
461 : !> \param n_loc_ri ...
462 : !> \param atom_P ...
463 : !> \param r_blk_sizes ...
464 : !> \param row_offset ...
465 : !> \param eps_filter ...
466 : ! **************************************************************************************************
467 59 : SUBROUTINE store_Z_lP_columns(mat_Z_lP, Z_local, local_grid_idx, n_local_grid, n_loc_ri, &
468 59 : atom_P, r_blk_sizes, row_offset, eps_filter)
469 :
470 : TYPE(dbcsr_type), INTENT(INOUT) :: mat_Z_lP
471 : REAL(KIND=dp), DIMENSION(:, :), INTENT(IN) :: Z_local
472 : INTEGER, DIMENSION(:), INTENT(IN) :: local_grid_idx
473 : INTEGER, INTENT(IN) :: n_local_grid, n_loc_ri, atom_P
474 : INTEGER, DIMENSION(:), INTENT(IN) :: r_blk_sizes, row_offset
475 : REAL(KIND=dp), INTENT(IN) :: eps_filter
476 :
477 : CHARACTER(LEN=*), PARAMETER :: routineN = 'store_Z_lP_columns'
478 :
479 : INTEGER :: current_chunk_size, g_pt, handle, i_blk, &
480 : loc_ptr, r_end, r_start
481 59 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: Z_blk
482 :
483 59 : CALL timeset(routineN, handle)
484 :
485 999 : ALLOCATE (Z_blk(MAXVAL(r_blk_sizes), n_loc_ri))
486 : ! local_grid_idx is ascending, so one walking pointer maps sphere rows to global grid blocks.
487 59 : loc_ptr = 1
488 :
489 822 : DO i_blk = 1, SIZE(r_blk_sizes)
490 763 : r_start = row_offset(i_blk) + 1
491 763 : r_end = row_offset(i_blk) + r_blk_sizes(i_blk)
492 763 : current_chunk_size = r_blk_sizes(i_blk)
493 763 : Z_blk = 0.0_dp
494 :
495 21257 : DO WHILE (loc_ptr <= n_local_grid)
496 21195 : g_pt = local_grid_idx(loc_ptr)
497 21195 : IF (g_pt > r_end) EXIT
498 273912 : Z_blk(g_pt - r_start + 1, 1:n_loc_ri) = Z_local(loc_ptr, 1:n_loc_ri)
499 21195 : loc_ptr = loc_ptr + 1
500 : END DO
501 :
502 : ! DBCSR routes each retained block to its global owner when the caller finalizes mat_Z_lP.
503 267121 : IF (MAXVAL(ABS(Z_blk(1:current_chunk_size, 1:n_loc_ri))) > eps_filter) THEN
504 : CALL dbcsr_put_block(mat_Z_lP, row=i_blk, col=atom_P, &
505 754 : block=Z_blk(1:current_chunk_size, 1:n_loc_ri))
506 : END IF
507 : END DO
508 :
509 59 : DEALLOCATE (Z_blk)
510 :
511 59 : CALL timestop(handle)
512 :
513 59 : END SUBROUTINE store_Z_lP_columns
514 :
515 : ! **************************************************************************************************
516 : !> \brief Solves the Jacobi-scaled, regularized RI-RS system with ScaLAPACK.
517 : !>
518 : !> Each rank builds its block-cyclic part of
519 : !>
520 : !> D'_ll' = d_l [Σ_μ ϕ_μ(r_l) ϕ_μ(r_l')]² d_l' + λ δ_ll'.
521 : !>
522 : !> The Cholesky solution Z'_lP is gathered into the replicated right-hand side.
523 : !> The caller then restores Z_lP = d_l Z'_lP.
524 : !> \param phi_local replicated AO values on the fitting grid
525 : !> \param d_vec Jacobi scaling vector d_l, with the same floor as in the dense solve
526 : !> \param d_lp replicated scaled RHS d_l d_lP on entry, intermediate solution Z'_lP on return
527 : !> \param tikhonov ...
528 : !> \param para_env_sub ...
529 : !> \param blacs_env_sub ...
530 : ! **************************************************************************************************
531 0 : SUBROUTINE solve_D_lp_distributed(phi_local, d_vec, d_lp, tikhonov, para_env_sub, blacs_env_sub)
532 :
533 : REAL(KIND=dp), DIMENSION(:, :), INTENT(IN) :: phi_local
534 : REAL(KIND=dp), DIMENSION(:), INTENT(IN) :: d_vec
535 : REAL(KIND=dp), DIMENSION(:, :), INTENT(INOUT) :: d_lp
536 : REAL(KIND=dp), INTENT(IN) :: tikhonov
537 : TYPE(mp_para_env_type), POINTER :: para_env_sub
538 : TYPE(cp_blacs_env_type), POINTER :: blacs_env_sub
539 :
540 : CHARACTER(LEN=*), PARAMETER :: routineN = 'solve_D_lp_distributed'
541 :
542 : INTEGER :: handle, i_loc, ig, info, j_loc, jg, &
543 : n_ao, n_loc, n_rhs, ncol_local, &
544 : nrow_local
545 0 : INTEGER, DIMENSION(:), POINTER :: col_indices, row_indices
546 : REAL(KIND=dp), CONTIGUOUS, DIMENSION(:, :), &
547 0 : POINTER :: local_data
548 : TYPE(cp_fm_struct_type), POINTER :: fm_struct_b, fm_struct_D
549 : TYPE(cp_fm_type) :: fm_b, fm_D
550 :
551 0 : CALL timeset(routineN, handle)
552 0 : n_loc = SIZE(phi_local, 1)
553 0 : n_ao = SIZE(phi_local, 2)
554 0 : n_rhs = SIZE(d_lp, 2)
555 :
556 0 : NULLIFY (fm_struct_D, fm_struct_b)
557 : CALL cp_fm_struct_create(fm_struct_D, para_env=para_env_sub, &
558 : context=blacs_env_sub, &
559 0 : nrow_global=n_loc, ncol_global=n_loc)
560 : CALL cp_fm_struct_create(fm_struct_b, para_env=para_env_sub, &
561 : context=blacs_env_sub, &
562 0 : nrow_global=n_loc, ncol_global=n_rhs)
563 0 : CALL cp_fm_create(fm_D, fm_struct_D)
564 0 : CALL cp_fm_create(fm_b, fm_struct_b)
565 :
566 : CALL cp_fm_get_info(fm_D, nrow_local=nrow_local, ncol_local=ncol_local, &
567 : row_indices=row_indices, col_indices=col_indices, &
568 0 : local_data=local_data)
569 :
570 : ! Build only the rank-owned Gram tiles, avoiding a replicated n_grid × n_grid matrix.
571 0 : IF (nrow_local > 0 .AND. ncol_local > 0) THEN
572 0 : BLOCK
573 : INTEGER, PARAMETER :: ntile = 1024
574 : INTEGER :: ib, ie, jb, je, mb, kb, ti, tj, handle_dgemm
575 0 : REAL(KIND=dp), ALLOCATABLE :: gram_t(:, :), phi_cols_t(:, :), phi_rows_t(:, :)
576 0 : ALLOCATE (phi_rows_t(ntile, n_ao), phi_cols_t(n_ao, ntile), gram_t(ntile, ntile))
577 : ! Pack AO values for the grid rows/columns selected by the block-cyclic FM layout.
578 0 : DO ib = 1, nrow_local, ntile
579 0 : ie = MIN(ib + ntile - 1, nrow_local)
580 0 : mb = ie - ib + 1
581 : !$OMP PARALLEL DO DEFAULT(NONE) &
582 : !$OMP SHARED(mb, n_ao, phi_rows_t, phi_local, row_indices, ib) &
583 0 : !$OMP PRIVATE(ti, j_loc) SCHEDULE(STATIC)
584 : DO j_loc = 1, n_ao
585 : DO ti = 1, mb
586 : phi_rows_t(ti, j_loc) = phi_local(row_indices(ib + ti - 1), j_loc)
587 : END DO
588 : END DO
589 : !$OMP END PARALLEL DO
590 0 : DO jb = 1, ncol_local, ntile
591 0 : je = MIN(jb + ntile - 1, ncol_local)
592 0 : kb = je - jb + 1
593 : !$OMP PARALLEL DO DEFAULT(NONE) &
594 : !$OMP SHARED(kb, n_ao, phi_cols_t, phi_local, col_indices, jb) &
595 0 : !$OMP PRIVATE(tj, i_loc) SCHEDULE(STATIC)
596 : DO tj = 1, kb
597 : DO i_loc = 1, n_ao
598 : phi_cols_t(i_loc, tj) = phi_local(col_indices(jb + tj - 1), i_loc)
599 : END DO
600 : END DO
601 : !$OMP END PARALLEL DO
602 0 : CALL timeset(routineN//'_dgemm', handle_dgemm)
603 : CALL dgemm('N', 'N', mb, kb, n_ao, &
604 : 1.0_dp, phi_rows_t, ntile, phi_cols_t, n_ao, &
605 0 : 0.0_dp, gram_t, ntile)
606 0 : CALL timestop(handle_dgemm)
607 : ! Square the AO Gram entries, apply d_l d_l', and add λ on the global diagonal.
608 : !$OMP PARALLEL DO DEFAULT(NONE) &
609 : !$OMP SHARED(mb, kb, gram_t, d_vec, row_indices, col_indices, ib, jb) &
610 : !$OMP SHARED(local_data, tikhonov) &
611 0 : !$OMP PRIVATE(ti, tj, ig, jg) SCHEDULE(STATIC)
612 : DO tj = 1, kb
613 : jg = col_indices(jb + tj - 1)
614 : DO ti = 1, mb
615 : ig = row_indices(ib + ti - 1)
616 : local_data(ib + ti - 1, jb + tj - 1) = &
617 : gram_t(ti, tj)*gram_t(ti, tj)*d_vec(ig)*d_vec(jg)
618 : IF (ig == jg) THEN
619 : local_data(ib + ti - 1, jb + tj - 1) = &
620 : local_data(ib + ti - 1, jb + tj - 1) + tikhonov
621 : END IF
622 : END DO
623 : END DO
624 : !$OMP END PARALLEL DO
625 : END DO
626 : END DO
627 0 : DEALLOCATE (phi_rows_t, phi_cols_t, gram_t)
628 : END BLOCK
629 : END IF
630 :
631 : ! Distribute the pre-scaled RHS and solve for Z'_lP within the BLACS subgroup.
632 0 : CALL cp_fm_set_submatrix(fm_b, d_lp)
633 :
634 0 : CALL cp_fm_cholesky_decompose(fm_D, n=n_loc, info_out=info)
635 0 : IF (info /= 0) CPABORT("pdpotrf failed in solve_D_lp_distributed")
636 :
637 0 : CALL cp_fm_cholesky_solve(fm_D, fm_b, n=n_loc, info_out=info)
638 0 : IF (info /= 0) CPABORT("pdpotrs failed in solve_D_lp_distributed")
639 :
640 : ! Replicate Z'_lP within the subgroup; the caller applies the final Jacobi rescaling.
641 0 : CALL cp_fm_get_submatrix(fm_b, d_lp)
642 :
643 0 : CALL cp_fm_release(fm_D)
644 0 : CALL cp_fm_release(fm_b)
645 0 : CALL cp_fm_struct_release(fm_struct_D)
646 0 : CALL cp_fm_struct_release(fm_struct_b)
647 :
648 0 : CALL timestop(handle)
649 :
650 0 : END SUBROUTINE solve_D_lp_distributed
651 :
652 : END MODULE ri_rs_fitting
|