(git:0e652e7)
Loading...
Searching...
No Matches
ec_diag_solver.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 Routines for an energy correction on top of a Kohn-Sham calculation
10!> \par History
11!> 03.2026 created from energy_correction
12!> \author JGH
13! **************************************************************************************************
19 USE cp_dbcsr_api, ONLY: &
21 dbcsr_set, dbcsr_type, dbcsr_type_no_symmetry
25 USE cp_fm_diag, ONLY: cp_fm_geeig
29 USE cp_fm_types, ONLY: cp_fm_create,&
47 USE ec_methods, ONLY: ec_mos_init
52 ec_ot_gs,&
55 USE kinds, ONLY: dp
70 USE qs_kind_types, ONLY: get_qs_kind,&
75 USE qs_mo_types, ONLY: allocate_mo_set,&
81 USE qs_rho_types, ONLY: qs_rho_get,&
85#include "./base/base_uses.f90"
86
87 IMPLICIT NONE
88
89 PRIVATE
90
91 ! Global parameters
92
93 CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'ec_diag_solver'
94
96
97! **************************************************************************************************
98
99CONTAINS
100
101! **************************************************************************************************
102!> \brief Solve KS equation using diagonalization
103!> \param qs_env ...
104!> \param ec_env ...
105!> \param matrix_ks ...
106!> \param matrix_s ...
107!> \param matrix_p ...
108!> \param matrix_w ...
109!> \par History
110!> 03.2014 created [JGH]
111!> \author JGH
112! **************************************************************************************************
113 SUBROUTINE ec_diag_solver_gamma(qs_env, ec_env, matrix_ks, matrix_s, matrix_p, matrix_w)
114
115 TYPE(qs_environment_type), POINTER :: qs_env
116 TYPE(energy_correction_type) :: ec_env
117 TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_ks, matrix_s, matrix_p, matrix_w
118
119 CHARACTER(LEN=*), PARAMETER :: routinen = 'ec_diag_solver_gamma'
120
121 INTEGER :: handle, ispin, nmo(2), nsize, nspins
122 REAL(kind=dp) :: eps_filter, flexible_electron_count, &
123 focc(2), n_el_f
124 REAL(kind=dp), DIMENSION(:), POINTER :: eigvals
125 TYPE(cp_blacs_env_type), POINTER :: blacs_env
126 TYPE(cp_fm_struct_type), POINTER :: fm_struct
127 TYPE(cp_fm_type) :: fm_k, fm_s, fm_w
128 TYPE(cp_fm_type), POINTER :: fm_mos
129 TYPE(dbcsr_type), POINTER :: ref_matrix
130 TYPE(dft_control_type), POINTER :: dft_control
131 TYPE(mo_set_type), ALLOCATABLE, DIMENSION(:) :: moset
132 TYPE(mp_para_env_type), POINTER :: para_env
133
134 CALL timeset(routinen, handle)
135
136 CALL get_qs_env(qs_env=qs_env, dft_control=dft_control)
137 eps_filter = dft_control%qs_control%eps_filter_matrix
138 nspins = dft_control%nspins
139
140 nmo = 0
141 CALL get_qs_env(qs_env=qs_env, nelectron_spin=nmo)
142 focc = 1._dp
143 IF (nspins == 1) focc = 2._dp
144
145 CALL dbcsr_get_info(matrix_ks(1, 1)%matrix, nfullrows_total=nsize)
146
147 NULLIFY (blacs_env, para_env)
148 CALL get_qs_env(qs_env=qs_env, blacs_env=blacs_env, para_env=para_env)
149 NULLIFY (fm_struct)
150 CALL cp_fm_struct_create(fm_struct, context=blacs_env, nrow_global=nsize, &
151 ncol_global=nsize, para_env=para_env)
152 CALL cp_fm_create(fm_k, fm_struct)
153 CALL cp_fm_create(fm_s, fm_struct)
154 CALL cp_fm_create(fm_w, fm_struct)
155 CALL cp_fm_struct_release(fm_struct)
156
157 ! MO sets
158 flexible_electron_count = 0.0_dp
159 IF (ec_env%smear%do_smear) flexible_electron_count = 0.0001_dp
160
161 ALLOCATE (moset(nspins))
162 DO ispin = 1, nspins
163 n_el_f = nmo(ispin)
164 CALL allocate_mo_set(moset(ispin), nsize, nsize, nmo(ispin), n_el_f, &
165 focc(ispin), flexible_electron_count)
166 CALL init_mo_set(moset(ispin), fm_ref=fm_w, name="MO")
167 END DO
168
169 DO ispin = 1, nspins
170 ref_matrix => matrix_s(1, 1)%matrix
171 CALL copy_dbcsr_to_fm(ref_matrix, fm_s)
172 ref_matrix => matrix_ks(ispin, 1)%matrix
173 CALL copy_dbcsr_to_fm(ref_matrix, fm_k)
174 CALL get_mo_set(moset(ispin), eigenvalues=eigvals, mo_coeff=fm_mos)
175 CALL cp_fm_geeig(fm_k, fm_s, fm_mos, eigvals, fm_w)
176 END DO
177 !
178 CALL set_mo_occupation(moset, ec_env%smear)
179 !
180 DO ispin = 1, nspins
181 ec_env%ekTS = ec_env%ekTS + moset(ispin)%kTS
182 CALL calculate_density_matrix(moset(ispin), matrix_p(ispin, 1)%matrix)
183 CALL calculate_w_matrix(moset(ispin), matrix_w(ispin, 1)%matrix)
184 END DO
185 !
186 CALL cp_fm_release(fm_k)
187 CALL cp_fm_release(fm_s)
188 CALL cp_fm_release(fm_w)
189 DO ispin = 1, nspins
190 CALL deallocate_mo_set(moset(ispin))
191 END DO
192 DEALLOCATE (moset)
193
194 CALL timestop(handle)
195
196 END SUBROUTINE ec_diag_solver_gamma
197
198! **************************************************************************************************
199!> \brief Solve Kpoint-KS equation using diagonalization
200!> \param qs_env ...
201!> \param ec_env ...
202!> \param matrix_ks ...
203!> \param matrix_s ...
204!> \param matrix_p ...
205!> \param matrix_w ...
206!> \par History
207!> 03.2026 created [JGH]
208!> \author JGH
209! **************************************************************************************************
210 SUBROUTINE ec_diag_solver_kp(qs_env, ec_env, matrix_ks, matrix_s, matrix_p, matrix_w)
211
212 TYPE(qs_environment_type), POINTER :: qs_env
213 TYPE(energy_correction_type) :: ec_env
214 TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_ks, matrix_s, matrix_p, matrix_w
215
216 CHARACTER(LEN=*), PARAMETER :: routinen = 'ec_diag_solver_kp'
217
218 INTEGER :: handle, i, ispin, nsize
219 TYPE(cp_blacs_env_type), POINTER :: blacs_env
220 TYPE(cp_fm_struct_type), POINTER :: fm_struct
221 TYPE(cp_fm_type), DIMENSION(:), POINTER :: fmwork
222 TYPE(dbcsr_type), POINTER :: tempmat
223 TYPE(mo_set_type), DIMENSION(:, :), POINTER :: mos
224 TYPE(mp_para_env_type), POINTER :: para_env
225
226 CALL timeset(routinen, handle)
227
228 CALL dbcsr_get_info(matrix_ks(1, 1)%matrix, nfullrows_total=nsize)
229
230 NULLIFY (blacs_env, para_env)
231 CALL get_qs_env(qs_env=qs_env, blacs_env=blacs_env, para_env=para_env)
232 NULLIFY (fm_struct)
233 CALL cp_fm_struct_create(fm_struct, context=blacs_env, nrow_global=nsize, &
234 ncol_global=nsize, para_env=para_env)
235 ALLOCATE (fmwork(4))
236 DO i = 1, 4
237 CALL cp_fm_create(fmwork(i), fm_struct)
238 END DO
239 CALL cp_fm_struct_release(fm_struct)
240
241 ! Diagonalization
242 CALL diag_kp_basic(matrix_ks, matrix_s, ec_env%kpoints, fmwork)
243 ! MO occupations
244 CALL kpoint_set_mo_occupation(ec_env%kpoints, ec_env%smear)
245 ! density matrices
246 CALL kpoint_density_matrices(ec_env%kpoints)
247 CALL kpoint_density_matrices(ec_env%kpoints, .true.)
248 ! density matrices in real space
249 tempmat => matrix_s(1, 1)%matrix
250 CALL kpoint_density_transform(ec_env%kpoints, matrix_p, .false., tempmat, &
251 ec_env%sab_orb, fmwork)
252 CALL kpoint_density_transform(ec_env%kpoints, matrix_w, .true., tempmat, &
253 ec_env%sab_orb, fmwork)
254
255 ec_env%ekTS = 0.0_dp
256 mos => ec_env%kpoints%kp_env(1)%kpoint_env%mos
257 DO ispin = 1, SIZE(mos, 2)
258 ec_env%ekTS = ec_env%ekTS + mos(1, ispin)%kTS
259 END DO
260
261 CALL cp_fm_release(fmwork)
262
263 CALL timestop(handle)
264
265 END SUBROUTINE ec_diag_solver_kp
266
267! **************************************************************************************************
268!> \brief Use OT-diagonalziation to obtain density matrix from Harris Kohn-Sham matrix
269!> Initial guess of density matrix is either the atomic block initial guess from SCF
270!> or the ground-state density matrix. The latter only works if the same basis is used
271!>
272!> \param qs_env ...
273!> \param ec_env ...
274!> \param matrix_ks Harris Kohn-Sham matrix
275!> \param matrix_s Overlap matrix in Harris functional basis
276!> \param matrix_p Harris dentiy matrix, calculated here
277!> \param matrix_w Harris energy weighted density matrix, calculated here
278!>
279!> \par History
280!> 09.2020 created
281!> \author F.Belleflamme
282! **************************************************************************************************
283 SUBROUTINE ec_ot_diag_solver(qs_env, ec_env, matrix_ks, matrix_s, matrix_p, matrix_w)
284 TYPE(qs_environment_type), POINTER :: qs_env
285 TYPE(energy_correction_type), POINTER :: ec_env
286 TYPE(dbcsr_p_type), DIMENSION(:, :), INTENT(IN), &
287 POINTER :: matrix_ks, matrix_s
288 TYPE(dbcsr_p_type), DIMENSION(:, :), &
289 INTENT(INOUT), POINTER :: matrix_p, matrix_w
290
291 CHARACTER(len=*), PARAMETER :: routinen = 'ec_ot_diag_solver'
292
293 INTEGER :: handle, homo, ikind, iounit, ispin, &
294 max_iter, nao, nkind, nmo, nspins
295 INTEGER, DIMENSION(2) :: nelectron_spin
296 REAL(kind=dp), DIMENSION(:), POINTER :: eigenvalues
297 TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
298 TYPE(cp_blacs_env_type), POINTER :: blacs_env
299 TYPE(cp_fm_type) :: sv
300 TYPE(cp_fm_type), POINTER :: mo_coeff
301 TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: p_rmpv
302 TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: rho_ao
303 TYPE(dft_control_type), POINTER :: dft_control
304 TYPE(gto_basis_set_type), POINTER :: basis_set, harris_basis
305 TYPE(mo_set_type), DIMENSION(:), POINTER :: mos
306 TYPE(mp_para_env_type), POINTER :: para_env
307 TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
308 TYPE(preconditioner_type), POINTER :: local_preconditioner
309 TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
310 TYPE(qs_kind_type), POINTER :: qs_kind
311 TYPE(qs_rho_type), POINTER :: rho
312
313 CALL timeset(routinen, handle)
314
315 iounit = cp_logger_get_default_unit_nr(local=.false.)
316
317 cpassert(ASSOCIATED(qs_env))
318 cpassert(ASSOCIATED(ec_env))
319 cpassert(ASSOCIATED(matrix_ks))
320 cpassert(ASSOCIATED(matrix_s))
321 cpassert(ASSOCIATED(matrix_p))
322 cpassert(ASSOCIATED(matrix_w))
323
324 CALL get_qs_env(qs_env=qs_env, &
325 atomic_kind_set=atomic_kind_set, &
326 blacs_env=blacs_env, &
327 dft_control=dft_control, &
328 nelectron_spin=nelectron_spin, &
329 para_env=para_env, &
330 particle_set=particle_set, &
331 qs_kind_set=qs_kind_set)
332 nspins = dft_control%nspins
333
334 ! Maximum number of OT iterations for diagonalization
335 max_iter = 200
336
337 ! If linear scaling, need to allocate and init MO set
338 ! set it to qs_env%mos
339 IF (dft_control%qs_control%do_ls_scf) THEN
340 CALL ec_mos_init(qs_env, matrix_s(1, 1)%matrix)
341 END IF
342
343 CALL get_qs_env(qs_env, mos=mos)
344
345 ! Inital guess to use
346 NULLIFY (p_rmpv)
347
348 ! Using ether ground-state DM or ATOMIC GUESS requires
349 ! Harris functional to use the same basis set
350 CALL get_qs_env(qs_env, qs_kind_set=qs_kind_set, nkind=nkind)
351 CALL uppercase(ec_env%basis)
352 ! Harris basis only differs from ground-state basis if explicitly added
353 ! thus only two cases that need to be tested
354 ! 1) explicit Harris basis present?
355 IF (ec_env%basis == "HARRIS") THEN
356 DO ikind = 1, nkind
357 qs_kind => qs_kind_set(ikind)
358 ! Basis sets of ground-state
359 CALL get_qs_kind(qs_kind=qs_kind, basis_set=basis_set, basis_type="ORB")
360 ! Basis sets of energy correction
361 CALL get_qs_kind(qs_kind=qs_kind, basis_set=harris_basis, basis_type="HARRIS")
362
363 IF (basis_set%name /= harris_basis%name) THEN
364 cpabort("OT-Diag initial guess: Harris and ground state need to use the same basis")
365 END IF
366 END DO
367 END IF
368 ! 2) Harris uses MAOs
369 IF (ec_env%mao) THEN
370 cpabort("OT-Diag initial guess: not implemented for MAOs")
371 END IF
372
373 ! Initital guess obtained for OT Diagonalization
374 SELECT CASE (ec_env%ec_initial_guess)
375 CASE (ec_ot_atomic)
376
377 p_rmpv => matrix_p(:, 1)
378
379 CALL calculate_atomic_block_dm(p_rmpv, matrix_s(1, 1)%matrix, atomic_kind_set, qs_kind_set, &
380 nspins, nelectron_spin, iounit, para_env)
381
382 CASE (ec_ot_gs)
383
384 CALL get_qs_env(qs_env, rho=rho)
385 CALL qs_rho_get(rho, rho_ao_kp=rho_ao)
386 p_rmpv => rho_ao(:, 1)
387
388 CASE DEFAULT
389 cpabort("Unknown inital guess for OT-Diagonalization (Harris functional)")
390 END SELECT
391
392 DO ispin = 1, nspins
393 CALL get_mo_set(mo_set=mos(ispin), &
394 mo_coeff=mo_coeff, &
395 nmo=nmo, &
396 nao=nao, &
397 homo=homo)
398
399 ! Calculate first MOs
400 CALL cp_fm_set_all(mo_coeff, 0.0_dp)
401 CALL cp_fm_init_random(mo_coeff, nmo)
402
403 CALL cp_fm_create(sv, mo_coeff%matrix_struct, "SV")
404 ! multiply times PS
405 ! PS*C(:,1:nomo)+C(:,nomo+1:nmo) (nomo=NINT(nelectron/maxocc))
406 CALL cp_dbcsr_sm_fm_multiply(matrix_s(1, 1)%matrix, mo_coeff, sv, nmo)
407 CALL cp_dbcsr_sm_fm_multiply(p_rmpv(ispin)%matrix, sv, mo_coeff, homo)
408 CALL cp_fm_release(sv)
409 ! and ortho the result
410 ! If DFBT or SE, then needs has_unit_metrix option
411 CALL make_basis_sm(mo_coeff, nmo, matrix_s(1, 1)%matrix)
412 END DO
413
414 ! Preconditioner
415 NULLIFY (local_preconditioner)
416 ALLOCATE (local_preconditioner)
417 CALL init_preconditioner(local_preconditioner, para_env=para_env, &
418 blacs_env=blacs_env)
419 DO ispin = 1, nspins
420 CALL make_preconditioner(local_preconditioner, &
421 precon_type=ot_precond_full_single_inverse, &
422 solver_type=ot_precond_solver_default, &
423 matrix_h=matrix_ks(ispin, 1)%matrix, &
424 matrix_s=matrix_s(ispin, 1)%matrix, &
425 convert_precond_to_dbcsr=.true., &
426 mo_set=mos(ispin), energy_gap=0.2_dp)
427
428 CALL get_mo_set(mos(ispin), &
429 mo_coeff=mo_coeff, &
430 eigenvalues=eigenvalues, &
431 nmo=nmo, &
432 homo=homo)
433 CALL ot_eigensolver(matrix_h=matrix_ks(ispin, 1)%matrix, &
434 matrix_s=matrix_s(1, 1)%matrix, &
435 matrix_c_fm=mo_coeff, &
436 preconditioner=local_preconditioner, &
437 eps_gradient=ec_env%eps_default, &
438 iter_max=max_iter, &
439 silent=.false.)
440 CALL calculate_subspace_eigenvalues(mo_coeff, matrix_ks(ispin, 1)%matrix, &
441 evals_arg=eigenvalues, do_rotation=.true.)
442
443 ! Deallocate preconditioner
444 CALL destroy_preconditioner(local_preconditioner)
445 DEALLOCATE (local_preconditioner)
446
447 !fm->dbcsr
448 CALL copy_fm_to_dbcsr(mos(ispin)%mo_coeff, &
449 mos(ispin)%mo_coeff_b)
450 END DO
451
452 ! Calculate density matrix from MOs
453 DO ispin = 1, nspins
454 CALL calculate_density_matrix(mos(ispin), matrix_p(ispin, 1)%matrix)
455
456 CALL calculate_w_matrix(mos(ispin), matrix_w(ispin, 1)%matrix)
457 END DO
458
459 ! Get rid of MO environment again
460 IF (dft_control%qs_control%do_ls_scf) THEN
461 DO ispin = 1, nspins
462 CALL deallocate_mo_set(mos(ispin))
463 END DO
464 IF (ASSOCIATED(qs_env%mos)) THEN
465 DO ispin = 1, SIZE(qs_env%mos)
466 CALL deallocate_mo_set(qs_env%mos(ispin))
467 END DO
468 DEALLOCATE (qs_env%mos)
469 END IF
470 END IF
471
472 CALL timestop(handle)
473
474 END SUBROUTINE ec_ot_diag_solver
475
476! **************************************************************************************************
477!> \brief Solve the Harris functional by linear scaling density purification scheme,
478!> instead of the diagonalization performed in ec_diag_solver
479!>
480!> \param qs_env ...
481!> \param matrix_ks Harris Kohn-Sham matrix
482!> \param matrix_s Overlap matrix in Harris functional basis
483!> \par History
484!> 09.2020 created
485!> \author F.Belleflamme
486! **************************************************************************************************
487 SUBROUTINE ec_ls_init(qs_env, matrix_ks, matrix_s)
488 TYPE(qs_environment_type), POINTER :: qs_env
489 TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_ks, matrix_s
490
491 CHARACTER(len=*), PARAMETER :: routinen = 'ec_ls_init'
492
493 INTEGER :: handle, ispin, nspins
494 TYPE(dft_control_type), POINTER :: dft_control
495 TYPE(energy_correction_type), POINTER :: ec_env
496 TYPE(ls_scf_env_type), POINTER :: ls_env
497
498 CALL timeset(routinen, handle)
499
500 CALL get_qs_env(qs_env=qs_env, &
501 dft_control=dft_control, &
502 ec_env=ec_env)
503 nspins = dft_control%nspins
504 ls_env => ec_env%ls_env
505
506 ! create the matrix template for use in the ls procedures
507 CALL matrix_ls_create(matrix_ls=ls_env%matrix_s, matrix_qs=matrix_s(1, 1)%matrix, &
508 ls_mstruct=ls_env%ls_mstruct)
509
510 IF (ALLOCATED(ls_env%matrix_p)) THEN
511 DO ispin = 1, SIZE(ls_env%matrix_p)
512 CALL dbcsr_release(ls_env%matrix_p(ispin))
513 END DO
514 ELSE
515 ALLOCATE (ls_env%matrix_p(nspins))
516 END IF
517
518 DO ispin = 1, nspins
519 CALL dbcsr_create(ls_env%matrix_p(ispin), template=ls_env%matrix_s, &
520 matrix_type=dbcsr_type_no_symmetry)
521 END DO
522
523 ALLOCATE (ls_env%matrix_ks(nspins))
524 DO ispin = 1, nspins
525 CALL dbcsr_create(ls_env%matrix_ks(ispin), template=ls_env%matrix_s, &
526 matrix_type=dbcsr_type_no_symmetry)
527 END DO
528
529 ! Set up S matrix and needed functions of S
530 CALL ls_scf_init_matrix_s(matrix_s(1, 1)%matrix, ls_env)
531
532 ! Bring KS matrix from QS to LS form
533 ! EC KS-matrix already calculated
534 DO ispin = 1, nspins
535 CALL matrix_qs_to_ls(matrix_ls=ls_env%matrix_ks(ispin), &
536 matrix_qs=matrix_ks(ispin, 1)%matrix, &
537 ls_mstruct=ls_env%ls_mstruct, &
538 covariant=.true.)
539 END DO
540
541 CALL timestop(handle)
542
543 END SUBROUTINE ec_ls_init
544
545! **************************************************************************************************
546!> \brief Solve the Harris functional by linear scaling density purification scheme,
547!> instead of the diagonalization performed in ec_diag_solver
548!>
549!> \param qs_env ...
550!> \param matrix_p Harris dentiy matrix, calculated here
551!> \param matrix_w Harris energy weighted density matrix, calculated here
552!> \param ec_ls_method which purification scheme should be used
553!> \par History
554!> 12.2019 created [JGH]
555!> 08.2020 refactoring [fbelle]
556!> \author Fabian Belleflamme
557! **************************************************************************************************
558
559 SUBROUTINE ec_ls_solver(qs_env, matrix_p, matrix_w, ec_ls_method)
560 TYPE(qs_environment_type), POINTER :: qs_env
561 TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_p, matrix_w
562 INTEGER, INTENT(IN) :: ec_ls_method
563
564 CHARACTER(LEN=*), PARAMETER :: routinen = 'ec_ls_solver'
565
566 INTEGER :: handle, ispin, nelectron_spin_real, &
567 nspins
568 INTEGER, DIMENSION(2) :: nmo
569 TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: wmat
570 TYPE(dbcsr_type), ALLOCATABLE, DIMENSION(:) :: matrix_ks_deviation
571 TYPE(dft_control_type), POINTER :: dft_control
572 TYPE(energy_correction_type), POINTER :: ec_env
573 TYPE(ls_scf_env_type), POINTER :: ls_env
574 TYPE(mp_para_env_type), POINTER :: para_env
575
576 CALL timeset(routinen, handle)
577
578 NULLIFY (para_env)
579 CALL get_qs_env(qs_env, &
580 dft_control=dft_control, &
581 para_env=para_env)
582 nspins = dft_control%nspins
583 ec_env => qs_env%ec_env
584 ls_env => ec_env%ls_env
585
586 nmo = 0
587 CALL get_qs_env(qs_env=qs_env, nelectron_spin=nmo)
588 IF (nspins == 1) nmo(1) = nmo(1)/2
589 ls_env%homo_spin(:) = 0.0_dp
590 ls_env%lumo_spin(:) = 0.0_dp
591
592 ALLOCATE (matrix_ks_deviation(nspins))
593 DO ispin = 1, nspins
594 CALL dbcsr_create(matrix_ks_deviation(ispin), template=ls_env%matrix_ks(ispin))
595 CALL dbcsr_set(matrix_ks_deviation(ispin), 0.0_dp)
596 END DO
597
598 ! F = S^-1/2 * F * S^-1/2
599 IF (ls_env%has_s_preconditioner) THEN
600 DO ispin = 1, nspins
601 CALL apply_matrix_preconditioner(ls_env%matrix_ks(ispin), "forward", &
602 ls_env%matrix_bs_sqrt, ls_env%matrix_bs_sqrt_inv)
603
604 CALL dbcsr_filter(ls_env%matrix_ks(ispin), ls_env%eps_filter)
605 END DO
606 END IF
607
608 DO ispin = 1, nspins
609 nelectron_spin_real = ls_env%nelectron_spin(ispin)
610 IF (ls_env%nspins == 1) nelectron_spin_real = nelectron_spin_real/2
611
612 SELECT CASE (ec_ls_method)
613 CASE (ec_matrix_sign)
614 CALL density_matrix_sign(ls_env%matrix_p(ispin), &
615 ls_env%mu_spin(ispin), &
616 ls_env%fixed_mu, &
617 ls_env%sign_method, &
618 ls_env%sign_order, &
619 ls_env%matrix_ks(ispin), &
620 ls_env%matrix_s, &
621 ls_env%matrix_s_inv, &
622 nelectron_spin_real, &
623 ec_env%eps_default)
624
625 CASE (ec_matrix_trs4)
626 CALL density_matrix_trs4( &
627 ls_env%matrix_p(ispin), &
628 ls_env%matrix_ks(ispin), &
629 ls_env%matrix_s_sqrt_inv, &
630 nelectron_spin_real, &
631 ec_env%eps_default, &
632 ls_env%homo_spin(ispin), &
633 ls_env%lumo_spin(ispin), &
634 ls_env%mu_spin(ispin), &
635 matrix_ks_deviation=matrix_ks_deviation(ispin), &
636 dynamic_threshold=ls_env%dynamic_threshold, &
637 eps_lanczos=ls_env%eps_lanczos, &
638 max_iter_lanczos=ls_env%max_iter_lanczos)
639
640 CASE (ec_matrix_tc2)
641 CALL density_matrix_tc2( &
642 ls_env%matrix_p(ispin), &
643 ls_env%matrix_ks(ispin), &
644 ls_env%matrix_s_sqrt_inv, &
645 nelectron_spin_real, &
646 ec_env%eps_default, &
647 ls_env%homo_spin(ispin), &
648 ls_env%lumo_spin(ispin), &
649 non_monotonic=ls_env%non_monotonic, &
650 eps_lanczos=ls_env%eps_lanczos, &
651 max_iter_lanczos=ls_env%max_iter_lanczos)
652
653 END SELECT
654
655 END DO
656
657 ! de-orthonormalize
658 IF (ls_env%has_s_preconditioner) THEN
659 DO ispin = 1, nspins
660 ! P = S^-1/2 * P_tilde * S^-1/2 (forward)
661 CALL apply_matrix_preconditioner(ls_env%matrix_p(ispin), "forward", &
662 ls_env%matrix_bs_sqrt, ls_env%matrix_bs_sqrt_inv)
663
664 CALL dbcsr_filter(ls_env%matrix_p(ispin), ls_env%eps_filter)
665 END DO
666 END IF
667
668 ! Closed-shell
669 IF (nspins == 1) CALL dbcsr_scale(ls_env%matrix_p(1), 2.0_dp)
670
671 IF (ls_env%report_all_sparsities) CALL post_scf_sparsities(ls_env)
672
673 ! ls_scf_dm_to_ks
674 ! Density matrix from LS to EC
675 DO ispin = 1, nspins
676 CALL matrix_ls_to_qs(matrix_qs=matrix_p(ispin, 1)%matrix, &
677 matrix_ls=ls_env%matrix_p(ispin), &
678 ls_mstruct=ls_env%ls_mstruct, &
679 covariant=.false.)
680 END DO
681
682 wmat => matrix_w(:, 1)
683 CALL calculate_w_matrix_ls(wmat, ec_env%ls_env)
684
685 ! clean up
686 CALL dbcsr_release(ls_env%matrix_s)
687 IF (ls_env%has_s_preconditioner) THEN
688 CALL dbcsr_release(ls_env%matrix_bs_sqrt)
689 CALL dbcsr_release(ls_env%matrix_bs_sqrt_inv)
690 END IF
691 IF (ls_env%needs_s_inv) THEN
692 CALL dbcsr_release(ls_env%matrix_s_inv)
693 END IF
694 IF (ls_env%use_s_sqrt) THEN
695 CALL dbcsr_release(ls_env%matrix_s_sqrt)
696 CALL dbcsr_release(ls_env%matrix_s_sqrt_inv)
697 END IF
698
699 DO ispin = 1, SIZE(ls_env%matrix_ks)
700 CALL dbcsr_release(ls_env%matrix_ks(ispin))
701 END DO
702 DEALLOCATE (ls_env%matrix_ks)
703
704 DO ispin = 1, nspins
705 CALL dbcsr_release(matrix_ks_deviation(ispin))
706 END DO
707 DEALLOCATE (matrix_ks_deviation)
708
709 CALL timestop(handle)
710
711 END SUBROUTINE ec_ls_solver
712
713END MODULE ec_diag_solver
714
Define the atomic kind types and their sub types.
methods related to the blacs parallel environment
Defines control structures, which contain the parameters and the settings for the DFT-based calculati...
subroutine, public dbcsr_scale(matrix, alpha_scalar)
...
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_filter(matrix, eps)
...
subroutine, public dbcsr_set(matrix, alpha)
...
subroutine, public dbcsr_release(matrix)
...
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)
Copy a DBCSR matrix to a BLACS matrix.
subroutine, public copy_fm_to_dbcsr(fm, matrix, keep_sparsity)
Copy a BLACS matrix to a dbcsr matrix.
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_geeig(amatrix, bmatrix, eigenvectors, eigenvalues, work)
General Eigenvalue Problem AX = BXE Single option version: Cholesky decomposition of B.
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_create(matrix, matrix_struct, name, use_sp, nrow, ncol, set_zero)
creates a new full matrix with the given structure
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_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 ...
recursive integer function, public cp_logger_get_default_unit_nr(logger, local, skip_not_ionode)
asks the default unit number of the given logger. try to use cp_logger_get_unit_nr
lower level routines for linear scaling SCF
subroutine, public density_matrix_trs4(matrix_p, matrix_ks, matrix_s_sqrt_inv, nelectron, threshold, e_homo, e_lumo, e_mu, dynamic_threshold, matrix_ks_deviation, max_iter_lanczos, eps_lanczos, converged, iounit)
compute the density matrix using a trace-resetting algorithm
subroutine, public ls_scf_init_matrix_s(matrix_s, ls_scf_env)
initialize S matrix related properties (sqrt, inverse...) Might be factored-out since this seems comm...
subroutine, public apply_matrix_preconditioner(matrix, direction, matrix_bs_sqrt, matrix_bs_sqrt_inv)
apply a preconditioner either forward (precondition) inv(sqrt(bs)) * A * inv(sqrt(bs)) backward (rest...
subroutine, public density_matrix_sign(matrix_p, mu, fixed_mu, sign_method, sign_order, matrix_ks, matrix_s, matrix_s_inv, nelectron, threshold, sign_symmetric, submatrix_sign_method, matrix_s_sqrt_inv)
compute the density matrix with a trace that is close to nelectron. take a mu as input,...
subroutine, public density_matrix_tc2(matrix_p, matrix_ks, matrix_s_sqrt_inv, nelectron, threshold, e_homo, e_lumo, non_monotonic, eps_lanczos, max_iter_lanczos, iounit)
compute the density matrix using a non monotonic trace conserving algorithm based on SIAM DOI....
Routines for a linear scaling quickstep SCF run based on the density matrix, with a focus on the inte...
subroutine, public matrix_ls_to_qs(matrix_qs, matrix_ls, ls_mstruct, covariant, keep_sparsity)
second link to QS, copy a LS matrix to QS matrix used to isolate QS style matrices from LS style will...
subroutine, public matrix_ls_create(matrix_ls, matrix_qs, ls_mstruct)
create a matrix for use (and as a template) in ls based on a qs template
subroutine, public matrix_qs_to_ls(matrix_ls, matrix_qs, ls_mstruct, covariant)
first link to QS, copy a QS matrix to LS matrix used to isolate QS style matrices from LS style will ...
Types needed for a linear scaling quickstep SCF run based on the density matrix.
Routines for a linear scaling quickstep SCF run based on the density matrix.
Definition dm_ls_scf.F:15
subroutine, public post_scf_sparsities(ls_scf_env)
Report on the sparsities of various interesting matrices.
Definition dm_ls_scf.F:1066
subroutine, public calculate_w_matrix_ls(matrix_w, ls_scf_env)
Compute matrix_w as needed for the forces.
Definition dm_ls_scf.F:1223
Routines for an energy correction on top of a Kohn-Sham calculation.
subroutine, public ec_diag_solver_gamma(qs_env, ec_env, matrix_ks, matrix_s, matrix_p, matrix_w)
Solve KS equation using diagonalization.
subroutine, public ec_ot_diag_solver(qs_env, ec_env, matrix_ks, matrix_s, matrix_p, matrix_w)
Use OT-diagonalziation to obtain density matrix from Harris Kohn-Sham matrix Initial guess of density...
subroutine, public ec_diag_solver_kp(qs_env, ec_env, matrix_ks, matrix_s, matrix_p, matrix_w)
Solve Kpoint-KS equation using diagonalization.
subroutine, public ec_ls_init(qs_env, matrix_ks, matrix_s)
Solve the Harris functional by linear scaling density purification scheme, instead of the diagonaliza...
Types needed for a for a Energy Correction.
Routines used for Harris functional Kohn-Sham calculation.
Definition ec_methods.F:15
subroutine, public ec_mos_init(qs_env, matrix_s)
Allocate and initiate molecular orbitals environment.
Definition ec_methods.F:166
collects all constants needed in input so that they can be used without circular dependencies
integer, parameter, public ec_ot_atomic
integer, parameter, public ec_ot_gs
integer, parameter, public ot_precond_solver_default
integer, parameter, public ot_precond_full_single_inverse
integer, parameter, public ec_matrix_tc2
integer, parameter, public ec_matrix_trs4
integer, parameter, public ec_matrix_sign
integer, parameter, public ec_ls_solver
Defines the basic variable types.
Definition kinds.F:23
integer, parameter, public dp
Definition kinds.F:34
Routines needed for kpoint calculation.
subroutine, public kpoint_density_transform(kpoint, denmat, wtype, tempmat, sab_nl, fmwork, for_aux_fit, pmat_ext)
generate real space density matrices in DBCSR format
subroutine, public kpoint_density_matrices(kpoint, energy_weighted, for_aux_fit)
Calculate kpoint density matrices (rho(k), owned by kpoint groups)
subroutine, public kpoint_set_mo_occupation(kpoint, smear, probe)
Given the eigenvalues of all kpoints, calculates the occupation numbers.
Interface to the message passing library MPI.
Define the data structure for the particle information.
types of preconditioners
subroutine, public init_preconditioner(preconditioner_env, para_env, blacs_env)
...
subroutine, public destroy_preconditioner(preconditioner_env)
...
computes preconditioners, and implements methods to apply them currently used in qs_ot
subroutine, public make_preconditioner(preconditioner_env, precon_type, solver_type, matrix_h, matrix_s, matrix_t, mo_set, energy_gap, convert_precond_to_dbcsr, chol_type)
...
Routine to return block diagonal density matrix. Blocks correspond to the atomic densities.
subroutine, public calculate_atomic_block_dm(pmatrix, matrix_s, atomic_kind_set, qs_kind_set, nspin, nelectron_spin, ounit, para_env)
returns a block diagonal density matrix. Blocks correspond to the atomic densities.
collects routines that calculate density matrices
subroutine, public get_qs_env(qs_env, atomic_kind_set, qs_kind_set, cell, super_cell, cell_ref, use_ref_cell, kpoints, dft_control, mos, sab_orb, sab_all, qmmm, qmmm_periodic, mimic, sac_ae, sac_ppl, sac_lri, sap_ppnl, sab_vdw, sab_scp, sap_oce, sab_lrc, sab_se, sab_xtbe, sab_tbe, sab_core, sab_xb, sab_xtb_pp, sab_xtb_nonbond, sab_almo, sab_kp, sab_kp_nosym, sab_cneo, particle_set, energy, force, matrix_h, matrix_h_im, matrix_ks, matrix_ks_im, matrix_vxc, run_rtp, rtp, matrix_h_kp, matrix_h_im_kp, matrix_ks_kp, matrix_ks_im_kp, matrix_vxc_kp, kinetic_kp, matrix_s_kp, matrix_w_kp, matrix_s_ri_aux_kp, matrix_s, matrix_s_ri_aux, matrix_w, matrix_p_mp2, matrix_p_mp2_admm, rho, rho_xc, pw_env, ewald_env, ewald_pw, active_space, mpools, input, para_env, blacs_env, scf_control, rel_control, kinetic, qs_charges, vppl, xcint_weights, rho_core, rho_nlcc, rho_nlcc_g, ks_env, ks_qmmm_env, wf_history, scf_env, local_particles, local_molecules, distribution_2d, dbcsr_dist, molecule_kind_set, molecule_set, subsys, cp_subsys, oce, local_rho_set, rho_atom_set, task_list, task_list_soft, rho0_atom_set, rho0_mpole, rhoz_set, rhoz_cneo_set, ecoul_1c, rho0_s_rs, rho0_s_gs, rhoz_cneo_s_rs, rhoz_cneo_s_gs, do_kpoints, has_unit_metric, requires_mo_derivs, mo_derivs, mo_loc_history, nkind, natom, nelectron_total, nelectron_spin, efield, neighbor_list_id, linres_control, xas_env, virial, cp_ddapc_env, cp_ddapc_ewald, outer_scf_history, outer_scf_ihistory, x_data, et_coupling, dftb_potential, results, se_taper, se_store_int_env, se_nddo_mpole, se_nonbond_env, admm_env, lri_env, lri_density, exstate_env, ec_env, harris_env, dispersion_env, gcp_env, vee, rho_external, external_vxc, mask, mp2_env, bs_env, kg_env, wanniercentres, atprop, ls_scf_env, do_transport, transport_env, v_hartree_rspace, s_mstruct_changed, rho_changed, potential_changed, forces_up_to_date, mscfg_env, almo_scf_env, gradient_history, variable_history, embed_pot, spin_embed_pot, polar_env, mos_last_converged, eeq, rhs, do_rixs, tb_tblite)
Get the QUICKSTEP environment.
Define the quickstep kind type and their sub types.
subroutine, public get_qs_kind(qs_kind, basis_set, basis_type, ncgf, nsgf, all_potential, tnadd_potential, gth_potential, sgp_potential, upf_potential, cneo_potential, se_parameter, dftb_parameter, xtb_parameter, dftb3_param, zatom, zeff, elec_conf, mao, lmax_dftb, alpha_core_charge, ccore_charge, core_charge, core_charge_radius, paw_proj_set, paw_atom, hard_radius, hard0_radius, max_rad_local, covalent_radius, vdw_radius, gpw_type_forced, harmonics, max_iso_not0, max_s_harm, grid_atom, ngrid_ang, ngrid_rad, lmax_rho0, dft_plus_u_atom, l_of_dft_plus_u, n_of_dft_plus_u, u_minus_j, u_of_dft_plus_u, j_of_dft_plus_u, alpha_of_dft_plus_u, beta_of_dft_plus_u, j0_of_dft_plus_u, occupation_of_dft_plus_u, dispersion, bs_occupation, magnetization, no_optimize, addel, laddel, naddel, orbitals, max_scf, eps_scf, smear, u_ramping, u_minus_j_target, eps_u_ramping, init_u_ramping_each_scf, reltmat, ghost, monovalent, floating, name, element_symbol, pao_basis_size, pao_model_file, pao_potentials, pao_descriptors, nelec)
Get attributes of an atomic kind.
collects routines that perform operations directly related to MOs
subroutine, public make_basis_sm(vmatrix, ncol, matrix_s)
returns an S-orthonormal basis v (v^T S v ==1)
Set occupation of molecular orbitals.
Definition and initialisation of the mo data type.
Definition qs_mo_types.F:22
subroutine, public allocate_mo_set(mo_set, nao, nmo, nelectron, n_el_f, maxocc, flexible_electron_count)
Allocates a mo set and partially initializes it (nao,nmo,nelectron, and flexible_electron_count are v...
subroutine, public deallocate_mo_set(mo_set)
Deallocate a wavefunction data structure.
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)
Get the components of a MO set data structure.
subroutine, public init_mo_set(mo_set, fm_pool, fm_ref, fm_struct, name)
initializes an allocated mo_set. eigenvalues, mo_coeff, occupation_numbers are valid only after this ...
an eigen-space solver for the generalised symmetric eigenvalue problem for sparse matrices,...
subroutine, public ot_eigensolver(matrix_h, matrix_s, matrix_orthogonal_space_fm, matrix_c_fm, preconditioner, eps_gradient, iter_max, size_ortho_space, silent, ot_settings)
...
superstucture that hold various representations of the density and keeps track of which ones are vali...
subroutine, public qs_rho_get(rho_struct, rho_ao, rho_ao_im, rho_ao_kp, rho_ao_im_kp, rho_r, drho_r, rho_g, drho_g, tau_r, tau_g, rho_r_valid, drho_r_valid, rho_g_valid, drho_g_valid, tau_r_valid, tau_g_valid, tot_rho_r, tot_rho_g, rho_r_sccs, soft_valid, complex_rho_ao)
returns info about the density described by this object. If some representation is not available an e...
Different diagonalization schemes that can be used for the iterative solution of the eigenvalue probl...
subroutine, public diag_kp_basic(matrix_ks, matrix_s, kpoints, fmwork)
Kpoint diagonalization routine Transforms matrices to kpoint, distributes kpoint groups,...
Utilities for string manipulations.
elemental subroutine, public uppercase(string)
Convert all lower case characters in a string to upper case.
Provides all information about an atomic kind.
represent a blacs multidimensional parallel environment (for the mpi corrispective see cp_paratypes/m...
keeps the information about the structure of a full matrix
represent a full matrix
Contains information on the energy correction functional for KG.
stores all the informations relevant to an mpi environment
Provides all information about a quickstep kind.
keeps the density in various representations, keeping track of which ones are valid.