LCOV - code coverage report
Current view: top level - src - gw_utils_fm.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:1be8651) Lines: 84.2 % 120 101
Test Date: 2026-11-24 01:22:08 Functions: 100.0 % 7 7

            Line data    Source code
       1              : !--------------------------------------------------------------------------------------------------!
       2              : !   CP2K: A general program to perform molecular dynamics simulations                              !
       3              : !   Copyright 2000-2026 CP2K developers group <https://cp2k.org>                                   !
       4              : !                                                                                                  !
       5              : !   SPDX-License-Identifier: GPL-2.0-or-later                                                      !
       6              : !--------------------------------------------------------------------------------------------------!
       7              : 
       8              : ! **************************************************************************************************
       9              : !> \brief Full-matrix operations not provided by the CP2K FM packages.
      10              : !> \par History
      11              : !>      09.2026 created Jan Wilhelm
      12              : ! **************************************************************************************************
      13              : MODULE gw_utils_fm
      14              :    USE cp_cfm_types,                    ONLY: cp_cfm_create,&
      15              :                                               cp_cfm_get_info,&
      16              :                                               cp_cfm_release,&
      17              :                                               cp_cfm_type
      18              :    USE cp_fm_basic_linalg,              ONLY: cp_fm_uplo_to_full
      19              :    USE cp_fm_cholesky,                  ONLY: cp_fm_cholesky_decompose,&
      20              :                                               cp_fm_cholesky_invert
      21              :    USE cp_fm_diag,                      ONLY: cp_fm_power
      22              :    USE cp_fm_struct,                    ONLY: cp_fm_struct_equivalent
      23              :    USE cp_fm_types,                     ONLY: cp_fm_create,&
      24              :                                               cp_fm_get_info,&
      25              :                                               cp_fm_release,&
      26              :                                               cp_fm_to_fm,&
      27              :                                               cp_fm_type
      28              :    USE kinds,                           ONLY: dp
      29              :    USE mathconstants,                   ONLY: z_one,&
      30              :                                               z_zero
      31              :    USE mathlib,                         ONLY: diag_complex,&
      32              :                                               gemm_square
      33              :    USE parallel_gemm_api,               ONLY: parallel_gemm
      34              : #include "./base/base_uses.f90"
      35              : 
      36              :    IMPLICIT NONE
      37              :    PRIVATE
      38              : 
      39              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'gw_utils_fm'
      40              : 
      41              :    PUBLIC :: cfm_contract_ABA, fm_contract_ABA, fm_invert, fm_sqrt, &
      42              :              local_add_on_diag, local_complex_power
      43              : 
      44              :    INTERFACE fm_contract_ABA
      45              :       MODULE PROCEDURE fm_contract_ABA, fm_contract_ABA_set
      46              :    END INTERFACE fm_contract_ABA
      47              : 
      48              : CONTAINS
      49              : 
      50              : ! **************************************************************************************************
      51              : !> \brief Computes A^H B A for complex full matrices.
      52              : !> \param matrix_A left and right matrix A; H denotes its Hermitian transpose
      53              : !> \param matrix_B input matrix B; overwritten by A^H B A if matrix_C is absent
      54              : !> \param matrix_C optional output matrix C=A^H B A
      55              : !> \param workspace optional reusable scratch matrix with the same layout as A;
      56              : !>        its contents are overwritten and it must not alias the input or output matrices
      57              : ! **************************************************************************************************
      58        47672 :    SUBROUTINE cfm_contract_ABA(matrix_A, matrix_B, matrix_C, workspace)
      59              :       TYPE(cp_cfm_type), INTENT(IN)                      :: matrix_A
      60              :       TYPE(cp_cfm_type), INTENT(INOUT)                   :: matrix_B
      61              :       TYPE(cp_cfm_type), INTENT(INOUT), OPTIONAL         :: matrix_C, workspace
      62              : 
      63              :       INTEGER                                            :: ncol, nrow
      64              :       LOGICAL                                            :: compatible_workspace
      65              :       TYPE(cp_cfm_type)                                  :: work
      66              : 
      67            0 :       CPASSERT(ASSOCIATED(matrix_A%matrix_struct))
      68        23836 :       CPASSERT(ASSOCIATED(matrix_B%matrix_struct))
      69        23836 :       CALL cp_cfm_get_info(matrix_A, nrow_global=nrow, ncol_global=ncol)
      70        23836 :       CPASSERT(nrow == ncol)
      71        23836 :       IF (PRESENT(workspace)) THEN
      72         5546 :          compatible_workspace = cp_fm_struct_equivalent(matrix_A%matrix_struct, workspace%matrix_struct)
      73              :          compatible_workspace = compatible_workspace .AND. &
      74        55460 :                                 ALL(matrix_A%matrix_struct%descriptor == workspace%matrix_struct%descriptor)
      75         5546 :          CPASSERT(compatible_workspace)
      76         5546 :          CPASSERT(ASSOCIATED(workspace%local_data))
      77         5546 :          CPASSERT(.NOT. ASSOCIATED(workspace%local_data, matrix_A%local_data))
      78         5546 :          CPASSERT(.NOT. ASSOCIATED(workspace%local_data, matrix_B%local_data))
      79         5546 :          IF (PRESENT(matrix_C)) THEN
      80            0 :             CPASSERT(.NOT. ASSOCIATED(workspace%local_data, matrix_C%local_data))
      81              :          END IF
      82         5546 :          work = workspace
      83              :       ELSE
      84        18290 :          CALL cp_cfm_create(work, matrix_A%matrix_struct)
      85              :       END IF
      86              : 
      87              :       CALL parallel_gemm("N", "N", nrow, nrow, nrow, z_one, matrix_B, matrix_A, &
      88        23836 :                          z_zero, work)
      89        23836 :       IF (PRESENT(matrix_C)) THEN
      90         2496 :          CPASSERT(ASSOCIATED(matrix_C%matrix_struct))
      91              :          CALL parallel_gemm("C", "N", nrow, nrow, nrow, z_one, matrix_A, work, &
      92         2496 :                             z_zero, matrix_C)
      93              :       ELSE
      94              :          CALL parallel_gemm("C", "N", nrow, nrow, nrow, z_one, matrix_A, work, &
      95        21340 :                             z_zero, matrix_B)
      96              :       END IF
      97              : 
      98        23836 :       IF (.NOT. PRESENT(workspace)) CALL cp_cfm_release(work)
      99              : 
     100        23836 :    END SUBROUTINE cfm_contract_ABA
     101              : 
     102              : ! **************************************************************************************************
     103              : !> \brief Add a scalar to the diagonal of a local complex matrix.
     104              : !> \param matrix input/output matrix
     105              : !> \param alpha scalar added to each diagonal element
     106              : ! **************************************************************************************************
     107        66560 :    SUBROUTINE local_add_on_diag(matrix, alpha)
     108              :       COMPLEX(KIND=dp), DIMENSION(:, :), INTENT(INOUT)   :: matrix
     109              :       COMPLEX(KIND=dp), INTENT(IN)                       :: alpha
     110              : 
     111              :       INTEGER                                            :: i, n
     112              : 
     113        66560 :       n = SIZE(matrix, 1)
     114        66560 :       CPASSERT(n == SIZE(matrix, 2))
     115              : 
     116       453120 :       DO i = 1, n
     117       453120 :          matrix(i, i) = matrix(i, i) + alpha
     118              :       END DO
     119              : 
     120        66560 :    END SUBROUTINE local_add_on_diag
     121              : 
     122              : ! **************************************************************************************************
     123              : !> \brief Compute a spectral power of a local complex Hermitian matrix.
     124              : !>        Eigenvalues not larger than eps (or relative_eps times the largest one) are discarded.
     125              : !> \param matrix input/output matrix
     126              : !> \param exponent matrix exponent
     127              : !> \param eps eigenvalue threshold
     128              : !> \param cond_nr optional condition number
     129              : !> \param min_ev optional minimum absolute eigenvalue
     130              : !> \param max_ev optional maximum absolute eigenvalue
     131              : !> \param relative_eps optional threshold relative to the largest eigenvalue
     132              : ! **************************************************************************************************
     133        83038 :    SUBROUTINE local_complex_power(matrix, exponent, eps, cond_nr, min_ev, max_ev, relative_eps)
     134              :       COMPLEX(KIND=dp), DIMENSION(:, :), INTENT(INOUT)   :: matrix
     135              :       REAL(KIND=dp), INTENT(IN)                          :: exponent, eps
     136              :       REAL(KIND=dp), INTENT(OUT), OPTIONAL               :: cond_nr, min_ev, max_ev
     137              :       REAL(KIND=dp), INTENT(IN), OPTIONAL                :: relative_eps
     138              : 
     139              :       CHARACTER(len=*), PARAMETER :: routineN = 'local_complex_power'
     140              : 
     141        83038 :       COMPLEX(KIND=dp), ALLOCATABLE, DIMENSION(:, :)     :: eigenvectors
     142              :       INTEGER                                            :: handle, i, n
     143              :       REAL(KIND=dp)                                      :: eigenvalue_cutoff, sqrt_power_eval
     144        83038 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: eigenvalues
     145              : 
     146        83038 :       CALL timeset(routineN, handle)
     147              : 
     148        83038 :       n = SIZE(matrix, 1)
     149        83038 :       CPASSERT(n == SIZE(matrix, 2))
     150      6723230 :       matrix(:, :) = 0.5_dp*(matrix(:, :) + CONJG(TRANSPOSE(matrix(:, :))))
     151              : 
     152       498228 :       ALLOCATE (eigenvalues(n), eigenvectors(n, n))
     153        83038 :       CALL diag_complex(matrix, eigenvectors, eigenvalues)
     154              : 
     155       143198 :       IF (PRESENT(cond_nr)) cond_nr = MAXVAL(ABS(eigenvalues))/MINVAL(ABS(eigenvalues))
     156       113118 :       IF (PRESENT(min_ev)) min_ev = MINVAL(ABS(eigenvalues))
     157       113118 :       IF (PRESENT(max_ev)) max_ev = MAXVAL(ABS(eigenvalues))
     158        83038 :       eigenvalue_cutoff = eps
     159        83836 :       IF (PRESENT(relative_eps)) eigenvalue_cutoff = MAX(eps, relative_eps*MAXVAL(eigenvalues))
     160              : 
     161              :       ! Reconstruct matrix**exponent as X*X^H, where
     162              :       ! X = U*diag(eigenvalues**(exponent/2)).
     163       566878 :       DO i = 1, n
     164       483840 :          IF (eigenvalue_cutoff < eigenvalues(i)) THEN
     165       483585 :             sqrt_power_eval = eigenvalues(i)**(0.5_dp*exponent)
     166              :          ELSE
     167              :             sqrt_power_eval = 0.0_dp
     168              :          END IF
     169      3403134 :          eigenvectors(:, i) = eigenvectors(:, i)*sqrt_power_eval
     170              :       END DO
     171              : 
     172              :       ! X*X^H = U*diag(eigenvalues**exponent)*U^H.
     173        83038 :       CALL gemm_square(eigenvectors, 'N', eigenvectors, 'C', matrix)
     174              : 
     175        83038 :       DEALLOCATE (eigenvalues, eigenvectors)
     176              : 
     177        83038 :       CALL timestop(handle)
     178              : 
     179        83038 :    END SUBROUTINE local_complex_power
     180              : 
     181              : ! **************************************************************************************************
     182              : !> \brief Inverts a symmetric matrix. First, Cholesky decomposition is tried.
     183              : !>        If it fails, the matrix is diagonalized and inverted by taking
     184              : !>        inverse eigenvalues.
     185              : !> \param matrix_A input A; output inverse or filtered pseudoinverse
     186              : !> \param eigenvalue_threshold optional relative eigenvalue threshold
     187              : !> \param unit_nr optional output unit for a failed Cholesky decomposition
     188              : ! **************************************************************************************************
     189          366 :    SUBROUTINE fm_invert(matrix_A, eigenvalue_threshold, unit_nr)
     190              :       TYPE(cp_fm_type), INTENT(INOUT)                    :: matrix_A
     191              :       REAL(KIND=dp), INTENT(IN), OPTIONAL                :: eigenvalue_threshold
     192              :       INTEGER, INTENT(IN), OPTIONAL                      :: unit_nr
     193              : 
     194              :       INTEGER                                            :: info, n_dependent
     195              :       LOGICAL                                            :: cholesky_successful
     196              :       REAL(KIND=dp)                                      :: threshold
     197              :       TYPE(cp_fm_type)                                   :: work
     198              : 
     199          122 :       threshold = 0.0_dp
     200          122 :       IF (PRESENT(eigenvalue_threshold)) threshold = eigenvalue_threshold
     201          122 :       CALL cp_fm_create(work, matrix_A%matrix_struct)
     202              : 
     203              :       ! Preserve A because a failed Cholesky decomposition overwrites it.
     204          122 :       CALL cp_fm_to_fm(matrix_A, work)
     205          122 :       CALL cp_fm_cholesky_decompose(matrix_A, info_out=info)
     206          122 :       cholesky_successful = info == 0
     207              :       n_dependent = 0
     208          122 :       IF (cholesky_successful) THEN
     209          122 :          CALL cp_fm_cholesky_invert(matrix_A)
     210          122 :          CALL cp_fm_uplo_to_full(matrix_A, work)
     211              :       ELSE
     212            0 :          CALL cp_fm_to_fm(work, matrix_A)
     213            0 :          CALL cp_fm_power(matrix_A, work, -1.0_dp, threshold, n_dependent)
     214            0 :          IF (PRESENT(unit_nr)) THEN
     215            0 :             IF (unit_nr > 0) THEN
     216              :                WRITE (unit_nr, '(T2,A)') &
     217            0 :                   'Cholesky decomposition failed; matrix inverted by diagonalization.'
     218            0 :                WRITE (unit_nr, '(T2,A,T72,I9)') 'Discarded eigenmodes:', n_dependent
     219              :             END IF
     220              :          END IF
     221              :       END IF
     222          122 :       CALL cp_fm_release(work)
     223              : 
     224          122 :    END SUBROUTINE fm_invert
     225              : 
     226              : ! **************************************************************************************************
     227              : !> \brief For input A, computes B such that B^T B=A.
     228              : !>        First, Cholesky decomposition is tried. If it fails, B is computed
     229              : !>        by diagonalizing A.
     230              : !> \param matrix_A symmetric input matrix A, retained unchanged
     231              : !> \param matrix_B output matrix B
     232              : !> \param eigenvalue_threshold optional relative eigenvalue threshold
     233              : !> \param unit_nr optional output unit for a failed Cholesky decomposition
     234              : ! **************************************************************************************************
     235          126 :    SUBROUTINE fm_sqrt(matrix_A, matrix_B, eigenvalue_threshold, unit_nr)
     236              :       TYPE(cp_fm_type), INTENT(IN)                       :: matrix_A
     237              :       TYPE(cp_fm_type), INTENT(INOUT)                    :: matrix_B
     238              :       REAL(KIND=dp), INTENT(IN), OPTIONAL                :: eigenvalue_threshold
     239              :       INTEGER, INTENT(IN), OPTIONAL                      :: unit_nr
     240              : 
     241              :       INTEGER                                            :: i_row, info, j_col, n_dependent, &
     242              :                                                             ncol_local, nrow_local
     243           42 :       INTEGER, DIMENSION(:), POINTER                     :: col_indices, row_indices
     244              :       LOGICAL                                            :: cholesky_successful
     245              :       REAL(KIND=dp)                                      :: threshold
     246              :       TYPE(cp_fm_type)                                   :: work
     247              : 
     248           42 :       threshold = 0.0_dp
     249           42 :       IF (PRESENT(eigenvalue_threshold)) threshold = eigenvalue_threshold
     250           42 :       CALL cp_fm_create(work, matrix_A%matrix_struct)
     251              : 
     252           42 :       CALL cp_fm_to_fm(matrix_A, matrix_B)
     253           42 :       CALL cp_fm_cholesky_decompose(matrix_B, info_out=info)
     254           42 :       cholesky_successful = info == 0
     255           42 :       IF (cholesky_successful) THEN
     256              :          n_dependent = 0
     257              :          CALL cp_fm_get_info(matrix=matrix_B, nrow_local=nrow_local, ncol_local=ncol_local, &
     258           42 :                              row_indices=row_indices, col_indices=col_indices)
     259         1244 :          DO j_col = 1, ncol_local
     260        21751 :             DO i_row = 1, nrow_local
     261        21709 :                IF (row_indices(i_row) > col_indices(j_col)) THEN
     262         9953 :                   matrix_B%local_data(i_row, j_col) = 0.0_dp
     263              :                END IF
     264              :             END DO
     265              :          END DO
     266              :       ELSE
     267            0 :          CALL cp_fm_to_fm(matrix_A, matrix_B)
     268            0 :          CALL cp_fm_power(matrix_B, work, 0.5_dp, threshold, n_dependent)
     269            0 :          IF (PRESENT(unit_nr)) THEN
     270            0 :             IF (unit_nr > 0) THEN
     271              :                WRITE (unit_nr, '(T2,A)') &
     272            0 :                   'Cholesky decomposition failed, using diagonalization.'
     273            0 :                WRITE (unit_nr, '(T2,A,T72,I9)') 'Discarded eigenmodes:', n_dependent
     274              :             END IF
     275              :          END IF
     276              :       END IF
     277           42 :       CALL cp_fm_release(work)
     278              : 
     279           42 :    END SUBROUTINE fm_sqrt
     280              : 
     281              : ! **************************************************************************************************
     282              : !> \brief Computes A^T B A.
     283              : !> \param matrix_A left and right matrix A
     284              : !> \param matrix_B input matrix B; overwritten by A^T B A if matrix_C is absent
     285              : !> \param matrix_C optional output matrix C=A^T B A
     286              : ! **************************************************************************************************
     287         7212 :    SUBROUTINE fm_contract_ABA(matrix_A, matrix_B, matrix_C)
     288              :       TYPE(cp_fm_type), INTENT(IN)                       :: matrix_A
     289              :       TYPE(cp_fm_type), INTENT(INOUT)                    :: matrix_B
     290              :       TYPE(cp_fm_type), INTENT(INOUT), OPTIONAL          :: matrix_C
     291              : 
     292              :       INTEGER                                            :: ncol, nrow
     293              :       TYPE(cp_fm_type)                                   :: work
     294              : 
     295            0 :       CPASSERT(ASSOCIATED(matrix_A%matrix_struct))
     296         2404 :       CPASSERT(ASSOCIATED(matrix_B%matrix_struct))
     297         2404 :       CALL cp_fm_get_info(matrix_A, nrow_global=nrow, ncol_global=ncol)
     298         2404 :       CPASSERT(nrow == ncol)
     299         2404 :       CALL cp_fm_create(work, matrix_A%matrix_struct)
     300              : 
     301              :       CALL parallel_gemm("N", "N", nrow, nrow, nrow, 1.0_dp, matrix_B, matrix_A, &
     302         2404 :                          0.0_dp, work)
     303         2404 :       IF (PRESENT(matrix_C)) THEN
     304         2390 :          CPASSERT(ASSOCIATED(matrix_C%matrix_struct))
     305              :          CALL parallel_gemm("T", "N", nrow, nrow, nrow, 1.0_dp, matrix_A, work, &
     306         2390 :                             0.0_dp, matrix_C)
     307              :       ELSE
     308              :          CALL parallel_gemm("T", "N", nrow, nrow, nrow, 1.0_dp, matrix_A, work, &
     309           14 :                             0.0_dp, matrix_B)
     310              :       END IF
     311              : 
     312         2404 :       CALL cp_fm_release(work)
     313              : 
     314         2404 :    END SUBROUTINE fm_contract_ABA
     315              : 
     316              : ! **************************************************************************************************
     317              : !> \brief Computes A^T B_i A for a set of matrices.
     318              : !> \param matrix_A left and right matrix A
     319              : !> \param matrix_B input matrices B_i; overwritten if matrix_C is absent
     320              : !> \param matrix_C optional output matrices C_i=A^T B_i A
     321              : ! **************************************************************************************************
     322          240 :    SUBROUTINE fm_contract_ABA_set(matrix_A, matrix_B, matrix_C)
     323              :       TYPE(cp_fm_type), INTENT(IN)                       :: matrix_A
     324              :       TYPE(cp_fm_type), DIMENSION(:), INTENT(INOUT)      :: matrix_B
     325              :       TYPE(cp_fm_type), DIMENSION(:), INTENT(INOUT), &
     326              :          OPTIONAL                                        :: matrix_C
     327              : 
     328              :       INTEGER                                            :: i, ncol, nrow
     329              :       TYPE(cp_fm_type)                                   :: work
     330              : 
     331            0 :       CPASSERT(ASSOCIATED(matrix_A%matrix_struct))
     332          240 :       CALL cp_fm_get_info(matrix_A, nrow_global=nrow, ncol_global=ncol)
     333          240 :       CPASSERT(nrow == ncol)
     334          240 :       IF (PRESENT(matrix_C)) THEN
     335            0 :          CPASSERT(SIZE(matrix_C) == SIZE(matrix_B))
     336              :       END IF
     337          240 :       CALL cp_fm_create(work, matrix_A%matrix_struct)
     338              : 
     339         1788 :       DO i = 1, SIZE(matrix_B)
     340         1548 :          CPASSERT(ASSOCIATED(matrix_B(i)%matrix_struct))
     341              :          CALL parallel_gemm("N", "N", nrow, nrow, nrow, 1.0_dp, matrix_B(i), matrix_A, &
     342         1548 :                             0.0_dp, work)
     343         1788 :          IF (PRESENT(matrix_C)) THEN
     344            0 :             CPASSERT(ASSOCIATED(matrix_C(i)%matrix_struct))
     345              :             CALL parallel_gemm("T", "N", nrow, nrow, nrow, 1.0_dp, matrix_A, work, &
     346            0 :                                0.0_dp, matrix_C(i))
     347              :          ELSE
     348              :             CALL parallel_gemm("T", "N", nrow, nrow, nrow, 1.0_dp, matrix_A, work, &
     349         1548 :                                0.0_dp, matrix_B(i))
     350              :          END IF
     351              :       END DO
     352              : 
     353          240 :       CALL cp_fm_release(work)
     354              : 
     355          240 :    END SUBROUTINE fm_contract_ABA_set
     356              : 
     357              : END MODULE gw_utils_fm
        

Generated by: LCOV version 2.0-1