Line data Source code
1 : !--------------------------------------------------------------------------------------------------!
2 : ! CP2K: A general program to perform molecular dynamics simulations !
3 : ! Copyright 2000-2024 CP2K developers group <https://cp2k.org> !
4 : ! !
5 : ! SPDX-License-Identifier: GPL-2.0-or-later !
6 : !--------------------------------------------------------------------------------------------------!
7 :
8 : ! **************************************************************************************************
9 : !> \brief Calculation of overlap matrix condition numbers
10 : !> \par History
11 : !> \author JGH (14.11.2016)
12 : ! **************************************************************************************************
13 : MODULE qs_condnum
14 : USE arnoldi_api, ONLY: arnoldi_conjugate_gradient,&
15 : arnoldi_extremal
16 : USE cp_blacs_env, ONLY: cp_blacs_env_type
17 : USE cp_dbcsr_api, ONLY: &
18 : dbcsr_add, dbcsr_copy, dbcsr_create, dbcsr_desymmetrize, dbcsr_gershgorin_norm, &
19 : dbcsr_get_block_diag, dbcsr_get_info, dbcsr_get_matrix_type, dbcsr_iterator_blocks_left, &
20 : dbcsr_iterator_next_block, dbcsr_iterator_start, dbcsr_iterator_stop, dbcsr_iterator_type, &
21 : dbcsr_p_type, dbcsr_release, dbcsr_type, dbcsr_type_no_symmetry, dbcsr_type_symmetric
22 : USE cp_dbcsr_operations, ONLY: copy_dbcsr_to_fm
23 : USE cp_fm_basic_linalg, ONLY: cp_fm_norm
24 : USE cp_fm_diag, ONLY: cp_fm_power
25 : USE cp_fm_struct, ONLY: cp_fm_struct_create,&
26 : cp_fm_struct_release,&
27 : cp_fm_struct_type
28 : USE cp_fm_types, ONLY: cp_fm_create,&
29 : cp_fm_release,&
30 : cp_fm_type
31 : USE kinds, ONLY: dp
32 : USE mathlib, ONLY: invmat
33 : #include "./base/base_uses.f90"
34 :
35 : IMPLICIT NONE
36 :
37 : PRIVATE
38 :
39 : ! *** Global parameters ***
40 :
41 : CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_condnum'
42 :
43 : ! *** Public subroutines ***
44 :
45 : PUBLIC :: overlap_condnum
46 :
47 : CONTAINS
48 :
49 : ! **************************************************************************************************
50 : !> \brief Calculation of the overlap matrix Condition Number
51 : !> \param matrixkp_s The overlap matrices to be calculated (kpoints, optional)
52 : !> \param condnum Condition numbers for 1 and 2 norm
53 : !> \param iunit output unit
54 : !> \param norml1 logical: calculate estimate to 1-norm
55 : !> \param norml2 logical: calculate estimate to 1-norm and 2-norm condition number
56 : !> \param use_arnoldi logical: use Arnoldi iteration to estimate 2-norm condition number
57 : !> \param blacs_env ...
58 : !> \date 07.11.2016
59 : !> \par History
60 : !> \author JHU
61 : !> \version 1.0
62 : ! **************************************************************************************************
63 274 : SUBROUTINE overlap_condnum(matrixkp_s, condnum, iunit, norml1, norml2, use_arnoldi, blacs_env)
64 :
65 : TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrixkp_s
66 : REAL(KIND=dp), DIMENSION(2), INTENT(INOUT) :: condnum
67 : INTEGER, INTENT(IN) :: iunit
68 : LOGICAL, INTENT(IN) :: norml1, norml2, use_arnoldi
69 : TYPE(cp_blacs_env_type), POINTER :: blacs_env
70 :
71 : CHARACTER(len=*), PARAMETER :: routineN = 'overlap_condnum'
72 :
73 : INTEGER :: handle, ic, maxiter, nbas, ndep
74 : LOGICAL :: converged
75 : REAL(KIND=dp) :: amnorm, anorm, eps_ev, max_ev, min_ev, &
76 : threshold
77 : REAL(KIND=dp), DIMENSION(2) :: eigvals
78 : TYPE(cp_fm_struct_type), POINTER :: matrix_struct
79 : TYPE(cp_fm_type) :: fmsmat, fmwork
80 : TYPE(dbcsr_type) :: tempmat
81 : TYPE(dbcsr_type), POINTER :: smat
82 :
83 274 : CALL timeset(routineN, handle)
84 :
85 274 : condnum(1:2) = 0.0_dp
86 274 : NULLIFY (smat)
87 274 : IF (SIZE(matrixkp_s, 2) == 1) THEN
88 268 : IF (iunit > 0) WRITE (iunit, '(/,T2,A)') "OVERLAP MATRIX CONDITION NUMBER"
89 268 : smat => matrixkp_s(1, 1)%matrix
90 : ELSE
91 6 : IF (iunit > 0) WRITE (iunit, '(/,T2,A)') "OVERLAP MATRIX CONDITION NUMBER AT GAMMA POINT"
92 6 : ALLOCATE (smat)
93 6 : CALL dbcsr_create(smat, template=matrixkp_s(1, 1)%matrix)
94 6 : CALL dbcsr_copy(smat, matrixkp_s(1, 1)%matrix)
95 2050 : DO ic = 2, SIZE(matrixkp_s, 2)
96 2050 : CALL dbcsr_add(smat, matrixkp_s(1, ic)%matrix, 1.0_dp, 1.0_dp)
97 : END DO
98 : END IF
99 : !
100 274 : IF (ASSOCIATED(smat)) THEN
101 274 : CPASSERT(dbcsr_get_matrix_type(smat) .EQ. dbcsr_type_symmetric)
102 274 : IF (norml1) THEN
103 : ! norm of S
104 40 : anorm = dbcsr_gershgorin_norm(smat)
105 40 : CALL estimate_norm_invmat(smat, amnorm)
106 40 : IF (iunit > 0) THEN
107 20 : WRITE (iunit, '(T2,A)') "1-Norm Condition Number (Estimate)"
108 : WRITE (iunit, '(T4,A,ES11.3E3,T32,A,ES11.3E3,A4,ES11.3E3,T63,A,F8.4)') &
109 20 : "CN : |A|*|A^-1|: ", anorm, " * ", amnorm, "=", anorm*amnorm, "Log(1-CN):", LOG10(anorm*amnorm)
110 : END IF
111 40 : condnum(1) = anorm*amnorm
112 : END IF
113 274 : IF (norml2) THEN
114 246 : eps_ev = 1.0E-14_dp
115 : ! diagonalization
116 246 : CALL dbcsr_get_info(smat, nfullrows_total=nbas)
117 : CALL cp_fm_struct_create(fmstruct=matrix_struct, context=blacs_env, &
118 246 : nrow_global=nbas, ncol_global=nbas)
119 246 : CALL cp_fm_create(fmsmat, matrix_struct)
120 246 : CALL cp_fm_create(fmwork, matrix_struct)
121 : ! transfer to FM
122 246 : CALL dbcsr_create(tempmat, template=smat, matrix_type=dbcsr_type_no_symmetry)
123 246 : CALL dbcsr_desymmetrize(smat, tempmat)
124 246 : CALL copy_dbcsr_to_fm(tempmat, fmsmat)
125 :
126 : ! diagonalize
127 246 : anorm = cp_fm_norm(fmsmat, "1")
128 246 : CALL cp_fm_power(fmsmat, fmwork, -1.0_dp, eps_ev, ndep, eigvals=eigvals)
129 246 : min_ev = eigvals(1)
130 246 : max_ev = eigvals(2)
131 246 : amnorm = cp_fm_norm(fmsmat, "1")
132 :
133 246 : CALL dbcsr_release(tempmat)
134 246 : CALL cp_fm_release(fmsmat)
135 246 : CALL cp_fm_release(fmwork)
136 246 : CALL cp_fm_struct_release(matrix_struct)
137 :
138 246 : IF (iunit > 0) THEN
139 6 : WRITE (iunit, '(T2,A)') "1-Norm and 2-Norm Condition Numbers using Diagonalization"
140 6 : IF (min_ev > 0) THEN
141 : WRITE (iunit, '(T4,A,ES11.3E3,T32,A,ES11.3E3,A4,ES11.3E3,T63,A,F8.4)') &
142 6 : "CN : |A|*|A^-1|: ", anorm, " * ", amnorm, "=", anorm*amnorm, "Log(1-CN):", LOG10(anorm*amnorm)
143 : WRITE (iunit, '(T4,A,ES11.3E3,T32,A,ES11.3E3,A4,ES11.3E3,T63,A,F8.4)') &
144 6 : "CN : max/min ev: ", max_ev, " / ", min_ev, "=", max_ev/min_ev, "Log(2-CN):", LOG10(max_ev/min_ev)
145 : ELSE
146 : WRITE (iunit, '(T4,A,ES11.3E3,T32,A,ES11.3E3,T63,A)') &
147 0 : "CN : max/min EV: ", max_ev, " / ", min_ev, "Log(CN): infinity"
148 : END IF
149 : END IF
150 246 : IF (min_ev > 0) THEN
151 246 : condnum(1) = anorm*amnorm
152 246 : condnum(2) = max_ev/min_ev
153 : ELSE
154 0 : condnum(1:2) = 0.0_dp
155 : END IF
156 : END IF
157 274 : IF (use_arnoldi) THEN
158 : ! parameters for matrix condition test
159 12 : threshold = 1.0E-6_dp
160 12 : maxiter = 1000
161 12 : eps_ev = 1.0E8_dp
162 : CALL arnoldi_extremal(smat, max_ev, min_ev, &
163 12 : threshold=threshold, max_iter=maxiter, converged=converged)
164 12 : IF (iunit > 0) THEN
165 6 : WRITE (iunit, '(T2,A)') "2-Norm Condition Number using Arnoldi iterations"
166 6 : IF (min_ev > 0) THEN
167 : WRITE (iunit, '(T4,A,ES11.3E3,T32,A,ES11.3E3,A4,ES11.3E3,T63,A,F8.4)') &
168 6 : "CN : max/min ev: ", max_ev, " / ", min_ev, "=", max_ev/min_ev, "Log(2-CN):", LOG10(max_ev/min_ev)
169 : ELSE
170 : WRITE (iunit, '(T4,A,ES11.3E3,T32,A,ES11.3E3,T63,A)') &
171 0 : "CN : max/min ev: ", max_ev, " / ", min_ev, "Log(CN): infinity"
172 : END IF
173 : END IF
174 12 : IF (min_ev > 0) THEN
175 12 : condnum(2) = max_ev/min_ev
176 : ELSE
177 0 : condnum(2) = 0.0_dp
178 : END IF
179 12 : IF (converged) THEN
180 12 : IF (min_ev == 0) THEN
181 0 : CPWARN("Ill-conditioned S matrix: basis set is overcomplete.")
182 12 : ELSE IF ((max_ev/min_ev) > eps_ev) THEN
183 0 : CPWARN("Ill-conditioned S matrix: basis set is overcomplete.")
184 : END IF
185 : ELSE
186 0 : CPWARN("Condition number estimate of overlap matrix is not reliable (not converged).")
187 : END IF
188 : END IF
189 : END IF
190 274 : IF (SIZE(matrixkp_s, 2) == 1) THEN
191 : NULLIFY (smat)
192 : ELSE
193 6 : CALL dbcsr_release(smat)
194 6 : DEALLOCATE (smat)
195 : END IF
196 :
197 274 : CALL timestop(handle)
198 :
199 274 : END SUBROUTINE overlap_condnum
200 :
201 : ! **************************************************************************************************
202 : !> \brief Calculates an estimate of the 1-norm of the inverse of a matrix
203 : !> Uses LAPACK norm estimator algorithm
204 : !> NJ Higham, Function of Matrices, Algorithm 3.21, page 66
205 : !> \param amat Sparse, symmetric matrix
206 : !> \param anorm Estimate of ||INV(A)||
207 : !> \date 15.11.2016
208 : !> \par History
209 : !> \author JHU
210 : !> \version 1.0
211 : ! **************************************************************************************************
212 40 : SUBROUTINE estimate_norm_invmat(amat, anorm)
213 : TYPE(dbcsr_type), POINTER :: amat
214 : REAL(KIND=dp), INTENT(OUT) :: anorm
215 :
216 : INTEGER :: i, k, nbas
217 : INTEGER, DIMENSION(1) :: r
218 : REAL(KIND=dp) :: g, gg
219 40 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: x, xsi
220 : REAL(KIND=dp), DIMENSION(2) :: work
221 : REAL(KIND=dp), EXTERNAL :: dlange
222 : TYPE(dbcsr_type) :: pmat
223 :
224 : ! generate a block diagonal preconditioner
225 40 : CALL dbcsr_create(pmat, name="SMAT Preconditioner", template=amat)
226 : ! replicate the diagonal blocks of the overlap matrix
227 40 : CALL dbcsr_get_block_diag(amat, pmat)
228 : ! invert preconditioner
229 40 : CALL smat_precon_diag(pmat)
230 :
231 40 : anorm = 1.0_dp
232 40 : CALL dbcsr_get_info(amat, nfullrows_total=nbas)
233 160 : ALLOCATE (x(nbas), xsi(nbas))
234 1796 : x(1:nbas) = 1._dp/REAL(nbas, KIND=dp)
235 40 : CALL dbcsr_solve(amat, x, pmat)
236 40 : g = dlange("1", nbas, 1, x, nbas, work)
237 1796 : xsi(1:nbas) = SIGN(1._dp, x(1:nbas))
238 1796 : x(1:nbas) = xsi(1:nbas)
239 40 : CALL dbcsr_solve(amat, x, pmat)
240 40 : k = 2
241 : DO
242 1836 : r = MAXLOC(ABS(x))
243 1796 : x(1:nbas) = 0._dp
244 80 : x(r) = 1._dp
245 40 : CALL dbcsr_solve(amat, x, pmat)
246 40 : gg = g
247 40 : g = dlange("1", nbas, 1, x, nbas, work)
248 40 : IF (g <= gg) EXIT
249 1796 : x(1:nbas) = SIGN(1._dp, x(1:nbas))
250 3552 : IF (SUM(ABS(x - xsi)) == 0 .OR. SUM(ABS(x + xsi)) == 0) EXIT
251 1768 : xsi(1:nbas) = x(1:nbas)
252 38 : CALL dbcsr_solve(amat, x, pmat)
253 38 : k = k + 1
254 38 : IF (k > 5) EXIT
255 1808 : IF (SUM(r) == SUM(MAXLOC(ABS(x)))) EXIT
256 : END DO
257 : !
258 40 : IF (nbas > 1) THEN
259 1796 : DO i = 1, nbas
260 1796 : x(i) = -1._dp**(i + 1)*(1._dp + REAL(i - 1, dp)/REAL(nbas - 1, dp))
261 : END DO
262 : ELSE
263 0 : x(1) = 1.0_dp
264 : END IF
265 40 : CALL dbcsr_solve(amat, x, pmat)
266 40 : gg = dlange("1", nbas, 1, x, nbas, work)
267 40 : gg = 2._dp*gg/REAL(3*nbas, dp)
268 40 : anorm = MAX(g, gg)
269 40 : DEALLOCATE (x, xsi)
270 40 : CALL dbcsr_release(pmat)
271 :
272 80 : END SUBROUTINE estimate_norm_invmat
273 :
274 : ! **************************************************************************************************
275 : !> \brief ...
276 : !> \param amat ...
277 : !> \param x ...
278 : !> \param pmat ...
279 : ! **************************************************************************************************
280 198 : SUBROUTINE dbcsr_solve(amat, x, pmat)
281 : TYPE(dbcsr_type), POINTER :: amat
282 : REAL(KIND=dp), DIMENSION(:), INTENT(INOUT) :: x
283 : TYPE(dbcsr_type) :: pmat
284 :
285 : INTEGER :: max_iter, nbas
286 : LOGICAL :: converged
287 : REAL(KIND=dp) :: threshold
288 :
289 198 : CALL dbcsr_get_info(amat, nfullrows_total=nbas)
290 198 : max_iter = MIN(1000, nbas)
291 198 : threshold = 1.e-6_dp
292 198 : CALL arnoldi_conjugate_gradient(amat, x, pmat, converged=converged, threshold=threshold, max_iter=max_iter)
293 :
294 198 : END SUBROUTINE dbcsr_solve
295 :
296 : ! **************************************************************************************************
297 : !> \brief ...
298 : !> \param pmat ...
299 : ! **************************************************************************************************
300 40 : SUBROUTINE smat_precon_diag(pmat)
301 : TYPE(dbcsr_type) :: pmat
302 :
303 : INTEGER :: iatom, info, jatom, n
304 40 : REAL(KIND=dp), DIMENSION(:, :), POINTER :: sblock
305 : TYPE(dbcsr_iterator_type) :: dbcsr_iter
306 :
307 40 : CALL dbcsr_iterator_start(dbcsr_iter, pmat)
308 122 : DO WHILE (dbcsr_iterator_blocks_left(dbcsr_iter))
309 82 : CALL dbcsr_iterator_next_block(dbcsr_iter, iatom, jatom, sblock)
310 82 : CPASSERT(iatom == jatom)
311 82 : n = SIZE(sblock, 1)
312 82 : CALL invmat(sblock(1:n, 1:n), info)
313 122 : CPASSERT(info == 0)
314 : END DO
315 40 : CALL dbcsr_iterator_stop(dbcsr_iter)
316 :
317 40 : END SUBROUTINE smat_precon_diag
318 :
319 : END MODULE qs_condnum
320 :
|