(git:27c966b)
Loading...
Searching...
No Matches
gw_auto_ri_generate_RI_basis.F
Go to the documentation of this file.
1!--------------------------------------------------------------------------------------------------!
2! CP2K: A general program to perform molecular dynamics simulations !
3! Copyright 2000-2026 CP2K developers group <https://cp2k.org> !
4! !
5! SPDX-License-Identifier: GPL-2.0-or-later !
6!--------------------------------------------------------------------------------------------------!
7
8! **************************************************************************************************
9!> \brief Automatic RI basis set optimization for molecular and periodic GW.
10!> \par History
11!> 09.2026 created [Jan Wilhelm]
12! **************************************************************************************************
22 USE coulomb_integral_interface, ONLY: compute_coulomb_2c,&
23 compute_coulomb_3c,&
24 coulomb_integral_cleanup,&
25 coulomb_integral_context_type,&
26 coulomb_integral_init,&
27 coulomb_operator_type
30 USE cp_dbcsr_api, ONLY: &
33 dbcsr_type_symmetric
38 USE cp_fm_diag, ONLY: cp_fm_geeig
42 USE cp_fm_types, ONLY: cp_fm_create,&
47 USE ieee_arithmetic, ONLY: ieee_is_finite
56 USE kinds, ONLY: dp
59 USE machine, ONLY: m_walltime
60 USE mathconstants, ONLY: z_zero
61 USE mathlib, ONLY: diamat_all
64 ncoset
70 USE util, ONLY: sort
71#include "base/base_uses.f90"
72
73 IMPLICIT NONE
74 PRIVATE
75
76 CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'gw_auto_ri_generate_RI_basis'
77
78 ! Numerical cutoffs for the reference M_PQ eigenspace and final M_pq pseudoinverse.
79 REAL(KIND=dp), PARAMETER :: reference_m_eigenval_threshold = 1.0e-10_dp
80 REAL(KIND=dp), PARAMETER :: optimized_m_eigenval_threshold = 1.0e-6_dp
81 ! Discard null target directions and reject non-positive-semidefinite M_PQ or B_PQ spectra.
82 REAL(KIND=dp), PARAMETER :: relative_target_eigenval_threshold = 1.0e-12_dp
83 REAL(KIND=dp), PARAMETER :: negative_eigenval_tolerance = 1.0e-10_dp
84 REAL(KIND=dp), PARAMETER :: conditioned_negative_eigenval_tolerance = sqrt(epsilon(1.0_dp))
85 ! Largest accepted deviation of the periodic AA metric from the identity after refinement.
86 REAL(KIND=dp), PARAMETER :: eps_metric_defect = 1.0e-7_dp
87
89
90CONTAINS
91
92!**************************************************************************************************!
93! The full derivation of the AUTO_RI algorithm will be published separately. !
94! !
95! The equations below define the notation used throughout this automatic RI basis optimizer. !
96! !
97! The RI scalar product uses the truncated Coulomb operator: !
98! !
99! (f|g) = ∫dr∫dr' f(r) V_rc(r-r') g(r'). (1) !
100! !
101! For the RI basis-set optimization, we consider orbital-product densities formed by !
102! AOs on the same atom A: !
103! !
104! ρ_mn^AA(r) = Σ_{μ∈A} Σ_{ν∈A} C_μm C_νn ϕ_μ(r) ϕ_ν(r). (2) !
105! !
106! The atom-local correlation target B_PQ^{A,C} mimics contributions to the density response χ, !
107! and the exchange target B_PQ^{A,X} mimics contributions to the exchange self-energy: !
108! !
109! B_PQ^{A,C} = (1/N_C^A) Σ_ia^oe w_ia (φ_P^A|ρ_ia^AA)(ρ_ia^AA|φ_Q^A). (3) !
110! !
111! B_PQ^{A,X} = (1/N_X^A) Σ_{n∈W_occ} Σ_i^occ (φ_P^A|ρ_ni^AA)(ρ_ni^AA|φ_Q^A). (4) !
112! !
113! W_occ = [ε_HOMO-E_win, ε_LUMO+E_win], where E_win is OCC_EMPTY_FRONTIER_ORBITAL_WINDOW. !
114! !
115! Here w_ia gives larger weight to frontier-orbital products close to the HOMO and LUMO: !
116! !
117! w_ia = 1/(|ε_i-E_F| |ε_a-E_F|), E_F = (ε_HOMO+ε_LUMO)/2. (5) !
118! !
119! Diagonalizing B^A = B^{A,C}+B^{A,X} identifies the most important atom-local RI functions !
120! that we use in the optimized RI basis set. The contraction coefficients U_Pp^A [see Eq. (7)] !
121! used for optimized RI basis functions are obtained from the eigenvalue problem !
122! !
123! B^A U^A = M^A U^A λ^A, Σ_Q B_PQ^A U_Qp^A = λ_p^A Σ_Q M_PQ^A U_Qp^A. (6) !
124! !
125! The eigenvectors with the largest eigenvalues are the contracted RI functions φ_p^A(r) !
126! used for the optimized RI basis set: !
127! !
128! φ_p^A(r) = Σ_P U_Pp^A φ_P^A(r). (7) !
129! !
130! Molecular and periodic GW retain only the atom-local AA contractions in Eq. (7). !
131! The requested optimized size is distributed among atoms using their target eigenvalues. !
132! !
133! The retained size can be smaller only when B^A or M^A has fewer numerically non-null modes. !
134! !
135!**************************************************************************************************!
136
137! **************************************************************************************************
138!> \brief Selects molecular or periodic atom-local AA RI-basis optimization.
139!> \param qs_env Quickstep environment needed by the molecular RI metric.
140!> \param bs_env Band-structure environment containing GW parameters.
141! **************************************************************************************************
142 SUBROUTINE generate_auto_ri_basis(qs_env, bs_env)
143 TYPE(qs_environment_type), POINTER :: qs_env
144 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
145
146 IF (bs_env%do_periodic) THEN
147 CALL generate_auto_ri_basis_periodic(bs_env)
148 ELSE
149 CALL generate_auto_ri_basis_molecular(qs_env, bs_env)
150 END IF
151
152 END SUBROUTINE generate_auto_ri_basis
153
154! **************************************************************************************************
155!> \brief Optimizes the molecular AA basis and computes its M⁻¹ and V using Eqs. (1)-(7).
156!> \param qs_env ...
157!> \param bs_env Band-structure environment containing GW parameters.
158! **************************************************************************************************
159 SUBROUTINE generate_auto_ri_basis_molecular(qs_env, bs_env)
160 TYPE(qs_environment_type), POINTER :: qs_env
161 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
162
163 CHARACTER(LEN=*), PARAMETER :: routinen = 'generate_auto_ri_basis_molecular'
164
165 INTEGER :: handle
166 REAL(kind=dp) :: time_start
167 REAL(kind=dp), ALLOCATABLE :: u_pp_a(:, :, :)
168
169 CALL timeset(routinen, handle)
170
171 cpassert(.NOT. bs_env%do_periodic)
172 time_start = m_walltime()
173
174 ! Compute AA target matrices B^{AA,C}, B^{AA,X} and contraction U_Pp^A in Eqs. (3)-(7).
175 CALL compute_aa_optimized_ri_basis(bs_env, u_pp_a)
176
177 CALL store_aa_basis(bs_env, u_pp_a)
178 DEALLOCATE (u_pp_a)
179
180 ! Form M⁻¹ and V in the optimized molecular RI basis.
181 CALL compute_m_pq_inv_and_v_pq_molecular(qs_env, bs_env)
182
183 ! Print the optimized RI basis dimension and total optimization time.
184 CALL print_auto_ri_summary(bs_env, time_start)
185
186 CALL timestop(handle)
187
188 END SUBROUTINE generate_auto_ri_basis_molecular
189
190! **************************************************************************************************
191!> \brief Optimizes atom-local AA contractions for periodic GW using Eqs. (1)-(7).
192!> \param bs_env Band-structure environment containing GW parameters.
193! **************************************************************************************************
194 SUBROUTINE generate_auto_ri_basis_periodic(bs_env)
195 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
196
197 CHARACTER(LEN=*), PARAMETER :: routinen = 'generate_auto_ri_basis_periodic'
198
199 INTEGER :: handle
200 REAL(kind=dp) :: time_start
201 REAL(kind=dp), ALLOCATABLE :: u_pp_a(:, :, :)
202
203 CALL timeset(routinen, handle)
204
205 cpassert(bs_env%do_periodic)
206 time_start = m_walltime()
207
208 ! Compute AA target matrices B^{AA,C}, B^{AA,X} and contraction U_Pp^A in Eqs. (3)-(7).
209 CALL compute_aa_optimized_ri_basis(bs_env, u_pp_a)
210
211 CALL store_aa_basis(bs_env, u_pp_a)
212 DEALLOCATE (u_pp_a)
213
214 ! Activate AA basis sizes; q-dependent M⁻¹ and V are built in the periodic GW driver.
215 CALL set_optimized_ri_basis_sizes(bs_env)
216
217 ! Print the optimized RI basis dimension and total optimization time.
218 CALL print_auto_ri_summary(bs_env, time_start)
219
220 CALL timestop(handle)
221
222 END SUBROUTINE generate_auto_ri_basis_periodic
223
224! **************************************************************************************************
225!> \brief Computes the atom-local contraction coefficients U^A from Eqs. (3)-(7):
226!>
227!> B_PQ^{A,C} = (1/N_C^A) Σ_ia^oe w_ia (φ_P^A|ρ_ia^AA)(ρ_ia^AA|φ_Q^A), (3)
228!>
229!> B_PQ^{A,X} = (1/N_X^A) Σ_{n∈W_occ} Σ_i^occ (φ_P^A|ρ_ni^AA)(ρ_ni^AA|φ_Q^A), (4)
230!>
231!> w_ia = 1/(|ε_i-E_F| |ε_a-E_F|), E_F = (ε_HOMO+ε_LUMO)/2, (5)
232!>
233!> B^A U^A = M^A U^A λ^A, (6)
234!>
235!> Contracted RI basis functions for optimized RI basis:
236!>
237!> φ_p^A(r) = Σ_P U_Pp^A φ_P^A(r). (7)
238!>
239!> \param bs_env ...
240!> \param U_Pp_A temporary atom-local contraction coefficients
241! **************************************************************************************************
242 SUBROUTINE compute_aa_optimized_ri_basis(bs_env, U_Pp_A)
243 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
244 REAL(kind=dp), ALLOCATABLE, INTENT(OUT) :: u_pp_a(:, :, :)
245
246 CHARACTER(LEN=*), PARAMETER :: routinen = 'compute_AA_optimized_RI_basis'
247
248 INTEGER :: handle, iatom, ikind, max_nao_kind, max_nri_kind, nao_kind, natom, nkind, &
249 nri_kind, ri_size_opt_aa_requested, root_rank
250 INTEGER, ALLOCATABLE, DIMENSION(:) :: kind_of_atom, sizes_opt_ri_aa
251 REAL(kind=dp) :: target_norm
252 REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: eigenval
253 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :) :: b_pq, eigenval_aa, m_pq, u_pp_a_atom, &
254 x_pi
255 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :, :) :: m_pq_by_kind
256 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :, :, :) :: int_3c
257
258 CALL timeset(routinen, handle)
259
260 ! Determine reference dimensions and the number of AA RI functions (i.e. number of
261 ! eigenvectors U_Pp^A used for RI-basis contractions).
262 CALL initialize_aa_optimization(bs_env, kind_of_atom, natom, nkind, &
263 max_nri_kind, ri_size_opt_aa_requested)
264 ALLOCATE (eigenval_aa(max_nri_kind, natom), source=0.0_dp)
265 ALLOCATE (sizes_opt_ri_aa(natom))
266
267 CALL initialize_auto_ri_integrals(bs_env)
268 max_nao_kind = maxval(bs_env%sizes_AO)
269 ALLOCATE (u_pp_a(max_nri_kind, max_nri_kind, natom), &
270 m_pq_by_kind(max_nri_kind, max_nri_kind, nkind), source=0.0_dp)
271 ALLOCATE (int_3c(max_nao_kind, max_nao_kind, max_nri_kind, nkind), source=0.0_dp)
272
273 ! The few atom-kind integral blocks are inexpensive; compute them on every rank.
274 DO ikind = 1, nkind
275 nao_kind = bs_env%basis_set_AO(ikind)%gto_basis_set%nsgf
276 nri_kind = bs_env%basis_set_RI(ikind)%gto_basis_set%nsgf
277 CALL compute_int_3c( &
278 ao_basis=bs_env%basis_set_AO(ikind)%gto_basis_set, &
279 ri_basis=bs_env%basis_set_RI(ikind)%gto_basis_set, &
280 potential=bs_env%ri_metric, m_pq=m_pq, &
281 int_3c=int_3c(1:nao_kind, 1:nao_kind, 1:nri_kind, ikind))
282 ! Full-k RI-RS applies REGULARIZATION_RI as an eigenvalue cutoff during inversion.
283 IF (bs_env%gw_implementation /= ri_rs_small_cell_full_kp) THEN
284 CALL add_ri_metric_regularization(bs_env%basis_set_RI(ikind)%gto_basis_set, &
285 bs_env%regularization_RI, m_pq)
286 END IF
287 m_pq_by_kind(1:nri_kind, 1:nri_kind, ikind) = m_pq
288 DEALLOCATE (m_pq)
289 END DO
290
291 bs_env%auto_ri%sizes_opt_RI = 0
292 DO iatom = 1, natom
293 root_rank = modulo(iatom - 1, bs_env%para_env%num_pe)
294 ikind = kind_of_atom(iatom)
295 nri_kind = bs_env%basis_set_RI(ikind)%gto_basis_set%nsgf
296 nao_kind = bs_env%sizes_AO(iatom)
297 ALLOCATE (m_pq(nri_kind, nri_kind))
298 m_pq(:, :) = m_pq_by_kind(1:nri_kind, 1:nri_kind, ikind)
299 ! Eqs. (3)-(5): form B^{A,C}, B^{A,X}, and their weighted sum B^A.
300 IF (bs_env%do_periodic) THEN
301 CALL compute_b_ij_aa_kaveraged(bs_env, iatom, root_rank, &
302 int_3c(1:nao_kind, 1:nao_kind, 1:nri_kind, ikind), &
303 m_pq, b_pq, target_norm, x_pi)
304 ELSE
305 CALL compute_b_pq_aa(bs_env, iatom, root_rank, &
306 int_3c(1:nao_kind, 1:nao_kind, 1:nri_kind, ikind), &
307 m_pq, b_pq, target_norm)
308 END IF
309 DEALLOCATE (m_pq)
310 IF (root_rank /= bs_env%para_env%mepos) THEN
311 DEALLOCATE (b_pq)
312 IF (ALLOCATED(x_pi)) DEALLOCATE (x_pi)
313 cycle
314 END IF
315 ! Eq. (6): solve B^A U^A = M^A U^A λ^A.
316 IF (bs_env%do_periodic) THEN
317 CALL solve_aa_eigenproblem_periodic(m_pq_by_kind(1:nri_kind, 1:nri_kind, ikind), &
318 x_pi, b_pq, target_norm, u_pp_a_atom, eigenval)
319 DEALLOCATE (x_pi)
320 ELSE
321 CALL solve_aa_eigenproblem(m_pq_by_kind(1:nri_kind, 1:nri_kind, ikind), &
322 b_pq, target_norm, u_pp_a_atom, eigenval)
323 END IF
324 eigenval_aa(1:SIZE(u_pp_a_atom, 2), iatom) = eigenval(1:SIZE(u_pp_a_atom, 2))
325 bs_env%auto_ri%sizes_opt_RI(iatom) = SIZE(u_pp_a_atom, 2)
326 u_pp_a(1:SIZE(u_pp_a_atom, 1), 1:SIZE(u_pp_a_atom, 2), iatom) = u_pp_a_atom
327 DEALLOCATE (u_pp_a_atom, eigenval, b_pq)
328 END DO
329 CALL bs_env%para_env%sum(bs_env%auto_ri%sizes_opt_RI)
330 CALL bs_env%para_env%sum(u_pp_a)
331 CALL bs_env%para_env%sum(eigenval_aa)
332 IF (bs_env%do_periodic) THEN
333 CALL select_ri_sizes_periodic(eigenval_aa, ri_size_opt_aa_requested, 1, sizes_opt_ri_aa)
334 ELSE
335 CALL select_ri_sizes(eigenval_aa, ri_size_opt_aa_requested, 1, sizes_opt_ri_aa)
336 END IF
337 bs_env%auto_ri%sizes_opt_RI(:) = sizes_opt_ri_aa
338 DO iatom = 1, natom
339 u_pp_a(:, sizes_opt_ri_aa(iatom) + 1:, iatom) = 0.0_dp
340 END DO
341 DEALLOCATE (int_3c, m_pq_by_kind)
342
343 CALL timestop(handle)
344
345 END SUBROUTINE compute_aa_optimized_ri_basis
346
347! **************************************************************************************************
348!> \brief Initializes dimensions for the atom-local AA optimization.
349!>
350!> Distributes the full requested RI dimension among atom-local contractions.
351!> \param bs_env ...
352!> \param kind_of_atom ...
353!> \param natom ...
354!> \param nkind ...
355!> \param max_nRI_kind ...
356!> \param RI_size_opt_AA_requested ...
357! **************************************************************************************************
358 SUBROUTINE initialize_aa_optimization(bs_env, kind_of_atom, &
359 natom, nkind, max_nRI_kind, &
360 RI_size_opt_AA_requested)
361 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
362 INTEGER, ALLOCATABLE, INTENT(OUT) :: kind_of_atom(:)
363 INTEGER, INTENT(OUT) :: natom, nkind, max_nri_kind, &
364 ri_size_opt_aa_requested
365
366 CHARACTER(LEN=*), PARAMETER :: routinen = 'initialize_AA_optimization'
367
368 INTEGER :: handle, ikind, nao_total, nri_reference, &
369 ri_size_requested
370 INTEGER, ALLOCATABLE :: sizes_ref_ri_by_kind(:)
371
372 CALL timeset(routinen, handle)
373
374 natom = bs_env%n_atom
375 cpassert(ASSOCIATED(bs_env%ri_rs%atomic_kind_set))
376 cpassert(ASSOCIATED(bs_env%ri_rs%particle_set))
377 cpassert(ASSOCIATED(bs_env%para_env) .AND. natom > 0)
378 cpassert(ALLOCATED(bs_env%basis_set_AO))
379 cpassert(ALLOCATED(bs_env%basis_set_RI))
380 nkind = SIZE(bs_env%basis_set_RI)
381 cpassert(nkind > 0)
382 cpassert(SIZE(bs_env%basis_set_AO) == nkind)
383 cpassert(bs_env%do_gw_ri_rs)
384 IF (bs_env%do_periodic .AND. &
385 bs_env%gw_implementation /= ri_rs_small_cell_full_kp) THEN
386 cpabort("Periodic AUTO_RI requires RI_RS_SMALL_CELL_FULL_KP")
387 END IF
388
389 cpassert(bs_env%gw_implementation /= tensor_small_cell_full_kp)
390
391 CALL get_atomic_kind_set(atomic_kind_set=bs_env%ri_rs%atomic_kind_set, &
392 kind_of=kind_of_atom)
393 ALLOCATE (bs_env%auto_ri%sizes_opt_RI(natom))
394 ALLOCATE (sizes_ref_ri_by_kind(nkind))
395 DO ikind = 1, nkind
396 cpassert(ASSOCIATED(bs_env%basis_set_AO(ikind)%gto_basis_set))
397 cpassert(ASSOCIATED(bs_env%basis_set_RI(ikind)%gto_basis_set))
398 sizes_ref_ri_by_kind(ikind) = bs_env%basis_set_RI(ikind)%gto_basis_set%nsgf
399 END DO
400 max_nri_kind = maxval(sizes_ref_ri_by_kind)
401 ALLOCATE (bs_env%auto_ri%sizes_ref_RI(natom))
402 bs_env%auto_ri%sizes_ref_RI(:) = sizes_ref_ri_by_kind(kind_of_atom)
403 nao_total = sum(bs_env%sizes_AO)
404 nri_reference = sum(bs_env%auto_ri%sizes_ref_RI)
405 cpassert(nao_total > 0 .AND. nri_reference > 0)
406 ri_size_requested = nint(bs_env%auto_ri%ri_ao_ratio*real(nao_total, dp))
407 IF (bs_env%do_periodic) THEN
408 IF (ri_size_requested < natom) THEN
409 cpabort("AUTO_RI RI_AO_RATIO requests fewer RI functions than atoms")
410 END IF
411 ri_size_opt_aa_requested = min(ri_size_requested, nri_reference)
412 ELSE
413 ri_size_opt_aa_requested = max(natom, min(ri_size_requested, nri_reference))
414 END IF
415 DEALLOCATE (sizes_ref_ri_by_kind)
416
417 CALL timestop(handle)
418
419 END SUBROUTINE initialize_aa_optimization
420
421!> \brief Sets the RI-RS block sizes to the optimized RI basis dimensions.
422!> \param bs_env ...
423! **************************************************************************************************
424 SUBROUTINE set_optimized_ri_basis_sizes(bs_env)
425 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
426
427 CHARACTER(LEN=*), PARAMETER :: routinen = 'set_optimized_RI_basis_sizes'
428
429 INTEGER :: handle
430
431 CALL timeset(routinen, handle)
432
433 bs_env%sizes_RI(:) = bs_env%auto_ri%sizes_opt_RI
434 bs_env%auto_ri%ready = .true.
435
436 CALL timestop(handle)
437
438 END SUBROUTINE set_optimized_ri_basis_sizes
439
440! **************************************************************************************************
441!> \brief Prints the optimized RI basis size and total optimization time.
442!> \param bs_env ...
443!> \param time_start ...
444! **************************************************************************************************
445 SUBROUTINE print_auto_ri_summary(bs_env, time_start)
446 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
447 REAL(kind=dp), INTENT(IN) :: time_start
448
449 CHARACTER(LEN=*), PARAMETER :: routinen = 'print_auto_ri_summary'
450
451 INTEGER :: handle, ri_size_optimized, unit_nr
452 REAL(kind=dp) :: elapsed
453
454 CALL timeset(routinen, handle)
455
456 ri_size_optimized = sum(bs_env%auto_ri%sizes_opt_RI)
457 elapsed = m_walltime() - time_start
458 CALL bs_env%para_env%max(elapsed)
459 unit_nr = bs_env%unit_nr
460 IF (unit_nr > 0) THEN
461 WRITE (unit_nr, '(A)') ' '
462 WRITE (unit_nr, '(T2,A,T74,I9)') &
463 'AUTO_RI| Number of automatic RI functions for χ, ε, W:', ri_size_optimized
464 WRITE (unit_nr, '(T2,A,T71,F8.1,A)') &
465 'AUTO_RI| Total RI basis optimization time:', elapsed, ' s'
466 END IF
467
468 CALL timestop(handle)
469
470 END SUBROUTINE print_auto_ri_summary
471
472! **************************************************************************************************
473!> \brief Adds REGULARIZATION_RI to a two-center RI metric matrix (P|Q).
474!> \param basis ...
475!> \param regularization ...
476!> \param M_PQ reference RI metric M_PQ
477!> \param offset ...
478! **************************************************************************************************
479 SUBROUTINE add_ri_metric_regularization(basis, regularization, M_PQ, offset)
481 TYPE(gto_basis_set_type), INTENT(IN) :: basis
482 REAL(kind=dp), INTENT(IN) :: regularization
483 REAL(kind=dp), DIMENSION(:, :), INTENT(INOUT) :: m_pq
484 INTEGER, INTENT(IN), OPTIONAL :: offset
485
486 CHARACTER(LEN=*), PARAMETER :: routinen = 'add_RI_metric_regularization'
487
488 INTEGER :: first, handle, i, iset, nset, start
489 INTEGER, DIMENSION(:), POINTER :: npgf, nsgf_set
490 INTEGER, DIMENSION(:, :), POINTER :: first_sgf
491 REAL(kind=dp) :: shift
492 REAL(kind=dp), DIMENSION(:, :), POINTER :: zet
493
494 CALL timeset(routinen, handle)
495
496 start = 0
497 IF (PRESENT(offset)) start = offset
498 CALL get_gto_basis_set(basis, first_sgf=first_sgf, npgf=npgf, nset=nset, &
499 nsgf_set=nsgf_set, zet=zet)
500 cpassert(start >= 0 .AND. start + basis%nsgf <= SIZE(m_pq, 1))
501 cpassert(start + basis%nsgf <= SIZE(m_pq, 2))
502 DO iset = 1, nset
503 shift = regularization*max(1.0_dp, 1.0_dp/minval(zet(1:npgf(iset), iset)))
504 first = start + first_sgf(1, iset)
505 DO i = first, first + nsgf_set(iset) - 1
506 m_pq(i, i) = m_pq(i, i) + shift
507 END DO
508 END DO
509
510 CALL timestop(handle)
511
512 END SUBROUTINE add_ri_metric_regularization
513
514! **************************************************************************************************
515!> \brief Copies atom-local diagonal blocks M^AA from a distributed metric M.
516!> \param M_pq_local locally stored entries of the optimized RI metric M_pq
517!> \param M_pq_AA_local locally stored entries of the atom-block-diagonal metric
518!> \param row_indices global indices of the locally owned rows
519!> \param col_indices global indices of the locally owned columns
520!> \param sizes_RI optimized RI functions in each atomic matrix block
521! **************************************************************************************************
522 SUBROUTINE copy_m_pq_aa_blocks(M_pq_local, M_pq_AA_local, row_indices, &
523 col_indices, sizes_RI)
524 REAL(kind=dp), DIMENSION(:, :), INTENT(IN) :: m_pq_local
525 REAL(kind=dp), DIMENSION(:, :), INTENT(OUT) :: m_pq_aa_local
526 INTEGER, DIMENSION(:), INTENT(IN) :: row_indices, col_indices, sizes_ri
527
528 CHARACTER(LEN=*), PARAMETER :: routinen = 'copy_M_pq_AA_blocks'
529
530 INTEGER :: handle, i, j, last_index
531 INTEGER, ALLOCATABLE, DIMENSION(:) :: block_ends, col_atom, row_atom
532
533 CALL timeset(routinen, handle)
534
535 cpassert(SIZE(m_pq_local, 1) == SIZE(row_indices))
536 cpassert(SIZE(m_pq_local, 2) == SIZE(col_indices))
537 cpassert(all(shape(m_pq_aa_local) == shape(m_pq_local)))
538 cpassert(SIZE(sizes_ri) > 0 .AND. all(sizes_ri > 0))
539
540 ALLOCATE (block_ends(SIZE(sizes_ri)))
541 block_ends(1) = sizes_ri(1)
542 DO i = 2, SIZE(sizes_ri)
543 block_ends(i) = block_ends(i - 1) + sizes_ri(i)
544 END DO
545 last_index = block_ends(SIZE(block_ends))
546 cpassert(all(row_indices >= 1 .AND. row_indices <= last_index))
547 cpassert(all(col_indices >= 1 .AND. col_indices <= last_index))
548
549 ALLOCATE (row_atom(SIZE(row_indices)), col_atom(SIZE(col_indices)))
550 DO i = 1, SIZE(row_indices)
551 row_atom(i) = m_pq_index_atom(row_indices(i), block_ends)
552 END DO
553 DO j = 1, SIZE(col_indices)
554 col_atom(j) = m_pq_index_atom(col_indices(j), block_ends)
555 END DO
556
557 m_pq_aa_local = 0.0_dp
558 DO j = 1, SIZE(col_indices)
559 DO i = 1, SIZE(row_indices)
560 IF (row_atom(i) == col_atom(j)) THEN
561 m_pq_aa_local(i, j) = m_pq_local(i, j)
562 END IF
563 END DO
564 END DO
565
566 CALL timestop(handle)
567
568 END SUBROUTINE copy_m_pq_aa_blocks
569
570! **************************************************************************************************
571!> \brief Forms f_i=1/sqrt(λ_i) for eigenvalues of the RI metric matrix M_PQ.
572!> \param eigenval generalized metric eigenvalues
573!> \param factors inverse-square-root weights, or zero for discarded modes
574! **************************************************************************************************
575 SUBROUTINE compute_optimized_m_pq_inv_factors(eigenval, factors)
576 REAL(kind=dp), DIMENSION(:), INTENT(IN) :: eigenval
577 REAL(kind=dp), ALLOCATABLE, DIMENSION(:), &
578 INTENT(OUT) :: factors
579
580 CHARACTER(LEN=*), PARAMETER :: routinen = 'compute_optimized_M_pq_inv_factors'
581
582 INTEGER :: handle, i, n_dependent
583 REAL(kind=dp) :: largest, threshold
584
585 CALL timeset(routinen, handle)
586
587 cpassert(SIZE(eigenval) > 0)
588
589 largest = maxval(eigenval)
590 cpassert(largest > 0.0_dp)
591 threshold = optimized_m_eigenval_threshold*largest
592 ALLOCATE (factors(SIZE(eigenval)))
593 factors = 0.0_dp
594 n_dependent = 0
595 DO i = 1, SIZE(eigenval)
596 IF (eigenval(i) > 0.0_dp .AND. eigenval(i) >= threshold) THEN
597 factors(i) = 1.0_dp/sqrt(eigenval(i))
598 ELSE
599 n_dependent = n_dependent + 1
600 END IF
601 END DO
602 cpassert(n_dependent < SIZE(eigenval))
603
604 CALL timestop(handle)
605
606 END SUBROUTINE compute_optimized_m_pq_inv_factors
607
608! **************************************************************************************************
609!> \brief Finds the atom owning a global RI metric index.
610!> \param global_index global metric index
611!> \param block_ends inclusive end index of each consecutive atom block
612!> \return atom index
613! **************************************************************************************************
614 PURE FUNCTION m_pq_index_atom(global_index, block_ends) RESULT(atom_index)
615 INTEGER, INTENT(IN) :: global_index
616 INTEGER, DIMENSION(:), INTENT(IN) :: block_ends
617 INTEGER :: atom_index
618
619 INTEGER :: high, low, middle
620
621 low = 1
622 high = SIZE(block_ends)
623 DO WHILE (low < high)
624 middle = (low + high)/2
625 IF (global_index <= block_ends(middle)) THEN
626 high = middle
627 ELSE
628 low = middle + 1
629 END IF
630 END DO
631 atom_index = low
632
633 END FUNCTION m_pq_index_atom
634
635! **************************************************************************************************
636!> \brief Computes X=U diag(1/sqrt(λ)) with X^T M X=I.
637!>
638!> The columns of X span the non-null eigenspace of the symmetric
639!> two-center RI metric matrix M=U diag(λ) U^T.
640!> \param M_PQ ...
641!> \param X_Pi ...
642!> \param rank ...
643! **************************************************************************************************
644 SUBROUTINE compute_reference_m_pq_inv_factor(M_PQ, X_Pi, rank)
645 REAL(kind=dp), DIMENSION(:, :), INTENT(IN) :: m_pq
646 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :), &
647 INTENT(OUT) :: x_pi
648 INTEGER, INTENT(OUT) :: rank
649
650 CHARACTER(LEN=*), PARAMETER :: routinen = 'compute_reference_M_PQ_inv_factor'
651
652 INTEGER :: column, handle, i, n
653 REAL(kind=dp) :: largest
654 REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: eigenval
655 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :) :: eigenvec
656
657 CALL timeset(routinen, handle)
658
659 n = SIZE(m_pq, 1)
660 cpassert(n > 0 .AND. SIZE(m_pq, 2) == n)
661 ALLOCATE (eigenvec, source=0.5_dp*(m_pq + transpose(m_pq)))
662 ALLOCATE (eigenval(n))
663 CALL diamat_all(eigenvec, eigenval, dac=.true.)
664 largest = maxval(eigenval)
665 IF (largest <= 0.0_dp) cpabort("AUTO_RI metric has no positive non-null range")
666 IF (minval(eigenval) < -negative_eigenval_tolerance*largest) THEN
667 cpabort("AUTO_RI metric has no positive non-null range")
668 END IF
669 rank = count(eigenval > reference_m_eigenval_threshold*largest)
670 IF (rank == 0) cpabort("AUTO_RI metric has no positive non-null range")
671 ALLOCATE (x_pi(n, rank))
672 column = 0
673 DO i = 1, n
674 IF (eigenval(i) <= reference_m_eigenval_threshold*largest) cycle
675 column = column + 1
676 x_pi(:, column) = eigenvec(:, i)/sqrt(eigenval(i))
677 END DO
678
679 CALL timestop(handle)
680
681 END SUBROUTINE compute_reference_m_pq_inv_factor
682
683! **************************************************************************************************
684!> \brief Solves eigenvalue problem B^A U^A=M^A U^A λ^A in Eq. (6).
685!>
686!> Columns are ordered from the largest to the smallest λ^A.
687!> Columns are retained when λ_i/λ_max >= eigenval_threshold.
688!> \param M_PQ metric of the large reference RI basis
689!> \param B_PQ normalized atom-local target matrix B^A
690!> \param target_normalization ...
691!> \param U_Pp_A selected AA contractions from the large RI basis P to optimized functions p
692!> \param eigenval normalized target eigenvalues in descending order
693! **************************************************************************************************
694 SUBROUTINE solve_aa_eigenproblem(M_PQ, B_PQ, target_normalization, U_Pp_A, eigenval)
695 REAL(kind=dp), DIMENSION(:, :), INTENT(IN) :: m_pq, b_pq
696 REAL(kind=dp), INTENT(IN) :: target_normalization
697 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :), &
698 INTENT(OUT) :: u_pp_a
699 REAL(kind=dp), ALLOCATABLE, DIMENSION(:), &
700 INTENT(OUT) :: eigenval
701
702 CHARACTER(LEN=*), PARAMETER :: routinen = 'solve_AA_eigenproblem'
703
704 INTEGER :: handle, i, j, m_pq_rank, n, nret
705 REAL(kind=dp) :: largest_score, relative_fitting_defect
706 REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: eigenval_b, eigenval_m
707 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :) :: b_ij, m_pp_check, u_ij, u_pp_a_rotated, &
708 x_pi
709
710 CALL timeset(routinen, handle)
711
712 n = SIZE(m_pq, 1)
713 cpassert(n > 0)
714 cpassert(SIZE(m_pq, 2) == n)
715 cpassert(all(shape(b_pq) == [n, n]))
716 cpassert(target_normalization > 0.0_dp)
717 CALL compute_reference_m_pq_inv_factor(m_pq, x_pi, m_pq_rank)
718
719 ALLOCATE (b_ij(m_pq_rank, m_pq_rank))
720 b_ij(:, :) = matmul(transpose(x_pi), &
721 matmul(0.5_dp*(b_pq + transpose(b_pq)), x_pi))/target_normalization
722 ALLOCATE (u_ij, source=b_ij)
723 ALLOCATE (eigenval_b(m_pq_rank))
724 CALL diamat_all(u_ij, eigenval_b, dac=.true.)
725 largest_score = maxval(eigenval_b)
726 IF (largest_score <= 0.0_dp) THEN
727 cpabort("AUTO_RI conditioned atom-local eigensolver failed")
728 END IF
729 IF (minval(eigenval_b) < -conditioned_negative_eigenval_tolerance*largest_score) THEN
730 cpabort("AUTO_RI conditioned atom-local eigensolver failed")
731 END IF
732 eigenval_b(:) = max(eigenval_b, 0.0_dp)
733 nret = count(eigenval_b/largest_score >= relative_target_eigenval_threshold)
734 ALLOCATE (u_pp_a(n, nret), eigenval(m_pq_rank))
735 DO i = 1, m_pq_rank
736 eigenval(i) = eigenval_b(m_pq_rank - i + 1)
737 END DO
738 DO i = 1, nret
739 j = m_pq_rank - i + 1
740 u_pp_a(:, i) = matmul(x_pi, u_ij(:, j))
741 END DO
742
743 relative_fitting_defect = 1.0_dp
744 DO i = 1, nret
745 j = m_pq_rank - i + 1
746 relative_fitting_defect = relative_fitting_defect - &
747 dot_product(u_ij(:, j), matmul(b_ij, u_ij(:, j)))
748 END DO
749 IF (abs(relative_fitting_defect) < 100.0_dp*epsilon(1.0_dp)) THEN
750 relative_fitting_defect = 0.0_dp
751 END IF
752 ! A projection cannot capture more than the exact product norm.
753 IF (relative_fitting_defect < -1.0e-8_dp .OR. &
754 relative_fitting_defect > 1.0_dp + 1.0e-8_dp) THEN
755 cpabort("AUTO_RI conditioned atom-local eigensolver failed")
756 END IF
757 IF (nret > 0) THEN
758 ALLOCATE (m_pp_check(nret, nret), eigenval_m(nret), u_pp_a_rotated(n, nret))
759 m_pp_check(:, :) = matmul(transpose(u_pp_a), &
760 matmul(0.5_dp*(m_pq + transpose(m_pq)), u_pp_a))
761 CALL diamat_all(m_pp_check, eigenval_m, dac=.true.)
762 IF (minval(eigenval_m) <= reference_m_eigenval_threshold) THEN
763 cpabort("AUTO_RI conditioned atom-local eigensolver failed")
764 END IF
765 u_pp_a_rotated(:, :) = matmul(u_pp_a, m_pp_check)
766 DO i = 1, nret
767 u_pp_a_rotated(:, i) = u_pp_a_rotated(:, i)/sqrt(eigenval_m(i))
768 END DO
769 u_pp_a(:, :) = matmul(u_pp_a_rotated, transpose(m_pp_check))
770 m_pp_check(:, :) = matmul(transpose(u_pp_a), &
771 matmul(0.5_dp*(m_pq + transpose(m_pq)), u_pp_a))
772 DO i = 1, nret
773 m_pp_check(i, i) = m_pp_check(i, i) - 1.0_dp
774 END DO
775 IF (maxval(abs(m_pp_check)) >= 1.0e-7_dp) THEN
776 cpabort("AUTO_RI conditioned atom-local eigensolver failed")
777 END IF
778 END IF
779
780 CALL timestop(handle)
781
782 END SUBROUTINE solve_aa_eigenproblem
783
784!> \brief Selects the requested RI sizes from the largest positive eigenvalues.
785!> \param eigenval_by_block descending eigenvalues; zero pads unavailable modes
786!> \param RI_size_requested requested total number of functions
787!> \param RI_size_minimum minimum retained size in every nonempty block
788!> \param sizes_RI_selected number retained from each block
789! **************************************************************************************************
790 SUBROUTINE select_ri_sizes(eigenval_by_block, RI_size_requested, RI_size_minimum, &
791 sizes_RI_selected)
792 REAL(kind=dp), INTENT(IN) :: eigenval_by_block(:, :)
793 INTEGER, INTENT(IN) :: ri_size_requested, ri_size_minimum
794 INTEGER, INTENT(OUT) :: sizes_ri_selected(:)
795
796 CHARACTER(LEN=*), PARAMETER :: routinen = 'select_RI_sizes'
797
798 INTEGER :: block, handle, i, n, nmode, remaining
799 INTEGER, ALLOCATABLE :: indices(:)
800 REAL(kind=dp), ALLOCATABLE :: values(:)
801
802 CALL timeset(routinen, handle)
803
804 nmode = SIZE(eigenval_by_block, 1)
805 n = SIZE(eigenval_by_block)
806 cpassert(SIZE(sizes_ri_selected) == SIZE(eigenval_by_block, 2))
807 cpassert(ri_size_minimum >= 0)
808 sizes_ri_selected(:) = 0
809 IF (n > 0) THEN
810 ALLOCATE (values(n), indices(n))
811 values(:) = -reshape(eigenval_by_block, [n])
812 DO block = 1, SIZE(sizes_ri_selected)
813 sizes_ri_selected(block) = &
814 min(ri_size_minimum, count(eigenval_by_block(:, block) > 0.0_dp))
815 values((block - 1)*nmode + 1: &
816 (block - 1)*nmode + sizes_ri_selected(block)) = 0.0_dp
817 END DO
818 CALL sort(values, n, indices)
819 remaining = min(max(0, ri_size_requested - sum(sizes_ri_selected)), count(values < 0.0_dp))
820 DO i = 1, remaining
821 block = (indices(i) - 1)/nmode + 1
822 sizes_ri_selected(block) = sizes_ri_selected(block) + 1
823 END DO
824 END IF
825
826 CALL timestop(handle)
827
828 END SUBROUTINE select_ri_sizes
829
830! **************************************************************************************************
831!> \brief Computes orbital factors for the occupied-empty weight in Eq. (5).
832!>
833!> weights(n) = 1/|ε_n-E_F|, so weights(i)*weights(a) = w_ia.
834!>
835!> \param eigenval_MO molecular-orbital eigenvalues, occupied first
836!> \param homo Last occupied state in this spin channel.
837!> \param weights factors whose occupied-empty products give w_ia
838! **************************************************************************************************
839 SUBROUTINE compute_orbital_weights(eigenval_MO, homo, weights)
840 REAL(kind=dp), INTENT(IN) :: eigenval_mo(:)
841 INTEGER, INTENT(IN) :: homo
842 REAL(kind=dp), INTENT(OUT) :: weights(:)
843
844 CHARACTER(LEN=*), PARAMETER :: routinen = 'compute_orbital_weights'
845
846 INTEGER :: handle, n
847 REAL(kind=dp) :: gap, middle
848
849 CALL timeset(routinen, handle)
850
851 cpassert(SIZE(weights) == SIZE(eigenval_mo))
852 IF (homo < 1 .OR. homo >= SIZE(eigenval_mo)) THEN
853 cpabort("AUTO_RI orbital weights require occupied and empty states")
854 END IF
855 IF (.NOT. all(ieee_is_finite(eigenval_mo))) THEN
856 cpabort("AUTO_RI orbital weights require finite eigenvalues")
857 END IF
858 gap = eigenval_mo(homo + 1) - eigenval_mo(homo)
859 IF (.NOT. ieee_is_finite(gap) .OR. gap <= 0.0_dp) THEN
860 cpabort("AUTO_RI orbital weights require a finite positive gap")
861 END IF
862 middle = 0.5_dp*(eigenval_mo(homo) + eigenval_mo(homo + 1))
863 DO n = 1, SIZE(eigenval_mo)
864 weights(n) = 1.0_dp/abs(eigenval_mo(n) - middle)
865 END DO
866
867 CALL timestop(handle)
868
869 END SUBROUTINE compute_orbital_weights
870
871! **************************************************************************************************
872!> \brief Computes the occupied-empty contribution B_PQ^C in Eqs. (3) or (10).
873!>
874!> B_PQ^C = Σ_ia w_ia (P|ρ_ia)(ρ_ia|Q).
875!>
876!> \param Int_3c atom-local or atom-pair three-center integrals (μν|P)
877!> \param C_mu_n molecular-orbital coefficients restricted to the relevant AO rows
878!> \param weights Per-orbital weights for this spin channel.
879!> \param homo Last occupied state.
880!> \param B_PQ unnormalized transition matrix for this spin channel
881! **************************************************************************************************
882 SUBROUTINE compute_b_pq_transition(Int_3c, C_mu_n, weights, homo, B_PQ)
883 REAL(kind=dp), CONTIGUOUS, INTENT(IN) :: int_3c(:, :, :), c_mu_n(:, :), weights(:)
884 INTEGER, INTENT(IN) :: homo
885 REAL(kind=dp), INTENT(OUT) :: b_pq(:, :)
886
887 CHARACTER(LEN=*), PARAMETER :: routinen = 'compute_B_PQ_transition'
888
889 INTEGER :: handle, n, nao, nri, nstates
890 REAL(kind=dp), ALLOCATABLE :: empty(:, :), occupied(:, :), scaled(:, :)
891 TYPE(local_gemm_ctxt_type) :: gemm_context
892
893 CALL timeset(routinen, handle)
894
895 CALL gemm_context%create(local_gemm_pu_host)
896 nao = SIZE(c_mu_n, 1)
897 nstates = SIZE(c_mu_n, 2)
898 nri = SIZE(int_3c, 3)
899 cpassert(SIZE(int_3c, 1) == nao .AND. SIZE(int_3c, 2) == nao)
900 cpassert(SIZE(b_pq, 1) == nri .AND. SIZE(b_pq, 2) == nri)
901 cpassert(SIZE(weights) == nstates .AND. homo > 0 .AND. homo < nstates)
902 ALLOCATE (scaled(nao, nstates), occupied(nao, nao), empty(nao, nao))
903 DO n = 1, nstates
904 scaled(:, n) = c_mu_n(:, n)*sqrt(weights(n))
905 END DO
906 CALL gemm_context%gemm( &
907 'N', 'T', nao, nao, homo, 1.0_dp, scaled, nao, scaled, nao, &
908 0.0_dp, occupied, nao)
909 CALL gemm_context%gemm('N', 'T', nao, nao, nstates - homo, 1.0_dp, &
910 scaled(:, homo + 1:), nao, scaled(:, homo + 1:), nao, &
911 0.0_dp, empty, nao)
912 CALL contract_b_pq(int_3c, occupied, empty, b_pq)
913 CALL gemm_context%destroy()
914
915 CALL timestop(handle)
916
917 END SUBROUTINE compute_b_pq_transition
918
919! **************************************************************************************************
920!> \brief Computes the frontier-exchange contribution B_PQ^X in Eqs. (4) or (11).
921!>
922!> B_PQ^X = Σ_{n∈W_occ} Σ_i^occ (P|ρ_ni)(ρ_ni|Q).
923!>
924!> \param Int_3c atom-local or atom-pair three-center integrals (μν|P)
925!> \param C_mu_n molecular-orbital coefficients restricted to the relevant AO rows
926!> \param eigenval_MO molecular-orbital eigenvalues, occupied first, in Hartree
927!> \param homo Last occupied state in this spin channel.
928!> \param occ_energy_window frontier-window extension in Hartree
929!> \param B_PQ unnormalized exchange matrix
930! **************************************************************************************************
931 SUBROUTINE compute_b_pq_exchange(Int_3c, C_mu_n, eigenval_MO, homo, occ_energy_window, B_PQ)
932 REAL(kind=dp), CONTIGUOUS, INTENT(IN) :: int_3c(:, :, :), c_mu_n(:, :), &
933 eigenval_mo(:)
934 INTEGER, INTENT(IN) :: homo
935 REAL(kind=dp), INTENT(IN) :: occ_energy_window
936 REAL(kind=dp), INTENT(OUT) :: b_pq(:, :)
937
938 CHARACTER(LEN=*), PARAMETER :: routinen = 'compute_B_PQ_exchange'
939
940 INTEGER :: handle, n, nao, nstates
941 REAL(kind=dp), ALLOCATABLE :: frontier(:, :), occupied(:, :), &
942 selected(:, :)
943 TYPE(local_gemm_ctxt_type) :: gemm_context
944
945 CALL timeset(routinen, handle)
946
947 b_pq(:, :) = 0.0_dp
948 nstates = SIZE(eigenval_mo)
949 IF (homo < 1 .OR. homo >= nstates) THEN
950 cpabort("AUTO_RI exchange target requires occupied and empty states")
951 END IF
952 IF (.NOT. ieee_is_finite(occ_energy_window) .OR. &
953 .NOT. all(ieee_is_finite(eigenval_mo))) THEN
954 cpabort("AUTO_RI exchange target requires finite input")
955 END IF
956 IF (occ_energy_window < 0.0_dp .OR. eigenval_mo(homo + 1) <= eigenval_mo(homo)) THEN
957 cpabort("AUTO_RI exchange target requires a valid frontier window")
958 END IF
959 nao = SIZE(c_mu_n, 1)
960 cpassert(SIZE(c_mu_n, 2) == nstates)
961 ALLOCATE (occupied(nao, nao), frontier(nao, nao), selected(nao, nstates), source=0.0_dp)
962 DO n = 1, nstates
963 IF (eigenval_mo(n) < eigenval_mo(homo) - occ_energy_window .OR. &
964 eigenval_mo(n) > eigenval_mo(homo + 1) + occ_energy_window) cycle
965 selected(:, n) = c_mu_n(:, n)
966 END DO
967 CALL gemm_context%create(local_gemm_pu_host)
968 ! D_μν = Σ_i^occ C_μi C_νi.
969 ! F_μν = Σ_{n: ε_n ∈ [ε_H−E_win,ε_L+E_win]} C_μn C_νn.
970 CALL gemm_context%gemm('N', 'T', nao, nao, homo, 1.0_dp, c_mu_n, nao, c_mu_n, nao, &
971 0.0_dp, occupied, nao)
972 CALL gemm_context%gemm('N', 'T', nao, nao, nstates, 1.0_dp, selected, nao, selected, nao, &
973 0.0_dp, frontier, nao)
974 CALL contract_b_pq(int_3c, occupied, frontier, b_pq)
975 CALL gemm_context%destroy()
976
977 CALL timestop(handle)
978
979 END SUBROUTINE compute_b_pq_exchange
980
981! **************************************************************************************************
982!> \brief Contracts AO density matrices and (μν|P) to form B_PQ.
983!>
984!> B_PQ = Σ_μν (μν|P) [left (..|Q) right]_μν.
985!>
986!> \param Int_3c atom-local or atom-pair three-center integrals (μν|P)
987!> \param left First density block, for occupied partners.
988!> \param right Second density block, for empty or hard-window target states.
989!> \param B_PQ symmetric RI-basis product matrix
990! **************************************************************************************************
991 SUBROUTINE contract_b_pq(Int_3c, left, right, B_PQ)
992 REAL(kind=dp), CONTIGUOUS, INTENT(IN) :: int_3c(:, :, :), left(:, :), right(:, :)
993 REAL(kind=dp), INTENT(OUT) :: b_pq(:, :)
994
995 CHARACTER(LEN=*), PARAMETER :: routinen = 'contract_B_PQ'
996
997 INTEGER :: b, handle, mu, naux, nu, p, q
998 REAL(kind=dp), ALLOCATABLE :: weighted(:, :), work(:, :)
999 TYPE(local_gemm_ctxt_type) :: gemm_context
1000
1001 CALL timeset(routinen, handle)
1002
1003 b = SIZE(int_3c, 1)
1004 naux = SIZE(int_3c, 3)
1005 cpassert(SIZE(int_3c, 2) == b)
1006 cpassert(SIZE(left, 1) == b .AND. SIZE(left, 2) == b)
1007 cpassert(SIZE(right, 1) == b .AND. SIZE(right, 2) == b)
1008 cpassert(SIZE(b_pq, 1) == naux .AND. SIZE(b_pq, 2) == naux)
1009 ALLOCATE (work(b, b), weighted(b, b))
1010 CALL gemm_context%create(local_gemm_pu_host)
1011 DO q = 1, naux
1012 ! B_PQ = Σ_μν (μν|P) [left (..|Q) right]_μν.
1013 CALL gemm_context%gemm('N', 'N', b, b, b, 1.0_dp, left, b, int_3c(:, :, q), b, &
1014 0.0_dp, work, b)
1015 CALL gemm_context%gemm('N', 'N', b, b, b, 1.0_dp, work, b, right, b, 0.0_dp, weighted, b)
1016 DO p = 1, q
1017 b_pq(p, q) = 0.0_dp
1018 DO nu = 1, b
1019 DO mu = 1, b
1020 b_pq(p, q) = b_pq(p, q) + int_3c(mu, nu, p)*weighted(mu, nu)
1021 END DO
1022 END DO
1023 b_pq(q, p) = b_pq(p, q)
1024 END DO
1025 END DO
1026 CALL gemm_context%destroy()
1027
1028 CALL timestop(handle)
1029
1030 END SUBROUTINE contract_b_pq
1031
1032! **************************************************************************************************
1033!> \brief Adds separately normalized transition and exchange matrices.
1034!> \param X_Pi inverse metric factor, X_Pi X_Qi = M_PQ^+
1035!> \param B_PQ_transition transition matrix, replaced by the combined target
1036!> \param B_PQ_exchange unnormalized hard-window exchange covariance
1037!> \param target_norm number of nonzero normalized terms
1038! **************************************************************************************************
1039 SUBROUTINE combine_transition_and_exchange_targets( &
1040 X_Pi, B_PQ_transition, B_PQ_exchange, target_norm)
1041 REAL(kind=dp), CONTIGUOUS, INTENT(IN) :: x_pi(:, :)
1042 REAL(kind=dp), INTENT(INOUT) :: b_pq_transition(:, :)
1043 REAL(kind=dp), CONTIGUOUS, INTENT(IN) :: b_pq_exchange(:, :)
1044 REAL(kind=dp), INTENT(OUT) :: target_norm
1045
1046 CHARACTER(LEN=*), PARAMETER :: routinen = 'combine_transition_and_exchange_targets'
1047
1048 INTEGER :: handle, k
1049 REAL(kind=dp) :: exchange_norm, transition_norm
1050 REAL(kind=dp), ALLOCATABLE :: work(:, :)
1051
1052 CALL timeset(routinen, handle)
1053
1054 ALLOCATE (work(SIZE(x_pi, 1), SIZE(x_pi, 2)))
1055 work(:, :) = matmul(b_pq_transition, x_pi)
1056 transition_norm = 0.0_dp
1057 DO k = 1, SIZE(x_pi, 2)
1058 transition_norm = transition_norm + dot_product(x_pi(:, k), work(:, k))
1059 END DO
1060 work(:, :) = matmul(b_pq_exchange, x_pi)
1061 exchange_norm = 0.0_dp
1062 DO k = 1, SIZE(x_pi, 2)
1063 exchange_norm = exchange_norm + dot_product(x_pi(:, k), work(:, k))
1064 END DO
1065 CALL normalize_transition_and_exchange_targets(b_pq_transition, b_pq_exchange, &
1066 transition_norm, exchange_norm, target_norm)
1067
1068 CALL timestop(handle)
1069
1070 END SUBROUTINE combine_transition_and_exchange_targets
1071
1072! **************************************************************************************************
1073!> \brief Combine the transition and exchange targets using their squared-norm traces.
1074!> \param B_PQ_transition ...
1075!> \param B_PQ_exchange ...
1076!> \param transition_norm ...
1077!> \param exchange_norm ...
1078!> \param target_norm ...
1079! **************************************************************************************************
1080 SUBROUTINE normalize_transition_and_exchange_targets(B_PQ_transition, B_PQ_exchange, &
1081 transition_norm, exchange_norm, target_norm)
1082 REAL(kind=dp), INTENT(INOUT) :: b_pq_transition(:, :)
1083 REAL(kind=dp), INTENT(IN) :: b_pq_exchange(:, :), transition_norm, &
1084 exchange_norm
1085 REAL(kind=dp), INTENT(OUT) :: target_norm
1086
1087 target_norm = 0.0_dp
1088 IF (.NOT. ieee_is_finite(transition_norm) .OR. &
1089 .NOT. ieee_is_finite(exchange_norm) .OR. &
1090 transition_norm < 0.0_dp .OR. exchange_norm < 0.0_dp) THEN
1091 cpabort("AUTO_RI target normalization is invalid")
1092 END IF
1093 ! B = B_ia / Tr(M^+ B_ia) + B_ni / Tr(M^+ B_ni), with zero-norm terms omitted.
1094 IF (transition_norm > 0.0_dp) THEN
1095 b_pq_transition(:, :) = b_pq_transition/transition_norm
1096 target_norm = target_norm + 1.0_dp
1097 ELSE
1098 b_pq_transition(:, :) = 0.0_dp
1099 END IF
1100 IF (exchange_norm > 0.0_dp) THEN
1101 b_pq_transition(:, :) = b_pq_transition + b_pq_exchange/exchange_norm
1102 target_norm = target_norm + 1.0_dp
1103 END IF
1104 IF (target_norm <= 0.0_dp .OR. .NOT. all(ieee_is_finite(b_pq_transition))) THEN
1105 cpabort("AUTO_RI target normalization is invalid")
1106 END IF
1107
1108 END SUBROUTINE normalize_transition_and_exchange_targets
1109
1110! **************************************************************************************************
1111!> \brief Computes the atom-local matrix B_PQ^A in Eqs. (3)-(6).
1112!>
1113!> B_PQ^A = B_PQ^{A,C}/N_C^A + B_PQ^{A,X}/N_X^A.
1114!>
1115!> The molecular-orbital coefficient rows belonging to one atom are
1116!> collected on one MPI rank. No global AO-pair matrix is formed.
1117!> \param bs_env ...
1118!> \param iatom Atom whose AO rows are needed.
1119!> \param root_rank MPI rank performing the atomic contractions.
1120!> \param Int_3c atom-local three-center integrals (μν|P)
1121!> \param M_PQ atom-local metric of the large reference RI basis
1122!> \param B_PQ normalized atom-local matrix B_PQ^A
1123!> \param target_norm number of nonzero normalized contributions
1124! **************************************************************************************************
1125 SUBROUTINE compute_b_pq_aa(bs_env, iatom, root_rank, Int_3c, M_PQ, &
1126 B_PQ, target_norm)
1127 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
1128 INTEGER, INTENT(IN) :: iatom, root_rank
1129 REAL(kind=dp), CONTIGUOUS, INTENT(IN) :: int_3c(:, :, :), m_pq(:, :)
1130 REAL(kind=dp), ALLOCATABLE, INTENT(OUT) :: b_pq(:, :)
1131 REAL(kind=dp), INTENT(OUT) :: target_norm
1132
1133 CHARACTER(LEN=*), PARAMETER :: routinen = 'compute_B_PQ_AA'
1134
1135 INTEGER :: column, first, handle, i, j, nao, nri, &
1136 nstates, rank, row, spin
1137 INTEGER, POINTER :: cols(:), rows(:)
1138 REAL(kind=dp), ALLOCATABLE :: b_pq_exchange(:, :), b_pq_spin(:, :), &
1139 c_mu_n(:, :), weights(:), x_pi(:, :)
1140
1141 CALL timeset(routinen, handle)
1142 cpassert(.NOT. bs_env%do_periodic)
1143 NULLIFY (rows, cols)
1144 nao = SIZE(int_3c, 1)
1145 nri = SIZE(int_3c, 3)
1146 nstates = bs_env%n_mo_retained
1147 cpassert(ALLOCATED(bs_env%eigenval_scf_Gamma))
1148 first = 1 + sum(bs_env%sizes_AO(:iatom - 1))
1149 ALLOCATE (b_pq(nri, nri), source=0.0_dp)
1150 ALLOCATE (c_mu_n(nao, nstates), weights(nstates), b_pq_spin(nri, nri))
1151 target_norm = 0.0_dp
1152 ALLOCATE (b_pq_exchange(nri, nri), source=0.0_dp)
1153 DO spin = 1, bs_env%n_spin
1154 CALL compute_orbital_weights(bs_env%eigenval_scf_Gamma(1:nstates, spin), &
1155 bs_env%n_occ(spin), weights)
1156 CALL cp_fm_get_info(bs_env%fm_mo_coeff_Gamma(spin), row_indices=rows, col_indices=cols)
1157 c_mu_n(:, :) = 0.0_dp
1158 DO i = 1, SIZE(rows)
1159 row = rows(i) - first + 1
1160 IF (row < 1 .OR. row > nao) cycle
1161 DO j = 1, SIZE(cols)
1162 column = cols(j)
1163 IF (column > nstates) cycle
1164 c_mu_n(row, column) = bs_env%fm_mo_coeff_Gamma(spin)%local_data(i, j)
1165 END DO
1166 END DO
1167 CALL bs_env%para_env%sum(c_mu_n, root_rank)
1168 IF (bs_env%para_env%mepos /= root_rank) cycle
1169 CALL compute_b_pq_transition(int_3c, c_mu_n, weights, bs_env%n_occ(spin), b_pq_spin)
1170 b_pq(:, :) = b_pq + b_pq_spin
1171 CALL compute_b_pq_exchange(int_3c, c_mu_n, &
1172 bs_env%eigenval_scf_Gamma(1:nstates, spin), &
1173 bs_env%n_occ(spin), bs_env%auto_ri%occ_energy_window, b_pq_spin)
1174 b_pq_exchange(:, :) = b_pq_exchange + b_pq_spin
1175 END DO
1176 IF (bs_env%para_env%mepos /= root_rank) THEN
1177 CALL timestop(handle)
1178 RETURN
1179 END IF
1180 CALL compute_reference_m_pq_inv_factor(m_pq, x_pi, rank)
1181 CALL combine_transition_and_exchange_targets(x_pi, b_pq, b_pq_exchange, target_norm)
1182 CALL timestop(handle)
1183
1184 END SUBROUTINE compute_b_pq_aa
1185
1186! **************************************************************************************************
1187!> \brief Normalizes each contraction coefficient vector U_Pp.
1188!>
1189!> U_Pp <- U_Pp/sqrt(Σ_P |U_Pp|^2).
1190!>
1191!> \param U_Pp contractions from the large RI basis P to optimized functions p
1192! **************************************************************************************************
1193 SUBROUTINE normalize_ri_coefficients(U_Pp)
1194 REAL(kind=dp), DIMENSION(:, :), INTENT(INOUT) :: u_pp
1195
1196 CHARACTER(LEN=*), PARAMETER :: routinen = 'normalize_RI_coefficients'
1197
1198 INTEGER :: handle, j
1199 REAL(kind=dp) :: column_norm
1200
1201 CALL timeset(routinen, handle)
1202
1203 DO j = 1, SIZE(u_pp, 2)
1204 column_norm = norm2(u_pp(:, j))
1205 IF (column_norm <= tiny(1.0_dp)) THEN
1206 cpabort("AUTO_RI produced a numerically zero localized column")
1207 END IF
1208 u_pp(:, j) = u_pp(:, j)/column_norm
1209 END DO
1210
1211 CALL timestop(handle)
1212
1213 END SUBROUTINE normalize_ri_coefficients
1214
1215! **************************************************************************************************
1216!> \brief Creates a matrix in the existing molecular BLACS context.
1217!> \param qs_env ...
1218!> \param matrix ...
1219!> \param nr ...
1220!> \param nc ...
1221! **************************************************************************************************
1222 SUBROUTINE create_distributed_matrix(qs_env, matrix, nr, nc)
1223 TYPE(qs_environment_type), POINTER :: qs_env
1224 TYPE(cp_fm_type), INTENT(OUT) :: matrix
1225 INTEGER, INTENT(IN) :: nr, nc
1226
1227 CHARACTER(LEN=*), PARAMETER :: routinen = 'create_distributed_matrix'
1228
1229 INTEGER :: handle
1230 TYPE(cp_blacs_env_type), POINTER :: blacs
1231 TYPE(cp_fm_struct_type), POINTER :: layout
1232 TYPE(mp_para_env_type), POINTER :: para
1233
1234 CALL timeset(routinen, handle)
1235
1236 NULLIFY (blacs, para, layout)
1237 CALL get_qs_env(qs_env, blacs_env=blacs, para_env=para)
1238 CALL cp_fm_struct_create(layout, context=blacs, nrow_global=nr, ncol_global=nc, para_env=para)
1239 CALL cp_fm_create(matrix, layout, set_zero=.true.)
1240 CALL cp_fm_struct_release(layout)
1241
1242 CALL timestop(handle)
1243
1244 END SUBROUTINE create_distributed_matrix
1245
1246! **************************************************************************************************
1247!> \brief Reads the normalized AA contraction for one atom.
1248!> \param bs_env Band-structure environment
1249!> \param atom Atom index
1250!> \param U_Pp Atom-local AA contraction
1251! **************************************************************************************************
1252 SUBROUTINE load_aa_contraction(bs_env, atom, U_Pp)
1253 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
1254 INTEGER, INTENT(IN) :: atom
1255 REAL(kind=dp), ALLOCATABLE, INTENT(OUT) :: u_pp(:, :)
1256
1257 INTEGER :: first, nopt, nref
1258
1259 nref = bs_env%auto_ri%sizes_ref_RI(atom)
1260 nopt = bs_env%auto_ri%sizes_opt_RI(atom)
1261 first = bs_env%auto_ri%U_Pp_atom_offset(atom)
1262 ALLOCATE (u_pp(nref, nopt))
1263 u_pp(:, :) = reshape(bs_env%auto_ri%U_Pp_atom(first:first + nref*nopt - 1), [nref, nopt])
1264 END SUBROUTINE load_aa_contraction
1265
1266! **************************************************************************************************
1267!> \brief Adds REGULARIZATION_RI to one atom-local reference metric block.
1268!> \param bs_env ...
1269!> \param atom ...
1270!> \param M_PQ atom-local metric block in the large reference RI basis
1271! **************************************************************************************************
1272 SUBROUTINE regularize_m_pq_block(bs_env, atom, M_PQ)
1273 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
1274 INTEGER, INTENT(IN) :: atom
1275 REAL(kind=dp), DIMENSION(:, :), INTENT(INOUT) :: m_pq
1276
1277 CHARACTER(LEN=*), PARAMETER :: routinen = 'regularize_M_PQ_block'
1278
1279 INTEGER :: handle, i, iset, kind, nset
1280 INTEGER, DIMENSION(:), POINTER :: npgf, nsgf_set
1281 INTEGER, DIMENSION(:, :), POINTER :: first_sgf
1282 REAL(kind=dp), DIMENSION(:, :), POINTER :: zet
1283
1284 CALL timeset(routinen, handle)
1285
1286 cpassert(SIZE(m_pq, 1) == SIZE(m_pq, 2))
1287 kind = bs_env%ri_rs%particle_set(atom)%atomic_kind%kind_number
1288 CALL get_gto_basis_set(bs_env%basis_set_RI(kind)%gto_basis_set, &
1289 first_sgf=first_sgf, npgf=npgf, nset=nset, &
1290 nsgf_set=nsgf_set, zet=zet)
1291 DO iset = 1, nset
1292 DO i = first_sgf(1, iset), first_sgf(1, iset) + nsgf_set(iset) - 1
1293 m_pq(i, i) = m_pq(i, i) + &
1294 bs_env%regularization_RI* &
1295 max(1.0_dp, 1.0_dp/minval(zet(1:npgf(iset), iset)))
1296 END DO
1297 END DO
1298
1299 CALL timestop(handle)
1300
1301 END SUBROUTINE regularize_m_pq_block
1302
1303! **************************************************************************************************
1304!> \brief Contracts a physical two-center metric block with AA coefficients.
1305!>
1306!> M_pq^{IJ} = (U^I)^T M_IJ U^J. Each reference block is discarded after contraction.
1307!> \param bs_env ...
1308!> \param atom_a First atom
1309!> \param atom_b Second atom
1310!> \param coulomb true for Coulomb; false for the GW fitting operator
1311!> \param matrix_optimized contracted M_pq or V_pq block in the optimized RI basis
1312! **************************************************************************************************
1313 SUBROUTINE compute_optimized_ri_matrix_block( &
1314 bs_env, atom_a, atom_b, coulomb, matrix_optimized)
1315 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
1316 INTEGER, INTENT(IN) :: atom_a, atom_b
1317 LOGICAL, INTENT(IN) :: coulomb
1318 REAL(kind=dp), ALLOCATABLE, INTENT(OUT) :: matrix_optimized(:, :)
1319
1320 CHARACTER(LEN=*), PARAMETER :: routinen = 'compute_optimized_RI_matrix_block'
1321
1322 INTEGER :: handle
1323 REAL(kind=dp), ALLOCATABLE :: matrix_reference(:, :), u_a(:, :), &
1324 u_b(:, :)
1325
1326 CALL timeset(routinen, handle)
1327 CALL load_aa_contraction(bs_env, atom_a, u_a)
1328 IF (atom_a == atom_b) THEN
1329 ALLOCATE (u_b, source=u_a)
1330 ELSE
1331 CALL load_aa_contraction(bs_env, atom_b, u_b)
1332 END IF
1333 IF (coulomb) THEN
1334 CALL compute_m_pq_atom_pair(bs_env, atom_a, atom_b, matrix_reference)
1335 ELSE
1336 CALL compute_m_pq_atom_pair(bs_env, atom_a, atom_b, matrix_reference, bs_env%ri_metric)
1337 IF (atom_a == atom_b .AND. bs_env%regularization_RI > 0.0_dp .AND. &
1338 bs_env%gw_implementation /= ri_rs_small_cell_full_kp) THEN
1339 CALL regularize_m_pq_block(bs_env, atom_a, matrix_reference)
1340 END IF
1341 END IF
1342 ALLOCATE (matrix_optimized(SIZE(u_a, 2), SIZE(u_b, 2)))
1343 matrix_optimized(:, :) = matmul(transpose(u_a), matmul(matrix_reference, u_b))
1344 IF (.NOT. all(ieee_is_finite(matrix_optimized))) THEN
1345 cpabort("AUTO_RI optimized metric block contains invalid values")
1346 END IF
1347 DEALLOCATE (matrix_reference, u_a, u_b)
1348 CALL timestop(handle)
1349
1350 END SUBROUTINE compute_optimized_ri_matrix_block
1351
1352! **************************************************************************************************
1353!> \brief Builds M_opt or V_opt directly from contracted atom-pair blocks.
1354!> \param qs_env ...
1355!> \param bs_env ...
1356!> \param matrix_pq contracted M_pq or V_pq in the optimized RI basis
1357!> \param coulomb true for Coulomb; false for the GW fitting operator
1358! **************************************************************************************************
1359 SUBROUTINE compute_contracted_ri_matrix(qs_env, bs_env, matrix_pq, coulomb)
1360 TYPE(qs_environment_type), POINTER :: qs_env
1361 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
1362 TYPE(cp_fm_type), INTENT(OUT) :: matrix_pq
1363 LOGICAL, INTENT(IN) :: coulomb
1364
1365 CHARACTER(LEN=*), PARAMETER :: routinen = 'compute_contracted_RI_matrix'
1366
1367 INTEGER :: handle, iatom, jatom, npcol, nprow, &
1368 pair_index, ri_size_opt
1369 INTEGER, POINTER :: col_dist(:), row_dist(:), sizes_opt_ri(:)
1370 REAL(kind=dp), ALLOCATABLE :: matrix_pq_block(:, :)
1371 TYPE(dbcsr_distribution_type) :: dist
1372 TYPE(dbcsr_distribution_type), POINTER :: template
1373 TYPE(dbcsr_type) :: blocks
1374
1375 CALL timeset(routinen, handle)
1376
1377 ALLOCATE (sizes_opt_ri(bs_env%n_atom), row_dist(bs_env%n_atom), col_dist(bs_env%n_atom))
1378 sizes_opt_ri(:) = bs_env%auto_ri%sizes_opt_RI
1379 CALL get_qs_env(qs_env, dbcsr_dist=template)
1380 CALL dbcsr_distribution_get(template, nprows=nprow, npcols=npcol)
1381 DO iatom = 1, SIZE(sizes_opt_ri)
1382 row_dist(iatom) = mod(iatom - 1, nprow)
1383 col_dist(iatom) = mod(iatom - 1, npcol)
1384 END DO
1385 ri_size_opt = sum(sizes_opt_ri)
1386 CALL create_distributed_matrix(qs_env, matrix_pq, ri_size_opt, ri_size_opt)
1387 CALL dbcsr_distribution_new(dist, template=template, row_dist=row_dist, col_dist=col_dist)
1388 CALL dbcsr_create(blocks, name='AUTO_RI contracted metric', dist=dist, &
1389 matrix_type=dbcsr_type_symmetric, &
1390 row_blk_size=sizes_opt_ri, col_blk_size=sizes_opt_ri)
1391
1392 pair_index = 0
1393 DO iatom = 1, SIZE(sizes_opt_ri)
1394 DO jatom = 1, iatom
1395 pair_index = pair_index + 1
1396 IF (mod(pair_index - 1, bs_env%para_env%num_pe) /= bs_env%para_env%mepos) cycle
1397 CALL compute_optimized_ri_matrix_block( &
1398 bs_env, iatom, jatom, coulomb, matrix_pq_block)
1399 IF (iatom == jatom) THEN
1400 matrix_pq_block(:, :) = 0.5_dp*(matrix_pq_block + transpose(matrix_pq_block))
1401 END IF
1402 CALL dbcsr_put_block(blocks, iatom, jatom, matrix_pq_block)
1403 DEALLOCATE (matrix_pq_block)
1404 END DO
1405 END DO
1406 CALL dbcsr_finalize(blocks)
1407 CALL copy_dbcsr_to_fm(blocks, matrix_pq)
1408 CALL dbcsr_release(blocks)
1410 DEALLOCATE (sizes_opt_ri, row_dist, col_dist)
1411
1412 CALL timestop(handle)
1413
1414 END SUBROUTINE compute_contracted_ri_matrix
1415
1416! **************************************************************************************************
1417!> \brief Computes the pseudoinverse of the optimized fitting metric M_opt.
1418!>
1419!> The generalized problem M_opt X=M_AA X λ separates linearly
1420!> dependent directions. The result is M_opt^+=X diag(1/λ) X^T.
1421!> \param qs_env ...
1422!> \param bs_env ...
1423!> \param M_pq optimized RI fitting metric
1424! **************************************************************************************************
1425 SUBROUTINE compute_m_pq_inv(qs_env, bs_env, M_pq)
1426 TYPE(qs_environment_type), POINTER :: qs_env
1427 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
1428 TYPE(cp_fm_type), INTENT(INOUT) :: m_pq
1429
1430 CHARACTER(LEN=*), PARAMETER :: routinen = 'compute_M_pq_inv'
1431
1432 INTEGER :: handle, ncol_local, nrow_local, &
1433 ri_size_opt
1434 INTEGER, DIMENSION(:), POINTER :: col_indices, row_indices
1435 REAL(kind=dp), ALLOCATABLE :: eigenval(:), factors(:)
1436 REAL(kind=dp), CONTIGUOUS, DIMENSION(:, :), &
1437 POINTER :: m_pq_aa_local, m_pq_local
1438 TYPE(cp_fm_type) :: m_pq_aa, work, x_pq
1439
1440 CALL timeset(routinen, handle)
1441
1442 ri_size_opt = sum(bs_env%auto_ri%sizes_opt_RI)
1443 CALL create_distributed_matrix(qs_env, bs_env%auto_ri%M_pq_inv, &
1444 ri_size_opt, ri_size_opt)
1445 CALL create_distributed_matrix(qs_env, x_pq, ri_size_opt, ri_size_opt)
1446 CALL create_distributed_matrix(qs_env, m_pq_aa, ri_size_opt, ri_size_opt)
1447 CALL create_distributed_matrix(qs_env, work, ri_size_opt, ri_size_opt)
1448 CALL cp_fm_get_info(m_pq, nrow_local=nrow_local, ncol_local=ncol_local, &
1449 row_indices=row_indices, col_indices=col_indices, &
1450 local_data=m_pq_local)
1451 CALL cp_fm_get_info(m_pq_aa, local_data=m_pq_aa_local)
1452 m_pq_aa_local = 0.0_dp
1453 CALL copy_m_pq_aa_blocks(m_pq_local(:nrow_local, :ncol_local), &
1454 m_pq_aa_local(:nrow_local, :ncol_local), &
1455 row_indices(:nrow_local), col_indices(:ncol_local), &
1456 bs_env%auto_ri%sizes_opt_RI)
1457
1458 ALLOCATE (eigenval(ri_size_opt))
1459 CALL cp_fm_geeig(m_pq, m_pq_aa, x_pq, eigenval, work)
1460 CALL compute_optimized_m_pq_inv_factors(eigenval, factors)
1461 CALL cp_fm_column_scale(x_pq, factors)
1462 CALL cp_fm_syrk("U", "N", ri_size_opt, 1.0_dp, x_pq, 1, 1, 0.0_dp, &
1463 bs_env%auto_ri%M_pq_inv)
1464 CALL cp_fm_uplo_to_full(bs_env%auto_ri%M_pq_inv, work)
1465
1466 DEALLOCATE (eigenval, factors)
1467 CALL cp_fm_release(x_pq)
1468 CALL cp_fm_release(m_pq_aa)
1469 CALL cp_fm_release(work)
1470
1471 CALL timestop(handle)
1472
1473 END SUBROUTINE compute_m_pq_inv
1474
1475! **************************************************************************************************
1476!> \brief Computes molecular M_opt and V_opt without constructing M_ref or V_ref.
1477!> \param qs_env ...
1478!> \param bs_env ...
1479! **************************************************************************************************
1480 SUBROUTINE compute_m_pq_inv_and_v_pq_molecular(qs_env, bs_env)
1481 TYPE(qs_environment_type), POINTER :: qs_env
1482 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
1483
1484 CHARACTER(LEN=*), PARAMETER :: routinen = 'compute_M_pq_inv_and_V_pq_molecular'
1485
1486 INTEGER :: handle
1487 TYPE(cp_fm_type) :: m_pq
1488
1489 CALL timeset(routinen, handle)
1490
1491 CALL initialize_auto_ri_integrals(bs_env)
1492 CALL compute_contracted_ri_matrix(qs_env, bs_env, m_pq, .false.)
1493 CALL compute_m_pq_inv(qs_env, bs_env, m_pq)
1494 CALL cp_fm_release(m_pq)
1495 CALL compute_contracted_ri_matrix(qs_env, bs_env, bs_env%auto_ri%V_pq, .true.)
1496
1497 ! Activate the optimized per-atom RI block sizes for RI-RS.
1498 CALL set_optimized_ri_basis_sizes(bs_env)
1499
1500 CALL timestop(handle)
1501
1502 END SUBROUTINE compute_m_pq_inv_and_v_pq_molecular
1503
1504! **************************************************************************************************
1505!> \brief Computes the atom-local two-center RI metric (P|Q) and the
1506!> three-center integrals (μν|P) used in Eqs. (3) and (4).
1507!> \param ao_basis ...
1508!> \param ri_basis ...
1509!> \param potential fitting operator used for both integral types
1510!> \param M_PQ two-center metric of the large reference RI basis
1511!> \param Int_3c three-center integrals (μν|P)
1512! **************************************************************************************************
1513 SUBROUTINE compute_int_3c(ao_basis, ri_basis, potential, M_PQ, Int_3c)
1514 TYPE(gto_basis_set_type), INTENT(IN) :: ao_basis, ri_basis
1515 TYPE(coulomb_operator_type), INTENT(IN) :: potential
1516 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :), &
1517 INTENT(OUT) :: m_pq
1518 REAL(kind=dp), INTENT(OUT) :: int_3c(:, :, :)
1519
1520 CHARACTER(LEN=*), PARAMETER :: routinen = 'compute_int_3c'
1521
1522 INTEGER :: handle, iset, jset, kset, max_ao_l, &
1523 max_l, max_ri_l, nao, ncoa, ncob, &
1524 ncoc, nri, nsgfa, nsgfb, nsgfc, sgfa, &
1525 sgfb, sgfc
1526 INTEGER, DIMENSION(:), POINTER :: lmax_a, lmax_c, lmin_a, lmin_c, npgf_a, &
1527 npgf_c, nsgf_set_a, nsgf_set_c
1528 INTEGER, DIMENSION(:, :), POINTER :: first_sgf_a, first_sgf_c
1529 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :) :: int_2c_cart, int_2c_spherical
1530 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :, :) :: int_3c_cart, int_3c_spherical_all, &
1531 int_3c_spherical_block
1532 REAL(kind=dp), DIMENSION(3) :: center
1533 REAL(kind=dp), DIMENSION(:), POINTER :: radius_a, radius_b, radius_c
1534 REAL(kind=dp), DIMENSION(:, :), POINTER :: pgf_radius_a, pgf_radius_c, sphi_a, &
1535 sphi_c, zeta_a, zeta_c
1536 TYPE(coulomb_integral_context_type) :: context
1537
1538 CALL timeset(routinen, handle)
1539
1540 CALL get_gto_basis_set(ao_basis, nsgf=nao, lmax=lmax_a, lmin=lmin_a, &
1541 npgf=npgf_a, nsgf_set=nsgf_set_a, first_sgf=first_sgf_a, &
1542 pgf_radius=pgf_radius_a, sphi=sphi_a, zet=zeta_a)
1543 CALL get_gto_basis_set(ri_basis, nsgf=nri, lmax=lmax_c, lmin=lmin_c, &
1544 npgf=npgf_c, nsgf_set=nsgf_set_c, first_sgf=first_sgf_c, &
1545 pgf_radius=pgf_radius_c, sphi=sphi_c, zet=zeta_c)
1546 cpassert(nao > 0 .AND. nri > 0)
1547 max_ao_l = maxval(lmax_a)
1548 max_ri_l = maxval(lmax_c)
1549 max_l = max(max_ao_l, max_ri_l)
1550 CALL init_orbital_pointers(max(max_l, 2*max_ao_l))
1551 CALL init_spherical_harmonics(max_l, -1)
1552 IF (potential%potential_type == do_potential_truncated .OR. &
1553 potential%potential_type == do_potential_short) THEN
1554 IF (active_integral_library%coulomb2_library /= library_libint .OR. &
1555 active_integral_library%coulomb3_library /= library_libint) THEN
1556 CALL cp_abort(__location__, &
1557 "AUTO_RI range-separated fitting requires LIBINT for "// &
1558 "COULOMB_2C and COULOMB_3C")
1559 END IF
1560 END IF
1561 ! Libint initialization takes the maximum angular momentum of one shell,
1562 ! not the sum of angular momenta entering an integral.
1563 CALL coulomb_integral_init(context, max_am_2c=max_ri_l, max_am_3c=max_l)
1564 center = 0.0_dp
1565
1566 ALLOCATE (m_pq(nri, nri), int_3c_spherical_all(nao, nao, nri))
1567 m_pq = 0.0_dp
1568 int_3c_spherical_all = 0.0_dp
1569
1570 DO iset = 1, ri_basis%nset
1571 ncoa = npgf_c(iset)*ncoset(lmax_c(iset))
1572 sgfa = first_sgf_c(1, iset)
1573 nsgfa = nsgf_set_c(iset)
1574 radius_a => pgf_radius_c(1:npgf_c(iset), iset)
1575 DO jset = 1, ri_basis%nset
1576 ncob = npgf_c(jset)*ncoset(lmax_c(jset))
1577 sgfb = first_sgf_c(1, jset)
1578 nsgfb = nsgf_set_c(jset)
1579 radius_b => pgf_radius_c(1:npgf_c(jset), jset)
1580 ALLOCATE (int_2c_cart(ncoa, ncob), int_2c_spherical(nsgfa, nsgfb))
1581 int_2c_cart = 0.0_dp
1582 CALL compute_coulomb_2c(context, lmin_c(iset), lmax_c(iset), &
1583 lmin_c(jset), lmax_c(jset), &
1584 npgf_c(iset), npgf_c(jset), zeta_c(1:npgf_c(iset), iset), &
1585 zeta_c(1:npgf_c(jset), jset), &
1586 radius_a, radius_b, center, center, int_2c_cart, potential)
1587 CALL ab_contract(int_2c_spherical, int_2c_cart, sphi_c(:, sgfa:), sphi_c(:, sgfb:), &
1588 ncoa, ncob, nsgfa, nsgfb)
1589 m_pq(sgfa:sgfa + nsgfa - 1, sgfb:sgfb + nsgfb - 1) = int_2c_spherical
1590 DEALLOCATE (int_2c_spherical, int_2c_cart)
1591 END DO
1592 END DO
1593
1594 DO iset = 1, ao_basis%nset
1595 ncoa = npgf_a(iset)*ncoset(lmax_a(iset))
1596 sgfa = first_sgf_a(1, iset)
1597 nsgfa = nsgf_set_a(iset)
1598 radius_a => pgf_radius_a(1:npgf_a(iset), iset)
1599 DO jset = 1, ao_basis%nset
1600 ncob = npgf_a(jset)*ncoset(lmax_a(jset))
1601 sgfb = first_sgf_a(1, jset)
1602 nsgfb = nsgf_set_a(jset)
1603 radius_b => pgf_radius_a(1:npgf_a(jset), jset)
1604 DO kset = 1, ri_basis%nset
1605 ncoc = npgf_c(kset)*ncoset(lmax_c(kset))
1606 sgfc = first_sgf_c(1, kset)
1607 nsgfc = nsgf_set_c(kset)
1608 radius_c => pgf_radius_c(1:npgf_c(kset), kset)
1609 ALLOCATE (int_3c_cart(ncoa, ncob, ncoc), &
1610 int_3c_spherical_block(nsgfa, nsgfb, nsgfc))
1611 int_3c_cart = 0.0_dp
1612 CALL compute_coulomb_3c(context, lmin_a(iset), lmax_a(iset), &
1613 lmin_a(jset), lmax_a(jset), &
1614 lmin_c(kset), lmax_c(kset), npgf_a(iset), npgf_a(jset), &
1615 npgf_c(kset), zeta_a(1:npgf_a(iset), iset), &
1616 zeta_a(1:npgf_a(jset), jset), zeta_c(1:npgf_c(kset), kset), &
1617 radius_a, radius_b, radius_c, center, center, center, &
1618 int_3c_cart, potential)
1619 CALL abc_contract(int_3c_spherical_block, int_3c_cart, &
1620 sphi_a(:, sgfa:), sphi_a(:, sgfb:), &
1621 sphi_c(:, sgfc:), ncoa, ncob, ncoc, nsgfa, nsgfb, nsgfc)
1622 int_3c_spherical_all(sgfa:sgfa + nsgfa - 1, sgfb:sgfb + nsgfb - 1, &
1623 sgfc:sgfc + nsgfc - 1) = int_3c_spherical_block
1624 DEALLOCATE (int_3c_spherical_block, int_3c_cart)
1625 END DO
1626 END DO
1627 END DO
1628
1629 int_3c(:, :, :) = int_3c_spherical_all
1630 CALL coulomb_integral_cleanup(context)
1631 IF (.NOT. all(ieee_is_finite(m_pq))) THEN
1632 cpabort("AUTO_RI metric contains invalid values")
1633 END IF
1634 IF (.NOT. all(ieee_is_finite(int_3c))) THEN
1635 cpabort("AUTO_RI three-center integrals contain invalid values")
1636 END IF
1637
1638 CALL timestop(handle)
1639
1640 END SUBROUTINE compute_int_3c
1641
1642! **************************************************************************************************
1643!> \brief Initializes the integral kernels used by the AUTO_RI optimization.
1644!> \param bs_env ...
1645! **************************************************************************************************
1646 SUBROUTINE initialize_auto_ri_integrals(bs_env)
1647 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
1648
1649 CHARACTER(LEN=*), PARAMETER :: routinen = 'initialize_auto_ri_integrals'
1650
1651 INTEGER :: handle, ikind, max_ao_l, max_l, &
1652 max_ri_l, table_lmax
1653
1654 CALL timeset(routinen, handle)
1655
1656 cpassert(ALLOCATED(bs_env%basis_set_AO))
1657 cpassert(ALLOCATED(bs_env%basis_set_RI))
1658 cpassert(SIZE(bs_env%basis_set_AO) == SIZE(bs_env%basis_set_RI))
1659 cpassert(SIZE(bs_env%basis_set_RI) > 0)
1660 max_ao_l = 0
1661 max_ri_l = 0
1662 DO ikind = 1, SIZE(bs_env%basis_set_RI)
1663 cpassert(ASSOCIATED(bs_env%basis_set_AO(ikind)%gto_basis_set))
1664 cpassert(ASSOCIATED(bs_env%basis_set_RI(ikind)%gto_basis_set))
1665 max_ao_l = max(max_ao_l, maxval(bs_env%basis_set_AO(ikind)%gto_basis_set%lmax))
1666 max_ri_l = max(max_ri_l, maxval(bs_env%basis_set_RI(ikind)%gto_basis_set%lmax))
1667 END DO
1668 max_l = max(max_ao_l, max_ri_l)
1669 ! The OS two-center kernel accesses l+1 even without force evaluation.
1670 CALL init_orbital_pointers(max_l + 1)
1671 CALL init_spherical_harmonics(max_l, -1)
1672
1673 IF (bs_env%ri_metric%potential_type == do_potential_truncated) THEN
1674 ! Cover both the RI-RI two-center and AO-AO-RI three-center kernels.
1675 table_lmax = max(2*max_ri_l + 1, 2*max_ao_l + max_ri_l)
1676 CALL r12_fundamentals_init(table_lmax, bs_env%ri_metric, bs_env%para_env)
1677 END IF
1678
1679 CALL timestop(handle)
1680
1681 END SUBROUTINE initialize_auto_ri_integrals
1682
1683! **************************************************************************************************
1684!> \brief Computes the coupling metric between atom-local RI blocks.
1685!> \param bs_env ...
1686!> \param atom_A ...
1687!> \param atom_B ...
1688!> \param M_PQ metric block in the large reference RI basis
1689!> \param ri_potential ...
1690! **************************************************************************************************
1691 SUBROUTINE compute_m_pq_atom_pair(bs_env, atom_A, atom_B, M_PQ, ri_potential)
1692 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
1693 INTEGER, INTENT(IN) :: atom_a, atom_b
1694 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :), &
1695 INTENT(OUT) :: m_pq
1696 TYPE(coulomb_operator_type), INTENT(IN), OPTIONAL :: ri_potential
1697
1698 CHARACTER(LEN=*), PARAMETER :: routinen = 'compute_M_PQ_atom_pair'
1699
1700 INTEGER :: atom_1, atom_2, handle, kind_1, kind_2
1701 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :) :: m_pq_canonical
1702 TYPE(coulomb_operator_type) :: potential
1703
1704 CALL timeset(routinen, handle)
1705
1706 cpassert(ASSOCIATED(bs_env%ri_rs%particle_set))
1707 cpassert(atom_a >= 1 .AND. atom_a <= bs_env%n_atom)
1708 cpassert(atom_b >= 1 .AND. atom_b <= bs_env%n_atom)
1709 IF (atom_a >= atom_b) THEN
1710 atom_1 = atom_a
1711 atom_2 = atom_b
1712 ELSE
1713 atom_1 = atom_b
1714 atom_2 = atom_a
1715 END IF
1716 kind_1 = bs_env%ri_rs%particle_set(atom_1)%atomic_kind%kind_number
1717 kind_2 = bs_env%ri_rs%particle_set(atom_2)%atomic_kind%kind_number
1718
1719 IF (PRESENT(ri_potential)) THEN
1720 IF (ri_potential%potential_type /= do_potential_truncated) THEN
1721 cpassert(ri_potential%potential_type == do_potential_short)
1722 END IF
1723 potential = ri_potential
1724 ELSE
1725 potential%potential_type = do_potential_coulomb
1726 END IF
1727 CALL compute_m_pq( &
1728 basis_a=bs_env%basis_set_RI(kind_1)%gto_basis_set, &
1729 basis_b=bs_env%basis_set_RI(kind_2)%gto_basis_set, &
1730 center_a=bs_env%ri_rs%particle_set(atom_1)%r, &
1731 center_b=bs_env%ri_rs%particle_set(atom_2)%r, &
1732 potential=potential, m_pq=m_pq_canonical)
1733
1734 IF (atom_a >= atom_b) THEN
1735 CALL move_alloc(m_pq_canonical, m_pq)
1736 ELSE
1737 ALLOCATE (m_pq(SIZE(m_pq_canonical, 2), SIZE(m_pq_canonical, 1)))
1738 m_pq(:, :) = transpose(m_pq_canonical)
1739 DEALLOCATE (m_pq_canonical)
1740 END IF
1741
1742 CALL timestop(handle)
1743
1744 END SUBROUTINE compute_m_pq_atom_pair
1745
1746! **************************************************************************************************
1747!> \brief Computes one contracted two-center RI metric block (P_A|Q_B).
1748!> \param basis_a ...
1749!> \param basis_b ...
1750!> \param center_a ...
1751!> \param center_b ...
1752!> \param potential ...
1753!> \param M_PQ metric block in the large reference RI basis
1754! **************************************************************************************************
1755 SUBROUTINE compute_m_pq(basis_a, basis_b, center_a, center_b, potential, M_PQ)
1756 TYPE(gto_basis_set_type), POINTER :: basis_a, basis_b
1757 REAL(kind=dp), DIMENSION(3), INTENT(IN) :: center_a, center_b
1758 TYPE(coulomb_operator_type), INTENT(IN) :: potential
1759 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :), &
1760 INTENT(OUT) :: m_pq
1761
1762 CHARACTER(LEN=*), PARAMETER :: routinen = 'compute_M_PQ'
1763
1764 INTEGER :: handle
1765
1766 CALL timeset(routinen, handle)
1767
1768 ALLOCATE (m_pq(basis_a%nsgf, basis_b%nsgf), source=0.0_dp)
1769 SELECT CASE (potential%potential_type)
1771 CALL int_operators_r12_ab_os(operator_truncated, m_pq, rab=center_b - center_a, &
1772 fba=basis_a, fbb=basis_b, &
1773 r_cutoff=potential%cutoff_radius, calculate_forces=.false.)
1775 CALL int_operators_r12_ab_os(operator_coulomb, m_pq, rab=center_b - center_a, &
1776 fba=basis_a, fbb=basis_b, calculate_forces=.false.)
1777 CASE (do_potential_short)
1778 CALL int_operators_r12_ab_os(operator_verfc, m_pq, rab=center_b - center_a, &
1779 fba=basis_a, fbb=basis_b, &
1780 omega=potential%omega, calculate_forces=.false.)
1781 CASE DEFAULT
1782 cpabort("AUTO_RI two-center metric requires COULOMB, TRUNCATED or SHORT")
1783 END SELECT
1784
1785 CALL timestop(handle)
1786
1787 END SUBROUTINE compute_m_pq
1788
1789! **************************************************************************************************
1790!> \brief Packs normalized atom-local AA coefficients for both paths.
1791!> \param bs_env Band-structure environment
1792!> \param U_Pp_A Atom-local contraction coefficients
1793! **************************************************************************************************
1794 SUBROUTINE store_aa_basis(bs_env, U_Pp_A)
1795 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
1796 REAL(kind=dp), INTENT(IN) :: u_pp_a(:, :, :)
1797
1798 INTEGER :: first, iatom, nopt, nref
1799 REAL(kind=dp), ALLOCATABLE :: u_pp_normalized(:, :)
1800
1801 ALLOCATE (bs_env%auto_ri%U_Pp_atom_offset(bs_env%n_atom))
1802 ALLOCATE (bs_env%auto_ri%U_Pp_atom( &
1803 sum(bs_env%auto_ri%sizes_ref_RI*bs_env%auto_ri%sizes_opt_RI)))
1804 first = 1
1805 DO iatom = 1, bs_env%n_atom
1806 nref = bs_env%auto_ri%sizes_ref_RI(iatom)
1807 nopt = bs_env%auto_ri%sizes_opt_RI(iatom)
1808 bs_env%auto_ri%U_Pp_atom_offset(iatom) = first
1809 IF (nopt > 0) THEN
1810 ALLOCATE (u_pp_normalized(nref, nopt))
1811 u_pp_normalized(:, :) = u_pp_a(1:nref, 1:nopt, iatom)
1812 CALL normalize_ri_coefficients(u_pp_normalized)
1813 bs_env%auto_ri%U_Pp_atom(first:first + nref*nopt - 1) = &
1814 reshape(u_pp_normalized, [nref*nopt])
1815 DEALLOCATE (u_pp_normalized)
1816 END IF
1817 first = first + nref*nopt
1818 END DO
1819 END SUBROUTINE store_aa_basis
1820
1821! **************************************************************************************************
1822!> \brief Forms the periodic atom-local AA target from k-averaged density matrices.
1823!>
1824!> The separable transition weight w_ia(k,k') = a_i(k) b_a(k') permits the
1825!> complete independent k,k' sum to be evaluated as
1826!> J_μν,i = Σ_P (μν|P) X_Pi, with X^T M X = I.
1827!> B_ij = Re Tr[J_i D_empty J_j D_occ]; exchange uses D_frontier and D_occ_exchange.
1828!> Each target is normalized by its trace before adding both terms, Eqs. (3)-(6).
1829!> Transforming the integrals first avoids cancellation in X^T B_PQ X.
1830!> Occupied and empty density matrices use the full SCF k mesh, not the
1831!> band-structure path. The same factorization applies to the frontier target.
1832!> This is an atom-local training objective, not a q-resolved GW error norm.
1833!> \param bs_env ...
1834!> \param iatom ...
1835!> \param root_rank ...
1836!> \param Int_3c ...
1837!> \param M_PQ ...
1838!> \param B_ij ...
1839!> \param target_norm ...
1840!> \param X_Pi inverse metric factor defining the orthonormal RI coordinates
1841! **************************************************************************************************
1842 SUBROUTINE compute_b_ij_aa_kaveraged(bs_env, iatom, root_rank, Int_3c, M_PQ, &
1843 B_ij, target_norm, X_Pi)
1844 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
1845 INTEGER, INTENT(IN) :: iatom, root_rank
1846 REAL(kind=dp), CONTIGUOUS, INTENT(IN) :: int_3c(:, :, :), m_pq(:, :)
1847 REAL(kind=dp), ALLOCATABLE, INTENT(OUT) :: b_ij(:, :)
1848 REAL(kind=dp), INTENT(OUT) :: target_norm
1849 REAL(kind=dp), ALLOCATABLE, INTENT(OUT) :: x_pi(:, :)
1850
1851 CHARACTER(LEN=*), PARAMETER :: routinen = 'compute_B_ij_AA_kaveraged'
1852
1853 COMPLEX(KIND=dp), ALLOCATABLE :: c_mu_n(:, :), d_empty(:, :), &
1854 d_frontier(:, :), d_occ(:, :), &
1855 d_occ_exchange(:, :)
1856 INTEGER :: first, handle, i, ikp, ispin, nao, nri, &
1857 nstates, rank
1858 REAL(kind=dp) :: cbm, e_f, exchange_norm, &
1859 transition_norm, vbm, weight
1860 REAL(kind=dp), ALLOCATABLE :: b_exchange(:, :), b_spin(:, :), &
1861 b_transition(:, :), &
1862 int_3c_metric(:, :, :), weights(:)
1863
1864 CALL timeset(routinen, handle)
1865
1866 cpassert(ALLOCATED(bs_env%cfm_mo_coeff_kp))
1867 cpassert(ALLOCATED(bs_env%eigenval_scf))
1868 cpassert(ASSOCIATED(bs_env%kpoints_scf_desymm))
1869 cpassert(bs_env%nkp_scf_desymm > 0)
1870 cpassert(SIZE(bs_env%cfm_mo_coeff_kp, 1) >= bs_env%nkp_scf_desymm)
1871 cpassert(SIZE(bs_env%eigenval_scf, 2) >= bs_env%nkp_scf_desymm)
1872
1873 nao = bs_env%sizes_AO(iatom)
1874 nri = bs_env%auto_ri%sizes_ref_RI(iatom)
1875 nstates = bs_env%n_mo_retained
1876 cpassert(nstates > 0 .AND. nstates <= SIZE(bs_env%eigenval_scf, 1))
1877 cpassert(all(bs_env%n_occ(1:bs_env%n_spin) > 0))
1878 cpassert(all(bs_env%n_occ(1:bs_env%n_spin) < nstates))
1879 first = 1 + sum(bs_env%sizes_AO(:iatom - 1))
1880 CALL compute_reference_m_pq_inv_factor(m_pq, x_pi, rank)
1881 ALLOCATE (int_3c_metric(nao, nao, rank))
1882 int_3c_metric(:, :, :) = reshape(matmul(reshape(int_3c, [nao*nao, nri]), x_pi), [nao, nao, rank])
1883 ALLOCATE (b_ij(rank, rank), source=0.0_dp)
1884 ALLOCATE (b_exchange(rank, rank), b_spin(rank, rank), b_transition(rank, rank), &
1885 c_mu_n(nao, nstates), weights(nstates), &
1886 d_empty(nao, nao), d_frontier(nao, nao), &
1887 d_occ(nao, nao), d_occ_exchange(nao, nao))
1888 b_exchange = 0.0_dp
1889 b_transition = 0.0_dp
1890
1891 DO ispin = 1, bs_env%n_spin
1892 d_empty = z_zero
1893 d_frontier = z_zero
1894 d_occ = z_zero
1895 d_occ_exchange = z_zero
1896 vbm = maxval(bs_env%eigenval_scf(bs_env%n_occ(ispin), 1:bs_env%nkp_scf_desymm, ispin))
1897 cbm = minval(bs_env%eigenval_scf(bs_env%n_occ(ispin) + 1, 1:bs_env%nkp_scf_desymm, ispin))
1898 IF (.NOT. ieee_is_finite(vbm) .OR. .NOT. ieee_is_finite(cbm) .OR. cbm <= vbm) THEN
1899 cpabort('Periodic AUTO_RI k-averaged training requires a finite positive gap')
1900 END IF
1901 e_f = 0.5_dp*(vbm + cbm)
1902
1903 DO ikp = 1, bs_env%nkp_scf_desymm
1904 CALL cp_cfm_get_submatrix(bs_env%cfm_mo_coeff_kp(ikp, ispin), c_mu_n, &
1905 start_row=first, n_rows=nao, n_cols=nstates)
1906 IF (bs_env%para_env%mepos == root_rank) THEN
1907 weights(:) = 1.0_dp/abs(bs_env%eigenval_scf(1:nstates, ikp, ispin) - e_f)
1908 weight = bs_env%kpoints_scf_desymm%wkp(ikp)
1909 CALL accumulate_aa_density_matrices( &
1910 c_mu_n, bs_env%eigenval_scf(1:nstates, ikp, ispin), &
1911 weights, bs_env%n_occ(ispin), weight, vbm, cbm, &
1912 bs_env%auto_ri%occ_energy_window, &
1913 d_occ, d_empty, d_occ_exchange, d_frontier)
1914 END IF
1915 END DO
1916 IF (bs_env%para_env%mepos == root_rank) THEN
1917 CALL contract_b_pq_density(int_3c_metric, d_empty, d_occ, b_spin)
1918 b_transition(:, :) = b_transition + b_spin
1919 CALL contract_b_pq_density(int_3c_metric, d_frontier, d_occ_exchange, b_spin)
1920 b_exchange(:, :) = b_exchange + b_spin
1921 END IF
1922 END DO
1923
1924 IF (bs_env%para_env%mepos == root_rank) THEN
1925 transition_norm = sum([(b_transition(i, i), i=1, rank)])
1926 exchange_norm = sum([(b_exchange(i, i), i=1, rank)])
1927 CALL normalize_transition_and_exchange_targets(b_transition, b_exchange, &
1928 transition_norm, exchange_norm, target_norm)
1929 b_ij(:, :) = b_transition
1930 END IF
1931 DEALLOCATE (b_exchange, b_spin, b_transition, c_mu_n, weights, &
1932 d_empty, d_frontier, d_occ, d_occ_exchange)
1933 CALL timestop(handle)
1934
1935 END SUBROUTINE compute_b_ij_aa_kaveraged
1936
1937! **************************************************************************************************
1938!> \brief Accumulates occupied, empty and frontier AA density matrices on one atom.
1939!> \param C_mu_n ...
1940!> \param eigenval ...
1941!> \param weights ...
1942!> \param homo ...
1943!> \param wk ...
1944!> \param VBM ...
1945!> \param CBM ...
1946!> \param energy_window ...
1947!> \param D_occ ...
1948!> \param D_empty ...
1949!> \param D_occ_exchange ...
1950!> \param D_frontier ...
1951! **************************************************************************************************
1952 SUBROUTINE accumulate_aa_density_matrices(C_mu_n, eigenval, weights, homo, wk, VBM, CBM, &
1953 energy_window, D_occ, D_empty, D_occ_exchange, &
1954 D_frontier)
1955 COMPLEX(KIND=dp), INTENT(IN) :: c_mu_n(:, :)
1956 REAL(kind=dp), INTENT(IN) :: eigenval(:), weights(:)
1957 INTEGER, INTENT(IN) :: homo
1958 REAL(kind=dp), INTENT(IN) :: wk, vbm, cbm, energy_window
1959 COMPLEX(KIND=dp), INTENT(INOUT) :: d_occ(:, :), d_empty(:, :), &
1960 d_occ_exchange(:, :), d_frontier(:, :)
1961
1962 COMPLEX(KIND=dp) :: product
1963 INTEGER :: imode, imu, inu
1964
1965 DO imode = 1, SIZE(eigenval)
1966 DO inu = 1, SIZE(c_mu_n, 1)
1967 DO imu = 1, SIZE(c_mu_n, 1)
1968 product = wk*c_mu_n(imu, imode)*conjg(c_mu_n(inu, imode))
1969 IF (imode <= homo) THEN
1970 d_occ(imu, inu) = d_occ(imu, inu) + weights(imode)*product
1971 d_occ_exchange(imu, inu) = d_occ_exchange(imu, inu) + product
1972 ELSE
1973 d_empty(imu, inu) = d_empty(imu, inu) + weights(imode)*product
1974 END IF
1975 IF (eigenval(imode) >= vbm - energy_window .AND. &
1976 eigenval(imode) <= cbm + energy_window) THEN
1977 d_frontier(imu, inu) = d_frontier(imu, inu) + product
1978 END IF
1979 END DO
1980 END DO
1981 END DO
1982
1983 END SUBROUTINE accumulate_aa_density_matrices
1984
1985! **************************************************************************************************
1986!> \brief Contracts the factorized local AA density target over complex k-point orbitals.
1987!> \param Int_3c ...
1988!> \param D_right ...
1989!> \param D_left ...
1990!> \param B_PQ ...
1991! **************************************************************************************************
1992 SUBROUTINE contract_b_pq_density(Int_3c, D_right, D_left, B_PQ)
1993 REAL(kind=dp), CONTIGUOUS, INTENT(IN) :: int_3c(:, :, :)
1994 COMPLEX(KIND=dp), INTENT(IN) :: d_right(:, :), d_left(:, :)
1995 REAL(kind=dp), INTENT(OUT) :: b_pq(:, :)
1996
1997 COMPLEX(KIND=dp), ALLOCATABLE :: weighted(:, :)
1998 INTEGER :: ip, iq
1999
2000 ALLOCATE (weighted(SIZE(d_left, 1), SIZE(d_left, 2)))
2001 DO iq = 1, SIZE(int_3c, 3)
2002 weighted(:, :) = matmul(d_right, matmul(int_3c(:, :, iq), d_left))
2003 DO ip = 1, SIZE(int_3c, 3)
2004 b_pq(ip, iq) = real(sum(int_3c(:, :, ip)*transpose(weighted)), kind=dp)
2005 END DO
2006 END DO
2007 b_pq = 0.5_dp*(b_pq + transpose(b_pq))
2008
2009 END SUBROUTINE contract_b_pq_density
2010
2011! **************************************************************************************************
2012!> \brief Solves eigenvalue problem B^A U^A=M^A U^A λ^A in Eq. (6).
2013!>
2014!> Columns are ordered from the largest to the smallest λ^A.
2015!> Columns are retained when λ_i/λ_max >= eigenval_threshold.
2016!> \param M_PQ metric of the large reference RI basis
2017!> \param X_Pi inverse metric factor with X^T M X = I
2018!> \param B_PQ normalized target X^T B^A X, formed directly from transformed integrals
2019!> \param target_normalization ...
2020!> \param U_Pp_A selected AA contractions from the large RI basis P to optimized functions p
2021!> \param eigenval normalized target eigenvalues in descending order
2022! **************************************************************************************************
2023 SUBROUTINE solve_aa_eigenproblem_periodic(M_PQ, X_Pi, B_PQ, target_normalization, U_Pp_A, eigenval)
2024 REAL(kind=dp), DIMENSION(:, :), INTENT(IN) :: m_pq, x_pi, b_pq
2025 REAL(kind=dp), INTENT(IN) :: target_normalization
2026 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :), &
2027 INTENT(OUT) :: u_pp_a
2028 REAL(kind=dp), ALLOCATABLE, DIMENSION(:), &
2029 INTENT(OUT) :: eigenval
2030
2031 CHARACTER(LEN=*), PARAMETER :: routinen = 'solve_AA_eigenproblem_periodic'
2032
2033 CHARACTER(LEN=256) :: diagnostic
2034 INTEGER :: handle, i, iortho, j, m_pq_rank, n, nret
2035 REAL(kind=dp) :: largest_score, metric_defect, &
2036 relative_fitting_defect
2037 REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: eigenval_b, eigenval_m
2038 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :) :: b_ij, m_pp_check, u_ij, u_pp_a_rotated
2039
2040 CALL timeset(routinen, handle)
2041
2042 n = SIZE(m_pq, 1)
2043 cpassert(n > 0)
2044 cpassert(SIZE(m_pq, 2) == n)
2045 m_pq_rank = SIZE(x_pi, 2)
2046 cpassert(SIZE(x_pi, 1) == n)
2047 cpassert(all(shape(b_pq) == [m_pq_rank, m_pq_rank]))
2048 cpassert(target_normalization > 0.0_dp)
2049
2050 ALLOCATE (b_ij(m_pq_rank, m_pq_rank))
2051 b_ij(:, :) = 0.5_dp*(b_pq + transpose(b_pq))/target_normalization
2052 ALLOCATE (u_ij, source=b_ij)
2053 ALLOCATE (eigenval_b(m_pq_rank))
2054 CALL diamat_all(u_ij, eigenval_b, dac=.true.)
2055 largest_score = maxval(eigenval_b)
2056 IF (largest_score <= 0.0_dp) THEN
2057 cpabort("AUTO_RI conditioned atom-local eigensolver failed")
2058 END IF
2059 ! Only positive target modes can contribute to the optimized RI basis.
2060 ! Discard negative modes after diagonalization; they are never selected.
2061 eigenval_b(:) = max(eigenval_b, 0.0_dp)
2062 nret = count(eigenval_b/largest_score >= relative_target_eigenval_threshold)
2063 ALLOCATE (u_pp_a(n, nret), eigenval(m_pq_rank))
2064 DO i = 1, m_pq_rank
2065 eigenval(i) = eigenval_b(m_pq_rank - i + 1)
2066 END DO
2067 eigenval(nret + 1:) = 0.0_dp
2068 DO i = 1, nret
2069 j = m_pq_rank - i + 1
2070 u_pp_a(:, i) = matmul(x_pi, u_ij(:, j))
2071 END DO
2072
2073 relative_fitting_defect = 1.0_dp
2074 DO i = 1, nret
2075 j = m_pq_rank - i + 1
2076 relative_fitting_defect = relative_fitting_defect - &
2077 dot_product(u_ij(:, j), matmul(b_ij, u_ij(:, j)))
2078 END DO
2079 IF (abs(relative_fitting_defect) < conditioned_negative_eigenval_tolerance) THEN
2080 relative_fitting_defect = 0.0_dp
2081 END IF
2082 ! A projection cannot capture more than the exact product norm.
2083 IF (relative_fitting_defect < -conditioned_negative_eigenval_tolerance .OR. &
2084 relative_fitting_defect > 1.0_dp + conditioned_negative_eigenval_tolerance) THEN
2085 WRITE (diagnostic, '(A,ES13.5,A,ES13.5,A,ES13.5,A,I0)') &
2086 'AUTO_RI AA defect=', relative_fitting_defect, &
2087 ' min eig=', minval(eigenval_b), &
2088 ' max eig=', largest_score, ' kept=', nret
2089 cpabort(trim(diagnostic))
2090 END IF
2091 IF (nret > 0) THEN
2092 ALLOCATE (m_pp_check(nret, nret), eigenval_m(nret), u_pp_a_rotated(n, nret))
2093 m_pp_check(:, :) = matmul(transpose(u_pp_a), &
2094 matmul(0.5_dp*(m_pq + transpose(m_pq)), u_pp_a))
2095 CALL diamat_all(m_pp_check, eigenval_m, dac=.true.)
2096 IF (minval(eigenval_m) <= reference_m_eigenval_threshold) THEN
2097 cpabort("AUTO_RI conditioned atom-local eigensolver failed")
2098 END IF
2099 u_pp_a_rotated(:, :) = matmul(u_pp_a, m_pp_check)
2100 DO i = 1, nret
2101 u_pp_a_rotated(:, i) = u_pp_a_rotated(:, i)/sqrt(eigenval_m(i))
2102 END DO
2103 u_pp_a(:, :) = matmul(u_pp_a_rotated, transpose(m_pp_check))
2104 ! Refine M-orthogonality when whitening the reference metric amplifies roundoff.
2105 DO iortho = 1, 5
2106 m_pp_check(:, :) = matmul(transpose(u_pp_a), &
2107 matmul(0.5_dp*(m_pq + transpose(m_pq)), u_pp_a))
2108 DO i = 1, nret
2109 m_pp_check(i, i) = m_pp_check(i, i) - 1.0_dp
2110 END DO
2111 metric_defect = maxval(abs(m_pp_check))
2112 IF (metric_defect < eps_metric_defect) EXIT
2113 IF (iortho == 5) EXIT
2114 DO i = 1, nret
2115 m_pp_check(i, i) = m_pp_check(i, i) + 1.0_dp
2116 END DO
2117 CALL diamat_all(m_pp_check, eigenval_m, dac=.true.)
2118 IF (minval(eigenval_m) <= reference_m_eigenval_threshold) THEN
2119 cpabort("AUTO_RI selected atom-local metric is singular")
2120 END IF
2121 u_pp_a_rotated(:, :) = matmul(u_pp_a, m_pp_check)
2122 DO i = 1, nret
2123 u_pp_a_rotated(:, i) = u_pp_a_rotated(:, i)/sqrt(eigenval_m(i))
2124 END DO
2125 u_pp_a(:, :) = matmul(u_pp_a_rotated, transpose(m_pp_check))
2126 END DO
2127 IF (metric_defect >= eps_metric_defect) THEN
2128 WRITE (diagnostic, '(A,ES13.5,A,I0)') &
2129 'AUTO_RI selected atom metric defect=', metric_defect, ' refinements=', iortho
2130 cpabort(trim(diagnostic))
2131 END IF
2132 END IF
2133
2134 CALL timestop(handle)
2135
2136 END SUBROUTINE solve_aa_eigenproblem_periodic
2137
2138! **************************************************************************************************
2139!> \brief Selects the requested RI sizes from the largest positive eigenvalues.
2140!> Scores within sqrt(machine epsilon) times the largest target score are numerically tied.
2141!> Each group is anchored at its largest score; in the partially retained group, prefer the
2142!> least populated block, then its index. Sorting and integer level filling retain descending
2143!> prefixes in O(N log N + nblock log nmode), with at most the tolerance lost per selected mode.
2144!> \param eigenval_by_block descending eigenvalues; zero pads unavailable modes
2145!> \param RI_size_requested requested total number of functions
2146!> \param RI_size_minimum minimum retained size in every nonempty block
2147!> \param sizes_RI_selected number retained from each block
2148! **************************************************************************************************
2149 SUBROUTINE select_ri_sizes_periodic(eigenval_by_block, RI_size_requested, RI_size_minimum, &
2150 sizes_RI_selected)
2151 REAL(kind=dp), INTENT(IN) :: eigenval_by_block(:, :)
2152 INTEGER, INTENT(IN) :: ri_size_requested, ri_size_minimum
2153 INTEGER, INTENT(OUT) :: sizes_ri_selected(:)
2154
2155 CHARACTER(LEN=*), PARAMETER :: routinen = 'select_RI_sizes_periodic'
2156
2157 INTEGER :: handle, i, iblock, ifirst, ilast, lower, &
2158 middle, n, nmode, npositive, &
2159 remaining, upper
2160 INTEGER, ALLOCATABLE :: capacity(:), indices(:)
2161 REAL(kind=dp) :: tolerance
2162 REAL(kind=dp), ALLOCATABLE :: values(:)
2163
2164 CALL timeset(routinen, handle)
2165
2166 nmode = SIZE(eigenval_by_block, 1)
2167 n = SIZE(eigenval_by_block)
2168 cpassert(SIZE(sizes_ri_selected) == SIZE(eigenval_by_block, 2))
2169 cpassert(ri_size_minimum >= 0)
2170 sizes_ri_selected(:) = 0
2171 IF (n > 0) THEN
2172 ALLOCATE (values(n), indices(n))
2173 tolerance = sqrt(epsilon(1.0_dp))*maxval(abs(eigenval_by_block))
2174 values(:) = -reshape(eigenval_by_block, [n])
2175 DO iblock = 1, SIZE(sizes_ri_selected)
2176 sizes_ri_selected(iblock) = &
2177 min(ri_size_minimum, count(eigenval_by_block(:, iblock) > 0.0_dp))
2178 values((iblock - 1)*nmode + 1: &
2179 (iblock - 1)*nmode + sizes_ri_selected(iblock)) = 0.0_dp
2180 END DO
2181 CALL sort(values, n, indices)
2182 npositive = count(values < 0.0_dp)
2183 remaining = min(max(0, ri_size_requested - sum(sizes_ri_selected)), npositive)
2184 ifirst = 1
2185 DO WHILE (remaining > 0)
2186 ilast = ifirst
2187 DO WHILE (ilast < npositive)
2188 IF (values(ilast + 1) - values(ifirst) > tolerance) EXIT
2189 ilast = ilast + 1
2190 END DO
2191 IF (ilast - ifirst + 1 <= remaining) THEN
2192 DO i = ifirst, ilast
2193 iblock = (indices(i) - 1)/nmode + 1
2194 sizes_ri_selected(iblock) = sizes_ri_selected(iblock) + 1
2195 END DO
2196 remaining = remaining - (ilast - ifirst + 1)
2197 ifirst = ilast + 1
2198 ELSE
2199 ALLOCATE (capacity(SIZE(sizes_ri_selected)), source=0)
2200 DO i = ifirst, ilast
2201 iblock = (indices(i) - 1)/nmode + 1
2202 capacity(iblock) = capacity(iblock) + 1
2203 END DO
2204 ! Fill the boundary group to a common block size; resolve the last layer by index.
2205 lower = minval(sizes_ri_selected)
2206 upper = maxval(sizes_ri_selected + capacity)
2207 DO WHILE (upper - lower > 1)
2208 middle = lower + (upper - lower)/2
2209 IF (sum(min(capacity, max(0, middle - sizes_ri_selected))) <= remaining) THEN
2210 lower = middle
2211 ELSE
2212 upper = middle
2213 END IF
2214 END DO
2215 DO iblock = 1, SIZE(sizes_ri_selected)
2216 i = min(capacity(iblock), max(0, lower - sizes_ri_selected(iblock)))
2217 sizes_ri_selected(iblock) = sizes_ri_selected(iblock) + i
2218 capacity(iblock) = capacity(iblock) - i
2219 remaining = remaining - i
2220 END DO
2221 DO iblock = 1, SIZE(sizes_ri_selected)
2222 IF (remaining == 0) EXIT
2223 IF (capacity(iblock) == 0 .OR. sizes_ri_selected(iblock) /= lower) cycle
2224 sizes_ri_selected(iblock) = sizes_ri_selected(iblock) + 1
2225 remaining = remaining - 1
2226 END DO
2227 cpassert(remaining == 0)
2228 END IF
2229 END DO
2230 END IF
2231
2232 CALL timestop(handle)
2233
2234 END SUBROUTINE select_ri_sizes_periodic
2235
static GRID_HOST_DEVICE int modulo(int a, int m)
Equivalent of Fortran's MODULO, which always return a positive number. https://gcc....
Contraction of integrals over primitive Cartesian Gaussians based on the contraction matrix sphi whic...
subroutine, public abc_contract(abcint, sabc, sphi_a, sphi_b, sphi_c, ncoa, ncob, ncoc, nsgfa, nsgfb, nsgfc)
contract three-center overlap integrals (a,b,c) and transfer to spherical Gaussians
subroutine, public ab_contract(abint, sab, sphi_a, sphi_b, ncoa, ncob, nsgfa, nsgfb)
contract overlap integrals (a,b) and transfer to spherical Gaussians
Definition atom.F:9
Define the atomic kind types and their sub types.
subroutine, public get_atomic_kind_set(atomic_kind_set, atom_of_kind, kind_of, natom_of_kind, maxatom, natom, nshell, fist_potential_present, shell_present, shell_adiabatic, shell_check_distance, damping_present)
Get attributes of an atomic kind set.
subroutine, public get_gto_basis_set(gto_basis_set, name, aliases, norm_type, kind_radius, ncgf, nset, nsgf, cgf_symbol, sgf_symbol, norm_cgf, set_radius, lmax, lmin, lx, ly, lz, m, ncgf_set, npgf, nsgf_set, nshell, cphi, pgf_radius, sphi, scon, zet, first_cgf, first_sgf, l, last_cgf, last_sgf, n, gcc, maxco, maxl, maxpgf, maxsgf_set, maxshell, maxso, nco_sum, npgf_sum, nshell_sum, maxder, short_kind_radius, npgf_seg_sum, ccon)
...
constants for the different operators of the 2c-integrals
integer, parameter, public operator_truncated
integer, parameter, public operator_verfc
integer, parameter, public operator_coulomb
Common interface for two- and three-center Coulomb integrals.
methods related to the blacs parallel environment
Represents a complex full matrix distributed on many processors.
subroutine, public cp_cfm_get_submatrix(fm, target_m, start_row, start_col, n_rows, n_cols, transpose)
Extract a sub-matrix from the full matrix: op(target_m)(1:n_rows,1:n_cols) = fm(start_row:start_row+n...
subroutine, public dbcsr_distribution_release(dist)
...
subroutine, public dbcsr_distribution_new(dist, template, group, pgrid, row_dist, col_dist, reuse_arrays)
...
subroutine, public dbcsr_finalize(matrix)
...
subroutine, public dbcsr_release(matrix)
...
subroutine, public dbcsr_put_block(matrix, row, col, block, summation)
...
subroutine, public dbcsr_distribution_get(dist, row_dist, col_dist, nrows, ncols, has_threads, group, mynode, numnodes, nprows, npcols, myprow, mypcol, pgrid, subgroups_defined, prow_group, pcol_group)
...
DBCSR operations in CP2K.
subroutine, public copy_dbcsr_to_fm(matrix, fm, plan, check_plan)
Copy a DBCSR matrix to a BLACS matrix.
Basic linear algebra operations for full matrices.
subroutine, public cp_fm_syrk(uplo, trans, k, alpha, matrix_a, ia, ja, beta, matrix_c)
performs a rank-k update of a symmetric matrix_c matrix_c = beta * matrix_c + alpha * matrix_a * tran...
subroutine, public cp_fm_uplo_to_full(matrix, work, uplo)
given a triangular matrix according to uplo, computes the corresponding full matrix
subroutine, public cp_fm_column_scale(matrixa, scaling, first_col, source)
Scales columns of the full matrix by corresponding factors.
used for collecting some of the diagonalization schemes available for cp_fm_type. cp_fm_power also mo...
Definition cp_fm_diag.F:17
subroutine, public cp_fm_geeig(amatrix, bmatrix, eigenvectors, eigenvalues, work)
General Eigenvalue Problem AX = BXE. Use cuSOLVERMp directly when requested and large enough; otherwi...
represent the structure of a full matrix
subroutine, public cp_fm_struct_create(fmstruct, para_env, context, nrow_global, ncol_global, nrow_block, ncol_block, descriptor, first_p_pos, local_leading_dimension, template_fmstruct, square_blocks, force_block)
allocates and initializes a full matrix structure
subroutine, public cp_fm_struct_release(fmstruct)
releases a full matrix structure
represent a full matrix distributed on many processors
Definition cp_fm_types.F:15
subroutine, public cp_fm_get_info(matrix, name, nrow_global, ncol_global, nrow_block, ncol_block, nrow_local, ncol_local, row_indices, col_indices, local_data, context, nrow_locals, ncol_locals, matrix_struct, para_env)
returns all kind of information about the full matrix
subroutine, public cp_fm_create(matrix, matrix_struct, name, nrow, ncol, set_zero)
creates a new full matrix with the given structure
Calculation of contracted, spherical Gaussian integrals using the (OS) integral scheme....
subroutine, public int_operators_r12_ab_os(r12_operator, vab, dvab, rab, fba, fbb, omega, r_cutoff, calculate_forces)
Calcululates the two-center integrals of the type (a|O(r12)|b) using the OS scheme.
Automatic RI basis set optimization for molecular and periodic GW.
subroutine, public generate_auto_ri_basis(qs_env, bs_env)
Selects molecular or periodic atom-local AA RI-basis optimization.
collects all constants needed in input so that they can be used without circular dependencies
integer, parameter, public tensor_small_cell_full_kp
integer, parameter, public ri_rs_small_cell_full_kp
integer, parameter, public do_potential_truncated
integer, parameter, public do_potential_coulomb
integer, parameter, public do_potential_short
Library choices for electronic integral APIs.
type(integral_library_type), save, public active_integral_library
integer, parameter, public library_libint
Defines the basic variable types.
Definition kinds.F:23
integer, parameter, public dp
Definition kinds.F:34
integer, parameter, public local_gemm_pu_host
Machine interface based on Fortran 2003 and POSIX.
Definition machine.F:17
real(kind=dp) function, public m_walltime()
returns time from a real-time clock, protected against rolling early/easily
Definition machine.F:144
Definition of mathematical constants and functions.
complex(kind=dp), parameter, public z_zero
Collection of simple mathematical functions and subroutines.
Definition mathlib.F:15
subroutine, public diamat_all(a, eigval, dac)
Diagonalize the symmetric n by n matrix a using the LAPACK library. Only the upper triangle of matrix...
Definition mathlib.F:383
Interface to the message passing library MPI.
Provides Cartesian and spherical orbital pointers and indices.
subroutine, public init_orbital_pointers(maxl)
Initialize or update the orbital pointers.
integer, dimension(:), allocatable, public ncoset
Calculation of the spherical harmonics and the corresponding orbital transformation matrices.
subroutine, public init_spherical_harmonics(maxl, output_unit)
Initialize or update the orbital transformation matrices.
subroutine, public get_qs_env(qs_env, atomic_kind_set, qs_kind_set, cell, super_cell, cell_ref, use_ref_cell, kpoints, dft_control, mos, sab_orb, sab_all, qmmm, qmmm_periodic, mimic, sac_ae, sac_ppl, sac_lri, sap_ppnl, sab_vdw, sab_scp, sap_oce, sab_lrc, sab_se, sab_xtbe, sab_tbe, sab_core, sab_xb, sab_xtb_pp, sab_xtb_nonbond, sab_almo, sab_kp, sab_kp_nosym, sab_cneo, particle_set, energy, force, matrix_h, matrix_h_im, matrix_ks, matrix_ks_im, matrix_vxc, run_rtp, rtp, matrix_h_kp, matrix_h_im_kp, matrix_ks_kp, matrix_ks_im_kp, matrix_vxc_kp, kinetic_kp, matrix_s_kp, matrix_w_kp, matrix_s_ri_aux_kp, matrix_s, matrix_s_ri_aux, matrix_w, matrix_p_mp2, matrix_p_mp2_admm, matrix_vhxc, rho, rho_xc, pw_env, ewald_env, ewald_pw, active_space, mpools, input, para_env, blacs_env, scf_control, rel_control, kinetic, qs_charges, vppl, xcint_weights, rho_core, rho_nlcc, rho_nlcc_g, ks_env, ks_qmmm_env, wf_history, scf_env, local_particles, local_molecules, distribution_2d, dbcsr_dist, molecule_kind_set, molecule_set, subsys, cp_subsys, oce, local_rho_set, rho_atom_set, task_list, task_list_soft, rho0_atom_set, rho0_mpole, rhoz_set, rhoz_cneo_set, ecoul_1c, rho0_s_rs, rho0_s_gs, rhoz_cneo_s_rs, rhoz_cneo_s_gs, do_kpoints, has_unit_metric, requires_mo_derivs, mo_derivs, mo_loc_history, nkind, natom, nelectron_total, nelectron_spin, efield, neighbor_list_id, linres_control, xas_env, virial, cp_ddapc_env, cp_ddapc_ewald, outer_scf_history, outer_scf_ihistory, x_data, et_coupling, dftb_potential, results, se_taper, se_store_int_env, se_nddo_mpole, se_nonbond_env, admm_env, lri_env, lri_density, exstate_env, ec_env, harris_env, dispersion_env, gcp_env, vee, rho_external, external_vxc, mask, mp2_env, bs_env, kg_env, wanniercentres, atprop, ls_scf_env, do_transport, transport_env, v_hartree_rspace, s_mstruct_changed, rho_changed, potential_changed, forces_up_to_date, mscfg_env, almo_scf_env, gradient_history, variable_history, embed_pot, spin_embed_pot, polar_env, mos_last_converged, eeq, rhs, do_rixs, tb_tblite)
Get the QUICKSTEP environment.
Radial fundamentals for Gaussian two-electron integrals. Entry m+1 stores order m....
subroutine, public r12_fundamentals_init(m_max, potential_parameter, para_env)
Prepare shared radial evaluator tables before entering a threaded evaluation region.
All kind of helpful little routines.
Definition util.F:14
represent a blacs multidimensional parallel environment (for the mpi corrispective see cp_paratypes/m...
keeps the information about the structure of a full matrix
represent a full matrix
stores all the informations relevant to an mpi environment