34#include "./base/base_uses.f90"
39 CHARACTER(len=*),
PARAMETER,
PRIVATE :: moduleN =
'gw_utils_fm'
61 TYPE(
cp_cfm_type),
INTENT(INOUT),
OPTIONAL :: matrix_c, workspace
64 LOGICAL :: compatible_workspace
67 cpassert(
ASSOCIATED(matrix_a%matrix_struct))
68 cpassert(
ASSOCIATED(matrix_b%matrix_struct))
70 cpassert(nrow == ncol)
71 IF (
PRESENT(workspace))
THEN
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))
89 IF (
PRESENT(matrix_c))
THEN
90 cpassert(
ASSOCIATED(matrix_c%matrix_struct))
108 COMPLEX(KIND=dp),
DIMENSION(:, :),
INTENT(INOUT) :: matrix
109 COMPLEX(KIND=dp),
INTENT(IN) :: alpha
114 cpassert(n ==
SIZE(matrix, 2))
117 matrix(i, i) = matrix(i, i) + alpha
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
139 CHARACTER(len=*),
PARAMETER :: routinen =
'local_complex_power'
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
146 CALL timeset(routinen, handle)
149 cpassert(n ==
SIZE(matrix, 2))
150 matrix(:, :) = 0.5_dp*(matrix(:, :) + conjg(transpose(matrix(:, :))))
152 ALLOCATE (eigenvalues(n), eigenvectors(n, n))
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))
164 IF (eigenvalue_cutoff < eigenvalues(i))
THEN
165 sqrt_power_eval = eigenvalues(i)**(0.5_dp*exponent)
167 sqrt_power_eval = 0.0_dp
169 eigenvectors(:, i) = eigenvectors(:, i)*sqrt_power_eval
173 CALL gemm_square(eigenvectors,
'N', eigenvectors,
'C', matrix)
175 DEALLOCATE (eigenvalues, eigenvectors)
177 CALL timestop(handle)
189 SUBROUTINE fm_invert(matrix_A, eigenvalue_threshold, unit_nr)
191 REAL(kind=
dp),
INTENT(IN),
OPTIONAL :: eigenvalue_threshold
192 INTEGER,
INTENT(IN),
OPTIONAL :: unit_nr
194 INTEGER :: info, n_dependent
195 LOGICAL :: cholesky_successful
196 REAL(kind=
dp) :: threshold
200 IF (
PRESENT(eigenvalue_threshold)) threshold = eigenvalue_threshold
206 cholesky_successful = info == 0
208 IF (cholesky_successful)
THEN
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
235 SUBROUTINE fm_sqrt(matrix_A, matrix_B, eigenvalue_threshold, unit_nr)
238 REAL(kind=
dp),
INTENT(IN),
OPTIONAL :: eigenvalue_threshold
239 INTEGER,
INTENT(IN),
OPTIONAL :: unit_nr
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
249 IF (
PRESENT(eigenvalue_threshold)) threshold = eigenvalue_threshold
254 cholesky_successful = info == 0
255 IF (cholesky_successful)
THEN
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
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
290 TYPE(
cp_fm_type),
INTENT(INOUT),
OPTIONAL :: matrix_C
292 INTEGER :: ncol, nrow
295 cpassert(
ASSOCIATED(matrix_a%matrix_struct))
296 cpassert(
ASSOCIATED(matrix_b%matrix_struct))
298 cpassert(nrow == ncol)
301 CALL parallel_gemm(
"N",
"N", nrow, nrow, nrow, 1.0_dp, matrix_b, matrix_a, &
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, &
308 CALL parallel_gemm(
"T",
"N", nrow, nrow, nrow, 1.0_dp, matrix_a, work, &
322 SUBROUTINE fm_contract_aba_set(matrix_A, matrix_B, matrix_C)
324 TYPE(
cp_fm_type),
DIMENSION(:),
INTENT(INOUT) :: matrix_B
325 TYPE(
cp_fm_type),
DIMENSION(:),
INTENT(INOUT), &
328 INTEGER :: i, ncol, nrow
331 cpassert(
ASSOCIATED(matrix_a%matrix_struct))
333 cpassert(nrow == ncol)
334 IF (
PRESENT(matrix_c))
THEN
335 cpassert(
SIZE(matrix_c) ==
SIZE(matrix_b))
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, &
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, &
348 CALL parallel_gemm(
"T",
"N", nrow, nrow, nrow, 1.0_dp, matrix_a, work, &
355 END SUBROUTINE fm_contract_aba_set
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...
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
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.
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.
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.
integer, parameter, public dp
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.
subroutine, public diag_complex(matrix, eigenvectors, eigenvalues)
Diagonalizes a local complex Hermitian matrix using LAPACK. Based on cp_cfm_heevd.
basic linear algebra operations for full matrixes
Represent a complex full matrix.