(git:27c966b)
Loading...
Searching...
No Matches
gw_utils_fm.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 Full-matrix operations not provided by the CP2K FM packages.
10!> \par History
11!> 09.2026 created Jan Wilhelm
12! **************************************************************************************************
14 USE cp_cfm_types, ONLY: cp_cfm_create,&
21 USE cp_fm_diag, ONLY: cp_fm_power
23 USE cp_fm_types, ONLY: cp_fm_create,&
28 USE kinds, ONLY: dp
29 USE mathconstants, ONLY: z_one,&
30 z_zero
31 USE mathlib, ONLY: diag_complex,&
34#include "./base/base_uses.f90"
35
36 IMPLICIT NONE
37 PRIVATE
38
39 CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'gw_utils_fm'
40
43
45 MODULE PROCEDURE fm_contract_aba, fm_contract_aba_set
46 END INTERFACE fm_contract_aba
47
48CONTAINS
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 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 cpassert(ASSOCIATED(matrix_a%matrix_struct))
68 cpassert(ASSOCIATED(matrix_b%matrix_struct))
69 CALL cp_cfm_get_info(matrix_a, nrow_global=nrow, ncol_global=ncol)
70 cpassert(nrow == ncol)
71 IF (PRESENT(workspace)) THEN
72 compatible_workspace = cp_fm_struct_equivalent(matrix_a%matrix_struct, workspace%matrix_struct)
73 compatible_workspace = compatible_workspace .AND. &
74 all(matrix_a%matrix_struct%descriptor == workspace%matrix_struct%descriptor)
75 cpassert(compatible_workspace)
76 cpassert(ASSOCIATED(workspace%local_data))
77 cpassert(.NOT. ASSOCIATED(workspace%local_data, matrix_a%local_data))
78 cpassert(.NOT. ASSOCIATED(workspace%local_data, matrix_b%local_data))
79 IF (PRESENT(matrix_c)) THEN
80 cpassert(.NOT. ASSOCIATED(workspace%local_data, matrix_c%local_data))
81 END IF
82 work = workspace
83 ELSE
84 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 z_zero, work)
89 IF (PRESENT(matrix_c)) THEN
90 cpassert(ASSOCIATED(matrix_c%matrix_struct))
91 CALL parallel_gemm("C", "N", nrow, nrow, nrow, z_one, matrix_a, work, &
92 z_zero, matrix_c)
93 ELSE
94 CALL parallel_gemm("C", "N", nrow, nrow, nrow, z_one, matrix_a, work, &
95 z_zero, matrix_b)
96 END IF
97
98 IF (.NOT. PRESENT(workspace)) CALL cp_cfm_release(work)
99
100 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 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 n = SIZE(matrix, 1)
114 cpassert(n == SIZE(matrix, 2))
115
116 DO i = 1, n
117 matrix(i, i) = matrix(i, i) + alpha
118 END DO
119
120 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 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 COMPLEX(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: eigenvectors
142 INTEGER :: handle, i, n
143 REAL(kind=dp) :: eigenvalue_cutoff, sqrt_power_eval
144 REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: eigenvalues
145
146 CALL timeset(routinen, handle)
147
148 n = SIZE(matrix, 1)
149 cpassert(n == SIZE(matrix, 2))
150 matrix(:, :) = 0.5_dp*(matrix(:, :) + conjg(transpose(matrix(:, :))))
151
152 ALLOCATE (eigenvalues(n), eigenvectors(n, n))
153 CALL diag_complex(matrix, eigenvectors, eigenvalues)
154
155 IF (PRESENT(cond_nr)) cond_nr = maxval(abs(eigenvalues))/minval(abs(eigenvalues))
156 IF (PRESENT(min_ev)) min_ev = minval(abs(eigenvalues))
157 IF (PRESENT(max_ev)) max_ev = maxval(abs(eigenvalues))
158 eigenvalue_cutoff = eps
159 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 DO i = 1, n
164 IF (eigenvalue_cutoff < eigenvalues(i)) THEN
165 sqrt_power_eval = eigenvalues(i)**(0.5_dp*exponent)
166 ELSE
167 sqrt_power_eval = 0.0_dp
168 END IF
169 eigenvectors(:, i) = eigenvectors(:, i)*sqrt_power_eval
170 END DO
171
172 ! X*X^H = U*diag(eigenvalues**exponent)*U^H.
173 CALL gemm_square(eigenvectors, 'N', eigenvectors, 'C', matrix)
174
175 DEALLOCATE (eigenvalues, eigenvectors)
176
177 CALL timestop(handle)
178
179 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 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 threshold = 0.0_dp
200 IF (PRESENT(eigenvalue_threshold)) threshold = eigenvalue_threshold
201 CALL cp_fm_create(work, matrix_a%matrix_struct)
202
203 ! Preserve A because a failed Cholesky decomposition overwrites it.
204 CALL cp_fm_to_fm(matrix_a, work)
205 CALL cp_fm_cholesky_decompose(matrix_a, info_out=info)
206 cholesky_successful = info == 0
207 n_dependent = 0
208 IF (cholesky_successful) THEN
209 CALL cp_fm_cholesky_invert(matrix_a)
210 CALL cp_fm_uplo_to_full(matrix_a, work)
211 ELSE
212 CALL cp_fm_to_fm(work, matrix_a)
213 CALL cp_fm_power(matrix_a, work, -1.0_dp, threshold, n_dependent)
214 IF (PRESENT(unit_nr)) THEN
215 IF (unit_nr > 0) THEN
216 WRITE (unit_nr, '(T2,A)') &
217 'Cholesky decomposition failed; matrix inverted by diagonalization.'
218 WRITE (unit_nr, '(T2,A,T72,I9)') 'Discarded eigenmodes:', n_dependent
219 END IF
220 END IF
221 END IF
222 CALL cp_fm_release(work)
223
224 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 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 INTEGER, DIMENSION(:), POINTER :: col_indices, row_indices
244 LOGICAL :: cholesky_successful
245 REAL(kind=dp) :: threshold
246 TYPE(cp_fm_type) :: work
247
248 threshold = 0.0_dp
249 IF (PRESENT(eigenvalue_threshold)) threshold = eigenvalue_threshold
250 CALL cp_fm_create(work, matrix_a%matrix_struct)
251
252 CALL cp_fm_to_fm(matrix_a, matrix_b)
253 CALL cp_fm_cholesky_decompose(matrix_b, info_out=info)
254 cholesky_successful = info == 0
255 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 row_indices=row_indices, col_indices=col_indices)
259 DO j_col = 1, ncol_local
260 DO i_row = 1, nrow_local
261 IF (row_indices(i_row) > col_indices(j_col)) THEN
262 matrix_b%local_data(i_row, j_col) = 0.0_dp
263 END IF
264 END DO
265 END DO
266 ELSE
267 CALL cp_fm_to_fm(matrix_a, matrix_b)
268 CALL cp_fm_power(matrix_b, work, 0.5_dp, threshold, n_dependent)
269 IF (PRESENT(unit_nr)) THEN
270 IF (unit_nr > 0) THEN
271 WRITE (unit_nr, '(T2,A)') &
272 'Cholesky decomposition failed, using diagonalization.'
273 WRITE (unit_nr, '(T2,A,T72,I9)') 'Discarded eigenmodes:', n_dependent
274 END IF
275 END IF
276 END IF
277 CALL cp_fm_release(work)
278
279 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 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 cpassert(ASSOCIATED(matrix_a%matrix_struct))
296 cpassert(ASSOCIATED(matrix_b%matrix_struct))
297 CALL cp_fm_get_info(matrix_a, nrow_global=nrow, ncol_global=ncol)
298 cpassert(nrow == ncol)
299 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 0.0_dp, work)
303 IF (PRESENT(matrix_c)) THEN
304 cpassert(ASSOCIATED(matrix_c%matrix_struct))
305 CALL parallel_gemm("T", "N", nrow, nrow, nrow, 1.0_dp, matrix_a, work, &
306 0.0_dp, matrix_c)
307 ELSE
308 CALL parallel_gemm("T", "N", nrow, nrow, nrow, 1.0_dp, matrix_a, work, &
309 0.0_dp, matrix_b)
310 END IF
311
312 CALL cp_fm_release(work)
313
314 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 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 cpassert(ASSOCIATED(matrix_a%matrix_struct))
332 CALL cp_fm_get_info(matrix_a, nrow_global=nrow, ncol_global=ncol)
333 cpassert(nrow == ncol)
334 IF (PRESENT(matrix_c)) THEN
335 cpassert(SIZE(matrix_c) == SIZE(matrix_b))
336 END IF
337 CALL cp_fm_create(work, matrix_a%matrix_struct)
338
339 DO i = 1, SIZE(matrix_b)
340 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 0.0_dp, work)
343 IF (PRESENT(matrix_c)) THEN
344 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_dp, matrix_c(i))
347 ELSE
348 CALL parallel_gemm("T", "N", nrow, nrow, nrow, 1.0_dp, matrix_a, work, &
349 0.0_dp, matrix_b(i))
350 END IF
351 END DO
352
353 CALL cp_fm_release(work)
354
355 END SUBROUTINE fm_contract_aba_set
356
357END MODULE gw_utils_fm
Represents a complex full matrix distributed on many processors.
subroutine, public cp_cfm_release(matrix)
Releases a full matrix.
subroutine, public cp_cfm_create(matrix, matrix_struct, name, nrow, ncol, set_zero)
Creates a new full matrix with the given structure.
subroutine, public cp_cfm_get_info(matrix, name, nrow_global, ncol_global, nrow_block, ncol_block, nrow_local, ncol_local, row_indices, col_indices, local_data, context, matrix_struct, para_env)
Returns information about a full matrix.
Basic linear algebra operations for full matrices.
subroutine, public cp_fm_uplo_to_full(matrix, work, uplo)
given a triangular matrix according to uplo, computes the corresponding full matrix
various cholesky decomposition related routines
subroutine, public cp_fm_cholesky_invert(matrix, n, info_out, work)
Replace the upper Cholesky factor by the upper triangle of the inverse. When work is provided,...
subroutine, public cp_fm_cholesky_decompose(matrix, n, info_out)
used to replace a symmetric positive def. matrix M with its cholesky decomposition U: M = U^T * U,...
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_power(matrix, work, exponent, threshold, n_dependent, verbose, eigvals)
...
represent the structure of a full matrix
logical function, public cp_fm_struct_equivalent(fmstruct1, fmstruct2)
returns true if the two matrix structures are equivalent, false otherwise.
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
Full-matrix operations not provided by the CP2K FM packages.
Definition gw_utils_fm.F:13
subroutine, public local_add_on_diag(matrix, alpha)
Add a scalar to the diagonal of a local complex matrix.
subroutine, public fm_invert(matrix_a, eigenvalue_threshold, unit_nr)
Inverts a symmetric matrix. First, Cholesky decomposition is tried. If it fails, the matrix is diagon...
subroutine, public fm_sqrt(matrix_a, matrix_b, eigenvalue_threshold, unit_nr)
For input A, computes B such that B^T B=A. First, Cholesky decomposition is tried....
subroutine, public cfm_contract_aba(matrix_a, matrix_b, matrix_c, workspace)
Computes A^H B A for complex full matrices.
Definition gw_utils_fm.F:59
subroutine, public local_complex_power(matrix, exponent, eps, cond_nr, min_ev, max_ev, relative_eps)
Compute a spectral power of a local complex Hermitian matrix. Eigenvalues not larger than eps (or rel...
Defines the basic variable types.
Definition kinds.F:23
integer, parameter, public dp
Definition kinds.F:34
Definition of mathematical constants and functions.
complex(kind=dp), parameter, public z_one
complex(kind=dp), parameter, public z_zero
Collection of simple mathematical functions and subroutines.
Definition mathlib.F:15
subroutine, public diag_complex(matrix, eigenvectors, eigenvalues)
Diagonalizes a local complex Hermitian matrix using LAPACK. Based on cp_cfm_heevd.
Definition mathlib.F:1909
basic linear algebra operations for full matrixes
Represent a complex full matrix.
represent a full matrix