(git:f2099e5)
Loading...
Searching...
No Matches
gw_utils_dbcsr.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 Common DBCSR matrix operations used by GW modules.
10!> \par History
11!> 09.2026 created Jan Wilhelm
12! **************************************************************************************************
14 USE cp_dbcsr_api, ONLY: &
18 USE kinds, ONLY: dp
19#include "./base/base_uses.f90"
20
21 IMPLICIT NONE
22 PRIVATE
23
25
26 CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'gw_utils_dbcsr'
27
28CONTAINS
29
30! **************************************************************************************************
31!> \brief Computes the scaled element-wise product C = factor (A ◦ B) while preserving the
32!> block structure of A. Blocks absent from B are retained in C with zero values.
33!> \param matrix_A First factor and source of the block structure
34!> \param matrix_B Second factor
35!> \param matrix_C Scaled element-wise product
36!> \param factor Scaling factor
37! **************************************************************************************************
38 SUBROUTINE hadamard_product(matrix_A, matrix_B, matrix_C, factor)
39 TYPE(dbcsr_type), INTENT(INOUT) :: matrix_a, matrix_b, matrix_c
40 REAL(kind=dp), INTENT(IN) :: factor
41
42 CHARACTER(LEN=*), PARAMETER :: routinen = 'hadamard_product'
43
44 INTEGER :: handle, icol, irow
45 LOGICAL :: found
46 REAL(kind=dp), DIMENSION(:, :), POINTER :: block_b, block_c
47 TYPE(dbcsr_iterator_type) :: iterator
48
49 CALL timeset(routinen, handle)
50
51 CALL dbcsr_copy(matrix_c, matrix_a)
52 CALL dbcsr_iterator_start(iterator, matrix_c)
53 DO WHILE (dbcsr_iterator_blocks_left(iterator))
54 CALL dbcsr_iterator_next_block(iterator, irow, icol, block_c)
55 CALL dbcsr_get_block_p(matrix_b, irow, icol, block_b, found)
56 IF (found) THEN
57 block_c(:, :) = factor*block_c(:, :)*block_b(:, :)
58 ELSE
59 block_c(:, :) = 0.0_dp
60 END IF
61 END DO
62 CALL dbcsr_iterator_stop(iterator)
63
64 CALL timestop(handle)
65
66 END SUBROUTINE hadamard_product
67
68! **************************************************************************************************
69!> \brief Form A = factor * (A element-wise B) without changing A's block structure.
70!> \param matrix_A First factor, overwritten by the product.
71!> \param matrix_B Second factor; a missing block represents zero.
72!> \param factor Product scale factor.
73! **************************************************************************************************
74 SUBROUTINE hadamard_product_inplace(matrix_A, matrix_B, factor)
75 TYPE(dbcsr_type), INTENT(INOUT) :: matrix_a, matrix_b
76 REAL(kind=dp), INTENT(IN) :: factor
77
78 CHARACTER(LEN=*), PARAMETER :: routinen = 'hadamard_product_inplace'
79
80 INTEGER :: handle, icol, irow
81 LOGICAL :: found
82 REAL(kind=dp), DIMENSION(:, :), POINTER :: block_a, block_b
83 TYPE(dbcsr_iterator_type) :: iterator
84
85 CALL timeset(routinen, handle)
86
87 CALL dbcsr_iterator_start(iterator, matrix_a)
88 DO WHILE (dbcsr_iterator_blocks_left(iterator))
89 CALL dbcsr_iterator_next_block(iterator, irow, icol, block_a)
90 CALL dbcsr_get_block_p(matrix_b, irow, icol, block_b, found)
91 IF (found) THEN
92 block_a(:, :) = factor*block_a(:, :)*block_b(:, :)
93 ELSE
94 block_a(:, :) = 0.0_dp
95 END IF
96 END DO
97 CALL dbcsr_iterator_stop(iterator)
98
99 CALL timestop(handle)
100 END SUBROUTINE hadamard_product_inplace
101
102! **************************************************************************************************
103!> \brief Computes C=A B A^T or C=A^T B A for DBCSR matrices.
104!> \param trans_A_left transposition applied to the left occurrence of A
105!> \param trans_A_right transposition applied to the right occurrence of A
106!> \param matrix_A left and right matrix A
107!> \param matrix_B input matrix B
108!> \param matrix_C output matrix C
109!> \param eps_filter filtering threshold for both matrix multiplications
110!> \param retain_sparsity if true, only existing blocks of C are filled
111! **************************************************************************************************
112 SUBROUTINE dbcsr_contract_aba(trans_A_left, trans_A_right, matrix_A, matrix_B, matrix_C, &
113 eps_filter, retain_sparsity)
114 CHARACTER(LEN=1), INTENT(IN) :: trans_a_left, trans_a_right
115 TYPE(dbcsr_type), INTENT(INOUT) :: matrix_a, matrix_b, matrix_c
116 REAL(kind=dp), INTENT(IN) :: eps_filter
117 LOGICAL, INTENT(IN), OPTIONAL :: retain_sparsity
118
119 CHARACTER(LEN=*), PARAMETER :: routinen = 'dbcsr_contract_ABA'
120
121 INTEGER :: handle
122 LOGICAL :: my_retain_sparsity
123 TYPE(dbcsr_type) :: work
124
125 CALL timeset(routinen, handle)
126
127 my_retain_sparsity = .false.
128 IF (PRESENT(retain_sparsity)) my_retain_sparsity = retain_sparsity
129
130 CALL dbcsr_create(work, template=matrix_a)
131
132 IF (trans_a_left == "N" .AND. trans_a_right == "T") THEN
133 ! C = A B A^T
134 CALL dbcsr_multiply("N", "N", 1.0_dp, matrix_a, matrix_b, &
135 0.0_dp, work, filter_eps=eps_filter)
136 CALL dbcsr_multiply("N", "T", 1.0_dp, work, matrix_a, &
137 0.0_dp, matrix_c, filter_eps=eps_filter, &
138 retain_sparsity=my_retain_sparsity)
139 ELSE IF (trans_a_left == "T" .AND. trans_a_right == "N") THEN
140 ! C = A^T B A
141 CALL dbcsr_multiply("N", "N", 1.0_dp, matrix_b, matrix_a, &
142 0.0_dp, work, filter_eps=eps_filter)
143 CALL dbcsr_multiply("T", "N", 1.0_dp, matrix_a, work, &
144 0.0_dp, matrix_c, filter_eps=eps_filter, &
145 retain_sparsity=my_retain_sparsity)
146 ELSE
147 cpabort("Unsupported transposition pair in dbcsr_contract_ABA")
148 END IF
149 CALL dbcsr_release(work)
150
151 CALL timestop(handle)
152
153 END SUBROUTINE dbcsr_contract_aba
154
155END MODULE gw_utils_dbcsr
logical function, public dbcsr_iterator_blocks_left(iterator)
...
subroutine, public dbcsr_iterator_stop(iterator)
...
subroutine, public dbcsr_copy(matrix_b, matrix_a, name, keep_sparsity, keep_imaginary)
...
subroutine, public dbcsr_get_block_p(matrix, row, col, block, found, row_size, col_size)
...
subroutine, public dbcsr_multiply(transa, transb, alpha, matrix_a, matrix_b, beta, matrix_c, first_row, last_row, first_column, last_column, first_k, last_k, retain_sparsity, filter_eps, flop)
...
subroutine, public dbcsr_iterator_next_block(iterator, row, column, block, block_number_argument_has_been_removed, row_size, col_size, row_offset, col_offset, transposed)
...
subroutine, public dbcsr_iterator_start(iterator, matrix, shared, dynamic, dynamic_byrows)
...
subroutine, public dbcsr_release(matrix)
...
Common DBCSR matrix operations used by GW modules.
subroutine, public hadamard_product(matrix_a, matrix_b, matrix_c, factor)
Computes the scaled element-wise product C = factor (A ◦ B) while preserving the block structure of A...
subroutine, public hadamard_product_inplace(matrix_a, matrix_b, factor)
Form A = factor * (A element-wise B) without changing A's block structure.
subroutine, public dbcsr_contract_aba(trans_a_left, trans_a_right, matrix_a, matrix_b, matrix_c, eps_filter, retain_sparsity)
Computes C=A B A^T or C=A^T B A for DBCSR matrices.
Defines the basic variable types.
Definition kinds.F:23
integer, parameter, public dp
Definition kinds.F:34