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 Optimize automatically initialized atom-centred RI-RS grids.
10 : !> \par History
11 : !> 09.2026 created [Jan Wilhelm]
12 : ! **************************************************************************************************
13 : MODULE ri_rs_grid_optimization
14 : USE basis_set_types, ONLY: gto_basis_set_type
15 : USE cell_types, ONLY: cell_type
16 : USE cp_lbfgs, ONLY: setulb
17 : USE gw_utils_compute_integrals, ONLY: build_3c_integral_block_ctx,&
18 : gw_3c_ctx_create,&
19 : gw_3c_ctx_release,&
20 : gw_3c_ctx_type,&
21 : gw_3c_ws_create,&
22 : gw_3c_ws_release,&
23 : gw_3c_ws_type
24 : USE kinds, ONLY: dp,&
25 : dp_size
26 : USE machine, ONLY: m_flush,&
27 : m_walltime
28 : USE message_passing, ONLY: mp_mem_avail_per_rank_GB,&
29 : mp_para_env_release,&
30 : mp_para_env_type
31 : USE particle_types, ONLY: particle_type
32 : USE post_scf_bandstructure_types, ONLY: post_scf_bandstructure_type
33 : USE ri_rs_fitting, ONLY: evaluate_ri_rs_fit
34 : USE ri_rs_grid_initialization, ONLY: initialize_ri_rs_grid
35 : USE ri_rs_types, ONLY: rirs_grid_type
36 : USE ri_rs_utils, ONLY: evaluate_ao_basis_on_points,&
37 : get_rirs_cluster_atoms
38 : #include "./base/base_uses.f90"
39 :
40 : IMPLICIT NONE
41 : PRIVATE
42 :
43 : PUBLIC :: optimize_ri_rs_grid
44 :
45 : CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'ri_rs_grid_optimization'
46 :
47 : ! Share of the available memory (in GB of 1.0E9 bytes) for the cluster integrals.
48 : REAL(KIND=dp), PARAMETER :: integral_memory_share = 0.8_dp, bytes_per_GB = 1.0E9_dp
49 :
50 : TYPE, PRIVATE :: local_cluster_3c_integrals_type
51 : INTEGER, ALLOCATABLE :: atom_indices(:)
52 : REAL(KIND=dp), ALLOCATABLE :: centers(:, :), Int_3c(:, :, :)
53 : REAL(KIND=dp) :: Int_3c_norm2
54 : END TYPE local_cluster_3c_integrals_type
55 :
56 : CONTAINS
57 :
58 : ! **************************************************************************************************
59 : !> \brief Initialize Lebedev grids and subsequently optimize their grid-point coordinates.
60 : !> \param bs_env ...
61 : ! **************************************************************************************************
62 24 : SUBROUTINE optimize_ri_rs_grid(bs_env)
63 : TYPE(post_scf_bandstructure_type), POINTER :: bs_env
64 :
65 : CHARACTER(len=*), PARAMETER :: routineN = 'optimize_ri_rs_grid'
66 :
67 : INTEGER :: handle
68 24 : TYPE(local_cluster_3c_integrals_type), ALLOCATABLE :: cluster_3c_int(:)
69 : TYPE(mp_para_env_type), POINTER :: atom_env
70 :
71 24 : CALL timeset(routineN, handle)
72 :
73 : ! Validate all state and user input before the initialization consumes them.
74 24 : CALL validate_ri_rs_grid_optimization_input(bs_env)
75 :
76 : ! setup Lebedev initial grid inside Voronoi volume, select points with Cholesky decomp.
77 24 : CALL initialize_ri_rs_grid(bs_env)
78 :
79 : ! Validate the initialized RI-RS state and report the start of the optimization.
80 24 : CALL prepare_ri_rs_grid_optimization(bs_env)
81 :
82 : ! C_A contains atom images within R_cut; distribute (μν|P) within its MPI group.
83 24 : CALL build_cluster_3c_int(bs_env, atom_env, cluster_3c_int)
84 :
85 : ! E_A = Σ_{μνP∈C_A} [(μν|P) - Σ_{l∈G_A} ϕ_μ(r_l) ϕ_ν(r_l) Z_lP^(A)]².
86 : ! Minimize E_loc(norm) = N_atom^(-1) Σ_A [E_A / Σ_{μνP∈C_A} |(μν|P)|²].
87 24 : CALL optimize_grid_coordinates(bs_env, atom_env, cluster_3c_int)
88 24 : CALL mp_para_env_release(atom_env)
89 :
90 24 : CALL timestop(handle)
91 :
92 53 : END SUBROUTINE optimize_ri_rs_grid
93 :
94 : ! **************************************************************************************************
95 : !> \brief Validate state and user input needed to initialize and optimize RI-RS grids.
96 : !> \param bs_env ...
97 : ! **************************************************************************************************
98 24 : SUBROUTINE validate_ri_rs_grid_optimization_input(bs_env)
99 : TYPE(post_scf_bandstructure_type), POINTER :: bs_env
100 :
101 : INTEGER :: iatom, ikind
102 :
103 24 : CPASSERT(ASSOCIATED(bs_env%ri_rs%cell))
104 24 : CPASSERT(ASSOCIATED(bs_env%ri_rs%particle_set))
105 24 : CPASSERT(ASSOCIATED(bs_env%para_env))
106 24 : CPASSERT(ALLOCATED(bs_env%basis_set_AO))
107 24 : CPASSERT(ALLOCATED(bs_env%basis_set_RI))
108 24 : CPASSERT(ALLOCATED(bs_env%sizes_AO))
109 24 : CPASSERT(SIZE(bs_env%ri_rs%particle_set) == bs_env%n_atom)
110 24 : CPASSERT(SIZE(bs_env%sizes_AO) == bs_env%n_atom)
111 24 : CPASSERT(SIZE(bs_env%basis_set_AO) == SIZE(bs_env%basis_set_RI))
112 :
113 24 : IF (bs_env%ri_rs%grid_opt%rs_ao_ratio == 0.0_dp) THEN
114 0 : CPABORT("GRID_OPTIMIZATION%RS_AO_RATIO must be nonzero")
115 : END IF
116 24 : IF (bs_env%ri_rs%grid_opt%cutoff_atomic_cluster == 0.0_dp) THEN
117 0 : CPABORT("GRID_OPTIMIZATION%CUTOFF_ATOMIC_CLUSTER must be nonzero")
118 : END IF
119 24 : IF (bs_env%ri_rs%grid_opt%max_iter < 1) THEN
120 0 : CPABORT("GRID_OPTIMIZATION%MAX_ITER must be positive")
121 : END IF
122 :
123 82 : DO iatom = 1, bs_env%n_atom
124 58 : ikind = bs_env%ri_rs%particle_set(iatom)%atomic_kind%kind_number
125 58 : CPASSERT(ikind >= 1 .AND. ikind <= SIZE(bs_env%basis_set_AO))
126 58 : CPASSERT(ASSOCIATED(bs_env%basis_set_AO(ikind)%gto_basis_set))
127 58 : CPASSERT(ASSOCIATED(bs_env%basis_set_RI(ikind)%gto_basis_set))
128 58 : IF (bs_env%sizes_AO(iatom) < 1 .OR. &
129 24 : bs_env%basis_set_RI(ikind)%gto_basis_set%nsgf < 1) THEN
130 0 : CPABORT("Every atom in GRID_OPTIMIZATION needs ORB and RI_AUX functions")
131 : END IF
132 : END DO
133 24 : END SUBROUTINE validate_ri_rs_grid_optimization_input
134 :
135 : ! **************************************************************************************************
136 : !> \brief Validate initialized RI-RS state and announce the grid optimization.
137 : !> \param bs_env ...
138 : ! **************************************************************************************************
139 24 : SUBROUTINE prepare_ri_rs_grid_optimization(bs_env)
140 : TYPE(post_scf_bandstructure_type), POINTER :: bs_env
141 :
142 : INTEGER :: iatom, unit_nr
143 :
144 24 : CPASSERT(ALLOCATED(bs_env%ri_rs%atomic_grids))
145 24 : CPASSERT(SIZE(bs_env%ri_rs%atomic_grids) == bs_env%n_atom)
146 :
147 82 : DO iatom = 1, bs_env%n_atom
148 58 : CPASSERT(ALLOCATED(bs_env%ri_rs%atomic_grids(iatom)%raw_points))
149 58 : CPASSERT(SIZE(bs_env%ri_rs%atomic_grids(iatom)%raw_points, 1) == 3)
150 82 : CPASSERT(SIZE(bs_env%ri_rs%atomic_grids(iatom)%raw_points, 2) > 0)
151 : END DO
152 :
153 24 : unit_nr = bs_env%unit_nr
154 24 : IF (unit_nr > 0) THEN
155 12 : WRITE (unit_nr, '(T2,A)') 'Started RI-RS grid optimization'
156 12 : CALL m_flush(unit_nr)
157 : END IF
158 24 : END SUBROUTINE prepare_ri_rs_grid_optimization
159 :
160 : ! **************************************************************************************************
161 : !> \brief Evaluate the normalized local objective E_loc and its gradient for one L-BFGS trial vector.
162 : !> \param grid_coordinates Flattened atom-relative grid coordinates.
163 : !> \param atom_coordinate_offsets Starting coordinate offset for each atom.
164 : !> \param bs_env ...
165 : !> \param atom_env ...
166 : !> \param cluster_3c_int Rank-local cluster three-centre integrals.
167 : !> \param normalized_error Mean normalized squared three-centre-integral error.
168 : !> \param coordinate_gradient Derivative of normalized_error with respect to coordinates.
169 : !> \param fit_successful Whether every local-cluster evaluation succeeded.
170 : !> \param max_abs_error Optional largest absolute three-centre-integral error.
171 : ! **************************************************************************************************
172 162 : SUBROUTINE grid_objective(grid_coordinates, atom_coordinate_offsets, bs_env, atom_env, &
173 324 : cluster_3c_int, normalized_error, coordinate_gradient, &
174 : fit_successful, max_abs_error)
175 : REAL(KIND=dp), DIMENSION(:), INTENT(IN) :: grid_coordinates
176 : INTEGER, DIMENSION(:), INTENT(IN) :: atom_coordinate_offsets
177 : TYPE(post_scf_bandstructure_type), POINTER :: bs_env
178 : TYPE(mp_para_env_type), POINTER :: atom_env
179 : TYPE(local_cluster_3c_integrals_type), &
180 : DIMENSION(:), INTENT(IN) :: cluster_3c_int
181 : REAL(KIND=dp), INTENT(OUT) :: normalized_error
182 : REAL(KIND=dp), DIMENSION(:), INTENT(OUT) :: coordinate_gradient
183 : LOGICAL, INTENT(OUT) :: fit_successful
184 : REAL(KIND=dp), INTENT(OUT), OPTIONAL :: max_abs_error
185 :
186 : INTEGER :: evaluated_cluster_count, iatom, &
187 : icluster, ilocal, jatom, metadata(2), &
188 : ngroups, nvalues, root, &
189 : successful_ranks
190 162 : INTEGER, ALLOCATABLE :: cluster_atoms(:), gradient_offsets(:)
191 : LOGICAL :: cluster_fit_successful
192 : REAL(KIND=dp) :: cluster_max_abs_error, &
193 : cluster_normalized_error
194 162 : REAL(KIND=dp), ALLOCATABLE :: cluster_coordinate_gradient(:, :), local_errors(:), &
195 162 : local_gradients(:)
196 : TYPE(mp_para_env_type), POINTER :: para_env
197 :
198 162 : para_env => bs_env%para_env
199 162 : CPASSERT(SIZE(coordinate_gradient) == SIZE(grid_coordinates))
200 : CALL unpack_lbfgs_grid_coordinates(grid_coordinates, atom_coordinate_offsets, &
201 162 : bs_env%ri_rs%atomic_grids)
202 162 : normalized_error = 0.0_dp
203 16308 : coordinate_gradient = 0.0_dp
204 162 : evaluated_cluster_count = 0
205 162 : IF (PRESENT(max_abs_error)) max_abs_error = 0.0_dp
206 162 : fit_successful = .TRUE.
207 810 : ALLOCATE (gradient_offsets(SIZE(cluster_3c_int) + 1), local_errors(SIZE(cluster_3c_int)))
208 162 : gradient_offsets(1) = 1
209 371 : DO icluster = 1, SIZE(cluster_3c_int)
210 209 : nvalues = 0
211 762 : DO jatom = 1, SIZE(cluster_3c_int(icluster)%atom_indices)
212 553 : iatom = cluster_3c_int(icluster)%atom_indices(jatom)
213 762 : nvalues = nvalues + 3*bs_env%ri_rs%atomic_grids(iatom)%npts
214 : END DO
215 371 : gradient_offsets(icluster + 1) = gradient_offsets(icluster) + nvalues
216 : END DO
217 486 : ALLOCATE (local_gradients(gradient_offsets(SIZE(cluster_3c_int) + 1) - 1))
218 371 : DO icluster = 1, SIZE(cluster_3c_int)
219 209 : IF (PRESENT(max_abs_error)) THEN
220 : CALL evaluate_local_cluster(cluster_3c_int(icluster), bs_env, atom_env, &
221 : cluster_normalized_error, cluster_coordinate_gradient, &
222 29 : cluster_fit_successful, cluster_max_abs_error)
223 29 : max_abs_error = MAX(max_abs_error, cluster_max_abs_error)
224 : ELSE
225 : CALL evaluate_local_cluster(cluster_3c_int(icluster), bs_env, atom_env, &
226 : cluster_normalized_error, cluster_coordinate_gradient, &
227 180 : cluster_fit_successful)
228 : END IF
229 209 : IF (.NOT. cluster_fit_successful) THEN
230 0 : fit_successful = .FALSE.
231 0 : IF (ALLOCATED(cluster_coordinate_gradient)) DEALLOCATE (cluster_coordinate_gradient)
232 : EXIT
233 : END IF
234 209 : IF (atom_env%mepos == 0) THEN
235 209 : local_errors(icluster) = cluster_normalized_error
236 627 : nvalues = SIZE(cluster_coordinate_gradient)
237 : local_gradients(gradient_offsets(icluster):gradient_offsets(icluster + 1) - 1) = &
238 418 : RESHAPE(cluster_coordinate_gradient, [nvalues])
239 209 : evaluated_cluster_count = evaluated_cluster_count + 1
240 : END IF
241 371 : DEALLOCATE (cluster_coordinate_gradient)
242 : END DO
243 162 : successful_ranks = MERGE(1, 0, fit_successful)
244 162 : CALL para_env%sum(evaluated_cluster_count)
245 162 : CALL para_env%sum(successful_ranks)
246 162 : IF (PRESENT(max_abs_error)) CALL para_env%max(max_abs_error)
247 : fit_successful = evaluated_cluster_count == bs_env%n_atom .AND. &
248 162 : successful_ranks == para_env%num_pe
249 162 : IF (.NOT. fit_successful) THEN
250 0 : normalized_error = HUGE(normalized_error)
251 0 : coordinate_gradient = 0.0_dp
252 : RETURN
253 : END IF
254 : ! Add complete cluster contributions in atom order, independent of the rank assignment.
255 162 : ngroups = MIN(bs_env%n_atom, para_env%num_pe)
256 580 : DO iatom = 1, bs_env%n_atom
257 418 : root = MOD(iatom - 1, ngroups)
258 418 : ilocal = (iatom - 1)/ngroups + 1
259 418 : IF (para_env%mepos == root) THEN
260 : metadata = [SIZE(cluster_3c_int(ilocal)%atom_indices), &
261 627 : gradient_offsets(ilocal + 1) - gradient_offsets(ilocal)]
262 209 : cluster_normalized_error = local_errors(ilocal)
263 : END IF
264 418 : CALL para_env%bcast(metadata, root)
265 418 : CALL para_env%bcast(cluster_normalized_error, root)
266 2090 : ALLOCATE (cluster_atoms(metadata(1)), cluster_coordinate_gradient(3, metadata(2)/3))
267 418 : IF (para_env%mepos == root) THEN
268 762 : cluster_atoms(:) = cluster_3c_int(ilocal)%atom_indices
269 : cluster_coordinate_gradient(:, :) = RESHAPE( &
270 : local_gradients(gradient_offsets(ilocal):gradient_offsets(ilocal + 1) - 1), &
271 627 : SHAPE(cluster_coordinate_gradient))
272 : END IF
273 418 : CALL para_env%bcast(cluster_atoms, root)
274 418 : CALL para_env%bcast(cluster_coordinate_gradient, root)
275 418 : normalized_error = normalized_error + cluster_normalized_error
276 : CALL accumulate_atom_gradient(cluster_atoms, bs_env%ri_rs%atomic_grids, &
277 418 : atom_coordinate_offsets, cluster_coordinate_gradient, coordinate_gradient)
278 580 : DEALLOCATE (cluster_atoms, cluster_coordinate_gradient)
279 : END DO
280 : ! E_loc(norm) = (1/N_atom) Σ_A [E_A / Σ_{μνP ∈ C_A} |(μν|P)|²].
281 162 : normalized_error = normalized_error/REAL(bs_env%n_atom, dp)
282 16308 : coordinate_gradient = coordinate_gradient/REAL(bs_env%n_atom, dp)
283 162 : END SUBROUTINE grid_objective
284 :
285 : ! **************************************************************************************************
286 : !> \brief Drive L-BFGS, repeatedly evaluating E_loc and its gradient, and return the best trial.
287 : !> \param bs_env ...
288 : !> \param atom_env ...
289 : !> \param cluster_3c_int Rank-local cluster three-centre integrals.
290 : ! **************************************************************************************************
291 24 : SUBROUTINE optimize_grid_coordinates(bs_env, atom_env, cluster_3c_int)
292 : TYPE(post_scf_bandstructure_type), POINTER :: bs_env
293 : TYPE(mp_para_env_type), POINTER :: atom_env
294 : TYPE(local_cluster_3c_integrals_type), &
295 : DIMENSION(:), INTENT(IN) :: cluster_3c_int
296 :
297 : CHARACTER(LEN=*), PARAMETER :: routineN = 'optimize_grid_coordinates'
298 : INTEGER, PARAMETER :: lbfgs_history = 7
299 : REAL(KIND=dp), PARAMETER :: lbfgs_factr = 0.0_dp, &
300 : lbfgs_pgtol = 1.0E-9_dp
301 :
302 : CHARACTER(LEN=60) :: line_search_state, optimizer_task
303 : INTEGER :: evaluation_count, handle, iatom, &
304 : npoints, unit_nr
305 24 : INTEGER, ALLOCATABLE :: atom_coordinate_offsets(:), &
306 24 : bound_types(:), integer_workspace(:)
307 : INTEGER, DIMENSION(44) :: integer_state
308 : LOGICAL :: evaluation_successful, have_best
309 : LOGICAL, DIMENSION(4) :: logical_state
310 : REAL(KIND=dp) :: best_normalized_error, evaluation_start, &
311 : evaluation_time, max_abs_error, &
312 : normalized_error, start_time
313 : REAL(KIND=dp), ALLOCATABLE :: best_grid_coordinates(:), coordinate_gradient(:), &
314 24 : grid_coordinates(:), lower_bounds(:), upper_bounds(:), workspace(:)
315 : REAL(KIND=dp), DIMENSION(29) :: real_state
316 :
317 24 : CALL timeset(routineN, handle)
318 :
319 24 : start_time = m_walltime()
320 :
321 : CALL pack_lbfgs_grid_coordinates(bs_env%ri_rs%atomic_grids, grid_coordinates, &
322 24 : atom_coordinate_offsets)
323 24 : CPASSERT(SIZE(grid_coordinates) > 0)
324 :
325 : ALLOCATE (best_grid_coordinates(SIZE(grid_coordinates)), &
326 : coordinate_gradient(SIZE(grid_coordinates)), &
327 : lower_bounds(SIZE(grid_coordinates)), &
328 : upper_bounds(SIZE(grid_coordinates)), &
329 : bound_types(SIZE(grid_coordinates)), &
330 : integer_workspace(3*SIZE(grid_coordinates)), &
331 : workspace(2*lbfgs_history*SIZE(grid_coordinates) + &
332 288 : 5*SIZE(grid_coordinates) + 11*lbfgs_history**2 + 8*lbfgs_history))
333 : ! L-BFGS-B ignores lower_bounds and upper_bounds when bound_types is zero.
334 24 : lower_bounds = 0.0_dp
335 24 : upper_bounds = 0.0_dp
336 24 : bound_types = 0
337 24 : optimizer_task = 'START'
338 24 : line_search_state = ''
339 24 : normalized_error = HUGE(normalized_error)
340 24 : coordinate_gradient = 0.0_dp
341 24 : workspace = 0.0_dp
342 24 : integer_workspace = 0
343 24 : logical_state = .FALSE.
344 24 : integer_state = 0
345 24 : real_state = 0.0_dp
346 24 : evaluation_count = 0
347 24 : have_best = .FALSE.
348 24 : best_normalized_error = HUGE(best_normalized_error)
349 3282 : best_grid_coordinates(:) = grid_coordinates
350 :
351 24 : IF (bs_env%unit_nr > 0) THEN
352 : WRITE (bs_env%unit_nr, '(T2,A,T69,I12)') &
353 12 : 'RI-RS grid maximum evaluations (MAX_ITER):', bs_env%ri_rs%grid_opt%max_iter
354 12 : CALL m_flush(bs_env%unit_nr)
355 : END IF
356 : DO
357 : CALL setulb(SIZE(grid_coordinates), lbfgs_history, grid_coordinates, &
358 : lower_bounds, upper_bounds, bound_types, &
359 : normalized_error, coordinate_gradient, lbfgs_factr, lbfgs_pgtol, &
360 : workspace, integer_workspace, optimizer_task, -1, line_search_state, &
361 250 : logical_state, integer_state, real_state, -1.0_dp)
362 274 : IF (optimizer_task(1:2) == 'FG') THEN
363 144 : IF (evaluation_count >= bs_env%ri_rs%grid_opt%max_iter) EXIT
364 138 : evaluation_start = m_walltime()
365 : CALL grid_objective(grid_coordinates, atom_coordinate_offsets, bs_env, atom_env, &
366 : cluster_3c_int, normalized_error, coordinate_gradient, &
367 138 : evaluation_successful)
368 138 : evaluation_time = m_walltime() - evaluation_start
369 138 : evaluation_count = evaluation_count + 1
370 138 : IF (evaluation_count == 1 .AND. bs_env%unit_nr > 0) THEN
371 : WRITE (bs_env%unit_nr, '(T2,A,T61,ES20.12)') &
372 12 : 'RI-RS grid initial normalized error', normalized_error
373 : END IF
374 138 : IF (bs_env%unit_nr > 0) THEN
375 : IF (bs_env%ri_rs%grid_opt%max_iter < 50 .OR. &
376 : ANY(evaluation_count == [1, 3, 10, 30, 100, 300, 1000, 3000, &
377 : 10000, 30000, 100000, 300000, 1000000, &
378 : 3000000, 10000000, 30000000, 100000000, &
379 992 : 300000000, 1000000000]) .OR. &
380 : evaluation_count == bs_env%ri_rs%grid_opt%max_iter) THEN
381 : CALL write_ri_rs_grid_progress(bs_env%unit_nr, evaluation_count, &
382 : bs_env%ri_rs%grid_opt%max_iter, &
383 69 : evaluation_time, normalized_error, evaluation_successful)
384 : END IF
385 : END IF
386 138 : IF (.NOT. evaluation_successful) EXIT
387 138 : IF (.NOT. have_best .OR. normalized_error < best_normalized_error) THEN
388 120 : have_best = .TRUE.
389 120 : best_normalized_error = normalized_error
390 11916 : best_grid_coordinates(:) = grid_coordinates
391 : END IF
392 106 : ELSE IF (optimizer_task(1:5) == 'NEW_X') THEN
393 : CYCLE
394 : ELSE
395 88 : EXIT
396 : END IF
397 : END DO
398 24 : IF (.NOT. have_best) CPABORT("RI-RS grid optimization produced no regularized fit")
399 : ! grid_objective restores the best coordinates to atomic_grids and forms the explicit
400 : ! residual for the maximum error once.
401 : CALL grid_objective(best_grid_coordinates, atom_coordinate_offsets, bs_env, atom_env, &
402 : cluster_3c_int, normalized_error, coordinate_gradient, &
403 24 : evaluation_successful, max_abs_error)
404 24 : IF (.NOT. evaluation_successful) THEN
405 0 : CPABORT("RI-RS grid optimization produced no regularized fit")
406 : END IF
407 :
408 82 : DO iatom = 1, SIZE(bs_env%ri_rs%atomic_grids)
409 : bs_env%ri_rs%atomic_grids(iatom)%npts = &
410 82 : SIZE(bs_env%ri_rs%atomic_grids(iatom)%raw_points, 2)
411 : END DO
412 24 : bs_env%ri_rs%Z_lP_exists = .FALSE.
413 :
414 24 : unit_nr = bs_env%unit_nr
415 24 : IF (unit_nr > 0) THEN
416 12 : npoints = 0
417 41 : DO iatom = 1, SIZE(bs_env%ri_rs%atomic_grids)
418 41 : npoints = npoints + bs_env%ri_rs%atomic_grids(iatom)%npts
419 : END DO
420 12 : WRITE (unit_nr, '(T2,A,T72,ES9.1)') 'Normalized 3C error:', normalized_error
421 12 : WRITE (unit_nr, '(T2,A,T72,ES9.1)') 'Maximum absolute 3C error:', max_abs_error
422 12 : WRITE (unit_nr, '(T2,A,T69,I12)') 'Total grid points used for RI-RS:', npoints
423 : WRITE (unit_nr, '(T2,A,T69,F10.1,A)') &
424 12 : 'RI-RS grid optimization completed, Exec. time:', m_walltime() - start_time, ' s'
425 12 : FLUSH (unit_nr)
426 : END IF
427 :
428 24 : CALL timestop(handle)
429 :
430 48 : END SUBROUTINE optimize_grid_coordinates
431 :
432 : ! **************************************************************************************************
433 : !> \brief Distribute the cluster integrals over the RI columns of each atom's MPI group.
434 : !> \param bs_env ...
435 : !> \param atom_env Communicator shared by the ranks processing the same atom clusters.
436 : !> \param cluster_3c_int Each rank stores both AO triangles of its contiguous range of RI columns.
437 : ! **************************************************************************************************
438 24 : SUBROUTINE build_cluster_3c_int(bs_env, atom_env, cluster_3c_int)
439 : TYPE(post_scf_bandstructure_type), POINTER :: bs_env
440 : TYPE(mp_para_env_type), POINTER :: atom_env
441 : TYPE(local_cluster_3c_integrals_type), &
442 : ALLOCATABLE, INTENT(OUT) :: cluster_3c_int(:)
443 :
444 : CHARACTER(LEN=*), PARAMETER :: routineN = 'build_cluster_3c_int'
445 :
446 : INTEGER :: first_ri, handle, iatom, icenter, icluster, iimage, ij, ik, ikind, imu, inu, ip, &
447 : jatom, katom, last_ri, my_group, nao, ngroups, nlocal, nri, p_first, p_last, patom
448 48 : INTEGER, ALLOCATABLE :: ao_offset(:), cells(:, :), ri_offset(:), &
449 24 : sizes_ri(:)
450 : REAL(KIND=dp) :: available_GB, integral_bytes, &
451 : resident_bytes
452 24 : REAL(KIND=dp), ALLOCATABLE :: block(:, :, :)
453 312 : TYPE(gw_3c_ctx_type) :: integral_context
454 24 : TYPE(gw_3c_ws_type) :: workspace
455 :
456 24 : CALL timeset(routineN, handle)
457 :
458 24 : ngroups = MIN(bs_env%n_atom, bs_env%para_env%num_pe)
459 24 : my_group = MOD(bs_env%para_env%mepos, ngroups)
460 24 : ALLOCATE (atom_env)
461 24 : CALL atom_env%from_split(bs_env%para_env, my_group)
462 164 : nlocal = COUNT([(MOD(icenter - 1, ngroups) == my_group, icenter=1, bs_env%n_atom)])
463 149 : ALLOCATE (cluster_3c_int(nlocal), sizes_ri(bs_env%n_atom))
464 82 : DO iatom = 1, bs_env%n_atom
465 58 : ikind = bs_env%ri_rs%particle_set(iatom)%atomic_kind%kind_number
466 82 : sizes_ri(iatom) = bs_env%basis_set_RI(ikind)%gto_basis_set%nsgf
467 : END DO
468 : CALL gw_3c_ctx_create(integral_context, bs_env, bs_env%ri_metric, &
469 : basis_j=bs_env%basis_set_AO, basis_k=bs_env%basis_set_AO, &
470 24 : basis_i=bs_env%basis_set_RI)
471 24 : CALL gw_3c_ws_create(workspace, integral_context)
472 24 : CALL mp_mem_avail_per_rank_GB(bs_env%para_env, available_GB)
473 24 : resident_bytes = 0.0_dp
474 24 : icluster = 0
475 82 : DO icenter = 1, bs_env%n_atom
476 58 : IF (MOD(icenter - 1, ngroups) /= my_group) CYCLE
477 29 : icluster = icluster + 1
478 : CALL get_rirs_cluster_atoms(bs_env%ri_rs%particle_set, bs_env%ri_rs%cell, icenter, &
479 : bs_env%ri_rs%grid_opt%cutoff_atomic_cluster, &
480 : cluster_3c_int(icluster)%atom_indices, cells, &
481 29 : cluster_3c_int(icluster)%centers)
482 116 : ALLOCATE (ao_offset(SIZE(cells, 2)), ri_offset(SIZE(cells, 2)))
483 106 : nao = 0
484 106 : nri = 0
485 106 : DO iimage = 1, SIZE(cells, 2)
486 77 : iatom = cluster_3c_int(icluster)%atom_indices(iimage)
487 77 : ao_offset(iimage) = nao
488 77 : ri_offset(iimage) = nri
489 77 : nao = nao + bs_env%sizes_AO(iatom)
490 106 : nri = nri + sizes_ri(iatom)
491 : END DO
492 29 : first_ri = nri*atom_env%mepos/atom_env%num_pe + 1
493 29 : last_ri = nri*(atom_env%mepos + 1)/atom_env%num_pe
494 29 : integral_bytes = REAL(dp_size, dp)*REAL(nao, dp)**2*REAL(last_ri - first_ri + 1, dp)
495 29 : IF (available_GB > 0.0_dp .AND. &
496 : resident_bytes + integral_bytes > integral_memory_share*available_GB*bytes_per_GB) THEN
497 0 : CPABORT('Distributed RI-RS cluster integrals exceed available memory per rank')
498 : END IF
499 83890 : ALLOCATE (cluster_3c_int(icluster)%Int_3c(nao, nao, first_ri:last_ri), SOURCE=0.0_dp)
500 106 : DO ip = 1, SIZE(cells, 2)
501 77 : patom = cluster_3c_int(icluster)%atom_indices(ip)
502 : ! Columns of this RI image owned by the rank.
503 77 : p_first = MAX(first_ri, ri_offset(ip) + 1)
504 77 : p_last = MIN(last_ri, ri_offset(ip) + sizes_ri(patom))
505 77 : IF (p_first > p_last) CYCLE
506 317 : DO ik = 1, SIZE(cells, 2)
507 211 : katom = cluster_3c_int(icluster)%atom_indices(ik)
508 690 : DO ij = 1, ik
509 402 : jatom = cluster_3c_int(icluster)%atom_indices(ij)
510 2010 : ALLOCATE (block(bs_env%sizes_AO(jatom), bs_env%sizes_AO(katom), sizes_ri(patom)), SOURCE=0.0_dp)
511 : CALL build_3c_integral_block_ctx(block, integral_context, workspace, &
512 : atom_j=jatom, atom_k=katom, atom_i=patom, &
513 402 : cell_j=cells(:, ij), cell_k=cells(:, ik), cell_i=cells(:, ip))
514 1360 : DO inu = 1, bs_env%sizes_AO(katom)
515 : ! Within a diagonal image block keep only the upper triangle mu <= nu.
516 : cluster_3c_int(icluster)%Int_3c(ao_offset(ij) + 1: &
517 : ao_offset(ij) + MERGE(inu, bs_env%sizes_AO(jatom), ij == ik), &
518 : ao_offset(ik) + inu, p_first:p_last) = &
519 : block(1:MERGE(inu, bs_env%sizes_AO(jatom), ij == ik), inu, &
520 61149 : p_first - ri_offset(ip):p_last - ri_offset(ip))
521 : END DO
522 613 : DEALLOCATE (block)
523 : END DO
524 : END DO
525 : END DO
526 : ! ||B||² of the upper triangle with off-diagonal pairs counted twice.
527 29 : cluster_3c_int(icluster)%Int_3c_norm2 = 0.0_dp
528 1750 : DO ip = first_ri, last_ri
529 12453 : DO inu = 1, nao
530 : cluster_3c_int(icluster)%Int_3c_norm2 = cluster_3c_int(icluster)%Int_3c_norm2 + &
531 : cluster_3c_int(icluster)%Int_3c(inu, inu, ip)**2 + &
532 42733 : 2.0_dp*SUM(cluster_3c_int(icluster)%Int_3c(1:inu - 1, inu, ip)**2)
533 : END DO
534 : END DO
535 29 : CALL atom_env%sum(cluster_3c_int(icluster)%Int_3c_norm2)
536 : ! The evaluator multiplies by the full symmetric B_P (one DGEMM per RI function).
537 1750 : DO ip = first_ri, last_ri
538 12453 : DO inu = 1, nao
539 42733 : DO imu = inu + 1, nao
540 41012 : cluster_3c_int(icluster)%Int_3c(imu, inu, ip) = cluster_3c_int(icluster)%Int_3c(inu, imu, ip)
541 : END DO
542 : END DO
543 : END DO
544 29 : resident_bytes = resident_bytes + integral_bytes
545 82 : DEALLOCATE (ao_offset, ri_offset, cells)
546 : END DO
547 24 : CALL gw_3c_ws_release(workspace)
548 24 : CALL gw_3c_ctx_release(integral_context)
549 :
550 24 : CALL timestop(handle)
551 :
552 48 : END SUBROUTINE build_cluster_3c_int
553 :
554 : ! **************************************************************************************************
555 : !> \brief Evaluate ϕ_μ(r_l) on G_A and compute the normalized local error E_A and its gradient.
556 : !> \param cluster_3c_int Local atom indices and exact three-centre integrals.
557 : !> \param bs_env ...
558 : !> \param atom_env ...
559 : !> \param normalized_error Normalized squared three-centre-integral error.
560 : !> \param coordinate_gradient Derivative with respect to the cluster's physical grid coordinates.
561 : !> \param fit_successful Whether the regularized fitting equations were solved.
562 : !> \param max_abs_error Optional largest absolute three-centre-integral error.
563 : ! **************************************************************************************************
564 209 : SUBROUTINE evaluate_local_cluster(cluster_3c_int, bs_env, atom_env, normalized_error, coordinate_gradient, &
565 : fit_successful, max_abs_error)
566 : TYPE(local_cluster_3c_integrals_type), INTENT(IN) :: cluster_3c_int
567 : TYPE(post_scf_bandstructure_type), POINTER :: bs_env
568 : TYPE(mp_para_env_type), POINTER :: atom_env
569 : REAL(KIND=dp), INTENT(OUT) :: normalized_error
570 : REAL(KIND=dp), ALLOCATABLE, INTENT(OUT) :: coordinate_gradient(:, :)
571 : LOGICAL, INTENT(OUT) :: fit_successful
572 : REAL(KIND=dp), INTENT(OUT), OPTIONAL :: max_abs_error
573 :
574 : INTEGER :: AO_offset, grid_point_offset, iatom, &
575 : icartesian, icluster_atom, igrid, &
576 : ikind, n_grid_points, nAO, nAO_atom
577 209 : REAL(KIND=dp), ALLOCATABLE :: ao_gradient(:, :), &
578 209 : dPhi_alpha_l_mu(:, :, :), &
579 : grid_points(:, :), Phi_l_mu(:, :)
580 : TYPE(cell_type), POINTER :: cell
581 : TYPE(gto_basis_set_type), POINTER :: AO_basis
582 209 : TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
583 :
584 209 : cell => bs_env%ri_rs%cell
585 209 : particle_set => bs_env%ri_rs%particle_set
586 1524 : nAO = SUM(bs_env%sizes_AO(cluster_3c_int%atom_indices))
587 209 : n_grid_points = 0
588 762 : DO icluster_atom = 1, SIZE(cluster_3c_int%atom_indices)
589 553 : iatom = cluster_3c_int%atom_indices(icluster_atom)
590 762 : n_grid_points = n_grid_points + SIZE(bs_env%ri_rs%atomic_grids(iatom)%raw_points, 2)
591 : END DO
592 : ALLOCATE (grid_points(3, n_grid_points), Phi_l_mu(n_grid_points, nAO), &
593 : dPhi_alpha_l_mu(3, n_grid_points, nAO), ao_gradient(n_grid_points, nAO), &
594 2508 : coordinate_gradient(3, n_grid_points))
595 209 : Phi_l_mu = 0.0_dp
596 209 : dPhi_alpha_l_mu = 0.0_dp
597 209 : grid_point_offset = 0
598 762 : DO icluster_atom = 1, SIZE(cluster_3c_int%atom_indices)
599 553 : iatom = cluster_3c_int%atom_indices(icluster_atom)
600 : grid_points(:, grid_point_offset + 1:grid_point_offset + &
601 : SIZE(bs_env%ri_rs%atomic_grids(iatom)%raw_points, 2)) = &
602 : SPREAD(cluster_3c_int%centers(:, icluster_atom) - cluster_3c_int%centers(:, 1), 2, &
603 : SIZE(bs_env%ri_rs%atomic_grids(iatom)%raw_points, 2)) + &
604 33376 : bs_env%ri_rs%atomic_grids(iatom)%raw_points
605 762 : grid_point_offset = grid_point_offset + SIZE(bs_env%ri_rs%atomic_grids(iatom)%raw_points, 2)
606 : END DO
607 : AO_offset = 0
608 762 : DO icluster_atom = 1, SIZE(cluster_3c_int%atom_indices)
609 553 : iatom = cluster_3c_int%atom_indices(icluster_atom)
610 553 : ikind = particle_set(iatom)%atomic_kind%kind_number
611 553 : AO_basis => bs_env%basis_set_AO(ikind)%gto_basis_set
612 553 : nAO_atom = AO_basis%nsgf
613 : CALL evaluate_ao_basis_on_points(Phi_l_mu(:, AO_offset + 1:AO_offset + nAO_atom), &
614 : grid_points, AO_basis, &
615 : cluster_3c_int%centers(:, icluster_atom) - cluster_3c_int%centers(:, 1), cell, &
616 : dphi=dPhi_alpha_l_mu(:, :, AO_offset + 1:AO_offset + nAO_atom), &
617 2212 : minimum_image=.FALSE.)
618 762 : AO_offset = AO_offset + nAO_atom
619 : END DO
620 : CALL evaluate_ri_rs_fit(Phi_l_mu, cluster_3c_int%Int_3c, cluster_3c_int%Int_3c_norm2, &
621 : bs_env%ri_rs%tikhonov, atom_env, normalized_error, fit_successful, &
622 209 : ao_gradient, max_abs_error)
623 : !$OMP PARALLEL DO DEFAULT(NONE) &
624 : !$OMP SHARED(n_grid_points, coordinate_gradient, ao_gradient, dPhi_alpha_l_mu) &
625 209 : !$OMP PRIVATE(igrid, icartesian) SCHEDULE(STATIC)
626 : DO igrid = 1, n_grid_points
627 : DO icartesian = 1, 3
628 : coordinate_gradient(icartesian, igrid) = &
629 : DOT_PRODUCT(ao_gradient(igrid, :), dPhi_alpha_l_mu(icartesian, igrid, :))
630 : END DO
631 : END DO
632 : !$OMP END PARALLEL DO
633 418 : END SUBROUTINE evaluate_local_cluster
634 :
635 : ! **************************************************************************************************
636 : !> \brief Sum gradient contributions from overlapping C_A into each atom-centred grid coordinate.
637 : !> \param cluster_atoms Atom indices in the local cluster.
638 : !> \param grids Atom-centred RI-RS grids.
639 : !> \param atom_coordinate_offsets Starting coordinate offset for each atom.
640 : !> \param cluster_coordinate_gradient Gradient for the cluster's physical grid points.
641 : !> \param coordinate_gradient Global flattened atom-relative gradient to update.
642 : ! **************************************************************************************************
643 418 : SUBROUTINE accumulate_atom_gradient(cluster_atoms, grids, atom_coordinate_offsets, &
644 418 : cluster_coordinate_gradient, coordinate_gradient)
645 : INTEGER, DIMENSION(:), INTENT(IN) :: cluster_atoms
646 : TYPE(rirs_grid_type), DIMENSION(:), INTENT(IN) :: grids
647 : INTEGER, DIMENSION(:), INTENT(IN) :: atom_coordinate_offsets
648 : REAL(KIND=dp), DIMENSION(:, :), INTENT(IN) :: cluster_coordinate_gradient
649 : REAL(KIND=dp), DIMENSION(:), INTENT(INOUT) :: coordinate_gradient
650 :
651 : INTEGER :: grid_point_offset, iatom, icartesian, &
652 : icluster_atom, igrid_point
653 :
654 418 : grid_point_offset = 0
655 1524 : DO icluster_atom = 1, SIZE(cluster_atoms)
656 1106 : iatom = cluster_atoms(icluster_atom)
657 16688 : DO igrid_point = 1, SIZE(grids(iatom)%raw_points, 2)
658 63434 : DO icartesian = 1, 3
659 : coordinate_gradient(atom_coordinate_offsets(iatom) + 3*(igrid_point - 1) + icartesian) = &
660 : coordinate_gradient(atom_coordinate_offsets(iatom) + 3*(igrid_point - 1) + icartesian) + &
661 62328 : cluster_coordinate_gradient(icartesian, grid_point_offset + igrid_point)
662 : END DO
663 : END DO
664 1524 : grid_point_offset = grid_point_offset + SIZE(grids(iatom)%raw_points, 2)
665 : END DO
666 418 : END SUBROUTINE accumulate_atom_gradient
667 :
668 : ! **************************************************************************************************
669 : !> \brief Pack all atom-centred grids into the single Cartesian vector required by L-BFGS.
670 : !> \param grids Atom-centred RI-RS grids.
671 : !> \param grid_coordinates Flattened atom-relative grid coordinates.
672 : !> \param atom_coordinate_offsets Starting coordinate offset for each atom.
673 : !>
674 : !> SIZE(grid_coordinates) = 3 Σ_A N_grid,A. Coordinates are ordered Cartesian component first,
675 : !> then grid point, then atom, matching the column-major layout of raw_points(3,N_grid,A).
676 : ! **************************************************************************************************
677 24 : SUBROUTINE pack_lbfgs_grid_coordinates(grids, grid_coordinates, atom_coordinate_offsets)
678 : TYPE(rirs_grid_type), DIMENSION(:), INTENT(IN) :: grids
679 : REAL(KIND=dp), ALLOCATABLE, INTENT(OUT) :: grid_coordinates(:)
680 : INTEGER, ALLOCATABLE, INTENT(OUT) :: atom_coordinate_offsets(:)
681 :
682 : INTEGER :: atom_coordinate_count, iatom, &
683 : n_grid_coordinates
684 :
685 72 : ALLOCATE (atom_coordinate_offsets(SIZE(grids)))
686 24 : n_grid_coordinates = 0
687 82 : DO iatom = 1, SIZE(grids)
688 58 : atom_coordinate_offsets(iatom) = n_grid_coordinates
689 198 : n_grid_coordinates = n_grid_coordinates + SIZE(grids(iatom)%raw_points)
690 : END DO
691 72 : ALLOCATE (grid_coordinates(n_grid_coordinates))
692 :
693 82 : DO iatom = 1, SIZE(grids)
694 174 : atom_coordinate_count = SIZE(grids(iatom)%raw_points)
695 : grid_coordinates(atom_coordinate_offsets(iatom) + 1: &
696 : atom_coordinate_offsets(iatom) + atom_coordinate_count) = &
697 140 : RESHAPE(grids(iatom)%raw_points, [atom_coordinate_count])
698 : END DO
699 24 : END SUBROUTINE pack_lbfgs_grid_coordinates
700 :
701 : ! **************************************************************************************************
702 : !> \brief Restore the optimized Cartesian vector to the persistent atom-centred RI-RS grids.
703 : !> \param grid_coordinates Flattened atom-relative grid coordinates.
704 : !> \param atom_coordinate_offsets Starting coordinate offset for each atom.
705 : !> \param grids Atom-centred RI-RS grids to update.
706 : ! **************************************************************************************************
707 162 : SUBROUTINE unpack_lbfgs_grid_coordinates(grid_coordinates, atom_coordinate_offsets, grids)
708 : REAL(KIND=dp), DIMENSION(:), INTENT(IN) :: grid_coordinates
709 : INTEGER, DIMENSION(:), INTENT(IN) :: atom_coordinate_offsets
710 : TYPE(rirs_grid_type), DIMENSION(:), INTENT(INOUT) :: grids
711 :
712 : INTEGER :: atom_coordinate_count, iatom
713 :
714 162 : CPASSERT(SIZE(atom_coordinate_offsets) == SIZE(grids))
715 580 : DO iatom = 1, SIZE(grids)
716 1254 : atom_coordinate_count = SIZE(grids(iatom)%raw_points)
717 : grids(iatom)%raw_points(:, :) = RESHAPE( &
718 : grid_coordinates(atom_coordinate_offsets(iatom) + 1: &
719 : atom_coordinate_offsets(iatom) + &
720 : atom_coordinate_count), &
721 22944 : SHAPE(grids(iatom)%raw_points))
722 : END DO
723 162 : END SUBROUTINE unpack_lbfgs_grid_coordinates
724 :
725 : ! **************************************************************************************************
726 : !> \brief Print one compact line for a grid-objective evaluation.
727 : !> \param unit_nr ...
728 : !> \param evaluation_count ...
729 : !> \param max_iter ...
730 : !> \param evaluation_time ...
731 : !> \param normalized_error ...
732 : !> \param evaluation_successful ...
733 : ! **************************************************************************************************
734 69 : SUBROUTINE write_ri_rs_grid_progress(unit_nr, evaluation_count, max_iter, &
735 : evaluation_time, normalized_error, evaluation_successful)
736 : INTEGER, INTENT(IN) :: unit_nr, evaluation_count, max_iter
737 : REAL(KIND=dp), INTENT(IN) :: evaluation_time, normalized_error
738 : LOGICAL, INTENT(IN) :: evaluation_successful
739 :
740 : CHARACTER(LEN=10) :: count_text, error_text, max_iter_text
741 : CHARACTER(LEN=64) :: progress_line
742 : CHARACTER(LEN=8) :: evaluation_text
743 : INTEGER :: iexponent
744 :
745 69 : WRITE (max_iter_text, '(I0)') max_iter
746 69 : WRITE (count_text, '(I10)') evaluation_count
747 69 : count_text = count_text(11 - LEN_TRIM(max_iter_text):)
748 69 : WRITE (evaluation_text, '(F8.1)') evaluation_time
749 69 : IF (INDEX(evaluation_text, '*') > 0) WRITE (evaluation_text, '(ES8.1E3)') evaluation_time
750 69 : IF (evaluation_successful) THEN
751 69 : WRITE (error_text, '(ES10.1E3)') normalized_error
752 69 : error_text = ADJUSTL(error_text)
753 69 : iexponent = INDEX(error_text, 'E')
754 69 : IF (iexponent > 0) THEN
755 69 : IF (error_text(iexponent + 2:iexponent + 2) == '0') THEN
756 69 : error_text = error_text(:iexponent + 1)//error_text(iexponent + 3:)
757 : END IF
758 : END IF
759 : ELSE
760 0 : error_text = 'no fit'
761 : END IF
762 : progress_line = 'RI-RS grid eval '//TRIM(count_text)//' / '//TRIM(max_iter_text)// &
763 69 : ', 3C error: '//TRIM(ADJUSTL(error_text))
764 69 : IF (LEN_TRIM(progress_line) > 56) progress_line = 'RI-RS eval '//progress_line(16:)
765 69 : WRITE (unit_nr, '(T2,A,T59,A,A,A)') TRIM(progress_line), 'Exec. time: ', evaluation_text, ' s'
766 69 : CALL m_flush(unit_nr)
767 69 : END SUBROUTINE write_ri_rs_grid_progress
768 :
769 : END MODULE ri_rs_grid_optimization
|