(git:4b1c2b1)
Loading...
Searching...
No Matches
qs_active_space_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 Determine active space Hamiltonian
10!> \par History
11!> 04.2016 created [JGH]
12!> \author JGH
13! **************************************************************************************************
15 USE admm_types, ONLY: admm_type, &
25 srules, &
36 USE cp_files, ONLY: close_file, &
43 USE cp_fm_types, ONLY: &
49 USE cp_output_handling, ONLY: &
54 USE cp_dbcsr_api, ONLY: &
60 USE erf_complex, ONLY: erfz_fast
62 USE input_constants, ONLY: &
74 USE iso_c_binding, ONLY: c_null_char
75 USE kinds, ONLY: default_path_length, &
77 dp, &
78 int_8
80 USE machine, ONLY: m_walltime, m_flush
81 USE mathlib, ONLY: diamat_all
82 USE mathconstants, ONLY: fourpi, twopi, pi, rootpi
85 USE message_passing, ONLY: mp_comm_type, &
89 USE mt_util, ONLY: mt0d
93 USE periodic_table, ONLY: ptable
94 USE physcon, ONLY: angstrom, bohr
99 USE pw_env_methods, ONLY: pw_env_create, &
101 USE pw_env_types, ONLY: pw_env_get, &
105 pw_multiply, &
107 pw_transfer, &
112 USE pw_poisson_types, ONLY: analytic0d, &
113 periodic3d, &
118 USE pw_pool_types, ONLY: &
120 USE pw_types, ONLY: &
123 USE qcschema, ONLY: qcschema_env_create, &
132 eri_type, &
143 USE qs_environment_types, ONLY: get_qs_env, &
146 USE qs_integrate_potential, ONLY: integrate_v_rspace
147 USE qs_kind_types, ONLY: qs_kind_type
150 USE qs_ks_types, ONLY: qs_ks_did_change, &
156 USE qs_mo_types, ONLY: allocate_mo_set, &
158 get_mo_set, &
159 init_mo_set, &
164 USE qs_rho_types, ONLY: qs_rho_get, &
166 USE qs_subsys_types, ONLY: qs_subsys_get, &
170#ifndef __NO_SOCKETS
171 USE sockets_interface, ONLY: accept_socket, &
172 close_socket, &
175 readbuffer, &
178#endif
183 USE util, ONLY: get_limit
184#include "./base/base_uses.f90"
185
186 IMPLICIT NONE
187 PRIVATE
188
189 CHARACTER(LEN=*), PARAMETER, PRIVATE :: moduleN = 'qs_active_space_methods'
190
191 PUBLIC :: active_space_main
192
193 TYPE, EXTENDS(eri_type_eri_element_func) :: eri_fcidump_print
194 INTEGER :: unit_nr = -1, bra_start = -1, ket_start = -1
195 INTEGER :: nalpha = 0, nbeta = 0
196 REAL(KIND=dp) :: determinant_energy = 0.0_dp
197 CONTAINS
198 PROCEDURE :: func => eri_fcidump_print_func
199 END TYPE eri_fcidump_print
200
201 TYPE, EXTENDS(eri_type_eri_element_func) :: eri_fcidump_checksum
202 INTEGER :: bra_start = 0, ket_start = 0
203 REAL(KIND=dp) :: checksum = 0.0_dp
204 CONTAINS
205 PROCEDURE, PASS :: set => eri_fcidump_set
206 PROCEDURE :: func => eri_fcidump_checksum_func
207 END TYPE eri_fcidump_checksum
208
209CONTAINS
210
211! **************************************************************************************************
212!> \brief Sets the starting indices of the bra and ket.
213!> \param this object reference
214!> \param bra_start starting index of the bra
215!> \param ket_start starting index of the ket
216! **************************************************************************************************
217 SUBROUTINE eri_fcidump_set(this, bra_start, ket_start)
218 CLASS(eri_fcidump_checksum) :: this
219 INTEGER, INTENT(IN) :: bra_start, ket_start
220
221 this%bra_start = bra_start
222 this%ket_start = ket_start
223 END SUBROUTINE eri_fcidump_set
224
225! **************************************************************************************************
226!> \brief Main method for determining the active space Hamiltonian
227!> \param qs_env ...
228! **************************************************************************************************
229 SUBROUTINE active_space_main(qs_env)
230 TYPE(qs_environment_type), POINTER :: qs_env
231
232 CHARACTER(LEN=*), PARAMETER :: routinen = 'active_space_main'
233
234 CHARACTER(LEN=10) :: cshell, lnam(5)
235 CHARACTER(LEN=default_path_length) :: qcschema_filename
236 CHARACTER(LEN=default_string_length) :: basis_type, kp_scheme
237 INTEGER :: as_solver, eri_method, eri_operator, eri_print, group_size, handle, i, iatom, &
238 ishell, isp, ispin, iw, j, jm, m, max_orb_ind, mselect, n1, n2, nao, natom, nel, &
239 nelec_active, nelec_inactive, nelec_total, nkp, nmo, nmo_active, nmo_available, &
240 nmo_inactive, nmo_inactive_remaining, nmo_occ, nmo_virtual, nn1, nn2, nrow_global, nspins
241 INTEGER, DIMENSION(5) :: nshell
242 INTEGER, DIMENSION(:), POINTER :: invals
243 LOGICAL :: do_ddapc, do_kpoints, ex_omega, &
244 ex_operator, ex_perd, ex_rcut, &
245 explicit, stop_after_print, store_wfn, &
246 use_real_wfn
247 REAL(kind=dp) :: eri_eps_filter, eri_eps_grid, eri_eps_int, eri_gpw_cutoff, eri_op_omega, &
248 eri_rcut, eri_rel_cutoff, fel, focc, max_offdiag, maxocc, nze_percentage
249 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :) :: eigenvalues
250 REAL(kind=dp), DIMENSION(:), POINTER :: evals_virtual
251 TYPE(active_space_type), POINTER :: active_space_env
252 TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
253 TYPE(cell_type), POINTER :: cell
254 TYPE(cp_blacs_env_type), POINTER :: context
255 TYPE(cp_fm_struct_type), POINTER :: fm_struct_tmp
256 TYPE(cp_fm_type) :: mo_virtual
257 TYPE(cp_fm_type), POINTER :: fm_target_active, fm_target_inactive, &
258 fmat, mo_coeff, mo_ref, mo_target
259 TYPE(cp_logger_type), POINTER :: logger
260 TYPE(dbcsr_csr_type), POINTER :: eri_mat
261 TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: ks_matrix, rho_ao, s_matrix
262 TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: ks_matrix_kp, rho_ao_kp, s_matrix_kp
263 TYPE(dbcsr_type), POINTER :: denmat
264 TYPE(dft_control_type), POINTER :: dft_control
265 TYPE(kpoint_type), POINTER :: kpoints
266 TYPE(mo_set_type), DIMENSION(:), POINTER :: mos
267 TYPE(mo_set_type), POINTER :: mo_set, mo_set_active, mo_set_inactive
268 TYPE(mp_para_env_type), POINTER :: para_env
269 TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
270 TYPE(preconditioner_type), POINTER :: local_preconditioner
271 TYPE(qcschema_type) :: qcschema_env
272 TYPE(qs_energy_type), POINTER :: energy
273 TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
274 TYPE(qs_ks_env_type), POINTER :: ks_env
275 TYPE(qs_rho_type), POINTER :: rho
276 TYPE(scf_control_type), POINTER :: scf_control
277 TYPE(section_vals_type), POINTER :: adiabatic_rescaling, as_input, &
278 hfx_section, input, loc_print, &
279 loc_section, print_orb, xc_section
280
281 !--------------------------------------------------------------------------------------------!
282
283 CALL get_qs_env(qs_env, input=input)
284 as_input => section_vals_get_subs_vals(input, "DFT%ACTIVE_SPACE")
285 CALL section_vals_get(as_input, explicit=explicit)
286 IF (.NOT. explicit) RETURN
287 CALL timeset(routinen, handle)
288
289 logger => cp_get_default_logger()
291
292 IF (iw > 0) THEN
293 WRITE (iw, '(/,T2,A)') &
294 '!-----------------------------------------------------------------------------!'
295 WRITE (iw, '(T26,A)') "Active Space Embedding Module"
296 WRITE (iw, '(T2,A)') &
297 '!-----------------------------------------------------------------------------!'
298 END IF
299
300 ! k-points?
301 NULLIFY (kpoints)
302 CALL get_qs_env(qs_env, do_kpoints=do_kpoints, dft_control=dft_control, kpoints=kpoints)
303 IF (do_kpoints) THEN
304 IF (.NOT. ASSOCIATED(kpoints)) THEN
305 CALL cp_abort(__location__, "Missing Gamma-point environment for active space module")
306 END IF
307 CALL get_kpoint_info(kpoints, kp_scheme=kp_scheme, nkp=nkp, use_real_wfn=use_real_wfn)
308 IF (trim(kp_scheme) /= "GAMMA" .OR. nkp /= 1 .OR. .NOT. use_real_wfn) THEN
309 CALL cp_abort(__location__, &
310 "Only Gamma-point DFT%KPOINTS are supported in the active space module")
311 END IF
312 IF (.NOT. ASSOCIATED(kpoints%kp_env)) THEN
313 CALL cp_abort(__location__, "Missing Gamma-point environment for active space module")
314 END IF
315 IF (.NOT. ASSOCIATED(kpoints%kp_env(1)%kpoint_env)) THEN
316 CALL cp_abort(__location__, "Missing Gamma-point environment for active space module")
317 END IF
318 IF (.NOT. ASSOCIATED(kpoints%kp_env(1)%kpoint_env%mos)) THEN
319 CALL cp_abort(__location__, "Missing Gamma-point MOs for active space module")
320 END IF
321 END IF
322
323 ! adiabatic rescaling?
324 adiabatic_rescaling => section_vals_get_subs_vals(input, "DFT%XC%ADIABATIC_RESCALING")
325 CALL section_vals_get(adiabatic_rescaling, explicit=explicit)
326 IF (explicit) THEN
327 CALL cp_abort(__location__, "Adiabatic rescaling not supported in active space module")
328 END IF
329
330 ! Setup the possible usage of DDAPC charges
331 do_ddapc = dft_control%qs_control%ddapc_restraint .OR. &
332 qs_env%cp_ddapc_ewald%do_decoupling .OR. &
333 qs_env%cp_ddapc_ewald%do_qmmm_periodic_decpl .OR. &
334 qs_env%cp_ddapc_ewald%do_solvation
335 IF (do_ddapc) THEN
336 CALL cp_abort(__location__, "DDAPC charges are not supported in the active space module")
337 END IF
338 IF (dft_control%do_sccs) THEN
339 CALL cp_abort(__location__, "SCCS is not supported in the active space module")
340 END IF
341 IF (dft_control%correct_surf_dip) THEN
342 IF (dft_control%surf_dip_correct_switch) THEN
343 CALL cp_abort(__location__, "Surface dipole correction not supported in the AS module")
344 END IF
345 END IF
346 IF (dft_control%smeagol_control%smeagol_enabled) THEN
347 CALL cp_abort(__location__, "SMEAGOL is not supported in the active space module")
348 END IF
349 IF (dft_control%qs_control%do_kg) THEN
350 CALL cp_abort(__location__, "KG correction not supported in the active space module")
351 END IF
352
353 NULLIFY (active_space_env)
354 CALL create_active_space_type(active_space_env)
355 active_space_env%energy_total = 0.0_dp
356 active_space_env%energy_ref = 0.0_dp
357 active_space_env%energy_inactive = 0.0_dp
358 active_space_env%energy_active = 0.0_dp
359
360 ! input options
361
362 CALL section_vals_val_get(as_input, "EPS_FCI", r_val=active_space_env%eps_fci)
363 IF (.NOT. (active_space_env%eps_fci > 0.0_dp)) THEN
364 cpabort("ACTIVE_SPACE%EPS_FCI must be positive.")
365 END IF
366
367 ! figure out what needs to be printed/stored
368 IF (btest(cp_print_key_should_output(logger%iter_info, as_input, "FCIDUMP"), cp_p_file)) THEN
369 active_space_env%fcidump = .true.
370 END IF
371
372 CALL section_vals_val_get(as_input, "QCSCHEMA", c_val=qcschema_filename, explicit=explicit)
373 IF (explicit) THEN
374 active_space_env%qcschema = .true.
375 active_space_env%qcschema_filename = qcschema_filename
376 END IF
377
378 CALL section_vals_val_get(as_input, "ACTIVE_ELECTRONS", i_val=nelec_active)
379 CALL get_qs_env(qs_env, nelectron_total=nelec_total)
380
381 IF (nelec_active <= 0) cpabort("Specify a positive number of active electrons.")
382 IF (nelec_active > nelec_total) cpabort("More active electrons than total electrons.")
383
384 nelec_inactive = nelec_total - nelec_active
385 IF (mod(nelec_inactive, 2) /= 0) THEN
386 cpabort("The remaining number of inactive electrons has to be even.")
387 END IF
388
389 IF (iw > 0) THEN
390 WRITE (iw, '(T3,A,T70,I10)') "Total number of electrons", nelec_total
391 WRITE (iw, '(T3,A,T70,I10)') "Number of inactive electrons", nelec_inactive
392 WRITE (iw, '(T3,A,T70,I10)') "Number of active electrons", nelec_active
393 END IF
394
395 CALL get_qs_env(qs_env, dft_control=dft_control)
396 nspins = dft_control%nspins
397
398 active_space_env%nelec_active = nelec_active
399 active_space_env%nelec_inactive = nelec_inactive
400 active_space_env%nelec_total = nelec_total
401 active_space_env%nspins = nspins
402 active_space_env%multiplicity = dft_control%multiplicity
403 active_space_env%restricted_orbitals = dft_control%roks
404
405 ! define the active/inactive space orbitals
406 CALL section_vals_val_get(as_input, "ACTIVE_ORBITALS", explicit=explicit, i_val=nmo_active)
407 IF (.NOT. explicit) THEN
408 CALL cp_abort(__location__, "Number of Active Orbitals has to be specified.")
409 END IF
410 active_space_env%nmo_active = nmo_active
411 ! this is safe because nelec_inactive is always even
412 nmo_inactive = nelec_inactive/2
413 active_space_env%nmo_inactive = nmo_inactive
414
415 CALL initialize_active_space_mixing(active_space_env, as_input)
416
417 CALL section_vals_val_get(as_input, "ORBITAL_SELECTION", i_val=mselect)
418 IF (iw > 0) THEN
419 SELECT CASE (mselect)
420 CASE DEFAULT
421 cpabort("Unknown orbital selection method")
422 CASE (casci_canonical)
423 WRITE (iw, '(/,T3,A)') &
424 "Active space orbitals selected using energy ordered canonical orbitals"
425 CASE (wannier_projection)
426 WRITE (iw, '(/,T3,A)') &
427 "Active space orbitals selected using projected Wannier orbitals"
428 CASE (mao_projection)
429 WRITE (iw, '(/,T3,A)') &
430 "Active space orbitals selected using modified atomic orbitals (MAO)"
431 CASE (manual_selection)
432 WRITE (iw, '(/,T3,A)') &
433 "Active space orbitals selected manually"
434 END SELECT
435
436 WRITE (iw, '(T3,A,T70,I10)') "Number of inactive orbitals", nmo_inactive
437 WRITE (iw, '(T3,A,T70,I10)') "Number of active orbitals", nmo_active
438 END IF
439
440 ! get projection spaces
441 CALL section_vals_val_get(as_input, "SUBSPACE_ATOM", i_val=iatom, explicit=explicit)
442 IF (explicit) THEN
443 CALL get_qs_env(qs_env, natom=natom)
444 IF (iatom <= 0 .OR. iatom > natom) THEN
445 IF (iw > 0) THEN
446 WRITE (iw, '(/,T3,A,I3)') "ERROR: SUBSPACE_ATOM number is not valid", iatom
447 END IF
448 cpabort("Select a valid SUBSPACE_ATOM")
449 END IF
450 END IF
451 CALL section_vals_val_get(as_input, "SUBSPACE_SHELL", c_val=cshell, explicit=explicit)
452 nshell = 0
453 lnam = ""
454 IF (explicit) THEN
455 cshell = adjustl(cshell)
456 n1 = 1
457 DO i = 1, 5
458 ishell = i
459 IF (cshell(n1:n1) == " ") THEN
460 ishell = ishell - 1
461 EXIT
462 END IF
463 READ (cshell(n1:), "(I1,A1)") nshell(i), lnam(i)
464 n1 = n1 + 2
465 END DO
466 END IF
467
468 ! generate orbitals
469 SELECT CASE (mselect)
470 CASE DEFAULT
471 cpabort("Unknown orbital selection method")
472 CASE (casci_canonical)
473 IF (do_kpoints) THEN
474 mos => kpoints%kp_env(1)%kpoint_env%mos(:)
475 ELSE
476 CALL get_qs_env(qs_env, mos=mos)
477 END IF
478
479 ! total number of occupied orbitals, i.e. inactive plus active MOs
480 nmo_occ = nmo_inactive + nmo_active
481
482 ! set inactive orbital indices, these are trivially 1...nmo_inactive
483 ALLOCATE (active_space_env%inactive_orbitals(nmo_inactive, nspins))
484 DO ispin = 1, nspins
485 DO i = 1, nmo_inactive
486 active_space_env%inactive_orbitals(i, ispin) = i
487 END DO
488 END DO
489
490 ! set active orbital indices, these are shifted by nmo_inactive
491 ALLOCATE (active_space_env%active_orbitals(nmo_active, nspins))
492 DO ispin = 1, nspins
493 DO i = 1, nmo_active
494 active_space_env%active_orbitals(i, ispin) = nmo_inactive + i
495 END DO
496 END DO
497
498 ! allocate and initialize inactive and active mo coefficients.
499 ! These are stored in a data structure for the full occupied space:
500 ! for inactive mos, the active subset is set to zero, vice versa for the active mos
501 ! TODO: allocate data structures only for the eaxct number MOs
502 maxocc = 2.0_dp
503 IF (nspins > 1) maxocc = 1.0_dp
504 ALLOCATE (active_space_env%mos_active(nspins))
505 ALLOCATE (active_space_env%mos_inactive(nspins))
506 DO ispin = 1, nspins
507 CALL get_mo_set(mos(ispin), mo_coeff=mo_ref, nao=nao)
508 CALL cp_fm_get_info(mo_ref, context=context, para_env=para_env, nrow_global=nrow_global)
509 ! the right number of active electrons per spin channel is initialized further down
510 CALL allocate_mo_set(active_space_env%mos_active(ispin), nao, nmo_occ, 0, 0.0_dp, maxocc, 0.0_dp)
511 CALL cp_fm_struct_create(fm_struct_tmp, para_env=para_env, context=context, &
512 nrow_global=nrow_global, ncol_global=nmo_occ)
513 CALL init_mo_set(active_space_env%mos_active(ispin), fm_struct=fm_struct_tmp, name="Active Space MO")
514 CALL cp_fm_struct_release(fm_struct_tmp)
515 IF (nspins == 2) THEN
516 nel = nelec_inactive/2
517 ELSE
518 nel = nelec_inactive
519 END IF
520 CALL allocate_mo_set(active_space_env%mos_inactive(ispin), nao, nmo_occ, nel, &
521 REAL(nel, kind=dp), maxocc, 0.0_dp)
522 CALL cp_fm_struct_create(fm_struct_tmp, para_env=para_env, context=context, &
523 nrow_global=nrow_global, ncol_global=nmo_occ)
524 CALL init_mo_set(active_space_env%mos_inactive(ispin), fm_struct=fm_struct_tmp, name="Inactive Space MO")
525 CALL cp_fm_struct_release(fm_struct_tmp)
526 END DO
527
528 ! create canonical orbitals
529 CALL get_qs_env(qs_env, scf_control=scf_control)
530 IF (dft_control%roks .AND. scf_control%roks_scheme /= high_spin_roks) THEN
531 CALL cp_abort(__location__, &
532 "Only high-spin ROKS is supported for ACTIVE_SPACE FCI; "// &
533 "general ROKS MO definitions are not implemented.")
534 ELSE
535 IF (dft_control%do_admm) THEN
536 IF (dft_control%do_admm_mo) THEN
537 cpabort("ADMM currently possible only with purification none_dm")
538 END IF
539 END IF
540
541 ALLOCATE (eigenvalues(nmo_occ, nspins))
542 eigenvalues = 0.0_dp
543 IF (do_kpoints) THEN
544 CALL get_qs_env(qs_env, matrix_ks_kp=ks_matrix_kp, matrix_s_kp=s_matrix_kp, &
545 scf_control=scf_control)
546 ks_matrix => ks_matrix_kp(:, 1)
547 s_matrix => s_matrix_kp(:, 1)
548 ELSE
549 CALL get_qs_env(qs_env, matrix_ks=ks_matrix, matrix_s=s_matrix, scf_control=scf_control)
550 END IF
551
552 ! calculate virtual MOs and copy inactive and active orbitals
553 IF (iw > 0) THEN
554 WRITE (iw, '(/,T3,A)') "Calculating virtual MOs..."
555 END IF
556 DO ispin = 1, nspins
557 ! the selection needs canonical SCF orbitals (e.g. not after OT)
558 CALL ensure_canonical_orbitals(qs_env, mos(ispin), ks_matrix(ispin)%matrix, ispin, &
559 .false., do_kpoints, iw)
560 ! nmo_available is the number of MOs available from the SCF calculation:
561 ! this is at least the number of occupied orbitals in the SCF, plus
562 ! any number of added MOs (virtuals) requested in the SCF section
563 CALL get_mo_set(mos(ispin), mo_coeff=mo_ref, nmo=nmo_available)
564
565 ! calculate how many extra MOs we still have to compute
566 nmo_virtual = nmo_occ - nmo_available
567 nmo_virtual = max(nmo_virtual, 0)
568
569 NULLIFY (evals_virtual)
570 ALLOCATE (evals_virtual(nmo_virtual))
571
572 CALL cp_fm_get_info(mo_ref, context=context, para_env=para_env, &
573 nrow_global=nrow_global)
574
575 CALL cp_fm_struct_create(fm_struct_tmp, para_env=para_env, context=context, &
576 nrow_global=nrow_global, ncol_global=nmo_virtual)
577 CALL cp_fm_create(mo_virtual, fm_struct_tmp, name="virtual")
578 CALL cp_fm_struct_release(fm_struct_tmp)
579 CALL cp_fm_init_random(mo_virtual, nmo_virtual)
580
581 ! compute missing virtual MOs
582 CALL virtual_orbital_preconditioner(local_preconditioner, mos(ispin), ks_matrix(ispin)%matrix, &
583 s_matrix(1)%matrix, nmo_virtual, para_env, context)
584 CALL ot_eigensolver(matrix_h=ks_matrix(ispin)%matrix, matrix_s=s_matrix(1)%matrix, &
585 matrix_c_fm=mo_virtual, matrix_orthogonal_space_fm=mo_ref, &
586 eps_gradient=scf_control%eps_lumos, &
587 preconditioner=local_preconditioner, &
588 iter_max=scf_control%max_iter_lumos, &
589 size_ortho_space=nmo_available)
590 IF (ASSOCIATED(local_preconditioner)) THEN
591 CALL destroy_preconditioner(local_preconditioner)
592 DEALLOCATE (local_preconditioner)
593 END IF
594
595 ! get the eigenvalues
596 CALL calculate_subspace_eigenvalues(mo_virtual, ks_matrix(ispin)%matrix, evals_virtual)
597
598 ! orbital energies <i|F|i> of the SCF orbitals, which are used unchanged
599 CALL calculate_orbital_fock_elements(mo_ref, ks_matrix(ispin)%matrix, &
600 eigenvalues(:, ispin), max_offdiag)
601
602 ! copy inactive orbitals
603 mo_set => active_space_env%mos_inactive(ispin)
604 CALL get_mo_set(mo_set, mo_coeff=mo_target)
605 DO i = 1, SIZE(active_space_env%inactive_orbitals, 1)
606 m = active_space_env%inactive_orbitals(i, ispin)
607 CALL cp_fm_to_fm(mo_ref, mo_target, 1, m, m)
608 mo_set%eigenvalues(m) = eigenvalues(m, ispin)
609 IF (nspins > 1) THEN
610 mo_set%occupation_numbers(m) = 1.0_dp
611 ELSE
612 mo_set%occupation_numbers(m) = 2.0_dp
613 END IF
614 END DO
615
616 ! copy active orbitals
617 mo_set => active_space_env%mos_active(ispin)
618 CALL get_mo_set(mo_set, mo_coeff=mo_target)
619 ! for mult > 1, put the polarized electrons in the alpha channel
620 IF (nspins == 2) THEN
621 IF (ispin == 1) THEN
622 nel = (nelec_active + active_space_env%multiplicity - 1)/2
623 ELSE
624 nel = (nelec_active - active_space_env%multiplicity + 1)/2
625 END IF
626 ELSE
627 nel = nelec_active
628 END IF
629 mo_set%nelectron = nel
630 mo_set%n_el_f = real(nel, kind=dp)
631 DO i = 1, nmo_active
632 m = active_space_env%active_orbitals(i, ispin)
633 IF (m > nmo_available) THEN
634 CALL cp_fm_to_fm(mo_virtual, mo_target, 1, m - nmo_available, m)
635 eigenvalues(m, ispin) = evals_virtual(m - nmo_available)
636 mo_set%occupation_numbers(m) = 0.0_dp
637 ELSE
638 CALL cp_fm_to_fm(mo_ref, mo_target, 1, m, m)
639 mo_set%occupation_numbers(m) = mos(ispin)%occupation_numbers(m)
640 END IF
641 mo_set%eigenvalues(m) = eigenvalues(m, ispin)
642 END DO
643 ! Release
644 DEALLOCATE (evals_virtual)
645 CALL cp_fm_release(mo_virtual)
646 END DO
647
648 IF (iw > 0) THEN
649 DO ispin = 1, nspins
650 WRITE (iw, '(/,T3,A,I3,T66,A)') "Canonical Orbital Selection for spin", ispin, &
651 "[atomic units]"
652 DO i = 1, nmo_inactive, 4
653 jm = min(3, nmo_inactive - i)
654 WRITE (iw, '(T3,4(F14.6,A5))') (eigenvalues(i + j, ispin), " [I]", j=0, jm)
655 END DO
656 DO i = nmo_inactive + 1, nmo_inactive + nmo_active, 4
657 jm = min(3, nmo_inactive + nmo_active - i)
658 WRITE (iw, '(T3,4(F14.6,A5))') (eigenvalues(i + j, ispin), " [A]", j=0, jm)
659 END DO
660 WRITE (iw, '(/,T3,A,I3)') "Active Orbital Indices for spin", ispin
661 DO i = 1, SIZE(active_space_env%active_orbitals, 1), 4
662 jm = min(3, SIZE(active_space_env%active_orbitals, 1) - i)
663 WRITE (iw, '(T3,4(I4))') (active_space_env%active_orbitals(i + j, ispin), j=0, jm)
664 END DO
665 END DO
666 END IF
667 DEALLOCATE (eigenvalues)
668 END IF
669
670 CASE (manual_selection)
671 ! create canonical orbitals
672 IF (dft_control%roks) THEN
673 CALL cp_abort(__location__, &
674 "Manual ACTIVE_SPACE orbital selection is not supported for ROKS; "// &
675 "use canonical high-spin ROKS.")
676 ELSE
677 IF (dft_control%do_admm) THEN
678 ! For admm_mo, the auxiliary density is computed from the MOs, which never change
679 ! in the rs-dft embedding, therefore the energy is wrong as the LR HFX never changes.
680 ! For admm_dm, the auxiliary density is computed from the density matrix, which is
681 ! updated at each iteration and therefore works.
682 IF (dft_control%do_admm_mo) THEN
683 cpabort("ADMM currently possible only with purification none_dm")
684 END IF
685 END IF
686
687 CALL section_vals_val_get(as_input, "ACTIVE_ORBITAL_INDICES", explicit=explicit, i_vals=invals)
688 IF (.NOT. explicit) THEN
689 CALL cp_abort(__location__, "Manual orbital selection requires to explicitly "// &
690 "set the active orbital indices via ACTIVE_ORBITAL_INDICES")
691 END IF
692
693 IF (nspins == 1) THEN
694 cpassert(SIZE(invals) == nmo_active)
695 ELSE
696 cpassert(SIZE(invals) == 2*nmo_active)
697 END IF
698 ALLOCATE (active_space_env%inactive_orbitals(nmo_inactive, nspins))
699 ALLOCATE (active_space_env%active_orbitals(nmo_active, nspins))
700
701 DO ispin = 1, nspins
702 DO i = 1, nmo_active
703 active_space_env%active_orbitals(i, ispin) = invals(i + (ispin - 1)*nmo_active)
704 END DO
705 END DO
706
707 IF (do_kpoints) THEN
708 mos => kpoints%kp_env(1)%kpoint_env%mos(:)
709 ELSE
710 CALL get_qs_env(qs_env, mos=mos)
711 END IF
712
713 ! include MOs up to the largest index in the list
714 max_orb_ind = maxval(invals)
715 maxocc = 2.0_dp
716 IF (nspins > 1) maxocc = 1.0_dp
717 ALLOCATE (active_space_env%mos_active(nspins))
718 ALLOCATE (active_space_env%mos_inactive(nspins))
719 DO ispin = 1, nspins
720 ! init active orbitals
721 CALL get_mo_set(mos(ispin), mo_coeff=mo_ref, nao=nao)
722 CALL cp_fm_get_info(mo_ref, context=context, para_env=para_env, nrow_global=nrow_global)
723 CALL allocate_mo_set(active_space_env%mos_active(ispin), nao, max_orb_ind, 0, 0.0_dp, maxocc, 0.0_dp)
724 CALL cp_fm_struct_create(fm_struct_tmp, para_env=para_env, context=context, &
725 nrow_global=nrow_global, ncol_global=max_orb_ind)
726 CALL init_mo_set(active_space_env%mos_active(ispin), fm_struct=fm_struct_tmp, name="Active Space MO")
727 CALL cp_fm_struct_release(fm_struct_tmp)
728
729 ! init inactive orbitals
730 IF (nspins == 2) THEN
731 nel = nelec_inactive/2
732 ELSE
733 nel = nelec_inactive
734 END IF
735 CALL allocate_mo_set(active_space_env%mos_inactive(ispin), nao, max_orb_ind, nel, real(nel, kind=dp), maxocc, 0.0_dp)
736 CALL cp_fm_struct_create(fm_struct_tmp, para_env=para_env, context=context, &
737 nrow_global=nrow_global, ncol_global=max_orb_ind)
738 CALL init_mo_set(active_space_env%mos_inactive(ispin), fm_struct=fm_struct_tmp, name="Inactive Space MO")
739 ! small hack: set the correct inactive occupations down below
740 active_space_env%mos_inactive(ispin)%occupation_numbers = 0.0_dp
741 CALL cp_fm_struct_release(fm_struct_tmp)
742 END DO
743
744 ALLOCATE (eigenvalues(max_orb_ind, nspins))
745 eigenvalues = 0.0_dp
746 IF (do_kpoints) THEN
747 CALL get_qs_env(qs_env, matrix_ks_kp=ks_matrix_kp, matrix_s_kp=s_matrix_kp, &
748 scf_control=scf_control)
749 ks_matrix => ks_matrix_kp(:, 1)
750 s_matrix => s_matrix_kp(:, 1)
751 ELSE
752 CALL get_qs_env(qs_env, matrix_ks=ks_matrix, matrix_s=s_matrix, scf_control=scf_control)
753 END IF
754
755 ! calculate virtual MOs and copy inactive and active orbitals
756 IF (iw > 0) THEN
757 WRITE (iw, '(/,T3,A)') "Calculating virtual MOs..."
758 END IF
759 DO ispin = 1, nspins
760 ! the orbital indices refer to canonical SCF orbitals (e.g. not after OT)
761 CALL ensure_canonical_orbitals(qs_env, mos(ispin), ks_matrix(ispin)%matrix, ispin, &
762 .true., do_kpoints, iw)
763 CALL get_mo_set(mos(ispin), mo_coeff=mo_ref, nmo=nmo_available)
764 nmo_virtual = max_orb_ind - nmo_available
765 nmo_virtual = max(nmo_virtual, 0)
766
767 NULLIFY (evals_virtual)
768 ALLOCATE (evals_virtual(nmo_virtual))
769
770 CALL cp_fm_get_info(mo_ref, context=context, para_env=para_env, &
771 nrow_global=nrow_global)
772
773 CALL cp_fm_struct_create(fm_struct_tmp, para_env=para_env, context=context, &
774 nrow_global=nrow_global, ncol_global=nmo_virtual)
775 CALL cp_fm_create(mo_virtual, fm_struct_tmp, name="virtual")
776 CALL cp_fm_struct_release(fm_struct_tmp)
777 CALL cp_fm_init_random(mo_virtual, nmo_virtual)
778
779 CALL virtual_orbital_preconditioner(local_preconditioner, mos(ispin), ks_matrix(ispin)%matrix, &
780 s_matrix(1)%matrix, nmo_virtual, para_env, context)
781 CALL ot_eigensolver(matrix_h=ks_matrix(ispin)%matrix, matrix_s=s_matrix(1)%matrix, &
782 matrix_c_fm=mo_virtual, matrix_orthogonal_space_fm=mo_ref, &
783 eps_gradient=scf_control%eps_lumos, &
784 preconditioner=local_preconditioner, &
785 iter_max=scf_control%max_iter_lumos, &
786 size_ortho_space=nmo_available)
787 IF (ASSOCIATED(local_preconditioner)) THEN
788 CALL destroy_preconditioner(local_preconditioner)
789 DEALLOCATE (local_preconditioner)
790 END IF
791
792 CALL calculate_subspace_eigenvalues(mo_virtual, ks_matrix(ispin)%matrix, &
793 evals_virtual)
794
795 ! orbital energies <i|F|i> of the SCF orbitals, which are used unchanged
796 CALL calculate_orbital_fock_elements(mo_ref, ks_matrix(ispin)%matrix, &
797 eigenvalues(:, ispin), max_offdiag)
798
799 mo_set_active => active_space_env%mos_active(ispin)
800 CALL get_mo_set(mo_set_active, mo_coeff=fm_target_active)
801 mo_set_inactive => active_space_env%mos_inactive(ispin)
802 CALL get_mo_set(mo_set_inactive, mo_coeff=fm_target_inactive)
803
804 ! copy orbitals
805 nmo_inactive_remaining = nmo_inactive
806 DO i = 1, max_orb_ind
807 ! case for i being an active orbital
808 IF (any(active_space_env%active_orbitals(:, ispin) == i)) THEN
809 IF (i > nmo_available) THEN
810 CALL cp_fm_to_fm(mo_virtual, fm_target_active, 1, i - nmo_available, i)
811 eigenvalues(i, ispin) = evals_virtual(i - nmo_available)
812 mo_set_active%occupation_numbers(i) = 0.0_dp
813 ELSE
814 CALL cp_fm_to_fm(mo_ref, fm_target_active, 1, i, i)
815 mo_set_active%occupation_numbers(i) = mos(ispin)%occupation_numbers(i)
816 END IF
817 mo_set_active%eigenvalues(i) = eigenvalues(i, ispin)
818 ! if it was not an active orbital, check whether it is an inactive orbital
819 ELSE IF (nmo_inactive_remaining > 0) THEN
820 CALL cp_fm_to_fm(mo_ref, fm_target_inactive, 1, i, i)
821 ! store on the fly the mapping of inactive orbitals
822 active_space_env%inactive_orbitals(nmo_inactive - nmo_inactive_remaining + 1, ispin) = i
823 mo_set_inactive%eigenvalues(i) = eigenvalues(i, ispin)
824 mo_set_inactive%occupation_numbers(i) = mos(ispin)%occupation_numbers(i)
825 ! hack: set homo and lumo manually
826 IF (nmo_inactive_remaining == 1) THEN
827 mo_set_inactive%homo = i
828 mo_set_inactive%lfomo = i + 1
829 END IF
830 nmo_inactive_remaining = nmo_inactive_remaining - 1
831 ELSE
832 cycle
833 END IF
834 END DO
835
836 ! Release
837 DEALLOCATE (evals_virtual)
838 CALL cp_fm_release(mo_virtual)
839 END DO
840
841 IF (iw > 0) THEN
842 DO ispin = 1, nspins
843 WRITE (iw, '(/,T3,A,I3,T66,A)') "Orbital Energies and Selection for spin", ispin, "[atomic units]"
844
845 DO i = 1, max_orb_ind, 4
846 jm = min(3, max_orb_ind - i)
847 WRITE (iw, '(T4)', advance="no")
848 DO j = 0, jm
849 IF (any(active_space_env%active_orbitals(:, ispin) == i + j)) THEN
850 WRITE (iw, '(T3,F12.6,A5)', advance="no") eigenvalues(i + j, ispin), " [A]"
851 ELSE IF (any(active_space_env%inactive_orbitals(:, ispin) == i + j)) THEN
852 WRITE (iw, '(T3,F12.6,A5)', advance="no") eigenvalues(i + j, ispin), " [I]"
853 ELSE
854 WRITE (iw, '(T3,F12.6,A5)', advance="no") eigenvalues(i + j, ispin), " [V]"
855 END IF
856 END DO
857 WRITE (iw, *)
858 END DO
859 WRITE (iw, '(/,T3,A,I3)') "Active Orbital Indices for spin", ispin
860 DO i = 1, SIZE(active_space_env%active_orbitals, 1), 4
861 jm = min(3, SIZE(active_space_env%active_orbitals, 1) - i)
862 WRITE (iw, '(T3,4(I4))') (active_space_env%active_orbitals(i + j, ispin), j=0, jm)
863 END DO
864 END DO
865 END IF
866 DEALLOCATE (eigenvalues)
867 END IF
868
869 CASE (wannier_projection)
870 NULLIFY (loc_section, loc_print)
871 loc_section => section_vals_get_subs_vals(as_input, "LOCALIZE")
872 cpassert(ASSOCIATED(loc_section))
873 loc_print => section_vals_get_subs_vals(as_input, "LOCALIZE%PRINT")
874 !
875 cpabort("not yet available")
876 !
877 CASE (mao_projection)
878 !
879 cpabort("not yet available")
880 !
881 END SELECT
882
883 ! Warn if the active space splits a set of degenerate orbitals
884 CALL check_active_space_degeneracy(active_space_env, mos)
885
886 ! Print orbitals on Cube files
887 print_orb => section_vals_get_subs_vals(as_input, "PRINT_ORBITAL_CUBES")
888 CALL section_vals_get(print_orb, explicit=explicit)
889 CALL section_vals_val_get(print_orb, "STOP_AFTER_CUBES", l_val=stop_after_print)
890 IF (explicit) THEN
891 !
892 CALL print_orbital_cubes(print_orb, qs_env, active_space_env%mos_active)
893 !
894 IF (stop_after_print) THEN
895
896 IF (iw > 0) THEN
897 WRITE (iw, '(/,T2,A)') &
898 '!----------------- Early End of Active Space Interface -----------------------!'
899 END IF
900
901 CALL timestop(handle)
902
903 RETURN
904 END IF
905 END IF
906
907 ! calculate inactive density matrix
908 CALL get_qs_env(qs_env, rho=rho)
909 IF (do_kpoints) THEN
910 CALL qs_rho_get(rho, rho_ao_kp=rho_ao_kp)
911 rho_ao => rho_ao_kp(:, 1)
912 ELSE
913 CALL qs_rho_get(rho, rho_ao=rho_ao)
914 END IF
915 cpassert(ASSOCIATED(rho_ao))
916 CALL dbcsr_allocate_matrix_set(active_space_env%pmat_inactive, nspins)
917 DO ispin = 1, nspins
918 ALLOCATE (denmat)
919 CALL dbcsr_copy(denmat, rho_ao(ispin)%matrix)
920 mo_set => active_space_env%mos_inactive(ispin)
921 CALL calculate_density_matrix(mo_set, denmat)
922 active_space_env%pmat_inactive(ispin)%matrix => denmat
923 END DO
924
925 ! read in ERI parameters
926 CALL section_vals_val_get(as_input, "ERI%METHOD", i_val=eri_method)
927 active_space_env%eri%method = eri_method
928 CALL section_vals_val_get(as_input, "ERI%OPERATOR", i_val=eri_operator, explicit=ex_operator)
929 active_space_env%eri%operator = eri_operator
930 CALL section_vals_val_get(as_input, "ERI%OMEGA", r_val=eri_op_omega, explicit=ex_omega)
931 active_space_env%eri%omega = eri_op_omega
932 CALL section_vals_val_get(as_input, "ERI%CUTOFF_RADIUS", r_val=eri_rcut, explicit=ex_rcut)
933 active_space_env%eri%cutoff_radius = eri_rcut ! this is already converted to bohr!
934 CALL section_vals_val_get(as_input, "ERI%PERIODICITY", i_vals=invals, explicit=ex_perd)
935 CALL section_vals_val_get(as_input, "ERI%EPS_INTEGRAL", r_val=eri_eps_int)
936 active_space_env%eri%eps_integral = eri_eps_int
937 ! if eri periodicity is explicitly set, we use it, otherwise we use the cell periodicity
938 IF (ex_perd) THEN
939 IF (SIZE(invals) == 1) THEN
940 active_space_env%eri%periodicity(1:3) = invals(1)
941 ELSE
942 active_space_env%eri%periodicity(1:3) = invals(1:3)
943 END IF
944 ELSE
945 CALL get_qs_env(qs_env, cell=cell)
946 active_space_env%eri%periodicity(1:3) = cell%perd(1:3)
947 END IF
948 IF (iw > 0) THEN
949 WRITE (iw, '(/,T3,A)') "Calculation of Electron Repulsion Integrals"
950
951 SELECT CASE (eri_method)
953 WRITE (iw, '(T3,A,T50,A)') "Integration method", "GPW Fourier transform over MOs"
954 CASE (eri_method_gpw_ht)
955 WRITE (iw, '(T3,A,T44,A)') "Integration method", "Half transformed integrals from GPW"
956 CASE DEFAULT
957 cpabort("Unknown ERI method")
958 END SELECT
959
960 SELECT CASE (eri_operator)
962 WRITE (iw, '(T3,A,T73,A)') "ERI operator", "Coulomb"
963
965 WRITE (iw, '(T3,A,T74,A)') "ERI operator", "Yukawa"
966 IF (.NOT. ex_omega) CALL cp_abort(__location__, &
967 "Yukawa operator requires OMEGA to be explicitly set")
968 WRITE (iw, '(T3,A,T66,F14.3)') "ERI operator parameter OMEGA", eri_op_omega
969
970 CASE (eri_operator_erf)
971 WRITE (iw, '(T3,A,T63,A)') "ERI operator", "Longrange Coulomb"
972 IF (.NOT. ex_omega) CALL cp_abort(__location__, &
973 "Longrange operator requires OMEGA to be explicitly set")
974 WRITE (iw, '(T3,A,T66,F14.3)') "ERI operator parameter OMEGA", eri_op_omega
975
976 CASE (eri_operator_erfc)
977 WRITE (iw, '(T3,A,T62,A)') "ERI operator", "Shortrange Coulomb"
978 IF (.NOT. ex_omega) CALL cp_abort(__location__, &
979 "Shortrange operator requires OMEGA to be explicitly set")
980 WRITE (iw, '(T3,A,T66,F14.3)') "ERI operator parameter OMEGA", eri_op_omega
981
982 CASE (eri_operator_trunc)
983 WRITE (iw, '(T3,A,T63,A)') "ERI operator", "Truncated Coulomb"
984 IF (.NOT. ex_rcut) CALL cp_abort(__location__, &
985 "Cutoff radius not specified for trunc. Coulomb operator")
986 WRITE (iw, '(T3,A,T66,F14.3)') "ERI operator cutoff radius (au)", eri_rcut
987
989 WRITE (iw, '(T3,A,T53,A)') "ERI operator", "Longrange truncated Coulomb"
990 IF (.NOT. ex_rcut) CALL cp_abort(__location__, &
991 "Cutoff radius not specified for trunc. longrange operator")
992 WRITE (iw, '(T3,A,T66,F14.3)') "ERI operator cutoff radius (au)", eri_rcut
993 IF (.NOT. ex_omega) CALL cp_abort(__location__, &
994 "LR truncated operator requires OMEGA to be explicitly set")
995 WRITE (iw, '(T3,A,T66,F14.3)') "ERI operator parameter OMEGA", eri_op_omega
996 IF (eri_op_omega < 0.01_dp) THEN
997 cpabort("LR truncated operator requires OMEGA >= 0.01 to be stable")
998 END IF
999
1000 CASE DEFAULT
1001 cpabort("Unknown ERI operator")
1002
1003 END SELECT
1004
1005 WRITE (iw, '(T3,A,T68,E12.4)') "Accuracy of ERIs", eri_eps_int
1006 WRITE (iw, '(T3,A,T71,3I3)') "Periodicity", active_space_env%eri%periodicity(1:3)
1007
1008 ! TODO: should be moved after ERI calculation, as it depends on screening
1009 IF (nspins < 2) THEN
1010 WRITE (iw, '(T3,A,T68,I12)') "Total Number of ERI", (nmo_active**4)/8
1011 ELSE
1012 WRITE (iw, '(T3,A,T68,I12)') "Total Number of ERI (aa|aa)", (nmo_active**4)/8
1013 WRITE (iw, '(T3,A,T68,I12)') "Total Number of ERI (bb|bb)", (nmo_active**4)/8
1014 WRITE (iw, '(T3,A,T68,I12)') "Total Number of ERI (aa|bb)", (nmo_active**4)/4
1015 END IF
1016 END IF
1017
1018 ! allocate container for integrals (CSR matrix)
1019 CALL get_qs_env(qs_env, para_env=para_env)
1020 m = (nspins*(nspins + 1))/2
1021 ! With ROHF/ROKS, we need ERIs from only a single set of orbitals
1022 IF (dft_control%roks) m = 1
1023 ALLOCATE (active_space_env%eri%eri(m))
1024 DO i = 1, m
1025 CALL get_mo_set(active_space_env%mos_active(1), nmo=nmo)
1026 ALLOCATE (active_space_env%eri%eri(i)%csr_mat)
1027 eri_mat => active_space_env%eri%eri(i)%csr_mat
1028 IF (i == 1) THEN
1029 n1 = nmo
1030 n2 = nmo
1031 ELSE IF (i == 2) THEN
1032 n1 = nmo
1033 n2 = nmo
1034 ELSE
1035 n1 = nmo
1036 n2 = nmo
1037 END IF
1038 nn1 = (n1*(n1 + 1))/2
1039 nn2 = (n2*(n2 + 1))/2
1040 CALL dbcsr_csr_create(eri_mat, nn1, nn2, 0_int_8, 0, 0, para_env%get_handle())
1041 active_space_env%eri%norb = nmo
1042 END DO
1043
1044 SELECT CASE (eri_method)
1046 CALL section_vals_val_get(as_input, "ERI_GPW%EPS_GRID", r_val=eri_eps_grid)
1047 active_space_env%eri%eri_gpw%eps_grid = eri_eps_grid
1048 CALL section_vals_val_get(as_input, "ERI_GPW%EPS_FILTER", r_val=eri_eps_filter)
1049 active_space_env%eri%eri_gpw%eps_filter = eri_eps_filter
1050 CALL section_vals_val_get(as_input, "ERI_GPW%CUTOFF", r_val=eri_gpw_cutoff)
1051 active_space_env%eri%eri_gpw%cutoff = eri_gpw_cutoff
1052 CALL section_vals_val_get(as_input, "ERI_GPW%REL_CUTOFF", r_val=eri_rel_cutoff)
1053 active_space_env%eri%eri_gpw%rel_cutoff = eri_rel_cutoff
1054 CALL section_vals_val_get(as_input, "ERI_GPW%PRINT_LEVEL", i_val=eri_print)
1055 active_space_env%eri%eri_gpw%print_level = eri_print
1056 CALL section_vals_val_get(as_input, "ERI_GPW%STORE_WFN", l_val=store_wfn)
1057 active_space_env%eri%eri_gpw%store_wfn = store_wfn
1058 CALL section_vals_val_get(as_input, "ERI_GPW%GROUP_SIZE", i_val=group_size)
1059 active_space_env%eri%eri_gpw%group_size = group_size
1060 ! Always redo Poisson solver for now
1061 active_space_env%eri%eri_gpw%redo_poisson = .true.
1062 ! active_space_env%eri%eri_gpw%redo_poisson = (ex_operator .OR. ex_perd)
1063 IF (iw > 0) THEN
1064 WRITE (iw, '(/,T2,A,T71,F10.1)') "ERI_GPW| Energy cutoff [Ry]", eri_gpw_cutoff
1065 WRITE (iw, '(T2,A,T71,F10.1)') "ERI_GPW| Relative energy cutoff [Ry]", eri_rel_cutoff
1066 END IF
1067 !
1068 CALL calculate_eri_gpw(active_space_env%mos_active, active_space_env%active_orbitals, active_space_env%eri, qs_env, iw, &
1069 dft_control%roks)
1070 !
1071 CASE DEFAULT
1072 cpabort("Unknown ERI method")
1073 END SELECT
1074 IF (iw > 0) THEN
1075 DO isp = 1, SIZE(active_space_env%eri%eri)
1076 eri_mat => active_space_env%eri%eri(isp)%csr_mat
1077 nze_percentage = 100.0_dp*(real(eri_mat%nze_total, kind=dp) &
1078 /real(eri_mat%nrows_total, kind=dp))/real(eri_mat%ncols_total, kind=dp)
1079 WRITE (iw, '(/,T2,A,I2,T30,A,T68,I12)') "ERI_GPW| Spinmatrix:", isp, &
1080 "Number of CSR non-zero elements:", eri_mat%nze_total
1081 WRITE (iw, '(T2,A,I2,T30,A,T68,F12.4)') "ERI_GPW| Spinmatrix:", isp, &
1082 "Percentage CSR non-zero elements:", nze_percentage
1083 WRITE (iw, '(T2,A,I2,T30,A,T68,I12)') "ERI_GPW| Spinmatrix:", isp, &
1084 "nrows_total", eri_mat%nrows_total
1085 WRITE (iw, '(T2,A,I2,T30,A,T68,I12)') "ERI_GPW| Spinmatrix:", isp, &
1086 "ncols_total", eri_mat%ncols_total
1087 WRITE (iw, '(T2,A,I2,T30,A,T68,I12)') "ERI_GPW| Spinmatrix:", isp, &
1088 "nrows_local", eri_mat%nrows_local
1089 END DO
1090 CALL m_flush(iw)
1091 END IF
1092 CALL para_env%sync()
1093
1094 ! set the reference active space density matrix
1095 nspins = active_space_env%nspins
1096 ALLOCATE (active_space_env%p_active(nspins))
1097 DO isp = 1, nspins
1098 mo_set => active_space_env%mos_active(isp)
1099 CALL get_mo_set(mo_set, mo_coeff=mo_coeff, nmo=nmo)
1100 CALL create_subspace_matrix(mo_coeff, active_space_env%p_active(isp), nmo)
1101 END DO
1102 SELECT CASE (mselect)
1103 CASE DEFAULT
1104 cpabort("Unknown orbital selection method")
1106 focc = 2.0_dp
1107 IF (nspins == 2) focc = 1.0_dp
1108 DO isp = 1, nspins
1109 fmat => active_space_env%p_active(isp)
1110 CALL cp_fm_set_all(fmat, alpha=0.0_dp)
1111 IF (nspins == 2) THEN
1112 IF (isp == 1) THEN
1113 nel = (active_space_env%nelec_active + active_space_env%multiplicity - 1)/2
1114 ELSE
1115 nel = (active_space_env%nelec_active - active_space_env%multiplicity + 1)/2
1116 END IF
1117 ELSE
1118 nel = active_space_env%nelec_active
1119 END IF
1120 DO i = 1, nmo_active
1121 m = active_space_env%active_orbitals(i, isp)
1122 fel = min(focc, real(nel, kind=dp))
1123 CALL cp_fm_set_element(fmat, m, m, fel)
1124 nel = nel - nint(fel)
1125 nel = max(nel, 0)
1126 END DO
1127 END DO
1128 CASE (wannier_projection)
1129 cpabort("NOT IMPLEMENTED")
1130 CASE (mao_projection)
1131 cpabort("NOT IMPLEMENTED")
1132 END SELECT
1133
1134 ! compute alpha-beta overlap matrix in case of spin-polarized calculation
1135 CALL calculate_spin_pol_overlap(active_space_env%mos_active, qs_env, active_space_env)
1136
1137 ! figure out if we have a new xc section for the AS
1138 xc_section => section_vals_get_subs_vals(input, "DFT%ACTIVE_SPACE%XC")
1139 explicit = .false.
1140 IF (ASSOCIATED(xc_section)) CALL section_vals_get(xc_section, explicit=explicit)
1141
1142 ! rebuild KS matrix if needed
1143 IF (explicit) THEN
1144 ! release the hfx data if it was part of the SCF functional
1145 IF (ASSOCIATED(qs_env%x_data)) CALL hfx_release(qs_env%x_data)
1146 ! also release the admm environment in case we are using admm
1147 IF (ASSOCIATED(qs_env%admm_env)) CALL admm_env_release(qs_env%admm_env)
1148
1149 CALL get_qs_env(qs_env, atomic_kind_set=atomic_kind_set, qs_kind_set=qs_kind_set, &
1150 particle_set=particle_set, cell=cell, ks_env=ks_env)
1151 IF (dft_control%do_admm) THEN
1152 basis_type = 'AUX_FIT'
1153 ELSE
1154 basis_type = 'ORB'
1155 END IF
1156 hfx_section => section_vals_get_subs_vals(xc_section, "HF")
1157 CALL hfx_create(qs_env%x_data, para_env, hfx_section, atomic_kind_set, &
1158 qs_kind_set, particle_set, dft_control, cell, orb_basis=basis_type, &
1159 nelectron_total=nelec_total)
1160
1161 qs_env%requires_matrix_vxc = .true. ! needs to be set only once
1162
1163 ! a bit of a hack: this forces a new re-init of HFX
1164 CALL set_ks_env(ks_env, s_mstruct_changed=.true.)
1165 CALL qs_ks_build_kohn_sham_matrix(qs_env, calculate_forces=.false., &
1166 just_energy=.false., &
1167 ext_xc_section=xc_section)
1168 ! we need to reset it to false
1169 CALL set_ks_env(ks_env, s_mstruct_changed=.false.)
1170 ELSE
1171 xc_section => section_vals_get_subs_vals(input, "DFT%XC")
1172 END IF
1173 ! set the xc_section
1174 active_space_env%xc_section => xc_section
1175
1176 CALL get_qs_env(qs_env, energy=energy)
1177 ! transform KS/Fock, Vxc and Hcore to AS MO basis
1178 CALL calculate_operators(active_space_env%mos_active, qs_env, active_space_env)
1179 ! set the reference energy in the active space
1180 active_space_env%energy_ref = energy%total
1181 ! calculate inactive energy and embedding potential
1182 CALL subspace_fock_matrix(active_space_env, dft_control%roks)
1183
1184 ! associate the active space environment with the qs environment
1185 CALL set_qs_env(qs_env, active_space=active_space_env)
1186
1187 ! Perform the embedding calculation when an active-space solver is specified
1188 CALL section_vals_val_get(as_input, "AS_SOLVER", i_val=as_solver)
1189 SELECT CASE (as_solver)
1190 CASE (no_solver)
1191 IF (iw > 0) THEN
1192 WRITE (iw, '(/,T3,A)') "No active space solver specified, skipping embedding calculation"
1193 CALL m_flush(iw)
1194 END IF
1195 CALL para_env%sync()
1196 CASE (qiskit_solver)
1197 CALL rsdft_embedding(qs_env, active_space_env, as_input)
1198 CALL qs_scf_compute_properties(qs_env, wf_type="MC-DFT", do_mp2=.false.)
1199 CASE (fci_solver)
1200 CALL local_fci_embedding(qs_env, active_space_env, as_input)
1201 CALL qs_scf_compute_properties(qs_env, wf_type="MC-DFT", do_mp2=.false.)
1202 CASE DEFAULT
1203 cpabort("Unknown active space solver")
1204 END SELECT
1205
1206 ! Output a FCIDUMP file if requested
1207 IF (active_space_env%fcidump) THEN
1208 CALL fcidump(active_space_env, as_input, dft_control%roks, &
1209 dft_control%roks .AND. mselect == casci_canonical .AND. as_solver == no_solver)
1210 END IF
1211
1212 ! Output the inactive and active orbitals in FCIDUMP order as a Molden file if requested
1213 IF (btest(cp_print_key_should_output(logger%iter_info, as_input, "MOLDEN"), cp_p_file)) THEN
1214 CALL write_active_space_molden(qs_env, active_space_env, as_input)
1215 END IF
1216
1217 ! Output a QCSchema file if requested
1218 IF (active_space_env%qcschema) THEN
1219 CALL qcschema_env_create(qcschema_env, qs_env)
1220 IF (para_env%is_source()) THEN
1221 CALL qcschema_to_hdf5(qcschema_env, active_space_env%qcschema_filename)
1222 END IF
1223 CALL qcschema_env_release(qcschema_env)
1224 END IF
1225
1226 IF (iw > 0) THEN
1227 WRITE (iw, '(/,T2,A)') &
1228 '!-------------------- End of Active Space Interface --------------------------!'
1229 CALL m_flush(iw)
1230 END IF
1231 CALL para_env%sync()
1232
1233 CALL timestop(handle)
1234
1235 END SUBROUTINE active_space_main
1236
1237! **************************************************************************************************
1238!> \brief Makes sure that the SCF orbitals used for the orbital selection are canonical. Orbitals
1239!> of a diagonalization SCF are left unchanged; non-canonical orbitals (OT) are rotated in
1240!> place once, separately within the occupied and the virtual orbitals (determinant
1241!> unchanged), together with the DBCSR copy and the OT orbital derivatives, so that later
1242!> users of the SCF orbitals see the same orbitals. The orbitals are left untouched for
1243!> k-points and ROKS (diagonalization only) and for orbitals localized on the fly in OT
1244!> (ON_THE_FLY_LOC), where MANUAL indices refer to the localized orbitals.
1245!> \param qs_env the QS environment
1246!> \param mo_set the SCF orbitals of one spin
1247!> \param ks_matrix Kohn-Sham or Fock matrix of that spin
1248!> \param ispin spin index
1249!> \param manual MANUAL orbital selection (else CANONICAL)
1250!> \param do_kpoints k-point calculation
1251!> \param iw output unit (> 0 on the I/O node)
1252! **************************************************************************************************
1253 SUBROUTINE ensure_canonical_orbitals(qs_env, mo_set, ks_matrix, ispin, manual, do_kpoints, iw)
1254 TYPE(qs_environment_type), POINTER :: qs_env
1255 TYPE(mo_set_type), INTENT(INOUT) :: mo_set
1256 TYPE(dbcsr_type), POINTER :: ks_matrix
1257 INTEGER, INTENT(IN) :: ispin
1258 LOGICAL, INTENT(IN) :: manual, do_kpoints
1259 INTEGER, INTENT(IN) :: iw
1260
1261 LOGICAL :: on_the_fly_loc, rotated
1262 REAL(kind=dp) :: eps_canonical, max_offdiag
1263 REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: energies
1264 TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: mo_derivs
1265 TYPE(dbcsr_type), POINTER :: mo_deriv
1266 TYPE(dft_control_type), POINTER :: dft_control
1267 TYPE(scf_control_type), POINTER :: scf_control
1268 TYPE(section_vals_type), POINTER :: input
1269
1270 ! k-point SCF and ROKS use diagonalization; ROKS orbitals are canonical with respect to the
1271 ! ROKS Fock matrix and not to the spin Fock matrices, so the test below does not apply
1272 IF (do_kpoints) RETURN
1273 NULLIFY (dft_control, input, mo_derivs, mo_deriv, scf_control)
1274 CALL get_qs_env(qs_env, dft_control=dft_control, scf_control=scf_control, input=input, &
1275 mo_derivs=mo_derivs)
1276 IF (dft_control%roks) RETURN
1277
1278 ! a converged diagonalization SCF leaves off-diagonal elements of the order of EPS_SCF
1279 eps_canonical = max(1.0e-4_dp, 100.0_dp*scf_control%eps_scf)
1280 IF (mo_set%use_mo_coeff_b) CALL copy_dbcsr_to_fm(mo_set%mo_coeff_b, mo_set%mo_coeff)
1281 ALLOCATE (energies(mo_set%nmo))
1282 CALL calculate_orbital_fock_elements(mo_set%mo_coeff, ks_matrix, energies, max_offdiag)
1283 DEALLOCATE (energies)
1284 IF (max_offdiag <= eps_canonical) THEN
1285 IF (iw > 0) WRITE (iw, '(/,T3,A,I0,A,ES9.2,A)') "SCF orbitals of spin ", ispin, &
1286 " are canonical (max |<i|F|j>| = ", max_offdiag, ")"
1287 RETURN
1288 END IF
1289
1290 on_the_fly_loc = .false.
1291 IF (scf_control%use_ot) CALL section_vals_val_get(input, "DFT%SCF%OT%ON_THE_FLY_LOC", l_val=on_the_fly_loc)
1292 IF (on_the_fly_loc) THEN
1293 IF (.NOT. manual) THEN
1294 CALL cp_abort(__location__, &
1295 "ORBITAL_SELECTION CANONICAL needs canonical orbitals, but the "// &
1296 "SCF orbitals are localized (ON_THE_FLY_LOC). Use ORBITAL_SELECTION MANUAL "// &
1297 "to select localized orbitals, or switch off ON_THE_FLY_LOC.")
1298 END IF
1299 IF (iw > 0) WRITE (iw, '(/,T3,A,I0,A)') "SCF orbitals of spin ", ispin, &
1300 " are localized (ON_THE_FLY_LOC): ACTIVE_ORBITAL_INDICES refer to the localized orbitals"
1301 RETURN
1302 END IF
1303
1304 IF (ASSOCIATED(mo_derivs)) THEN
1305 IF (SIZE(mo_derivs) >= ispin) mo_deriv => mo_derivs(ispin)%matrix
1306 END IF
1307 CALL make_mo_set_canonical(mo_set, ks_matrix, eps_canonical, rotated, max_offdiag, mo_deriv)
1308 IF (iw > 0) THEN
1309 WRITE (iw, '(/,T3,A,I0,A,ES9.2,A)') "SCF orbitals of spin ", ispin, &
1310 " are not canonical (max |<i|F|j>| = ", max_offdiag, ")"
1311 WRITE (iw, '(T3,A)') "Rotated into canonical orbitals, separately for occupied and virtual orbitals"
1312 END IF
1313
1314 END SUBROUTINE ensure_canonical_orbitals
1315
1316! **************************************************************************************************
1317!> \brief Preconditioner (FULL_SINGLE_INVERSE) for the OT eigensolver that computes the virtual
1318!> orbitals not available from the SCF; without it the eigensolver hardly converges.
1319!> \param preconditioner the preconditioner, not associated if no virtual orbitals are needed
1320!> \param mo_set the SCF orbitals of one spin
1321!> \param ks_matrix Kohn-Sham or Fock matrix of that spin
1322!> \param s_matrix overlap matrix
1323!> \param nmo_virtual number of virtual orbitals to compute
1324!> \param para_env ...
1325!> \param blacs_env ...
1326! **************************************************************************************************
1327 SUBROUTINE virtual_orbital_preconditioner(preconditioner, mo_set, ks_matrix, s_matrix, nmo_virtual, &
1328 para_env, blacs_env)
1329 TYPE(preconditioner_type), POINTER :: preconditioner
1330 TYPE(mo_set_type), INTENT(IN) :: mo_set
1331 TYPE(dbcsr_type), POINTER :: ks_matrix, s_matrix
1332 INTEGER, INTENT(IN) :: nmo_virtual
1333 TYPE(mp_para_env_type), POINTER :: para_env
1334 TYPE(cp_blacs_env_type), POINTER :: blacs_env
1335
1336 NULLIFY (preconditioner)
1337 IF (nmo_virtual <= 0) RETURN
1338 ALLOCATE (preconditioner)
1339 CALL init_preconditioner(preconditioner, para_env=para_env, blacs_env=blacs_env)
1341 solver_type=ot_precond_solver_default, matrix_h=ks_matrix, &
1342 matrix_s=s_matrix, convert_precond_to_dbcsr=.true., &
1343 mo_set=mo_set, energy_gap=0.2_dp)
1344
1345 END SUBROUTINE virtual_orbital_preconditioner
1346
1347! **************************************************************************************************
1348!> \brief Warns if active and non-active orbitals are degenerate, i.e. if the active space
1349!> splits a set of degenerate orbitals. The orbitals of such a set are only defined up
1350!> to rotations within the set, so the active space is not uniquely defined.
1351!> \param active_space_env the active space environment
1352!> \param mos the SCF orbitals with their eigenvalues
1353! **************************************************************************************************
1354 SUBROUTINE check_active_space_degeneracy(active_space_env, mos)
1355 TYPE(active_space_type), POINTER :: active_space_env
1356 TYPE(mo_set_type), DIMENSION(:), INTENT(IN) :: mos
1357
1358 REAL(kind=dp), PARAMETER :: eps_degenerate = 1.0e-5_dp
1359
1360 CHARACTER(LEN=200) :: message
1361 INTEGER :: first_active, first_other, ispin, mspin, &
1362 nmo, npairs
1363 REAL(kind=dp), DIMENSION(:), POINTER :: eigenvalues
1364
1365 DO ispin = 1, SIZE(active_space_env%active_orbitals, 2)
1366 mspin = min(ispin, SIZE(mos))
1367 CALL get_mo_set(mos(mspin), nmo=nmo, eigenvalues=eigenvalues)
1368 ! no orbital energies available
1369 IF (.NOT. ASSOCIATED(eigenvalues)) cycle
1370 IF (all(abs(eigenvalues(1:nmo)) < epsilon(1.0_dp))) cycle
1371 CALL find_degenerate_splits(eigenvalues(1:nmo), active_space_env%active_orbitals(:, ispin), &
1372 eps_degenerate, npairs, first_active, first_other)
1373 IF (npairs > 0) THEN
1374 WRITE (message, '(A,I0,A,I0,A,I0,A,I0,A)') &
1375 "The active space splits a set of degenerate orbitals: active orbital ", first_active, &
1376 " and inactive/virtual orbital ", first_other, " (spin ", ispin, ", ", npairs, &
1377 " degenerate pairs in total)."
1378 cpwarn(trim(message))
1379 END IF
1380 END DO
1381
1382 END SUBROUTINE check_active_space_degeneracy
1383
1384! **************************************************************************************************
1385!> \brief Writes the orbitals of the active space Hamiltonian to a Molden file: the inactive
1386!> orbitals first, then the active orbitals in the order of the active orbital list,
1387!> i.e. the orbital order of the FCIDUMP file.
1388!> \param qs_env the QS environment
1389!> \param active_space_env the active space environment
1390!> \param as_input the ACTIVE_SPACE input section
1391! **************************************************************************************************
1392 SUBROUTINE write_active_space_molden(qs_env, active_space_env, as_input)
1393 TYPE(qs_environment_type), POINTER :: qs_env
1394 TYPE(active_space_type), POINTER :: active_space_env
1395 TYPE(section_vals_type), POINTER :: as_input
1396
1397 INTEGER :: i, iorb, ispin, m, nao, nmo_active, &
1398 nmo_inactive, nmo_out, nspins
1399 REAL(kind=dp) :: maxocc
1400 TYPE(cell_type), POINTER :: cell
1401 TYPE(cp_fm_struct_type), POINTER :: fm_struct
1402 TYPE(cp_fm_type), POINTER :: mo_coeff_active, mo_coeff_inactive
1403 TYPE(mo_set_type), ALLOCATABLE, DIMENSION(:) :: mos_out
1404 TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
1405 TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
1406 TYPE(section_vals_type), POINTER :: molden_section
1407
1408 NULLIFY (cell, fm_struct, mo_coeff_active, mo_coeff_inactive, particle_set, qs_kind_set)
1409 CALL get_qs_env(qs_env, cell=cell, particle_set=particle_set, qs_kind_set=qs_kind_set)
1410 molden_section => section_vals_get_subs_vals(as_input, "MOLDEN")
1411
1412 nspins = SIZE(active_space_env%mos_active)
1413 nmo_active = SIZE(active_space_env%active_orbitals, 1)
1414 nmo_inactive = SIZE(active_space_env%inactive_orbitals, 1)
1415 nmo_out = nmo_inactive + nmo_active
1416 ALLOCATE (mos_out(nspins))
1417 DO ispin = 1, nspins
1418 CALL get_mo_set(active_space_env%mos_active(ispin), nao=nao, maxocc=maxocc, &
1419 mo_coeff=mo_coeff_active)
1420 CALL get_mo_set(active_space_env%mos_inactive(ispin), mo_coeff=mo_coeff_inactive)
1421 CALL allocate_mo_set(mos_out(ispin), nao, nmo_out, 0, 0.0_dp, maxocc, 0.0_dp)
1422 CALL cp_fm_struct_create(fm_struct, nrow_global=nao, ncol_global=nmo_out, &
1423 template_fmstruct=mo_coeff_active%matrix_struct)
1424 CALL init_mo_set(mos_out(ispin), fm_struct=fm_struct, name="active space orbitals")
1425 CALL cp_fm_struct_release(fm_struct)
1426 ! the MO sets of the active space store each orbital in the column of its MO index
1427 DO i = 1, nmo_inactive
1428 m = active_space_env%inactive_orbitals(i, ispin)
1429 CALL cp_fm_to_fm(mo_coeff_inactive, mos_out(ispin)%mo_coeff, 1, m, i)
1430 mos_out(ispin)%eigenvalues(i) = active_space_env%mos_inactive(ispin)%eigenvalues(m)
1431 mos_out(ispin)%occupation_numbers(i) = active_space_env%mos_inactive(ispin)%occupation_numbers(m)
1432 END DO
1433 DO i = 1, nmo_active
1434 m = active_space_env%active_orbitals(i, ispin)
1435 iorb = nmo_inactive + i
1436 CALL cp_fm_to_fm(mo_coeff_active, mos_out(ispin)%mo_coeff, 1, m, iorb)
1437 mos_out(ispin)%eigenvalues(iorb) = active_space_env%mos_active(ispin)%eigenvalues(m)
1438 mos_out(ispin)%occupation_numbers(iorb) = active_space_env%mos_active(ispin)%occupation_numbers(m)
1439 END DO
1440 END DO
1441
1442 CALL write_mos_molden(mos_out, qs_kind_set, particle_set, molden_section, cell=cell)
1443
1444 DO ispin = 1, nspins
1445 CALL deallocate_mo_set(mos_out(ispin))
1446 END DO
1447 DEALLOCATE (mos_out)
1448
1449 END SUBROUTINE write_active_space_molden
1450
1451! **************************************************************************************************
1452!> \brief computes the alpha-beta overlap within the active subspace
1453!> \param mos the molecular orbital set within the active subspace
1454!> \param qs_env ...
1455!> \param active_space_env ...
1456!> \par History
1457!> 04.2016 created [JGH]
1458! **************************************************************************************************
1459 SUBROUTINE calculate_spin_pol_overlap(mos, qs_env, active_space_env)
1460
1461 TYPE(mo_set_type), DIMENSION(:), INTENT(IN) :: mos
1462 TYPE(qs_environment_type), POINTER :: qs_env
1463 TYPE(active_space_type), POINTER :: active_space_env
1464
1465 CHARACTER(LEN=*), PARAMETER :: routinen = 'calculate_spin_pol_overlap'
1466
1467 INTEGER :: handle, nmo, nspins
1468 LOGICAL :: do_kpoints
1469 TYPE(cp_fm_type), POINTER :: mo_coeff_a, mo_coeff_b
1470 TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: s_matrix
1471 TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: s_matrix_kp
1472
1473 CALL timeset(routinen, handle)
1474
1475 nspins = active_space_env%nspins
1476
1477 ! overlap in AO
1478 IF (nspins > 1) THEN
1479 CALL get_qs_env(qs_env, do_kpoints=do_kpoints)
1480 IF (do_kpoints) THEN
1481 CALL get_qs_env(qs_env, matrix_s_kp=s_matrix_kp)
1482 s_matrix => s_matrix_kp(:, 1)
1483 ELSE
1484 CALL get_qs_env(qs_env, matrix_s=s_matrix)
1485 END IF
1486 ALLOCATE (active_space_env%sab_sub(1))
1487
1488 CALL get_mo_set(mo_set=mos(1), mo_coeff=mo_coeff_a, nmo=nmo)
1489 CALL get_mo_set(mo_set=mos(2), mo_coeff=mo_coeff_b, nmo=nmo)
1490 CALL subspace_operator(mo_coeff_a, nmo, s_matrix(1)%matrix, active_space_env%sab_sub(1), mo_coeff_b)
1491 END IF
1492
1493 CALL timestop(handle)
1494
1495 END SUBROUTINE calculate_spin_pol_overlap
1496
1497! **************************************************************************************************
1498!> \brief computes the one-electron operators in the subspace of the provided orbital set
1499!> \param mos the molecular orbital set within the active subspace
1500!> \param qs_env ...
1501!> \param active_space_env ...
1502!> \par History
1503!> 04.2016 created [JGH]
1504! **************************************************************************************************
1505 SUBROUTINE calculate_operators(mos, qs_env, active_space_env)
1506
1507 TYPE(mo_set_type), DIMENSION(:), INTENT(IN) :: mos
1508 TYPE(qs_environment_type), POINTER :: qs_env
1509 TYPE(active_space_type), POINTER :: active_space_env
1510
1511 CHARACTER(LEN=*), PARAMETER :: routinen = 'calculate_operators'
1512
1513 INTEGER :: handle, ispin, nmo, nspins
1514 TYPE(cp_fm_type), POINTER :: mo_coeff
1515 TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: h_matrix, ks_matrix
1516
1517 CALL timeset(routinen, handle)
1518
1519 nspins = active_space_env%nspins
1520
1521 ! Kohn-Sham / Fock operator
1522 CALL cp_fm_release(active_space_env%ks_sub)
1523 CALL get_qs_env(qs_env, matrix_ks_kp=ks_matrix)
1524 ALLOCATE (active_space_env%ks_sub(nspins))
1525 DO ispin = 1, nspins
1526 CALL get_mo_set(mo_set=mos(ispin), mo_coeff=mo_coeff, nmo=nmo)
1527 CALL subspace_operator(mo_coeff, nmo, ks_matrix(ispin, 1)%matrix, active_space_env%ks_sub(ispin))
1528 END DO
1529
1530 ! Core Hamiltonian
1531 CALL cp_fm_release(active_space_env%h_sub)
1532
1533 NULLIFY (h_matrix)
1534 CALL get_qs_env(qs_env=qs_env, matrix_h_kp=h_matrix)
1535 ALLOCATE (active_space_env%h_sub(nspins))
1536 DO ispin = 1, nspins
1537 CALL get_mo_set(mo_set=mos(ispin), mo_coeff=mo_coeff, nmo=nmo)
1538 CALL subspace_operator(mo_coeff, nmo, h_matrix(1, 1)%matrix, active_space_env%h_sub(ispin))
1539 END DO
1540
1541 CALL timestop(handle)
1542
1543 END SUBROUTINE calculate_operators
1544
1545! **************************************************************************************************
1546!> \brief computes a one-electron operator in the subspace of the provided orbital set
1547!> \param mo_coeff the orbital coefficient matrix
1548!> \param nmo the number of subspace orbitals
1549!> \param op_matrix operator matrix in AO basis
1550!> \param op_sub operator in orbital basis
1551!> \param mo_coeff_b the beta orbital coefficients
1552!> \par History
1553!> 04.2016 created [JGH]
1554! **************************************************************************************************
1555 SUBROUTINE subspace_operator(mo_coeff, nmo, op_matrix, op_sub, mo_coeff_b)
1556
1557 TYPE(cp_fm_type), INTENT(IN) :: mo_coeff
1558 INTEGER, INTENT(IN) :: nmo
1559 TYPE(dbcsr_type), POINTER :: op_matrix
1560 TYPE(cp_fm_type), INTENT(INOUT) :: op_sub
1561 TYPE(cp_fm_type), INTENT(IN), OPTIONAL :: mo_coeff_b
1562
1563 CHARACTER(LEN=*), PARAMETER :: routinen = 'subspace_operator'
1564
1565 INTEGER :: handle, ncol, nrow
1566 TYPE(cp_fm_type) :: vectors
1567
1568 CALL timeset(routinen, handle)
1569
1570 CALL cp_fm_get_info(matrix=mo_coeff, ncol_global=ncol, nrow_global=nrow)
1571 cpassert(nmo <= ncol)
1572
1573 IF (nmo > 0) THEN
1574 CALL cp_fm_create(vectors, mo_coeff%matrix_struct, "vectors")
1575 CALL create_subspace_matrix(mo_coeff, op_sub, nmo)
1576
1577 IF (PRESENT(mo_coeff_b)) THEN
1578 ! if beta orbitals are present, compute the cross alpha_beta term
1579 CALL cp_dbcsr_sm_fm_multiply(op_matrix, mo_coeff_b, vectors, nmo)
1580 ELSE
1581 ! otherwise the same spin, whatever that is
1582 CALL cp_dbcsr_sm_fm_multiply(op_matrix, mo_coeff, vectors, nmo)
1583 END IF
1584
1585 CALL parallel_gemm('T', 'N', nmo, nmo, nrow, 1.0_dp, mo_coeff, vectors, 0.0_dp, op_sub)
1586 CALL cp_fm_release(vectors)
1587 END IF
1588
1589 CALL timestop(handle)
1590
1591 END SUBROUTINE subspace_operator
1592
1593! **************************************************************************************************
1594!> \brief creates a matrix of subspace size
1595!> \param orbitals the orbital coefficient matrix
1596!> \param op_sub operator in orbital basis
1597!> \param n the number of orbitals
1598!> \par History
1599!> 04.2016 created [JGH]
1600! **************************************************************************************************
1601 SUBROUTINE create_subspace_matrix(orbitals, op_sub, n)
1602
1603 TYPE(cp_fm_type), INTENT(IN) :: orbitals
1604 TYPE(cp_fm_type), INTENT(OUT) :: op_sub
1605 INTEGER, INTENT(IN) :: n
1606
1607 TYPE(cp_fm_struct_type), POINTER :: fm_struct
1608
1609 IF (n > 0) THEN
1610
1611 NULLIFY (fm_struct)
1612 CALL cp_fm_struct_create(fm_struct, nrow_global=n, ncol_global=n, &
1613 para_env=orbitals%matrix_struct%para_env, &
1614 context=orbitals%matrix_struct%context)
1615 CALL cp_fm_create(op_sub, fm_struct, name="Subspace operator")
1616 CALL cp_fm_struct_release(fm_struct)
1617
1618 END IF
1619
1620 END SUBROUTINE create_subspace_matrix
1621
1622! **************************************************************************************************
1623!> \brief computes the electron repulsion integrals using the GPW technology
1624!> \param mos the molecular orbital set within the active subspace
1625!> \param orbitals ...
1626!> \param eri_env ...
1627!> \param qs_env ...
1628!> \param iw ...
1629!> \param restricted ...
1630!> \par History
1631!> 04.2016 created [JGH]
1632! **************************************************************************************************
1633 SUBROUTINE calculate_eri_gpw(mos, orbitals, eri_env, qs_env, iw, restricted)
1634
1635 TYPE(mo_set_type), DIMENSION(:), INTENT(IN) :: mos
1636 INTEGER, DIMENSION(:, :), POINTER :: orbitals
1637 TYPE(eri_type) :: eri_env
1638 TYPE(qs_environment_type), POINTER :: qs_env
1639 INTEGER, INTENT(IN) :: iw
1640 LOGICAL, INTENT(IN) :: restricted
1641
1642 CHARACTER(LEN=*), PARAMETER :: routinen = 'calculate_eri_gpw'
1643
1644 INTEGER :: col_local, color, handle, i1, i2, i3, i4, i_multigrid, icount2, intcount, isp, &
1645 isp1, isp2, ispin, iwa1, iwa12, iwa2, iwb1, iwb12, iwb2, iwbs, iwbt, iwfn, n_multigrid, &
1646 ncol_global, ncol_local, nmm, nmo, nmo1, nmo2, nrow_global, nrow_local, nspins, &
1647 number_of_subgroups, nx, row_local, stored_integrals
1648 INTEGER, ALLOCATABLE, DIMENSION(:) :: eri_index
1649 INTEGER, DIMENSION(:), POINTER :: col_indices, row_indices
1650 LOGICAL :: print1, print2, &
1651 skip_load_balance_distributed
1652 REAL(kind=dp) :: dvol, erint, pair_int, &
1653 progression_factor, rc, rsize, t1, t2
1654 REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: eri
1655 TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
1656 TYPE(cell_type), POINTER :: cell
1657 TYPE(cp_blacs_env_type), POINTER :: blacs_env, blacs_env_sub
1658 TYPE(cp_fm_struct_type), POINTER :: fm_struct
1659 TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:) :: fm_matrix_pq_rnu, fm_matrix_pq_rs, &
1660 fm_mo_coeff_as
1661 TYPE(cp_fm_type), POINTER :: mo_coeff
1662 TYPE(dbcsr_p_type) :: mat_munu
1663 TYPE(dbcsr_type), ALLOCATABLE, DIMENSION(:) :: matrix_pq_rnu, mo_coeff_as
1664 TYPE(dft_control_type), POINTER :: dft_control
1665 TYPE(mp_para_env_type), POINTER :: para_env
1666 TYPE(neighbor_list_set_p_type), DIMENSION(:), &
1667 POINTER :: sab_orb_sub
1668 TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
1669 TYPE(pw_c1d_gs_type) :: pot_g, rho_g
1670 TYPE(pw_env_type), POINTER :: pw_env_sub
1671 TYPE(pw_poisson_type), POINTER :: poisson_env
1672 TYPE(pw_pool_type), POINTER :: auxbas_pw_pool
1673 TYPE(pw_r3d_rs_type) :: rho_r, wfn_r
1674 TYPE(pw_r3d_rs_type), ALLOCATABLE, &
1675 DIMENSION(:, :), TARGET :: wfn_a
1676 TYPE(pw_r3d_rs_type), POINTER :: wfn1, wfn2, wfn3, wfn4
1677 TYPE(qs_control_type), POINTER :: qs_control, qs_control_old
1678 TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
1679 TYPE(qs_ks_env_type), POINTER :: ks_env
1680 TYPE(task_list_type), POINTER :: task_list_sub
1681
1682 CALL timeset(routinen, handle)
1683
1684 IF (iw > 0) t1 = m_walltime()
1685
1686 ! print levels
1687 SELECT CASE (eri_env%eri_gpw%print_level)
1688 CASE (silent_print_level)
1689 print1 = .false.
1690 print2 = .false.
1691 CASE (low_print_level)
1692 print1 = .false.
1693 print2 = .false.
1694 CASE (medium_print_level)
1695 print1 = .true.
1696 print2 = .false.
1697 CASE (high_print_level)
1698 print1 = .true.
1699 print2 = .true.
1700 CASE (debug_print_level)
1701 print1 = .true.
1702 print2 = .true.
1703 CASE DEFAULT
1704 ! do nothing
1705 END SELECT
1706
1707 ! Check the input group
1708 CALL get_qs_env(qs_env, para_env=para_env, blacs_env=blacs_env)
1709 IF (eri_env%eri_gpw%group_size < 1) eri_env%eri_gpw%group_size = para_env%num_pe
1710 IF (mod(para_env%num_pe, eri_env%eri_gpw%group_size) /= 0) THEN
1711 cpabort("Group size must be a divisor of the total number of processes!")
1712 END IF
1713 ! Create a new para_env or reuse the old one
1714 IF (eri_env%eri_gpw%group_size == para_env%num_pe) THEN
1715 eri_env%para_env_sub => para_env
1716 CALL eri_env%para_env_sub%retain()
1717 blacs_env_sub => blacs_env
1718 CALL blacs_env_sub%retain()
1719 number_of_subgroups = 1
1720 color = 0
1721 ELSE
1722 number_of_subgroups = para_env%num_pe/eri_env%eri_gpw%group_size
1723 color = para_env%mepos/eri_env%eri_gpw%group_size
1724 ALLOCATE (eri_env%para_env_sub)
1725 CALL eri_env%para_env_sub%from_split(para_env, color)
1726 NULLIFY (blacs_env_sub)
1727 CALL cp_blacs_env_create(blacs_env_sub, eri_env%para_env_sub, blacs_grid_square, .true.)
1728 END IF
1729 CALL eri_env%comm_exchange%from_split(para_env, eri_env%para_env_sub%mepos)
1730
1731 ! This should be done differently! Copied from MP2 code
1732 CALL get_qs_env(qs_env, dft_control=dft_control)
1733 ALLOCATE (qs_control)
1734 qs_control_old => dft_control%qs_control
1735 qs_control = qs_control_old
1736 dft_control%qs_control => qs_control
1737 progression_factor = qs_control%progression_factor
1738 n_multigrid = SIZE(qs_control%e_cutoff)
1739 nspins = SIZE(mos)
1740 ! In case of ROHF/ROKS, we assume the orbital coefficients in both spin channels to be the same
1741 ! and save operations by calculating ERIs from only one spin channel
1742 IF (restricted) nspins = 1
1743 ! Allocate new cutoffs (just in private qs_control, not in qs_control_old)
1744 ALLOCATE (qs_control%e_cutoff(n_multigrid))
1745
1746 qs_control%cutoff = eri_env%eri_gpw%cutoff*0.5_dp
1747 qs_control%e_cutoff(1) = qs_control%cutoff
1748 DO i_multigrid = 2, n_multigrid
1749 qs_control%e_cutoff(i_multigrid) = qs_control%e_cutoff(i_multigrid - 1) &
1750 /progression_factor
1751 END DO
1752 qs_control%relative_cutoff = eri_env%eri_gpw%rel_cutoff*0.5_dp
1753
1754 ! For now, we will distribute neighbor lists etc. within the global communicator
1755 CALL get_qs_env(qs_env, ks_env=ks_env)
1756 CALL create_mat_munu(mat_munu, qs_env, eri_env%eri_gpw%eps_grid, blacs_env_sub, sab_orb_sub=sab_orb_sub, &
1757 do_alloc_blocks_from_nbl=.true., dbcsr_sym_type=dbcsr_type_symmetric)
1758 CALL dbcsr_set(mat_munu%matrix, 0.0_dp)
1759
1760 ! Generate the appropriate pw_env
1761 NULLIFY (pw_env_sub)
1762 CALL pw_env_create(pw_env_sub)
1763 CALL pw_env_rebuild(pw_env_sub, qs_env, external_para_env=eri_env%para_env_sub)
1764 CALL pw_env_get(pw_env_sub, auxbas_pw_pool=auxbas_pw_pool, poisson_env=poisson_env)
1765
1766 ! TODO: maybe we can let `pw_env_rebuild` do what we manually overwrite here?
1767 IF (eri_env%eri_gpw%redo_poisson) THEN
1768 ! We need to rebuild the Poisson solver on the fly
1769 IF (sum(eri_env%periodicity) /= 0) THEN
1770 poisson_env%parameters%solver = pw_poisson_periodic
1771 ELSE
1772 poisson_env%parameters%solver = pw_poisson_analytic
1773 END IF
1774 poisson_env%parameters%periodic = eri_env%periodicity
1775
1776 ! Rebuilds the poisson green (influence) function according
1777 ! to the poisson solver and parameters set so far.
1778 ! Also sets the variable poisson_env%rebuild to .FALSE.
1779 CALL pw_poisson_rebuild(poisson_env)
1780
1781 ! set the cutoff radius for the Greens function in case we use ANALYTIC Poisson solver
1782 CALL get_qs_env(qs_env, cell=cell)
1783 rc = cell%hmat(1, 1)
1784 DO iwa1 = 1, 3
1785 ! TODO: I think this is not the largest possible radius inscribed in the cell
1786 rc = min(rc, 0.5_dp*cell%hmat(iwa1, iwa1))
1787 END DO
1788 poisson_env%green_fft%radius = rc
1789
1790 ! Overwrite the Greens function with the one we want
1791 CALL pw_eri_green_create(poisson_env%green_fft, eri_env)
1792
1793 IF (iw > 0) THEN
1794 CALL get_qs_env(qs_env, cell=cell)
1795 IF (sum(cell%perd) /= sum(eri_env%periodicity)) THEN
1796 IF (sum(eri_env%periodicity) /= 0) THEN
1797 WRITE (unit=iw, fmt="(/,T2,A,T51,A30)") &
1798 "ERI_GPW| Switching Poisson solver to", "PERIODIC"
1799 ELSE
1800 WRITE (unit=iw, fmt="(/,T2,A,T51,A30)") &
1801 "ERI_GPW| Switching Poisson solver to", "ANALYTIC"
1802 END IF
1803 END IF
1804 ! print out the Greens function to check it matches the Poisson solver
1805 SELECT CASE (poisson_env%green_fft%method)
1806 CASE (periodic3d)
1807 WRITE (unit=iw, fmt="(T2,A,T51,A30)") &
1808 "ERI_GPW| Poisson Greens function", "PERIODIC"
1809 CASE (analytic0d)
1810 WRITE (unit=iw, fmt="(T2,A,T51,A30)") &
1811 "ERI_GPW| Poisson Greens function", "ANALYTIC"
1812 WRITE (unit=iw, fmt="(T2,A,T71,F10.4)") "ERI_GPW| Poisson cutoff radius", &
1813 poisson_env%green_fft%radius*angstrom
1814 CASE DEFAULT
1815 cpabort("Wrong Greens function setup")
1816 END SELECT
1817 END IF
1818 END IF
1819
1820 ALLOCATE (mo_coeff_as(nspins), fm_mo_coeff_as(nspins))
1821 DO ispin = 1, nspins
1822 block
1823 REAL(kind=dp), DIMENSION(:, :), ALLOCATABLE :: c, c_active
1824 INTEGER :: nmo
1825 TYPE(group_dist_d1_type) :: gd_array
1826 TYPE(cp_fm_type), POINTER :: mo_coeff
1827 CALL get_mo_set(mos(ispin), mo_coeff=mo_coeff, nmo=nmo)
1828 CALL grep_rows_in_subgroups(para_env, eri_env%para_env_sub, mo_coeff, gd_array, c)
1829
1830 ALLOCATE (c_active(SIZE(c, 1), SIZE(orbitals, 1)))
1831 DO i1 = 1, SIZE(orbitals, 1)
1832 c_active(:, i1) = c(:, orbitals(i1, ispin))
1833 END DO
1834 CALL build_dbcsr_from_rows(eri_env%para_env_sub, mo_coeff_as(ispin), &
1835 c_active, mat_munu%matrix, gd_array, eri_env%eri_gpw%eps_filter)
1836 CALL release_group_dist(gd_array)
1837 DEALLOCATE (c, c_active)
1838 END block
1839
1840 CALL dbcsr_get_info(mo_coeff_as(ispin), nfullrows_total=nrow_global, nfullcols_total=ncol_global)
1841
1842 NULLIFY (fm_struct)
1843 CALL cp_fm_struct_create(fm_struct, context=blacs_env_sub, para_env=eri_env%para_env_sub, &
1844 nrow_global=nrow_global, ncol_global=ncol_global)
1845 CALL cp_fm_create(fm_mo_coeff_as(ispin), fm_struct)
1846 CALL cp_fm_struct_release(fm_struct)
1847
1848 CALL copy_dbcsr_to_fm(mo_coeff_as(ispin), fm_mo_coeff_as(ispin))
1849 END DO
1850
1851 IF (eri_env%method == eri_method_gpw_ht) THEN
1852 ! We need a task list
1853 NULLIFY (task_list_sub)
1854 skip_load_balance_distributed = dft_control%qs_control%skip_load_balance_distributed
1855 CALL allocate_task_list(task_list_sub)
1856 CALL generate_qs_task_list(ks_env, task_list_sub, basis_type="ORB", &
1857 reorder_rs_grid_ranks=.true., &
1858 skip_load_balance_distributed=skip_load_balance_distributed, &
1859 pw_env_external=pw_env_sub, sab_orb_external=sab_orb_sub)
1860
1861 ! Create sparse matrices carrying the matrix products, Code borrowed from the MP2 GPW method
1862 ! Create equal distributions for them (no sparsity present)
1863 ! We use the routines from mp2 suggesting that one may replicate the grids later for better performance
1864 ALLOCATE (matrix_pq_rnu(nspins), fm_matrix_pq_rnu(nspins), fm_matrix_pq_rs(nspins))
1865 DO ispin = 1, nspins
1866 CALL dbcsr_create(matrix_pq_rnu(ispin), template=mo_coeff_as(ispin))
1867 CALL dbcsr_set(matrix_pq_rnu(ispin), 0.0_dp)
1868
1869 CALL dbcsr_get_info(matrix_pq_rnu(ispin), nfullrows_total=nrow_global, nfullcols_total=ncol_global)
1870
1871 NULLIFY (fm_struct)
1872 CALL cp_fm_struct_create(fm_struct, context=blacs_env_sub, para_env=eri_env%para_env_sub, &
1873 nrow_global=nrow_global, ncol_global=ncol_global)
1874 CALL cp_fm_create(fm_matrix_pq_rnu(ispin), fm_struct)
1875 CALL cp_fm_struct_release(fm_struct)
1876
1877 NULLIFY (fm_struct)
1878 CALL cp_fm_struct_create(fm_struct, context=blacs_env_sub, para_env=eri_env%para_env_sub, &
1879 nrow_global=ncol_global, ncol_global=ncol_global)
1880 CALL cp_fm_create(fm_matrix_pq_rs(ispin), fm_struct)
1881 CALL cp_fm_struct_release(fm_struct)
1882 END DO
1883
1884 ! Copy the active space of the MOs into DBCSR matrices
1885 END IF
1886
1887 CALL auxbas_pw_pool%create_pw(wfn_r)
1888 CALL auxbas_pw_pool%create_pw(rho_g)
1889 CALL get_qs_env(qs_env, qs_kind_set=qs_kind_set, cell=cell, &
1890 particle_set=particle_set, atomic_kind_set=atomic_kind_set)
1891
1892 ! pre-calculate wavefunctions on reals space grid
1893 nspins = SIZE(mos)
1894 ! In case of ROHF/ROKS, we assume the orbital coefficients in both spin channels to be the same
1895 ! and save operations by calculating ERIs from only one spin channel
1896 IF (restricted) nspins = 1
1897 IF (eri_env%eri_gpw%store_wfn) THEN
1898 ! pre-calculate wavefunctions on reals space grid
1899 rsize = 0.0_dp
1900 nmo = 0
1901 DO ispin = 1, nspins
1902 CALL get_mo_set(mo_set=mos(ispin), nmo=nx)
1903 nmo = max(nmo, nx)
1904 rsize = real(SIZE(wfn_r%array), kind=dp)*nx
1905 END DO
1906 IF (print1 .AND. iw > 0) THEN
1907 rsize = rsize*8._dp/1000000._dp
1908 WRITE (iw, "(T2,'ERI_GPW|',' Store active orbitals on real space grid ',T66,F12.3,' MB')") rsize
1909 END IF
1910 ALLOCATE (wfn_a(nmo, nspins))
1911 DO ispin = 1, nspins
1912 CALL get_mo_set(mo_set=mos(ispin), mo_coeff=mo_coeff, nmo=nmo)
1913 DO i1 = 1, SIZE(orbitals, 1)
1914 iwfn = orbitals(i1, ispin)
1915 CALL auxbas_pw_pool%create_pw(wfn_a(iwfn, ispin))
1916 CALL calculate_wavefunction(mo_coeff, iwfn, wfn_a(iwfn, ispin), rho_g, atomic_kind_set, &
1917 qs_kind_set, cell, dft_control, particle_set, pw_env_sub)
1918 IF (print2 .AND. iw > 0) THEN
1919 WRITE (iw, "(T2,'ERI_GPW|',' Orbital stored ',I4,' Spin ',i1)") iwfn, ispin
1920 END IF
1921 END DO
1922 END DO
1923 ELSE
1924 ! Even if we do not store all WFNs, we still need containers for the functions to store
1925 ALLOCATE (wfn1, wfn2)
1926 CALL auxbas_pw_pool%create_pw(wfn1)
1927 CALL auxbas_pw_pool%create_pw(wfn2)
1928 IF (eri_env%method /= eri_method_gpw_ht) THEN
1929 ALLOCATE (wfn3, wfn4)
1930 CALL auxbas_pw_pool%create_pw(wfn3)
1931 CALL auxbas_pw_pool%create_pw(wfn4)
1932 END IF
1933 END IF
1934
1935 ! get some of the grids ready
1936 CALL auxbas_pw_pool%create_pw(rho_r)
1937 CALL auxbas_pw_pool%create_pw(pot_g)
1938
1939 ! run the FFT once, to set up buffers and to take into account the memory
1940 CALL pw_zero(rho_r)
1941 CALL pw_transfer(rho_r, rho_g)
1942 dvol = rho_r%pw_grid%dvol
1943
1944 IF (iw > 0) THEN
1945 CALL m_flush(iw)
1946 END IF
1947 ! calculate the integrals
1948 stored_integrals = 0
1949 DO isp1 = 1, nspins
1950 CALL get_mo_set(mo_set=mos(isp1), nmo=nmo1)
1951 nmm = (nmo1*(nmo1 + 1))/2
1952 DO i1 = 1, SIZE(orbitals, 1)
1953 iwa1 = orbitals(i1, isp1)
1954 IF (eri_env%eri_gpw%store_wfn) THEN
1955 wfn1 => wfn_a(iwa1, isp1)
1956 ELSE
1957 CALL calculate_wavefunction(fm_mo_coeff_as(isp1), iwa1, wfn1, rho_g, atomic_kind_set, &
1958 qs_kind_set, cell, dft_control, particle_set, pw_env_sub)
1959 END IF
1960 DO i2 = i1, SIZE(orbitals, 1)
1961 iwa2 = orbitals(i2, isp1)
1962 iwa12 = csr_idx_to_combined(iwa1, iwa2, nmo1)
1963 ! Skip calculation directly if the pair is not part of our subgroup
1964 IF (mod(iwa12 - 1, eri_env%comm_exchange%num_pe) /= eri_env%comm_exchange%mepos) cycle
1965 iwa12 = (iwa12 - 1)/eri_env%comm_exchange%num_pe + 1
1966 IF (eri_env%eri_gpw%store_wfn) THEN
1967 wfn2 => wfn_a(iwa2, isp1)
1968 ELSE
1969 CALL calculate_wavefunction(fm_mo_coeff_as(isp1), iwa2, wfn2, rho_g, atomic_kind_set, &
1970 qs_kind_set, cell, dft_control, particle_set, pw_env_sub)
1971 END IF
1972 ! calculate charge distribution and potential
1973 CALL pw_zero(rho_r)
1974 CALL pw_multiply(rho_r, wfn1, wfn2)
1975 CALL pw_transfer(rho_r, rho_g)
1976 CALL pw_poisson_solve(poisson_env, rho_g, pair_int, pot_g)
1977
1978 ! screening using pair_int
1979 IF (pair_int < eri_env%eps_integral) cycle
1980 CALL pw_transfer(pot_g, rho_r)
1981 !
1982 IF (eri_env%method == eri_method_gpw_ht) THEN
1983 CALL pw_scale(rho_r, dvol)
1984 DO isp2 = isp1, nspins
1985 CALL get_mo_set(mo_set=mos(isp2), nmo=nmo2)
1986 nx = (nmo2*(nmo2 + 1))/2
1987 ALLOCATE (eri(nx), eri_index(nx))
1988 CALL dbcsr_set(mat_munu%matrix, 0.0_dp)
1989 CALL integrate_v_rspace(rho_r, hmat=mat_munu, qs_env=qs_env, &
1990 calculate_forces=.false., compute_tau=.false., gapw=.false., &
1991 pw_env_external=pw_env_sub, task_list_external=task_list_sub)
1992
1993 CALL dbcsr_multiply("N", "N", 1.0_dp, mat_munu%matrix, mo_coeff_as(isp2), &
1994 0.0_dp, matrix_pq_rnu(isp2), filter_eps=eri_env%eri_gpw%eps_filter)
1995 CALL copy_dbcsr_to_fm(matrix_pq_rnu(isp2), fm_matrix_pq_rnu(isp2))
1996
1997 CALL cp_fm_get_info(fm_matrix_pq_rnu(isp2), ncol_global=ncol_global, nrow_global=nrow_global)
1998
1999 CALL parallel_gemm("T", "N", ncol_global, ncol_global, nrow_global, 0.5_dp, &
2000 fm_matrix_pq_rnu(isp2), fm_mo_coeff_as(isp2), &
2001 0.0_dp, fm_matrix_pq_rs(isp2))
2002 CALL parallel_gemm("T", "N", ncol_global, ncol_global, nrow_global, 0.5_dp, &
2003 fm_mo_coeff_as(isp2), fm_matrix_pq_rnu(isp2), &
2004 1.0_dp, fm_matrix_pq_rs(isp2))
2005
2006 CALL cp_fm_get_info(fm_matrix_pq_rs(isp2), ncol_local=ncol_local, nrow_local=nrow_local, &
2007 col_indices=col_indices, row_indices=row_indices)
2008
2009 icount2 = 0
2010 DO col_local = 1, ncol_local
2011 iwb1 = orbitals(col_indices(col_local), isp2)
2012 IF (isp1 == isp2 .AND. iwb1 < iwa1) cycle
2013 DO row_local = 1, nrow_local
2014 iwb2 = orbitals(row_indices(row_local), isp2)
2015 IF (iwb2 < iwb1) cycle
2016 IF (isp1 == isp2 .AND. iwa1 == iwb1 .AND. iwb2 < iwa2) cycle
2017
2018 iwb12 = csr_idx_to_combined(iwb1, iwb2, nmo2)
2019 erint = fm_matrix_pq_rs(isp2)%local_data(row_local, col_local)
2020 IF (abs(erint) > eri_env%eps_integral) THEN
2021 icount2 = icount2 + 1
2022 eri(icount2) = erint
2023 eri_index(icount2) = iwb12
2024 END IF
2025 END DO
2026 END DO
2027 stored_integrals = stored_integrals + icount2
2028 !
2029 isp = (isp1 - 1)*isp2 + (isp2 - isp1 + 1)
2030 CALL update_csr_matrix(eri_env%eri(isp)%csr_mat, icount2, eri, eri_index, iwa12)
2031 !
2032 DEALLOCATE (eri, eri_index)
2033 END DO
2034 ELSE IF (eri_env%method == eri_method_full_gpw) THEN
2035 DO isp2 = isp1, nspins
2036 CALL get_mo_set(mo_set=mos(isp2), nmo=nmo2)
2037 nx = (nmo2*(nmo2 + 1))/2
2038 ALLOCATE (eri(nx), eri_index(nx))
2039 icount2 = 0
2040 iwbs = 1
2041 IF (isp1 == isp2) iwbs = i1
2042 isp = (isp1 - 1)*isp2 + (isp2 - isp1 + 1)
2043 DO i3 = iwbs, SIZE(orbitals, 1)
2044 iwb1 = orbitals(i3, isp2)
2045 IF (eri_env%eri_gpw%store_wfn) THEN
2046 wfn3 => wfn_a(iwb1, isp2)
2047 ELSE
2048 CALL calculate_wavefunction(fm_mo_coeff_as(isp1), iwb1, wfn3, rho_g, atomic_kind_set, &
2049 qs_kind_set, cell, dft_control, particle_set, pw_env_sub)
2050 END IF
2051 CALL pw_zero(wfn_r)
2052 CALL pw_multiply(wfn_r, rho_r, wfn3)
2053 iwbt = i3
2054 IF (isp1 == isp2 .AND. i1 == i3) iwbt = i2
2055 DO i4 = iwbt, SIZE(orbitals, 1)
2056 iwb2 = orbitals(i4, isp2)
2057 IF (eri_env%eri_gpw%store_wfn) THEN
2058 wfn4 => wfn_a(iwb2, isp2)
2059 ELSE
2060 CALL calculate_wavefunction(fm_mo_coeff_as(isp1), iwb2, wfn4, rho_g, atomic_kind_set, &
2061 qs_kind_set, cell, dft_control, particle_set, pw_env_sub)
2062 END IF
2063 ! We reduce the amount of communication by collecting the local sums first and sum globally later
2064 erint = pw_integral_ab(wfn_r, wfn4, local_only=.true.)
2065 icount2 = icount2 + 1
2066 eri(icount2) = erint
2067 eri_index(icount2) = csr_idx_to_combined(iwb1, iwb2, nmo2)
2068 END DO
2069 END DO
2070 ! Now, we sum the integrals globally
2071 CALL eri_env%para_env_sub%sum(eri)
2072 ! and we reorder the integrals to prevent storing too small integrals
2073 intcount = 0
2074 icount2 = 0
2075 iwbs = 1
2076 IF (isp1 == isp2) iwbs = i1
2077 isp = (isp1 - 1)*isp2 + (isp2 - isp1 + 1)
2078 DO i3 = iwbs, SIZE(orbitals, 1)
2079 iwb1 = orbitals(i3, isp2)
2080 iwbt = i3
2081 IF (isp1 == isp2 .AND. i1 == i3) iwbt = i2
2082 DO i4 = iwbt, SIZE(orbitals, 1)
2083 iwb2 = orbitals(i4, isp2)
2084 intcount = intcount + 1
2085 erint = eri(intcount)
2086 IF (abs(erint) > eri_env%eps_integral) THEN
2087 IF (mod(intcount, eri_env%para_env_sub%num_pe) == eri_env%para_env_sub%mepos) THEN
2088 icount2 = icount2 + 1
2089 eri(icount2) = erint
2090 eri_index(icount2) = eri_index(intcount)
2091 END IF
2092 END IF
2093 END DO
2094 END DO
2095 stored_integrals = stored_integrals + icount2
2096 !
2097 CALL update_csr_matrix(eri_env%eri(isp)%csr_mat, icount2, eri, eri_index, iwa12)
2098 !
2099 DEALLOCATE (eri, eri_index)
2100 END DO
2101 ELSE
2102 cpabort("Unknown option")
2103 END IF
2104 END DO
2105 END DO
2106 END DO
2107
2108 IF (print1 .AND. iw > 0) THEN
2109 WRITE (iw, "(T2,'ERI_GPW|',' Number of Integrals stored locally',T71,I10)") stored_integrals
2110 END IF
2111
2112 IF (eri_env%eri_gpw%store_wfn) THEN
2113 DO ispin = 1, nspins
2114 DO i1 = 1, SIZE(orbitals, 1)
2115 iwfn = orbitals(i1, ispin)
2116 CALL wfn_a(iwfn, ispin)%release()
2117 END DO
2118 END DO
2119 DEALLOCATE (wfn_a)
2120 ELSE
2121 CALL wfn1%release()
2122 CALL wfn2%release()
2123 DEALLOCATE (wfn1, wfn2)
2124 IF (eri_env%method /= eri_method_gpw_ht) THEN
2125 CALL wfn3%release()
2126 CALL wfn4%release()
2127 DEALLOCATE (wfn3, wfn4)
2128 END IF
2129 END IF
2130 CALL auxbas_pw_pool%give_back_pw(wfn_r)
2131 CALL auxbas_pw_pool%give_back_pw(rho_g)
2132 CALL auxbas_pw_pool%give_back_pw(rho_r)
2133 CALL auxbas_pw_pool%give_back_pw(pot_g)
2134
2135 IF (eri_env%method == eri_method_gpw_ht) THEN
2136 DO ispin = 1, nspins
2137 CALL dbcsr_release(mo_coeff_as(ispin))
2138 CALL dbcsr_release(matrix_pq_rnu(ispin))
2139 CALL cp_fm_release(fm_matrix_pq_rnu(ispin))
2140 CALL cp_fm_release(fm_matrix_pq_rs(ispin))
2141 END DO
2142 DEALLOCATE (matrix_pq_rnu, fm_matrix_pq_rnu, fm_matrix_pq_rs)
2143 CALL deallocate_task_list(task_list_sub)
2144 END IF
2145 DO ispin = 1, nspins
2146 CALL dbcsr_release(mo_coeff_as(ispin))
2147 CALL cp_fm_release(fm_mo_coeff_as(ispin))
2148 END DO
2149 DEALLOCATE (mo_coeff_as, fm_mo_coeff_as)
2150 CALL release_neighbor_list_sets(sab_orb_sub)
2151 CALL cp_blacs_env_release(blacs_env_sub)
2152 CALL dbcsr_release(mat_munu%matrix)
2153 DEALLOCATE (mat_munu%matrix)
2154 CALL pw_env_release(pw_env_sub)
2155 ! Return to the old qs_control
2156 dft_control%qs_control => qs_control_old
2157 DEALLOCATE (qs_control%e_cutoff)
2158 DEALLOCATE (qs_control)
2159
2160 ! print out progress
2161 IF (iw > 0) THEN
2162 t2 = m_walltime()
2163 WRITE (iw, '(/,T2,A,T66,F14.2)') "ERI_GPW| ERI calculation took (sec)", t2 - t1
2164 CALL m_flush(iw)
2165 END IF
2166
2167 CALL timestop(handle)
2168
2169 END SUBROUTINE calculate_eri_gpw
2170
2171! **************************************************************************************************
2172!> \brief Sets the Green's function for the ERI calculation. Here we deal with the G=0 case!
2173!> \param green ...
2174!> \param eri_env ...
2175!> \par History
2176!> 04.2016 created [JGH]
2177!> 08.2025 added support for the LR truncation [SB]
2178! **************************************************************************************************
2179 SUBROUTINE pw_eri_green_create(green, eri_env)
2180
2181 TYPE(greens_fn_type), INTENT(INOUT) :: green
2182 TYPE(eri_type) :: eri_env
2183
2184 COMPLEX(KIND=dp) :: erf_fac_p, z_p
2185 INTEGER :: ig
2186 REAL(kind=dp) :: cossin_fac, ea, erfcos_fac, exp_prefac, &
2187 g, g0, g2, g3d, ga, ginf, omega, &
2188 omega2, rc, rc2
2189
2190 ! initialize influence function
2191 associate(gf => green%influence_fn, grid => green%influence_fn%pw_grid)
2192 SELECT CASE (green%method)
2193 CASE (periodic3d)
2194
2195 SELECT CASE (eri_env%operator)
2196 CASE (eri_operator_coulomb)
2197 DO ig = grid%first_gne0, grid%ngpts_cut_local
2198 g2 = grid%gsq(ig)
2199 gf%array(ig) = fourpi/g2
2200 END DO
2201 IF (grid%have_g0) gf%array(1) = 0.0_dp
2202
2203 CASE (eri_operator_yukawa)
2204 CALL cp_warn(__location__, "Yukawa operator has not been tested")
2205 omega2 = eri_env%omega**2
2206 DO ig = grid%first_gne0, grid%ngpts_cut_local
2207 g2 = grid%gsq(ig)
2208 gf%array(ig) = fourpi/(omega2 + g2)
2209 END DO
2210 IF (grid%have_g0) gf%array(1) = fourpi/omega2
2211
2212 CASE (eri_operator_erf)
2213 omega2 = eri_env%omega**2
2214 DO ig = grid%first_gne0, grid%ngpts_cut_local
2215 g2 = grid%gsq(ig)
2216 gf%array(ig) = fourpi/g2*exp(-0.25_dp*g2/omega2)
2217 END DO
2218 IF (grid%have_g0) gf%array(1) = 0.0_dp
2219
2220 CASE (eri_operator_erfc)
2221 omega2 = eri_env%omega**2
2222 DO ig = grid%first_gne0, grid%ngpts_cut_local
2223 g2 = grid%gsq(ig)
2224 gf%array(ig) = fourpi/g2*(1.0_dp - exp(-0.25_dp*g2/omega2))
2225 END DO
2226 IF (grid%have_g0) gf%array(1) = pi/omega2
2227
2228 CASE (eri_operator_trunc)
2229 rc = eri_env%cutoff_radius
2230 DO ig = grid%first_gne0, grid%ngpts_cut_local
2231 g2 = grid%gsq(ig)
2232 g = sqrt(g2)
2233 ! Taylor expansion around zero
2234 IF (g*rc >= 0.005_dp) THEN
2235 gf%array(ig) = fourpi/g2*(1.0_dp - cos(g*rc))
2236 ELSE
2237 gf%array(ig) = fourpi/g2*(g*rc)**2/2.0_dp*(1.0_dp - (g*rc)**2/12.0_dp)
2238 END IF
2239 END DO
2240 IF (grid%have_g0) gf%array(1) = twopi*rc**2
2241
2242 CASE (eri_operator_lr_trunc)
2243 omega = eri_env%omega
2244 omega2 = omega**2
2245 rc = eri_env%cutoff_radius
2246 rc2 = rc**2
2247 g0 = 0.001_dp ! threshold for the G=0 case
2248 ginf = 20.0_dp ! threshold for the Taylor exapnsion arounf G=∞
2249 DO ig = grid%first_gne0, grid%ngpts_cut_local
2250 g2 = grid%gsq(ig)
2251 g = sqrt(g2)
2252 IF (g <= 2.0_dp*g0) THEN
2253 gf%array(ig) = -pi/omega2*erf(omega*rc) &
2254 + twopi*rc2*erf(omega*rc) &
2255 + 2*rootpi*rc*exp(-omega2*rc2)/omega
2256 ELSE IF (g >= 2.0_dp*ginf*omega) THEN
2257 ! exponential prefactor
2258 exp_prefac = exp(-omega2*rc2)/(rootpi*(omega2*rc2 + 0.25_dp*g2/omega2))
2259 ! cos sin factor
2260 cossin_fac = omega*rc*cos(g*rc) - 0.5_dp*g/omega*sin(g*rc)
2261 ! real erf term with cosine
2262 erfcos_fac = erf(omega*rc)*cos(g*rc)
2263 ! Combine terms
2264 gf%array(ig) = fourpi/g2*(-exp_prefac*cossin_fac - erfcos_fac)
2265 ELSE
2266 ! exponential prefactor
2267 exp_prefac = twopi/g2*exp(-0.25_dp*g2/omega2)
2268 ! Compute complex arguments for erf
2269 z_p = cmplx(omega*rc, 0.5_dp*g/omega, kind=dp)
2270 ! Evaluate complex error functions
2271 erf_fac_p = 2.0_dp*real(erfz_fast(z_p))
2272 ! Real erf term with cosine
2273 erfcos_fac = fourpi/g2*erf(omega*rc)*cos(g*rc)
2274 ! Combine terms
2275 gf%array(ig) = exp_prefac*erf_fac_p - erfcos_fac
2276 END IF
2277 END DO
2278 IF (grid%have_g0) THEN
2279 gf%array(1) = -pi/omega2*erf(omega*rc) &
2280 + twopi*rc2*erf(omega*rc) &
2281 + 2*rootpi*rc*exp(-omega2*rc2)/omega
2282 END IF
2283
2284 CASE DEFAULT
2285 cpabort("Please specify a valid operator for the periodic Poisson solver")
2286 END SELECT
2287
2288 ! The analytic Poisson solver simply limits the domain of integration
2289 ! of the Fourier transform to a sphere of radius Rc, rather than integrating
2290 ! over all space (-∞,∞)
2291 CASE (analytic0d)
2292
2293 SELECT CASE (eri_env%operator)
2294 ! This is identical to the truncated Coulomb operator integrated
2295 ! over all space, when the truncation radius is equal to the radius of
2296 ! the Poisson solver
2297 CASE (eri_operator_coulomb, eri_operator_trunc)
2298 IF (eri_env%operator == eri_operator_coulomb) THEN
2299 rc = green%radius
2300 ELSE
2301 rc = eri_env%cutoff_radius
2302 END IF
2303 DO ig = grid%first_gne0, grid%ngpts_cut_local
2304 g2 = grid%gsq(ig)
2305 g = sqrt(g2)
2306 ! Taylor expansion around zero
2307 IF (g*rc >= 0.005_dp) THEN
2308 gf%array(ig) = fourpi/g2*(1.0_dp - cos(g*rc))
2309 ELSE
2310 gf%array(ig) = fourpi/g2*(g*rc)**2/2.0_dp*(1.0_dp - (g*rc)**2/12.0_dp)
2311 END IF
2312 END DO
2313 IF (grid%have_g0) gf%array(1) = twopi*rc**2
2314
2315 ! Not tested
2316 CASE (eri_operator_yukawa)
2317 CALL cp_warn(__location__, "Yukawa operator has not been tested")
2318 rc = green%radius
2319 omega = eri_env%omega
2320 ea = exp(-omega*rc)
2321 DO ig = grid%first_gne0, grid%ngpts_cut_local
2322 g2 = grid%gsq(ig)
2323 g = sqrt(g2)
2324 g3d = fourpi/(omega**2 + g2)
2325 gf%array(ig) = g3d*(1.0_dp - ea*(cos(g*rc) + omega/g*sin(g*rc)))
2326 END DO
2327 IF (grid%have_g0) gf%array(1) = fourpi/(omega**2)*(1.0_dp - ea*(1.0_dp + omega*rc))
2328
2329 ! Long-range Coulomb
2330 ! TODO: this should be equivalent to LR truncated Coulomb from above!
2331 CASE (eri_operator_erf, eri_operator_lr_trunc)
2332 IF (eri_env%operator == eri_operator_erf) THEN
2333 rc = green%radius
2334 ELSE
2335 rc = eri_env%cutoff_radius
2336 END IF
2337 omega2 = eri_env%omega**2
2338 DO ig = grid%first_gne0, grid%ngpts_cut_local
2339 g2 = grid%gsq(ig)
2340 g = sqrt(g2)
2341 ga = -0.25_dp*g2/omega2
2342 gf%array(ig) = fourpi/g2*exp(ga)*(1.0_dp - cos(g*rc))
2343 END DO
2344 IF (grid%have_g0) gf%array(1) = twopi*rc**2
2345
2346 ! Short-range Coulomb
2347 ! TODO: this should actually be properly derived and see whether it is correct
2348 CASE (eri_operator_erfc)
2349 CALL cp_warn(__location__, &
2350 "Short-range Coulomb operator may be incorrect with ANALYTIC0D Poisson solver")
2351 rc = green%radius
2352 omega2 = eri_env%omega**2
2353 DO ig = grid%first_gne0, grid%ngpts_cut_local
2354 g2 = grid%gsq(ig)
2355 g = sqrt(g2)
2356 ga = -0.25_dp*g2/omega2
2357 gf%array(ig) = fourpi/g2*(1.0_dp - exp(ga))*(1.0_dp - cos(g*rc))
2358 END DO
2359 IF (grid%have_g0) gf%array(1) = pi/omega2
2360
2361 CASE DEFAULT
2362 cpabort("Unsupported operator")
2363 END SELECT
2364
2365 CASE DEFAULT
2366 cpabort("Unsupported Poisson solver")
2367 END SELECT
2368 END associate
2369
2370 END SUBROUTINE pw_eri_green_create
2371
2372! **************************************************************************************************
2373!> \brief Adds data for a new row to the csr matrix
2374!> \param csr_mat ...
2375!> \param nnz ...
2376!> \param rdat ...
2377!> \param rind ...
2378!> \param irow ...
2379!> \par History
2380!> 04.2016 created [JGH]
2381! **************************************************************************************************
2382 SUBROUTINE update_csr_matrix(csr_mat, nnz, rdat, rind, irow)
2383
2384 TYPE(dbcsr_csr_type), INTENT(INOUT) :: csr_mat
2385 INTEGER, INTENT(IN) :: nnz
2386 REAL(kind=dp), DIMENSION(:), INTENT(IN) :: rdat
2387 INTEGER, DIMENSION(:), INTENT(IN) :: rind
2388 INTEGER, INTENT(IN) :: irow
2389
2390 INTEGER :: k, nrow, nze, nze_new
2391
2392 IF (irow /= 0) THEN
2393 nze = csr_mat%nze_local
2394 nze_new = nze + nnz
2395 ! values
2396 CALL reallocate(csr_mat%nzval_local%r_dp, 1, nze_new)
2397 csr_mat%nzval_local%r_dp(nze + 1:nze_new) = rdat(1:nnz)
2398 ! col indices
2399 CALL reallocate(csr_mat%colind_local, 1, nze_new)
2400 csr_mat%colind_local(nze + 1:nze_new) = rind(1:nnz)
2401 ! rows
2402 nrow = csr_mat%nrows_local
2403 CALL reallocate(csr_mat%rowptr_local, 1, irow + 1)
2404 csr_mat%rowptr_local(nrow + 1:irow) = nze + 1
2405 csr_mat%rowptr_local(irow + 1) = nze_new + 1
2406 ! nzerow
2407 CALL reallocate(csr_mat%nzerow_local, 1, irow)
2408 DO k = nrow + 1, irow
2409 csr_mat%nzerow_local(k) = csr_mat%rowptr_local(k + 1) - csr_mat%rowptr_local(k)
2410 END DO
2411 csr_mat%nrows_local = irow
2412 csr_mat%nze_local = csr_mat%nze_local + nnz
2413 END IF
2414 csr_mat%nze_total = csr_mat%nze_total + nnz
2415 csr_mat%has_indices = .true.
2416
2417 END SUBROUTINE update_csr_matrix
2418
2419! **************************************************************************************************
2420!> \brief Computes and prints the active orbitals on Cube Files
2421!> \param input ...
2422!> \param qs_env the qs_env in which the qs_env lives
2423!> \param mos ...
2424! **************************************************************************************************
2425 SUBROUTINE print_orbital_cubes(input, qs_env, mos)
2426 TYPE(section_vals_type), POINTER :: input
2427 TYPE(qs_environment_type), POINTER :: qs_env
2428 TYPE(mo_set_type), DIMENSION(:), INTENT(IN) :: mos
2429
2430 CHARACTER(LEN=default_path_length) :: filebody, filename, title
2431 INTEGER :: i, imo, isp, nmo, str(3), unit_nr
2432 INTEGER, DIMENSION(:), POINTER :: alist, blist, istride
2433 LOGICAL :: do_mo, explicit_a, explicit_b
2434 TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
2435 TYPE(cell_type), POINTER :: cell
2436 TYPE(cp_fm_type), POINTER :: mo_coeff
2437 TYPE(dft_control_type), POINTER :: dft_control
2438 TYPE(mp_para_env_type), POINTER :: para_env
2439 TYPE(particle_list_type), POINTER :: particles
2440 TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
2441 TYPE(pw_c1d_gs_type) :: wf_g
2442 TYPE(pw_env_type), POINTER :: pw_env
2443 TYPE(pw_pool_type), POINTER :: auxbas_pw_pool
2444 TYPE(pw_r3d_rs_type) :: wf_r
2445 TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
2446 TYPE(qs_subsys_type), POINTER :: subsys
2447 TYPE(section_vals_type), POINTER :: dft_section, scf_input
2448
2449 CALL section_vals_val_get(input, "FILENAME", c_val=filebody)
2450 CALL section_vals_val_get(input, "STRIDE", i_vals=istride)
2451 IF (SIZE(istride) == 1) THEN
2452 str(1:3) = istride(1)
2453 ELSE IF (SIZE(istride) == 3) THEN
2454 str(1:3) = istride(1:3)
2455 ELSE
2456 cpabort("STRIDE arguments inconsistent")
2457 END IF
2458 CALL section_vals_val_get(input, "ALIST", i_vals=alist, explicit=explicit_a)
2459 CALL section_vals_val_get(input, "BLIST", i_vals=blist, explicit=explicit_b)
2460
2461 CALL get_qs_env(qs_env=qs_env, &
2462 dft_control=dft_control, &
2463 para_env=para_env, &
2464 subsys=subsys, &
2465 atomic_kind_set=atomic_kind_set, &
2466 qs_kind_set=qs_kind_set, &
2467 cell=cell, &
2468 particle_set=particle_set, &
2469 pw_env=pw_env, &
2470 input=scf_input)
2471
2472 CALL qs_subsys_get(subsys, particles=particles)
2473 !
2474 CALL pw_env_get(pw_env, auxbas_pw_pool=auxbas_pw_pool)
2475 CALL auxbas_pw_pool%create_pw(wf_r)
2476 CALL auxbas_pw_pool%create_pw(wf_g)
2477 !
2478 dft_section => section_vals_get_subs_vals(scf_input, "DFT")
2479 !
2480 DO isp = 1, SIZE(mos)
2481 CALL get_mo_set(mo_set=mos(isp), mo_coeff=mo_coeff, nmo=nmo)
2482
2483 IF (SIZE(mos) > 1) THEN
2484 SELECT CASE (isp)
2485 CASE (1)
2486 CALL write_mo_set_to_output_unit(mos(isp), qs_kind_set, particle_set, &
2487 dft_section, 4, 0, final_mos=.true., spin="ALPHA")
2488 CASE (2)
2489 CALL write_mo_set_to_output_unit(mos(isp), qs_kind_set, particle_set, &
2490 dft_section, 4, 0, final_mos=.true., spin="BETA")
2491 CASE DEFAULT
2492 cpabort("Invalid spin")
2493 END SELECT
2494 ELSE
2495 CALL write_mo_set_to_output_unit(mos(isp), qs_kind_set, particle_set, &
2496 dft_section, 4, 0, final_mos=.true.)
2497 END IF
2498
2499 DO imo = 1, nmo
2500 IF (isp == 1 .AND. explicit_a) THEN
2501 IF (alist(1) == -1) THEN
2502 do_mo = .true.
2503 ELSE
2504 do_mo = .false.
2505 DO i = 1, SIZE(alist)
2506 IF (imo == alist(i)) do_mo = .true.
2507 END DO
2508 END IF
2509 ELSE IF (isp == 2 .AND. explicit_b) THEN
2510 IF (blist(1) == -1) THEN
2511 do_mo = .true.
2512 ELSE
2513 do_mo = .false.
2514 DO i = 1, SIZE(blist)
2515 IF (imo == blist(i)) do_mo = .true.
2516 END DO
2517 END IF
2518 ELSE
2519 do_mo = .true.
2520 END IF
2521 IF (.NOT. do_mo) cycle
2522 CALL calculate_wavefunction(mo_coeff, imo, wf_r, wf_g, atomic_kind_set, &
2523 qs_kind_set, cell, dft_control, particle_set, pw_env)
2524 IF (para_env%is_source()) THEN
2525 WRITE (filename, '(A,A1,I4.4,A1,I1.1,A)') trim(filebody), "_", imo, "_", isp, ".cube"
2526 CALL open_file(filename, unit_number=unit_nr, file_status="UNKNOWN", file_action="WRITE")
2527 WRITE (title, *) "Active Orbital ", imo, " spin ", isp
2528 ELSE
2529 unit_nr = -1
2530 END IF
2531 CALL cp_pw_to_cube(wf_r, unit_nr, title, particles=particles, stride=istride)
2532 IF (para_env%is_source()) THEN
2533 CALL close_file(unit_nr)
2534 END IF
2535 END DO
2536 END DO
2537
2538 CALL auxbas_pw_pool%give_back_pw(wf_r)
2539 CALL auxbas_pw_pool%give_back_pw(wf_g)
2540
2541 END SUBROUTINE print_orbital_cubes
2542
2543! **************************************************************************************************
2544!> \brief Writes a FCIDUMP file
2545!> \param active_space_env ...
2546!> \param as_input ...
2547!> \param restricted ...
2548!> \param check_reference compare the canonical ROKS determinant with the reference energy
2549!> \par History
2550!> 04.2016 created [JGH]
2551! **************************************************************************************************
2552 SUBROUTINE fcidump(active_space_env, as_input, restricted, check_reference)
2553
2554 TYPE(active_space_type), POINTER :: active_space_env
2555 TYPE(section_vals_type), POINTER :: as_input
2556 LOGICAL, INTENT(IN) :: restricted, check_reference
2557
2558 CHARACTER(LEN=*), PARAMETER :: roks_warning = &
2559 "ROKS FCIDUMP spin blocks differ. Check the cell size and the consistency of "// &
2560 "the SCF exchange and active-space ERI operators."
2561 REAL(kind=dp), PARAMETER :: roks_fock_tol = 1.0e-6_dp
2562
2563 INTEGER :: i, i1, i2, i3, i4, isym, iw, m1, m2, &
2564 ms2, nmo, norb, nspins
2565 REAL(kind=dp) :: checksum, esub, spin_difference
2566 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :) :: fmat, fock_diff, pmat
2567 TYPE(cp_logger_type), POINTER :: logger
2568 TYPE(eri_fcidump_checksum) :: eri_checksum
2569 TYPE(eri_fcidump_print) :: writer
2570
2571 checksum = 0.0_dp
2572 spin_difference = 0.0_dp
2573
2574 logger => cp_get_default_logger()
2575 iw = cp_print_key_unit_nr(logger, as_input, "FCIDUMP", &
2576 extension=".fcidump", file_status="REPLACE", file_action="WRITE", file_form="FORMATTED")
2577 !
2578 nspins = active_space_env%nspins
2579 norb = SIZE(active_space_env%active_orbitals, 1)
2580 ms2 = active_space_env%multiplicity - 1
2581 IF (nspins == 1 .OR. restricted) THEN
2582 ! Closed shell or restricted open-shell
2583 associate(nelec => active_space_env%nelec_active)
2584
2585 IF (iw > 0) THEN
2586 WRITE (iw, "(A,A,I4,A,I4,A,I2,A)") "&FCI", " NORB=", norb, ",NELEC=", nelec, ",MS2=", ms2, ","
2587 isym = 1
2588 WRITE (iw, "(A,1000(I1,','))") " ORBSYM=", (isym, i=1, norb)
2589 isym = 0
2590 WRITE (iw, "(A,I1,A)") " ISYM=", isym, ","
2591 IF (restricted) WRITE (iw, "(A,I1,A)") " UHF=", 0, ","
2592 WRITE (iw, "(A)") " /"
2593 END IF
2594 !
2595 ! Print integrals: ERI
2596 writer = eri_fcidump_print(iw, 1, 1)
2597 IF (check_reference) THEN
2598 writer%nalpha = (nelec + ms2)/2
2599 writer%nbeta = (nelec - ms2)/2
2600 END IF
2601 CALL active_space_env%eri%eri_foreach(1, active_space_env%active_orbitals, &
2602 writer, 1, 1)
2603 CALL eri_checksum%set(1, 1)
2604 CALL active_space_env%eri%eri_foreach(1, active_space_env%active_orbitals, eri_checksum, 1, 1)
2605
2606 ! Print integrals: Fij
2607 ! replicate Fock matrix
2608 nmo = active_space_env%eri%norb
2609 ALLOCATE (fmat(nmo, nmo))
2610 CALL replicate_and_symmetrize_matrix(nmo, active_space_env%fock_sub(1), fmat)
2611 esub = active_space_env%energy_inactive
2612 IF (restricted) THEN
2613 ALLOCATE (fock_diff(nmo, nmo), pmat(nmo, nmo))
2614 CALL replicate_and_symmetrize_matrix(nmo, active_space_env%fock_sub(2), fock_diff)
2615 fock_diff(:, :) = fmat - fock_diff
2616 associate(active => active_space_env%active_orbitals(:, 1))
2617 spin_difference = maxval(abs(fock_diff(active, active)))
2618 END associate
2619 ! Use one spin-independent operator and preserve the energy at the embedding density.
2620 fmat(:, :) = fmat - 0.5_dp*fock_diff
2621 CALL replicate_and_symmetrize_matrix(nmo, active_space_env%p_active(1), pmat)
2622 esub = esub + 0.5_dp*sum(fock_diff*pmat)
2623 CALL replicate_and_symmetrize_matrix(nmo, active_space_env%p_active(2), pmat)
2624 esub = esub - 0.5_dp*sum(fock_diff*pmat)
2625 DEALLOCATE (fock_diff, pmat)
2626 END IF
2627 IF (iw > 0) THEN
2628 i3 = 0; i4 = 0
2629 DO m1 = 1, SIZE(active_space_env%active_orbitals, 1)
2630 i1 = active_space_env%active_orbitals(m1, 1)
2631 IF (m1 <= writer%nalpha) writer%determinant_energy = writer%determinant_energy + fmat(i1, i1)
2632 IF (m1 <= writer%nbeta) writer%determinant_energy = writer%determinant_energy + fmat(i1, i1)
2633 DO m2 = m1, SIZE(active_space_env%active_orbitals, 1)
2634 i2 = active_space_env%active_orbitals(m2, 1)
2635 checksum = checksum + abs(fmat(i1, i2))
2636 WRITE (iw, "(ES23.16,4I4)") fmat(i1, i2), m1, m2, i3, i4
2637 END DO
2638 END DO
2639 END IF
2640 DEALLOCATE (fmat)
2641 ! Print energy
2642 i1 = 0; i2 = 0; i3 = 0; i4 = 0
2643 checksum = checksum + abs(esub)
2644 IF (iw > 0) WRITE (iw, "(ES23.16,4I4)") esub, i1, i2, i3, i4
2645 END associate
2646
2647 ELSE
2648 associate(nelec => active_space_env%nelec_active)
2649
2650 IF (iw > 0) THEN
2651 WRITE (iw, "(A,A,I4,A,I4,A,I2,A)") "&FCI", " NORB=", norb, ",NELEC=", nelec, ",MS2=", ms2, ","
2652 isym = 1
2653 WRITE (iw, "(A,1000(I1,','))") " ORBSYM=", (isym, i=1, norb)
2654 isym = 0
2655 WRITE (iw, "(A,I1,A)") " ISYM=", isym, ","
2656 WRITE (iw, "(A,I1,A)") " UHF=", 1, ","
2657 WRITE (iw, "(A)") " /"
2658 END IF
2659 !
2660 ! Print integrals: ERI
2661 ! alpha-alpha
2662 CALL active_space_env%eri%eri_foreach(1, active_space_env%active_orbitals, &
2663 eri_fcidump_print(iw, 1, 1), 1, 1)
2664 CALL eri_checksum%set(1, 1)
2665 CALL active_space_env%eri%eri_foreach(1, active_space_env%active_orbitals, eri_checksum, 1, 1)
2666 ! alpha-beta
2667 CALL active_space_env%eri%eri_foreach(2, active_space_env%active_orbitals, &
2668 eri_fcidump_print(iw, 1, norb + 1), 1, 2)
2669 CALL eri_checksum%set(1, norb + 1)
2670 CALL active_space_env%eri%eri_foreach(2, active_space_env%active_orbitals, eri_checksum, 1, 2)
2671 ! beta-beta
2672 CALL active_space_env%eri%eri_foreach(3, active_space_env%active_orbitals, &
2673 eri_fcidump_print(iw, norb + 1, norb + 1), 2, 2)
2674 CALL eri_checksum%set(norb + 1, norb + 1)
2675 CALL active_space_env%eri%eri_foreach(3, active_space_env%active_orbitals, eri_checksum, 2, 2)
2676 ! Print integrals: Fij
2677 ! alpha
2678 nmo = active_space_env%eri%norb
2679 ALLOCATE (fmat(nmo, nmo))
2680 CALL replicate_and_symmetrize_matrix(nmo, active_space_env%fock_sub(1), fmat)
2681 IF (iw > 0) THEN
2682 i3 = 0; i4 = 0
2683 DO m1 = 1, norb
2684 i1 = active_space_env%active_orbitals(m1, 1)
2685 DO m2 = m1, norb
2686 i2 = active_space_env%active_orbitals(m2, 1)
2687 checksum = checksum + abs(fmat(i1, i2))
2688 WRITE (iw, "(ES23.16,4I4)") fmat(i1, i2), m1, m2, i3, i4
2689 END DO
2690 END DO
2691 END IF
2692 DEALLOCATE (fmat)
2693 ! beta
2694 ALLOCATE (fmat(nmo, nmo))
2695 CALL replicate_and_symmetrize_matrix(nmo, active_space_env%fock_sub(2), fmat)
2696 IF (iw > 0) THEN
2697 i3 = 0; i4 = 0
2698 DO m1 = 1, SIZE(active_space_env%active_orbitals, 1)
2699 i1 = active_space_env%active_orbitals(m1, 2)
2700 DO m2 = m1, SIZE(active_space_env%active_orbitals, 1)
2701 i2 = active_space_env%active_orbitals(m2, 2)
2702 checksum = checksum + abs(fmat(i1, i2))
2703 WRITE (iw, "(ES23.16,4I4)") fmat(i1, i2), m1 + norb, m2 + norb, i3, i4
2704 END DO
2705 END DO
2706 END IF
2707 DEALLOCATE (fmat)
2708 ! Print energy
2709 esub = active_space_env%energy_inactive
2710 i1 = 0; i2 = 0; i3 = 0; i4 = 0
2711 checksum = checksum + abs(esub)
2712 IF (iw > 0) WRITE (iw, "(ES23.16,4I4)") esub, i1, i2, i3, i4
2713 END associate
2714 END IF
2715 !
2716 CALL cp_print_key_finished_output(iw, logger, as_input, "FCIDUMP")
2717
2718 !>>
2719 iw = cp_logger_get_default_io_unit(logger)
2720 IF (iw > 0) WRITE (iw, '(T4,A,T66,F12.8)') "FCIDUMP| Checksum:", eri_checksum%checksum + checksum
2721 IF (restricted) THEN
2722 IF (check_reference .AND. writer%unit_nr > 0 .AND. iw > 0) THEN
2723 WRITE (iw, '(T4,A,1X,ES23.16)') "FCIDUMP| Reference determinant energy offset [hartree]:", &
2724 writer%determinant_energy + esub - active_space_env%energy_ref
2725 END IF
2726 IF (iw > 0) WRITE (iw, '(T4,A,1X,ES12.5)') &
2727 "FCIDUMP| Max. ROKS spin-block difference [hartree]:", spin_difference
2728 IF (spin_difference > roks_fock_tol) THEN
2729 cpwarn(roks_warning)
2730 END IF
2731 END IF
2732 !<<
2733
2734 END SUBROUTINE fcidump
2735
2736! **************************************************************************************************
2737!> \brief replicate and symmetrize a matrix
2738!> \param norb the number of orbitals
2739!> \param distributed_matrix ...
2740!> \param replicated_matrix ...
2741! **************************************************************************************************
2742 SUBROUTINE replicate_and_symmetrize_matrix(norb, distributed_matrix, replicated_matrix)
2743 INTEGER, INTENT(IN) :: norb
2744 TYPE(cp_fm_type), INTENT(IN) :: distributed_matrix
2745 REAL(dp), DIMENSION(:, :), INTENT(INOUT) :: replicated_matrix
2746
2747 INTEGER :: i1, i2
2748 REAL(dp) :: mval
2749
2750 replicated_matrix(:, :) = 0.0_dp
2751 DO i1 = 1, norb
2752 DO i2 = i1, norb
2753 CALL cp_fm_get_element(distributed_matrix, i1, i2, mval)
2754 replicated_matrix(i1, i2) = mval
2755 replicated_matrix(i2, i1) = mval
2756 END DO
2757 END DO
2758 END SUBROUTINE replicate_and_symmetrize_matrix
2759
2760! **************************************************************************************************
2761!> \brief Calculates active space Fock matrix and inactive energy
2762!> \param active_space_env ...
2763!> \param restricted ...
2764!> \par History
2765!> 06.2016 created [JGH]
2766! **************************************************************************************************
2767 SUBROUTINE subspace_fock_matrix(active_space_env, restricted)
2768
2769 TYPE(active_space_type), POINTER :: active_space_env
2770 LOGICAL, INTENT(IN) :: restricted
2771
2772 INTEGER :: i1, i2, is, norb, nspins
2773 REAL(kind=dp) :: eeri, eref, esub, mval
2774 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :) :: ks_a_mat, ks_a_ref, ks_b_mat, ks_b_ref, &
2775 ks_mat, ks_ref, p_a_mat, p_b_mat, p_mat
2776 TYPE(cp_fm_type), POINTER :: matrix, mo_coef
2777 TYPE(dbcsr_csr_type), POINTER :: eri, eri_aa, eri_ab, eri_bb
2778
2779 eref = active_space_env%energy_ref
2780 nspins = active_space_env%nspins
2781
2782 IF (nspins == 1) THEN
2783 CALL get_mo_set(active_space_env%mos_active(1), nmo=norb, mo_coeff=mo_coef)
2784 !
2785 ! Loop over ERI, calculate subspace HF energy and Fock matrix
2786 !
2787 ! replicate KS, Core, and P matrices
2788 ALLOCATE (ks_mat(norb, norb), ks_ref(norb, norb), p_mat(norb, norb))
2789 ks_ref = 0.0_dp
2790
2791 ! ks_mat contains the KS/Fock matrix (of full density) projected onto the AS MO subspace (f_ref in eq. 19)
2792 CALL replicate_and_symmetrize_matrix(norb, active_space_env%ks_sub(1), ks_mat)
2793 CALL replicate_and_symmetrize_matrix(norb, active_space_env%p_active(1), p_mat)
2794
2795 ! compute ks_ref = V_H[rho^A] + V_HFX[rho^A]
2796 eri => active_space_env%eri%eri(1)%csr_mat
2797 CALL build_subspace_fock_matrix(active_space_env%active_orbitals, eri, p_mat, ks_ref, &
2798 active_space_env%eri%comm_exchange)
2799
2800 ! compute eeri = E_H[rho^A] + E_HFX[rho^A] as
2801 ! eeri = 1/2 * (SUM_pq (V_H[rho^A] + V_HFX[rho^A])_pq * D^A_pq)
2802 eeri = 0.5_dp*sum(ks_ref*p_mat)
2803
2804 ! now calculate the inactive energy acoording to eq. 19, that is
2805 ! esub = E^I = E_ref - f_ref .* D^A + E_H[rho^A] + E_HFX[rho^A]
2806 ! where f^ref = ks_mat, which is the KS/Fock matrix in MO basis, transformed previously
2807 ! and is equal to ks_mat = h^0 + V_core + V_H[rho] + V_HFX[rho]
2808 esub = eref - sum(ks_mat(1:norb, 1:norb)*p_mat(1:norb, 1:norb)) + eeri
2809
2810 ! reuse ks_mat to store f^I = f^ref - (V_H[rho^A] + V_HFX[rho^A]) according to eq. 20
2811 ks_mat(1:norb, 1:norb) = ks_mat(1:norb, 1:norb) - ks_ref(1:norb, 1:norb)
2812 ! this is now the embedding potential for the AS calculation!
2813
2814 active_space_env%energy_inactive = esub
2815
2816 CALL cp_fm_release(active_space_env%fock_sub)
2817 ALLOCATE (active_space_env%fock_sub(nspins))
2818 DO is = 1, nspins
2819 matrix => active_space_env%ks_sub(is)
2820 CALL cp_fm_create(active_space_env%fock_sub(is), matrix%matrix_struct, &
2821 name="Active Fock operator")
2822 END DO
2823 matrix => active_space_env%fock_sub(1)
2824 DO i1 = 1, norb
2825 DO i2 = 1, norb
2826 mval = ks_mat(i1, i2)
2827 CALL cp_fm_set_element(matrix, i1, i2, mval)
2828 END DO
2829 END DO
2830 ELSE
2831
2832 CALL get_mo_set(active_space_env%mos_active(1), nmo=norb)
2833 !
2834 ! Loop over ERI, calculate subspace HF energy and Fock matrix
2835 !
2836 ! replicate KS, Core, and P matrices
2837 ALLOCATE (ks_a_mat(norb, norb), ks_b_mat(norb, norb), &
2838 & ks_a_ref(norb, norb), ks_b_ref(norb, norb), &
2839 & p_a_mat(norb, norb), p_b_mat(norb, norb))
2840 ks_a_ref(:, :) = 0.0_dp; ks_b_ref(:, :) = 0.0_dp
2841
2842 CALL replicate_and_symmetrize_matrix(norb, active_space_env%p_active(1), p_a_mat)
2843 CALL replicate_and_symmetrize_matrix(norb, active_space_env%p_active(2), p_b_mat)
2844 CALL replicate_and_symmetrize_matrix(norb, active_space_env%ks_sub(1), ks_a_mat)
2845 CALL replicate_and_symmetrize_matrix(norb, active_space_env%ks_sub(2), ks_b_mat)
2846 !
2847 !
2848 IF (restricted) THEN
2849 ! In the restricted case, we use the same ERIs for each spin channel
2850 eri_aa => active_space_env%eri%eri(1)%csr_mat
2851 CALL build_subspace_spin_fock_matrix(active_space_env%active_orbitals, eri_aa, eri_aa, p_a_mat, p_b_mat, ks_a_ref, &
2852 tr_mixed_eri=.false., comm_exchange=active_space_env%eri%comm_exchange, &
2853 restricted=restricted)
2854 CALL build_subspace_spin_fock_matrix(active_space_env%active_orbitals, eri_aa, eri_aa, p_b_mat, p_a_mat, ks_b_ref, &
2855 tr_mixed_eri=.true., comm_exchange=active_space_env%eri%comm_exchange, &
2856 restricted=restricted)
2857 ELSE
2858 eri_aa => active_space_env%eri%eri(1)%csr_mat
2859 eri_ab => active_space_env%eri%eri(2)%csr_mat
2860 eri_bb => active_space_env%eri%eri(3)%csr_mat
2861 CALL build_subspace_spin_fock_matrix(active_space_env%active_orbitals, eri_aa, eri_ab, p_a_mat, p_b_mat, ks_a_ref, &
2862 tr_mixed_eri=.false., comm_exchange=active_space_env%eri%comm_exchange, &
2863 restricted=restricted)
2864 CALL build_subspace_spin_fock_matrix(active_space_env%active_orbitals, eri_bb, eri_ab, p_b_mat, p_a_mat, ks_b_ref, &
2865 tr_mixed_eri=.true., comm_exchange=active_space_env%eri%comm_exchange, &
2866 restricted=restricted)
2867 END IF
2868 !
2869 ! calculate energy
2870 eeri = 0.0_dp
2871 eeri = 0.5_dp*(sum(ks_a_ref*p_a_mat) + sum(ks_b_ref*p_b_mat))
2872 esub = eref - sum(ks_a_mat*p_a_mat) - sum(ks_b_mat*p_b_mat) + eeri
2873 ks_a_mat(:, :) = ks_a_mat(:, :) - ks_a_ref(:, :)
2874 ks_b_mat(:, :) = ks_b_mat(:, :) - ks_b_ref(:, :)
2875 !
2876 active_space_env%energy_inactive = esub
2877 !
2878 CALL cp_fm_release(active_space_env%fock_sub)
2879 ALLOCATE (active_space_env%fock_sub(nspins))
2880 DO is = 1, nspins
2881 matrix => active_space_env%ks_sub(is)
2882 CALL cp_fm_create(active_space_env%fock_sub(is), matrix%matrix_struct, &
2883 name="Active Fock operator")
2884 END DO
2885
2886 matrix => active_space_env%fock_sub(1)
2887 DO i1 = 1, norb
2888 DO i2 = 1, norb
2889 mval = ks_a_mat(i1, i2)
2890 CALL cp_fm_set_element(matrix, i1, i2, mval)
2891 END DO
2892 END DO
2893 matrix => active_space_env%fock_sub(2)
2894 DO i1 = 1, norb
2895 DO i2 = 1, norb
2896 mval = ks_b_mat(i1, i2)
2897 CALL cp_fm_set_element(matrix, i1, i2, mval)
2898 END DO
2899 END DO
2900
2901 END IF
2902
2903 END SUBROUTINE subspace_fock_matrix
2904
2905! **************************************************************************************************
2906!> \brief build subspace fockian
2907!> \param active_orbitals the active orbital indices
2908!> \param eri two electon integrals in MO
2909!> \param p_mat density matrix
2910!> \param ks_ref fockian matrix
2911!> \param comm_exchange ...
2912! **************************************************************************************************
2913 SUBROUTINE build_subspace_fock_matrix(active_orbitals, eri, p_mat, ks_ref, comm_exchange)
2914 INTEGER, DIMENSION(:, :), INTENT(IN) :: active_orbitals
2915 TYPE(dbcsr_csr_type), INTENT(IN) :: eri
2916 REAL(dp), DIMENSION(:, :), INTENT(IN) :: p_mat
2917 REAL(dp), DIMENSION(:, :), INTENT(INOUT) :: ks_ref
2918 TYPE(mp_comm_type), INTENT(IN) :: comm_exchange
2919
2920 CHARACTER(LEN=*), PARAMETER :: routinen = 'build_subspace_fock_matrix'
2921
2922 INTEGER :: handle, i1, i12, i12l, i2, i3, i34, &
2923 i34l, i4, irptr, m1, m2, nindex, &
2924 nmo_total, norb
2925 REAL(dp) :: erint
2926 TYPE(mp_comm_type) :: mp_group
2927
2928 CALL timeset(routinen, handle)
2929
2930 ! Nothing to do
2931 norb = SIZE(active_orbitals, 1)
2932 nmo_total = SIZE(p_mat, 1)
2933 nindex = (nmo_total*(nmo_total + 1))/2
2934 CALL mp_group%set_handle(eri%mp_group%get_handle())
2935 DO m1 = 1, norb
2936 i1 = active_orbitals(m1, 1)
2937 DO m2 = m1, norb
2938 i2 = active_orbitals(m2, 1)
2939 i12 = csr_idx_to_combined(i1, i2, nmo_total)
2940 IF (mod(i12 - 1, comm_exchange%num_pe) == comm_exchange%mepos) THEN
2941 i12l = (i12 - 1)/comm_exchange%num_pe + 1
2942 irptr = eri%rowptr_local(i12l) - 1
2943 DO i34l = 1, eri%nzerow_local(i12l)
2944 i34 = eri%colind_local(irptr + i34l)
2945 CALL csr_idx_from_combined(i34, nmo_total, i3, i4)
2946 erint = eri%nzval_local%r_dp(irptr + i34l)
2947 ! Coulomb
2948 ks_ref(i1, i2) = ks_ref(i1, i2) + erint*p_mat(i3, i4)
2949 IF (i3 /= i4) THEN
2950 ks_ref(i1, i2) = ks_ref(i1, i2) + erint*p_mat(i3, i4)
2951 END IF
2952 IF (i12 /= i34) THEN
2953 ks_ref(i3, i4) = ks_ref(i3, i4) + erint*p_mat(i1, i2)
2954 IF (i1 /= i2) THEN
2955 ks_ref(i3, i4) = ks_ref(i3, i4) + erint*p_mat(i1, i2)
2956 END IF
2957 END IF
2958 ! Exchange
2959 erint = -0.5_dp*erint
2960 ks_ref(i1, i3) = ks_ref(i1, i3) + erint*p_mat(i2, i4)
2961 IF (i1 /= i2) THEN
2962 ks_ref(i2, i3) = ks_ref(i2, i3) + erint*p_mat(i1, i4)
2963 END IF
2964 IF (i3 /= i4) THEN
2965 ks_ref(i1, i4) = ks_ref(i1, i4) + erint*p_mat(i2, i3)
2966 END IF
2967 IF (i1 /= i2 .AND. i3 /= i4) THEN
2968 ks_ref(i2, i4) = ks_ref(i2, i4) + erint*p_mat(i1, i3)
2969 END IF
2970 ! The stored integral represents both orders of unequal orbital pairs.
2971 IF (i12 /= i34) THEN
2972 ks_ref(i3, i1) = ks_ref(i3, i1) + erint*p_mat(i4, i2)
2973 IF (i1 /= i2) THEN
2974 ks_ref(i3, i2) = ks_ref(i3, i2) + erint*p_mat(i4, i1)
2975 END IF
2976 IF (i3 /= i4) THEN
2977 ks_ref(i4, i1) = ks_ref(i4, i1) + erint*p_mat(i3, i2)
2978 END IF
2979 IF (i1 /= i2 .AND. i3 /= i4) THEN
2980 ks_ref(i4, i2) = ks_ref(i4, i2) + erint*p_mat(i3, i1)
2981 END IF
2982 END IF
2983 END DO
2984 END IF
2985 END DO
2986 END DO
2987 !
2988 DO m1 = 1, norb
2989 i1 = active_orbitals(m1, 1)
2990 DO m2 = m1, norb
2991 i2 = active_orbitals(m2, 1)
2992 ks_ref(i2, i1) = ks_ref(i1, i2)
2993 END DO
2994 END DO
2995 CALL mp_group%sum(ks_ref)
2996
2997 CALL timestop(handle)
2998
2999 END SUBROUTINE build_subspace_fock_matrix
3000
3001! **************************************************************************************************
3002!> \brief build subspace fockian for unrestricted spins
3003!> \param active_orbitals the active orbital indices
3004!> \param eri_aa two electon integrals in MO with parallel spins
3005!> \param eri_ab two electon integrals in MO with anti-parallel spins
3006!> \param p_a_mat density matrix for up-spin
3007!> \param p_b_mat density matrix for down-spin
3008!> \param ks_a_ref fockian matrix for up-spin
3009!> \param tr_mixed_eri boolean to indicate Coulomb interaction alignment
3010!> \param comm_exchange ...
3011!> \param restricted use the common, eightfold-symmetric ERIs for both spin densities
3012! **************************************************************************************************
3013 SUBROUTINE build_subspace_spin_fock_matrix(active_orbitals, eri_aa, eri_ab, p_a_mat, p_b_mat, ks_a_ref, tr_mixed_eri, &
3014 comm_exchange, restricted)
3015 INTEGER, DIMENSION(:, :), INTENT(IN) :: active_orbitals
3016 TYPE(dbcsr_csr_type), INTENT(IN) :: eri_aa, eri_ab
3017 REAL(dp), DIMENSION(:, :), INTENT(IN) :: p_a_mat, p_b_mat
3018 REAL(dp), DIMENSION(:, :), INTENT(INOUT) :: ks_a_ref
3019 LOGICAL, INTENT(IN) :: tr_mixed_eri
3020 TYPE(mp_comm_type), INTENT(IN) :: comm_exchange
3021 LOGICAL, INTENT(IN) :: restricted
3022
3023 CHARACTER(LEN=*), PARAMETER :: routinen = 'build_subspace_spin_fock_matrix'
3024
3025 INTEGER :: handle, i1, i12, i12l, i2, i3, i34, &
3026 i34l, i4, irptr, m1, m2, nindex, &
3027 nmo_total, norb, spin1, spin2
3028 REAL(dp) :: erint, p12, p34
3029 TYPE(mp_comm_type) :: mp_group
3030
3031 CALL timeset(routinen, handle)
3032
3033 norb = SIZE(active_orbitals, 1)
3034 nmo_total = SIZE(p_a_mat, 1)
3035 nindex = (nmo_total*(nmo_total + 1))/2
3036 IF (tr_mixed_eri) THEN
3037 spin1 = 2
3038 spin2 = 1
3039 ELSE
3040 spin1 = 1
3041 spin2 = 2
3042 END IF
3043 DO m1 = 1, norb
3044 i1 = active_orbitals(m1, spin1)
3045 DO m2 = m1, norb
3046 i2 = active_orbitals(m2, spin1)
3047 i12 = csr_idx_to_combined(i1, i2, nmo_total)
3048 IF (mod(i12 - 1, comm_exchange%num_pe) == comm_exchange%mepos) THEN
3049 i12l = (i12 - 1)/comm_exchange%num_pe + 1
3050 irptr = eri_aa%rowptr_local(i12l) - 1
3051 DO i34l = 1, eri_aa%nzerow_local(i12l)
3052 i34 = eri_aa%colind_local(irptr + i34l)
3053 CALL csr_idx_from_combined(i34, nmo_total, i3, i4)
3054 erint = eri_aa%nzval_local%r_dp(irptr + i34l)
3055 ! Coulomb
3056 p12 = p_a_mat(i1, i2)
3057 p34 = p_a_mat(i3, i4)
3058 IF (restricted) THEN
3059 p12 = p12 + p_b_mat(i1, i2)
3060 p34 = p34 + p_b_mat(i3, i4)
3061 END IF
3062 IF (i1 /= i2) p12 = 2.0_dp*p12
3063 IF (i3 /= i4) p34 = 2.0_dp*p34
3064 !F_ij += (ij|kl)*d_kl
3065 ks_a_ref(i1, i2) = ks_a_ref(i1, i2) + erint*p34
3066 IF (i12 /= i34) THEN
3067 !F_kl += (ij|kl)*d_ij
3068 ks_a_ref(i3, i4) = ks_a_ref(i3, i4) + erint*p12
3069 END IF
3070 ! Exchange
3071 erint = -1.0_dp*erint
3072 !F_ik -= (ij|kl)*d_jl
3073 ks_a_ref(i1, i3) = ks_a_ref(i1, i3) + erint*p_a_mat(i2, i4)
3074 IF (i1 /= i2) THEN
3075 !F_jk -= (ij|kl)*d_il
3076 ks_a_ref(i2, i3) = ks_a_ref(i2, i3) + erint*p_a_mat(i1, i4)
3077 END IF
3078 IF (i3 /= i4) THEN
3079 !F_il -= (ij|kl)*d_jk
3080 ks_a_ref(i1, i4) = ks_a_ref(i1, i4) + erint*p_a_mat(i2, i3)
3081 END IF
3082 IF (i1 /= i2 .AND. i3 /= i4) THEN
3083 !F_jl -= (ij|kl)*d_ik
3084 ks_a_ref(i2, i4) = ks_a_ref(i2, i4) + erint*p_a_mat(i1, i3)
3085 END IF
3086 IF (i12 /= i34) THEN
3087 ks_a_ref(i3, i1) = ks_a_ref(i3, i1) + erint*p_a_mat(i4, i2)
3088 IF (i1 /= i2) THEN
3089 ks_a_ref(i3, i2) = ks_a_ref(i3, i2) + erint*p_a_mat(i4, i1)
3090 END IF
3091 IF (i3 /= i4) THEN
3092 ks_a_ref(i4, i1) = ks_a_ref(i4, i1) + erint*p_a_mat(i3, i2)
3093 END IF
3094 IF (i1 /= i2 .AND. i3 /= i4) THEN
3095 ks_a_ref(i4, i2) = ks_a_ref(i4, i2) + erint*p_a_mat(i3, i1)
3096 END IF
3097 END IF
3098 END DO
3099 END IF
3100 END DO
3101 END DO
3102 !
3103
3104 ! Shared-orbital Coulomb contributions from both spins were included above.
3105 IF (.NOT. restricted) THEN
3106 DO m1 = 1, norb
3107 i1 = active_orbitals(m1, 1)
3108 DO m2 = m1, norb
3109 i2 = active_orbitals(m2, 1)
3110 i12 = csr_idx_to_combined(i1, i2, nmo_total)
3111 IF (mod(i12 - 1, comm_exchange%num_pe) == comm_exchange%mepos) THEN
3112 i12l = (i12 - 1)/comm_exchange%num_pe + 1
3113 irptr = eri_ab%rowptr_local(i12l) - 1
3114 DO i34l = 1, eri_ab%nzerow_local(i12l)
3115 i34 = eri_ab%colind_local(irptr + i34l)
3116 CALL csr_idx_from_combined(i34, nmo_total, i3, i4)
3117 erint = eri_ab%nzval_local%r_dp(irptr + i34l)
3118 ! Coulomb
3119 IF (tr_mixed_eri) THEN
3120 !F_kl += (kl beta|ij alpha )*d_alpha_ij
3121 ks_a_ref(i3, i4) = ks_a_ref(i3, i4) + erint*p_b_mat(i1, i2)
3122 IF (i1 /= i2) ks_a_ref(i3, i4) = ks_a_ref(i3, i4) + erint*p_b_mat(i1, i2)
3123 ELSE
3124 !F_ij += (ij alpha|kl beta )*d_beta_kl
3125 ks_a_ref(i1, i2) = ks_a_ref(i1, i2) + erint*p_b_mat(i3, i4)
3126 IF (i3 /= i4) ks_a_ref(i1, i2) = ks_a_ref(i1, i2) + erint*p_b_mat(i3, i4)
3127 END IF
3128 END DO
3129 END IF
3130 END DO
3131 END DO
3132 END IF
3133 !
3134 DO m1 = 1, norb
3135 i1 = active_orbitals(m1, spin1)
3136 DO m2 = m1, norb
3137 i2 = active_orbitals(m2, spin1)
3138 ks_a_ref(i2, i1) = ks_a_ref(i1, i2)
3139 END DO
3140 END DO
3141 CALL mp_group%set_handle(eri_aa%mp_group%get_handle())
3142 CALL mp_group%sum(ks_a_ref)
3143
3144 CALL timestop(handle)
3145
3146 END SUBROUTINE build_subspace_spin_fock_matrix
3147
3148! **************************************************************************************************
3149!> \brief Creates a local basis
3150!> \param pro_basis_set ...
3151!> \param zval ...
3152!> \param ishell ...
3153!> \param nshell ...
3154!> \param lnam ...
3155!> \par History
3156!> 05.2016 created [JGH]
3157! **************************************************************************************************
3158 SUBROUTINE create_pro_basis(pro_basis_set, zval, ishell, nshell, lnam)
3159 TYPE(gto_basis_set_type), POINTER :: pro_basis_set
3160 INTEGER, INTENT(IN) :: zval, ishell
3161 INTEGER, DIMENSION(:), INTENT(IN) :: nshell
3162 CHARACTER(LEN=*), DIMENSION(:), INTENT(IN) :: lnam
3163
3164 CHARACTER(LEN=6), DIMENSION(:), POINTER :: sym
3165 INTEGER :: i, l, nj
3166 INTEGER, DIMENSION(4, 7) :: ne
3167 INTEGER, DIMENSION(:), POINTER :: lq, nq
3168 REAL(kind=dp), DIMENSION(:), POINTER :: zet
3169 TYPE(sto_basis_set_type), POINTER :: sto_basis_set
3170
3171 cpassert(.NOT. ASSOCIATED(pro_basis_set))
3172 NULLIFY (sto_basis_set)
3173
3174 ! electronic configuration
3175 ne = 0
3176 DO l = 1, 4 !lq(1)+1
3177 nj = 2*(l - 1) + 1
3178 DO i = l, 7 ! nq(1)
3179 ne(l, i) = ptable(zval)%e_conv(l - 1) - 2*nj*(i - l)
3180 ne(l, i) = max(ne(l, i), 0)
3181 ne(l, i) = min(ne(l, i), 2*nj)
3182 END DO
3183 END DO
3184 ALLOCATE (nq(ishell), lq(ishell), zet(ishell), sym(ishell))
3185 DO i = 1, ishell
3186 nq(i) = nshell(i)
3187 SELECT CASE (lnam(i))
3188 CASE ('S', 's')
3189 lq(i) = 0
3190 CASE ('P', 'p')
3191 lq(i) = 1
3192 CASE ('D', 'd')
3193 lq(i) = 2
3194 CASE ('F', 'f')
3195 lq(i) = 3
3196 CASE DEFAULT
3197 cpabort("Wrong l QN")
3198 END SELECT
3199 sym(i) = lnam(i)
3200 zet(i) = srules(zval, ne, nq(1), lq(1))
3201 END DO
3202 CALL allocate_sto_basis_set(sto_basis_set)
3203 CALL set_sto_basis_set(sto_basis_set, nshell=1, nq=nq, lq=lq, zet=zet, symbol=sym)
3204 CALL create_gto_from_sto_basis(sto_basis_set, pro_basis_set, 6)
3205 pro_basis_set%norm_type = 2
3206 CALL init_orb_basis_set(pro_basis_set)
3207 CALL deallocate_sto_basis_set(sto_basis_set)
3208
3209 END SUBROUTINE create_pro_basis
3210
3211! **************************************************************************************************
3212!> \brief Update the density matrix in AO basis with the active density contribution
3213!> \param active_space_env the active space environment
3214!> \param rho_ao the density matrix in AO basis
3215! **************************************************************************************************
3216 SUBROUTINE update_density_ao(active_space_env, rho_ao)
3217 TYPE(active_space_type), POINTER :: active_space_env
3218 TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: rho_ao
3219
3220 INTEGER :: ispin, nao, nmo, nspins
3221 TYPE(cp_fm_type) :: r, u
3222 TYPE(cp_fm_type), POINTER :: c_active, p_active_mo
3223 TYPE(dbcsr_type), POINTER :: p_inactive_ao
3224 TYPE(mo_set_type), DIMENSION(:), POINTER :: mos_active
3225
3226 ! Transform the AS density matrix P_MO to the atomic orbital basis,
3227 ! this is simply C * P_MO * C^T
3228 nspins = active_space_env%nspins
3229 mos_active => active_space_env%mos_active
3230 DO ispin = 1, nspins
3231 ! size of p_inactive_ao is (nao x nao)
3232 p_inactive_ao => active_space_env%pmat_inactive(ispin)%matrix
3233
3234 ! copy p_inactive_ao to rho_ao
3235 CALL dbcsr_copy(rho_ao(ispin)%matrix, p_inactive_ao)
3236
3237 ! size of p_active_mo is (nmo x nmo)
3238 p_active_mo => active_space_env%p_active(ispin)
3239
3240 ! calculate R = p_mo
3241 CALL cp_fm_create(r, p_active_mo%matrix_struct)
3242 CALL cp_fm_to_fm(p_active_mo, r)
3243
3244 ! calculate U = C * p_mo
3245 CALL get_mo_set(mos_active(ispin), mo_coeff=c_active, nao=nao, nmo=nmo)
3246 CALL cp_fm_create(u, c_active%matrix_struct)
3247 CALL parallel_gemm("N", "N", nao, nmo, nmo, 1.0_dp, c_active, r, 0.0_dp, u)
3248
3249 CALL cp_dbcsr_plus_fm_fm_t(sparse_matrix=rho_ao(ispin)%matrix, &
3250 matrix_v=u, matrix_g=c_active, ncol=nmo, alpha=1.0_dp)
3251
3252 CALL cp_fm_release(r)
3253 CALL cp_fm_release(u)
3254 END DO
3255
3256 END SUBROUTINE update_density_ao
3257
3258! **************************************************************************************************
3259!> \brief Print each value on the master node
3260!> \param this object reference
3261!> \param i i-index
3262!> \param j j-index
3263!> \param k k-index
3264!> \param l l-index
3265!> \param val value of the integral at (i,j,k.l)
3266!> \return always true to dump all integrals
3267! **************************************************************************************************
3268 LOGICAL FUNCTION eri_fcidump_print_func(this, i, j, k, l, val) RESULT(cont)
3269 CLASS(eri_fcidump_print), INTENT(INOUT) :: this
3270 INTEGER, INTENT(IN) :: i, j, k, l
3271 REAL(kind=dp), INTENT(IN) :: val
3272
3273 ! write to the actual file only on the master
3274 IF (this%unit_nr > 0) THEN
3275 WRITE (this%unit_nr, "(ES23.16,4I4)") val, i + this%bra_start - 1, j + this%bra_start - 1, &
3276 & k + this%ket_start - 1, l + this%ket_start - 1
3277 ! Each spatial integral is emitted once per permutation class.
3278 IF (max(i, j, k, l) <= this%nalpha) THEN
3279 IF (i == j .AND. k == l) THEN
3280 IF (i == k) THEN
3281 IF (i <= this%nbeta) this%determinant_energy = this%determinant_energy + val
3282 ELSE
3283 this%determinant_energy = this%determinant_energy + &
3284 merge(2, 1, i <= this%nbeta)*merge(2, 1, k <= this%nbeta)*val
3285 END IF
3286 ELSE IF (i == k .AND. j == l) THEN
3287 this%determinant_energy = this%determinant_energy - merge(2, 1, max(i, j) <= this%nbeta)*val
3288 END IF
3289 END IF
3290 END IF
3291
3292 cont = .true.
3293 END FUNCTION eri_fcidump_print_func
3294
3295! **************************************************************************************************
3296!> \brief checksum each value on the master node
3297!> \param this object reference
3298!> \param i i-index
3299!> \param j j-index
3300!> \param k k-index
3301!> \param l l-index
3302!> \param val value of the integral at (i,j,k.l)
3303!> \return always true to dump all integrals
3304! **************************************************************************************************
3305 LOGICAL FUNCTION eri_fcidump_checksum_func(this, i, j, k, l, val) RESULT(cont)
3306 CLASS(eri_fcidump_checksum), INTENT(INOUT) :: this
3307 INTEGER, INTENT(IN) :: i, j, k, l
3308 REAL(kind=dp), INTENT(IN) :: val
3309
3310 mark_used(i)
3311 mark_used(j)
3312 mark_used(k)
3313 mark_used(l)
3314
3315 this%checksum = this%checksum + abs(val)
3316
3317 cont = .true.
3318 END FUNCTION eri_fcidump_checksum_func
3319
3320! **************************************************************************************************
3321!> \brief Compute and print the AS rdm and the natural orbitals occupation numbers
3322!> \param active_space_env active space environment
3323!> \param iw output unit
3324!> \author Stefano Battaglia
3325! **************************************************************************************************
3326 SUBROUTINE print_pmat_noon(active_space_env, iw)
3327 TYPE(active_space_type), POINTER :: active_space_env
3328 INTEGER :: iw
3329
3330 INTEGER :: i1, i2, ii, ispin, jm, m1, m2, &
3331 nmo_active, nspins
3332 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :) :: noon, pmat
3333 TYPE(cp_fm_type), POINTER :: p_active
3334
3335 nspins = active_space_env%nspins
3336 nmo_active = active_space_env%nmo_active
3337
3338 ALLOCATE (noon(nmo_active, nspins))
3339 ALLOCATE (pmat(nmo_active, nmo_active))
3340
3341 DO ispin = 1, nspins
3342 p_active => active_space_env%p_active(ispin)
3343 noon(:, ispin) = 0.0_dp
3344 pmat = 0.0_dp
3345
3346 DO i1 = 1, nmo_active
3347 m1 = active_space_env%active_orbitals(i1, ispin)
3348 DO i2 = 1, nmo_active
3349 m2 = active_space_env%active_orbitals(i2, ispin)
3350 CALL cp_fm_get_element(p_active, m1, m2, pmat(i1, i2))
3351 END DO
3352 END DO
3353
3354 IF (iw > 0) THEN
3355 WRITE (iw, '(/,T3,A,I2,A)') "Active space density matrix for spin ", ispin
3356 DO i1 = 1, nmo_active
3357 DO ii = 1, nmo_active, 8
3358 jm = min(7, nmo_active - ii)
3359 WRITE (iw, '(T3,6(F9.4))') (pmat(i1, ii + i2), i2=0, jm)
3360 END DO
3361 END DO
3362 END IF
3363
3364 ! diagonalize the density matrix
3365 CALL diamat_all(pmat, noon(:, ispin))
3366
3367 IF (iw > 0) THEN
3368 WRITE (iw, '(/,T3,A,I2,A)') "Natural orbitals occupation numbers for spin ", ispin
3369 DO i1 = 1, nmo_active, 8
3370 jm = min(7, nmo_active - i1)
3371 ! noons are stored in ascending order, so reverse-print them
3372 WRITE (iw, '(T3,6(F9.4))') (noon(nmo_active - i1 - i2 + 1, ispin), i2=0, jm)
3373 END DO
3374 END IF
3375
3376 END DO
3377
3378 DEALLOCATE (noon)
3379 DEALLOCATE (pmat)
3380
3381 END SUBROUTINE print_pmat_noon
3382
3383! **************************************************************************************************
3384!> \brief Run range-separated DFT embedding with the local FCI active-space solver.
3385!> \param qs_env Quickstep environment
3386!> \param active_space_env active-space environment
3387!> \param as_input ACTIVE_SPACE input section
3388! **************************************************************************************************
3389 SUBROUTINE local_fci_embedding(qs_env, active_space_env, as_input)
3390 TYPE(qs_environment_type), POINTER :: qs_env
3391 TYPE(active_space_type), POINTER :: active_space_env
3392 TYPE(section_vals_type), POINTER :: as_input
3393
3394 CHARACTER(LEN=*), PARAMETER :: routinen = 'local_fci_embedding'
3395
3396 INTEGER :: handle, iter, iw, max_iter
3397 LOGICAL :: converged, do_scf_embedding
3398 REAL(kind=dp) :: delta_e, energy_corr, energy_new, &
3399 energy_old, energy_scf, eps_iter, t1, &
3400 t2
3401 REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: p_act_mo_a, p_act_mo_b
3402 TYPE(cp_logger_type), POINTER :: logger
3403 TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: rho_ao
3404 TYPE(dft_control_type), POINTER :: dft_control
3405 TYPE(mo_set_type), DIMENSION(:), POINTER :: mos_active
3406 TYPE(mp_para_env_type), POINTER :: para_env
3407 TYPE(qs_energy_type), POINTER :: energy
3408 TYPE(qs_ks_env_type), POINTER :: ks_env
3409 TYPE(qs_rho_type), POINTER :: rho
3410
3411 CALL timeset(routinen, handle)
3412
3413 t1 = m_walltime()
3414 logger => cp_get_default_logger()
3415 iw = cp_logger_get_default_io_unit(logger)
3416
3417 CALL get_qs_env(qs_env, para_env=para_env, dft_control=dft_control)
3418
3419 CALL section_vals_val_get(as_input, "SCF_EMBEDDING", l_val=do_scf_embedding)
3420 active_space_env%do_scf_embedding = do_scf_embedding
3421 CALL section_vals_val_get(as_input, "MAX_ITER", i_val=max_iter)
3422 IF (max_iter < 0) cpabort("Specify a non-negative number of max iterations.")
3423 CALL section_vals_val_get(as_input, "EPS_ITER", r_val=eps_iter)
3424 IF (eps_iter < 0.0_dp) cpabort("Specify a non-negative convergence threshold.")
3425
3426 CALL get_qs_env(qs_env, rho=rho, energy=energy, ks_env=ks_env)
3427 CALL qs_rho_get(rho, rho_ao=rho_ao)
3428
3429 IF (iw > 0) THEN
3430 WRITE (unit=iw, fmt="(/,T2,A,/)") &
3431 "RANGE-SEPARATED DFT EMBEDDING WITH LIBFCI SOLVER"
3432 WRITE (iw, '(T3,A,T68,I12)') "Max. iterations", max_iter
3433 WRITE (iw, '(T3,A,T68,E12.4)') "Conv. threshold", eps_iter
3434 WRITE (iw, '(T3,A,T68,A)') "Density mixer", trim(active_space_mixing_label(active_space_env))
3435 WRITE (iw, '(T3,A,T66,F14.2)') "Mixing alpha", active_space_env%alpha
3436 WRITE (unit=iw, fmt="(/,T3,A,T11,A,T21,A,T34,A,T55,A,T75,A,/,T3,A)") &
3437 "Iter", "Update", "Time", "Corr. energy", "Total energy", "Change", repeat("-", 78)
3438 END IF
3439
3440 iter = 0
3441 converged = .false.
3442 energy_scf = active_space_env%energy_ref
3443 energy_new = energy_scf
3444 mos_active => active_space_env%mos_active
3445
3446 DO WHILE (iter < max_iter)
3447 iter = iter + 1
3448
3449 IF (active_space_env%nspins == 2) THEN
3450 CALL solve_active_space_fci(active_space_env, para_env, p_act_mo_a, p_act_mo_b)
3451 active_space_env%energy_total = active_space_env%energy_inactive + active_space_env%energy_active
3452 CALL update_active_density(p_act_mo_a, active_space_env, p_act_mo_b)
3453 DEALLOCATE (p_act_mo_a, p_act_mo_b)
3454 ELSE
3455 CALL solve_active_space_fci(active_space_env, para_env, p_act_mo_a)
3456 active_space_env%energy_total = active_space_env%energy_inactive + active_space_env%energy_active
3457 CALL update_active_density(p_act_mo_a, active_space_env)
3458 DEALLOCATE (p_act_mo_a)
3459 END IF
3460
3461 energy_old = energy_new
3462 energy_new = active_space_env%energy_total
3463 energy_corr = energy_new - energy_scf
3464 delta_e = energy_new - energy_old
3465
3466 t2 = t1
3467 t1 = m_walltime()
3468 IF (iw > 0) THEN
3469 WRITE (unit=iw, &
3470 fmt="(T3,I4,T11,A,T19,F6.1,T28,F18.10,T49,F18.10,T70,ES11.2)") &
3471 iter, trim(active_space_mixing_label(active_space_env)), &
3472 t1 - t2, energy_corr, energy_new, delta_e
3473 CALL m_flush(iw)
3474 END IF
3475
3476 CALL update_density_ao(active_space_env, rho_ao)
3477 CALL qs_rho_update_rho(rho, qs_env=qs_env)
3478 CALL qs_ks_did_change(qs_env%ks_env, rho_changed=.true.)
3479 CALL evaluate_core_matrix_traces(qs_env)
3480 CALL qs_ks_build_kohn_sham_matrix(qs_env, calculate_forces=.false., &
3481 just_energy=.false., &
3482 ext_xc_section=active_space_env%xc_section)
3483
3484 active_space_env%energy_ref = energy%total
3485 CALL calculate_operators(mos_active, qs_env, active_space_env)
3486 CALL subspace_fock_matrix(active_space_env, dft_control%roks)
3487
3488 IF (.NOT. active_space_env%do_scf_embedding) THEN
3489 IF (iw > 0) THEN
3490 WRITE (unit=iw, fmt="(/,T3,A,I5,A)") &
3491 "*** one-shot embedding correction finished ***"
3492 END IF
3493 converged = .true.
3494 EXIT
3495 ELSE IF (abs(delta_e) <= eps_iter) THEN
3496 IF (iw > 0) THEN
3497 WRITE (unit=iw, fmt="(/,T3,A,I5,A)") &
3498 "*** rs-DFT embedding run converged in ", iter, " iteration(s) ***"
3499 END IF
3500 converged = .true.
3501 EXIT
3502 END IF
3503 END DO
3504
3505 IF (.NOT. converged) THEN
3506 IF (iw > 0) THEN
3507 WRITE (unit=iw, fmt="(/,T3,A,I5,A)") &
3508 "*** rs-DFT embedding did not converged after ", iter, " iteration(s) ***"
3509 END IF
3510 END IF
3511
3512 energy%total = active_space_env%energy_total
3513
3514 IF (iw > 0) THEN
3515 WRITE (unit=iw, fmt="(/,T3,A)") &
3516 "Final energy contributions:"
3517 WRITE (unit=iw, fmt="(T6,A,T56,F20.10)") &
3518 "Inactive energy:", active_space_env%energy_inactive
3519 WRITE (unit=iw, fmt="(T6,A,T56,F20.10)") &
3520 "Active energy:", active_space_env%energy_active
3521 WRITE (unit=iw, fmt="(T6,A,T56,F20.10)") &
3522 "Correlation energy:", energy_corr
3523 WRITE (unit=iw, fmt="(T6,A,T56,F20.10)") &
3524 "Total rs-DFT energy:", active_space_env%energy_total
3525 END IF
3526
3527 CALL print_pmat_noon(active_space_env, iw)
3528 CALL para_env%sync()
3529 CALL timestop(handle)
3530
3531 END SUBROUTINE local_fci_embedding
3532
3533! **************************************************************************************************
3534!> \brief ...
3535!> \param qs_env ...
3536!> \param active_space_env ...
3537!> \param as_input ...
3538! **************************************************************************************************
3539 SUBROUTINE rsdft_embedding(qs_env, active_space_env, as_input)
3540 TYPE(qs_environment_type), POINTER :: qs_env
3541 TYPE(active_space_type), POINTER :: active_space_env
3542 TYPE(section_vals_type), POINTER :: as_input
3543
3544 CHARACTER(LEN=*), PARAMETER :: routinen = 'rsdft_embedding'
3545 INTEGER :: handle
3546
3547#ifdef __NO_SOCKETS
3548 CALL timeset(routinen, handle)
3549 cpabort("CP2K was compiled with the __NO_SOCKETS option!")
3550 mark_used(qs_env)
3551 mark_used(active_space_env)
3552 mark_used(as_input)
3553#else
3554
3555 INTEGER :: iw, client_fd, socket_fd, iter, max_iter
3556 LOGICAL :: converged, do_scf_embedding, ionode
3557 REAL(kind=dp) :: delta_e, energy_corr, energy_new, &
3558 energy_old, energy_scf, eps_iter, t1, t2
3559 TYPE(cp_logger_type), POINTER :: logger
3560 TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: rho_ao
3561 TYPE(mo_set_type), DIMENSION(:), POINTER :: mos_active
3562 TYPE(mp_para_env_type), POINTER :: para_env
3563 TYPE(qs_energy_type), POINTER :: energy
3564 TYPE(qs_ks_env_type), POINTER :: ks_env
3565 TYPE(qs_rho_type), POINTER :: rho
3566 TYPE(dft_control_type), POINTER :: dft_control
3567
3568 CALL timeset(routinen, handle)
3569
3570 t1 = m_walltime()
3571
3572 logger => cp_get_default_logger()
3573 iw = cp_logger_get_default_io_unit(logger)
3574
3575 CALL get_qs_env(qs_env, para_env=para_env, dft_control=dft_control)
3576 ionode = para_env%is_source()
3577
3578 ! get info from the input
3579 CALL section_vals_val_get(as_input, "SCF_EMBEDDING", l_val=do_scf_embedding)
3580 active_space_env%do_scf_embedding = do_scf_embedding
3581 CALL section_vals_val_get(as_input, "MAX_ITER", i_val=max_iter)
3582 IF (max_iter < 0) cpabort("Specify a non-negative number of max iterations.")
3583 CALL section_vals_val_get(as_input, "EPS_ITER", r_val=eps_iter)
3584 IF (eps_iter < 0.0_dp) cpabort("Specify a non-negative convergence threshold.")
3585
3586 ! create the socket and wait for the client to connect
3587 CALL initialize_socket(socket_fd, client_fd, as_input, ionode)
3588 CALL para_env%sync()
3589
3590 ! send two-electron integrals to the client
3591 CALL send_eri_to_client(client_fd, active_space_env, para_env)
3592
3593 ! get pointer to density in ao basis
3594 CALL get_qs_env(qs_env, rho=rho, energy=energy, ks_env=ks_env)
3595 CALL qs_rho_get(rho, rho_ao=rho_ao)
3596
3597 IF (iw > 0) THEN
3598 WRITE (unit=iw, fmt="(/,T2,A,/)") &
3599 "RANGE-SEPARATED DFT EMBEDDING SELF-CONSISTENT OPTIMIZATION"
3600
3601 WRITE (iw, '(T3,A,T68,I12)') "Max. iterations", max_iter
3602 WRITE (iw, '(T3,A,T68,E12.4)') "Conv. threshold", eps_iter
3603 WRITE (iw, '(T3,A,T68,A)') "Density mixer", trim(active_space_mixing_label(active_space_env))
3604 WRITE (iw, '(T3,A,T66,F14.2)') "Mixing alpha", active_space_env%alpha
3605
3606 WRITE (unit=iw, fmt="(/,T3,A,T11,A,T21,A,T34,A,T55,A,T75,A,/,T3,A)") &
3607 "Iter", "Update", "Time", "Corr. energy", "Total energy", "Change", repeat("-", 78)
3608 END IF
3609 ! TODO: consider using an iteration level
3610
3611 iter = 0
3612 converged = .false.
3613 ! store the scf energy
3614 energy_scf = active_space_env%energy_ref
3615 energy_new = energy_scf
3616 mos_active => active_space_env%mos_active
3617 ! CALL set_qs_env(qs_env, active_space=active_space_env)
3618
3619 ! start the self-consistent embedding loop
3620 DO WHILE (iter < max_iter)
3621 iter = iter + 1
3622
3623 ! send V_emb and E_ina to the active space solver and update
3624 ! the active space environment with the new active energy and density
3625 CALL send_fock_to_client(client_fd, active_space_env, para_env)
3626
3627 ! update energies
3628 energy_old = energy_new
3629 energy_new = active_space_env%energy_total
3630 energy_corr = energy_new - energy_scf
3631 delta_e = energy_new - energy_old
3632
3633 ! get timer
3634 t2 = t1
3635 t1 = m_walltime()
3636 ! print out progress
3637 IF ((iw > 0)) THEN
3638 WRITE (unit=iw, &
3639 fmt="(T3,I4,T11,A,T19,F6.1,T28,F18.10,T49,F18.10,T70,ES11.2)") &
3640 iter, trim(active_space_mixing_label(active_space_env)), &
3641 t1 - t2, energy_corr, energy_new, delta_e
3642 CALL m_flush(iw)
3643 END IF
3644
3645 ! update total density in AO basis with the AS contribution
3646 CALL update_density_ao(active_space_env, rho_ao) ! rho_ao is updated
3647
3648 ! calculate F_ks in AO basis (which contains Vxc) with the new density
3649 CALL qs_rho_update_rho(rho, qs_env=qs_env) ! updates rho_r and rho_g using rho_ao
3650 CALL qs_ks_did_change(qs_env%ks_env, rho_changed=.true.) ! set flags about the change
3651 ! Re-evaluate the traces between the density matrix and the core Hamiltonians
3652 CALL evaluate_core_matrix_traces(qs_env)
3653 ! the ks matrix will be rebuilt so this is fine now
3654 ! CALL set_ks_env(qs_env%ks_env, potential_changed=.FALSE.)
3655 CALL qs_ks_build_kohn_sham_matrix(qs_env, calculate_forces=.false., &
3656 just_energy=.false., &
3657 ext_xc_section=active_space_env%xc_section)
3658
3659 ! update the reference energy
3660 active_space_env%energy_ref = energy%total
3661
3662 ! transform KS/Fock, Vxc and Hcore from AO to MO basis
3663 CALL calculate_operators(mos_active, qs_env, active_space_env)
3664
3665 ! calculate the new inactive energy and embedding potential
3666 CALL subspace_fock_matrix(active_space_env, dft_control%roks)
3667
3668 ! check if it is a one-shot correction
3669 IF (.NOT. active_space_env%do_scf_embedding) THEN
3670 IF (iw > 0) THEN
3671 WRITE (unit=iw, fmt="(/,T3,A,I5,A)") &
3672 "*** one-shot embedding correction finished ***"
3673 END IF
3674 converged = .true.
3675 EXIT
3676 ! check for convergence
3677 ELSE IF (abs(delta_e) <= eps_iter) THEN
3678 IF (iw > 0) THEN
3679 WRITE (unit=iw, fmt="(/,T3,A,I5,A)") &
3680 "*** rs-DFT embedding run converged in ", iter, " iteration(s) ***"
3681 END IF
3682 converged = .true.
3683 EXIT
3684 END IF
3685 END DO
3686
3687 IF (.NOT. converged) THEN
3688 IF (iw > 0) THEN
3689 WRITE (unit=iw, fmt="(/,T3,A,I5,A)") &
3690 "*** rs-DFT embedding did not converged after ", iter, " iteration(s) ***"
3691 END IF
3692 END IF
3693
3694 ! update qs total energy to the final rs-DFT energy
3695 energy%total = active_space_env%energy_total
3696
3697 ! print final energy contributions
3698 IF (iw > 0) THEN
3699 WRITE (unit=iw, fmt="(/,T3,A)") &
3700 "Final energy contributions:"
3701 WRITE (unit=iw, fmt="(T6,A,T56,F20.10)") &
3702 "Inactive energy:", active_space_env%energy_inactive
3703 WRITE (unit=iw, fmt="(T6,A,T56,F20.10)") &
3704 "Active energy:", active_space_env%energy_active
3705 WRITE (unit=iw, fmt="(T6,A,T56,F20.10)") &
3706 "Correlation energy:", energy_corr
3707 WRITE (unit=iw, fmt="(T6,A,T56,F20.10)") &
3708 "Total rs-DFT energy:", active_space_env%energy_total
3709 END IF
3710
3711 ! print the AS rdm and the natural orbital occupation numbers
3712 CALL print_pmat_noon(active_space_env, iw)
3713
3714 CALL finalize_socket(socket_fd, client_fd, as_input, ionode)
3715 CALL para_env%sync()
3716#endif
3717
3718 CALL timestop(handle)
3719
3720 END SUBROUTINE rsdft_embedding
3721
3722#ifndef __NO_SOCKETS
3723! **************************************************************************************************
3724!> \brief Creates the socket, spawns the client and connects to it
3725!> \param socket_fd the socket file descriptor
3726!> \param client_fd the client file descriptor
3727!> \param as_input active space inpute section
3728!> \param ionode logical flag indicating if the process is the master
3729! **************************************************************************************************
3730 SUBROUTINE initialize_socket(socket_fd, client_fd, as_input, ionode)
3731 INTEGER, INTENT(OUT) :: socket_fd, client_fd
3732 TYPE(section_vals_type), INTENT(IN), POINTER :: as_input
3733 LOGICAL, INTENT(IN) :: ionode
3734
3735 CHARACTER(LEN=*), PARAMETER :: routinen = 'initialize_socket'
3736 INTEGER, PARAMETER :: backlog = 10
3737
3738 CHARACTER(LEN=default_path_length) :: hostname
3739 INTEGER :: handle, iw, port, protocol
3740 LOGICAL :: inet
3741 TYPE(cp_logger_type), POINTER :: logger
3742
3743 CALL timeset(routinen, handle)
3744
3745 logger => cp_get_default_logger()
3746 iw = cp_logger_get_default_io_unit(logger)
3747
3748 ! protocol == 0 for UNIX, protocol > 0 for INET
3749 CALL section_vals_val_get(as_input, "SOCKET%INET", l_val=inet)
3750 IF (inet) THEN
3751 protocol = 1
3752 ELSE
3753 protocol = 0
3754 END IF
3755 CALL section_vals_val_get(as_input, "SOCKET%HOST", c_val=hostname)
3756 CALL section_vals_val_get(as_input, "SOCKET%PORT", i_val=port)
3757
3758 IF (ionode) THEN
3759 CALL open_bind_socket(socket_fd, protocol, port, trim(hostname)//c_null_char)
3760 WRITE (iw, '(/,T2,A,A)') "@SERVER: Created socket with address ", trim(hostname)
3761 CALL listen_socket(socket_fd, backlog)
3762
3763 ! wait until a connetion request arrives
3764 WRITE (iw, '(T2,A)') "@SERVER: Waiting for requests..."
3765 CALL accept_socket(socket_fd, client_fd)
3766 WRITE (iw, '(T2,A,I2)') "@SERVER: Accepted socket with fd ", client_fd
3767 END IF
3768
3769 CALL timestop(handle)
3770
3771 END SUBROUTINE initialize_socket
3772
3773! **************************************************************************************************
3774!> \brief Closes the connection to the socket and deletes the file
3775!> \param socket_fd the socket file descriptor
3776!> \param client_fd the client file descriptor
3777!> \param as_input active space inpute section
3778!> \param ionode logical flag indicating if the process is the master
3779! **************************************************************************************************
3780 SUBROUTINE finalize_socket(socket_fd, client_fd, as_input, ionode)
3781 INTEGER, INTENT(IN) :: socket_fd, client_fd
3782 TYPE(section_vals_type), INTENT(IN), POINTER :: as_input
3783 LOGICAL, INTENT(IN) :: ionode
3784
3785 CHARACTER(LEN=*), PARAMETER :: routinen = 'finalize_socket'
3786 INTEGER, PARAMETER :: header_len = 12
3787
3788 CHARACTER(LEN=default_path_length) :: hostname
3789 INTEGER :: handle
3790
3791 CALL timeset(routinen, handle)
3792
3793 CALL section_vals_val_get(as_input, "SOCKET%HOST", c_val=hostname)
3794
3795 IF (ionode) THEN
3796 ! signal the client to quit
3797 CALL writebuffer(client_fd, "QUIT ", header_len)
3798 ! close the connection
3799 CALL close_socket(client_fd)
3800 CALL close_socket(socket_fd)
3801
3802 ! delete the socket file
3803 IF (file_exists(trim(hostname))) THEN
3804 CALL remove_socket_file(trim(hostname)//c_null_char)
3805 END IF
3806 END IF
3807
3808 CALL timestop(handle)
3809
3810 END SUBROUTINE finalize_socket
3811
3812! **************************************************************************************************
3813!> \brief Sends the two-electron integrals to the client vie the socket
3814!> \param client_fd the client file descriptor
3815!> \param active_space_env active space environment
3816!> \param para_env parallel environment
3817! **************************************************************************************************
3818 SUBROUTINE send_eri_to_client(client_fd, active_space_env, para_env)
3819 INTEGER, INTENT(IN) :: client_fd
3820 TYPE(active_space_type), INTENT(IN), POINTER :: active_space_env
3821 TYPE(mp_para_env_type), INTENT(IN), POINTER :: para_env
3822
3823 CHARACTER(LEN=*), PARAMETER :: routinen = 'send_eri_to_client'
3824 INTEGER, PARAMETER :: header_len = 12
3825
3826 CHARACTER(LEN=default_string_length) :: header
3827 INTEGER :: handle, iw
3828 LOGICAL :: ionode, restricted_orbitals
3829 REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: eri_aa, eri_ab, eri_bb, s_ab
3830 TYPE(cp_logger_type), POINTER :: logger
3831
3832 CALL timeset(routinen, handle)
3833
3834 logger => cp_get_default_logger()
3835 iw = cp_logger_get_default_io_unit(logger)
3836 ionode = para_env%is_source()
3837 restricted_orbitals = active_space_env%restricted_orbitals
3838
3839 ALLOCATE (eri_aa(active_space_env%nmo_active**4))
3840 CALL eri_to_array(active_space_env%eri, eri_aa, active_space_env%active_orbitals, 1, 1)
3841 IF (active_space_env%nspins == 2) THEN
3842 ALLOCATE (eri_ab(active_space_env%nmo_active**4))
3843 ALLOCATE (eri_bb(active_space_env%nmo_active**4))
3844 IF (restricted_orbitals) THEN
3845 eri_ab(:) = eri_aa
3846 eri_bb(:) = eri_aa
3847 ELSE
3848 CALL eri_to_array(active_space_env%eri, eri_ab, active_space_env%active_orbitals, 1, 2)
3849 CALL eri_to_array(active_space_env%eri, eri_bb, active_space_env%active_orbitals, 2, 2)
3850 END IF
3851 ! get the overlap_ab matrix into Fortran array
3852 ALLOCATE (s_ab(active_space_env%nmo_active**2))
3853 associate(act_indices_a => active_space_env%active_orbitals(:, 1), &
3854 act_indices_b => active_space_env%active_orbitals(:, 2))
3855 CALL subspace_matrix_to_array(active_space_env%sab_sub(1), s_ab, act_indices_a, act_indices_b)
3856 END associate
3857 END IF
3858
3859 ! ask the status of the client
3860 IF (ionode) CALL writebuffer(client_fd, "STATUS ", header_len)
3861 DO
3862 header = ""
3863 CALL para_env%sync()
3864 IF (ionode) THEN
3865 ! IF (iw > 0) WRITE(iw, *) "@SERVER: Waiting for messages..."
3866 CALL readbuffer(client_fd, header, header_len)
3867 END IF
3868 CALL para_env%bcast(header, para_env%source)
3869
3870 ! IF (iw > 0) WRITE(iw, *) "@SERVER: Message from client: ", TRIM(header)
3871
3872 IF (trim(header) == "READY") THEN
3873 ! if the client is ready, send the data
3874 CALL para_env%sync()
3875 IF (ionode) THEN
3876 CALL writebuffer(client_fd, "TWOBODY ", header_len)
3877 CALL writebuffer(client_fd, active_space_env%nspins)
3878 CALL writebuffer(client_fd, active_space_env%nmo_active)
3879 CALL writebuffer(client_fd, active_space_env%nelec_active)
3880 CALL writebuffer(client_fd, active_space_env%multiplicity)
3881 ! send the alpha component
3882 CALL writebuffer(client_fd, eri_aa, SIZE(eri_aa))
3883 ! send the beta part for unrestricted calculations
3884 IF (active_space_env%nspins == 2) THEN
3885 CALL writebuffer(client_fd, eri_ab, SIZE(eri_ab))
3886 CALL writebuffer(client_fd, eri_bb, SIZE(eri_bb))
3887 CALL writebuffer(client_fd, s_ab, SIZE(s_ab))
3888 END IF
3889 END IF
3890 ELSE IF (trim(header) == "RECEIVED") THEN
3891 EXIT
3892 END IF
3893 END DO
3894
3895 DEALLOCATE (eri_aa)
3896 IF (active_space_env%nspins == 2) THEN
3897 DEALLOCATE (eri_ab)
3898 DEALLOCATE (eri_bb)
3899 DEALLOCATE (s_ab)
3900 END IF
3901
3902 CALL para_env%sync()
3903
3904 CALL timestop(handle)
3905
3906 END SUBROUTINE send_eri_to_client
3907
3908! **************************************************************************************************
3909!> \brief Sends the one-electron embedding potential and the inactive energy to the client
3910!> \param client_fd the client file descriptor
3911!> \param active_space_env active space environment
3912!> \param para_env parallel environment
3913! **************************************************************************************************
3914 SUBROUTINE send_fock_to_client(client_fd, active_space_env, para_env)
3915 INTEGER, INTENT(IN) :: client_fd
3916 TYPE(active_space_type), INTENT(INOUT), POINTER :: active_space_env
3917 TYPE(mp_para_env_type), INTENT(IN), POINTER :: para_env
3918
3919 CHARACTER(LEN=*), PARAMETER :: routinen = 'send_fock_to_client'
3920 INTEGER, PARAMETER :: header_len = 12
3921
3922 CHARACTER(LEN=default_string_length) :: header
3923 INTEGER :: handle, iw
3924 LOGICAL :: debug, ionode
3925 REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: fock_a, fock_b, p_act_mo_a, p_act_mo_b
3926 TYPE(cp_logger_type), POINTER :: logger
3927
3928 CALL timeset(routinen, handle)
3929
3930 ! Set to .TRUE. to activate debug output
3931 debug = .false.
3932
3933 logger => cp_get_default_logger()
3934 iw = cp_logger_get_default_io_unit(logger)
3935 ionode = para_env%is_source()
3936
3937 ALLOCATE (p_act_mo_a(active_space_env%nmo_active**2))
3938 ALLOCATE (fock_a(active_space_env%nmo_active**2))
3939 IF (active_space_env%nspins == 2) THEN
3940 ALLOCATE (p_act_mo_b(active_space_env%nmo_active**2))
3941 ALLOCATE (fock_b(active_space_env%nmo_active**2))
3942 END IF
3943
3944 ! get the fock matrix into Fortran arrays
3945 associate(act_indices => active_space_env%active_orbitals(:, 1))
3946 CALL subspace_matrix_to_array(active_space_env%fock_sub(1), fock_a, act_indices, act_indices)
3947 END associate
3948
3949 IF (active_space_env%nspins == 2) THEN
3950 associate(act_indices => active_space_env%active_orbitals(:, 2))
3951 CALL subspace_matrix_to_array(active_space_env%fock_sub(2), fock_b, act_indices, act_indices)
3952 END associate
3953 END IF
3954
3955 ! ask the status of the client
3956 IF (ionode) CALL writebuffer(client_fd, "STATUS ", header_len)
3957 DO
3958 header = ""
3959
3960 CALL para_env%sync()
3961 IF (ionode) THEN
3962 IF (debug .AND. iw > 0) WRITE (iw, *) "@SERVER: Waiting for messages..."
3963 CALL readbuffer(client_fd, header, header_len)
3964 END IF
3965 CALL para_env%bcast(header, para_env%source)
3966
3967 IF (debug .AND. iw > 0) WRITE (iw, *) "@SERVER: Message from client: ", trim(header)
3968
3969 IF (trim(header) == "READY") THEN
3970 ! if the client is ready, send the data
3971 CALL para_env%sync()
3972 IF (ionode) THEN
3973 CALL writebuffer(client_fd, "ONEBODY ", header_len)
3974 CALL writebuffer(client_fd, active_space_env%energy_inactive)
3975 ! send the alpha component
3976 CALL writebuffer(client_fd, fock_a, SIZE(fock_a))
3977 ! send the beta part for unrestricted calculations
3978 IF (active_space_env%nspins == 2) THEN
3979 CALL writebuffer(client_fd, fock_b, SIZE(fock_b))
3980 END IF
3981 END IF
3982
3983 ELSE IF (trim(header) == "HAVEDATA") THEN
3984 ! qiskit has data to transfer, let them know we want it and wait for it
3985 CALL para_env%sync()
3986 IF (ionode) THEN
3987 IF (debug .AND. iw > 0) WRITE (iw, *) "@SERVER: Qiskit has data to transfer"
3988 CALL writebuffer(client_fd, "GETDENSITY ", header_len)
3989
3990 ! read the active energy and density
3991 CALL readbuffer(client_fd, active_space_env%energy_active)
3992 CALL readbuffer(client_fd, p_act_mo_a, SIZE(p_act_mo_a))
3993 IF (active_space_env%nspins == 2) THEN
3994 CALL readbuffer(client_fd, p_act_mo_b, SIZE(p_act_mo_b))
3995 END IF
3996 END IF
3997
3998 ! broadcast the data to all processors
3999 CALL para_env%bcast(active_space_env%energy_active, para_env%source)
4000 CALL para_env%bcast(p_act_mo_a, para_env%source)
4001 IF (active_space_env%nspins == 2) THEN
4002 CALL para_env%bcast(p_act_mo_b, para_env%source)
4003 END IF
4004
4005 ! update total and reference energies in active space enviornment
4006 active_space_env%energy_total = active_space_env%energy_inactive + active_space_env%energy_active
4007
4008 ! update the active density matrix in the active space environment
4009 IF (active_space_env%nspins == 2) THEN
4010 CALL update_active_density(p_act_mo_a, active_space_env, p_act_mo_b)
4011 ELSE
4012 CALL update_active_density(p_act_mo_a, active_space_env)
4013 END IF
4014
4015 ! the non-iterative part is done, we can continue
4016 EXIT
4017 END IF
4018
4019 END DO
4020
4021 DEALLOCATE (p_act_mo_a)
4022 DEALLOCATE (fock_a)
4023 IF (active_space_env%nspins == 2) THEN
4024 DEALLOCATE (p_act_mo_b)
4025 DEALLOCATE (fock_b)
4026 END IF
4027
4028 CALL para_env%sync()
4029
4030 CALL timestop(handle)
4031
4032 END SUBROUTINE send_fock_to_client
4033#endif
4034
4035END MODULE qs_active_space_methods
Types and set/get functions for auxiliary density matrix methods.
Definition admm_types.F:15
subroutine, public get_admm_env(admm_env, mo_derivs_aux_fit, mos_aux_fit, sab_aux_fit, sab_aux_fit_asymm, sab_aux_fit_vs_orb, matrix_s_aux_fit, matrix_s_aux_fit_kp, matrix_s_aux_fit_vs_orb, matrix_s_aux_fit_vs_orb_kp, task_list_aux_fit, matrix_ks_aux_fit, matrix_ks_aux_fit_kp, matrix_ks_aux_fit_im, matrix_ks_aux_fit_dft, matrix_ks_aux_fit_hfx, matrix_ks_aux_fit_dft_kp, matrix_ks_aux_fit_hfx_kp, rho_aux_fit, rho_aux_fit_buffer, admm_dm)
Get routine for the ADMM env.
Definition admm_types.F:600
subroutine, public admm_env_release(admm_env)
releases the ADMM environment, cleans up all types
Definition admm_types.F:430
Define the atomic kind types and their sub types.
subroutine, public create_gto_from_sto_basis(sto_basis_set, gto_basis_set, ngauss, ortho, ngaussflex)
...
pure real(dp) function, public srules(z, ne, n, l)
...
subroutine, public deallocate_sto_basis_set(sto_basis_set)
...
subroutine, public allocate_sto_basis_set(sto_basis_set)
...
subroutine, public set_sto_basis_set(sto_basis_set, name, nshell, symbol, nq, lq, zet)
...
subroutine, public init_orb_basis_set(gto_basis_set)
Initialise a Gaussian-type orbital (GTO) basis set data set.
Handles all functions related to the CELL.
subroutine, public write_cell_low(cell, unit_str, output_unit, label)
Write the cell parameters to the output unit.
subroutine, public set_cell_param(cell, cell_length, cell_angle, periodic, do_init_cell)
Sets the cell using the internal parameters (a,b,c) (alpha,beta,gamma) using the convention: a parall...
subroutine, public init_cell(cell, hmat, periodic)
Initialise/readjust a simulation cell after hmat has been changed.
Handles all functions related to the CELL.
Definition cell_types.F:15
integer, parameter, public use_perd_xyz
Definition cell_types.F:42
integer, parameter, public use_perd_none
Definition cell_types.F:42
methods related to the blacs parallel environment
integer, parameter, public blacs_grid_square
subroutine, public cp_blacs_env_release(blacs_env)
releases the given blacs_env
subroutine, public cp_blacs_env_create(blacs_env, para_env, blacs_grid_layout, blacs_repeatable, row_major, grid_2d)
allocates and initializes a type that represent a blacs context
Defines control structures, which contain the parameters and the settings for the DFT-based calculati...
logical function, public dbcsr_iterator_blocks_left(iterator)
...
subroutine, public dbcsr_iterator_stop(iterator)
...
subroutine, public dbcsr_copy(matrix_b, matrix_a, name, keep_sparsity, keep_imaginary)
...
subroutine, public dbcsr_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_iterator_start(iterator, matrix, dynamic, dynamic_byrows)
...
subroutine, public dbcsr_iterator_next_block(iterator, row, column, block, block_number_argument_has_been_removed, row_size, col_size, row_offset, col_offset, transposed)
...
real(kind=dp) function, public dbcsr_get_occupation(matrix)
...
subroutine, public dbcsr_set(matrix, alpha)
...
subroutine, public dbcsr_release(matrix)
...
CSR matrix type, DBCSR-CSR conversions, and CSR file I/O built on cp_dbcsr_api.
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_plus_fm_fm_t(sparse_matrix, matrix_v, matrix_g, ncol, alpha, keep_sparsity, symmetry_mode)
performs the multiplication sparse_matrix+dense_mat*dens_mat^T if matrix_g is not explicitly given,...
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...
DBCSR output in CP2K.
subroutine, public cp_dbcsr_write_sparse_matrix(sparse_matrix, before, after, qs_env, para_env, first_row, last_row, first_col, last_col, scale, output_unit, omit_headers, cartesian_basis)
...
Utility routines to open and close files.
Definition cp_files.F:22
logical function, public file_exists(file_name)
Checks if a file exists, considering also the file discovery mechanism.
Definition cp_files.F:518
subroutine, public open_file(file_name, unit_number, file_status, file_form, file_action, file_position, file_pad, file_access, debug)
Opens a file and returns its logical unit number.
Definition cp_files.F:197
subroutine, public close_file(unit_number, file_status)
Closes an open file given by its logical unit number.
Definition cp_files.F:64
Basic linear algebra operations for full matrices.
subroutine, public cp_fm_column_scale(matrixa, scaling, first_col, source)
Scales columns of the full matrix by corresponding factors.
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_get_element(matrix, irow_global, icol_global, alpha, local)
returns an element of a fm this value is valid on every cpu using this call is expensive
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_set_element(matrix, irow_global, icol_global, alpha)
sets an element of a matrix
subroutine, public cp_fm_init_random(matrix, ncol, start_col)
fills a matrix with random numbers
subroutine, public cp_fm_write_formatted(fm, unit, header, value_format)
Write out a full matrix in plain text.
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...
type(cp_logger_type) function, pointer, public cp_get_default_logger()
returns the default logger
routines to handle the output, The idea is to remove the decision of wheter to output and what to out...
integer, parameter, public debug_print_level
integer, parameter, public low_print_level
integer, parameter, public medium_print_level
subroutine, public cp_print_key_finished_output(unit_nr, logger, basis_section, print_key_path, local, ignore_should_output, on_file, mpi_io)
should be called after you finish working with a unit obtained with cp_print_key_unit_nr,...
integer, parameter, public cp_p_file
integer, parameter, public high_print_level
integer function, public cp_print_key_should_output(iteration_info, basis_section, print_key_path, used_print_key, first_time)
returns what should be done with the given property if btest(res,cp_p_store) then the property should...
integer, parameter, public silent_print_level
integer function, public cp_print_key_unit_nr(logger, basis_section, print_key_path, extension, middle_name, local, log_filename, ignore_should_output, file_form, file_position, file_action, file_status, do_backup, on_file, is_new_file, mpi_io, fout, file_access)
...
A wrapper around pw_to_cube() which accepts particle_list_type.
subroutine, public cp_pw_to_cube(pw, unit_nr, title, particles, zeff, stride, max_file_size_mb, zero_tails, silent, mpi_io)
...
Module to compute the error function of a complex argument.
Definition erf_complex.F:50
elemental complex(kind=dp) function, public erfz_fast(z)
Computes the error function of a complex argument using the Poppe and Wijers algorithm.
Types to describe group distributions.
Types and set/get functions for HFX.
Definition hfx_types.F:16
subroutine, public hfx_create(x_data, para_env, hfx_section, atomic_kind_set, qs_kind_set, particle_set, dft_control, cell, orb_basis, ri_basis, nelectron_total, nkp_grid)
This routine allocates and initializes all types in hfx_data
Definition hfx_types.F:565
subroutine, public hfx_release(x_data)
This routine deallocates all data structures
Definition hfx_types.F:1863
collects all constants needed in input so that they can be used without circular dependencies
integer, parameter, public eri_operator_erf
integer, parameter, public qiskit_solver
integer, parameter, public no_solver
integer, parameter, public wannier_projection
integer, parameter, public mao_projection
integer, parameter, public eri_method_full_gpw
integer, parameter, public casci_canonical
integer, parameter, public manual_selection
integer, parameter, public eri_operator_gaussian
integer, parameter, public high_spin_roks
integer, parameter, public ot_precond_solver_default
integer, parameter, public eri_method_gpw_ht
integer, parameter, public eri_operator_erfc
integer, parameter, public ot_precond_full_single_inverse
integer, parameter, public eri_poisson_mt
integer, parameter, public eri_poisson_analytic
integer, parameter, public eri_operator_trunc
integer, parameter, public eri_poisson_periodic
integer, parameter, public eri_operator_coulomb
integer, parameter, public eri_operator_yukawa
integer, parameter, public eri_operator_lr_trunc
integer, parameter, public fci_solver
objects that represent the structure of input sections and the data contained in an input section
subroutine, public section_vals_val_set(section_vals, keyword_name, i_rep_section, i_rep_val, val, l_val, i_val, r_val, c_val, l_vals_ptr, i_vals_ptr, r_vals_ptr, c_vals_ptr)
sets the requested value
recursive type(section_vals_type) function, pointer, public section_vals_get_subs_vals(section_vals, subsection_name, i_rep_section, can_return_null)
returns the values of the requested subsection
subroutine, public section_vals_get(section_vals, ref_count, n_repetition, n_subs_vals_rep, section, explicit)
returns various attributes about the section_vals
subroutine, public section_vals_set_subs_vals(section_vals, subsection_name, new_section_vals, i_rep_section)
replaces of the requested subsection with the one given
subroutine, public section_vals_val_get(section_vals, keyword_name, i_rep_section, i_rep_val, n_rep_val, val, l_val, i_val, r_val, c_val, l_vals, i_vals, r_vals, c_vals, explicit)
returns the requested value
Defines the basic variable types.
Definition kinds.F:23
integer, parameter, public int_8
Definition kinds.F:54
integer, parameter, public dp
Definition kinds.F:34
integer, parameter, public default_string_length
Definition kinds.F:57
integer, parameter, public default_path_length
Definition kinds.F:58
Types and basic routines needed for a kpoint calculation.
subroutine, public get_kpoint_info(kpoint, kp_scheme, nkp_grid, kp_shift, symmetry, verbose, full_grid, use_real_wfn, eps_geo, parallel_group_size, kp_range, nkp, xkp, wkp, para_env, blacs_env_all, para_env_kp, para_env_inter_kp, blacs_env, kp_env, kp_aux_env, mpools, iogrp, nkp_groups, kp_dist, cell_to_index, index_to_cell, sab_nl, sab_nl_nosym, inversion_symmetry_only, symmetry_backend, symmetry_reduction_method, gamma_centered, rskp_transform)
Retrieve information from a kpoint environment.
Machine interface based on Fortran 2003 and POSIX.
Definition machine.F:17
subroutine, public m_flush(lunit)
flushes units if the &GLOBAL flag is set accordingly
Definition machine.F:129
real(kind=dp) function, public m_walltime()
returns time from a real-time clock, protected against rolling early/easily
Definition machine.F:146
Definition of mathematical constants and functions.
real(kind=dp), parameter, public pi
real(kind=dp), parameter, public rootpi
real(kind=dp), parameter, public fourpi
real(kind=dp), parameter, public twopi
Collection of simple mathematical functions and subroutines.
Definition mathlib.F:15
subroutine, public diamat_all(a, eigval, dac)
Diagonalize the symmetric n by n matrix a using the LAPACK library. Only the upper triangle of matrix...
Definition mathlib.F:384
Utility routines for the memory handling.
Interface to the message passing library MPI.
subroutine, public mp_para_env_release(para_env)
releases the para object (to be called when you don't want anymore the shared copy of this object)
Functions handling the MOLDEN format. Split from mode_selective.
subroutine, public write_mos_molden(mos, qs_kind_set, particle_set, print_section, cell, unoccupied_orbs, unoccupied_evals, qs_env, calc_energies)
Write out the MOs in molden format for visualisation.
Calls routines to get RI integrals and calculate total energies.
Definition mp2_gpw.F:14
subroutine, public grep_rows_in_subgroups(para_env, para_env_sub, mo_coeff, gd_array, c)
...
Definition mp2_gpw.F:763
subroutine, public build_dbcsr_from_rows(para_env_sub, mo_coeff_to_build, cread, mat_munu, gd_array, eps_filter)
Encapsulate the building of dbcsr_matrices mo_coeff_(v,o,all).
Definition mp2_gpw.F:891
subroutine, public create_mat_munu(mat_munu, qs_env, eps_grid, blacs_env_sub, do_ri_aux_basis, do_mixed_basis, group_size_prim, do_alloc_blocks_from_nbl, do_kpoints, sab_orb_sub, dbcsr_sym_type, custom_row_blk_sizes)
Encapsulate the building of dbcsr_matrix mat_munu.
Definition mp2_gpw.F:996
integer, parameter, public mt0d
Definition mt_util.F:34
basic linear algebra operations for full matrixes
represent a simple array based list of the given type
Define the data structure for the particle information.
Periodic Table related data definitions.
type(atom), dimension(0:nelem), public ptable
Definition of physical constants:
Definition physcon.F:68
real(kind=dp), parameter, public angstrom
Definition physcon.F:144
real(kind=dp), parameter, public bohr
Definition physcon.F:147
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, full_all_rotation)
...
methods of pw_env that have dependence on qs_env
subroutine, public pw_env_rebuild(pw_env, qs_env, external_para_env)
rebuilds the pw_env data (necessary if cell or cutoffs change)
subroutine, public pw_env_create(pw_env)
creates a pw_env, if qs_env is given calls pw_env_rebuild
container for various plainwaves related things
subroutine, public pw_env_release(pw_env, para_env)
releases the given pw_env (see doc/ReferenceCounting.html)
subroutine, public pw_env_get(pw_env, pw_pools, cube_info, gridlevel_info, auxbas_pw_pool, auxbas_grid, auxbas_rs_desc, auxbas_rs_grid, rs_descs, rs_grids, xc_pw_pool, vdw_pw_pool, poisson_env, interp_section)
returns the various attributes of the pw env
subroutine, public pw_gauss_damp(pw, omega)
Multiply all data points with a Gaussian damping factor Needed for longrange Coulomb potential V(\vec...
subroutine, public pw_compl_gauss_damp(pw, omega)
Multiply all data points with a Gaussian damping factor Needed for longrange Coulomb potential V(\vec...
functions related to the poisson solver on regular grids
integer, parameter, public pw_poisson_periodic
integer, parameter, public periodic3d
integer, parameter, public analytic0d
integer, parameter, public pw_poisson_analytic
Manages a pool of grids (to be used for example as tmp objects), but can also be used to instantiate ...
The module to read/write QCSchema HDF5 files for interfacing CP2K with other programs.
Definition qcschema.F:14
subroutine, public qcschema_env_release(qcschema_env)
Releases the allocated memory of a qcschema environment.
Definition qcschema.F:611
subroutine, public qcschema_env_create(qcschema_env, qs_env)
Create and initialize a qcschema object from a quickstep environment.
Definition qcschema.F:272
subroutine, public qcschema_to_hdf5(qcschema_env, filename)
Writes a qcschema object to an hdf5 file.
Definition qcschema.F:741
Local FCI solver interface for active-space embedding.
subroutine, public solve_active_space_fci(active_space_env, para_env, p_act_mo_a, p_act_mo_b)
Solve the current active-space Hamiltonian with the local FCI kernel.
Determine active space Hamiltonian.
subroutine eri_fcidump_set(this, bra_start, ket_start)
Sets the starting indices of the bra and ket.
subroutine, public active_space_main(qs_env)
Main method for determining the active space Hamiltonian.
Dense density mixing for active-space embedding.
character(len=15) function, public active_space_mixing_label(active_space_env)
Return the current active-space mixer label for iteration output.
subroutine, public update_active_density(p_act_mo_a, active_space_env, p_act_mo_b)
Update active space density matrix from Fortran arrays.
subroutine, public initialize_active_space_mixing(active_space_env, as_input)
Initialize the density mixer used in the self-consistent active-space embedding loop.
The types needed for the calculation of active space Hamiltonians.
subroutine, public csr_idx_from_combined(ij, n, i, j)
extracts indices i and j from combined index ij
integer function, public csr_idx_to_combined(i, j, n)
calculates combined index (ij)
subroutine, public create_active_space_type(active_space_env)
Creates an active space environment type, nullifying all quantities.
Contains utility routines for the active space module.
pure subroutine, public find_degenerate_splits(eigenvalues, active, eps_degenerate, npairs, first_active, first_other)
Finds pairs of degenerate orbitals of which one is active and the other is not, i....
subroutine, public eri_to_array(eri_env, array, active_orbitals, spin1, spin2)
Copy the eri tensor for spins isp1 and isp2 to a standard 1D Fortran array.
subroutine, public subspace_matrix_to_array(source_matrix, target_array, row_index, col_index)
Copy a (square portion) of a cp_fm_type matrix to a standard 1D Fortran array.
Calculate the plane wave density by collocating the primitive Gaussian functions (pgf).
subroutine, public calculate_wavefunction(mo_vectors, ivector, rho, rho_gspace, atomic_kind_set, qs_kind_set, cell, dft_control, particle_set, pw_env, basis_type)
maps a given wavefunction on the grid
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, matrix_vhxc, 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, matrix_metric, matrix_metric_kp)
Get the QUICKSTEP environment.
subroutine, public set_qs_env(qs_env, super_cell, mos, qmmm, qmmm_periodic, mimic, ewald_env, ewald_pw, mpools, rho_external, external_vxc, mask, scf_control, rel_control, qs_charges, ks_env, ks_qmmm_env, wf_history, scf_env, active_space, input, oce, rho_atom_set, rho0_atom_set, rho0_mpole, run_rtp, rtp, rhoz_set, rhoz_tot, ecoul_1c, has_unit_metric, requires_mo_derivs, mo_derivs, mo_loc_history, efield, rhoz_cneo_set, linres_control, xas_env, cp_ddapc_env, cp_ddapc_ewald, outer_scf_history, outer_scf_ihistory, x_data, et_coupling, dftb_potential, se_taper, se_store_int_env, se_nddo_mpole, se_nonbond_env, admm_env, ls_scf_env, do_transport, transport_env, lri_env, lri_density, exstate_env, ec_env, dispersion_env, harris_env, gcp_env, mp2_env, bs_env, kg_env, force, kpoints, wanniercentres, almo_scf_env, gradient_history, variable_history, embed_pot, spin_embed_pot, polar_env, mos_last_converged, eeq, rhs, do_rixs, tb_tblite)
Set the QUICKSTEP environment.
Integrate single or product functions over a potential on a RS grid.
Define the quickstep kind type and their sub types.
routines that build the Kohn-Sham matrix (i.e calculate the coulomb and xc parts
subroutine, public evaluate_core_matrix_traces(qs_env, rho_ao_ext)
Calculates the traces of the core matrices and the density matrix.
subroutine, public qs_ks_update_qs_env(qs_env, calculate_forces, just_energy, print_active)
updates the Kohn Sham matrix of the given qs_env (facility method)
subroutine, public qs_ks_build_kohn_sham_matrix(qs_env, calculate_forces, just_energy, print_active, ext_ks_matrix, ext_xc_section)
routine where the real calculations are made: the KS matrix is calculated
subroutine, public set_ks_env(ks_env, v_hartree_rspace, s_mstruct_changed, rho_changed, exc_accint, potential_changed, forces_up_to_date, complex_ks, matrix_h, matrix_h_im, matrix_ks, matrix_ks_im, matrix_vxc, kinetic, matrix_s, matrix_s_ri_aux, matrix_w, matrix_p_mp2, matrix_p_mp2_admm, matrix_vhxc, matrix_h_kp, matrix_h_im_kp, matrix_ks_kp, matrix_vxc_kp, kinetic_kp, matrix_s_kp, matrix_w_kp, matrix_s_ri_aux_kp, matrix_ks_im_kp, vppl, xcint_weights, rho_core, rho_nlcc, rho_nlcc_g, vee, neighbor_list_id, kpoints, sab_orb, sab_all, sac_ae, sac_ppl, sac_lri, sap_ppnl, sap_oce, sab_lrc, sab_se, sab_xtbe, sab_tbe, sab_core, sab_xb, sab_xtb_pp, sab_xtb_nonbond, sab_vdw, sab_scp, sab_almo, sab_kp, sab_kp_nosym, sab_cneo, task_list, task_list_soft, subsys, dft_control, dbcsr_dist, distribution_2d, pw_env, para_env, blacs_env, uspp, upf_inverse, skala)
...
subroutine, public qs_ks_did_change(ks_env, s_mstruct_changed, rho_changed, potential_changed, full_reset)
tells that some of the things relevant to the ks calculation did change. has to be called when change...
Definition and initialisation of the mo data type.
Definition qs_mo_io.F:21
subroutine, public write_mo_set_to_output_unit(mo_set, qs_kind_set, particle_set, dft_section, before, kpoint, final_mos, spin, solver_method, rtp, cpart, sim_step, umo_set, qs_env, para_env_inter_kp)
Write MO information to output file (eigenvalues, occupation numbers, coefficients).
Definition qs_mo_io.F:1027
collects routines that perform operations directly related to MOs
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...
Definition and initialisation of the mo data type.
Definition qs_mo_types.F:22
subroutine, public init_mo_set(mo_set, fm_pool, fm_ref, fm_struct, name, counter, complex_coeff)
initializes an allocated mo_set. eigenvalues, mo_coeff, occupation_numbers are valid only after this ...
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, cmo_coeff)
Get the components of a MO set data structure.
Define the neighbor list data types and the corresponding functionality.
subroutine, public release_neighbor_list_sets(nlists)
releases an array of neighbor_list_sets
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)
...
methods of the rho structure (defined in qs_rho_types)
subroutine, public qs_rho_update_rho(rho_struct, qs_env, rho_xc_external, local_rho_set, task_list_external, task_list_external_soft, pw_env_external, para_env_external)
updates rho_r and rho_g to the rhorho_ao. if use_kinetic_energy_density also computes tau_r and tau_g...
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...
Utility routines for qs_scf.
subroutine, public qs_scf_compute_properties(qs_env, wf_type, do_mp2)
computes properties for a given hamilonian using the current wfn
types that represent a quickstep subsys
subroutine, public qs_subsys_get(subsys, atomic_kinds, atomic_kind_set, particles, particle_set, local_particles, molecules, molecule_set, molecule_kinds, molecule_kind_set, local_molecules, para_env, colvar_p, shell_particles, core_particles, gci, multipoles, natom, nparticle, ncore, nshell, nkind, atprop, virial, results, cell, cell_ref, use_ref_cell, energy, force, qs_kind_set, cp_subsys, nelectron_total, nelectron_spin)
...
parameters that control an scf iteration
Implements UNIX and INET sockets.
generate the tasks lists used by collocate and integrate routines
subroutine, public generate_qs_task_list(ks_env, task_list, basis_type, reorder_rs_grid_ranks, skip_load_balance_distributed, pw_env_external, sab_orb_external, ext_kpoints)
...
types for task lists
subroutine, public deallocate_task_list(task_list)
deallocates the components and the object itself
subroutine, public allocate_task_list(task_list)
allocates and initialised the components of the task_list_type
All kind of helpful little routines.
Definition util.F:14
pure integer function, dimension(2), public get_limit(m, n, me)
divide m entries into n parts, return size of part me
Definition util.F:333
void open_bind_socket(int *psockfd, int *inet, int *port, char *host)
Opens and binds a socket.
Definition sockets.c:144
void writebuffer(int *psockfd, char *data, int *plen)
Writes to a socket.
Definition sockets.c:212
void accept_socket(int *psockfd, int *pclientfd)
Listens to a socket.
Definition sockets.c:277
void close_socket(int *psockfd)
Closes a socket.
Definition sockets.c:288
void listen_socket(int *psockfd, int *backlog)
Listens to a socket.
Definition sockets.c:264
void readbuffer(int *psockfd, char *data, int *plen)
Reads from a socket.
Definition sockets.c:236
void remove_socket_file(char *host)
Removes a socket file.
Definition sockets.c:294
stores some data used in wavefunction fitting
Definition admm_types.F:121
Provides all information about an atomic kind.
Type defining parameters related to the simulation cell.
Definition cell_types.F:60
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
type of a logger, at the moment it contains just a print level starting at which level it should be l...
Contains information about kpoints.
stores all the informations relevant to an mpi environment
contained for different pw related things
contains all the informations needed by the fft based poisson solvers
environment for the poisson solver
Manages a pool of grids (to be used for example as tmp objects), but can also be used to instantiate ...
The full QCSchema output type. For more information refer to: https://molssi-qc-schema....
Definition qcschema.F:253
Abstract function object for the eri_type_eri_foreach method.
Provides all information about a quickstep kind.
calculation environment to calculate the ks matrix, holds all the needed vars. assumes that the core ...
keeps the density in various representations, keeping track of which ones are valid.