24 coulomb_integral_cleanup,&
25 coulomb_integral_context_type,&
26 coulomb_integral_init,&
47 USE ieee_arithmetic,
ONLY: ieee_is_finite
71#include "base/base_uses.f90"
76 CHARACTER(len=*),
PARAMETER,
PRIVATE :: moduleN =
'gw_auto_ri_generate_RI_basis'
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
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))
86 REAL(KIND=
dp),
PARAMETER :: eps_metric_defect = 1.0e-7_dp
146 IF (bs_env%do_periodic)
THEN
147 CALL generate_auto_ri_basis_periodic(bs_env)
149 CALL generate_auto_ri_basis_molecular(qs_env, bs_env)
159 SUBROUTINE generate_auto_ri_basis_molecular(qs_env, bs_env)
163 CHARACTER(LEN=*),
PARAMETER :: routinen =
'generate_auto_ri_basis_molecular'
166 REAL(kind=
dp) :: time_start
167 REAL(kind=
dp),
ALLOCATABLE :: u_pp_a(:, :, :)
169 CALL timeset(routinen, handle)
171 cpassert(.NOT. bs_env%do_periodic)
175 CALL compute_aa_optimized_ri_basis(bs_env, u_pp_a)
177 CALL store_aa_basis(bs_env, u_pp_a)
181 CALL compute_m_pq_inv_and_v_pq_molecular(qs_env, bs_env)
184 CALL print_auto_ri_summary(bs_env, time_start)
186 CALL timestop(handle)
188 END SUBROUTINE generate_auto_ri_basis_molecular
194 SUBROUTINE generate_auto_ri_basis_periodic(bs_env)
197 CHARACTER(LEN=*),
PARAMETER :: routinen =
'generate_auto_ri_basis_periodic'
200 REAL(kind=
dp) :: time_start
201 REAL(kind=
dp),
ALLOCATABLE :: u_pp_a(:, :, :)
203 CALL timeset(routinen, handle)
205 cpassert(bs_env%do_periodic)
209 CALL compute_aa_optimized_ri_basis(bs_env, u_pp_a)
211 CALL store_aa_basis(bs_env, u_pp_a)
215 CALL set_optimized_ri_basis_sizes(bs_env)
218 CALL print_auto_ri_summary(bs_env, time_start)
220 CALL timestop(handle)
222 END SUBROUTINE generate_auto_ri_basis_periodic
242 SUBROUTINE compute_aa_optimized_ri_basis(bs_env, U_Pp_A)
244 REAL(kind=
dp),
ALLOCATABLE,
INTENT(OUT) :: u_pp_a(:, :, :)
246 CHARACTER(LEN=*),
PARAMETER :: routinen =
'compute_AA_optimized_RI_basis'
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, &
255 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:, :, :) :: m_pq_by_kind
256 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:, :, :, :) :: int_3c
258 CALL timeset(routinen, handle)
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))
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)
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))
284 CALL add_ri_metric_regularization(bs_env%basis_set_RI(ikind)%gto_basis_set, &
285 bs_env%regularization_RI, m_pq)
287 m_pq_by_kind(1:nri_kind, 1:nri_kind, ikind) = m_pq
291 bs_env%auto_ri%sizes_opt_RI = 0
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)
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)
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)
310 IF (root_rank /= bs_env%para_env%mepos)
THEN
312 IF (
ALLOCATED(x_pi))
DEALLOCATE (x_pi)
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)
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)
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)
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)
335 CALL select_ri_sizes(eigenval_aa, ri_size_opt_aa_requested, 1, sizes_opt_ri_aa)
337 bs_env%auto_ri%sizes_opt_RI(:) = sizes_opt_ri_aa
339 u_pp_a(:, sizes_opt_ri_aa(iatom) + 1:, iatom) = 0.0_dp
341 DEALLOCATE (int_3c, m_pq_by_kind)
343 CALL timestop(handle)
345 END SUBROUTINE compute_aa_optimized_ri_basis
358 SUBROUTINE initialize_aa_optimization(bs_env, kind_of_atom, &
359 natom, nkind, max_nRI_kind, &
360 RI_size_opt_AA_requested)
362 INTEGER,
ALLOCATABLE,
INTENT(OUT) :: kind_of_atom(:)
363 INTEGER,
INTENT(OUT) :: natom, nkind, max_nri_kind, &
364 ri_size_opt_aa_requested
366 CHARACTER(LEN=*),
PARAMETER :: routinen =
'initialize_AA_optimization'
368 INTEGER :: handle, ikind, nao_total, nri_reference, &
370 INTEGER,
ALLOCATABLE :: sizes_ref_ri_by_kind(:)
372 CALL timeset(routinen, handle)
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)
382 cpassert(
SIZE(bs_env%basis_set_AO) == nkind)
383 cpassert(bs_env%do_gw_ri_rs)
384 IF (bs_env%do_periodic .AND. &
386 cpabort(
"Periodic AUTO_RI requires RI_RS_SMALL_CELL_FULL_KP")
392 kind_of=kind_of_atom)
393 ALLOCATE (bs_env%auto_ri%sizes_opt_RI(natom))
394 ALLOCATE (sizes_ref_ri_by_kind(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
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")
411 ri_size_opt_aa_requested = min(ri_size_requested, nri_reference)
413 ri_size_opt_aa_requested = max(natom, min(ri_size_requested, nri_reference))
415 DEALLOCATE (sizes_ref_ri_by_kind)
417 CALL timestop(handle)
419 END SUBROUTINE initialize_aa_optimization
424 SUBROUTINE set_optimized_ri_basis_sizes(bs_env)
427 CHARACTER(LEN=*),
PARAMETER :: routinen =
'set_optimized_RI_basis_sizes'
431 CALL timeset(routinen, handle)
433 bs_env%sizes_RI(:) = bs_env%auto_ri%sizes_opt_RI
434 bs_env%auto_ri%ready = .true.
436 CALL timestop(handle)
438 END SUBROUTINE set_optimized_ri_basis_sizes
445 SUBROUTINE print_auto_ri_summary(bs_env, time_start)
447 REAL(kind=
dp),
INTENT(IN) :: time_start
449 CHARACTER(LEN=*),
PARAMETER :: routinen =
'print_auto_ri_summary'
451 INTEGER :: handle, ri_size_optimized, unit_nr
452 REAL(kind=
dp) :: elapsed
454 CALL timeset(routinen, handle)
456 ri_size_optimized = sum(bs_env%auto_ri%sizes_opt_RI)
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'
468 CALL timestop(handle)
470 END SUBROUTINE print_auto_ri_summary
479 SUBROUTINE add_ri_metric_regularization(basis, regularization, M_PQ, offset)
482 REAL(kind=
dp),
INTENT(IN) :: regularization
483 REAL(kind=
dp),
DIMENSION(:, :),
INTENT(INOUT) :: m_pq
484 INTEGER,
INTENT(IN),
OPTIONAL :: offset
486 CHARACTER(LEN=*),
PARAMETER :: routinen =
'add_RI_metric_regularization'
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
494 CALL timeset(routinen, handle)
497 IF (
PRESENT(offset)) start = offset
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))
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
510 CALL timestop(handle)
512 END SUBROUTINE add_ri_metric_regularization
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
528 CHARACTER(LEN=*),
PARAMETER :: routinen =
'copy_M_pq_AA_blocks'
530 INTEGER :: handle, i, j, last_index
531 INTEGER,
ALLOCATABLE,
DIMENSION(:) :: block_ends, col_atom, row_atom
533 CALL timeset(routinen, handle)
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))
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)
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))
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)
553 DO j = 1,
SIZE(col_indices)
554 col_atom(j) = m_pq_index_atom(col_indices(j), block_ends)
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)
566 CALL timestop(handle)
568 END SUBROUTINE copy_m_pq_aa_blocks
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
580 CHARACTER(LEN=*),
PARAMETER :: routinen =
'compute_optimized_M_pq_inv_factors'
582 INTEGER :: handle, i, n_dependent
583 REAL(kind=
dp) :: largest, threshold
585 CALL timeset(routinen, handle)
587 cpassert(
SIZE(eigenval) > 0)
589 largest = maxval(eigenval)
590 cpassert(largest > 0.0_dp)
591 threshold = optimized_m_eigenval_threshold*largest
592 ALLOCATE (factors(
SIZE(eigenval)))
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))
599 n_dependent = n_dependent + 1
602 cpassert(n_dependent <
SIZE(eigenval))
604 CALL timestop(handle)
606 END SUBROUTINE compute_optimized_m_pq_inv_factors
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
619 INTEGER :: high, low, middle
622 high =
SIZE(block_ends)
623 DO WHILE (low < high)
624 middle = (low + high)/2
625 IF (global_index <= block_ends(middle))
THEN
633 END FUNCTION m_pq_index_atom
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(:, :), &
648 INTEGER,
INTENT(OUT) :: rank
650 CHARACTER(LEN=*),
PARAMETER :: routinen =
'compute_reference_M_PQ_inv_factor'
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
657 CALL timeset(routinen, handle)
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")
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))
674 IF (eigenval(i) <= reference_m_eigenval_threshold*largest) cycle
676 x_pi(:, column) = eigenvec(:, i)/sqrt(eigenval(i))
679 CALL timestop(handle)
681 END SUBROUTINE compute_reference_m_pq_inv_factor
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
702 CHARACTER(LEN=*),
PARAMETER :: routinen =
'solve_AA_eigenproblem'
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, &
710 CALL timeset(routinen, handle)
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)
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))
725 largest_score = maxval(eigenval_b)
726 IF (largest_score <= 0.0_dp)
THEN
727 cpabort(
"AUTO_RI conditioned atom-local eigensolver failed")
729 IF (minval(eigenval_b) < -conditioned_negative_eigenval_tolerance*largest_score)
THEN
730 cpabort(
"AUTO_RI conditioned atom-local eigensolver failed")
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))
736 eigenval(i) = eigenval_b(m_pq_rank - i + 1)
739 j = m_pq_rank - i + 1
740 u_pp_a(:, i) = matmul(x_pi, u_ij(:, j))
743 relative_fitting_defect = 1.0_dp
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)))
749 IF (abs(relative_fitting_defect) < 100.0_dp*epsilon(1.0_dp))
THEN
750 relative_fitting_defect = 0.0_dp
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")
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")
765 u_pp_a_rotated(:, :) = matmul(u_pp_a, m_pp_check)
767 u_pp_a_rotated(:, i) = u_pp_a_rotated(:, i)/sqrt(eigenval_m(i))
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))
773 m_pp_check(i, i) = m_pp_check(i, i) - 1.0_dp
775 IF (maxval(abs(m_pp_check)) >= 1.0e-7_dp)
THEN
776 cpabort(
"AUTO_RI conditioned atom-local eigensolver failed")
780 CALL timestop(handle)
782 END SUBROUTINE solve_aa_eigenproblem
790 SUBROUTINE select_ri_sizes(eigenval_by_block, RI_size_requested, RI_size_minimum, &
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(:)
796 CHARACTER(LEN=*),
PARAMETER :: routinen =
'select_RI_sizes'
798 INTEGER :: block, handle, i, n, nmode, remaining
799 INTEGER,
ALLOCATABLE :: indices(:)
800 REAL(kind=
dp),
ALLOCATABLE :: values(:)
802 CALL timeset(routinen, handle)
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
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
818 CALL sort(values, n, indices)
819 remaining = min(max(0, ri_size_requested - sum(sizes_ri_selected)), count(values < 0.0_dp))
821 block = (indices(i) - 1)/nmode + 1
822 sizes_ri_selected(block) = sizes_ri_selected(block) + 1
826 CALL timestop(handle)
828 END SUBROUTINE select_ri_sizes
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(:)
844 CHARACTER(LEN=*),
PARAMETER :: routinen =
'compute_orbital_weights'
847 REAL(kind=
dp) :: gap, middle
849 CALL timeset(routinen, handle)
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")
855 IF (.NOT. all(ieee_is_finite(eigenval_mo)))
THEN
856 cpabort(
"AUTO_RI orbital weights require finite eigenvalues")
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")
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)
867 CALL timestop(handle)
869 END SUBROUTINE compute_orbital_weights
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(:, :)
887 CHARACTER(LEN=*),
PARAMETER :: routinen =
'compute_B_PQ_transition'
889 INTEGER :: handle, n, nao, nri, nstates
890 REAL(kind=
dp),
ALLOCATABLE :: empty(:, :), occupied(:, :), scaled(:, :)
893 CALL timeset(routinen, handle)
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))
904 scaled(:, n) = c_mu_n(:, n)*sqrt(weights(n))
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, &
912 CALL contract_b_pq(int_3c, occupied, empty, b_pq)
913 CALL gemm_context%destroy()
915 CALL timestop(handle)
917 END SUBROUTINE compute_b_pq_transition
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(:, :), &
934 INTEGER,
INTENT(IN) :: homo
935 REAL(kind=
dp),
INTENT(IN) :: occ_energy_window
936 REAL(kind=
dp),
INTENT(OUT) :: b_pq(:, :)
938 CHARACTER(LEN=*),
PARAMETER :: routinen =
'compute_B_PQ_exchange'
940 INTEGER :: handle, n, nao, nstates
941 REAL(kind=
dp),
ALLOCATABLE :: frontier(:, :), occupied(:, :), &
945 CALL timeset(routinen, handle)
948 nstates =
SIZE(eigenval_mo)
949 IF (homo < 1 .OR. homo >= nstates)
THEN
950 cpabort(
"AUTO_RI exchange target requires occupied and empty states")
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")
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")
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)
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)
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()
977 CALL timestop(handle)
979 END SUBROUTINE compute_b_pq_exchange
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(:, :)
995 CHARACTER(LEN=*),
PARAMETER :: routinen =
'contract_B_PQ'
997 INTEGER :: b, handle, mu, naux, nu, p, q
998 REAL(kind=
dp),
ALLOCATABLE :: weighted(:, :), work(:, :)
1001 CALL timeset(routinen, handle)
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))
1013 CALL gemm_context%gemm(
'N',
'N', b, b, b, 1.0_dp, left, b, int_3c(:, :, q), b, &
1015 CALL gemm_context%gemm(
'N',
'N', b, b, b, 1.0_dp, work, b, right, b, 0.0_dp, weighted, b)
1020 b_pq(p, q) = b_pq(p, q) + int_3c(mu, nu, p)*weighted(mu, nu)
1023 b_pq(q, p) = b_pq(p, q)
1026 CALL gemm_context%destroy()
1028 CALL timestop(handle)
1030 END SUBROUTINE contract_b_pq
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
1046 CHARACTER(LEN=*),
PARAMETER :: routinen =
'combine_transition_and_exchange_targets'
1048 INTEGER :: handle, k
1049 REAL(kind=
dp) :: exchange_norm, transition_norm
1050 REAL(kind=
dp),
ALLOCATABLE :: work(:, :)
1052 CALL timeset(routinen, handle)
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))
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))
1065 CALL normalize_transition_and_exchange_targets(b_pq_transition, b_pq_exchange, &
1066 transition_norm, exchange_norm, target_norm)
1068 CALL timestop(handle)
1070 END SUBROUTINE combine_transition_and_exchange_targets
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, &
1085 REAL(kind=
dp),
INTENT(OUT) :: target_norm
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")
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
1098 b_pq_transition(:, :) = 0.0_dp
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
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")
1108 END SUBROUTINE normalize_transition_and_exchange_targets
1125 SUBROUTINE compute_b_pq_aa(bs_env, iatom, root_rank, Int_3c, M_PQ, &
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
1133 CHARACTER(LEN=*),
PARAMETER :: routinen =
'compute_B_PQ_AA'
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(:, :)
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)
1163 IF (column > nstates) cycle
1164 c_mu_n(row, column) = bs_env%fm_mo_coeff_Gamma(spin)%local_data(i, j)
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
1176 IF (bs_env%para_env%mepos /= root_rank)
THEN
1177 CALL timestop(handle)
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)
1184 END SUBROUTINE compute_b_pq_aa
1193 SUBROUTINE normalize_ri_coefficients(U_Pp)
1194 REAL(kind=
dp),
DIMENSION(:, :),
INTENT(INOUT) :: u_pp
1196 CHARACTER(LEN=*),
PARAMETER :: routinen =
'normalize_RI_coefficients'
1198 INTEGER :: handle, j
1199 REAL(kind=
dp) :: column_norm
1201 CALL timeset(routinen, handle)
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")
1208 u_pp(:, j) = u_pp(:, j)/column_norm
1211 CALL timestop(handle)
1213 END SUBROUTINE normalize_ri_coefficients
1222 SUBROUTINE create_distributed_matrix(qs_env, matrix, nr, nc)
1225 INTEGER,
INTENT(IN) :: nr, nc
1227 CHARACTER(LEN=*),
PARAMETER :: routinen =
'create_distributed_matrix'
1234 CALL timeset(routinen, handle)
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)
1242 CALL timestop(handle)
1244 END SUBROUTINE create_distributed_matrix
1252 SUBROUTINE load_aa_contraction(bs_env, atom, U_Pp)
1254 INTEGER,
INTENT(IN) ::
atom
1255 REAL(kind=
dp),
ALLOCATABLE,
INTENT(OUT) :: u_pp(:, :)
1257 INTEGER :: first, nopt, nref
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
1272 SUBROUTINE regularize_m_pq_block(bs_env, atom, M_PQ)
1274 INTEGER,
INTENT(IN) ::
atom
1275 REAL(kind=
dp),
DIMENSION(:, :),
INTENT(INOUT) :: m_pq
1277 CHARACTER(LEN=*),
PARAMETER :: routinen =
'regularize_M_PQ_block'
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
1284 CALL timeset(routinen, handle)
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)
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)))
1299 CALL timestop(handle)
1301 END SUBROUTINE regularize_m_pq_block
1313 SUBROUTINE compute_optimized_ri_matrix_block( &
1314 bs_env, atom_a, atom_b, coulomb, matrix_optimized)
1316 INTEGER,
INTENT(IN) :: atom_a, atom_b
1317 LOGICAL,
INTENT(IN) :: coulomb
1318 REAL(kind=
dp),
ALLOCATABLE,
INTENT(OUT) :: matrix_optimized(:, :)
1320 CHARACTER(LEN=*),
PARAMETER :: routinen =
'compute_optimized_RI_matrix_block'
1323 REAL(kind=
dp),
ALLOCATABLE :: matrix_reference(:, :), u_a(:, :), &
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)
1331 CALL load_aa_contraction(bs_env, atom_b, u_b)
1334 CALL compute_m_pq_atom_pair(bs_env, atom_a, atom_b, matrix_reference)
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. &
1339 CALL regularize_m_pq_block(bs_env, atom_a, matrix_reference)
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")
1347 DEALLOCATE (matrix_reference, u_a, u_b)
1348 CALL timestop(handle)
1350 END SUBROUTINE compute_optimized_ri_matrix_block
1359 SUBROUTINE compute_contracted_ri_matrix(qs_env, bs_env, matrix_pq, coulomb)
1363 LOGICAL,
INTENT(IN) :: coulomb
1365 CHARACTER(LEN=*),
PARAMETER :: routinen =
'compute_contracted_RI_matrix'
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(:, :)
1375 CALL timeset(routinen, handle)
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
1381 DO iatom = 1,
SIZE(sizes_opt_ri)
1382 row_dist(iatom) = mod(iatom - 1, nprow)
1383 col_dist(iatom) = mod(iatom - 1, npcol)
1385 ri_size_opt = sum(sizes_opt_ri)
1386 CALL create_distributed_matrix(qs_env, matrix_pq, ri_size_opt, ri_size_opt)
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)
1393 DO iatom = 1,
SIZE(sizes_opt_ri)
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))
1403 DEALLOCATE (matrix_pq_block)
1410 DEALLOCATE (sizes_opt_ri, row_dist, col_dist)
1412 CALL timestop(handle)
1414 END SUBROUTINE compute_contracted_ri_matrix
1425 SUBROUTINE compute_m_pq_inv(qs_env, bs_env, M_pq)
1430 CHARACTER(LEN=*),
PARAMETER :: routinen =
'compute_M_pq_inv'
1432 INTEGER :: handle, ncol_local, nrow_local, &
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
1440 CALL timeset(routinen, handle)
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)
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)
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)
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)
1466 DEALLOCATE (eigenval, factors)
1471 CALL timestop(handle)
1473 END SUBROUTINE compute_m_pq_inv
1480 SUBROUTINE compute_m_pq_inv_and_v_pq_molecular(qs_env, bs_env)
1484 CHARACTER(LEN=*),
PARAMETER :: routinen =
'compute_M_pq_inv_and_V_pq_molecular'
1489 CALL timeset(routinen, handle)
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)
1495 CALL compute_contracted_ri_matrix(qs_env, bs_env, bs_env%auto_ri%V_pq, .true.)
1498 CALL set_optimized_ri_basis_sizes(bs_env)
1500 CALL timestop(handle)
1502 END SUBROUTINE compute_m_pq_inv_and_v_pq_molecular
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
1516 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:, :), &
1518 REAL(kind=
dp),
INTENT(OUT) :: int_3c(:, :, :)
1520 CHARACTER(LEN=*),
PARAMETER :: routinen =
'compute_int_3c'
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, &
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
1538 CALL timeset(routinen, handle)
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)
1556 CALL cp_abort(__location__, &
1557 "AUTO_RI range-separated fitting requires LIBINT for "// &
1558 "COULOMB_2C and COULOMB_3C")
1563 CALL coulomb_integral_init(context, max_am_2c=max_ri_l, max_am_3c=max_l)
1566 ALLOCATE (m_pq(nri, nri), int_3c_spherical_all(nao, nao, nri))
1568 int_3c_spherical_all = 0.0_dp
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)
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)
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")
1634 IF (.NOT. all(ieee_is_finite(int_3c)))
THEN
1635 cpabort(
"AUTO_RI three-center integrals contain invalid values")
1638 CALL timestop(handle)
1640 END SUBROUTINE compute_int_3c
1646 SUBROUTINE initialize_auto_ri_integrals(bs_env)
1649 CHARACTER(LEN=*),
PARAMETER :: routinen =
'initialize_auto_ri_integrals'
1651 INTEGER :: handle, ikind, max_ao_l, max_l, &
1652 max_ri_l, table_lmax
1654 CALL timeset(routinen, handle)
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)
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))
1668 max_l = max(max_ao_l, max_ri_l)
1675 table_lmax = max(2*max_ri_l + 1, 2*max_ao_l + max_ri_l)
1679 CALL timestop(handle)
1681 END SUBROUTINE initialize_auto_ri_integrals
1691 SUBROUTINE compute_m_pq_atom_pair(bs_env, atom_A, atom_B, M_PQ, ri_potential)
1693 INTEGER,
INTENT(IN) :: atom_a, atom_b
1694 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:, :), &
1698 CHARACTER(LEN=*),
PARAMETER :: routinen =
'compute_M_PQ_atom_pair'
1700 INTEGER :: atom_1, atom_2, handle, kind_1, kind_2
1701 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:, :) :: m_pq_canonical
1704 CALL timeset(routinen, handle)
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
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
1719 IF (
PRESENT(ri_potential))
THEN
1723 potential = ri_potential
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)
1734 IF (atom_a >= atom_b)
THEN
1735 CALL move_alloc(m_pq_canonical, m_pq)
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)
1742 CALL timestop(handle)
1744 END SUBROUTINE compute_m_pq_atom_pair
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
1759 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:, :), &
1762 CHARACTER(LEN=*),
PARAMETER :: routinen =
'compute_M_PQ'
1766 CALL timeset(routinen, handle)
1768 ALLOCATE (m_pq(basis_a%nsgf, basis_b%nsgf), source=0.0_dp)
1769 SELECT CASE (potential%potential_type)
1772 fba=basis_a, fbb=basis_b, &
1773 r_cutoff=potential%cutoff_radius, calculate_forces=.false.)
1776 fba=basis_a, fbb=basis_b, calculate_forces=.false.)
1779 fba=basis_a, fbb=basis_b, &
1780 omega=potential%omega, calculate_forces=.false.)
1782 cpabort(
"AUTO_RI two-center metric requires COULOMB, TRUNCATED or SHORT")
1785 CALL timestop(handle)
1787 END SUBROUTINE compute_m_pq
1794 SUBROUTINE store_aa_basis(bs_env, U_Pp_A)
1796 REAL(kind=
dp),
INTENT(IN) :: u_pp_a(:, :, :)
1798 INTEGER :: first, iatom, nopt, nref
1799 REAL(kind=
dp),
ALLOCATABLE :: u_pp_normalized(:, :)
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)))
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
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)
1817 first = first + nref*nopt
1819 END SUBROUTINE store_aa_basis
1842 SUBROUTINE compute_b_ij_aa_kaveraged(bs_env, iatom, root_rank, Int_3c, M_PQ, &
1843 B_ij, target_norm, X_Pi)
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(:, :)
1851 CHARACTER(LEN=*),
PARAMETER :: routinen =
'compute_B_ij_AA_kaveraged'
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, &
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(:)
1864 CALL timeset(routinen, handle)
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)
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))
1889 b_transition = 0.0_dp
1891 DO ispin = 1, bs_env%n_spin
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')
1901 e_f = 0.5_dp*(vbm + cbm)
1903 DO ikp = 1, bs_env%nkp_scf_desymm
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)
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
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
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)
1935 END SUBROUTINE compute_b_ij_aa_kaveraged
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, &
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(:, :)
1962 COMPLEX(KIND=dp) :: product
1963 INTEGER :: imode, imu, inu
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
1973 d_empty(imu, inu) = d_empty(imu, inu) + weights(imode)*product
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
1983 END SUBROUTINE accumulate_aa_density_matrices
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(:, :)
1997 COMPLEX(KIND=dp),
ALLOCATABLE :: weighted(:, :)
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)
2007 b_pq = 0.5_dp*(b_pq + transpose(b_pq))
2009 END SUBROUTINE contract_b_pq_density
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
2031 CHARACTER(LEN=*),
PARAMETER :: routinen =
'solve_AA_eigenproblem_periodic'
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
2040 CALL timeset(routinen, handle)
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)
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")
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))
2065 eigenval(i) = eigenval_b(m_pq_rank - i + 1)
2067 eigenval(nret + 1:) = 0.0_dp
2069 j = m_pq_rank - i + 1
2070 u_pp_a(:, i) = matmul(x_pi, u_ij(:, j))
2073 relative_fitting_defect = 1.0_dp
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)))
2079 IF (abs(relative_fitting_defect) < conditioned_negative_eigenval_tolerance)
THEN
2080 relative_fitting_defect = 0.0_dp
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))
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")
2099 u_pp_a_rotated(:, :) = matmul(u_pp_a, m_pp_check)
2101 u_pp_a_rotated(:, i) = u_pp_a_rotated(:, i)/sqrt(eigenval_m(i))
2103 u_pp_a(:, :) = matmul(u_pp_a_rotated, transpose(m_pp_check))
2106 m_pp_check(:, :) = matmul(transpose(u_pp_a), &
2107 matmul(0.5_dp*(m_pq + transpose(m_pq)), u_pp_a))
2109 m_pp_check(i, i) = m_pp_check(i, i) - 1.0_dp
2111 metric_defect = maxval(abs(m_pp_check))
2112 IF (metric_defect < eps_metric_defect)
EXIT
2113 IF (iortho == 5)
EXIT
2115 m_pp_check(i, i) = m_pp_check(i, i) + 1.0_dp
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")
2121 u_pp_a_rotated(:, :) = matmul(u_pp_a, m_pp_check)
2123 u_pp_a_rotated(:, i) = u_pp_a_rotated(:, i)/sqrt(eigenval_m(i))
2125 u_pp_a(:, :) = matmul(u_pp_a_rotated, transpose(m_pp_check))
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))
2134 CALL timestop(handle)
2136 END SUBROUTINE solve_aa_eigenproblem_periodic
2149 SUBROUTINE select_ri_sizes_periodic(eigenval_by_block, RI_size_requested, RI_size_minimum, &
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(:)
2155 CHARACTER(LEN=*),
PARAMETER :: routinen =
'select_RI_sizes_periodic'
2157 INTEGER :: handle, i, iblock, ifirst, ilast, lower, &
2158 middle, n, nmode, npositive, &
2160 INTEGER,
ALLOCATABLE :: capacity(:), indices(:)
2161 REAL(kind=
dp) :: tolerance
2162 REAL(kind=
dp),
ALLOCATABLE :: values(:)
2164 CALL timeset(routinen, handle)
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
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
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)
2185 DO WHILE (remaining > 0)
2187 DO WHILE (ilast < npositive)
2188 IF (values(ilast + 1) - values(ifirst) > tolerance)
EXIT
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
2196 remaining = remaining - (ilast - ifirst + 1)
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
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
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
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
2227 cpassert(remaining == 0)
2232 CALL timestop(handle)
2234 END SUBROUTINE select_ri_sizes_periodic
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
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...
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
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.
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.
integer, parameter, public dp
integer, parameter, public local_gemm_pu_host
Machine interface based on Fortran 2003 and POSIX.
real(kind=dp) function, public m_walltime()
returns time from a real-time clock, protected against rolling early/easily
Definition of mathematical constants and functions.
complex(kind=dp), parameter, public z_zero
Collection of simple mathematical functions and subroutines.
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...
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
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.
represent a blacs multidimensional parallel environment (for the mpi corrispective see cp_paratypes/m...
keeps the information about the structure of a full matrix
stores all the informations relevant to an mpi environment