(git:4b1c2b1)
Loading...
Searching...
No Matches
qs_mo_methods.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 collects routines that perform operations directly related to MOs
10!> \note
11!> first version : most routines imported
12!> \author Joost VandeVondele (2003-08)
13! **************************************************************************************************
15 USE admm_types, ONLY: admm_type
22 USE cp_cfm_types, ONLY: cp_cfm_create,&
28 USE cp_dbcsr_api, ONLY: dbcsr_copy,&
35 dbcsr_type_no_symmetry
36 USE cp_dbcsr_diag, ONLY: cp_dbcsr_syevd,&
51 USE cp_fm_types, ONLY: &
55 USE kinds, ONLY: dp
56 USE mathconstants, ONLY: z_one,&
57 z_zero
60 USE physcon, ONLY: evolt
62 USE qs_mo_types, ONLY: get_mo_set,&
65#include "./base/base_uses.f90"
66
67 IMPLICIT NONE
68 PRIVATE
69 CHARACTER(LEN=*), PARAMETER, PRIVATE :: moduleN = 'qs_mo_methods'
70 REAL(KIND=dp), PARAMETER :: empty_mo_norm = 100.0_dp*epsilon(1.0_dp)
71
72 PUBLIC :: complete_mo_basis
77
79 MODULE PROCEDURE subspace_eigenvalues_ks_fm
80 MODULE PROCEDURE subspace_eigenvalues_ks_dbcsr
81 END INTERFACE
82
83 INTERFACE make_basis_sv
84 MODULE PROCEDURE make_basis_sv_fm
85 MODULE PROCEDURE make_basis_sv_dbcsr
86 END INTERFACE
87
89 MODULE PROCEDURE complete_mo_basis_real
90 MODULE PROCEDURE complete_mo_basis_complex
91 END INTERFACE
92
93CONTAINS
94
95! **************************************************************************************************
96!> \brief Complete and orthonormalize virtual MOs while preserving the occupied columns.
97!> \param mo_coeff Orbital coefficients, with possible trailing storage columns.
98!> \param nocc Number of occupied columns to preserve.
99!> \param nactive Number of orbitals to prepare, including virtual states.
100!> \param matrix_s Overlap matrix, or a null pointer for the unit metric.
101! **************************************************************************************************
102 SUBROUTINE complete_mo_basis_real(mo_coeff, nocc, nactive, matrix_s)
103 TYPE(cp_fm_type), INTENT(IN) :: mo_coeff
104 INTEGER, INTENT(IN) :: nocc, nactive
105 TYPE(dbcsr_type), POINTER :: matrix_s
106
107 CHARACTER(LEN=*), PARAMETER :: routineN = 'complete_mo_basis_real'
108
109 INTEGER :: handle, imo, nao, pass
110 REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: column_norm
111 TYPE(cp_fm_struct_type), POINTER :: active_struct
112 TYPE(cp_fm_type) :: coeff, gram, s_coeff
113
114 cpassert(nocc >= 0 .AND. nocc <= nactive)
115 cpassert(nactive <= mo_coeff%matrix_struct%ncol_global)
116 IF (nactive == nocc) RETURN
117
118 CALL timeset(routinen, handle)
119 nao = mo_coeff%matrix_struct%nrow_global
120 cpassert(nactive <= nao)
121 NULLIFY (active_struct)
122 CALL cp_fm_struct_create(active_struct, template_fmstruct=mo_coeff%matrix_struct, ncol_global=nactive)
123 CALL cp_fm_create(coeff, active_struct)
124 CALL cp_fm_create(s_coeff, active_struct)
125 CALL cp_fm_create(gram, active_struct, nrow=nactive, ncol=nactive)
126 CALL cp_fm_to_fm(mo_coeff, coeff, nactive)
127 ALLOCATE (column_norm(nactive))
128 CALL cp_fm_vectorsnorm(coeff, column_norm)
129 IF (any(column_norm(nocc + 1:nactive) <= empty_mo_norm)) THEN
130 CALL cp_fm_init_random(s_coeff)
131 DO imo = nocc + 1, nactive
132 IF (column_norm(imo) > empty_mo_norm) cycle
133 CALL cp_fm_to_fm(s_coeff, coeff, 1, source_start=imo, target_start=imo)
134 END DO
135 END IF
136 DEALLOCATE (column_norm)
137
138 ! Upper-triangular Cholesky orthogonalization preserves the occupied span.
139 ! A second pass removes roundoff from the randomly completed complement.
140 DO pass = 1, 2
141 IF (ASSOCIATED(matrix_s)) THEN
142 CALL cp_dbcsr_sm_fm_multiply(matrix_s, coeff, s_coeff, nactive)
143 ELSE
144 CALL cp_fm_to_fm(coeff, s_coeff)
145 END IF
146 CALL parallel_gemm('T', 'N', nactive, nactive, nao, 1.0_dp, coeff, s_coeff, 0.0_dp, gram)
147 CALL cp_fm_cholesky_decompose(gram)
148 CALL cp_fm_triangular_multiply(gram, coeff, side='R', invert_tr=.true.)
149 END DO
150 CALL cp_fm_to_fm(coeff, mo_coeff, nactive - nocc, source_start=nocc + 1, target_start=nocc + 1)
151 CALL cp_fm_release(gram)
152 CALL cp_fm_release(s_coeff)
153 CALL cp_fm_release(coeff)
154 CALL cp_fm_struct_release(active_struct)
155 CALL timestop(handle)
156 END SUBROUTINE complete_mo_basis_real
157
158! **************************************************************************************************
159!> \brief Complete and orthonormalize virtual MOs while preserving the occupied columns.
160!> \param mo_coeff Orbital coefficients, with possible trailing storage columns.
161!> \param nocc Number of occupied columns to preserve.
162!> \param nactive Number of orbitals to prepare, including virtual states.
163!> \param matrix_s Overlap matrix.
164! **************************************************************************************************
165 SUBROUTINE complete_mo_basis_complex(mo_coeff, nocc, nactive, matrix_s)
166 TYPE(cp_cfm_type), INTENT(IN) :: mo_coeff
167 INTEGER, INTENT(IN) :: nocc, nactive
168 TYPE(cp_cfm_type), INTENT(IN) :: matrix_s
169
170 CHARACTER(LEN=*), PARAMETER :: routineN = 'complete_mo_basis_complex'
171
172 INTEGER :: handle, imo, nao, pass
173 REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: column_norm
174 TYPE(cp_cfm_type) :: coeff, gram, s_coeff
175 TYPE(cp_fm_struct_type), POINTER :: active_struct
176 TYPE(cp_fm_type) :: random_coeff
177
178 cpassert(nocc >= 0 .AND. nocc <= nactive)
179 cpassert(nactive <= mo_coeff%matrix_struct%ncol_global)
180 IF (nactive == nocc) RETURN
181
182 CALL timeset(routinen, handle)
183 nao = mo_coeff%matrix_struct%nrow_global
184 cpassert(nactive <= nao)
185 NULLIFY (active_struct)
186 CALL cp_fm_struct_create(active_struct, template_fmstruct=mo_coeff%matrix_struct, ncol_global=nactive)
187 CALL cp_cfm_create(coeff, active_struct)
188 CALL cp_cfm_create(s_coeff, active_struct)
189 CALL cp_cfm_create(gram, active_struct, nrow=nactive, ncol=nactive)
190 CALL cp_cfm_to_cfm(mo_coeff, coeff, nactive)
191 ALLOCATE (column_norm(nactive))
192 CALL cp_cfm_vectorsnorm(coeff, column_norm)
193 IF (any(column_norm(nocc + 1:nactive) <= empty_mo_norm)) THEN
194 CALL cp_fm_create(random_coeff, active_struct, set_zero=.true.)
195 CALL cp_fm_init_random(random_coeff)
196 CALL cp_fm_to_cfm(msourcer=random_coeff, mtarget=s_coeff)
197 CALL cp_fm_release(random_coeff)
198 DO imo = nocc + 1, nactive
199 IF (column_norm(imo) > empty_mo_norm) cycle
200 CALL cp_cfm_to_cfm(s_coeff, coeff, 1, source_start=imo, target_start=imo)
201 END DO
202 END IF
203 DEALLOCATE (column_norm)
204
205 ! Upper-triangular Cholesky orthogonalization preserves the occupied span.
206 ! A second pass removes roundoff from the randomly completed complement.
207 DO pass = 1, 2
208 CALL parallel_gemm('N', 'N', nao, nactive, nao, z_one, matrix_s, coeff, z_zero, s_coeff)
209 CALL parallel_gemm('C', 'N', nactive, nactive, nao, z_one, coeff, s_coeff, z_zero, gram)
211 CALL cp_cfm_triangular_multiply(gram, coeff, side='R', invert_tr=.true.)
212 END DO
213 CALL cp_cfm_to_cfm(coeff, mo_coeff, nactive - nocc, source_start=nocc + 1, target_start=nocc + 1)
214 CALL cp_cfm_release(gram)
215 CALL cp_cfm_release(s_coeff)
216 CALL cp_cfm_release(coeff)
217 CALL cp_fm_struct_release(active_struct)
218 CALL timestop(handle)
219 END SUBROUTINE complete_mo_basis_complex
220
221! **************************************************************************************************
222!> \brief returns an S-orthonormal basis v (v^T S v ==1)
223!> \param vmatrix ...
224!> \param ncol ...
225!> \param matrix_s ...
226!> \par History
227!> 03.2006 created [Joost VandeVondele]
228! **************************************************************************************************
229 SUBROUTINE make_basis_sm(vmatrix, ncol, matrix_s)
230 TYPE(cp_fm_type), INTENT(IN) :: vmatrix
231 INTEGER, INTENT(IN) :: ncol
232 TYPE(dbcsr_type), POINTER :: matrix_s
233
234 CHARACTER(LEN=*), PARAMETER :: routinen = 'make_basis_sm'
235 REAL(kind=dp), PARAMETER :: rone = 1.0_dp, rzero = 0.0_dp
236
237 INTEGER :: handle, n, ncol_global
238 TYPE(cp_fm_struct_type), POINTER :: fm_struct_tmp
239 TYPE(cp_fm_type) :: overlap_vv, svmatrix
240
241 IF (ncol == 0) RETURN
242
243 CALL timeset(routinen, handle)
244
245 CALL cp_fm_get_info(matrix=vmatrix, nrow_global=n, ncol_global=ncol_global)
246 IF (ncol > ncol_global) cpabort("Wrong ncol value")
247
248 CALL cp_fm_create(svmatrix, vmatrix%matrix_struct, "SV", set_zero=.true.)
249 CALL cp_dbcsr_sm_fm_multiply(matrix_s, vmatrix, svmatrix, ncol)
250
251 NULLIFY (fm_struct_tmp)
252 CALL cp_fm_struct_create(fm_struct_tmp, nrow_global=ncol, ncol_global=ncol, &
253 para_env=vmatrix%matrix_struct%para_env, &
254 context=vmatrix%matrix_struct%context)
255 CALL cp_fm_create(overlap_vv, fm_struct_tmp, "overlap_vv")
256 CALL cp_fm_struct_release(fm_struct_tmp)
257
258 CALL parallel_gemm('T', 'N', ncol, ncol, n, rone, vmatrix, svmatrix, rzero, overlap_vv)
259 CALL cp_fm_cholesky_decompose(overlap_vv)
260 CALL cp_fm_triangular_multiply(overlap_vv, vmatrix, n_cols=ncol, side='R', invert_tr=.true.)
261
262 CALL cp_fm_release(overlap_vv)
263 CALL cp_fm_release(svmatrix)
264
265 CALL timestop(handle)
266
267 END SUBROUTINE make_basis_sm
268
269! **************************************************************************************************
270!> \brief returns an S-orthonormal basis v and the corresponding matrix S*v as well
271!> \param vmatrix ...
272!> \param ncol ...
273!> \param svmatrix ...
274!> \par History
275!> 03.2006 created [Joost VandeVondele]
276! **************************************************************************************************
277 SUBROUTINE make_basis_sv_fm(vmatrix, ncol, svmatrix)
278
279 TYPE(cp_fm_type), INTENT(IN) :: vmatrix
280 INTEGER, INTENT(IN) :: ncol
281 TYPE(cp_fm_type), INTENT(IN) :: svmatrix
282
283 CHARACTER(LEN=*), PARAMETER :: routineN = 'make_basis_sv_fm'
284 REAL(KIND=dp), PARAMETER :: rone = 1.0_dp, rzero = 0.0_dp
285
286 INTEGER :: handle, n, ncol_global
287 TYPE(cp_fm_struct_type), POINTER :: fm_struct_tmp
288 TYPE(cp_fm_type) :: overlap_vv
289
290 IF (ncol == 0) RETURN
291
292 CALL timeset(routinen, handle)
293 NULLIFY (fm_struct_tmp)
294
295 CALL cp_fm_get_info(matrix=vmatrix, nrow_global=n, ncol_global=ncol_global)
296 IF (ncol > ncol_global) cpabort("Wrong ncol value")
297
298 CALL cp_fm_struct_create(fm_struct_tmp, nrow_global=ncol, ncol_global=ncol, &
299 para_env=vmatrix%matrix_struct%para_env, &
300 context=vmatrix%matrix_struct%context)
301 CALL cp_fm_create(overlap_vv, fm_struct_tmp, "overlap_vv")
302 CALL cp_fm_struct_release(fm_struct_tmp)
303
304 CALL parallel_gemm('T', 'N', ncol, ncol, n, rone, vmatrix, svmatrix, rzero, overlap_vv)
305 CALL cp_fm_cholesky_decompose(overlap_vv)
306 CALL cp_fm_triangular_multiply(overlap_vv, vmatrix, n_cols=ncol, side='R', invert_tr=.true.)
307 CALL cp_fm_triangular_multiply(overlap_vv, svmatrix, n_cols=ncol, side='R', invert_tr=.true.)
308
309 CALL cp_fm_release(overlap_vv)
310
311 CALL timestop(handle)
312
313 END SUBROUTINE make_basis_sv_fm
314
315! **************************************************************************************************
316!> \brief ...
317!> \param vmatrix ...
318!> \param ncol ...
319!> \param svmatrix ...
320!> \param para_env ...
321!> \param blacs_env ...
322! **************************************************************************************************
323 SUBROUTINE make_basis_sv_dbcsr(vmatrix, ncol, svmatrix, para_env, blacs_env)
324
325 TYPE(dbcsr_type) :: vmatrix
326 INTEGER, INTENT(IN) :: ncol
327 TYPE(dbcsr_type) :: svmatrix
328 TYPE(mp_para_env_type), POINTER :: para_env
329 TYPE(cp_blacs_env_type), POINTER :: blacs_env
330
331 CHARACTER(LEN=*), PARAMETER :: routineN = 'make_basis_sv_dbcsr'
332 REAL(KIND=dp), PARAMETER :: rone = 1.0_dp, rzero = 0.0_dp
333
334 INTEGER :: handle, n, ncol_global
335 TYPE(cp_fm_struct_type), POINTER :: fm_struct_tmp
336 TYPE(cp_fm_type) :: fm_svmatrix, fm_vmatrix, overlap_vv
337
338 IF (ncol == 0) RETURN
339
340 CALL timeset(routinen, handle)
341
342 !CALL cp_fm_get_info(matrix=vmatrix,nrow_global=n,ncol_global=ncol_global)
343 CALL dbcsr_get_info(vmatrix, nfullrows_total=n, nfullcols_total=ncol_global)
344 IF (ncol > ncol_global) cpabort("Wrong ncol value")
345
346 CALL cp_fm_struct_create(fm_struct_tmp, context=blacs_env, nrow_global=ncol, &
347 ncol_global=ncol, para_env=para_env)
348 CALL cp_fm_create(overlap_vv, fm_struct_tmp, name="fm_overlap_vv")
349 CALL cp_fm_struct_release(fm_struct_tmp)
350
351 CALL cp_fm_struct_create(fm_struct_tmp, context=blacs_env, nrow_global=n, &
352 ncol_global=ncol_global, para_env=para_env)
353 CALL cp_fm_create(fm_vmatrix, fm_struct_tmp, name="fm_vmatrix")
354 CALL cp_fm_create(fm_svmatrix, fm_struct_tmp, name="fm_svmatrix")
355 CALL cp_fm_struct_release(fm_struct_tmp)
356
357 CALL copy_dbcsr_to_fm(vmatrix, fm_vmatrix)
358 CALL copy_dbcsr_to_fm(svmatrix, fm_svmatrix)
359
360 CALL parallel_gemm('T', 'N', ncol, ncol, n, rone, fm_vmatrix, fm_svmatrix, rzero, overlap_vv)
361 CALL cp_fm_cholesky_decompose(overlap_vv)
362 CALL cp_fm_triangular_multiply(overlap_vv, fm_vmatrix, n_cols=ncol, side='R', invert_tr=.true.)
363 CALL cp_fm_triangular_multiply(overlap_vv, fm_svmatrix, n_cols=ncol, side='R', invert_tr=.true.)
364
365 CALL copy_fm_to_dbcsr(fm_vmatrix, vmatrix)
366 CALL copy_fm_to_dbcsr(fm_svmatrix, svmatrix)
367
368 CALL cp_fm_release(overlap_vv)
369 CALL cp_fm_release(fm_vmatrix)
370 CALL cp_fm_release(fm_svmatrix)
371
372 CALL timestop(handle)
373
374 END SUBROUTINE make_basis_sv_dbcsr
375
376! **************************************************************************************************
377!> \brief return a set of S orthonormal vectors (C^T S C == 1) where
378!> the cholesky decomposed form of S is passed as an argument
379!> \param vmatrix ...
380!> \param ncol ...
381!> \param ortho cholesky decomposed S matrix
382!> \par History
383!> 03.2006 created [Joost VandeVondele]
384!> \note
385!> if the cholesky decomposed S matrix is not available
386!> use make_basis_sm since this is much faster than computing the
387!> cholesky decomposition of S
388! **************************************************************************************************
389 SUBROUTINE make_basis_cholesky(vmatrix, ncol, ortho)
390
391 TYPE(cp_fm_type), INTENT(IN) :: vmatrix
392 INTEGER, INTENT(IN) :: ncol
393 TYPE(cp_fm_type), INTENT(IN) :: ortho
394
395 CHARACTER(LEN=*), PARAMETER :: routinen = 'make_basis_cholesky'
396 REAL(kind=dp), PARAMETER :: rone = 1.0_dp, rzero = 0.0_dp
397
398 INTEGER :: handle, n, ncol_global
399 TYPE(cp_fm_struct_type), POINTER :: fm_struct_tmp
400 TYPE(cp_fm_type) :: overlap_vv
401
402 IF (ncol == 0) RETURN
403
404 CALL timeset(routinen, handle)
405 NULLIFY (fm_struct_tmp)
406
407 CALL cp_fm_get_info(matrix=vmatrix, nrow_global=n, ncol_global=ncol_global)
408 IF (ncol > ncol_global) cpabort("Wrong ncol value")
409
410 CALL cp_fm_struct_create(fm_struct_tmp, nrow_global=ncol, ncol_global=ncol, &
411 para_env=vmatrix%matrix_struct%para_env, &
412 context=vmatrix%matrix_struct%context)
413 CALL cp_fm_create(overlap_vv, fm_struct_tmp, "overlap_vv")
414 CALL cp_fm_struct_release(fm_struct_tmp)
415
416 CALL cp_fm_triangular_multiply(ortho, vmatrix, n_cols=ncol)
417 CALL cp_fm_syrk('U', 'T', n, rone, vmatrix, 1, 1, rzero, overlap_vv)
418 CALL cp_fm_cholesky_decompose(overlap_vv)
419 CALL cp_fm_triangular_multiply(overlap_vv, vmatrix, n_cols=ncol, side='R', invert_tr=.true.)
420 CALL cp_fm_triangular_multiply(ortho, vmatrix, n_cols=ncol, invert_tr=.true.)
421
422 CALL cp_fm_release(overlap_vv)
423
424 CALL timestop(handle)
425
426 END SUBROUTINE make_basis_cholesky
427
428! **************************************************************************************************
429!> \brief return a set of S orthonormal vectors (C^T S C == 1) where
430!> a Loedwin transformation is applied to keep the rotated vectors as close
431!> as possible to the original ones
432!> \param vmatrix ...
433!> \param ncol ...
434!> \param matrix_s ...
435!> \param
436!> \par History
437!> 05.2009 created [MI]
438!> \note
439! **************************************************************************************************
440 SUBROUTINE make_basis_lowdin(vmatrix, ncol, matrix_s)
441
442 TYPE(cp_fm_type), INTENT(IN) :: vmatrix
443 INTEGER, INTENT(IN) :: ncol
444 TYPE(dbcsr_type) :: matrix_s
445
446 CHARACTER(LEN=*), PARAMETER :: routinen = 'make_basis_lowdin'
447 REAL(kind=dp), PARAMETER :: rone = 1.0_dp, rzero = 0.0_dp
448
449 INTEGER :: handle, n, ncol_global, ndep
450 REAL(dp) :: threshold
451 TYPE(cp_fm_struct_type), POINTER :: fm_struct_tmp
452 TYPE(cp_fm_type) :: csc, sc, work
453
454 IF (ncol == 0) RETURN
455
456 CALL timeset(routinen, handle)
457 NULLIFY (fm_struct_tmp)
458 threshold = 1.0e-7_dp
459 CALL cp_fm_get_info(matrix=vmatrix, nrow_global=n, ncol_global=ncol_global)
460 IF (ncol > ncol_global) cpabort("Wrong ncol value")
461
462 CALL cp_fm_create(sc, vmatrix%matrix_struct, "SC")
463 CALL cp_dbcsr_sm_fm_multiply(matrix_s, vmatrix, sc, ncol)
464
465 NULLIFY (fm_struct_tmp)
466 CALL cp_fm_struct_create(fm_struct_tmp, nrow_global=ncol, ncol_global=ncol, &
467 para_env=vmatrix%matrix_struct%para_env, &
468 context=vmatrix%matrix_struct%context)
469 CALL cp_fm_create(csc, fm_struct_tmp, "csc")
470 CALL cp_fm_set_all(csc, 0.0_dp)
471 CALL cp_fm_create(work, fm_struct_tmp, "work")
472 CALL cp_fm_set_all(work, 0.0_dp)
473 CALL cp_fm_struct_release(fm_struct_tmp)
474
475 CALL parallel_gemm('T', 'N', ncol, ncol, n, rone, vmatrix, sc, rzero, csc)
476 CALL cp_fm_power(csc, work, -0.5_dp, threshold, ndep)
477 CALL parallel_gemm('N', 'N', n, ncol, ncol, rone, vmatrix, csc, rzero, sc)
478 CALL cp_fm_to_fm(sc, vmatrix, ncol, 1, 1)
479
480 CALL cp_fm_release(csc)
481 CALL cp_fm_release(sc)
482 CALL cp_fm_release(work)
483
484 CALL timestop(handle)
485
486 END SUBROUTINE make_basis_lowdin
487
488! **************************************************************************************************
489!> \brief given a set of vectors, return an orthogonal (C^T C == 1) set
490!> spanning the same space (notice, only for cases where S==1)
491!> \param vmatrix ...
492!> \param ncol ...
493!> \par History
494!> 03.2006 created [Joost VandeVondele]
495! **************************************************************************************************
496 SUBROUTINE make_basis_simple(vmatrix, ncol)
497
498 TYPE(cp_fm_type), INTENT(IN) :: vmatrix
499 INTEGER, INTENT(IN) :: ncol
500
501 CHARACTER(LEN=*), PARAMETER :: routinen = 'make_basis_simple'
502 REAL(kind=dp), PARAMETER :: rone = 1.0_dp, rzero = 0.0_dp
503
504 INTEGER :: handle, n, ncol_global
505 TYPE(cp_fm_struct_type), POINTER :: fm_struct_tmp
506 TYPE(cp_fm_type) :: overlap_vv
507
508 IF (ncol == 0) RETURN
509
510 CALL timeset(routinen, handle)
511
512 NULLIFY (fm_struct_tmp)
513
514 CALL cp_fm_get_info(matrix=vmatrix, nrow_global=n, ncol_global=ncol_global)
515 IF (ncol > ncol_global) cpabort("Wrong ncol value")
516
517 CALL cp_fm_struct_create(fm_struct_tmp, nrow_global=ncol, ncol_global=ncol, &
518 para_env=vmatrix%matrix_struct%para_env, &
519 context=vmatrix%matrix_struct%context)
520 CALL cp_fm_create(overlap_vv, fm_struct_tmp, "overlap_vv")
521 CALL cp_fm_struct_release(fm_struct_tmp)
522
523 CALL parallel_gemm('T', 'N', ncol, ncol, n, rone, vmatrix, vmatrix, rzero, overlap_vv)
524 CALL cp_fm_cholesky_decompose(overlap_vv)
525 CALL cp_fm_triangular_multiply(overlap_vv, vmatrix, n_cols=ncol, side='R', invert_tr=.true.)
526
527 CALL cp_fm_release(overlap_vv)
528
529 CALL timestop(handle)
530
531 END SUBROUTINE make_basis_simple
532
533! **************************************************************************************************
534!> \brief computes ritz values of a set of orbitals given a ks_matrix
535!> rotates the orbitals into eigenstates depending on do_rotation
536!> writes the evals to the screen depending on ionode/scr
537!> \param orbitals S-orthonormal orbitals
538!> \param ks_matrix Kohn-Sham matrix
539!> \param evals_arg optional, filled with the evals
540!> \param ionode , scr if present write to unit scr where ionode
541!> \param scr ...
542!> \param do_rotation optional rotate orbitals (default=.TRUE.)
543!> note that rotating the orbitals is slower
544!> \param co_rotate an optional set of orbitals rotated by the same rotation matrix
545!> \param co_rotate_dbcsr ...
546!> \par History
547!> 08.2004 documented and added do_rotation [Joost VandeVondele]
548!> 09.2008 only compute eigenvalues if rotation is not needed
549! **************************************************************************************************
550 SUBROUTINE subspace_eigenvalues_ks_fm(orbitals, ks_matrix, evals_arg, ionode, scr, &
551 do_rotation, co_rotate, co_rotate_dbcsr)
552
553 TYPE(cp_fm_type), INTENT(IN) :: orbitals
554 TYPE(dbcsr_type), POINTER :: ks_matrix
555 REAL(KIND=dp), DIMENSION(:), OPTIONAL :: evals_arg
556 LOGICAL, INTENT(IN), OPTIONAL :: ionode
557 INTEGER, INTENT(IN), OPTIONAL :: scr
558 LOGICAL, INTENT(IN), OPTIONAL :: do_rotation
559 TYPE(cp_fm_type), INTENT(IN), OPTIONAL :: co_rotate
560 TYPE(dbcsr_type), OPTIONAL, POINTER :: co_rotate_dbcsr
561
562 CHARACTER(LEN=*), PARAMETER :: routineN = 'subspace_eigenvalues_ks_fm'
563
564 INTEGER :: handle, i, j, n, ncol_global, nrow_global
565 LOGICAL :: compute_evecs, do_rotation_local
566 REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: evals
567 TYPE(cp_fm_struct_type), POINTER :: fm_struct_tmp
568 TYPE(cp_fm_type) :: e_vectors, h_block, weighted_vectors, &
569 weighted_vectors2
570
571 CALL timeset(routinen, handle)
572
573 do_rotation_local = .true.
574 IF (PRESENT(do_rotation)) do_rotation_local = do_rotation
575
576 NULLIFY (fm_struct_tmp)
577 CALL cp_fm_get_info(matrix=orbitals, &
578 ncol_global=ncol_global, &
579 nrow_global=nrow_global)
580
581 IF (do_rotation_local) THEN
582 compute_evecs = .true.
583 ELSE
584 ! this would be the logical choice if syevx computing only evals were faster than syevd computing evecs and evals.
585 compute_evecs = .false.
586 ! this is not the case, so lets compute evecs always
587 compute_evecs = .true.
588 END IF
589
590 IF (ncol_global > 0) THEN
591
592 ALLOCATE (evals(ncol_global))
593
594 CALL cp_fm_create(weighted_vectors, orbitals%matrix_struct, "weighted_vectors")
595 CALL cp_fm_struct_create(fm_struct_tmp, nrow_global=ncol_global, ncol_global=ncol_global, &
596 para_env=orbitals%matrix_struct%para_env, &
597 context=orbitals%matrix_struct%context)
598 CALL cp_fm_create(h_block, fm_struct_tmp, name="h block")
599 IF (compute_evecs) THEN
600 CALL cp_fm_create(e_vectors, fm_struct_tmp, name="e vectors")
601 END IF
602 CALL cp_fm_struct_release(fm_struct_tmp)
603
604 ! h subblock and diag
605 CALL cp_dbcsr_sm_fm_multiply(ks_matrix, orbitals, weighted_vectors, ncol_global)
606
607 CALL parallel_gemm('T', 'N', ncol_global, ncol_global, nrow_global, 1.0_dp, &
608 orbitals, weighted_vectors, 0.0_dp, h_block)
609
610 ! if eigenvectors are required, go for syevd, otherwise only compute eigenvalues
611 IF (compute_evecs) THEN
612 CALL choose_eigv_solver(h_block, e_vectors, evals)
613 ELSE
614 CALL cp_fm_syevx(h_block, eigenvalues=evals)
615 END IF
616
617 ! rotate the orbitals
618 IF (do_rotation_local) THEN
619 CALL parallel_gemm('N', 'N', nrow_global, ncol_global, ncol_global, 1.0_dp, &
620 orbitals, e_vectors, 0.0_dp, weighted_vectors)
621 CALL cp_fm_to_fm(weighted_vectors, orbitals)
622 IF (PRESENT(co_rotate)) THEN
623 CALL parallel_gemm('N', 'N', nrow_global, ncol_global, ncol_global, 1.0_dp, &
624 co_rotate, e_vectors, 0.0_dp, weighted_vectors)
625 CALL cp_fm_to_fm(weighted_vectors, co_rotate)
626 END IF
627 IF (PRESENT(co_rotate_dbcsr)) THEN
628 IF (ASSOCIATED(co_rotate_dbcsr)) THEN
629 CALL cp_fm_create(weighted_vectors2, orbitals%matrix_struct, "weighted_vectors")
630 CALL copy_dbcsr_to_fm(co_rotate_dbcsr, weighted_vectors2)
631 CALL parallel_gemm('N', 'N', nrow_global, ncol_global, ncol_global, 1.0_dp, &
632 weighted_vectors2, e_vectors, 0.0_dp, weighted_vectors)
633 CALL copy_fm_to_dbcsr(weighted_vectors, co_rotate_dbcsr)
634 CALL cp_fm_release(weighted_vectors2)
635 END IF
636 END IF
637 END IF
638
639 ! give output
640 IF (PRESENT(evals_arg)) THEN
641 n = min(SIZE(evals_arg), SIZE(evals))
642 evals_arg(1:n) = evals(1:n)
643 END IF
644
645 IF (PRESENT(ionode) .OR. PRESENT(scr)) THEN
646 IF (.NOT. PRESENT(ionode)) cpabort("IONODE?")
647 IF (.NOT. PRESENT(scr)) cpabort("SCR?")
648 IF (ionode) THEN
649 DO i = 1, ncol_global, 4
650 j = min(3, ncol_global - i)
651 SELECT CASE (j)
652 CASE (3)
653 WRITE (scr, '(1X,4F16.8)') evals(i:i + j)
654 CASE (2)
655 WRITE (scr, '(1X,3F16.8)') evals(i:i + j)
656 CASE (1)
657 WRITE (scr, '(1X,2F16.8)') evals(i:i + j)
658 CASE (0)
659 WRITE (scr, '(1X,1F16.8)') evals(i:i + j)
660 END SELECT
661 END DO
662 END IF
663 END IF
664
665 CALL cp_fm_release(weighted_vectors)
666 CALL cp_fm_release(h_block)
667 IF (compute_evecs) THEN
668 CALL cp_fm_release(e_vectors)
669 END IF
670
671 DEALLOCATE (evals)
672
673 END IF
674
675 CALL timestop(handle)
676
677 END SUBROUTINE subspace_eigenvalues_ks_fm
678
679! **************************************************************************************************
680!> \brief ...
681!> \param orbitals ...
682!> \param ks_matrix ...
683!> \param evals_arg ...
684!> \param ionode ...
685!> \param scr ...
686!> \param do_rotation ...
687!> \param co_rotate ...
688!> \param para_env ...
689!> \param blacs_env ...
690! **************************************************************************************************
691 SUBROUTINE subspace_eigenvalues_ks_dbcsr(orbitals, ks_matrix, evals_arg, ionode, scr, &
692 do_rotation, co_rotate, para_env, blacs_env)
693
694 TYPE(dbcsr_type), POINTER :: orbitals, ks_matrix
695 REAL(KIND=dp), DIMENSION(:), OPTIONAL :: evals_arg
696 LOGICAL, INTENT(IN), OPTIONAL :: ionode
697 INTEGER, INTENT(IN), OPTIONAL :: scr
698 LOGICAL, INTENT(IN), OPTIONAL :: do_rotation
699 TYPE(dbcsr_type), OPTIONAL, POINTER :: co_rotate
700 TYPE(mp_para_env_type), POINTER :: para_env
701 TYPE(cp_blacs_env_type), POINTER :: blacs_env
702
703 CHARACTER(LEN=*), PARAMETER :: routineN = 'subspace_eigenvalues_ks_dbcsr'
704
705 INTEGER :: handle, i, j, ncol_global, nrow_global
706 LOGICAL :: compute_evecs, do_rotation_local
707 REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: evals
708 TYPE(dbcsr_type), POINTER :: e_vectors, h_block, weighted_vectors
709
710 CALL timeset(routinen, handle)
711
712 do_rotation_local = .true.
713 IF (PRESENT(do_rotation)) do_rotation_local = do_rotation
714
715 NULLIFY (e_vectors, h_block, weighted_vectors)
716
717 CALL dbcsr_get_info(matrix=orbitals, &
718 nfullcols_total=ncol_global, &
719 nfullrows_total=nrow_global)
720
721 IF (do_rotation_local) THEN
722 compute_evecs = .true.
723 ELSE
724 ! this would be the logical choice if syevx computing only evals were faster than syevd computing evecs and evals.
725 compute_evecs = .false.
726 ! this is not the case, so lets compute evecs always
727 compute_evecs = .true.
728 END IF
729
730 IF (ncol_global > 0) THEN
731
732 ALLOCATE (evals(ncol_global))
733
734 CALL dbcsr_init_p(weighted_vectors)
735 CALL dbcsr_copy(weighted_vectors, orbitals, name="weighted_vectors")
736
737 CALL dbcsr_init_p(h_block)
738 CALL cp_dbcsr_m_by_n_from_template(h_block, template=orbitals, m=ncol_global, n=ncol_global, &
739 sym=dbcsr_type_no_symmetry)
740
741!!!!!!!!!!!!!!XXXXXXXXXXXXXXXXXXX!!!!!!!!!!!!!
742 IF (compute_evecs) THEN
743 CALL dbcsr_init_p(e_vectors)
744 CALL cp_dbcsr_m_by_n_from_template(e_vectors, template=orbitals, m=ncol_global, n=ncol_global, &
745 sym=dbcsr_type_no_symmetry)
746 END IF
747
748 ! h subblock and diag
749 CALL dbcsr_multiply('N', 'N', 1.0_dp, ks_matrix, orbitals, &
750 0.0_dp, weighted_vectors)
751 !CALL cp_dbcsr_sm_fm_multiply(ks_matrix,orbitals,weighted_vectors, ncol_global)
752
753 CALL dbcsr_multiply('T', 'N', 1.0_dp, orbitals, weighted_vectors, 0.0_dp, h_block)
754 !CALL parallel_gemm('T','N',ncol_global,ncol_global,nrow_global,1.0_dp, &
755 ! orbitals,weighted_vectors,0.0_dp,h_block)
756
757 ! if eigenvectors are required, go for syevd, otherwise only compute eigenvalues
758 IF (compute_evecs) THEN
759 CALL cp_dbcsr_syevd(h_block, e_vectors, evals, para_env=para_env, blacs_env=blacs_env)
760 ELSE
761 CALL cp_dbcsr_syevx(h_block, eigenvalues=evals, para_env=para_env, blacs_env=blacs_env)
762 END IF
763
764 ! rotate the orbitals
765 IF (do_rotation_local) THEN
766 CALL dbcsr_multiply('N', 'N', 1.0_dp, orbitals, e_vectors, 0.0_dp, weighted_vectors)
767 !CALL parallel_gemm('N','N',nrow_global,ncol_global,ncol_global,1.0_dp, &
768 ! orbitals,e_vectors,0.0_dp,weighted_vectors)
769 CALL dbcsr_copy(orbitals, weighted_vectors)
770 !CALL cp_fm_to_fm(weighted_vectors,orbitals)
771 IF (PRESENT(co_rotate)) THEN
772 IF (ASSOCIATED(co_rotate)) THEN
773 CALL dbcsr_multiply('N', 'N', 1.0_dp, co_rotate, e_vectors, 0.0_dp, weighted_vectors)
774 !CALL parallel_gemm('N','N',nrow_global,ncol_global,ncol_global,1.0_dp, &
775 ! co_rotate,e_vectors,0.0_dp,weighted_vectors)
776 CALL dbcsr_copy(co_rotate, weighted_vectors)
777 !CALL cp_fm_to_fm(weighted_vectors,co_rotate)
778 END IF
779 END IF
780 END IF
781
782 ! give output
783 IF (PRESENT(evals_arg)) THEN
784 evals_arg(:) = evals(:)
785 END IF
786
787 IF (PRESENT(ionode) .OR. PRESENT(scr)) THEN
788 IF (.NOT. PRESENT(ionode)) cpabort("IONODE?")
789 IF (.NOT. PRESENT(scr)) cpabort("SCR?")
790 IF (ionode) THEN
791 DO i = 1, ncol_global, 4
792 j = min(3, ncol_global - i)
793 SELECT CASE (j)
794 CASE (3)
795 WRITE (scr, '(1X,4F16.8)') evals(i:i + j)
796 CASE (2)
797 WRITE (scr, '(1X,3F16.8)') evals(i:i + j)
798 CASE (1)
799 WRITE (scr, '(1X,2F16.8)') evals(i:i + j)
800 CASE (0)
801 WRITE (scr, '(1X,1F16.8)') evals(i:i + j)
802 END SELECT
803 END DO
804 END IF
805 END IF
806
807 CALL dbcsr_release_p(weighted_vectors)
808 CALL dbcsr_release_p(h_block)
809 IF (compute_evecs) THEN
810 CALL dbcsr_release_p(e_vectors)
811 END IF
812
813 DEALLOCATE (evals)
814
815 END IF
816
817 CALL timestop(handle)
818
819 END SUBROUTINE subspace_eigenvalues_ks_dbcsr
820
821! computes the effective orthonormality of a set of mos given an s-matrix
822! orthonormality is the max deviation from unity of the C^T S C
823! **************************************************************************************************
824!> \brief ...
825!> \param orthonormality ...
826!> \param mo_array ...
827!> \param matrix_s ...
828! **************************************************************************************************
829 SUBROUTINE calculate_orthonormality(orthonormality, mo_array, matrix_s)
830 REAL(kind=dp) :: orthonormality
831 TYPE(mo_set_type), DIMENSION(:), INTENT(IN) :: mo_array
832 TYPE(dbcsr_type), OPTIONAL, POINTER :: matrix_s
833
834 CHARACTER(LEN=*), PARAMETER :: routinen = 'calculate_orthonormality'
835
836 INTEGER :: handle, i, ispin, j, k, n, ncol_local, &
837 nrow_local, nspin
838 INTEGER, DIMENSION(:), POINTER :: col_indices, row_indices
839 REAL(kind=dp) :: alpha, max_alpha
840 TYPE(cp_fm_struct_type), POINTER :: tmp_fm_struct
841 TYPE(cp_fm_type) :: overlap, svec
842
843 NULLIFY (tmp_fm_struct)
844
845 CALL timeset(routinen, handle)
846
847 nspin = SIZE(mo_array)
848 max_alpha = 0.0_dp
849
850 DO ispin = 1, nspin
851 IF (PRESENT(matrix_s)) THEN
852 ! get S*C
853 CALL cp_fm_create(svec, mo_array(ispin)%mo_coeff%matrix_struct)
854 CALL cp_fm_get_info(mo_array(ispin)%mo_coeff, &
855 nrow_global=n, ncol_global=k)
856 CALL cp_dbcsr_sm_fm_multiply(matrix_s, mo_array(ispin)%mo_coeff, &
857 svec, k)
858 ! get C^T (S*C)
859 CALL cp_fm_struct_create(tmp_fm_struct, nrow_global=k, ncol_global=k, &
860 para_env=mo_array(ispin)%mo_coeff%matrix_struct%para_env, &
861 context=mo_array(ispin)%mo_coeff%matrix_struct%context)
862 CALL cp_fm_create(overlap, tmp_fm_struct)
863 CALL cp_fm_struct_release(tmp_fm_struct)
864 CALL parallel_gemm('T', 'N', k, k, n, 1.0_dp, mo_array(ispin)%mo_coeff, &
865 svec, 0.0_dp, overlap)
866 CALL cp_fm_release(svec)
867 ELSE
868 ! orthogonal basis C^T C
869 CALL cp_fm_get_info(mo_array(ispin)%mo_coeff, &
870 nrow_global=n, ncol_global=k)
871 CALL cp_fm_struct_create(tmp_fm_struct, nrow_global=k, ncol_global=k, &
872 para_env=mo_array(ispin)%mo_coeff%matrix_struct%para_env, &
873 context=mo_array(ispin)%mo_coeff%matrix_struct%context)
874 CALL cp_fm_create(overlap, tmp_fm_struct)
875 CALL cp_fm_struct_release(tmp_fm_struct)
876 CALL parallel_gemm('T', 'N', k, k, n, 1.0_dp, mo_array(ispin)%mo_coeff, &
877 mo_array(ispin)%mo_coeff, 0.0_dp, overlap)
878 END IF
879 CALL cp_fm_get_info(overlap, nrow_local=nrow_local, ncol_local=ncol_local, &
880 row_indices=row_indices, col_indices=col_indices)
881 DO i = 1, nrow_local
882 DO j = 1, ncol_local
883 alpha = overlap%local_data(i, j)
884 IF (row_indices(i) == col_indices(j)) alpha = alpha - 1.0_dp
885 max_alpha = max(max_alpha, abs(alpha))
886 END DO
887 END DO
888 CALL cp_fm_release(overlap)
889 END DO
890 CALL mo_array(1)%mo_coeff%matrix_struct%para_env%max(max_alpha)
891 orthonormality = max_alpha
892 ! write(*,*) "max deviation from orthonormalization ",orthonormality
893
894 CALL timestop(handle)
895
896 END SUBROUTINE calculate_orthonormality
897
898! computes the minimum/maximum magnitudes of C^T C. This could be useful
899! to detect problems in the case of nearly singular overlap matrices.
900! in this case, we expect the ratio of min/max to be large
901! this routine is only similar to mo_orthonormality if S==1
902! **************************************************************************************************
903!> \brief ...
904!> \param mo_array ...
905!> \param mo_mag_min ...
906!> \param mo_mag_max ...
907! **************************************************************************************************
908 SUBROUTINE calculate_magnitude(mo_array, mo_mag_min, mo_mag_max)
909 TYPE(mo_set_type), DIMENSION(:), INTENT(IN) :: mo_array
910 REAL(kind=dp) :: mo_mag_min, mo_mag_max
911
912 CHARACTER(LEN=*), PARAMETER :: routinen = 'calculate_magnitude'
913
914 INTEGER :: handle, ispin, k, n, nspin
915 REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: evals
916 TYPE(cp_fm_struct_type), POINTER :: tmp_fm_struct
917 TYPE(cp_fm_type) :: evecs, overlap
918
919 NULLIFY (tmp_fm_struct)
920
921 CALL timeset(routinen, handle)
922
923 nspin = SIZE(mo_array)
924 mo_mag_min = huge(0.0_dp)
925 mo_mag_max = -huge(0.0_dp)
926 DO ispin = 1, nspin
927 CALL cp_fm_get_info(mo_array(ispin)%mo_coeff, &
928 nrow_global=n, ncol_global=k)
929 ALLOCATE (evals(k))
930 CALL cp_fm_struct_create(tmp_fm_struct, nrow_global=k, ncol_global=k, &
931 para_env=mo_array(ispin)%mo_coeff%matrix_struct%para_env, &
932 context=mo_array(ispin)%mo_coeff%matrix_struct%context)
933 CALL cp_fm_create(overlap, tmp_fm_struct)
934 CALL cp_fm_create(evecs, tmp_fm_struct)
935 CALL cp_fm_struct_release(tmp_fm_struct)
936 CALL parallel_gemm('T', 'N', k, k, n, 1.0_dp, mo_array(ispin)%mo_coeff, &
937 mo_array(ispin)%mo_coeff, 0.0_dp, overlap)
938 CALL choose_eigv_solver(overlap, evecs, evals)
939 mo_mag_min = min(minval(evals), mo_mag_min)
940 mo_mag_max = max(maxval(evals), mo_mag_max)
941 CALL cp_fm_release(overlap)
942 CALL cp_fm_release(evecs)
943 DEALLOCATE (evals)
944 END DO
945 CALL timestop(handle)
946
947 END SUBROUTINE calculate_magnitude
948
949! **************************************************************************************************
950!> \brief Calculate KS eigenvalues starting from OF MOS
951!> \param mos ...
952!> \param nspins ...
953!> \param ks_rmpv ...
954!> \param scf_control ...
955!> \param mo_derivs ...
956!> \param admm_env ...
957!> \param hairy_probes ...
958!> \param probe ...
959!> \par History
960!> 02.2013 moved from qs_scf_post_gpw
961!>
962! **************************************************************************************************
963 SUBROUTINE make_mo_eig(mos, nspins, ks_rmpv, scf_control, mo_derivs, admm_env, &
964 hairy_probes, probe)
965
966 TYPE(mo_set_type), DIMENSION(:), INTENT(INOUT) :: mos
967 INTEGER, INTENT(IN) :: nspins
968 TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: ks_rmpv
969 TYPE(scf_control_type), POINTER :: scf_control
970 TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: mo_derivs
971 TYPE(admm_type), OPTIONAL, POINTER :: admm_env
972 LOGICAL, OPTIONAL :: hairy_probes
973 TYPE(hairy_probes_type), DIMENSION(:), OPTIONAL, &
974 POINTER :: probe
975
976 CHARACTER(LEN=*), PARAMETER :: routinen = 'make_mo_eig'
977
978 INTEGER :: handle, homo, ispin, nmo, output_unit
979 REAL(kind=dp), DIMENSION(:), POINTER :: mo_eigenvalues
980 TYPE(cp_fm_type), POINTER :: mo_coeff
981 TYPE(dbcsr_type), POINTER :: mo_coeff_deriv
982
983 CALL timeset(routinen, handle)
984
985 NULLIFY (mo_coeff_deriv, mo_coeff, mo_eigenvalues)
986 output_unit = cp_logger_get_default_io_unit()
987
988 DO ispin = 1, nspins
989 CALL get_mo_set(mo_set=mos(ispin), mo_coeff=mo_coeff, &
990 eigenvalues=mo_eigenvalues, homo=homo, nmo=nmo)
991 IF (output_unit > 0) WRITE (output_unit, *) " "
992 IF (output_unit > 0) WRITE (output_unit, *) " Eigenvalues of the occupied subspace spin ", ispin
993 ! IF (homo /= nmo) THEN
994 ! IF (output_unit>0) WRITE(output_unit,*)" and ",nmo-homo," added MO eigenvalues"
995 ! END IF
996 IF (output_unit > 0) WRITE (output_unit, *) "---------------------------------------------"
997
998 IF (scf_control%use_ot .AND. &
999 .NOT. (any(scf_control%added_mos > 0) .AND. .NOT. scf_control%smear%do_smear)) THEN
1000 ! Also rotate the OT derivs, since they are needed for force calculations
1001 IF (ASSOCIATED(mo_derivs)) THEN
1002 mo_coeff_deriv => mo_derivs(ispin)%matrix
1003 ELSE
1004 mo_coeff_deriv => null()
1005 END IF
1006
1007 ! If we do ADMM, we add have to modify the Kohn-Sham matrix
1008 IF (PRESENT(admm_env)) THEN
1009 CALL admm_correct_for_eigenvalues(ispin, admm_env, ks_rmpv(ispin)%matrix)
1010 END IF
1011
1012 CALL calculate_subspace_eigenvalues(mo_coeff, ks_rmpv(ispin)%matrix, mo_eigenvalues, &
1013 scr=output_unit, ionode=output_unit > 0, do_rotation=.true., &
1014 co_rotate_dbcsr=mo_coeff_deriv)
1015
1016 ! If we do ADMM, we restore the original Kohn-Sham matrix
1017 IF (PRESENT(admm_env)) THEN
1018 CALL admm_uncorrect_for_eigenvalues(ispin, admm_env, ks_rmpv(ispin)%matrix)
1019 END IF
1020 ELSE
1021 IF (output_unit > 0) WRITE (output_unit, '(4(1X,1F16.8))') mo_eigenvalues(1:homo)
1022 END IF
1023 IF (.NOT. scf_control%diagonalization%mom) THEN
1024 IF (PRESENT(hairy_probes) .EQV. .true.) THEN
1025 scf_control%smear%do_smear = .false.
1026 CALL set_mo_occupation(mo_set=mos(ispin), &
1027 smear=scf_control%smear, &
1028 probe=probe)
1029 ELSE
1030 CALL set_mo_occupation(mo_set=mos(ispin), smear=scf_control%smear)
1031 END IF
1032 END IF
1033 IF (output_unit > 0) WRITE (output_unit, '(T2,A,F12.6)') &
1034 "Fermi Energy [eV] :", mos(ispin)%mu*evolt
1035 END DO
1036
1037 CALL timestop(handle)
1038
1039 END SUBROUTINE make_mo_eig
1040
1041! **************************************************************************************************
1042!> \brief Orbital energies <phi_i|F|phi_i> of a set of orbitals and the largest off-diagonal Fock
1043!> matrix element max_{i/=j} |<phi_i|F|phi_j>| within the set. Unlike
1044!> calculate_subspace_eigenvalues, the orbitals are not rotated and the energies belong to
1045!> the given orbitals. The orbitals are canonical if the off-diagonal elements vanish.
1046!> \param orbitals S-orthonormal orbitals (columns)
1047!> \param ks_matrix Kohn-Sham or Fock matrix
1048!> \param orbital_energies diagonal elements; filled up to MIN(SIZE, number of orbitals)
1049!> \param max_offdiag largest off-diagonal element in absolute value
1050! **************************************************************************************************
1051 SUBROUTINE calculate_orbital_fock_elements(orbitals, ks_matrix, orbital_energies, max_offdiag)
1052 TYPE(cp_fm_type), INTENT(IN) :: orbitals
1053 TYPE(dbcsr_type), POINTER :: ks_matrix
1054 REAL(kind=dp), DIMENSION(:), INTENT(OUT) :: orbital_energies
1055 REAL(kind=dp), INTENT(OUT) :: max_offdiag
1056
1057 CHARACTER(LEN=*), PARAMETER :: routinen = 'calculate_orbital_fock_elements'
1058
1059 INTEGER :: handle, i, ncol_global, nrow_global
1060 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :) :: fock
1061 TYPE(cp_fm_struct_type), POINTER :: fm_struct_tmp
1062 TYPE(cp_fm_type) :: h_block, weighted_vectors
1063
1064 CALL timeset(routinen, handle)
1065
1066 orbital_energies = 0.0_dp
1067 max_offdiag = 0.0_dp
1068 CALL cp_fm_get_info(matrix=orbitals, ncol_global=ncol_global, nrow_global=nrow_global)
1069 IF (ncol_global > 0) THEN
1070 NULLIFY (fm_struct_tmp)
1071 CALL cp_fm_create(weighted_vectors, orbitals%matrix_struct, "weighted_vectors")
1072 CALL cp_fm_struct_create(fm_struct_tmp, nrow_global=ncol_global, ncol_global=ncol_global, &
1073 para_env=orbitals%matrix_struct%para_env, &
1074 context=orbitals%matrix_struct%context)
1075 CALL cp_fm_create(h_block, fm_struct_tmp, name="h block")
1076 CALL cp_fm_struct_release(fm_struct_tmp)
1077
1078 CALL cp_dbcsr_sm_fm_multiply(ks_matrix, orbitals, weighted_vectors, ncol_global)
1079 CALL parallel_gemm('T', 'N', ncol_global, ncol_global, nrow_global, 1.0_dp, &
1080 orbitals, weighted_vectors, 0.0_dp, h_block)
1081
1082 ALLOCATE (fock(ncol_global, ncol_global))
1083 CALL cp_fm_get_submatrix(h_block, fock)
1084 DO i = 1, min(ncol_global, SIZE(orbital_energies))
1085 orbital_energies(i) = fock(i, i)
1086 END DO
1087 DO i = 1, ncol_global
1088 fock(i, i) = 0.0_dp
1089 END DO
1090 max_offdiag = maxval(abs(fock))
1091 DEALLOCATE (fock)
1092
1093 CALL cp_fm_release(weighted_vectors)
1094 CALL cp_fm_release(h_block)
1095 END IF
1096
1097 CALL timestop(handle)
1098
1099 END SUBROUTINE calculate_orbital_fock_elements
1100
1101! **************************************************************************************************
1102!> \brief Rotates the orbitals of an MO set into canonical orbitals (eigenvectors of the Fock
1103!> matrix in the space of the orbitals), separately within the occupied and within the
1104!> unoccupied orbitals, so that the occupied space (the determinant) is unchanged.
1105!> The orbitals are rotated in place: the eigenvalues, the DBCSR copy of the coefficients
1106!> (if used) and the orbital derivatives (if given) are rotated consistently, C' = C U and
1107!> D' = D U. For positive eps_canonical, nothing is done if no off-diagonal Fock
1108!> matrix element exceeds the threshold. Zero also sorts already diagonal blocks.
1109!> Requires integer occupations (0 or the maximum occupation); fractionally occupied
1110!> orbitals and ROKS orbitals must not be mixed this way.
1111!> \param mo_set the MO set (e.g. the SCF orbitals after an OT optimization)
1112!> \param ks_matrix Kohn-Sham or Fock matrix
1113!> \param eps_canonical largest off-diagonal Fock element accepted as canonical; zero forces a Ritz rotation
1114!> \param rotated whether the orbitals were rotated
1115!> \param max_offdiag largest off-diagonal Fock matrix element before the rotation
1116!> \param mo_derivs derivatives of the energy with respect to the orbitals (OT), rotated as well
1117! **************************************************************************************************
1118 SUBROUTINE make_mo_set_canonical(mo_set, ks_matrix, eps_canonical, rotated, max_offdiag, mo_derivs)
1119 TYPE(mo_set_type), INTENT(INOUT) :: mo_set
1120 TYPE(dbcsr_type), POINTER :: ks_matrix
1121 REAL(kind=dp), INTENT(IN) :: eps_canonical
1122 LOGICAL, INTENT(OUT) :: rotated
1123 REAL(kind=dp), INTENT(OUT) :: max_offdiag
1124 TYPE(dbcsr_type), OPTIONAL, POINTER :: mo_derivs
1125
1126 CHARACTER(LEN=*), PARAMETER :: routinen = 'make_mo_set_canonical'
1127 REAL(kind=dp), PARAMETER :: eps_occ = 1.0e-10_dp
1128
1129 INTEGER :: first, handle, i, iblock, n, nmo, nocc, &
1130 nrow_global
1131 LOGICAL :: do_derivs
1132 REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: energies
1133 TYPE(cp_fm_struct_type), POINTER :: fm_struct_tmp
1134 TYPE(cp_fm_type) :: block_coeff, block_derivs, derivs
1135
1136 CALL timeset(routinen, handle)
1137
1138 rotated = .false.
1139 nmo = mo_set%nmo
1140 IF (mo_set%use_mo_coeff_b) CALL copy_dbcsr_to_fm(mo_set%mo_coeff_b, mo_set%mo_coeff)
1141 ALLOCATE (energies(nmo))
1142 CALL calculate_orbital_fock_elements(mo_set%mo_coeff, ks_matrix, energies, max_offdiag)
1143 DEALLOCATE (energies)
1144
1145 IF (max_offdiag > eps_canonical .OR. eps_canonical <= 0.0_dp) THEN
1146 DO i = 1, nmo
1147 IF (abs(mo_set%occupation_numbers(i)) > eps_occ .AND. &
1148 abs(mo_set%occupation_numbers(i) - mo_set%maxocc) > eps_occ) THEN
1149 cpabort("Canonicalization of orbitals with fractional occupations is not possible")
1150 END IF
1151 END DO
1152 do_derivs = .false.
1153 IF (PRESENT(mo_derivs)) do_derivs = ASSOCIATED(mo_derivs)
1154 ! occupied orbitals: those with nonzero occupation, they come first
1155 nocc = count(mo_set%occupation_numbers(1:nmo) > eps_occ)
1156 CALL cp_fm_get_info(matrix=mo_set%mo_coeff, nrow_global=nrow_global)
1157 IF (do_derivs) THEN
1158 CALL cp_fm_create(derivs, mo_set%mo_coeff%matrix_struct, name="orbital derivatives")
1159 CALL copy_dbcsr_to_fm(mo_derivs, derivs)
1160 END IF
1161 DO iblock = 1, 2
1162 IF (iblock == 1) THEN
1163 first = 1
1164 n = nocc
1165 ELSE
1166 first = nocc + 1
1167 n = nmo - nocc
1168 END IF
1169 IF (n <= 0) cycle
1170 NULLIFY (fm_struct_tmp)
1171 CALL cp_fm_struct_create(fm_struct_tmp, nrow_global=nrow_global, ncol_global=n, &
1172 template_fmstruct=mo_set%mo_coeff%matrix_struct)
1173 CALL cp_fm_create(block_coeff, fm_struct_tmp, name="canonicalized orbitals")
1174 CALL cp_fm_to_fm(mo_set%mo_coeff, block_coeff, n, first, 1)
1175 IF (do_derivs) THEN
1176 CALL cp_fm_create(block_derivs, fm_struct_tmp, name="rotated orbital derivatives")
1177 CALL cp_fm_to_fm(derivs, block_derivs, n, first, 1)
1178 CALL calculate_subspace_eigenvalues(block_coeff, ks_matrix, &
1179 evals_arg=mo_set%eigenvalues(first:first + n - 1), &
1180 do_rotation=.true., co_rotate=block_derivs)
1181 CALL cp_fm_to_fm(block_derivs, derivs, n, 1, first)
1182 CALL cp_fm_release(block_derivs)
1183 ELSE
1184 CALL calculate_subspace_eigenvalues(block_coeff, ks_matrix, &
1185 evals_arg=mo_set%eigenvalues(first:first + n - 1), &
1186 do_rotation=.true.)
1187 END IF
1188 CALL cp_fm_struct_release(fm_struct_tmp)
1189 CALL cp_fm_to_fm(block_coeff, mo_set%mo_coeff, n, 1, first)
1190 CALL cp_fm_release(block_coeff)
1191 END DO
1192 IF (do_derivs) THEN
1193 CALL copy_fm_to_dbcsr(derivs, mo_derivs)
1194 CALL cp_fm_release(derivs)
1195 END IF
1196 IF (mo_set%use_mo_coeff_b) CALL copy_fm_to_dbcsr(mo_set%mo_coeff, mo_set%mo_coeff_b)
1197 rotated = .true.
1198 END IF
1199
1200 CALL timestop(handle)
1201
1202 END SUBROUTINE make_mo_set_canonical
1203
1204END MODULE qs_mo_methods
Types and set/get functions for auxiliary density matrix methods.
Definition admm_types.F:15
Contains methods used in the context of density fitting.
Definition admm_utils.F:15
subroutine, public admm_uncorrect_for_eigenvalues(ispin, admm_env, ks_matrix)
...
Definition admm_utils.F:127
subroutine, public admm_correct_for_eigenvalues(ispin, admm_env, ks_matrix)
...
Definition admm_utils.F:53
methods related to the blacs parallel environment
Basic linear algebra operations for complex full matrices.
subroutine, public cp_cfm_vectorsnorm(matrix, norm_array, squared)
find the norm of each column norm_{j}= sqrt( \sum_{i} A_{ij}*conjg(A_{ij}) ) Complex-valued mirror of...
subroutine, public cp_cfm_triangular_multiply(triangular_matrix, matrix_b, side, transa_tr, invert_tr, uplo_tr, unit_diag_tr, n_rows, n_cols, alpha)
Multiplies in place by a triangular matrix: matrix_b = alpha op(triangular_matrix) matrix_b or (if si...
various cholesky decomposition related routines
subroutine, public cp_cfm_cholesky_decompose(matrix, n, info_out)
Used to replace a symmetric positive definite matrix M with its Cholesky decomposition U: M = U^T * U...
Represents a complex full matrix distributed on many processors.
subroutine, public cp_cfm_release(matrix)
Releases a full matrix.
subroutine, public cp_fm_to_cfm(msourcer, msourcei, mtarget)
Construct a complex full matrix by taking its real and imaginary parts from two separate real-value f...
subroutine, public cp_cfm_create(matrix, matrix_struct, name, nrow, ncol, set_zero)
Creates a new full matrix with the given structure.
Defines control structures, which contain the parameters and the settings for the DFT-based calculati...
subroutine, public dbcsr_release_p(matrix)
...
subroutine, public dbcsr_copy(matrix_b, matrix_a, name, keep_sparsity, keep_imaginary)
...
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_get_info(matrix, nblkrows_total, nblkcols_total, nfullrows_total, nfullcols_total, nblkrows_local, nblkcols_local, nfullrows_local, nfullcols_local, my_prow, my_pcol, local_rows, local_cols, proc_row_dist, proc_col_dist, row_blk_size, col_blk_size, row_blk_offset, col_blk_offset, distribution, name, matrix_type, group)
...
subroutine, public dbcsr_init_p(matrix)
...
Interface to (sca)lapack for the Cholesky based procedures.
subroutine, public cp_dbcsr_syevx(matrix, eigenvectors, eigenvalues, neig, work_syevx, para_env, blacs_env)
compute eigenvalues and optionally eigenvectors of a real symmetric matrix using scalapack....
subroutine, public cp_dbcsr_syevd(matrix, eigenvectors, eigenvalues, para_env, blacs_env)
...
DBCSR operations in CP2K.
subroutine, public cp_dbcsr_sm_fm_multiply(matrix, fm_in, fm_out, ncol, alpha, beta)
multiply a dbcsr with a fm matrix
subroutine, public copy_dbcsr_to_fm(matrix, fm, plan, check_plan)
Copy a DBCSR matrix to a BLACS matrix.
subroutine, public cp_dbcsr_m_by_n_from_template(matrix, template, m, n, sym)
Utility function to create an arbitrary shaped dbcsr matrix with the same processor grid as the templ...
subroutine, public copy_fm_to_dbcsr(fm, matrix, keep_sparsity)
Copy a BLACS matrix to a dbcsr matrix.
Basic linear algebra operations for full matrices.
subroutine, public cp_fm_syrk(uplo, trans, k, alpha, matrix_a, ia, ja, beta, matrix_c)
performs a rank-k update of a symmetric matrix_c matrix_c = beta * matrix_c + alpha * matrix_a * tran...
subroutine, public cp_fm_triangular_multiply(triangular_matrix, matrix_b, side, transpose_tr, invert_tr, uplo_tr, unit_diag_tr, n_rows, n_cols, alpha)
multiplies in place by a triangular matrix: matrix_b = alpha op(triangular_matrix) matrix_b or (if si...
various cholesky decomposition related routines
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)
...
subroutine, public choose_eigv_solver(matrix, eigenvectors, eigenvalues, info)
Choose the Eigensolver depending on which library is available ELPA seems to be unstable for small sy...
Definition cp_fm_diag.F:440
subroutine, public cp_fm_syevx(matrix, eigenvectors, eigenvalues, neig, work_syevx)
compute eigenvalues and optionally eigenvectors of a real symmetric matrix using scalapack....
Definition cp_fm_diag.F:982
represent the structure of a full matrix
subroutine, public cp_fm_struct_create(fmstruct, para_env, context, nrow_global, ncol_global, nrow_block, ncol_block, descriptor, first_p_pos, local_leading_dimension, template_fmstruct, square_blocks, force_block)
allocates and initializes a full matrix structure
subroutine, public cp_fm_struct_release(fmstruct)
releases a full matrix structure
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_set_all(matrix, alpha, beta)
set all elements of a matrix to the same value, and optionally the diagonal to a different one
subroutine, public cp_fm_create(matrix, matrix_struct, name, nrow, ncol, set_zero)
creates a new full matrix with the given structure
subroutine, public cp_fm_vectorsnorm(matrix, norm_array, squared)
find the norm of each column norm_{j}= sqrt( \sum_{i} A_{ij}*A_{ij} )
subroutine, public cp_fm_get_submatrix(fm, target_m, start_row, start_col, n_rows, n_cols, transpose)
gets a submatrix of a full matrix op(target_m)(1:n_rows,1:n_cols) =fm(start_row:start_row+n_rows,...
subroutine, public cp_fm_init_random(matrix, ncol, start_col)
fills a matrix with random numbers
various routines to log and control the output. The idea is that decisions about where to log should ...
integer function, public cp_logger_get_default_io_unit(logger)
returns the unit nr for the ionode (-1 on all other processors) skips as well checks if the procs cal...
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
Interface to the message passing library MPI.
basic linear algebra operations for full matrixes
Definition of physical constants:
Definition physcon.F:68
real(kind=dp), parameter, public evolt
Definition physcon.F:189
collects routines that perform operations directly related to MOs
subroutine, public make_basis_simple(vmatrix, ncol)
given a set of vectors, return an orthogonal (C^T C == 1) set spanning the same space (notice,...
subroutine, public make_basis_lowdin(vmatrix, ncol, matrix_s)
return a set of S orthonormal vectors (C^T S C == 1) where a Loedwin transformation is applied to kee...
subroutine, public make_basis_cholesky(vmatrix, ncol, ortho)
return a set of S orthonormal vectors (C^T S C == 1) where the cholesky decomposed form of S is passe...
subroutine, public calculate_magnitude(mo_array, mo_mag_min, mo_mag_max)
...
subroutine, public make_mo_set_canonical(mo_set, ks_matrix, eps_canonical, rotated, max_offdiag, mo_derivs)
Rotates the orbitals of an MO set into canonical orbitals (eigenvectors of the Fock matrix in the spa...
subroutine, public calculate_orbital_fock_elements(orbitals, ks_matrix, orbital_energies, max_offdiag)
Orbital energies <phi_i|F|phi_i> of a set of orbitals and the largest off-diagonal Fock matrix elemen...
subroutine, public make_basis_sm(vmatrix, ncol, matrix_s)
returns an S-orthonormal basis v (v^T S v ==1)
subroutine, public make_mo_eig(mos, nspins, ks_rmpv, scf_control, mo_derivs, admm_env, hairy_probes, probe)
Calculate KS eigenvalues starting from OF MOS.
subroutine, public calculate_orthonormality(orthonormality, mo_array, matrix_s)
...
Set occupation of molecular orbitals.
Definition and initialisation of the mo data type.
Definition qs_mo_types.F:22
subroutine, public get_mo_set(mo_set, maxocc, homo, lfomo, nao, nelectron, n_el_f, nmo, eigenvalues, occupation_numbers, mo_coeff, mo_coeff_b, uniform_occupation, kts, mu, flexible_electron_count, cmo_coeff)
Get the components of a MO set data structure.
parameters that control an scf iteration
stores some data used in wavefunction fitting
Definition admm_types.F:121
represent a blacs multidimensional parallel environment (for the mpi corrispective see cp_paratypes/m...
Represent a complex full matrix.
keeps the information about the structure of a full matrix
represent a full matrix
stores all the informations relevant to an mpi environment