LCOV - code coverage report
Current view: top level - src - qs_active_space_methods.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:3db43b4) Lines: 72.7 % 1611 1172
Test Date: 2026-04-03 06:55:30 Functions: 60.7 % 28 17

            Line data    Source code
       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              : ! **************************************************************************************************
      14              : MODULE qs_active_space_methods
      15              :    USE admm_types, ONLY: admm_type, &
      16              :                          get_admm_env, &
      17              :                          admm_env_release
      18              :    USE atomic_kind_types, ONLY: atomic_kind_type
      19              :    USE basis_set_types, ONLY: allocate_sto_basis_set, &
      20              :                               create_gto_from_sto_basis, &
      21              :                               deallocate_sto_basis_set, &
      22              :                               gto_basis_set_type, &
      23              :                               init_orb_basis_set, &
      24              :                               set_sto_basis_set, &
      25              :                               srules, &
      26              :                               sto_basis_set_type
      27              :    USE cell_types, ONLY: cell_type, use_perd_none, use_perd_xyz
      28              :    USE cell_methods, ONLY: init_cell, set_cell_param, write_cell_low
      29              :    USE cp_blacs_env, ONLY: cp_blacs_env_type, cp_blacs_env_create, cp_blacs_env_release, BLACS_GRID_SQUARE
      30              :    USE cp_control_types, ONLY: dft_control_type, qs_control_type
      31              :    USE cp_dbcsr_operations, ONLY: cp_dbcsr_plus_fm_fm_t, &
      32              :                                   cp_dbcsr_sm_fm_multiply, &
      33              :                                   dbcsr_allocate_matrix_set, &
      34              :                                   cp_dbcsr_m_by_n_from_template, copy_dbcsr_to_fm
      35              :    USE cp_dbcsr_output, ONLY: cp_dbcsr_write_sparse_matrix
      36              :    USE cp_files, ONLY: close_file, &
      37              :                        file_exists, &
      38              :                        open_file
      39              :    USE cp_fm_basic_linalg, ONLY: cp_fm_column_scale
      40              :    USE cp_fm_struct, ONLY: cp_fm_struct_create, &
      41              :                            cp_fm_struct_release, &
      42              :                            cp_fm_struct_type
      43              :    USE cp_fm_types, ONLY: &
      44              :       cp_fm_create, cp_fm_get_element, cp_fm_get_info, cp_fm_init_random, cp_fm_release, &
      45              :       cp_fm_set_all, cp_fm_set_element, cp_fm_to_fm, cp_fm_type, cp_fm_write_formatted
      46              :    USE cp_log_handling, ONLY: cp_get_default_logger, &
      47              :                               cp_logger_get_default_io_unit, &
      48              :                               cp_logger_type
      49              :    USE cp_output_handling, ONLY: &
      50              :       cp_p_file, cp_print_key_finished_output, cp_print_key_should_output, cp_print_key_unit_nr, &
      51              :       debug_print_level, high_print_level, low_print_level, medium_print_level, &
      52              :       silent_print_level
      53              :    USE cp_realspace_grid_cube, ONLY: cp_pw_to_cube
      54              :    USE cp_dbcsr_api, ONLY: &
      55              :       dbcsr_copy, dbcsr_csr_create, dbcsr_csr_type, dbcsr_p_type, dbcsr_type, dbcsr_release, &
      56              :       dbcsr_type_no_symmetry, dbcsr_create, dbcsr_set, dbcsr_multiply, dbcsr_iterator_next_block, &
      57              :       dbcsr_iterator_start, dbcsr_iterator_stop, dbcsr_iterator_blocks_left, &
      58              :       dbcsr_iterator_type, dbcsr_type_symmetric, dbcsr_get_occupation, dbcsr_get_info
      59              :    USE erf_complex, ONLY: erfz_fast
      60              :    USE group_dist_types, ONLY: get_group_dist, release_group_dist, group_dist_d1_type
      61              :    USE input_constants, ONLY: &
      62              :       casci_canonical, eri_method_full_gpw, eri_method_gpw_ht, eri_operator_coulomb, &
      63              :       eri_operator_erf, eri_operator_erfc, eri_operator_gaussian, eri_operator_yukawa, &
      64              :       eri_operator_trunc, eri_operator_lr_trunc, &
      65              :       manual_selection, mao_projection, no_solver, qiskit_solver, wannier_projection, &
      66              :       eri_poisson_analytic, eri_poisson_periodic, eri_poisson_mt, high_spin_roks
      67              :    USE input_section_types, ONLY: section_vals_get, section_vals_get_subs_vals, &
      68              :                                   section_vals_set_subs_vals, section_vals_type, &
      69              :                                   section_vals_val_get, &
      70              :                                   section_vals_val_set
      71              :    USE ISO_C_BINDING, ONLY: c_null_char
      72              :    USE kinds, ONLY: default_path_length, &
      73              :                     default_string_length, &
      74              :                     dp, &
      75              :                     int_8
      76              :    USE hfx_types, ONLY: hfx_create, hfx_release
      77              :    USE machine, ONLY: m_walltime, m_flush
      78              :    USE mathlib, ONLY: diamat_all
      79              :    USE mathconstants, ONLY: fourpi, twopi, pi, rootpi
      80              :    USE memory_utilities, ONLY: reallocate
      81              :    USE message_passing, ONLY: mp_comm_type, &
      82              :                               mp_para_env_type, &
      83              :                               mp_para_env_release
      84              :    USE mp2_gpw, ONLY: create_mat_munu, grep_rows_in_subgroups, build_dbcsr_from_rows
      85              :    USE mt_util, ONLY: MT0D
      86              :    USE parallel_gemm_api, ONLY: parallel_gemm
      87              :    USE particle_list_types, ONLY: particle_list_type
      88              :    USE particle_types, ONLY: particle_type
      89              :    USE periodic_table, ONLY: ptable
      90              :    USE physcon, ONLY: angstrom, bohr
      91              :    USE preconditioner_types, ONLY: preconditioner_type
      92              :    USE pw_env_methods, ONLY: pw_env_create, &
      93              :                              pw_env_rebuild
      94              :    USE pw_env_types, ONLY: pw_env_get, &
      95              :                            pw_env_release, &
      96              :                            pw_env_type
      97              :    USE pw_methods, ONLY: pw_integrate_function, &
      98              :                          pw_multiply, &
      99              :                          pw_multiply_with, &
     100              :                          pw_transfer, &
     101              :                          pw_zero, pw_integral_ab, pw_scale, &
     102              :                          pw_gauss_damp, pw_compl_gauss_damp
     103              :    USE pw_poisson_methods, ONLY: pw_poisson_rebuild, &
     104              :                                  pw_poisson_solve
     105              :    USE pw_poisson_types, ONLY: ANALYTIC0D, &
     106              :                                PERIODIC3D, &
     107              :                                greens_fn_type, &
     108              :                                pw_poisson_analytic, &
     109              :                                pw_poisson_periodic, &
     110              :                                pw_poisson_type
     111              :    USE pw_pool_types, ONLY: &
     112              :       pw_pool_type
     113              :    USE pw_types, ONLY: &
     114              :       pw_c1d_gs_type, &
     115              :       pw_r3d_rs_type
     116              :    USE qcschema, ONLY: qcschema_env_create, &
     117              :                        qcschema_env_release, &
     118              :                        qcschema_to_hdf5, &
     119              :                        qcschema_type
     120              :    USE qs_active_space_types, ONLY: active_space_type, &
     121              :                                     create_active_space_type, &
     122              :                                     csr_idx_from_combined, &
     123              :                                     csr_idx_to_combined, &
     124              :                                     eri_type, &
     125              :                                     eri_type_eri_element_func
     126              :    USE qs_active_space_utils, ONLY: eri_to_array, &
     127              :                                     subspace_matrix_to_array
     128              :    USE qs_collocate_density, ONLY: calculate_wavefunction
     129              :    USE qs_density_matrices, ONLY: calculate_density_matrix
     130              :    USE qs_energy_types, ONLY: qs_energy_type
     131              :    USE qs_environment_types, ONLY: get_qs_env, &
     132              :                                    qs_environment_type, &
     133              :                                    set_qs_env
     134              :    USE qs_integrate_potential, ONLY: integrate_v_rspace
     135              :    USE qs_kind_types, ONLY: qs_kind_type
     136              :    USE qs_ks_methods, ONLY: qs_ks_update_qs_env, qs_ks_build_kohn_sham_matrix, &
     137              :                             evaluate_core_matrix_traces
     138              :    USE qs_ks_types, ONLY: qs_ks_did_change, &
     139              :                           qs_ks_env_type, set_ks_env
     140              :    USE qs_mo_io, ONLY: write_mo_set_to_output_unit
     141              :    USE qs_mo_methods, ONLY: calculate_subspace_eigenvalues
     142              :    USE qs_mo_types, ONLY: allocate_mo_set, &
     143              :                           get_mo_set, &
     144              :                           init_mo_set, &
     145              :                           mo_set_type
     146              :    USE qs_neighbor_list_types, ONLY: neighbor_list_set_p_type, release_neighbor_list_sets
     147              :    USE qs_ot_eigensolver, ONLY: ot_eigensolver
     148              :    USE qs_rho_methods, ONLY: qs_rho_update_rho
     149              :    USE qs_rho_types, ONLY: qs_rho_get, &
     150              :                            qs_rho_type
     151              :    USE qs_subsys_types, ONLY: qs_subsys_get, &
     152              :                               qs_subsys_type
     153              :    USE qs_scf_post_scf, ONLY: qs_scf_compute_properties
     154              :    USE scf_control_types, ONLY: scf_control_type
     155              : #ifndef __NO_SOCKETS
     156              :    USE sockets_interface, ONLY: accept_socket, &
     157              :                                 close_socket, &
     158              :                                 listen_socket, &
     159              :                                 open_bind_socket, &
     160              :                                 readbuffer, &
     161              :                                 remove_socket_file, &
     162              :                                 writebuffer
     163              : #endif
     164              :    USE task_list_methods, ONLY: generate_qs_task_list
     165              :    USE task_list_types, ONLY: allocate_task_list, &
     166              :                               deallocate_task_list, &
     167              :                               task_list_type
     168              :    USE util, ONLY: get_limit
     169              : #include "./base/base_uses.f90"
     170              : 
     171              :    IMPLICIT NONE
     172              :    PRIVATE
     173              : 
     174              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_active_space_methods'
     175              : 
     176              :    PUBLIC :: active_space_main
     177              : 
     178              :    TYPE, EXTENDS(eri_type_eri_element_func) :: eri_fcidump_print
     179              :       INTEGER :: unit_nr = -1, bra_start = -1, ket_start = -1
     180              :    CONTAINS
     181              :       PROCEDURE :: func => eri_fcidump_print_func
     182              :    END TYPE eri_fcidump_print
     183              : 
     184              :    TYPE, EXTENDS(eri_type_eri_element_func) :: eri_fcidump_checksum
     185              :       INTEGER :: bra_start = 0, ket_start = 0
     186              :       REAL(KIND=dp) :: checksum = 0.0_dp
     187              :    CONTAINS
     188              :       PROCEDURE, PASS :: set => eri_fcidump_set
     189              :       PROCEDURE :: func => eri_fcidump_checksum_func
     190              :    END TYPE eri_fcidump_checksum
     191              : 
     192              : CONTAINS
     193              : 
     194              : ! **************************************************************************************************
     195              : !> \brief Sets the starting indices of the bra and ket.
     196              : !> \param this object reference
     197              : !> \param bra_start starting index of the bra
     198              : !> \param ket_start starting index of the ket
     199              : ! **************************************************************************************************
     200           96 :    SUBROUTINE eri_fcidump_set(this, bra_start, ket_start)
     201              :       CLASS(eri_fcidump_checksum) :: this
     202              :       INTEGER, INTENT(IN) :: bra_start, ket_start
     203           96 :       this%bra_start = bra_start
     204           96 :       this%ket_start = ket_start
     205           96 :    END SUBROUTINE eri_fcidump_set
     206              : 
     207              : ! **************************************************************************************************
     208              : !> \brief Main method for determining the active space Hamiltonian
     209              : !> \param qs_env ...
     210              : ! **************************************************************************************************
     211        22667 :    SUBROUTINE active_space_main(qs_env)
     212              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     213              : 
     214              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'active_space_main'
     215              : 
     216              :       CHARACTER(len=10)                                  :: cshell, lnam(5)
     217              :       CHARACTER(len=default_path_length)                 :: qcschema_filename
     218              :       CHARACTER(LEN=default_string_length)               :: basis_type
     219              :       INTEGER :: as_solver, eri_method, eri_operator, eri_print, group_size, handle, i, iatom, &
     220              :          ishell, isp, ispin, iw, j, jm, m, max_orb_ind, mselect, n1, n2, nao, natom, nel, &
     221              :          nelec_active, nelec_inactive, nelec_total, nmo, nmo_active, nmo_available, nmo_inactive, &
     222              :          nmo_inactive_remaining, nmo_occ, nmo_virtual, nn1, nn2, nrow_global, nspins
     223              :       INTEGER, DIMENSION(5)                              :: nshell
     224        22667 :       INTEGER, DIMENSION(:), POINTER                     :: invals
     225              :       LOGICAL                                            :: do_ddapc, do_kpoints, ex_omega, &
     226              :                                                             ex_operator, ex_perd, ex_rcut, &
     227              :                                                             explicit, stop_after_print, store_wfn
     228              :       REAL(KIND=dp) :: alpha, eri_eps_filter, eri_eps_grid, eri_eps_int, eri_gpw_cutoff, &
     229              :          eri_op_omega, eri_rcut, eri_rel_cutoff, fel, focc, maxocc, nze_percentage
     230        22667 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)        :: eigenvalues
     231        22667 :       REAL(KIND=dp), DIMENSION(:), POINTER               :: evals_virtual
     232              :       TYPE(active_space_type), POINTER                   :: active_space_env
     233        22667 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
     234              :       TYPE(cell_type), POINTER                           :: cell
     235              :       TYPE(cp_blacs_env_type), POINTER                   :: context
     236              :       TYPE(cp_fm_struct_type), POINTER                   :: fm_struct_tmp
     237              :       TYPE(cp_fm_type)                                   :: fm_dummy, mo_virtual
     238              :       TYPE(cp_fm_type), POINTER                          :: fm_target_active, fm_target_inactive, &
     239              :                                                             fmat, mo_coeff, mo_ref, mo_target
     240              :       TYPE(cp_logger_type), POINTER                      :: logger
     241              :       TYPE(dbcsr_csr_type), POINTER                      :: eri_mat
     242        45334 :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: ks_matrix, rho_ao, s_matrix
     243              :       TYPE(dbcsr_type), POINTER                          :: denmat
     244              :       TYPE(dft_control_type), POINTER                    :: dft_control
     245        22667 :       TYPE(mo_set_type), DIMENSION(:), POINTER           :: mos
     246              :       TYPE(mo_set_type), POINTER                         :: mo_set, mo_set_active, mo_set_inactive
     247              :       TYPE(mp_para_env_type), POINTER                    :: para_env
     248        22667 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     249              :       TYPE(preconditioner_type), POINTER                 :: local_preconditioner
     250        90668 :       TYPE(qcschema_type)                                :: qcschema_env
     251              :       TYPE(qs_energy_type), POINTER                      :: energy
     252        22667 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
     253              :       TYPE(qs_ks_env_type), POINTER                      :: ks_env
     254              :       TYPE(qs_rho_type), POINTER                         :: rho
     255              :       TYPE(scf_control_type), POINTER                    :: scf_control
     256              :       TYPE(section_vals_type), POINTER                   :: adiabatic_rescaling, as_input, &
     257              :                                                             hfx_section, input, loc_print, &
     258              :                                                             loc_section, print_orb, xc_section
     259              : 
     260              :       !--------------------------------------------------------------------------------------------!
     261              : 
     262        22667 :       CALL get_qs_env(qs_env, input=input)
     263        22667 :       as_input => section_vals_get_subs_vals(input, "DFT%ACTIVE_SPACE")
     264        22667 :       CALL section_vals_get(as_input, explicit=explicit)
     265        22667 :       IF (.NOT. explicit) RETURN
     266           72 :       CALL timeset(routineN, handle)
     267              : 
     268           72 :       logger => cp_get_default_logger()
     269           72 :       iw = cp_logger_get_default_io_unit(logger)
     270              : 
     271           72 :       IF (iw > 0) THEN
     272              :          WRITE (iw, '(/,T2,A)') &
     273           36 :             '!-----------------------------------------------------------------------------!'
     274           36 :          WRITE (iw, '(T26,A)') "Active Space Embedding Module"
     275              :          WRITE (iw, '(T2,A)') &
     276           36 :             '!-----------------------------------------------------------------------------!'
     277              :       END IF
     278              : 
     279              :       ! k-points?
     280           72 :       CALL get_qs_env(qs_env, do_kpoints=do_kpoints, dft_control=dft_control)
     281           72 :       IF (do_kpoints) THEN
     282            0 :          CALL cp_abort(__LOCATION__, "k-points not supported in active space module")
     283              :       END IF
     284              : 
     285              :       ! adiabatic rescaling?
     286           72 :       adiabatic_rescaling => section_vals_get_subs_vals(input, "DFT%XC%ADIABATIC_RESCALING")
     287           72 :       CALL section_vals_get(adiabatic_rescaling, explicit=explicit)
     288           72 :       IF (explicit) THEN
     289            0 :          CALL cp_abort(__LOCATION__, "Adiabatic rescaling not supported in active space module")
     290              :       END IF
     291              : 
     292              :       ! Setup the possible usage of DDAPC charges
     293              :       do_ddapc = dft_control%qs_control%ddapc_restraint .OR. &
     294              :                  qs_env%cp_ddapc_ewald%do_decoupling .OR. &
     295              :                  qs_env%cp_ddapc_ewald%do_qmmm_periodic_decpl .OR. &
     296           72 :                  qs_env%cp_ddapc_ewald%do_solvation
     297              :       IF (do_ddapc) THEN
     298            0 :          CALL cp_abort(__LOCATION__, "DDAPC charges are not supported in the active space module")
     299              :       END IF
     300           72 :       IF (dft_control%do_sccs) THEN
     301            0 :          CALL cp_abort(__LOCATION__, "SCCS is not supported in the active space module")
     302              :       END IF
     303           72 :       IF (dft_control%correct_surf_dip) THEN
     304            0 :          IF (dft_control%surf_dip_correct_switch) THEN
     305            0 :             CALL cp_abort(__LOCATION__, "Surface dipole correction not supported in the AS module")
     306              :          END IF
     307              :       END IF
     308           72 :       IF (dft_control%smeagol_control%smeagol_enabled) THEN
     309            0 :          CALL cp_abort(__LOCATION__, "SMEAGOL is not supported in the active space module")
     310              :       END IF
     311           72 :       IF (dft_control%qs_control%do_kg) THEN
     312            0 :          CALL cp_abort(__LOCATION__, "KG correction not supported in the active space module")
     313              :       END IF
     314              : 
     315           72 :       NULLIFY (active_space_env)
     316           72 :       CALL create_active_space_type(active_space_env)
     317           72 :       active_space_env%energy_total = 0.0_dp
     318           72 :       active_space_env%energy_ref = 0.0_dp
     319           72 :       active_space_env%energy_inactive = 0.0_dp
     320           72 :       active_space_env%energy_active = 0.0_dp
     321              : 
     322              :       ! input options
     323              : 
     324              :       ! figure out what needs to be printed/stored
     325           72 :       IF (BTEST(cp_print_key_should_output(logger%iter_info, as_input, "FCIDUMP"), cp_p_file)) THEN
     326           72 :          active_space_env%fcidump = .TRUE.
     327              :       END IF
     328              : 
     329           72 :       CALL section_vals_val_get(as_input, "QCSCHEMA", c_val=qcschema_filename, explicit=explicit)
     330           72 :       IF (explicit) THEN
     331            4 :          active_space_env%qcschema = .TRUE.
     332            4 :          active_space_env%qcschema_filename = qcschema_filename
     333              :       END IF
     334              : 
     335           72 :       CALL section_vals_val_get(as_input, "ACTIVE_ELECTRONS", i_val=nelec_active)
     336           72 :       CALL get_qs_env(qs_env, nelectron_total=nelec_total)
     337              : 
     338           72 :       IF (nelec_active <= 0) CPABORT("Specify a positive number of active electrons.")
     339           72 :       IF (nelec_active > nelec_total) CPABORT("More active electrons than total electrons.")
     340              : 
     341           72 :       nelec_inactive = nelec_total - nelec_active
     342           72 :       IF (MOD(nelec_inactive, 2) /= 0) THEN
     343            0 :          CPABORT("The remaining number of inactive electrons has to be even.")
     344              :       END IF
     345              : 
     346           72 :       CALL section_vals_val_get(as_input, "ALPHA", r_val=alpha)
     347           72 :       IF (alpha < 0.0 .OR. alpha > 1.0) CPABORT("Specify a damping factor between 0 and 1.")
     348           72 :       active_space_env%alpha = alpha
     349              : 
     350           72 :       IF (iw > 0) THEN
     351           36 :          WRITE (iw, '(T3,A,T70,I10)') "Total number of electrons", nelec_total
     352           36 :          WRITE (iw, '(T3,A,T70,I10)') "Number of inactive electrons", nelec_inactive
     353           36 :          WRITE (iw, '(T3,A,T70,I10)') "Number of active electrons", nelec_active
     354              :       END IF
     355              : 
     356           72 :       CALL get_qs_env(qs_env, dft_control=dft_control)
     357           72 :       nspins = dft_control%nspins
     358              : 
     359           72 :       active_space_env%nelec_active = nelec_active
     360           72 :       active_space_env%nelec_inactive = nelec_inactive
     361           72 :       active_space_env%nelec_total = nelec_total
     362           72 :       active_space_env%nspins = nspins
     363           72 :       active_space_env%multiplicity = dft_control%multiplicity
     364              : 
     365              :       ! define the active/inactive space orbitals
     366           72 :       CALL section_vals_val_get(as_input, "ACTIVE_ORBITALS", explicit=explicit, i_val=nmo_active)
     367           72 :       IF (.NOT. explicit) THEN
     368            0 :          CALL cp_abort(__LOCATION__, "Number of Active Orbitals has to be specified.")
     369              :       END IF
     370           72 :       active_space_env%nmo_active = nmo_active
     371              :       ! this is safe because nelec_inactive is always even
     372           72 :       nmo_inactive = nelec_inactive/2
     373           72 :       active_space_env%nmo_inactive = nmo_inactive
     374              : 
     375           72 :       CALL section_vals_val_get(as_input, "ORBITAL_SELECTION", i_val=mselect)
     376           72 :       IF (iw > 0) THEN
     377            0 :          SELECT CASE (mselect)
     378              :          CASE DEFAULT
     379            0 :             CPABORT("Unknown orbital selection method")
     380              :          CASE (casci_canonical)
     381              :             WRITE (iw, '(/,T3,A)') &
     382           30 :                "Active space orbitals selected using energy ordered canonical orbitals"
     383              :          CASE (wannier_projection)
     384              :             WRITE (iw, '(/,T3,A)') &
     385            0 :                "Active space orbitals selected using projected Wannier orbitals"
     386              :          CASE (mao_projection)
     387              :             WRITE (iw, '(/,T3,A)') &
     388            0 :                "Active space orbitals selected using modified atomic orbitals (MAO)"
     389              :          CASE (manual_selection)
     390              :             WRITE (iw, '(/,T3,A)') &
     391           36 :                "Active space orbitals selected manually"
     392              :          END SELECT
     393              : 
     394           36 :          WRITE (iw, '(T3,A,T70,I10)') "Number of inactive orbitals", nmo_inactive
     395           36 :          WRITE (iw, '(T3,A,T70,I10)') "Number of active orbitals", nmo_active
     396              :       END IF
     397              : 
     398              :       ! get projection spaces
     399           72 :       CALL section_vals_val_get(as_input, "SUBSPACE_ATOM", i_val=iatom, explicit=explicit)
     400           72 :       IF (explicit) THEN
     401            0 :          CALL get_qs_env(qs_env, natom=natom)
     402            0 :          IF (iatom <= 0 .OR. iatom > natom) THEN
     403            0 :             IF (iw > 0) THEN
     404            0 :                WRITE (iw, '(/,T3,A,I3)') "ERROR: SUBSPACE_ATOM number is not valid", iatom
     405              :             END IF
     406            0 :             CPABORT("Select a valid SUBSPACE_ATOM")
     407              :          END IF
     408              :       END IF
     409           72 :       CALL section_vals_val_get(as_input, "SUBSPACE_SHELL", c_val=cshell, explicit=explicit)
     410           72 :       nshell = 0
     411          432 :       lnam = ""
     412           72 :       IF (explicit) THEN
     413            0 :          cshell = ADJUSTL(cshell)
     414            0 :          n1 = 1
     415            0 :          DO i = 1, 5
     416            0 :             ishell = i
     417            0 :             IF (cshell(n1:n1) == " ") THEN
     418           72 :                ishell = ishell - 1
     419              :                EXIT
     420              :             END IF
     421            0 :             READ (cshell(n1:), "(I1,A1)") nshell(i), lnam(i)
     422            0 :             n1 = n1 + 2
     423              :          END DO
     424              :       END IF
     425              : 
     426              :       ! generate orbitals
     427            0 :       SELECT CASE (mselect)
     428              :       CASE DEFAULT
     429            0 :          CPABORT("Unknown orbital selection method")
     430              :       CASE (casci_canonical)
     431           60 :          CALL get_qs_env(qs_env, mos=mos)
     432              : 
     433              :          ! total number of occupied orbitals, i.e. inactive plus active MOs
     434           60 :          nmo_occ = nmo_inactive + nmo_active
     435              : 
     436              :          ! set inactive orbital indices, these are trivially 1...nmo_inactive
     437          192 :          ALLOCATE (active_space_env%inactive_orbitals(nmo_inactive, nspins))
     438          128 :          DO ispin = 1, nspins
     439          172 :             DO i = 1, nmo_inactive
     440          112 :                active_space_env%inactive_orbitals(i, ispin) = i
     441              :             END DO
     442              :          END DO
     443              : 
     444              :          ! set active orbital indices, these are shifted by nmo_inactive
     445          240 :          ALLOCATE (active_space_env%active_orbitals(nmo_active, nspins))
     446          128 :          DO ispin = 1, nspins
     447          322 :             DO i = 1, nmo_active
     448          262 :                active_space_env%active_orbitals(i, ispin) = nmo_inactive + i
     449              :             END DO
     450              :          END DO
     451              : 
     452              :          ! allocate and initialize inactive and active mo coefficients.
     453              :          ! These are stored in a data structure for the full occupied space:
     454              :          ! for inactive mos, the active subset is set to zero, vice versa for the active mos
     455              :          ! TODO: allocate data structures only for the eaxct number MOs
     456           60 :          maxocc = 2.0_dp
     457           60 :          IF (nspins > 1) maxocc = 1.0_dp
     458          248 :          ALLOCATE (active_space_env%mos_active(nspins))
     459          188 :          ALLOCATE (active_space_env%mos_inactive(nspins))
     460          128 :          DO ispin = 1, nspins
     461           68 :             CALL get_mo_set(mos(ispin), mo_coeff=mo_ref, nao=nao)
     462           68 :             CALL cp_fm_get_info(mo_ref, context=context, para_env=para_env, nrow_global=nrow_global)
     463              :             ! the right number of active electrons per spin channel is initialized further down
     464           68 :             CALL allocate_mo_set(active_space_env%mos_active(ispin), nao, nmo_occ, 0, 0.0_dp, maxocc, 0.0_dp)
     465              :             CALL cp_fm_struct_create(fm_struct_tmp, para_env=para_env, context=context, &
     466           68 :                                      nrow_global=nrow_global, ncol_global=nmo_occ)
     467           68 :             CALL init_mo_set(active_space_env%mos_active(ispin), fm_struct=fm_struct_tmp, name="Active Space MO")
     468           68 :             CALL cp_fm_struct_release(fm_struct_tmp)
     469           68 :             IF (nspins == 2) THEN
     470           16 :                nel = nelec_inactive/2
     471              :             ELSE
     472           52 :                nel = nelec_inactive
     473              :             END IF
     474              :             CALL allocate_mo_set(active_space_env%mos_inactive(ispin), nao, nmo_occ, nel, &
     475           68 :                                  REAL(nel, KIND=dp), maxocc, 0.0_dp)
     476              :             CALL cp_fm_struct_create(fm_struct_tmp, para_env=para_env, context=context, &
     477           68 :                                      nrow_global=nrow_global, ncol_global=nmo_occ)
     478           68 :             CALL init_mo_set(active_space_env%mos_inactive(ispin), fm_struct=fm_struct_tmp, name="Inactive Space MO")
     479          196 :             CALL cp_fm_struct_release(fm_struct_tmp)
     480              :          END DO
     481              : 
     482              :          ! create canonical orbitals
     483           60 :          CALL get_qs_env(qs_env, scf_control=scf_control)
     484           60 :          IF (dft_control%roks .AND. scf_control%roks_scheme /= high_spin_roks) THEN
     485            0 :             CPABORT("Unclear how we define MOs in the general restricted case ... stopping")
     486              :          ELSE
     487           60 :             IF (dft_control%do_admm) THEN
     488            0 :                IF (dft_control%do_admm_mo) THEN
     489            0 :                   CPABORT("ADMM currently possible only with purification none_dm")
     490              :                END IF
     491              :             END IF
     492              : 
     493          240 :             ALLOCATE (eigenvalues(nmo_occ, nspins))
     494          366 :             eigenvalues = 0.0_dp
     495           60 :             CALL get_qs_env(qs_env, matrix_ks=ks_matrix, matrix_s=s_matrix, scf_control=scf_control)
     496              : 
     497              :             ! calculate virtual MOs and copy inactive and active orbitals
     498           60 :             IF (iw > 0) THEN
     499           30 :                WRITE (iw, '(/,T3,A)') "Calculating virtual MOs..."
     500              :             END IF
     501          128 :             DO ispin = 1, nspins
     502              :                ! nmo_available is the number of MOs available from the SCF calculation:
     503              :                ! this is at least the number of occupied orbitals in the SCF, plus
     504              :                ! any number of added MOs (virtuals) requested in the SCF section
     505           68 :                CALL get_mo_set(mos(ispin), mo_coeff=mo_ref, nmo=nmo_available)
     506              : 
     507              :                ! calculate how many extra MOs we still have to compute
     508           68 :                nmo_virtual = nmo_occ - nmo_available
     509           68 :                nmo_virtual = MAX(nmo_virtual, 0)
     510              : 
     511              :                NULLIFY (evals_virtual)
     512          136 :                ALLOCATE (evals_virtual(nmo_virtual))
     513              : 
     514              :                CALL cp_fm_get_info(mo_ref, context=context, para_env=para_env, &
     515           68 :                                    nrow_global=nrow_global)
     516              : 
     517              :                CALL cp_fm_struct_create(fm_struct_tmp, para_env=para_env, context=context, &
     518           68 :                                         nrow_global=nrow_global, ncol_global=nmo_virtual)
     519           68 :                CALL cp_fm_create(mo_virtual, fm_struct_tmp, name="virtual")
     520           68 :                CALL cp_fm_struct_release(fm_struct_tmp)
     521           68 :                CALL cp_fm_init_random(mo_virtual, nmo_virtual)
     522              : 
     523           68 :                NULLIFY (local_preconditioner)
     524              : 
     525              :                ! compute missing virtual MOs
     526              :                CALL ot_eigensolver(matrix_h=ks_matrix(ispin)%matrix, matrix_s=s_matrix(1)%matrix, &
     527              :                                    matrix_c_fm=mo_virtual, matrix_orthogonal_space_fm=mo_ref, &
     528              :                                    eps_gradient=scf_control%eps_lumos, &
     529              :                                    preconditioner=local_preconditioner, &
     530              :                                    iter_max=scf_control%max_iter_lumos, &
     531           68 :                                    size_ortho_space=nmo_available)
     532              : 
     533              :                ! get the eigenvalues
     534           68 :                CALL calculate_subspace_eigenvalues(mo_virtual, ks_matrix(ispin)%matrix, evals_virtual)
     535              : 
     536              :                ! we need to send the copy of MOs to preserve the sign
     537           68 :                CALL cp_fm_create(fm_dummy, mo_ref%matrix_struct)
     538           68 :                CALL cp_fm_to_fm(mo_ref, fm_dummy)
     539              :                CALL calculate_subspace_eigenvalues(fm_dummy, ks_matrix(ispin)%matrix, &
     540           68 :                                                    evals_arg=eigenvalues(:, ispin), do_rotation=.TRUE.)
     541              : 
     542              :                ! copy inactive orbitals
     543           68 :                mo_set => active_space_env%mos_inactive(ispin)
     544           68 :                CALL get_mo_set(mo_set, mo_coeff=mo_target)
     545          112 :                DO i = 1, SIZE(active_space_env%inactive_orbitals, 1)
     546           44 :                   m = active_space_env%inactive_orbitals(i, ispin)
     547           44 :                   CALL cp_fm_to_fm(mo_ref, mo_target, 1, m, m)
     548           44 :                   mo_set%eigenvalues(m) = eigenvalues(m, ispin)
     549          112 :                   IF (nspins > 1) THEN
     550           28 :                      mo_set%occupation_numbers(m) = 1.0
     551              :                   ELSE
     552           16 :                      mo_set%occupation_numbers(m) = 2.0
     553              :                   END IF
     554              :                END DO
     555              : 
     556              :                ! copy active orbitals
     557           68 :                mo_set => active_space_env%mos_active(ispin)
     558           68 :                CALL get_mo_set(mo_set, mo_coeff=mo_target)
     559              :                ! for mult > 1, put the polarized electrons in the alpha channel
     560           68 :                IF (nspins == 2) THEN
     561           16 :                   IF (ispin == 1) THEN
     562            8 :                      nel = (nelec_active + active_space_env%multiplicity - 1)/2
     563              :                   ELSE
     564            8 :                      nel = (nelec_active - active_space_env%multiplicity + 1)/2
     565              :                   END IF
     566              :                ELSE
     567           52 :                   nel = nelec_active
     568              :                END IF
     569           68 :                mo_set%nelectron = nel
     570           68 :                mo_set%n_el_f = REAL(nel, KIND=dp)
     571          262 :                DO i = 1, nmo_active
     572          194 :                   m = active_space_env%active_orbitals(i, ispin)
     573          194 :                   IF (m > nmo_available) THEN
     574            0 :                      CALL cp_fm_to_fm(mo_virtual, mo_target, 1, m - nmo_available, m)
     575            0 :                      eigenvalues(m, ispin) = evals_virtual(m - nmo_available)
     576            0 :                      mo_set%occupation_numbers(m) = 0.0
     577              :                   ELSE
     578          194 :                      CALL cp_fm_to_fm(mo_ref, mo_target, 1, m, m)
     579          194 :                      mo_set%occupation_numbers(m) = mos(ispin)%occupation_numbers(m)
     580              :                   END IF
     581          262 :                   mo_set%eigenvalues(m) = eigenvalues(m, ispin)
     582              :                END DO
     583              :                ! Release
     584           68 :                DEALLOCATE (evals_virtual)
     585           68 :                CALL cp_fm_release(fm_dummy)
     586          400 :                CALL cp_fm_release(mo_virtual)
     587              :             END DO
     588              : 
     589           60 :             IF (iw > 0) THEN
     590           64 :                DO ispin = 1, nspins
     591           34 :                   WRITE (iw, '(/,T3,A,I3,T66,A)') "Canonical Orbital Selection for spin", ispin, &
     592           68 :                      "[atomic units]"
     593           43 :                   DO i = 1, nmo_inactive, 4
     594            9 :                      jm = MIN(3, nmo_inactive - i)
     595           65 :                      WRITE (iw, '(T3,4(F14.6,A5))') (eigenvalues(i + j, ispin), " [I]", j=0, jm)
     596              :                   END DO
     597           70 :                   DO i = nmo_inactive + 1, nmo_inactive + nmo_active, 4
     598           36 :                      jm = MIN(3, nmo_inactive + nmo_active - i)
     599          167 :                      WRITE (iw, '(T3,4(F14.6,A5))') (eigenvalues(i + j, ispin), " [A]", j=0, jm)
     600              :                   END DO
     601           34 :                   WRITE (iw, '(/,T3,A,I3)') "Active Orbital Indices for spin", ispin
     602          100 :                   DO i = 1, SIZE(active_space_env%active_orbitals, 1), 4
     603           36 :                      jm = MIN(3, SIZE(active_space_env%active_orbitals, 1) - i)
     604          167 :                      WRITE (iw, '(T3,4(I4))') (active_space_env%active_orbitals(i + j, ispin), j=0, jm)
     605              :                   END DO
     606              :                END DO
     607              :             END IF
     608           60 :             DEALLOCATE (eigenvalues)
     609              :          END IF
     610              : 
     611              :       CASE (manual_selection)
     612              :          ! create canonical orbitals
     613           12 :          IF (dft_control%roks) THEN
     614            0 :             CPABORT("Unclear how we define MOs in the restricted case ... stopping")
     615              :          ELSE
     616           12 :             IF (dft_control%do_admm) THEN
     617              :                ! For admm_mo, the auxiliary density is computed from the MOs, which never change
     618              :                ! in the rs-dft embedding, therefore the energy is wrong as the LR HFX never changes.
     619              :                ! For admm_dm, the auxiliary density is computed from the density matrix, which is
     620              :                ! updated at each iteration and therefore works.
     621            0 :                IF (dft_control%do_admm_mo) THEN
     622            0 :                   CPABORT("ADMM currently possible only with purification none_dm")
     623              :                END IF
     624              :             END IF
     625              : 
     626           12 :             CALL section_vals_val_get(as_input, "ACTIVE_ORBITAL_INDICES", explicit=explicit, i_vals=invals)
     627           12 :             IF (.NOT. explicit) THEN
     628              :                CALL cp_abort(__LOCATION__, "Manual orbital selection requires to explicitly "// &
     629            0 :                              "set the active orbital indices via ACTIVE_ORBITAL_INDICES")
     630              :             END IF
     631              : 
     632           12 :             IF (nspins == 1) THEN
     633            6 :                CPASSERT(SIZE(invals) == nmo_active)
     634              :             ELSE
     635            6 :                CPASSERT(SIZE(invals) == 2*nmo_active)
     636              :             END IF
     637           36 :             ALLOCATE (active_space_env%inactive_orbitals(nmo_inactive, nspins))
     638           48 :             ALLOCATE (active_space_env%active_orbitals(nmo_active, nspins))
     639              : 
     640           30 :             DO ispin = 1, nspins
     641           66 :                DO i = 1, nmo_active
     642           54 :                   active_space_env%active_orbitals(i, ispin) = invals(i + (ispin - 1)*nmo_active)
     643              :                END DO
     644              :             END DO
     645              : 
     646           12 :             CALL get_qs_env(qs_env, mos=mos)
     647              : 
     648              :             ! include MOs up to the largest index in the list
     649           48 :             max_orb_ind = MAXVAL(invals)
     650           12 :             maxocc = 2.0_dp
     651           12 :             IF (nspins > 1) maxocc = 1.0_dp
     652           54 :             ALLOCATE (active_space_env%mos_active(nspins))
     653           42 :             ALLOCATE (active_space_env%mos_inactive(nspins))
     654           30 :             DO ispin = 1, nspins
     655              :                ! init active orbitals
     656           18 :                CALL get_mo_set(mos(ispin), mo_coeff=mo_ref, nao=nao)
     657           18 :                CALL cp_fm_get_info(mo_ref, context=context, para_env=para_env, nrow_global=nrow_global)
     658           18 :                CALL allocate_mo_set(active_space_env%mos_active(ispin), nao, max_orb_ind, 0, 0.0_dp, maxocc, 0.0_dp)
     659              :                CALL cp_fm_struct_create(fm_struct_tmp, para_env=para_env, context=context, &
     660           18 :                                         nrow_global=nrow_global, ncol_global=max_orb_ind)
     661           18 :                CALL init_mo_set(active_space_env%mos_active(ispin), fm_struct=fm_struct_tmp, name="Active Space MO")
     662           18 :                CALL cp_fm_struct_release(fm_struct_tmp)
     663              : 
     664              :                ! init inactive orbitals
     665           18 :                IF (nspins == 2) THEN
     666           12 :                   nel = nelec_inactive/2
     667              :                ELSE
     668            6 :                   nel = nelec_inactive
     669              :                END IF
     670           18 :                CALL allocate_mo_set(active_space_env%mos_inactive(ispin), nao, max_orb_ind, nel, REAL(nel, KIND=dp), maxocc, 0.0_dp)
     671              :                CALL cp_fm_struct_create(fm_struct_tmp, para_env=para_env, context=context, &
     672           18 :                                         nrow_global=nrow_global, ncol_global=max_orb_ind)
     673           18 :                CALL init_mo_set(active_space_env%mos_inactive(ispin), fm_struct=fm_struct_tmp, name="Inactive Space MO")
     674              :                ! small hack: set the correct inactive occupations down below
     675           68 :                active_space_env%mos_inactive(ispin)%occupation_numbers = 0.0_dp
     676           48 :                CALL cp_fm_struct_release(fm_struct_tmp)
     677              :             END DO
     678              : 
     679           48 :             ALLOCATE (eigenvalues(max_orb_ind, nspins))
     680           80 :             eigenvalues = 0.0_dp
     681           12 :             CALL get_qs_env(qs_env, matrix_ks=ks_matrix, matrix_s=s_matrix, scf_control=scf_control)
     682              : 
     683              :             ! calculate virtual MOs and copy inactive and active orbitals
     684           12 :             IF (iw > 0) THEN
     685            6 :                WRITE (iw, '(/,T3,A)') "Calculating virtual MOs..."
     686              :             END IF
     687           30 :             DO ispin = 1, nspins
     688           18 :                CALL get_mo_set(mos(ispin), mo_coeff=mo_ref, nmo=nmo_available)
     689           18 :                nmo_virtual = max_orb_ind - nmo_available
     690           18 :                nmo_virtual = MAX(nmo_virtual, 0)
     691              : 
     692              :                NULLIFY (evals_virtual)
     693           36 :                ALLOCATE (evals_virtual(nmo_virtual))
     694              : 
     695              :                CALL cp_fm_get_info(mo_ref, context=context, para_env=para_env, &
     696           18 :                                    nrow_global=nrow_global)
     697              : 
     698              :                CALL cp_fm_struct_create(fm_struct_tmp, para_env=para_env, context=context, &
     699           18 :                                         nrow_global=nrow_global, ncol_global=nmo_virtual)
     700           18 :                CALL cp_fm_create(mo_virtual, fm_struct_tmp, name="virtual")
     701           18 :                CALL cp_fm_struct_release(fm_struct_tmp)
     702           18 :                CALL cp_fm_init_random(mo_virtual, nmo_virtual)
     703              : 
     704           18 :                NULLIFY (local_preconditioner)
     705              : 
     706              :                CALL ot_eigensolver(matrix_h=ks_matrix(ispin)%matrix, matrix_s=s_matrix(1)%matrix, &
     707              :                                    matrix_c_fm=mo_virtual, matrix_orthogonal_space_fm=mo_ref, &
     708              :                                    eps_gradient=scf_control%eps_lumos, &
     709              :                                    preconditioner=local_preconditioner, &
     710              :                                    iter_max=scf_control%max_iter_lumos, &
     711           18 :                                    size_ortho_space=nmo_available)
     712              : 
     713              :                CALL calculate_subspace_eigenvalues(mo_virtual, ks_matrix(ispin)%matrix, &
     714           18 :                                                    evals_virtual)
     715              : 
     716              :                ! We need to send the copy of MOs to preserve the sign
     717           18 :                CALL cp_fm_create(fm_dummy, mo_ref%matrix_struct)
     718           18 :                CALL cp_fm_to_fm(mo_ref, fm_dummy)
     719              : 
     720              :                CALL calculate_subspace_eigenvalues(fm_dummy, ks_matrix(ispin)%matrix, &
     721           18 :                                                    evals_arg=eigenvalues(:, ispin), do_rotation=.TRUE.)
     722              : 
     723           18 :                mo_set_active => active_space_env%mos_active(ispin)
     724           18 :                CALL get_mo_set(mo_set_active, mo_coeff=fm_target_active)
     725           18 :                mo_set_inactive => active_space_env%mos_inactive(ispin)
     726           18 :                CALL get_mo_set(mo_set_inactive, mo_coeff=fm_target_inactive)
     727              : 
     728              :                ! copy orbitals
     729           18 :                nmo_inactive_remaining = nmo_inactive
     730           68 :                DO i = 1, max_orb_ind
     731              :                   ! case for i being an active orbital
     732          114 :                   IF (ANY(active_space_env%active_orbitals(:, ispin) == i)) THEN
     733           36 :                      IF (i > nmo_available) THEN
     734            0 :                         CALL cp_fm_to_fm(mo_virtual, fm_target_active, 1, i - nmo_available, i)
     735            0 :                         eigenvalues(i, ispin) = evals_virtual(i - nmo_available)
     736            0 :                         mo_set_active%occupation_numbers(i) = 0.0
     737              :                      ELSE
     738           36 :                         CALL cp_fm_to_fm(fm_dummy, fm_target_active, 1, i, i)
     739           36 :                         mo_set_active%occupation_numbers(i) = mos(ispin)%occupation_numbers(i)
     740              :                      END IF
     741           36 :                      mo_set_active%eigenvalues(i) = eigenvalues(i, ispin)
     742              :                      ! if it was not an active orbital, check whether it is an inactive orbital
     743           14 :                   ELSEIF (nmo_inactive_remaining > 0) THEN
     744            0 :                      CALL cp_fm_to_fm(fm_dummy, fm_target_inactive, 1, i, i)
     745              :                      ! store on the fly the mapping of inactive orbitals
     746            0 :                      active_space_env%inactive_orbitals(nmo_inactive - nmo_inactive_remaining + 1, ispin) = i
     747            0 :                      mo_set_inactive%eigenvalues(i) = eigenvalues(i, ispin)
     748            0 :                      mo_set_inactive%occupation_numbers(i) = mos(ispin)%occupation_numbers(i)
     749              :                      ! hack: set homo and lumo manually
     750            0 :                      IF (nmo_inactive_remaining == 1) THEN
     751            0 :                         mo_set_inactive%homo = i
     752            0 :                         mo_set_inactive%lfomo = i + 1
     753              :                      END IF
     754            0 :                      nmo_inactive_remaining = nmo_inactive_remaining - 1
     755              :                   ELSE
     756           14 :                      CYCLE
     757              :                   END IF
     758              :                END DO
     759              : 
     760              :                ! Release
     761           18 :                DEALLOCATE (evals_virtual)
     762           18 :                CALL cp_fm_release(fm_dummy)
     763          102 :                CALL cp_fm_release(mo_virtual)
     764              :             END DO
     765              : 
     766           12 :             IF (iw > 0) THEN
     767           15 :                DO ispin = 1, nspins
     768            9 :                   WRITE (iw, '(/,T3,A,I3,T66,A)') "Orbital Energies and Selection for spin", ispin, "[atomic units]"
     769              : 
     770           18 :                   DO i = 1, max_orb_ind, 4
     771            9 :                      jm = MIN(3, max_orb_ind - i)
     772            9 :                      WRITE (iw, '(T4)', advance="no")
     773           34 :                      DO j = 0, jm
     774           57 :                         IF (ANY(active_space_env%active_orbitals(:, ispin) == i + j)) THEN
     775           18 :                            WRITE (iw, '(T3,F12.6,A5)', advance="no") eigenvalues(i + j, ispin), " [A]"
     776            7 :                         ELSEIF (ANY(active_space_env%inactive_orbitals(:, ispin) == i + j)) THEN
     777            0 :                            WRITE (iw, '(T3,F12.6,A5)', advance="no") eigenvalues(i + j, ispin), " [I]"
     778              :                         ELSE
     779            7 :                            WRITE (iw, '(T3,F12.6,A5)', advance="no") eigenvalues(i + j, ispin), " [V]"
     780              :                         END IF
     781              :                      END DO
     782           18 :                      WRITE (iw, *)
     783              :                   END DO
     784            9 :                   WRITE (iw, '(/,T3,A,I3)') "Active Orbital Indices for spin", ispin
     785           24 :                   DO i = 1, SIZE(active_space_env%active_orbitals, 1), 4
     786            9 :                      jm = MIN(3, SIZE(active_space_env%active_orbitals, 1) - i)
     787           36 :                      WRITE (iw, '(T3,4(I4))') (active_space_env%active_orbitals(i + j, ispin), j=0, jm)
     788              :                   END DO
     789              :                END DO
     790              :             END IF
     791           24 :             DEALLOCATE (eigenvalues)
     792              :          END IF
     793              : 
     794              :       CASE (wannier_projection)
     795            0 :          NULLIFY (loc_section, loc_print)
     796            0 :          loc_section => section_vals_get_subs_vals(as_input, "LOCALIZE")
     797            0 :          CPASSERT(ASSOCIATED(loc_section))
     798            0 :          loc_print => section_vals_get_subs_vals(as_input, "LOCALIZE%PRINT")
     799              :          !
     800            0 :          CPABORT("not yet available")
     801              :          !
     802              :       CASE (mao_projection)
     803              :          !
     804           72 :          CPABORT("not yet available")
     805              :          !
     806              :       END SELECT
     807              : 
     808              :       ! Print orbitals on Cube files
     809           72 :       print_orb => section_vals_get_subs_vals(as_input, "PRINT_ORBITAL_CUBES")
     810           72 :       CALL section_vals_get(print_orb, explicit=explicit)
     811           72 :       CALL section_vals_val_get(print_orb, "STOP_AFTER_CUBES", l_val=stop_after_print)
     812           72 :       IF (explicit) THEN
     813              :          !
     814            4 :          CALL print_orbital_cubes(print_orb, qs_env, active_space_env%mos_active)
     815              :          !
     816            4 :          IF (stop_after_print) THEN
     817              : 
     818            0 :             IF (iw > 0) THEN
     819              :                WRITE (iw, '(/,T2,A)') &
     820            0 :                   '!----------------- Early End of Active Space Interface -----------------------!'
     821              :             END IF
     822              : 
     823            0 :             CALL timestop(handle)
     824              : 
     825            0 :             RETURN
     826              :          END IF
     827              :       END IF
     828              : 
     829              :       ! calculate inactive density matrix
     830           72 :       CALL get_qs_env(qs_env, rho=rho)
     831           72 :       CALL qs_rho_get(rho, rho_ao=rho_ao)
     832           72 :       CPASSERT(ASSOCIATED(rho_ao))
     833           72 :       CALL dbcsr_allocate_matrix_set(active_space_env%pmat_inactive, nspins)
     834          158 :       DO ispin = 1, nspins
     835           86 :          ALLOCATE (denmat)
     836           86 :          CALL dbcsr_copy(denmat, rho_ao(ispin)%matrix)
     837           86 :          mo_set => active_space_env%mos_inactive(ispin)
     838           86 :          CALL calculate_density_matrix(mo_set, denmat)
     839          158 :          active_space_env%pmat_inactive(ispin)%matrix => denmat
     840              :       END DO
     841              : 
     842              :       ! read in ERI parameters
     843           72 :       CALL section_vals_val_get(as_input, "ERI%METHOD", i_val=eri_method)
     844           72 :       active_space_env%eri%method = eri_method
     845           72 :       CALL section_vals_val_get(as_input, "ERI%OPERATOR", i_val=eri_operator, explicit=ex_operator)
     846           72 :       active_space_env%eri%operator = eri_operator
     847           72 :       CALL section_vals_val_get(as_input, "ERI%OMEGA", r_val=eri_op_omega, explicit=ex_omega)
     848           72 :       active_space_env%eri%omega = eri_op_omega
     849           72 :       CALL section_vals_val_get(as_input, "ERI%CUTOFF_RADIUS", r_val=eri_rcut, explicit=ex_rcut)
     850           72 :       active_space_env%eri%cutoff_radius = eri_rcut  ! this is already converted to bohr!
     851           72 :       CALL section_vals_val_get(as_input, "ERI%PERIODICITY", i_vals=invals, explicit=ex_perd)
     852           72 :       CALL section_vals_val_get(as_input, "ERI%EPS_INTEGRAL", r_val=eri_eps_int)
     853           72 :       active_space_env%eri%eps_integral = eri_eps_int
     854              :       ! if eri periodicity is explicitly set, we use it, otherwise we use the cell periodicity
     855           72 :       IF (ex_perd) THEN
     856           64 :          IF (SIZE(invals) == 1) THEN
     857            0 :             active_space_env%eri%periodicity(1:3) = invals(1)
     858              :          ELSE
     859          448 :             active_space_env%eri%periodicity(1:3) = invals(1:3)
     860              :          END IF
     861              :       ELSE
     862            8 :          CALL get_qs_env(qs_env, cell=cell)
     863           56 :          active_space_env%eri%periodicity(1:3) = cell%perd(1:3)
     864              :       END IF
     865           72 :       IF (iw > 0) THEN
     866           36 :          WRITE (iw, '(/,T3,A)') "Calculation of Electron Repulsion Integrals"
     867              : 
     868           29 :          SELECT CASE (eri_method)
     869              :          CASE (eri_method_full_gpw)
     870           29 :             WRITE (iw, '(T3,A,T50,A)') "Integration method", "GPW Fourier transform over MOs"
     871              :          CASE (eri_method_gpw_ht)
     872            7 :             WRITE (iw, '(T3,A,T44,A)') "Integration method", "Half transformed integrals from GPW"
     873              :          CASE DEFAULT
     874           36 :             CPABORT("Unknown ERI method")
     875              :          END SELECT
     876              : 
     877           27 :          SELECT CASE (eri_operator)
     878              :          CASE (eri_operator_coulomb)
     879           27 :             WRITE (iw, '(T3,A,T73,A)') "ERI operator", "Coulomb"
     880              : 
     881              :          CASE (eri_operator_yukawa)
     882            0 :             WRITE (iw, '(T3,A,T74,A)') "ERI operator", "Yukawa"
     883            0 :             IF (.NOT. ex_omega) CALL cp_abort(__LOCATION__, &
     884            0 :                                               "Yukawa operator requires OMEGA to be explicitly set")
     885            0 :             WRITE (iw, '(T3,A,T66,F14.3)') "ERI operator parameter OMEGA", eri_op_omega
     886              : 
     887              :          CASE (eri_operator_erf)
     888            7 :             WRITE (iw, '(T3,A,T63,A)') "ERI operator", "Longrange Coulomb"
     889            7 :             IF (.NOT. ex_omega) CALL cp_abort(__LOCATION__, &
     890            0 :                                               "Longrange operator requires OMEGA to be explicitly set")
     891            7 :             WRITE (iw, '(T3,A,T66,F14.3)') "ERI operator parameter OMEGA", eri_op_omega
     892              : 
     893              :          CASE (eri_operator_erfc)
     894            0 :             WRITE (iw, '(T3,A,T62,A)') "ERI operator", "Shortrange Coulomb"
     895            0 :             IF (.NOT. ex_omega) CALL cp_abort(__LOCATION__, &
     896            0 :                                               "Shortrange operator requires OMEGA to be explicitly set")
     897            0 :             WRITE (iw, '(T3,A,T66,F14.3)') "ERI operator parameter OMEGA", eri_op_omega
     898              : 
     899              :          CASE (eri_operator_trunc)
     900            0 :             WRITE (iw, '(T3,A,T63,A)') "ERI operator", "Truncated Coulomb"
     901            0 :             IF (.NOT. ex_rcut) CALL cp_abort(__LOCATION__, &
     902            0 :                                              "Cutoff radius not specified for trunc. Coulomb operator")
     903            0 :             WRITE (iw, '(T3,A,T66,F14.3)') "ERI operator cutoff radius (au)", eri_rcut
     904              : 
     905              :          CASE (eri_operator_lr_trunc)
     906            2 :             WRITE (iw, '(T3,A,T53,A)') "ERI operator", "Longrange truncated Coulomb"
     907            2 :             IF (.NOT. ex_rcut) CALL cp_abort(__LOCATION__, &
     908            0 :                                              "Cutoff radius not specified for trunc. longrange operator")
     909            2 :             WRITE (iw, '(T3,A,T66,F14.3)') "ERI operator cutoff radius (au)", eri_rcut
     910            2 :             IF (.NOT. ex_omega) CALL cp_abort(__LOCATION__, &
     911            0 :                                               "LR truncated operator requires OMEGA to be explicitly set")
     912            2 :             WRITE (iw, '(T3,A,T66,F14.3)') "ERI operator parameter OMEGA", eri_op_omega
     913            2 :             IF (eri_op_omega < 0.01_dp) THEN
     914            0 :                CPABORT("LR truncated operator requires OMEGA >= 0.01 to be stable")
     915              :             END IF
     916              : 
     917              :          CASE DEFAULT
     918           36 :             CPABORT("Unknown ERI operator")
     919              : 
     920              :          END SELECT
     921              : 
     922           36 :          WRITE (iw, '(T3,A,T68,E12.4)') "Accuracy of ERIs", eri_eps_int
     923           36 :          WRITE (iw, '(T3,A,T71,3I3)') "Periodicity", active_space_env%eri%periodicity(1:3)
     924              : 
     925              :          ! TODO: should be moved after ERI calculation, as it depends on screening
     926           36 :          IF (nspins < 2) THEN
     927           29 :             WRITE (iw, '(T3,A,T68,I12)') "Total Number of ERI", (nmo_active**4)/8
     928              :          ELSE
     929            7 :             WRITE (iw, '(T3,A,T68,I12)') "Total Number of ERI (aa|aa)", (nmo_active**4)/8
     930            7 :             WRITE (iw, '(T3,A,T68,I12)') "Total Number of ERI (bb|bb)", (nmo_active**4)/8
     931            7 :             WRITE (iw, '(T3,A,T68,I12)') "Total Number of ERI (aa|bb)", (nmo_active**4)/4
     932              :          END IF
     933              :       END IF
     934              : 
     935              :       ! allocate container for integrals (CSR matrix)
     936           72 :       CALL get_qs_env(qs_env, para_env=para_env)
     937           72 :       m = (nspins*(nspins + 1))/2
     938              :       ! With ROHF/ROKS, we need ERIs from only a single set of orbitals
     939           72 :       IF (dft_control%roks) m = 1
     940          312 :       ALLOCATE (active_space_env%eri%eri(m))
     941          168 :       DO i = 1, m
     942           96 :          CALL get_mo_set(active_space_env%mos_active(1), nmo=nmo)
     943           96 :          ALLOCATE (active_space_env%eri%eri(i)%csr_mat)
     944           96 :          eri_mat => active_space_env%eri%eri(i)%csr_mat
     945           96 :          IF (i == 1) THEN
     946           72 :             n1 = nmo
     947           72 :             n2 = nmo
     948           24 :          ELSEIF (i == 2) THEN
     949           12 :             n1 = nmo
     950           12 :             n2 = nmo
     951              :          ELSE
     952           12 :             n1 = nmo
     953           12 :             n2 = nmo
     954              :          END IF
     955           96 :          nn1 = (n1*(n1 + 1))/2
     956           96 :          nn2 = (n2*(n2 + 1))/2
     957           96 :          CALL dbcsr_csr_create(eri_mat, nn1, nn2, 0_int_8, 0, 0, para_env%get_handle())
     958          264 :          active_space_env%eri%norb = nmo
     959              :       END DO
     960              : 
     961           72 :       SELECT CASE (eri_method)
     962              :       CASE (eri_method_full_gpw, eri_method_gpw_ht)
     963           72 :          CALL section_vals_val_get(as_input, "ERI_GPW%EPS_GRID", r_val=eri_eps_grid)
     964           72 :          active_space_env%eri%eri_gpw%eps_grid = eri_eps_grid
     965           72 :          CALL section_vals_val_get(as_input, "ERI_GPW%EPS_FILTER", r_val=eri_eps_filter)
     966           72 :          active_space_env%eri%eri_gpw%eps_filter = eri_eps_filter
     967           72 :          CALL section_vals_val_get(as_input, "ERI_GPW%CUTOFF", r_val=eri_gpw_cutoff)
     968           72 :          active_space_env%eri%eri_gpw%cutoff = eri_gpw_cutoff
     969           72 :          CALL section_vals_val_get(as_input, "ERI_GPW%REL_CUTOFF", r_val=eri_rel_cutoff)
     970           72 :          active_space_env%eri%eri_gpw%rel_cutoff = eri_rel_cutoff
     971           72 :          CALL section_vals_val_get(as_input, "ERI_GPW%PRINT_LEVEL", i_val=eri_print)
     972           72 :          active_space_env%eri%eri_gpw%print_level = eri_print
     973           72 :          CALL section_vals_val_get(as_input, "ERI_GPW%STORE_WFN", l_val=store_wfn)
     974           72 :          active_space_env%eri%eri_gpw%store_wfn = store_wfn
     975           72 :          CALL section_vals_val_get(as_input, "ERI_GPW%GROUP_SIZE", i_val=group_size)
     976           72 :          active_space_env%eri%eri_gpw%group_size = group_size
     977              :          ! Always redo Poisson solver for now
     978           72 :          active_space_env%eri%eri_gpw%redo_poisson = .TRUE.
     979              :          ! active_space_env%eri%eri_gpw%redo_poisson = (ex_operator .OR. ex_perd)
     980           72 :          IF (iw > 0) THEN
     981           36 :             WRITE (iw, '(/,T2,A,T71,F10.1)') "ERI_GPW| Energy cutoff [Ry]", eri_gpw_cutoff
     982           36 :             WRITE (iw, '(T2,A,T71,F10.1)') "ERI_GPW| Relative energy cutoff [Ry]", eri_rel_cutoff
     983              :          END IF
     984              :          !
     985              :          CALL calculate_eri_gpw(active_space_env%mos_active, active_space_env%active_orbitals, active_space_env%eri, qs_env, iw, &
     986           72 :                                 dft_control%roks)
     987              :          !
     988              :       CASE DEFAULT
     989           72 :          CPABORT("Unknown ERI method")
     990              :       END SELECT
     991           72 :       IF (iw > 0) THEN
     992           84 :          DO isp = 1, SIZE(active_space_env%eri%eri)
     993           48 :             eri_mat => active_space_env%eri%eri(isp)%csr_mat
     994              :             nze_percentage = 100.0_dp*(REAL(eri_mat%nze_total, KIND=dp) &
     995           48 :                                        /REAL(eri_mat%nrows_total, KIND=dp))/REAL(eri_mat%ncols_total, KIND=dp)
     996           48 :             WRITE (iw, '(/,T2,A,I2,T30,A,T68,I12)') "ERI_GPW| Spinmatrix:", isp, &
     997           96 :                "Number of  CSR non-zero elements:", eri_mat%nze_total
     998           48 :             WRITE (iw, '(T2,A,I2,T30,A,T68,F12.4)') "ERI_GPW| Spinmatrix:", isp, &
     999           96 :                "Percentage CSR non-zero elements:", nze_percentage
    1000           48 :             WRITE (iw, '(T2,A,I2,T30,A,T68,I12)') "ERI_GPW| Spinmatrix:", isp, &
    1001           96 :                "nrows_total", eri_mat%nrows_total
    1002           48 :             WRITE (iw, '(T2,A,I2,T30,A,T68,I12)') "ERI_GPW| Spinmatrix:", isp, &
    1003           96 :                "ncols_total", eri_mat%ncols_total
    1004           48 :             WRITE (iw, '(T2,A,I2,T30,A,T68,I12)') "ERI_GPW| Spinmatrix:", isp, &
    1005          132 :                "nrows_local", eri_mat%nrows_local
    1006              :          END DO
    1007           36 :          CALL m_flush(iw)
    1008              :       END IF
    1009           72 :       CALL para_env%sync()
    1010              : 
    1011              :       ! set the reference active space density matrix
    1012           72 :       nspins = active_space_env%nspins
    1013          302 :       ALLOCATE (active_space_env%p_active(nspins))
    1014          158 :       DO isp = 1, nspins
    1015           86 :          mo_set => active_space_env%mos_active(isp)
    1016           86 :          CALL get_mo_set(mo_set, mo_coeff=mo_coeff, nmo=nmo)
    1017          158 :          CALL create_subspace_matrix(mo_coeff, active_space_env%p_active(isp), nmo)
    1018              :       END DO
    1019            0 :       SELECT CASE (mselect)
    1020              :       CASE DEFAULT
    1021            0 :          CPABORT("Unknown orbital selection method")
    1022              :       CASE (casci_canonical, manual_selection)
    1023           72 :          focc = 2.0_dp
    1024           72 :          IF (nspins == 2) focc = 1.0_dp
    1025          158 :          DO isp = 1, nspins
    1026           86 :             fmat => active_space_env%p_active(isp)
    1027           86 :             CALL cp_fm_set_all(fmat, alpha=0.0_dp)
    1028           86 :             IF (nspins == 2) THEN
    1029           28 :                IF (isp == 1) THEN
    1030           14 :                   nel = (active_space_env%nelec_active + active_space_env%multiplicity - 1)/2
    1031              :                ELSE
    1032           14 :                   nel = (active_space_env%nelec_active - active_space_env%multiplicity + 1)/2
    1033              :                END IF
    1034              :             ELSE
    1035           58 :                nel = active_space_env%nelec_active
    1036              :             END IF
    1037          388 :             DO i = 1, nmo_active
    1038          230 :                m = active_space_env%active_orbitals(i, isp)
    1039          230 :                fel = MIN(focc, REAL(nel, KIND=dp))
    1040          230 :                CALL cp_fm_set_element(fmat, m, m, fel)
    1041          230 :                nel = nel - NINT(fel)
    1042          316 :                nel = MAX(nel, 0)
    1043              :             END DO
    1044              :          END DO
    1045              :       CASE (wannier_projection)
    1046            0 :          CPABORT("NOT IMPLEMENTED")
    1047              :       CASE (mao_projection)
    1048           72 :          CPABORT("NOT IMPLEMENTED")
    1049              :       END SELECT
    1050              : 
    1051              :       ! compute alpha-beta overlap matrix in case of spin-polarized calculation
    1052           72 :       CALL calculate_spin_pol_overlap(active_space_env%mos_active, qs_env, active_space_env)
    1053              : 
    1054              :       ! figure out if we have a new xc section for the AS
    1055           72 :       xc_section => section_vals_get_subs_vals(input, "DFT%ACTIVE_SPACE%XC")
    1056           72 :       explicit = .FALSE.
    1057           72 :       IF (ASSOCIATED(xc_section)) CALL section_vals_get(xc_section, explicit=explicit)
    1058              : 
    1059              :       ! rebuild KS matrix if needed
    1060           72 :       IF (explicit) THEN
    1061              :          ! release the hfx data if it was part of the SCF functional
    1062            2 :          IF (ASSOCIATED(qs_env%x_data)) CALL hfx_release(qs_env%x_data)
    1063              :          ! also release the admm environment in case we are using admm
    1064            2 :          IF (ASSOCIATED(qs_env%admm_env)) CALL admm_env_release(qs_env%admm_env)
    1065              : 
    1066              :          CALL get_qs_env(qs_env, atomic_kind_set=atomic_kind_set, qs_kind_set=qs_kind_set, &
    1067            2 :                          particle_set=particle_set, cell=cell, ks_env=ks_env)
    1068            2 :          IF (dft_control%do_admm) THEN
    1069            0 :             basis_type = 'AUX_FIT'
    1070              :          ELSE
    1071            2 :             basis_type = 'ORB'
    1072              :          END IF
    1073            2 :          hfx_section => section_vals_get_subs_vals(xc_section, "HF")
    1074              :          CALL hfx_create(qs_env%x_data, para_env, hfx_section, atomic_kind_set, &
    1075              :                          qs_kind_set, particle_set, dft_control, cell, orb_basis=basis_type, &
    1076            2 :                          nelectron_total=nelec_total)
    1077              : 
    1078            2 :          qs_env%requires_matrix_vxc = .TRUE.  ! needs to be set only once
    1079              : 
    1080              :          ! a bit of a hack: this forces a new re-init of HFX
    1081            2 :          CALL set_ks_env(ks_env, s_mstruct_changed=.TRUE.)
    1082              :          CALL qs_ks_build_kohn_sham_matrix(qs_env, calculate_forces=.FALSE., &
    1083              :                                            just_energy=.FALSE., &
    1084            2 :                                            ext_xc_section=xc_section)
    1085              :          ! we need to reset it to false
    1086            2 :          CALL set_ks_env(ks_env, s_mstruct_changed=.FALSE.)
    1087              :       ELSE
    1088           70 :          xc_section => section_vals_get_subs_vals(input, "DFT%XC")
    1089              :       END IF
    1090              :       ! set the xc_section
    1091           72 :       active_space_env%xc_section => xc_section
    1092              : 
    1093           72 :       CALL get_qs_env(qs_env, energy=energy)
    1094              :       ! transform KS/Fock, Vxc and Hcore to AS MO basis
    1095           72 :       CALL calculate_operators(active_space_env%mos_active, qs_env, active_space_env)
    1096              :       ! set the reference energy in the active space
    1097           72 :       active_space_env%energy_ref = energy%total
    1098              :       ! calculate inactive energy and embedding potential
    1099           72 :       CALL subspace_fock_matrix(active_space_env, dft_control%roks)
    1100              : 
    1101              :       ! associate the active space environment with the qs environment
    1102           72 :       CALL set_qs_env(qs_env, active_space=active_space_env)
    1103              : 
    1104              :       ! Perform the embedding calculation only if qiskit is specified
    1105           72 :       CALL section_vals_val_get(as_input, "AS_SOLVER", i_val=as_solver)
    1106           72 :       SELECT CASE (as_solver)
    1107              :       CASE (no_solver)
    1108           72 :          IF (iw > 0) THEN
    1109           36 :             WRITE (iw, '(/,T3,A)') "No active space solver specified, skipping embedding calculation"
    1110           36 :             CALL m_flush(iw)
    1111              :          END IF
    1112           72 :          CALL para_env%sync()
    1113              :       CASE (qiskit_solver)
    1114            0 :          CALL rsdft_embedding(qs_env, active_space_env, as_input)
    1115            0 :          CALL qs_scf_compute_properties(qs_env, wf_type="MC-DFT", do_mp2=.FALSE.)
    1116              :       CASE DEFAULT
    1117           72 :          CPABORT("Unknown active space solver")
    1118              :       END SELECT
    1119              : 
    1120              :       ! Output a FCIDUMP file if requested
    1121           72 :       IF (active_space_env%fcidump) CALL fcidump(active_space_env, as_input, dft_control%roks)
    1122              : 
    1123              :       ! Output a QCSchema file if requested
    1124           72 :       IF (active_space_env%qcschema) THEN
    1125            4 :          CALL qcschema_env_create(qcschema_env, qs_env)
    1126            4 :          CALL qcschema_to_hdf5(qcschema_env, active_space_env%qcschema_filename)
    1127            4 :          CALL qcschema_env_release(qcschema_env)
    1128              :       END IF
    1129              : 
    1130           72 :       IF (iw > 0) THEN
    1131              :          WRITE (iw, '(/,T2,A)') &
    1132           36 :             '!-------------------- End of Active Space Interface --------------------------!'
    1133           36 :          CALL m_flush(iw)
    1134              :       END IF
    1135           72 :       CALL para_env%sync()
    1136              : 
    1137           72 :       CALL timestop(handle)
    1138              : 
    1139        91532 :    END SUBROUTINE active_space_main
    1140              : 
    1141              : ! **************************************************************************************************
    1142              : !> \brief computes the alpha-beta overlap within the active subspace
    1143              : !> \param mos the molecular orbital set within the active subspace
    1144              : !> \param qs_env ...
    1145              : !> \param active_space_env ...
    1146              : !> \par History
    1147              : !>      04.2016 created [JGH]
    1148              : ! **************************************************************************************************
    1149           72 :    SUBROUTINE calculate_spin_pol_overlap(mos, qs_env, active_space_env)
    1150              : 
    1151              :       TYPE(mo_set_type), DIMENSION(:), INTENT(IN)        :: mos
    1152              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1153              :       TYPE(active_space_type), POINTER                   :: active_space_env
    1154              : 
    1155              :       CHARACTER(len=*), PARAMETER :: routineN = 'calculate_spin_pol_overlap'
    1156              : 
    1157              :       INTEGER                                            :: handle, nmo, nspins
    1158              :       TYPE(cp_fm_type), POINTER                          :: mo_coeff_a, mo_coeff_b
    1159           72 :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: s_matrix
    1160              : 
    1161           72 :       CALL timeset(routineN, handle)
    1162              : 
    1163           72 :       nspins = active_space_env%nspins
    1164              : 
    1165              :       ! overlap in AO
    1166           72 :       IF (nspins > 1) THEN
    1167           14 :          CALL get_qs_env(qs_env, matrix_s=s_matrix)
    1168           28 :          ALLOCATE (active_space_env%sab_sub(1))
    1169              : 
    1170           14 :          CALL get_mo_set(mo_set=mos(1), mo_coeff=mo_coeff_a, nmo=nmo)
    1171           14 :          CALL get_mo_set(mo_set=mos(2), mo_coeff=mo_coeff_b, nmo=nmo)
    1172           14 :          CALL subspace_operator(mo_coeff_a, nmo, s_matrix(1)%matrix, active_space_env%sab_sub(1), mo_coeff_b)
    1173              :       END IF
    1174              : 
    1175           72 :       CALL timestop(handle)
    1176              : 
    1177           72 :    END SUBROUTINE calculate_spin_pol_overlap
    1178              : 
    1179              : ! **************************************************************************************************
    1180              : !> \brief computes the one-electron operators in the subspace of the provided orbital set
    1181              : !> \param mos the molecular orbital set within the active subspace
    1182              : !> \param qs_env ...
    1183              : !> \param active_space_env ...
    1184              : !> \par History
    1185              : !>      04.2016 created [JGH]
    1186              : ! **************************************************************************************************
    1187           72 :    SUBROUTINE calculate_operators(mos, qs_env, active_space_env)
    1188              : 
    1189              :       TYPE(mo_set_type), DIMENSION(:), INTENT(IN)        :: mos
    1190              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1191              :       TYPE(active_space_type), POINTER                   :: active_space_env
    1192              : 
    1193              :       CHARACTER(len=*), PARAMETER :: routineN = 'calculate_operators'
    1194              : 
    1195              :       INTEGER                                            :: handle, ispin, nmo, nspins
    1196              :       TYPE(cp_fm_type), POINTER                          :: mo_coeff
    1197           72 :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: h_matrix, ks_matrix
    1198              : 
    1199           72 :       CALL timeset(routineN, handle)
    1200              : 
    1201           72 :       nspins = active_space_env%nspins
    1202              : 
    1203              :       ! Kohn-Sham / Fock operator
    1204           72 :       CALL cp_fm_release(active_space_env%ks_sub)
    1205           72 :       CALL get_qs_env(qs_env, matrix_ks_kp=ks_matrix)
    1206          302 :       ALLOCATE (active_space_env%ks_sub(nspins))
    1207          158 :       DO ispin = 1, nspins
    1208           86 :          CALL get_mo_set(mo_set=mos(ispin), mo_coeff=mo_coeff, nmo=nmo)
    1209          158 :          CALL subspace_operator(mo_coeff, nmo, ks_matrix(ispin, 1)%matrix, active_space_env%ks_sub(ispin))
    1210              :       END DO
    1211              : 
    1212              :       ! Core Hamiltonian
    1213           72 :       CALL cp_fm_release(active_space_env%h_sub)
    1214              : 
    1215           72 :       NULLIFY (h_matrix)
    1216           72 :       CALL get_qs_env(qs_env=qs_env, matrix_h_kp=h_matrix)
    1217          302 :       ALLOCATE (active_space_env%h_sub(nspins))
    1218          158 :       DO ispin = 1, nspins
    1219           86 :          CALL get_mo_set(mo_set=mos(ispin), mo_coeff=mo_coeff, nmo=nmo)
    1220          158 :          CALL subspace_operator(mo_coeff, nmo, h_matrix(1, 1)%matrix, active_space_env%h_sub(ispin))
    1221              :       END DO
    1222              : 
    1223           72 :       CALL timestop(handle)
    1224              : 
    1225           72 :    END SUBROUTINE calculate_operators
    1226              : 
    1227              : ! **************************************************************************************************
    1228              : !> \brief computes a one-electron operator in the subspace of the provided orbital set
    1229              : !> \param mo_coeff the orbital coefficient matrix
    1230              : !> \param nmo the number of subspace orbitals
    1231              : !> \param op_matrix operator matrix in AO basis
    1232              : !> \param op_sub operator in orbital basis
    1233              : !> \param mo_coeff_b the beta orbital coefficients
    1234              : !> \par History
    1235              : !>      04.2016 created [JGH]
    1236              : ! **************************************************************************************************
    1237          372 :    SUBROUTINE subspace_operator(mo_coeff, nmo, op_matrix, op_sub, mo_coeff_b)
    1238              : 
    1239              :       TYPE(cp_fm_type), INTENT(IN)                       :: mo_coeff
    1240              :       INTEGER, INTENT(IN)                                :: nmo
    1241              :       TYPE(dbcsr_type), POINTER                          :: op_matrix
    1242              :       TYPE(cp_fm_type), INTENT(INOUT)                    :: op_sub
    1243              :       TYPE(cp_fm_type), INTENT(IN), OPTIONAL             :: mo_coeff_b
    1244              : 
    1245              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'subspace_operator'
    1246              : 
    1247              :       INTEGER                                            :: handle, ncol, nrow
    1248              :       TYPE(cp_fm_type)                                   :: vectors
    1249              : 
    1250          186 :       CALL timeset(routineN, handle)
    1251              : 
    1252          186 :       CALL cp_fm_get_info(matrix=mo_coeff, ncol_global=ncol, nrow_global=nrow)
    1253          186 :       CPASSERT(nmo <= ncol)
    1254              : 
    1255          186 :       IF (nmo > 0) THEN
    1256          186 :          CALL cp_fm_create(vectors, mo_coeff%matrix_struct, "vectors")
    1257          186 :          CALL create_subspace_matrix(mo_coeff, op_sub, nmo)
    1258              : 
    1259          186 :          IF (PRESENT(mo_coeff_b)) THEN
    1260              :             ! if beta orbitals are present, compute the cross alpha_beta term
    1261           14 :             CALL cp_dbcsr_sm_fm_multiply(op_matrix, mo_coeff_b, vectors, nmo)
    1262              :          ELSE
    1263              :             ! otherwise the same spin, whatever that is
    1264          172 :             CALL cp_dbcsr_sm_fm_multiply(op_matrix, mo_coeff, vectors, nmo)
    1265              :          END IF
    1266              : 
    1267          186 :          CALL parallel_gemm('T', 'N', nmo, nmo, nrow, 1.0_dp, mo_coeff, vectors, 0.0_dp, op_sub)
    1268          186 :          CALL cp_fm_release(vectors)
    1269              :       END IF
    1270              : 
    1271          186 :       CALL timestop(handle)
    1272              : 
    1273          186 :    END SUBROUTINE subspace_operator
    1274              : 
    1275              : ! **************************************************************************************************
    1276              : !> \brief creates a matrix of subspace size
    1277              : !> \param orbitals the orbital coefficient matrix
    1278              : !> \param op_sub operator in orbital basis
    1279              : !> \param n the number of orbitals
    1280              : !> \par History
    1281              : !>      04.2016 created [JGH]
    1282              : ! **************************************************************************************************
    1283          272 :    SUBROUTINE create_subspace_matrix(orbitals, op_sub, n)
    1284              : 
    1285              :       TYPE(cp_fm_type), INTENT(IN)                       :: orbitals
    1286              :       TYPE(cp_fm_type), INTENT(OUT)                      :: op_sub
    1287              :       INTEGER, INTENT(IN)                                :: n
    1288              : 
    1289              :       TYPE(cp_fm_struct_type), POINTER                   :: fm_struct
    1290              : 
    1291          272 :       IF (n > 0) THEN
    1292              : 
    1293          272 :          NULLIFY (fm_struct)
    1294              :          CALL cp_fm_struct_create(fm_struct, nrow_global=n, ncol_global=n, &
    1295              :                                   para_env=orbitals%matrix_struct%para_env, &
    1296          272 :                                   context=orbitals%matrix_struct%context)
    1297          272 :          CALL cp_fm_create(op_sub, fm_struct, name="Subspace operator")
    1298          272 :          CALL cp_fm_struct_release(fm_struct)
    1299              : 
    1300              :       END IF
    1301              : 
    1302          272 :    END SUBROUTINE create_subspace_matrix
    1303              : 
    1304              : ! **************************************************************************************************
    1305              : !> \brief computes the electron repulsion integrals using the GPW technology
    1306              : !> \param mos the molecular orbital set within the active subspace
    1307              : !> \param orbitals ...
    1308              : !> \param eri_env ...
    1309              : !> \param qs_env ...
    1310              : !> \param iw ...
    1311              : !> \param restricted ...
    1312              : !> \par History
    1313              : !>      04.2016 created [JGH]
    1314              : ! **************************************************************************************************
    1315           72 :    SUBROUTINE calculate_eri_gpw(mos, orbitals, eri_env, qs_env, iw, restricted)
    1316              : 
    1317              :       TYPE(mo_set_type), DIMENSION(:), INTENT(IN)        :: mos
    1318              :       INTEGER, DIMENSION(:, :), POINTER                  :: orbitals
    1319              :       TYPE(eri_type)                                     :: eri_env
    1320              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1321              :       INTEGER, INTENT(IN)                                :: iw
    1322              :       LOGICAL, INTENT(IN)                                :: restricted
    1323              : 
    1324              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'calculate_eri_gpw'
    1325              : 
    1326              :       INTEGER :: col_local, color, handle, i1, i2, i3, i4, i_multigrid, icount2, intcount, isp, &
    1327              :          isp1, isp2, ispin, iwa1, iwa12, iwa2, iwb1, iwb12, iwb2, iwbs, iwbt, iwfn, n_multigrid, &
    1328              :          ncol_global, ncol_local, nmm, nmo, nmo1, nmo2, nrow_global, nrow_local, nspins, &
    1329              :          number_of_subgroups, nx, row_local, stored_integrals
    1330           72 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: eri_index
    1331           72 :       INTEGER, DIMENSION(:), POINTER                     :: col_indices, row_indices
    1332              :       LOGICAL                                            :: print1, print2, &
    1333              :                                                             skip_load_balance_distributed
    1334              :       REAL(KIND=dp)                                      :: dvol, erint, pair_int, &
    1335              :                                                             progression_factor, rc, rsize, t1, t2
    1336           72 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: eri
    1337           72 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
    1338              :       TYPE(cell_type), POINTER                           :: cell
    1339              :       TYPE(cp_blacs_env_type), POINTER                   :: blacs_env, blacs_env_sub
    1340              :       TYPE(cp_fm_struct_type), POINTER                   :: fm_struct
    1341           72 :       TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:)        :: fm_matrix_pq_rnu, fm_matrix_pq_rs, &
    1342           72 :                                                             fm_mo_coeff_as
    1343              :       TYPE(cp_fm_type), POINTER                          :: mo_coeff
    1344              :       TYPE(dbcsr_p_type)                                 :: mat_munu
    1345           72 :       TYPE(dbcsr_type), ALLOCATABLE, DIMENSION(:)        :: matrix_pq_rnu, mo_coeff_as
    1346              :       TYPE(dft_control_type), POINTER                    :: dft_control
    1347              :       TYPE(mp_para_env_type), POINTER                    :: para_env
    1348              :       TYPE(neighbor_list_set_p_type), DIMENSION(:), &
    1349           72 :          POINTER                                         :: sab_orb_sub
    1350           72 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
    1351              :       TYPE(pw_c1d_gs_type)                               :: pot_g, rho_g
    1352              :       TYPE(pw_env_type), POINTER                         :: pw_env_sub
    1353              :       TYPE(pw_poisson_type), POINTER                     :: poisson_env
    1354              :       TYPE(pw_pool_type), POINTER                        :: auxbas_pw_pool
    1355              :       TYPE(pw_r3d_rs_type)                               :: rho_r, wfn_r
    1356              :       TYPE(pw_r3d_rs_type), ALLOCATABLE, &
    1357           72 :          DIMENSION(:, :), TARGET                         :: wfn_a
    1358              :       TYPE(pw_r3d_rs_type), POINTER                      :: wfn1, wfn2, wfn3, wfn4
    1359              :       TYPE(qs_control_type), POINTER                     :: qs_control, qs_control_old
    1360           72 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
    1361              :       TYPE(qs_ks_env_type), POINTER                      :: ks_env
    1362              :       TYPE(task_list_type), POINTER                      :: task_list_sub
    1363              : 
    1364           72 :       CALL timeset(routineN, handle)
    1365              : 
    1366           72 :       IF (iw > 0) t1 = m_walltime()
    1367              : 
    1368              :       ! print levels
    1369          136 :       SELECT CASE (eri_env%eri_gpw%print_level)
    1370              :       CASE (silent_print_level)
    1371           64 :          print1 = .FALSE.
    1372           64 :          print2 = .FALSE.
    1373              :       CASE (low_print_level)
    1374            4 :          print1 = .FALSE.
    1375            4 :          print2 = .FALSE.
    1376              :       CASE (medium_print_level)
    1377            4 :          print1 = .TRUE.
    1378            4 :          print2 = .FALSE.
    1379              :       CASE (high_print_level)
    1380            0 :          print1 = .TRUE.
    1381            0 :          print2 = .TRUE.
    1382              :       CASE (debug_print_level)
    1383            0 :          print1 = .TRUE.
    1384           72 :          print2 = .TRUE.
    1385              :       CASE DEFAULT
    1386              :          ! do nothing
    1387              :       END SELECT
    1388              : 
    1389              :       ! Check the input group
    1390           72 :       CALL get_qs_env(qs_env, para_env=para_env, blacs_env=blacs_env)
    1391           72 :       IF (eri_env%eri_gpw%group_size < 1) eri_env%eri_gpw%group_size = para_env%num_pe
    1392           72 :       IF (MOD(para_env%num_pe, eri_env%eri_gpw%group_size) /= 0) &
    1393            0 :          CPABORT("Group size must be a divisor of the total number of processes!")
    1394              :       ! Create a new para_env or reuse the old one
    1395           72 :       IF (eri_env%eri_gpw%group_size == para_env%num_pe) THEN
    1396           66 :          eri_env%para_env_sub => para_env
    1397           66 :          CALL eri_env%para_env_sub%retain()
    1398           66 :          blacs_env_sub => blacs_env
    1399           66 :          CALL blacs_env_sub%retain()
    1400           66 :          number_of_subgroups = 1
    1401           66 :          color = 0
    1402              :       ELSE
    1403            6 :          number_of_subgroups = para_env%num_pe/eri_env%eri_gpw%group_size
    1404            6 :          color = para_env%mepos/eri_env%eri_gpw%group_size
    1405            6 :          ALLOCATE (eri_env%para_env_sub)
    1406            6 :          CALL eri_env%para_env_sub%from_split(para_env, color)
    1407            6 :          NULLIFY (blacs_env_sub)
    1408            6 :          CALL cp_blacs_env_create(blacs_env_sub, eri_env%para_env_sub, BLACS_GRID_SQUARE, .TRUE.)
    1409              :       END IF
    1410           72 :       CALL eri_env%comm_exchange%from_split(para_env, eri_env%para_env_sub%mepos)
    1411              : 
    1412              :       ! This should be done differently! Copied from MP2 code
    1413           72 :       CALL get_qs_env(qs_env, dft_control=dft_control)
    1414          288 :       ALLOCATE (qs_control)
    1415           72 :       qs_control_old => dft_control%qs_control
    1416           72 :       qs_control = qs_control_old
    1417           72 :       dft_control%qs_control => qs_control
    1418           72 :       progression_factor = qs_control%progression_factor
    1419           72 :       n_multigrid = SIZE(qs_control%e_cutoff)
    1420           72 :       nspins = SIZE(mos)
    1421              :       ! In case of ROHF/ROKS, we assume the orbital coefficients in both spin channels to be the same
    1422              :       ! and save operations by calculating ERIs from only one spin channel
    1423           72 :       IF (restricted) nspins = 1
    1424              :       ! Allocate new cutoffs (just in private qs_control, not in qs_control_old)
    1425          216 :       ALLOCATE (qs_control%e_cutoff(n_multigrid))
    1426              : 
    1427           72 :       qs_control%cutoff = eri_env%eri_gpw%cutoff*0.5_dp
    1428           72 :       qs_control%e_cutoff(1) = qs_control%cutoff
    1429          288 :       DO i_multigrid = 2, n_multigrid
    1430              :          qs_control%e_cutoff(i_multigrid) = qs_control%e_cutoff(i_multigrid - 1) &
    1431          288 :                                             /progression_factor
    1432              :       END DO
    1433           72 :       qs_control%relative_cutoff = eri_env%eri_gpw%rel_cutoff*0.5_dp
    1434              : 
    1435              :       ! For now, we will distribute neighbor lists etc. within the global communicator
    1436           72 :       CALL get_qs_env(qs_env, ks_env=ks_env)
    1437              :       CALL create_mat_munu(mat_munu, qs_env, eri_env%eri_gpw%eps_grid, blacs_env_sub, sab_orb_sub=sab_orb_sub, &
    1438           72 :                            do_alloc_blocks_from_nbl=.TRUE., dbcsr_sym_type=dbcsr_type_symmetric)
    1439           72 :       CALL dbcsr_set(mat_munu%matrix, 0.0_dp)
    1440              : 
    1441              :       ! Generate the appropriate pw_env
    1442           72 :       NULLIFY (pw_env_sub)
    1443           72 :       CALL pw_env_create(pw_env_sub)
    1444           72 :       CALL pw_env_rebuild(pw_env_sub, qs_env, external_para_env=eri_env%para_env_sub)
    1445           72 :       CALL pw_env_get(pw_env_sub, auxbas_pw_pool=auxbas_pw_pool, poisson_env=poisson_env)
    1446              : 
    1447              :       ! TODO: maybe we can let `pw_env_rebuild` do what we manually overwrite here?
    1448           72 :       IF (eri_env%eri_gpw%redo_poisson) THEN
    1449              :          ! We need to rebuild the Poisson solver on the fly
    1450          288 :          IF (SUM(eri_env%periodicity) /= 0) THEN
    1451            8 :             poisson_env%parameters%solver = pw_poisson_periodic
    1452              :          ELSE
    1453           64 :             poisson_env%parameters%solver = pw_poisson_analytic
    1454              :          END IF
    1455          288 :          poisson_env%parameters%periodic = eri_env%periodicity
    1456              : 
    1457              :          ! Rebuilds the poisson green (influence) function according
    1458              :          ! to the poisson solver and parameters set so far.
    1459              :          ! Also sets the variable poisson_env%rebuild to .FALSE.
    1460           72 :          CALL pw_poisson_rebuild(poisson_env)
    1461              : 
    1462              :          ! set the cutoff radius for the Greens function in case we use ANALYTIC Poisson solver
    1463           72 :          CALL get_qs_env(qs_env, cell=cell)
    1464           72 :          rc = cell%hmat(1, 1)
    1465          288 :          DO iwa1 = 1, 3
    1466              :             ! TODO: I think this is not the largest possible radius inscribed in the cell
    1467          288 :             rc = MIN(rc, 0.5_dp*cell%hmat(iwa1, iwa1))
    1468              :          END DO
    1469           72 :          poisson_env%green_fft%radius = rc
    1470              : 
    1471              :          ! Overwrite the Greens function with the one we want
    1472           72 :          CALL pw_eri_green_create(poisson_env%green_fft, eri_env)
    1473              : 
    1474           72 :          IF (iw > 0) THEN
    1475           36 :             CALL get_qs_env(qs_env, cell=cell)
    1476          288 :             IF (SUM(cell%perd) /= SUM(eri_env%periodicity)) THEN
    1477            0 :                IF (SUM(eri_env%periodicity) /= 0) THEN
    1478              :                   WRITE (UNIT=iw, FMT="(/,T2,A,T51,A30)") &
    1479            0 :                      "ERI_GPW| Switching Poisson solver to", "PERIODIC"
    1480              :                ELSE
    1481              :                   WRITE (UNIT=iw, FMT="(/,T2,A,T51,A30)") &
    1482            0 :                      "ERI_GPW| Switching Poisson solver to", "ANALYTIC"
    1483              :                END IF
    1484              :             END IF
    1485              :             ! print out the Greens function to check it matches the Poisson solver
    1486           40 :             SELECT CASE (poisson_env%green_fft%method)
    1487              :             CASE (PERIODIC3D)
    1488              :                WRITE (UNIT=iw, FMT="(T2,A,T51,A30)") &
    1489            4 :                   "ERI_GPW| Poisson Greens function", "PERIODIC"
    1490              :             CASE (ANALYTIC0D)
    1491              :                WRITE (UNIT=iw, FMT="(T2,A,T51,A30)") &
    1492           32 :                   "ERI_GPW| Poisson Greens function", "ANALYTIC"
    1493           32 :                WRITE (UNIT=iw, FMT="(T2,A,T71,F10.4)") "ERI_GPW| Poisson cutoff radius", &
    1494           64 :                   poisson_env%green_fft%radius*angstrom
    1495              :             CASE DEFAULT
    1496           36 :                CPABORT("Wrong Greens function setup")
    1497              :             END SELECT
    1498              :          END IF
    1499              :       END IF
    1500              : 
    1501          528 :       ALLOCATE (mo_coeff_as(nspins), fm_mo_coeff_as(nspins))
    1502          156 :       DO ispin = 1, nspins
    1503          252 :          BLOCK
    1504           84 :             REAL(KIND=dp), DIMENSION(:, :), ALLOCATABLE :: C, C_active
    1505              :             INTEGER :: nmo
    1506           84 :             TYPE(group_dist_d1_type) :: gd_array
    1507              :             TYPE(cp_fm_type), POINTER :: mo_coeff
    1508           84 :             CALL get_mo_set(mos(ispin), mo_coeff=mo_coeff, nmo=nmo)
    1509              :             CALL grep_rows_in_subgroups(para_env, eri_env%para_env_sub, mo_coeff, gd_array, C)
    1510              : 
    1511          336 :             ALLOCATE (C_active(SIZE(C, 1), SIZE(orbitals, 1)))
    1512          310 :             DO i1 = 1, SIZE(orbitals, 1)
    1513         1216 :                C_active(:, i1) = C(:, orbitals(i1, ispin))
    1514              :             END DO
    1515              :             CALL build_dbcsr_from_rows(eri_env%para_env_sub, mo_coeff_as(ispin), &
    1516           84 :                                        C_active, mat_munu%matrix, gd_array, eri_env%eri_gpw%eps_filter)
    1517           84 :             CALL release_group_dist(gd_array)
    1518          336 :             DEALLOCATE (C, C_active)
    1519              :          END BLOCK
    1520              : 
    1521           84 :          CALL dbcsr_get_info(mo_coeff_as(ispin), nfullrows_total=nrow_global, nfullcols_total=ncol_global)
    1522              : 
    1523           84 :          NULLIFY (fm_struct)
    1524              :          CALL cp_fm_struct_create(fm_struct, context=blacs_env_sub, para_env=eri_env%para_env_sub, &
    1525           84 :                                   nrow_global=nrow_global, ncol_global=ncol_global)
    1526           84 :          CALL cp_fm_create(fm_mo_coeff_as(ispin), fm_struct)
    1527           84 :          CALL cp_fm_struct_release(fm_struct)
    1528              : 
    1529          240 :          CALL copy_dbcsr_to_fm(mo_coeff_as(ispin), fm_mo_coeff_as(ispin))
    1530              :       END DO
    1531              : 
    1532           72 :       IF (eri_env%method == eri_method_gpw_ht) THEN
    1533              :          ! We need a task list
    1534           14 :          NULLIFY (task_list_sub)
    1535           14 :          skip_load_balance_distributed = dft_control%qs_control%skip_load_balance_distributed
    1536           14 :          CALL allocate_task_list(task_list_sub)
    1537              :          CALL generate_qs_task_list(ks_env, task_list_sub, basis_type="ORB", &
    1538              :                                     reorder_rs_grid_ranks=.TRUE., &
    1539              :                                     skip_load_balance_distributed=skip_load_balance_distributed, &
    1540           14 :                                     pw_env_external=pw_env_sub, sab_orb_external=sab_orb_sub)
    1541              : 
    1542              :          ! Create sparse matrices carrying the matrix products, Code borrowed from the MP2 GPW method
    1543              :          ! Create equal distributions for them (no sparsity present)
    1544              :          ! We use the routines from mp2 suggesting that one may replicate the grids later for better performance
    1545           98 :          ALLOCATE (matrix_pq_rnu(nspins), fm_matrix_pq_rnu(nspins), fm_matrix_pq_rs(nspins))
    1546           28 :          DO ispin = 1, nspins
    1547           14 :             CALL dbcsr_create(matrix_pq_rnu(ispin), template=mo_coeff_as(ispin))
    1548           14 :             CALL dbcsr_set(matrix_pq_rnu(ispin), 0.0_dp)
    1549              : 
    1550           14 :             CALL dbcsr_get_info(matrix_pq_rnu(ispin), nfullrows_total=nrow_global, nfullcols_total=ncol_global)
    1551              : 
    1552           14 :             NULLIFY (fm_struct)
    1553              :             CALL cp_fm_struct_create(fm_struct, context=blacs_env_sub, para_env=eri_env%para_env_sub, &
    1554           14 :                                      nrow_global=nrow_global, ncol_global=ncol_global)
    1555           14 :             CALL cp_fm_create(fm_matrix_pq_rnu(ispin), fm_struct)
    1556           14 :             CALL cp_fm_struct_release(fm_struct)
    1557              : 
    1558           14 :             NULLIFY (fm_struct)
    1559              :             CALL cp_fm_struct_create(fm_struct, context=blacs_env_sub, para_env=eri_env%para_env_sub, &
    1560           14 :                                      nrow_global=ncol_global, ncol_global=ncol_global)
    1561           14 :             CALL cp_fm_create(fm_matrix_pq_rs(ispin), fm_struct)
    1562           42 :             CALL cp_fm_struct_release(fm_struct)
    1563              :          END DO
    1564              : 
    1565              :          ! Copy the active space of the MOs into DBCSR matrices
    1566              :       END IF
    1567              : 
    1568           72 :       CALL auxbas_pw_pool%create_pw(wfn_r)
    1569           72 :       CALL auxbas_pw_pool%create_pw(rho_g)
    1570              :       CALL get_qs_env(qs_env, qs_kind_set=qs_kind_set, cell=cell, &
    1571           72 :                       particle_set=particle_set, atomic_kind_set=atomic_kind_set)
    1572              : 
    1573              :       ! pre-calculate wavefunctions on reals space grid
    1574           72 :       nspins = SIZE(mos)
    1575              :       ! In case of ROHF/ROKS, we assume the orbital coefficients in both spin channels to be the same
    1576              :       ! and save operations by calculating ERIs from only one spin channel
    1577              :       IF (restricted) nspins = 1
    1578           72 :       IF (eri_env%eri_gpw%store_wfn) THEN
    1579              :          ! pre-calculate wavefunctions on reals space grid
    1580           60 :          rsize = 0.0_dp
    1581           60 :          nmo = 0
    1582          132 :          DO ispin = 1, nspins
    1583           72 :             CALL get_mo_set(mo_set=mos(ispin), nmo=nx)
    1584           72 :             nmo = MAX(nmo, nx)
    1585          348 :             rsize = REAL(SIZE(wfn_r%array), KIND=dp)*nx
    1586              :          END DO
    1587           60 :          IF (print1 .AND. iw > 0) THEN
    1588            2 :             rsize = rsize*8._dp/1000000._dp
    1589            2 :             WRITE (iw, "(T2,'ERI_GPW|',' Store active orbitals on real space grid ',T66,F12.3,' MB')") rsize
    1590              :          END IF
    1591          572 :          ALLOCATE (wfn_a(nmo, nspins))
    1592          132 :          DO ispin = 1, nspins
    1593           72 :             CALL get_mo_set(mo_set=mos(ispin), mo_coeff=mo_coeff, nmo=nmo)
    1594          334 :             DO i1 = 1, SIZE(orbitals, 1)
    1595          202 :                iwfn = orbitals(i1, ispin)
    1596          202 :                CALL auxbas_pw_pool%create_pw(wfn_a(iwfn, ispin))
    1597              :                CALL calculate_wavefunction(mo_coeff, iwfn, wfn_a(iwfn, ispin), rho_g, atomic_kind_set, &
    1598          202 :                                            qs_kind_set, cell, dft_control, particle_set, pw_env_sub)
    1599          274 :                IF (print2 .AND. iw > 0) THEN
    1600            0 :                   WRITE (iw, "(T2,'ERI_GPW|',' Orbital stored ',I4,'  Spin ',i1)") iwfn, ispin
    1601              :                END IF
    1602              :             END DO
    1603              :          END DO
    1604              :       ELSE
    1605              :          ! Even if we do not store all WFNs, we still need containers for the functions to store
    1606           12 :          ALLOCATE (wfn1, wfn2)
    1607           12 :          CALL auxbas_pw_pool%create_pw(wfn1)
    1608           12 :          CALL auxbas_pw_pool%create_pw(wfn2)
    1609           12 :          IF (eri_env%method /= eri_method_gpw_ht) THEN
    1610            6 :             ALLOCATE (wfn3, wfn4)
    1611            6 :             CALL auxbas_pw_pool%create_pw(wfn3)
    1612            6 :             CALL auxbas_pw_pool%create_pw(wfn4)
    1613              :          END IF
    1614              :       END IF
    1615              : 
    1616              :       ! get some of the grids ready
    1617           72 :       CALL auxbas_pw_pool%create_pw(rho_r)
    1618           72 :       CALL auxbas_pw_pool%create_pw(pot_g)
    1619              : 
    1620              :       ! run the FFT once, to set up buffers and to take into account the memory
    1621           72 :       CALL pw_zero(rho_r)
    1622           72 :       CALL pw_transfer(rho_r, rho_g)
    1623           72 :       dvol = rho_r%pw_grid%dvol
    1624              : 
    1625           72 :       IF (iw > 0) THEN
    1626           36 :          CALL m_flush(iw)
    1627              :       END IF
    1628              :       ! calculate the integrals
    1629           72 :       stored_integrals = 0
    1630          156 :       DO isp1 = 1, nspins
    1631           84 :          CALL get_mo_set(mo_set=mos(isp1), nmo=nmo1)
    1632           84 :          nmm = (nmo1*(nmo1 + 1))/2
    1633          382 :          DO i1 = 1, SIZE(orbitals, 1)
    1634          226 :             iwa1 = orbitals(i1, isp1)
    1635          226 :             IF (eri_env%eri_gpw%store_wfn) THEN
    1636          202 :                wfn1 => wfn_a(iwa1, isp1)
    1637              :             ELSE
    1638              :                CALL calculate_wavefunction(fm_mo_coeff_as(isp1), iwa1, wfn1, rho_g, atomic_kind_set, &
    1639           24 :                                            qs_kind_set, cell, dft_control, particle_set, pw_env_sub)
    1640              :             END IF
    1641          770 :             DO i2 = i1, SIZE(orbitals, 1)
    1642          460 :                iwa2 = orbitals(i2, isp1)
    1643          460 :                iwa12 = csr_idx_to_combined(iwa1, iwa2, nmo1)
    1644              :                ! Skip calculation directly if the pair is not part of our subgroup
    1645          460 :                IF (MOD(iwa12 - 1, eri_env%comm_exchange%num_pe) /= eri_env%comm_exchange%mepos) CYCLE
    1646          451 :                iwa12 = (iwa12 - 1)/eri_env%comm_exchange%num_pe + 1
    1647          451 :                IF (eri_env%eri_gpw%store_wfn) THEN
    1648          421 :                   wfn2 => wfn_a(iwa2, isp1)
    1649              :                ELSE
    1650              :                   CALL calculate_wavefunction(fm_mo_coeff_as(isp1), iwa2, wfn2, rho_g, atomic_kind_set, &
    1651           30 :                                               qs_kind_set, cell, dft_control, particle_set, pw_env_sub)
    1652              :                END IF
    1653              :                ! calculate charge distribution and potential
    1654          451 :                CALL pw_zero(rho_r)
    1655          451 :                CALL pw_multiply(rho_r, wfn1, wfn2)
    1656          451 :                CALL pw_transfer(rho_r, rho_g)
    1657          451 :                CALL pw_poisson_solve(poisson_env, rho_g, pair_int, pot_g)
    1658              : 
    1659              :                ! screening using pair_int
    1660          451 :                IF (pair_int < eri_env%eps_integral) CYCLE
    1661          451 :                CALL pw_transfer(pot_g, rho_r)
    1662              :                !
    1663         1128 :                IF (eri_env%method == eri_method_gpw_ht) THEN
    1664           36 :                   CALL pw_scale(rho_r, dvol)
    1665           72 :                   DO isp2 = isp1, nspins
    1666           36 :                      CALL get_mo_set(mo_set=mos(isp2), nmo=nmo2)
    1667           36 :                      nx = (nmo2*(nmo2 + 1))/2
    1668          180 :                      ALLOCATE (eri(nx), eri_index(nx))
    1669           36 :                      CALL dbcsr_set(mat_munu%matrix, 0.0_dp)
    1670              :                      CALL integrate_v_rspace(rho_r, hmat=mat_munu, qs_env=qs_env, &
    1671              :                                              calculate_forces=.FALSE., compute_tau=.FALSE., gapw=.FALSE., &
    1672           36 :                                              pw_env_external=pw_env_sub, task_list_external=task_list_sub)
    1673              : 
    1674              :                      CALL dbcsr_multiply("N", "N", 1.0_dp, mat_munu%matrix, mo_coeff_as(isp2), &
    1675           36 :                                          0.0_dp, matrix_pq_rnu(isp2), filter_eps=eri_env%eri_gpw%eps_filter)
    1676           36 :                      CALL copy_dbcsr_to_fm(matrix_pq_rnu(isp2), fm_matrix_pq_rnu(isp2))
    1677              : 
    1678           36 :                      CALL cp_fm_get_info(fm_matrix_pq_rnu(isp2), ncol_global=ncol_global, nrow_global=nrow_global)
    1679              : 
    1680              :                      CALL parallel_gemm("T", "N", ncol_global, ncol_global, nrow_global, 0.5_dp, &
    1681              :                                         fm_matrix_pq_rnu(isp2), fm_mo_coeff_as(isp2), &
    1682           36 :                                         0.0_dp, fm_matrix_pq_rs(isp2))
    1683              :                      CALL parallel_gemm("T", "N", ncol_global, ncol_global, nrow_global, 0.5_dp, &
    1684              :                                         fm_mo_coeff_as(isp2), fm_matrix_pq_rnu(isp2), &
    1685           36 :                                         1.0_dp, fm_matrix_pq_rs(isp2))
    1686              : 
    1687              :                      CALL cp_fm_get_info(fm_matrix_pq_rs(isp2), ncol_local=ncol_local, nrow_local=nrow_local, &
    1688           36 :                                          col_indices=col_indices, row_indices=row_indices)
    1689              : 
    1690           36 :                      icount2 = 0
    1691          108 :                      DO col_local = 1, ncol_local
    1692           72 :                         iwb1 = orbitals(col_indices(col_local), isp2)
    1693           72 :                         IF (isp1 == isp2 .AND. iwb1 < iwa1) CYCLE
    1694          166 :                         DO row_local = 1, nrow_local
    1695           70 :                            iwb2 = orbitals(row_indices(row_local), isp2)
    1696           70 :                            IF (iwb2 < iwb1) CYCLE
    1697           49 :                            IF (isp1 == isp2 .AND. iwa1 == iwb1 .AND. iwb2 < iwa2) CYCLE
    1698              : 
    1699           42 :                            iwb12 = csr_idx_to_combined(iwb1, iwb2, nmo2)
    1700           42 :                            erint = fm_matrix_pq_rs(isp2)%local_data(row_local, col_local)
    1701          114 :                            IF (ABS(erint) > eri_env%eps_integral) THEN
    1702           35 :                               icount2 = icount2 + 1
    1703           35 :                               eri(icount2) = erint
    1704           35 :                               eri_index(icount2) = iwb12
    1705              :                            END IF
    1706              :                         END DO
    1707              :                      END DO
    1708           36 :                      stored_integrals = stored_integrals + icount2
    1709              :                      !
    1710           36 :                      isp = (isp1 - 1)*isp2 + (isp2 - isp1 + 1)
    1711           36 :                      CALL update_csr_matrix(eri_env%eri(isp)%csr_mat, icount2, eri, eri_index, iwa12)
    1712              :                      !
    1713          180 :                      DEALLOCATE (eri, eri_index)
    1714              :                   END DO
    1715          415 :                ELSEIF (eri_env%method == eri_method_full_gpw) THEN
    1716          884 :                   DO isp2 = isp1, nspins
    1717          469 :                      CALL get_mo_set(mo_set=mos(isp2), nmo=nmo2)
    1718          469 :                      nx = (nmo2*(nmo2 + 1))/2
    1719         2345 :                      ALLOCATE (eri(nx), eri_index(nx))
    1720          469 :                      icount2 = 0
    1721          469 :                      iwbs = 1
    1722          469 :                      IF (isp1 == isp2) iwbs = i1
    1723          469 :                      isp = (isp1 - 1)*isp2 + (isp2 - isp1 + 1)
    1724         1750 :                      DO i3 = iwbs, SIZE(orbitals, 1)
    1725         1281 :                         iwb1 = orbitals(i3, isp2)
    1726         1281 :                         IF (eri_env%eri_gpw%store_wfn) THEN
    1727         1256 :                            wfn3 => wfn_a(iwb1, isp2)
    1728              :                         ELSE
    1729              :                            CALL calculate_wavefunction(fm_mo_coeff_as(isp1), iwb1, wfn3, rho_g, atomic_kind_set, &
    1730           25 :                                                        qs_kind_set, cell, dft_control, particle_set, pw_env_sub)
    1731              :                         END IF
    1732         1281 :                         CALL pw_zero(wfn_r)
    1733         1281 :                         CALL pw_multiply(wfn_r, rho_r, wfn3)
    1734         1281 :                         iwbt = i3
    1735         1281 :                         IF (isp1 == isp2 .AND. i1 == i3) iwbt = i2
    1736         4154 :                         DO i4 = iwbt, SIZE(orbitals, 1)
    1737         2404 :                            iwb2 = orbitals(i4, isp2)
    1738         2404 :                            IF (eri_env%eri_gpw%store_wfn) THEN
    1739         2374 :                               wfn4 => wfn_a(iwb2, isp2)
    1740              :                            ELSE
    1741              :                               CALL calculate_wavefunction(fm_mo_coeff_as(isp1), iwb2, wfn4, rho_g, atomic_kind_set, &
    1742           30 :                                                           qs_kind_set, cell, dft_control, particle_set, pw_env_sub)
    1743              :                            END IF
    1744              :                            ! We reduce the amount of communication by collecting the local sums first and sum globally later
    1745         2404 :                            erint = pw_integral_ab(wfn_r, wfn4, local_only=.TRUE.)
    1746         2404 :                            icount2 = icount2 + 1
    1747         2404 :                            eri(icount2) = erint
    1748         3685 :                            eri_index(icount2) = csr_idx_to_combined(iwb1, iwb2, nmo2)
    1749              :                         END DO
    1750              :                      END DO
    1751              :                      ! Now, we sum the integrals globally
    1752          469 :                      CALL eri_env%para_env_sub%sum(eri)
    1753              :                      ! and we reorder the integrals to prevent storing too small integrals
    1754          469 :                      intcount = 0
    1755          469 :                      icount2 = 0
    1756              :                      iwbs = 1
    1757              :                      IF (isp1 == isp2) iwbs = i1
    1758          469 :                      isp = (isp1 - 1)*isp2 + (isp2 - isp1 + 1)
    1759         1750 :                      DO i3 = iwbs, SIZE(orbitals, 1)
    1760         1281 :                         iwb1 = orbitals(i3, isp2)
    1761         1281 :                         iwbt = i3
    1762         1281 :                         IF (isp1 == isp2 .AND. i1 == i3) iwbt = i2
    1763         4154 :                         DO i4 = iwbt, SIZE(orbitals, 1)
    1764         2404 :                            iwb2 = orbitals(i4, isp2)
    1765         2404 :                            intcount = intcount + 1
    1766         2404 :                            erint = eri(intcount)
    1767         3685 :                            IF (ABS(erint) > eri_env%eps_integral) THEN
    1768         2028 :                               IF (MOD(intcount, eri_env%para_env_sub%num_pe) == eri_env%para_env_sub%mepos) THEN
    1769         1016 :                                  icount2 = icount2 + 1
    1770         1016 :                                  eri(icount2) = erint
    1771         1016 :                                  eri_index(icount2) = eri_index(intcount)
    1772              :                               END IF
    1773              :                            END IF
    1774              :                         END DO
    1775              :                      END DO
    1776          469 :                      stored_integrals = stored_integrals + icount2
    1777              :                      !
    1778          469 :                      CALL update_csr_matrix(eri_env%eri(isp)%csr_mat, icount2, eri, eri_index, iwa12)
    1779              :                      !
    1780         1353 :                      DEALLOCATE (eri, eri_index)
    1781              :                   END DO
    1782              :                ELSE
    1783            0 :                   CPABORT("Unknown option")
    1784              :                END IF
    1785              :             END DO
    1786              :          END DO
    1787              :       END DO
    1788              : 
    1789           72 :       IF (print1 .AND. iw > 0) THEN
    1790            2 :          WRITE (iw, "(T2,'ERI_GPW|',' Number of Integrals stored locally',T71,I10)") stored_integrals
    1791              :       END IF
    1792              : 
    1793           72 :       IF (eri_env%eri_gpw%store_wfn) THEN
    1794          132 :          DO ispin = 1, nspins
    1795          334 :             DO i1 = 1, SIZE(orbitals, 1)
    1796          202 :                iwfn = orbitals(i1, ispin)
    1797          274 :                CALL wfn_a(iwfn, ispin)%release()
    1798              :             END DO
    1799              :          END DO
    1800           60 :          DEALLOCATE (wfn_a)
    1801              :       ELSE
    1802           12 :          CALL wfn1%release()
    1803           12 :          CALL wfn2%release()
    1804           12 :          DEALLOCATE (wfn1, wfn2)
    1805           12 :          IF (eri_env%method /= eri_method_gpw_ht) THEN
    1806            6 :             CALL wfn3%release()
    1807            6 :             CALL wfn4%release()
    1808            6 :             DEALLOCATE (wfn3, wfn4)
    1809              :          END IF
    1810              :       END IF
    1811           72 :       CALL auxbas_pw_pool%give_back_pw(wfn_r)
    1812           72 :       CALL auxbas_pw_pool%give_back_pw(rho_g)
    1813           72 :       CALL auxbas_pw_pool%give_back_pw(rho_r)
    1814           72 :       CALL auxbas_pw_pool%give_back_pw(pot_g)
    1815              : 
    1816           72 :       IF (eri_env%method == eri_method_gpw_ht) THEN
    1817           28 :          DO ispin = 1, nspins
    1818           14 :             CALL dbcsr_release(mo_coeff_as(ispin))
    1819           14 :             CALL dbcsr_release(matrix_pq_rnu(ispin))
    1820           14 :             CALL cp_fm_release(fm_matrix_pq_rnu(ispin))
    1821           28 :             CALL cp_fm_release(fm_matrix_pq_rs(ispin))
    1822              :          END DO
    1823           14 :          DEALLOCATE (matrix_pq_rnu, fm_matrix_pq_rnu, fm_matrix_pq_rs)
    1824           14 :          CALL deallocate_task_list(task_list_sub)
    1825              :       END IF
    1826          156 :       DO ispin = 1, nspins
    1827           84 :          CALL dbcsr_release(mo_coeff_as(ispin))
    1828          156 :          CALL cp_fm_release(fm_mo_coeff_as(ispin))
    1829              :       END DO
    1830           72 :       DEALLOCATE (mo_coeff_as, fm_mo_coeff_as)
    1831           72 :       CALL release_neighbor_list_sets(sab_orb_sub)
    1832           72 :       CALL cp_blacs_env_release(blacs_env_sub)
    1833           72 :       CALL dbcsr_release(mat_munu%matrix)
    1834           72 :       DEALLOCATE (mat_munu%matrix)
    1835           72 :       CALL pw_env_release(pw_env_sub)
    1836              :       ! Return to the old qs_control
    1837           72 :       dft_control%qs_control => qs_control_old
    1838           72 :       DEALLOCATE (qs_control%e_cutoff)
    1839           72 :       DEALLOCATE (qs_control)
    1840              : 
    1841              :       ! print out progress
    1842           72 :       IF (iw > 0) THEN
    1843           36 :          t2 = m_walltime()
    1844           36 :          WRITE (iw, '(/,T2,A,T66,F14.2)') "ERI_GPW| ERI calculation took (sec)", t2 - t1
    1845           36 :          CALL m_flush(iw)
    1846              :       END IF
    1847              : 
    1848           72 :       CALL timestop(handle)
    1849              : 
    1850          144 :    END SUBROUTINE calculate_eri_gpw
    1851              : 
    1852              : ! **************************************************************************************************
    1853              : !> \brief Sets the Green's function for the ERI calculation. Here we deal with the G=0 case!
    1854              : !> \param green ...
    1855              : !> \param eri_env ...
    1856              : !> \par History
    1857              : !>      04.2016 created [JGH]
    1858              : !>      08.2025 added support for the LR truncation [SB]
    1859              : ! **************************************************************************************************
    1860           72 :    SUBROUTINE pw_eri_green_create(green, eri_env)
    1861              : 
    1862              :       TYPE(greens_fn_type), INTENT(INOUT)                :: green
    1863              :       TYPE(eri_type)                                     :: eri_env
    1864              : 
    1865              :       COMPLEX(KIND=dp)                                   :: erf_fac_p, z_p
    1866              :       INTEGER                                            :: ig
    1867              :       REAL(KIND=dp)                                      :: cossin_fac, ea, erfcos_fac, exp_prefac, &
    1868              :                                                             g, G0, g2, g3d, ga, Ginf, omega, &
    1869              :                                                             omega2, Rc, Rc2
    1870              : 
    1871              :       ! initialize influence function
    1872              :       ASSOCIATE (gf => green%influence_fn, grid => green%influence_fn%pw_grid)
    1873           80 :          SELECT CASE (green%method)
    1874              :          CASE (PERIODIC3D)
    1875              : 
    1876           76 :             SELECT CASE (eri_env%operator)
    1877              :             CASE (eri_operator_coulomb)
    1878       524290 :                DO ig = grid%first_gne0, grid%ngpts_cut_local
    1879       524286 :                   g2 = grid%gsq(ig)
    1880       524290 :                   gf%array(ig) = fourpi/g2
    1881              :                END DO
    1882            4 :                IF (grid%have_g0) gf%array(1) = 0.0_dp
    1883              : 
    1884              :             CASE (eri_operator_yukawa)
    1885            0 :                CALL cp_warn(__LOCATION__, "Yukawa operator has not been tested")
    1886            0 :                omega2 = eri_env%omega**2
    1887            0 :                DO ig = grid%first_gne0, grid%ngpts_cut_local
    1888            0 :                   g2 = grid%gsq(ig)
    1889            0 :                   gf%array(ig) = fourpi/(omega2 + g2)
    1890              :                END DO
    1891            0 :                IF (grid%have_g0) gf%array(1) = fourpi/omega2
    1892              : 
    1893              :             CASE (eri_operator_erf)
    1894            0 :                omega2 = eri_env%omega**2
    1895            0 :                DO ig = grid%first_gne0, grid%ngpts_cut_local
    1896            0 :                   g2 = grid%gsq(ig)
    1897            0 :                   gf%array(ig) = fourpi/g2*EXP(-0.25_dp*g2/omega2)
    1898              :                END DO
    1899            0 :                IF (grid%have_g0) gf%array(1) = 0.0_dp
    1900              : 
    1901              :             CASE (eri_operator_erfc)
    1902            0 :                omega2 = eri_env%omega**2
    1903            0 :                DO ig = grid%first_gne0, grid%ngpts_cut_local
    1904            0 :                   g2 = grid%gsq(ig)
    1905            0 :                   gf%array(ig) = fourpi/g2*(1.0_dp - EXP(-0.25_dp*g2/omega2))
    1906              :                END DO
    1907            0 :                IF (grid%have_g0) gf%array(1) = pi/omega2
    1908              : 
    1909              :             CASE (eri_operator_trunc)
    1910            0 :                Rc = eri_env%cutoff_radius
    1911            0 :                DO ig = grid%first_gne0, grid%ngpts_cut_local
    1912            0 :                   g2 = grid%gsq(ig)
    1913            0 :                   g = SQRT(g2)
    1914              :                   ! Taylor expansion around zero
    1915            0 :                   IF (g*Rc >= 0.005_dp) THEN
    1916            0 :                      gf%array(ig) = fourpi/g2*(1.0_dp - COS(g*Rc))
    1917              :                   ELSE
    1918            0 :                      gf%array(ig) = fourpi/g2*(g*Rc)**2/2.0_dp*(1.0_dp - (g*Rc)**2/12.0_dp)
    1919              :                   END IF
    1920              :                END DO
    1921            0 :                IF (grid%have_g0) gf%array(1) = twopi*Rc**2
    1922              : 
    1923              :             CASE (eri_operator_lr_trunc)
    1924            4 :                omega = eri_env%omega
    1925            4 :                omega2 = omega**2
    1926            4 :                Rc = eri_env%cutoff_radius
    1927            4 :                Rc2 = Rc**2
    1928            4 :                G0 = 0.001_dp ! threshold for the G=0 case
    1929            4 :                Ginf = 20.0_dp  ! threshold for the Taylor exapnsion arounf G=∞
    1930       843752 :                DO ig = grid%first_gne0, grid%ngpts_cut_local
    1931       843748 :                   g2 = grid%gsq(ig)
    1932       843748 :                   g = SQRT(g2)
    1933       843752 :                   IF (g <= 2.0_dp*G0) THEN
    1934              :                      gf%array(ig) = -pi/omega2*erf(omega*Rc) &
    1935              :                                     + twopi*Rc2*erf(omega*Rc) &
    1936            0 :                                     + 2*rootpi*Rc*EXP(-omega2*Rc2)/omega
    1937       843748 :                   ELSE IF (g >= 2.0_dp*Ginf*omega) THEN
    1938              :                      ! exponential prefactor
    1939         1488 :                      exp_prefac = EXP(-omega2*Rc2)/(rootpi*(omega2*Rc2 + 0.25_dp*g2/omega2))
    1940              :                      ! cos sin factor
    1941         1488 :                      cossin_fac = omega*Rc*COS(g*Rc) - 0.5_dp*g/omega*SIN(g*Rc)
    1942              :                      ! real erf term with cosine
    1943         1488 :                      erfcos_fac = ERF(omega*Rc)*COS(g*Rc)
    1944              :                      ! Combine terms
    1945         1488 :                      gf%array(ig) = fourpi/g2*(-exp_prefac*cossin_fac - erfcos_fac)
    1946              :                   ELSE
    1947              :                      ! exponential prefactor
    1948       842260 :                      exp_prefac = twopi/g2*EXP(-0.25_dp*g2/omega2)
    1949              :                      ! Compute complex arguments for erf
    1950       842260 :                      z_p = CMPLX(omega*Rc, 0.5_dp*g/omega, kind=dp)
    1951              :                      ! Evaluate complex error functions
    1952       842260 :                      erf_fac_p = 2.0_dp*REAL(erfz_fast(z_p))
    1953              :                      ! Real erf term with cosine
    1954       842260 :                      erfcos_fac = fourpi/g2*ERF(omega*Rc)*COS(g*Rc)
    1955              :                      ! Combine terms
    1956       842260 :                      gf%array(ig) = exp_prefac*erf_fac_p - erfcos_fac
    1957              :                   END IF
    1958              :                END DO
    1959            4 :                IF (grid%have_g0) THEN
    1960              :                   gf%array(1) = -pi/omega2*ERF(omega*Rc) &
    1961              :                                 + twopi*Rc2*ERF(omega*Rc) &
    1962            2 :                                 + 2*rootpi*Rc*EXP(-omega2*Rc2)/omega
    1963              :                END IF
    1964              : 
    1965              :             CASE DEFAULT
    1966            8 :                CPABORT("Please specify a valid operator for the periodic Poisson solver")
    1967              :             END SELECT
    1968              : 
    1969              :             ! The analytic Poisson solver simply limits the domain of integration
    1970              :             ! of the Fourier transform to a sphere of radius Rc, rather than integrating
    1971              :             ! over all space (-∞,∞)
    1972              :          CASE (ANALYTIC0D)
    1973              : 
    1974          114 :             SELECT CASE (eri_env%operator)
    1975              :                ! This is identical to the truncated Coulomb operator integrated
    1976              :                ! over all space, when the truncation radius is equal to the radius of
    1977              :                ! the Poisson solver
    1978              :             CASE (eri_operator_coulomb, eri_operator_trunc)
    1979           50 :                IF (eri_env%operator == eri_operator_coulomb) THEN
    1980           50 :                   Rc = green%radius
    1981              :                ELSE
    1982            0 :                   Rc = eri_env%cutoff_radius
    1983              :                END IF
    1984     16336912 :                DO ig = grid%first_gne0, grid%ngpts_cut_local
    1985     16336862 :                   g2 = grid%gsq(ig)
    1986     16336862 :                   g = SQRT(g2)
    1987              :                   ! Taylor expansion around zero
    1988     16336912 :                   IF (g*Rc >= 0.005_dp) THEN
    1989     16336862 :                      gf%array(ig) = fourpi/g2*(1.0_dp - COS(g*Rc))
    1990              :                   ELSE
    1991            0 :                      gf%array(ig) = fourpi/g2*(g*Rc)**2/2.0_dp*(1.0_dp - (g*Rc)**2/12.0_dp)
    1992              :                   END IF
    1993              :                END DO
    1994           50 :                IF (grid%have_g0) gf%array(1) = twopi*Rc**2
    1995              : 
    1996              :                ! Not tested
    1997              :             CASE (eri_operator_yukawa)
    1998            0 :                CALL cp_warn(__LOCATION__, "Yukawa operator has not been tested")
    1999            0 :                Rc = green%radius
    2000            0 :                omega = eri_env%omega
    2001            0 :                ea = EXP(-omega*Rc)
    2002            0 :                DO ig = grid%first_gne0, grid%ngpts_cut_local
    2003            0 :                   g2 = grid%gsq(ig)
    2004            0 :                   g = SQRT(g2)
    2005            0 :                   g3d = fourpi/(omega**2 + g2)
    2006            0 :                   gf%array(ig) = g3d*(1.0_dp - ea*(COS(g*Rc) + omega/g*SIN(g*Rc)))
    2007              :                END DO
    2008            0 :                IF (grid%have_g0) gf%array(1) = fourpi/(omega**2)*(1.0_dp - ea*(1.0_dp + omega*Rc))
    2009              : 
    2010              :                ! Long-range Coulomb
    2011              :                ! TODO: this should be equivalent to LR truncated Coulomb from above!
    2012              :             CASE (eri_operator_erf, eri_operator_lr_trunc)
    2013           14 :                IF (eri_env%operator == eri_operator_erf) THEN
    2014           14 :                   Rc = green%radius
    2015              :                ELSE
    2016            0 :                   Rc = eri_env%cutoff_radius
    2017              :                END IF
    2018           14 :                omega2 = eri_env%omega**2
    2019      1512007 :                DO ig = grid%first_gne0, grid%ngpts_cut_local
    2020      1511993 :                   g2 = grid%gsq(ig)
    2021      1511993 :                   g = SQRT(g2)
    2022      1511993 :                   ga = -0.25_dp*g2/omega2
    2023      1512007 :                   gf%array(ig) = fourpi/g2*EXP(ga)*(1.0_dp - COS(g*Rc))
    2024              :                END DO
    2025           14 :                IF (grid%have_g0) gf%array(1) = twopi*Rc**2
    2026              : 
    2027              :                ! Short-range Coulomb
    2028              :                ! TODO: this should actually be properly derived and see whether it is correct
    2029              :             CASE (eri_operator_erfc)
    2030              :                CALL cp_warn(__LOCATION__, &
    2031            0 :                             "Short-range Coulomb operator may be incorrect with ANALYTIC0D Poisson solver")
    2032            0 :                Rc = green%radius
    2033            0 :                omega2 = eri_env%omega**2
    2034            0 :                DO ig = grid%first_gne0, grid%ngpts_cut_local
    2035            0 :                   g2 = grid%gsq(ig)
    2036            0 :                   g = SQRT(g2)
    2037            0 :                   ga = -0.25_dp*g2/omega2
    2038            0 :                   gf%array(ig) = fourpi/g2*(1.0_dp - EXP(ga))*(1.0_dp - COS(g*Rc))
    2039              :                END DO
    2040            0 :                IF (grid%have_g0) gf%array(1) = pi/omega2
    2041              : 
    2042              :             CASE DEFAULT
    2043           64 :                CPABORT("Unsupported operator")
    2044              :             END SELECT
    2045              : 
    2046              :          CASE DEFAULT
    2047           72 :             CPABORT("Unsupported Poisson solver")
    2048              :          END SELECT
    2049              :       END ASSOCIATE
    2050              : 
    2051           72 :    END SUBROUTINE pw_eri_green_create
    2052              : 
    2053              : ! **************************************************************************************************
    2054              : !> \brief Adds data for a new row to the csr matrix
    2055              : !> \param csr_mat ...
    2056              : !> \param nnz ...
    2057              : !> \param rdat ...
    2058              : !> \param rind ...
    2059              : !> \param irow ...
    2060              : !> \par History
    2061              : !>      04.2016 created [JGH]
    2062              : ! **************************************************************************************************
    2063          505 :    SUBROUTINE update_csr_matrix(csr_mat, nnz, rdat, rind, irow)
    2064              : 
    2065              :       TYPE(dbcsr_csr_type), INTENT(INOUT)                :: csr_mat
    2066              :       INTEGER, INTENT(IN)                                :: nnz
    2067              :       REAL(KIND=dp), DIMENSION(:), INTENT(IN)            :: rdat
    2068              :       INTEGER, DIMENSION(:), INTENT(IN)                  :: rind
    2069              :       INTEGER, INTENT(IN)                                :: irow
    2070              : 
    2071              :       INTEGER                                            :: k, nrow, nze, nze_new
    2072              : 
    2073          505 :       IF (irow /= 0) THEN
    2074          505 :          nze = csr_mat%nze_local
    2075          505 :          nze_new = nze + nnz
    2076              :          ! values
    2077          505 :          CALL reallocate(csr_mat%nzval_local%r_dp, 1, nze_new)
    2078         1556 :          csr_mat%nzval_local%r_dp(nze + 1:nze_new) = rdat(1:nnz)
    2079              :          ! col indices
    2080          505 :          CALL reallocate(csr_mat%colind_local, 1, nze_new)
    2081         1556 :          csr_mat%colind_local(nze + 1:nze_new) = rind(1:nnz)
    2082              :          ! rows
    2083          505 :          nrow = csr_mat%nrows_local
    2084          505 :          CALL reallocate(csr_mat%rowptr_local, 1, irow + 1)
    2085         1348 :          csr_mat%rowptr_local(nrow + 1:irow) = nze + 1
    2086          505 :          csr_mat%rowptr_local(irow + 1) = nze_new + 1
    2087              :          ! nzerow
    2088          505 :          CALL reallocate(csr_mat%nzerow_local, 1, irow)
    2089         1348 :          DO k = nrow + 1, irow
    2090         1348 :             csr_mat%nzerow_local(k) = csr_mat%rowptr_local(k + 1) - csr_mat%rowptr_local(k)
    2091              :          END DO
    2092          505 :          csr_mat%nrows_local = irow
    2093          505 :          csr_mat%nze_local = csr_mat%nze_local + nnz
    2094              :       END IF
    2095          505 :       csr_mat%nze_total = csr_mat%nze_total + nnz
    2096          505 :       csr_mat%has_indices = .TRUE.
    2097              : 
    2098          505 :    END SUBROUTINE update_csr_matrix
    2099              : 
    2100              : ! **************************************************************************************************
    2101              : !> \brief Computes and prints the active orbitals on Cube Files
    2102              : !> \param input ...
    2103              : !> \param qs_env the qs_env in which the qs_env lives
    2104              : !> \param mos ...
    2105              : ! **************************************************************************************************
    2106            4 :    SUBROUTINE print_orbital_cubes(input, qs_env, mos)
    2107              :       TYPE(section_vals_type), POINTER                   :: input
    2108              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    2109              :       TYPE(mo_set_type), DIMENSION(:), INTENT(IN)        :: mos
    2110              : 
    2111              :       CHARACTER(LEN=default_path_length)                 :: filebody, filename, title
    2112              :       INTEGER                                            :: i, imo, isp, nmo, str(3), unit_nr
    2113            4 :       INTEGER, DIMENSION(:), POINTER                     :: alist, blist, istride
    2114              :       LOGICAL                                            :: do_mo, explicit_a, explicit_b
    2115            4 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
    2116              :       TYPE(cell_type), POINTER                           :: cell
    2117              :       TYPE(cp_fm_type), POINTER                          :: mo_coeff
    2118              :       TYPE(dft_control_type), POINTER                    :: dft_control
    2119              :       TYPE(mp_para_env_type), POINTER                    :: para_env
    2120              :       TYPE(particle_list_type), POINTER                  :: particles
    2121            4 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
    2122              :       TYPE(pw_c1d_gs_type)                               :: wf_g
    2123              :       TYPE(pw_env_type), POINTER                         :: pw_env
    2124              :       TYPE(pw_pool_type), POINTER                        :: auxbas_pw_pool
    2125              :       TYPE(pw_r3d_rs_type)                               :: wf_r
    2126            4 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
    2127              :       TYPE(qs_subsys_type), POINTER                      :: subsys
    2128              :       TYPE(section_vals_type), POINTER                   :: dft_section, scf_input
    2129              : 
    2130            4 :       CALL section_vals_val_get(input, "FILENAME", c_val=filebody)
    2131            4 :       CALL section_vals_val_get(input, "STRIDE", i_vals=istride)
    2132            4 :       IF (SIZE(istride) == 1) THEN
    2133           16 :          str(1:3) = istride(1)
    2134            0 :       ELSEIF (SIZE(istride) == 3) THEN
    2135            0 :          str(1:3) = istride(1:3)
    2136              :       ELSE
    2137            0 :          CPABORT("STRIDE arguments inconsistent")
    2138              :       END IF
    2139            4 :       CALL section_vals_val_get(input, "ALIST", i_vals=alist, explicit=explicit_a)
    2140            4 :       CALL section_vals_val_get(input, "BLIST", i_vals=blist, explicit=explicit_b)
    2141              : 
    2142              :       CALL get_qs_env(qs_env=qs_env, &
    2143              :                       dft_control=dft_control, &
    2144              :                       para_env=para_env, &
    2145              :                       subsys=subsys, &
    2146              :                       atomic_kind_set=atomic_kind_set, &
    2147              :                       qs_kind_set=qs_kind_set, &
    2148              :                       cell=cell, &
    2149              :                       particle_set=particle_set, &
    2150              :                       pw_env=pw_env, &
    2151            4 :                       input=scf_input)
    2152              : 
    2153            4 :       CALL qs_subsys_get(subsys, particles=particles)
    2154              :       !
    2155            4 :       CALL pw_env_get(pw_env, auxbas_pw_pool=auxbas_pw_pool)
    2156            4 :       CALL auxbas_pw_pool%create_pw(wf_r)
    2157            4 :       CALL auxbas_pw_pool%create_pw(wf_g)
    2158              :       !
    2159            4 :       dft_section => section_vals_get_subs_vals(scf_input, "DFT")
    2160              :       !
    2161            8 :       DO isp = 1, SIZE(mos)
    2162            4 :          CALL get_mo_set(mo_set=mos(isp), mo_coeff=mo_coeff, nmo=nmo)
    2163              : 
    2164            4 :          IF (SIZE(mos) > 1) THEN
    2165            0 :             SELECT CASE (isp)
    2166              :             CASE (1)
    2167              :                CALL write_mo_set_to_output_unit(mos(isp), qs_kind_set, particle_set, &
    2168            0 :                                                 dft_section, 4, 0, final_mos=.TRUE., spin="ALPHA")
    2169              :             CASE (2)
    2170              :                CALL write_mo_set_to_output_unit(mos(isp), qs_kind_set, particle_set, &
    2171            0 :                                                 dft_section, 4, 0, final_mos=.TRUE., spin="BETA")
    2172              :             CASE DEFAULT
    2173            0 :                CPABORT("Invalid spin")
    2174              :             END SELECT
    2175              :          ELSE
    2176              :             CALL write_mo_set_to_output_unit(mos(isp), qs_kind_set, particle_set, &
    2177            4 :                                              dft_section, 4, 0, final_mos=.TRUE.)
    2178              :          END IF
    2179              : 
    2180           44 :          DO imo = 1, nmo
    2181           32 :             IF (isp == 1 .AND. explicit_a) THEN
    2182           32 :                IF (alist(1) == -1) THEN
    2183              :                   do_mo = .TRUE.
    2184              :                ELSE
    2185           32 :                   do_mo = .FALSE.
    2186          128 :                   DO i = 1, SIZE(alist)
    2187          128 :                      IF (imo == alist(i)) do_mo = .TRUE.
    2188              :                   END DO
    2189              :                END IF
    2190            0 :             ELSE IF (isp == 2 .AND. explicit_b) THEN
    2191            0 :                IF (blist(1) == -1) THEN
    2192              :                   do_mo = .TRUE.
    2193              :                ELSE
    2194            0 :                   do_mo = .FALSE.
    2195            0 :                   DO i = 1, SIZE(blist)
    2196            0 :                      IF (imo == blist(i)) do_mo = .TRUE.
    2197              :                   END DO
    2198              :                END IF
    2199              :             ELSE
    2200              :                do_mo = .TRUE.
    2201              :             END IF
    2202           32 :             IF (.NOT. do_mo) CYCLE
    2203              :             CALL calculate_wavefunction(mo_coeff, imo, wf_r, wf_g, atomic_kind_set, &
    2204           12 :                                         qs_kind_set, cell, dft_control, particle_set, pw_env)
    2205           12 :             IF (para_env%is_source()) THEN
    2206            6 :                WRITE (filename, '(A,A1,I4.4,A1,I1.1,A)') TRIM(filebody), "_", imo, "_", isp, ".cube"
    2207            6 :                CALL open_file(filename, unit_number=unit_nr, file_status="UNKNOWN", file_action="WRITE")
    2208            6 :                WRITE (title, *) "Active Orbital ", imo, " spin ", isp
    2209              :             ELSE
    2210            6 :                unit_nr = -1
    2211              :             END IF
    2212           12 :             CALL cp_pw_to_cube(wf_r, unit_nr, title, particles=particles, stride=istride)
    2213           16 :             IF (para_env%is_source()) THEN
    2214           26 :                CALL close_file(unit_nr)
    2215              :             END IF
    2216              :          END DO
    2217              :       END DO
    2218              : 
    2219            4 :       CALL auxbas_pw_pool%give_back_pw(wf_r)
    2220            4 :       CALL auxbas_pw_pool%give_back_pw(wf_g)
    2221              : 
    2222            4 :    END SUBROUTINE print_orbital_cubes
    2223              : 
    2224              : ! **************************************************************************************************
    2225              : !> \brief Writes a FCIDUMP file
    2226              : !> \param active_space_env ...
    2227              : !> \param as_input ...
    2228              : !> \param restricted ...
    2229              : !> \par History
    2230              : !>      04.2016 created [JGH]
    2231              : ! **************************************************************************************************
    2232           72 :    SUBROUTINE fcidump(active_space_env, as_input, restricted)
    2233              : 
    2234              :       TYPE(active_space_type), POINTER                   :: active_space_env
    2235              :       TYPE(section_vals_type), POINTER                   :: as_input
    2236              :       LOGICAL, INTENT(IN)                                :: restricted
    2237              : 
    2238              :       INTEGER                                            :: i, i1, i2, i3, i4, isym, iw, m1, m2, &
    2239              :                                                             nmo, norb, nspins
    2240              :       REAL(KIND=dp)                                      :: checksum, esub
    2241           72 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)        :: fmat
    2242              :       TYPE(cp_logger_type), POINTER                      :: logger
    2243              :       TYPE(eri_fcidump_checksum)                         :: eri_checksum
    2244              : 
    2245           72 :       checksum = 0.0_dp
    2246              : 
    2247          144 :       logger => cp_get_default_logger()
    2248              :       iw = cp_print_key_unit_nr(logger, as_input, "FCIDUMP", &
    2249           72 :                                 extension=".fcidump", file_status="REPLACE", file_action="WRITE", file_form="FORMATTED")
    2250              :       !
    2251           72 :       nspins = active_space_env%nspins
    2252           72 :       norb = SIZE(active_space_env%active_orbitals, 1)
    2253           72 :       IF (nspins == 1 .OR. restricted) THEN
    2254              :          ! Closed shell or restricted open-shell
    2255              :          ASSOCIATE (ms2 => active_space_env%multiplicity, &
    2256              :                     nelec => active_space_env%nelec_active)
    2257              : 
    2258           60 :             IF (iw > 0) THEN
    2259           30 :                WRITE (iw, "(A,A,I4,A,I4,A,I2,A)") "&FCI", " NORB=", norb, ",NELEC=", nelec, ",MS2=", ms2, ","
    2260           30 :                isym = 1
    2261          113 :                WRITE (iw, "(A,1000(I1,','))") "  ORBSYM=", (isym, i=1, norb)
    2262           30 :                isym = 0
    2263           30 :                WRITE (iw, "(A,I1,A)") "  ISYM=", isym, ","
    2264           30 :                IF (restricted) WRITE (iw, "(A,I1,A)") "  UHF=", 0, ","
    2265           30 :                WRITE (iw, "(A)") " /"
    2266              :             END IF
    2267              :             !
    2268              :             ! Print integrals: ERI
    2269              :             CALL active_space_env%eri%eri_foreach(1, active_space_env%active_orbitals, &
    2270           60 :                                                   eri_fcidump_print(iw, 1, 1), 1, 1)
    2271           60 :             CALL eri_checksum%set(1, 1)
    2272           60 :             CALL active_space_env%eri%eri_foreach(1, active_space_env%active_orbitals, eri_checksum, 1, 1)
    2273              : 
    2274              :             ! Print integrals: Fij
    2275              :             ! replicate Fock matrix
    2276           60 :             nmo = active_space_env%eri%norb
    2277          240 :             ALLOCATE (fmat(nmo, nmo))
    2278           60 :             CALL replicate_and_symmetrize_matrix(nmo, active_space_env%fock_sub(1), fmat)
    2279           60 :             IF (iw > 0) THEN
    2280           30 :                i3 = 0; i4 = 0
    2281          113 :                DO m1 = 1, SIZE(active_space_env%active_orbitals, 1)
    2282           83 :                   i1 = active_space_env%active_orbitals(m1, 1)
    2283          289 :                   DO m2 = m1, SIZE(active_space_env%active_orbitals, 1)
    2284          176 :                      i2 = active_space_env%active_orbitals(m2, 1)
    2285          176 :                      checksum = checksum + ABS(fmat(i1, i2))
    2286          259 :                      WRITE (iw, "(ES23.16,4I4)") fmat(i1, i2), m1, m2, i3, i4
    2287              :                   END DO
    2288              :                END DO
    2289              :             END IF
    2290           60 :             DEALLOCATE (fmat)
    2291              :             ! Print energy
    2292           60 :             esub = active_space_env%energy_inactive
    2293           60 :             i1 = 0; i2 = 0; i3 = 0; i4 = 0
    2294           60 :             checksum = checksum + ABS(esub)
    2295          120 :             IF (iw > 0) WRITE (iw, "(ES23.16,4I4)") esub, i1, i2, i3, i4
    2296              :          END ASSOCIATE
    2297              : 
    2298              :       ELSE
    2299              :          ASSOCIATE (ms2 => active_space_env%multiplicity, &
    2300              :                     nelec => active_space_env%nelec_active)
    2301              : 
    2302           12 :             IF (iw > 0) THEN
    2303            6 :                WRITE (iw, "(A,A,I4,A,I4,A,I2,A)") "&FCI", " NORB=", norb, ",NELEC=", nelec, ",MS2=", ms2, ","
    2304            6 :                isym = 1
    2305           21 :                WRITE (iw, "(A,1000(I1,','))") "  ORBSYM=", (isym, i=1, norb)
    2306            6 :                isym = 0
    2307            6 :                WRITE (iw, "(A,I1,A)") "  ISYM=", isym, ","
    2308            6 :                WRITE (iw, "(A,I1,A)") "  UHF=", 1, ","
    2309            6 :                WRITE (iw, "(A)") " /"
    2310              :             END IF
    2311              :             !
    2312              :             ! Print integrals: ERI
    2313              :             ! alpha-alpha
    2314              :             CALL active_space_env%eri%eri_foreach(1, active_space_env%active_orbitals, &
    2315           12 :                                                   eri_fcidump_print(iw, 1, 1), 1, 1)
    2316           12 :             CALL eri_checksum%set(1, 1)
    2317           12 :             CALL active_space_env%eri%eri_foreach(1, active_space_env%active_orbitals, eri_checksum, 1, 1)
    2318              :             ! alpha-beta
    2319              :             CALL active_space_env%eri%eri_foreach(2, active_space_env%active_orbitals, &
    2320           12 :                                                   eri_fcidump_print(iw, 1, norb + 1), 1, 2)
    2321           12 :             CALL eri_checksum%set(1, norb + 1)
    2322           12 :             CALL active_space_env%eri%eri_foreach(2, active_space_env%active_orbitals, eri_checksum, 1, 2)
    2323              :             ! beta-beta
    2324              :             CALL active_space_env%eri%eri_foreach(3, active_space_env%active_orbitals, &
    2325           12 :                                                   eri_fcidump_print(iw, norb + 1, norb + 1), 2, 2)
    2326           12 :             CALL eri_checksum%set(norb + 1, norb + 1)
    2327           12 :             CALL active_space_env%eri%eri_foreach(3, active_space_env%active_orbitals, eri_checksum, 2, 2)
    2328              :             ! Print integrals: Fij
    2329              :             ! alpha
    2330           12 :             nmo = active_space_env%eri%norb
    2331           48 :             ALLOCATE (fmat(nmo, nmo))
    2332           12 :             CALL replicate_and_symmetrize_matrix(nmo, active_space_env%fock_sub(1), fmat)
    2333           12 :             IF (iw > 0) THEN
    2334            6 :                i3 = 0; i4 = 0
    2335           21 :                DO m1 = 1, norb
    2336           15 :                   i1 = active_space_env%active_orbitals(m1, 1)
    2337           48 :                   DO m2 = m1, norb
    2338           27 :                      i2 = active_space_env%active_orbitals(m2, 1)
    2339           27 :                      checksum = checksum + ABS(fmat(i1, i2))
    2340           42 :                      WRITE (iw, "(ES23.16,4I4)") fmat(i1, i2), m1, m2, i3, i4
    2341              :                   END DO
    2342              :                END DO
    2343              :             END IF
    2344           12 :             DEALLOCATE (fmat)
    2345              :             ! beta
    2346           48 :             ALLOCATE (fmat(nmo, nmo))
    2347           12 :             CALL replicate_and_symmetrize_matrix(nmo, active_space_env%fock_sub(2), fmat)
    2348           12 :             IF (iw > 0) THEN
    2349            6 :                i3 = 0; i4 = 0
    2350           21 :                DO m1 = 1, SIZE(active_space_env%active_orbitals, 1)
    2351           15 :                   i1 = active_space_env%active_orbitals(m1, 2)
    2352           48 :                   DO m2 = m1, SIZE(active_space_env%active_orbitals, 1)
    2353           27 :                      i2 = active_space_env%active_orbitals(m2, 2)
    2354           27 :                      checksum = checksum + ABS(fmat(i1, i2))
    2355           42 :                      WRITE (iw, "(ES23.16,4I4)") fmat(i1, i2), m1 + norb, m2 + norb, i3, i4
    2356              :                   END DO
    2357              :                END DO
    2358              :             END IF
    2359           12 :             DEALLOCATE (fmat)
    2360              :             ! Print energy
    2361           12 :             esub = active_space_env%energy_inactive
    2362           12 :             i1 = 0; i2 = 0; i3 = 0; i4 = 0
    2363           12 :             checksum = checksum + ABS(esub)
    2364           24 :             IF (iw > 0) WRITE (iw, "(ES23.16,4I4)") esub, i1, i2, i3, i4
    2365              :          END ASSOCIATE
    2366              :       END IF
    2367              :       !
    2368           72 :       CALL cp_print_key_finished_output(iw, logger, as_input, "FCIDUMP")
    2369              : 
    2370              :       !>>
    2371           72 :       iw = cp_logger_get_default_io_unit(logger)
    2372           72 :       IF (iw > 0) WRITE (iw, '(T4,A,T66,F12.8)') "FCIDUMP| Checksum:", eri_checksum%checksum + checksum
    2373              :       !<<
    2374              : 
    2375          144 :    END SUBROUTINE fcidump
    2376              : 
    2377              : ! **************************************************************************************************
    2378              : !> \brief replicate and symmetrize a matrix
    2379              : !> \param norb the number of orbitals
    2380              : !> \param distributed_matrix ...
    2381              : !> \param replicated_matrix ...
    2382              : ! **************************************************************************************************
    2383          256 :    SUBROUTINE replicate_and_symmetrize_matrix(norb, distributed_matrix, replicated_matrix)
    2384              :       INTEGER, INTENT(IN)                                :: norb
    2385              :       TYPE(cp_fm_type), INTENT(IN)                       :: distributed_matrix
    2386              :       REAL(dp), DIMENSION(:, :), INTENT(INOUT)           :: replicated_matrix
    2387              : 
    2388              :       INTEGER                                            :: i1, i2
    2389              :       REAL(dp)                                           :: mval
    2390              : 
    2391         4672 :       replicated_matrix(:, :) = 0.0_dp
    2392         1116 :       DO i1 = 1, norb
    2393         3324 :          DO i2 = i1, norb
    2394         2208 :             CALL cp_fm_get_element(distributed_matrix, i1, i2, mval)
    2395         2208 :             replicated_matrix(i1, i2) = mval
    2396         3068 :             replicated_matrix(i2, i1) = mval
    2397              :          END DO
    2398              :       END DO
    2399          256 :    END SUBROUTINE replicate_and_symmetrize_matrix
    2400              : 
    2401              : ! **************************************************************************************************
    2402              : !> \brief Calculates active space Fock matrix and inactive energy
    2403              : !> \param active_space_env ...
    2404              : !> \param restricted ...
    2405              : !> \par History
    2406              : !>      06.2016 created [JGH]
    2407              : ! **************************************************************************************************
    2408           72 :    SUBROUTINE subspace_fock_matrix(active_space_env, restricted)
    2409              : 
    2410              :       TYPE(active_space_type), POINTER                   :: active_space_env
    2411              :       LOGICAL, INTENT(IN)                                :: restricted
    2412              : 
    2413              :       INTEGER                                            :: i1, i2, is, norb, nspins
    2414              :       REAL(KIND=dp)                                      :: eeri, eref, esub, mval
    2415           72 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)        :: ks_a_mat, ks_a_ref, ks_b_mat, ks_b_ref, &
    2416           72 :                                                             ks_mat, ks_ref, p_a_mat, p_b_mat, p_mat
    2417              :       TYPE(cp_fm_type), POINTER                          :: matrix, mo_coef
    2418              :       TYPE(dbcsr_csr_type), POINTER                      :: eri, eri_aa, eri_ab, eri_bb
    2419              : 
    2420           72 :       eref = active_space_env%energy_ref
    2421           72 :       nspins = active_space_env%nspins
    2422              : 
    2423           72 :       IF (nspins == 1) THEN
    2424           58 :          CALL get_mo_set(active_space_env%mos_active(1), nmo=norb, mo_coeff=mo_coef)
    2425              :          !
    2426              :          ! Loop over ERI, calculate subspace HF energy and Fock matrix
    2427              :          !
    2428              :          ! replicate KS, Core, and P matrices
    2429          580 :          ALLOCATE (ks_mat(norb, norb), ks_ref(norb, norb), p_mat(norb, norb))
    2430          982 :          ks_ref = 0.0_dp
    2431              : 
    2432              :          ! ks_mat contains the KS/Fock matrix (of full density) projected onto the AS MO subspace (f_ref in eq. 19)
    2433           58 :          CALL replicate_and_symmetrize_matrix(norb, active_space_env%ks_sub(1), ks_mat)
    2434           58 :          CALL replicate_and_symmetrize_matrix(norb, active_space_env%p_active(1), p_mat)
    2435              : 
    2436              :          ! compute ks_ref = V_H[rho^A] + V_HFX[rho^A]
    2437           58 :          eri => active_space_env%eri%eri(1)%csr_mat
    2438              :          CALL build_subspace_fock_matrix(active_space_env%active_orbitals, eri, p_mat, ks_ref, &
    2439           58 :                                          active_space_env%eri%comm_exchange)
    2440              : 
    2441              :          ! compute eeri = E_H[rho^A] + E_HFX[rho^A] as
    2442              :          ! eeri = 1/2 * (SUM_pq (V_H[rho^A] + V_HFX[rho^A])_pq * D^A_pq)
    2443          982 :          eeri = 0.5_dp*SUM(ks_ref*p_mat)
    2444              : 
    2445              :          ! now calculate the inactive energy acoording to eq. 19, that is
    2446              :          ! esub = E^I = E_ref - f_ref .* D^A + E_H[rho^A] + E_HFX[rho^A]
    2447              :          ! where f^ref = ks_mat, which is the KS/Fock matrix in MO basis, transformed previously
    2448              :          ! and is equal to ks_mat = h^0 + V_core + V_H[rho] + V_HFX[rho]
    2449          982 :          esub = eref - SUM(ks_mat(1:norb, 1:norb)*p_mat(1:norb, 1:norb)) + eeri
    2450              : 
    2451              :          ! reuse ks_mat to store f^I = f^ref - (V_H[rho^A] + V_HFX[rho^A]) according to eq. 20
    2452          982 :          ks_mat(1:norb, 1:norb) = ks_mat(1:norb, 1:norb) - ks_ref(1:norb, 1:norb)
    2453              :          ! this is now the embedding potential for the AS calculation!
    2454              : 
    2455           58 :          active_space_env%energy_inactive = esub
    2456              : 
    2457           58 :          CALL cp_fm_release(active_space_env%fock_sub)
    2458          232 :          ALLOCATE (active_space_env%fock_sub(nspins))
    2459          116 :          DO is = 1, nspins
    2460           58 :             matrix => active_space_env%ks_sub(is)
    2461              :             CALL cp_fm_create(active_space_env%fock_sub(is), matrix%matrix_struct, &
    2462          116 :                               name="Active Fock operator")
    2463              :          END DO
    2464           58 :          matrix => active_space_env%fock_sub(1)
    2465          300 :          DO i1 = 1, norb
    2466          982 :             DO i2 = 1, norb
    2467          740 :                mval = ks_mat(i1, i2)
    2468          924 :                CALL cp_fm_set_element(matrix, i1, i2, mval)
    2469              :             END DO
    2470              :          END DO
    2471              :       ELSE
    2472              : 
    2473           14 :          CALL get_mo_set(active_space_env%mos_active(1), nmo=norb)
    2474              :          !
    2475              :          ! Loop over ERI, calculate subspace HF energy and Fock matrix
    2476              :          !
    2477              :          ! replicate KS, Core, and P matrices
    2478              :          ALLOCATE (ks_a_mat(norb, norb), ks_b_mat(norb, norb), &
    2479              :               &    ks_a_ref(norb, norb), ks_b_ref(norb, norb), &
    2480          266 :               &     p_a_mat(norb, norb), p_b_mat(norb, norb))
    2481          580 :          ks_a_ref(:, :) = 0.0_dp; ks_b_ref(:, :) = 0.0_dp
    2482              : 
    2483           14 :          CALL replicate_and_symmetrize_matrix(norb, active_space_env%p_active(1), p_a_mat)
    2484           14 :          CALL replicate_and_symmetrize_matrix(norb, active_space_env%p_active(2), p_b_mat)
    2485           14 :          CALL replicate_and_symmetrize_matrix(norb, active_space_env%ks_sub(1), ks_a_mat)
    2486           14 :          CALL replicate_and_symmetrize_matrix(norb, active_space_env%ks_sub(2), ks_b_mat)
    2487              :          !
    2488              :          !
    2489           14 :          IF (restricted) THEN
    2490              :             ! In the restricted case, we use the same ERIs for each spin channel
    2491            2 :             eri_aa => active_space_env%eri%eri(1)%csr_mat
    2492              :             CALL build_subspace_spin_fock_matrix(active_space_env%active_orbitals, eri_aa, eri_aa, p_a_mat, p_b_mat, ks_a_ref, &
    2493            2 :                                                  tr_mixed_eri=.FALSE., comm_exchange=active_space_env%eri%comm_exchange)
    2494              :             CALL build_subspace_spin_fock_matrix(active_space_env%active_orbitals, eri_aa, eri_aa, p_b_mat, p_a_mat, ks_b_ref, &
    2495            2 :                                                  tr_mixed_eri=.TRUE., comm_exchange=active_space_env%eri%comm_exchange)
    2496              :          ELSE
    2497           12 :             eri_aa => active_space_env%eri%eri(1)%csr_mat
    2498           12 :             eri_ab => active_space_env%eri%eri(2)%csr_mat
    2499           12 :             eri_bb => active_space_env%eri%eri(3)%csr_mat
    2500              :             CALL build_subspace_spin_fock_matrix(active_space_env%active_orbitals, eri_aa, eri_ab, p_a_mat, p_b_mat, ks_a_ref, &
    2501           12 :                                                  tr_mixed_eri=.FALSE., comm_exchange=active_space_env%eri%comm_exchange)
    2502              :             CALL build_subspace_spin_fock_matrix(active_space_env%active_orbitals, eri_bb, eri_ab, p_b_mat, p_a_mat, ks_b_ref, &
    2503           12 :                                                  tr_mixed_eri=.TRUE., comm_exchange=active_space_env%eri%comm_exchange)
    2504              :          END IF
    2505              :          !
    2506              :          ! calculate energy
    2507           14 :          eeri = 0.0_dp
    2508          580 :          eeri = 0.5_dp*(SUM(ks_a_ref*p_a_mat) + SUM(ks_b_ref*p_b_mat))
    2509          580 :          esub = eref - SUM(ks_a_mat*p_a_mat) - SUM(ks_b_mat*p_b_mat) + eeri
    2510          290 :          ks_a_mat(:, :) = ks_a_mat(:, :) - ks_a_ref(:, :)
    2511          290 :          ks_b_mat(:, :) = ks_b_mat(:, :) - ks_b_ref(:, :)
    2512              :          !
    2513           14 :          active_space_env%energy_inactive = esub
    2514              :          !
    2515           14 :          CALL cp_fm_release(active_space_env%fock_sub)
    2516           70 :          ALLOCATE (active_space_env%fock_sub(nspins))
    2517           42 :          DO is = 1, nspins
    2518           28 :             matrix => active_space_env%ks_sub(is)
    2519              :             CALL cp_fm_create(active_space_env%fock_sub(is), matrix%matrix_struct, &
    2520           42 :                               name="Active Fock operator")
    2521              :          END DO
    2522              : 
    2523           14 :          matrix => active_space_env%fock_sub(1)
    2524           66 :          DO i1 = 1, norb
    2525          290 :             DO i2 = 1, norb
    2526          224 :                mval = ks_a_mat(i1, i2)
    2527          276 :                CALL cp_fm_set_element(matrix, i1, i2, mval)
    2528              :             END DO
    2529              :          END DO
    2530           14 :          matrix => active_space_env%fock_sub(2)
    2531           80 :          DO i1 = 1, norb
    2532          290 :             DO i2 = 1, norb
    2533          224 :                mval = ks_b_mat(i1, i2)
    2534          276 :                CALL cp_fm_set_element(matrix, i1, i2, mval)
    2535              :             END DO
    2536              :          END DO
    2537              : 
    2538              :       END IF
    2539              : 
    2540           72 :    END SUBROUTINE subspace_fock_matrix
    2541              : 
    2542              : ! **************************************************************************************************
    2543              : !> \brief build subspace fockian
    2544              : !> \param active_orbitals the active orbital indices
    2545              : !> \param eri two electon integrals in MO
    2546              : !> \param p_mat density matrix
    2547              : !> \param ks_ref fockian matrix
    2548              : !> \param comm_exchange ...
    2549              : ! **************************************************************************************************
    2550           58 :    SUBROUTINE build_subspace_fock_matrix(active_orbitals, eri, p_mat, ks_ref, comm_exchange)
    2551              :       INTEGER, DIMENSION(:, :), INTENT(IN)               :: active_orbitals
    2552              :       TYPE(dbcsr_csr_type), INTENT(IN)                   :: eri
    2553              :       REAL(dp), DIMENSION(:, :), INTENT(IN)              :: p_mat
    2554              :       REAL(dp), DIMENSION(:, :), INTENT(INOUT)           :: ks_ref
    2555              :       TYPE(mp_comm_type), INTENT(IN)                     :: comm_exchange
    2556              : 
    2557              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'build_subspace_fock_matrix'
    2558              : 
    2559              :       INTEGER                                            :: handle, i1, i12, i12l, i2, i3, i34, &
    2560              :                                                             i34l, i4, irptr, m1, m2, nindex, &
    2561              :                                                             nmo_total, norb
    2562              :       REAL(dp)                                           :: erint
    2563              :       TYPE(mp_comm_type)                                 :: mp_group
    2564              : 
    2565           58 :       CALL timeset(routineN, handle)
    2566              : 
    2567              :       ! Nothing to do
    2568           58 :       norb = SIZE(active_orbitals, 1)
    2569           58 :       nmo_total = SIZE(p_mat, 1)
    2570           58 :       nindex = (nmo_total*(nmo_total + 1))/2
    2571           58 :       CALL mp_group%set_handle(eri%mp_group%get_handle())
    2572          220 :       DO m1 = 1, norb
    2573          162 :          i1 = active_orbitals(m1, 1)
    2574          566 :          DO m2 = m1, norb
    2575          346 :             i2 = active_orbitals(m2, 1)
    2576          346 :             i12 = csr_idx_to_combined(i1, i2, nmo_total)
    2577          508 :             IF (MOD(i12 - 1, comm_exchange%num_pe) == comm_exchange%mepos) THEN
    2578          337 :                i12l = (i12 - 1)/comm_exchange%num_pe + 1
    2579          337 :                irptr = eri%rowptr_local(i12l) - 1
    2580         1111 :                DO i34l = 1, eri%nzerow_local(i12l)
    2581          774 :                   i34 = eri%colind_local(irptr + i34l)
    2582          774 :                   CALL csr_idx_from_combined(i34, nmo_total, i3, i4)
    2583          774 :                   erint = eri%nzval_local%r_dp(irptr + i34l)
    2584              :                   ! Coulomb
    2585          774 :                   ks_ref(i1, i2) = ks_ref(i1, i2) + erint*p_mat(i3, i4)
    2586          774 :                   IF (i3 /= i4) THEN
    2587          442 :                      ks_ref(i1, i2) = ks_ref(i1, i2) + erint*p_mat(i3, i4)
    2588              :                   END IF
    2589          774 :                   IF (i12 /= i34) THEN
    2590          601 :                      ks_ref(i3, i4) = ks_ref(i3, i4) + erint*p_mat(i1, i2)
    2591          601 :                      IF (i1 /= i2) THEN
    2592          411 :                         ks_ref(i3, i4) = ks_ref(i3, i4) + erint*p_mat(i1, i2)
    2593              :                      END IF
    2594              :                   END IF
    2595              :                   ! Exchange
    2596          774 :                   erint = -0.5_dp*erint
    2597          774 :                   ks_ref(i1, i3) = ks_ref(i1, i3) + erint*p_mat(i2, i4)
    2598          774 :                   IF (i1 /= i2) THEN
    2599          503 :                      ks_ref(i2, i3) = ks_ref(i2, i3) + erint*p_mat(i1, i4)
    2600              :                   END IF
    2601          774 :                   IF (i3 /= i4) THEN
    2602          442 :                      ks_ref(i1, i4) = ks_ref(i1, i4) + erint*p_mat(i2, i3)
    2603              :                   END IF
    2604         1885 :                   IF (i1 /= i2 .AND. i3 /= i4) THEN
    2605          344 :                      ks_ref(i2, i4) = ks_ref(i2, i4) + erint*p_mat(i1, i3)
    2606              :                   END IF
    2607              :                END DO
    2608              :             END IF
    2609              :          END DO
    2610              :       END DO
    2611              :       !
    2612          220 :       DO m1 = 1, norb
    2613          162 :          i1 = active_orbitals(m1, 1)
    2614          566 :          DO m2 = m1, norb
    2615          346 :             i2 = active_orbitals(m2, 1)
    2616          508 :             ks_ref(i2, i1) = ks_ref(i1, i2)
    2617              :          END DO
    2618              :       END DO
    2619         1906 :       CALL mp_group%sum(ks_ref)
    2620              : 
    2621           58 :       CALL timestop(handle)
    2622              : 
    2623           58 :    END SUBROUTINE build_subspace_fock_matrix
    2624              : 
    2625              : ! **************************************************************************************************
    2626              : !> \brief build subspace fockian for unrestricted spins
    2627              : !> \param active_orbitals the active orbital indices
    2628              : !> \param eri_aa two electon integrals in MO with parallel spins
    2629              : !> \param eri_ab two electon integrals in MO with anti-parallel spins
    2630              : !> \param p_a_mat density matrix for up-spin
    2631              : !> \param p_b_mat density matrix for down-spin
    2632              : !> \param ks_a_ref fockian matrix for up-spin
    2633              : !> \param tr_mixed_eri boolean to indicate Coulomb interaction alignment
    2634              : !> \param comm_exchange ...
    2635              : ! **************************************************************************************************
    2636           28 :    SUBROUTINE build_subspace_spin_fock_matrix(active_orbitals, eri_aa, eri_ab, p_a_mat, p_b_mat, ks_a_ref, tr_mixed_eri, &
    2637              :                                               comm_exchange)
    2638              :       INTEGER, DIMENSION(:, :), INTENT(IN)               :: active_orbitals
    2639              :       TYPE(dbcsr_csr_type), INTENT(IN)                   :: eri_aa, eri_ab
    2640              :       REAL(dp), DIMENSION(:, :), INTENT(IN)              :: p_a_mat, p_b_mat
    2641              :       REAL(dp), DIMENSION(:, :), INTENT(INOUT)           :: ks_a_ref
    2642              :       LOGICAL, INTENT(IN)                                :: tr_mixed_eri
    2643              :       TYPE(mp_comm_type), INTENT(IN)                     :: comm_exchange
    2644              : 
    2645              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'build_subspace_spin_fock_matrix'
    2646              : 
    2647              :       INTEGER                                            :: handle, i1, i12, i12l, i2, i3, i34, &
    2648              :                                                             i34l, i4, irptr, m1, m2, nindex, &
    2649              :                                                             nmo_total, norb, spin1, spin2
    2650              :       REAL(dp)                                           :: erint
    2651              :       TYPE(mp_comm_type)                                 :: mp_group
    2652              : 
    2653           28 :       CALL timeset(routineN, handle)
    2654              : 
    2655           28 :       norb = SIZE(active_orbitals, 1)
    2656           28 :       nmo_total = SIZE(p_a_mat, 1)
    2657           28 :       nindex = (nmo_total*(nmo_total + 1))/2
    2658           28 :       IF (tr_mixed_eri) THEN
    2659              :          spin1 = 2
    2660           28 :          spin2 = 1
    2661              :       ELSE
    2662           14 :          spin1 = 1
    2663           14 :          spin2 = 2
    2664              :       END IF
    2665           96 :       DO m1 = 1, norb
    2666           68 :          i1 = active_orbitals(m1, spin1)
    2667          216 :          DO m2 = m1, norb
    2668          120 :             i2 = active_orbitals(m2, spin1)
    2669          120 :             i12 = csr_idx_to_combined(i1, i2, nmo_total)
    2670          188 :             IF (MOD(i12 - 1, comm_exchange%num_pe) == comm_exchange%mepos) THEN
    2671          120 :                i12l = (i12 - 1)/comm_exchange%num_pe + 1
    2672          120 :                irptr = eri_aa%rowptr_local(i12l) - 1
    2673          281 :                DO i34l = 1, eri_aa%nzerow_local(i12l)
    2674          161 :                   i34 = eri_aa%colind_local(irptr + i34l)
    2675          161 :                   CALL csr_idx_from_combined(i34, nmo_total, i3, i4)
    2676          161 :                   erint = eri_aa%nzval_local%r_dp(irptr + i34l)
    2677              :                   ! Coulomb
    2678              :                   !F_ij += (ij|kl)*d_kl
    2679          161 :                   ks_a_ref(i1, i2) = ks_a_ref(i1, i2) + erint*p_a_mat(i3, i4)
    2680          161 :                   IF (i12 /= i34) THEN
    2681              :                      !F_kl += (ij|kl)*d_ij
    2682          101 :                      ks_a_ref(i3, i4) = ks_a_ref(i3, i4) + erint*p_a_mat(i1, i2)
    2683              :                   END IF
    2684              :                   ! Exchange
    2685          161 :                   erint = -1.0_dp*erint
    2686              :                   !F_ik -= (ij|kl)*d_jl
    2687          161 :                   ks_a_ref(i1, i3) = ks_a_ref(i1, i3) + erint*p_a_mat(i2, i4)
    2688          161 :                   IF (i1 /= i2) THEN
    2689              :                      !F_jk -= (ij|kl)*d_il
    2690           75 :                      ks_a_ref(i2, i3) = ks_a_ref(i2, i3) + erint*p_a_mat(i1, i4)
    2691              :                   END IF
    2692          161 :                   IF (i3 /= i4) THEN
    2693              :                      !F_il -= (ij|kl)*d_jk
    2694           70 :                      ks_a_ref(i1, i4) = ks_a_ref(i1, i4) + erint*p_a_mat(i2, i3)
    2695              :                   END IF
    2696          442 :                   IF (i1 /= i2 .AND. i3 /= i4) THEN
    2697              :                      !F_jl -= (ij|kl)*d_ik
    2698           44 :                      ks_a_ref(i2, i4) = ks_a_ref(i2, i4) + erint*p_a_mat(i1, i3)
    2699              :                   END IF
    2700              :                END DO
    2701              :             END IF
    2702              :          END DO
    2703              :       END DO
    2704              :       !
    2705              : 
    2706           96 :       DO m1 = 1, norb
    2707           68 :          i1 = active_orbitals(m1, 1)
    2708          216 :          DO m2 = m1, norb
    2709          120 :             i2 = active_orbitals(m2, 1)
    2710          120 :             i12 = csr_idx_to_combined(i1, i2, nmo_total)
    2711          188 :             IF (MOD(i12 - 1, comm_exchange%num_pe) == comm_exchange%mepos) THEN
    2712          120 :                i12l = (i12 - 1)/comm_exchange%num_pe + 1
    2713          120 :                irptr = eri_ab%rowptr_local(i12l) - 1
    2714          372 :                DO i34l = 1, eri_ab%nzerow_local(i12l)
    2715          252 :                   i34 = eri_ab%colind_local(irptr + i34l)
    2716          252 :                   CALL csr_idx_from_combined(i34, nmo_total, i3, i4)
    2717          252 :                   erint = eri_ab%nzval_local%r_dp(irptr + i34l)
    2718              :                   ! Coulomb
    2719          372 :                   IF (tr_mixed_eri) THEN
    2720              :                      !F_kl += (kl beta|ij alpha )*d_alpha_ij
    2721          126 :                      ks_a_ref(i3, i4) = ks_a_ref(i3, i4) + erint*p_b_mat(i1, i2)
    2722              :                   ELSE
    2723              :                      !F_ij += (ij alpha|kl beta )*d_beta_kl
    2724          126 :                      ks_a_ref(i1, i2) = ks_a_ref(i1, i2) + erint*p_b_mat(i3, i4)
    2725              :                   END IF
    2726              :                END DO
    2727              :             END IF
    2728              :          END DO
    2729              :       END DO
    2730              :       !
    2731           96 :       DO m1 = 1, norb
    2732           68 :          i1 = active_orbitals(m1, spin1)
    2733          216 :          DO m2 = m1, norb
    2734          120 :             i2 = active_orbitals(m2, spin1)
    2735          188 :             ks_a_ref(i2, i1) = ks_a_ref(i1, i2)
    2736              :          END DO
    2737              :       END DO
    2738           28 :       CALL mp_group%set_handle(eri_aa%mp_group%get_handle())
    2739         1132 :       CALL mp_group%sum(ks_a_ref)
    2740              : 
    2741           28 :       CALL timestop(handle)
    2742              : 
    2743           28 :    END SUBROUTINE build_subspace_spin_fock_matrix
    2744              : 
    2745              : ! **************************************************************************************************
    2746              : !> \brief Creates a local basis
    2747              : !> \param pro_basis_set ...
    2748              : !> \param zval ...
    2749              : !> \param ishell ...
    2750              : !> \param nshell ...
    2751              : !> \param lnam ...
    2752              : !> \par History
    2753              : !>      05.2016 created [JGH]
    2754              : ! **************************************************************************************************
    2755            0 :    SUBROUTINE create_pro_basis(pro_basis_set, zval, ishell, nshell, lnam)
    2756              :       TYPE(gto_basis_set_type), POINTER                  :: pro_basis_set
    2757              :       INTEGER, INTENT(IN)                                :: zval, ishell
    2758              :       INTEGER, DIMENSION(:), INTENT(IN)                  :: nshell
    2759              :       CHARACTER(len=*), DIMENSION(:), INTENT(IN)         :: lnam
    2760              : 
    2761            0 :       CHARACTER(len=6), DIMENSION(:), POINTER            :: sym
    2762              :       INTEGER                                            :: i, l, nj
    2763              :       INTEGER, DIMENSION(4, 7)                           :: ne
    2764            0 :       INTEGER, DIMENSION(:), POINTER                     :: lq, nq
    2765            0 :       REAL(KIND=dp), DIMENSION(:), POINTER               :: zet
    2766              :       TYPE(sto_basis_set_type), POINTER                  :: sto_basis_set
    2767              : 
    2768            0 :       CPASSERT(.NOT. ASSOCIATED(pro_basis_set))
    2769            0 :       NULLIFY (sto_basis_set)
    2770              : 
    2771              :       ! electronic configuration
    2772            0 :       ne = 0
    2773            0 :       DO l = 1, 4 !lq(1)+1
    2774            0 :          nj = 2*(l - 1) + 1
    2775            0 :          DO i = l, 7 ! nq(1)
    2776            0 :             ne(l, i) = ptable(zval)%e_conv(l - 1) - 2*nj*(i - l)
    2777            0 :             ne(l, i) = MAX(ne(l, i), 0)
    2778            0 :             ne(l, i) = MIN(ne(l, i), 2*nj)
    2779              :          END DO
    2780              :       END DO
    2781            0 :       ALLOCATE (nq(ishell), lq(ishell), zet(ishell), sym(ishell))
    2782            0 :       DO i = 1, ishell
    2783            0 :          nq(i) = nshell(i)
    2784            0 :          SELECT CASE (lnam(i))
    2785              :          CASE ('S', 's')
    2786            0 :             lq(i) = 0
    2787              :          CASE ('P', 'p')
    2788            0 :             lq(i) = 1
    2789              :          CASE ('D', 'd')
    2790            0 :             lq(i) = 2
    2791              :          CASE ('F', 'f')
    2792            0 :             lq(i) = 3
    2793              :          CASE DEFAULT
    2794            0 :             CPABORT("Wrong l QN")
    2795              :          END SELECT
    2796            0 :          sym(i) = lnam(i)
    2797            0 :          zet(i) = srules(zval, ne, nq(1), lq(1))
    2798              :       END DO
    2799            0 :       CALL allocate_sto_basis_set(sto_basis_set)
    2800            0 :       CALL set_sto_basis_set(sto_basis_set, nshell=1, nq=nq, lq=lq, zet=zet, symbol=sym)
    2801            0 :       CALL create_gto_from_sto_basis(sto_basis_set, pro_basis_set, 6)
    2802            0 :       pro_basis_set%norm_type = 2
    2803            0 :       CALL init_orb_basis_set(pro_basis_set)
    2804            0 :       CALL deallocate_sto_basis_set(sto_basis_set)
    2805              : 
    2806            0 :    END SUBROUTINE create_pro_basis
    2807              : 
    2808              : ! **************************************************************************************************
    2809              : !> \brief Update the density matrix in AO basis with the active density contribution
    2810              : !> \param active_space_env the active space environment
    2811              : !> \param rho_ao the density matrix in AO basis
    2812              : ! **************************************************************************************************
    2813            0 :    SUBROUTINE update_density_ao(active_space_env, rho_ao)
    2814              :       TYPE(active_space_type), POINTER                   :: active_space_env
    2815              :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: rho_ao
    2816              : 
    2817              :       INTEGER                                            :: ispin, nao, nmo, nspins
    2818              :       TYPE(cp_fm_type)                                   :: R, U
    2819              :       TYPE(cp_fm_type), POINTER                          :: C_active, p_active_mo
    2820              :       TYPE(dbcsr_type), POINTER                          :: p_inactive_ao
    2821            0 :       TYPE(mo_set_type), DIMENSION(:), POINTER           :: mos_active
    2822              : 
    2823              :       ! Transform the AS density matrix P_MO to the atomic orbital basis,
    2824              :       ! this is simply C * P_MO * C^T
    2825            0 :       nspins = active_space_env%nspins
    2826            0 :       mos_active => active_space_env%mos_active
    2827            0 :       DO ispin = 1, nspins
    2828              :          ! size of p_inactive_ao is (nao x nao)
    2829            0 :          p_inactive_ao => active_space_env%pmat_inactive(ispin)%matrix
    2830              : 
    2831              :          ! copy p_inactive_ao to rho_ao
    2832            0 :          CALL dbcsr_copy(rho_ao(ispin)%matrix, p_inactive_ao)
    2833              : 
    2834              :          ! size of p_active_mo is (nmo x nmo)
    2835            0 :          p_active_mo => active_space_env%p_active(ispin)
    2836              : 
    2837              :          ! calculate R = p_mo
    2838            0 :          CALL cp_fm_create(R, p_active_mo%matrix_struct)
    2839            0 :          CALL cp_fm_to_fm(p_active_mo, R)
    2840              : 
    2841              :          ! calculate U = C * p_mo
    2842            0 :          CALL get_mo_set(mos_active(ispin), mo_coeff=C_active, nao=nao, nmo=nmo)
    2843            0 :          CALL cp_fm_create(U, C_active%matrix_struct)
    2844            0 :          CALL parallel_gemm("N", "N", nao, nmo, nmo, 1.0_dp, C_active, R, 0.0_dp, U)
    2845              : 
    2846              :          CALL cp_dbcsr_plus_fm_fm_t(sparse_matrix=rho_ao(ispin)%matrix, &
    2847            0 :                                     matrix_v=U, matrix_g=C_active, ncol=nmo, alpha=1.0_dp)
    2848              : 
    2849            0 :          CALL cp_fm_release(R)
    2850            0 :          CALL cp_fm_release(U)
    2851              :       END DO
    2852              : 
    2853            0 :    END SUBROUTINE update_density_ao
    2854              : 
    2855              : ! **************************************************************************************************
    2856              : !> \brief Print each value on the master node
    2857              : !> \param this object reference
    2858              : !> \param i i-index
    2859              : !> \param j j-index
    2860              : !> \param k k-index
    2861              : !> \param l l-index
    2862              : !> \param val value of the integral at (i,j,k.l)
    2863              : !> \return always true to dump all integrals
    2864              : ! **************************************************************************************************
    2865         2102 :    LOGICAL FUNCTION eri_fcidump_print_func(this, i, j, k, l, val) RESULT(cont)
    2866              :       CLASS(eri_fcidump_print), INTENT(inout) :: this
    2867              :       INTEGER, INTENT(in)                     :: i, j, k, l
    2868              :       REAL(KIND=dp), INTENT(in)               :: val
    2869              : 
    2870              :       ! write to the actual file only on the master
    2871         2102 :       IF (this%unit_nr > 0) THEN
    2872         1051 :          WRITE (this%unit_nr, "(ES23.16,4I4)") val, i + this%bra_start - 1, j + this%bra_start - 1, &
    2873         2102 :               &                                     k + this%ket_start - 1, l + this%ket_start - 1
    2874              :       END IF
    2875              : 
    2876         2102 :       cont = .TRUE.
    2877         2102 :    END FUNCTION eri_fcidump_print_func
    2878              : 
    2879              : ! **************************************************************************************************
    2880              : !> \brief checksum each value on the master node
    2881              : !> \param this object reference
    2882              : !> \param i i-index
    2883              : !> \param j j-index
    2884              : !> \param k k-index
    2885              : !> \param l l-index
    2886              : !> \param val value of the integral at (i,j,k.l)
    2887              : !> \return always true to dump all integrals
    2888              : ! **************************************************************************************************
    2889         2102 :    LOGICAL FUNCTION eri_fcidump_checksum_func(this, i, j, k, l, val) RESULT(cont)
    2890              :       CLASS(eri_fcidump_checksum), INTENT(inout) :: this
    2891              :       INTEGER, INTENT(in)                     :: i, j, k, l
    2892              :       REAL(KIND=dp), INTENT(in)               :: val
    2893              :       MARK_USED(i)
    2894              :       MARK_USED(j)
    2895              :       MARK_USED(k)
    2896              :       MARK_USED(l)
    2897              : 
    2898         2102 :       this%checksum = this%checksum + ABS(val)
    2899              : 
    2900         2102 :       cont = .TRUE.
    2901         2102 :    END FUNCTION eri_fcidump_checksum_func
    2902              : 
    2903              : ! **************************************************************************************************
    2904              : !> \brief Update active space density matrix from a fortran array
    2905              : !> \param p_act_mo density matrix in active space MO basis
    2906              : !> \param active_space_env active space environment
    2907              : !> \param ispin spin index
    2908              : !> \author Vladimir Rybkin
    2909              : ! **************************************************************************************************
    2910            0 :    SUBROUTINE update_active_density(p_act_mo, active_space_env, ispin)
    2911              :       REAL(KIND=dp), DIMENSION(:)                        :: p_act_mo
    2912              :       TYPE(active_space_type), POINTER                   :: active_space_env
    2913              :       INTEGER                                            :: ispin
    2914              : 
    2915              :       INTEGER                                            :: i1, i2, m1, m2, nmo_active
    2916              :       REAL(KIND=dp)                                      :: alpha, pij_new, pij_old
    2917              :       TYPE(cp_fm_type), POINTER                          :: p_active
    2918              : 
    2919            0 :       p_active => active_space_env%p_active(ispin)
    2920            0 :       nmo_active = active_space_env%nmo_active
    2921            0 :       alpha = active_space_env%alpha
    2922              : 
    2923            0 :       DO i1 = 1, nmo_active
    2924            0 :          m1 = active_space_env%active_orbitals(i1, ispin)
    2925            0 :          DO i2 = 1, nmo_active
    2926            0 :             m2 = active_space_env%active_orbitals(i2, ispin)
    2927            0 :             CALL cp_fm_get_element(p_active, m1, m2, pij_old)
    2928            0 :             pij_new = p_act_mo(i2 + (i1 - 1)*nmo_active)
    2929            0 :             pij_new = alpha*pij_new + (1.0_dp - alpha)*pij_old
    2930            0 :             CALL cp_fm_set_element(p_active, m1, m2, pij_new)
    2931              :          END DO
    2932              :       END DO
    2933              : 
    2934            0 :    END SUBROUTINE update_active_density
    2935              : 
    2936              : ! **************************************************************************************************
    2937              : !> \brief Compute and print the AS rdm and the natural orbitals occupation numbers
    2938              : !> \param active_space_env active space environment
    2939              : !> \param iw output unit
    2940              : !> \author Stefano Battaglia
    2941              : ! **************************************************************************************************
    2942            0 :    SUBROUTINE print_pmat_noon(active_space_env, iw)
    2943              :       TYPE(active_space_type), POINTER                   :: active_space_env
    2944              :       INTEGER                                            :: iw
    2945              : 
    2946              :       INTEGER                                            :: i1, i2, ii, ispin, jm, m1, m2, &
    2947              :                                                             nmo_active, nspins
    2948            0 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)        :: noon, pmat
    2949              :       TYPE(cp_fm_type), POINTER                          :: p_active
    2950              : 
    2951            0 :       nspins = active_space_env%nspins
    2952            0 :       nmo_active = active_space_env%nmo_active
    2953              : 
    2954            0 :       ALLOCATE (noon(nmo_active, nspins))
    2955            0 :       ALLOCATE (pmat(nmo_active, nmo_active))
    2956              : 
    2957            0 :       DO ispin = 1, nspins
    2958            0 :          p_active => active_space_env%p_active(ispin)
    2959            0 :          noon(:, ispin) = 0.0_dp
    2960            0 :          pmat = 0.0_dp
    2961              : 
    2962            0 :          DO i1 = 1, nmo_active
    2963            0 :             m1 = active_space_env%active_orbitals(i1, ispin)
    2964            0 :             DO i2 = 1, nmo_active
    2965            0 :                m2 = active_space_env%active_orbitals(i2, ispin)
    2966            0 :                CALL cp_fm_get_element(p_active, m1, m2, pmat(i1, i2))
    2967              :             END DO
    2968              :          END DO
    2969              : 
    2970            0 :          IF (iw > 0) THEN
    2971            0 :             WRITE (iw, '(/,T3,A,I2,A)') "Active space density matrix for spin ", ispin
    2972            0 :             DO i1 = 1, nmo_active
    2973            0 :                DO ii = 1, nmo_active, 8
    2974            0 :                   jm = MIN(7, nmo_active - ii)
    2975            0 :                   WRITE (iw, '(T3,6(F9.4))') (pmat(i1, ii + i2), i2=0, jm)
    2976              :                END DO
    2977              :             END DO
    2978              :          END IF
    2979              : 
    2980              :          ! diagonalize the density matrix
    2981            0 :          CALL diamat_all(pmat, noon(:, ispin))
    2982              : 
    2983            0 :          IF (iw > 0) THEN
    2984            0 :             WRITE (iw, '(/,T3,A,I2,A)') "Natural orbitals occupation numbers for spin ", ispin
    2985            0 :             DO i1 = 1, nmo_active, 8
    2986            0 :                jm = MIN(7, nmo_active - i1)
    2987              :                ! noons are stored in ascending order, so reverse-print them
    2988            0 :                WRITE (iw, '(T3,6(F9.4))') (noon(nmo_active - i1 - i2 + 1, ispin), i2=0, jm)
    2989              :             END DO
    2990              :          END IF
    2991              : 
    2992              :       END DO
    2993              : 
    2994            0 :       DEALLOCATE (noon)
    2995            0 :       DEALLOCATE (pmat)
    2996              : 
    2997            0 :    END SUBROUTINE print_pmat_noon
    2998              : 
    2999              : ! **************************************************************************************************
    3000              : !> \brief ...
    3001              : !> \param qs_env ...
    3002              : !> \param active_space_env ...
    3003              : !> \param as_input ...
    3004              : ! **************************************************************************************************
    3005            0 :    SUBROUTINE rsdft_embedding(qs_env, active_space_env, as_input)
    3006              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    3007              :       TYPE(active_space_type), POINTER                   :: active_space_env
    3008              :       TYPE(section_vals_type), POINTER                   :: as_input
    3009              : 
    3010              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'rsdft_embedding'
    3011              :       INTEGER                                            :: handle
    3012              : 
    3013              : #ifdef __NO_SOCKETS
    3014              :       CALL timeset(routineN, handle)
    3015              :       CPABORT("CP2K was compiled with the __NO_SOCKETS option!")
    3016              :       MARK_USED(qs_env)
    3017              :       MARK_USED(active_space_env)
    3018              :       MARK_USED(as_input)
    3019              : #else
    3020              : 
    3021              :       INTEGER                                            :: iw, client_fd, socket_fd, iter, max_iter
    3022              :       LOGICAL                                            :: converged, do_scf_embedding, ionode
    3023              :       REAL(KIND=dp)                                      :: delta_E, energy_corr, energy_new, &
    3024              :                                                             energy_old, energy_scf, eps_iter, t1, t2
    3025              :       TYPE(cp_logger_type), POINTER                      :: logger
    3026            0 :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: rho_ao
    3027            0 :       TYPE(mo_set_type), DIMENSION(:), POINTER           :: mos_active
    3028              :       TYPE(mp_para_env_type), POINTER                    :: para_env
    3029              :       TYPE(qs_energy_type), POINTER                      :: energy
    3030              :       TYPE(qs_ks_env_type), POINTER                      :: ks_env
    3031              :       TYPE(qs_rho_type), POINTER                         :: rho
    3032              :       TYPE(dft_control_type), POINTER                    :: dft_control
    3033              : 
    3034            0 :       CALL timeset(routineN, handle)
    3035              : 
    3036            0 :       t1 = m_walltime()
    3037              : 
    3038            0 :       logger => cp_get_default_logger()
    3039            0 :       iw = cp_logger_get_default_io_unit(logger)
    3040              : 
    3041            0 :       CALL get_qs_env(qs_env, para_env=para_env, dft_control=dft_control)
    3042            0 :       ionode = para_env%is_source()
    3043              : 
    3044              :       ! get info from the input
    3045            0 :       CALL section_vals_val_get(as_input, "SCF_EMBEDDING", l_val=do_scf_embedding)
    3046            0 :       active_space_env%do_scf_embedding = do_scf_embedding
    3047            0 :       CALL section_vals_val_get(as_input, "MAX_ITER", i_val=max_iter)
    3048            0 :       IF (max_iter < 0) CPABORT("Specify a non-negative number of max iterations.")
    3049            0 :       CALL section_vals_val_get(as_input, "EPS_ITER", r_val=eps_iter)
    3050            0 :       IF (eps_iter < 0.0) CPABORT("Specify a non-negative convergence threshold.")
    3051              : 
    3052              :       ! create the socket and wait for the client to connect
    3053            0 :       CALL initialize_socket(socket_fd, client_fd, as_input, ionode)
    3054            0 :       CALL para_env%sync()
    3055              : 
    3056              :       ! send two-electron integrals to the client
    3057            0 :       CALL send_eri_to_client(client_fd, active_space_env, para_env)
    3058              : 
    3059              :       ! get pointer to density in ao basis
    3060            0 :       CALL get_qs_env(qs_env, rho=rho, energy=energy, ks_env=ks_env)
    3061            0 :       CALL qs_rho_get(rho, rho_ao=rho_ao)
    3062              : 
    3063            0 :       IF (iw > 0) THEN
    3064              :          WRITE (UNIT=iw, FMT="(/,T2,A,/)") &
    3065            0 :             "RANGE-SEPARATED DFT EMBEDDING SELF-CONSISTENT OPTIMIZATION"
    3066              : 
    3067            0 :          WRITE (iw, '(T3,A,T68,I12)') "Max. iterations", max_iter
    3068            0 :          WRITE (iw, '(T3,A,T68,E12.4)') "Conv. threshold", eps_iter
    3069            0 :          WRITE (iw, '(T3,A,T66,F14.2)') "Density damping", active_space_env%alpha
    3070              : 
    3071              :          WRITE (UNIT=iw, FMT="(/,T3,A,T11,A,T21,A,T34,A,T55,A,T75,A,/,T3,A)") &
    3072            0 :             "Iter", "Update", "Time", "Corr. energy", "Total energy", "Change", REPEAT("-", 78)
    3073              :       END IF
    3074              :       ! CALL cp_add_iter_level(logger%iter_info, "QS_SCF")
    3075              : 
    3076            0 :       iter = 0
    3077            0 :       converged = .FALSE.
    3078              :       ! store the scf energy
    3079            0 :       energy_scf = active_space_env%energy_ref
    3080            0 :       energy_new = energy_scf
    3081            0 :       mos_active => active_space_env%mos_active
    3082              :       ! CALL set_qs_env(qs_env, active_space=active_space_env)
    3083              : 
    3084              :       ! start the self-consistent embedding loop
    3085            0 :       DO WHILE (iter < max_iter)
    3086            0 :          iter = iter + 1
    3087              : 
    3088              :          ! send V_emb and E_ina to the active space solver and update
    3089              :          ! the active space environment with the new active energy and density
    3090            0 :          CALL send_fock_to_client(client_fd, active_space_env, para_env)
    3091              : 
    3092              :          ! update energies
    3093            0 :          energy_old = energy_new
    3094            0 :          energy_new = active_space_env%energy_total
    3095            0 :          energy_corr = energy_new - energy_scf
    3096            0 :          delta_E = energy_new - energy_old
    3097              : 
    3098              :          ! get timer
    3099            0 :          t2 = t1
    3100            0 :          t1 = m_walltime()
    3101              :          ! print out progress
    3102            0 :          IF ((iw > 0)) THEN
    3103              :             WRITE (UNIT=iw, &
    3104              :                    FMT="(T3,I4,T11,A,T19,F6.1,T28,F18.10,T49,F18.10,T70,ES11.2)") &
    3105            0 :                iter, 'P_Mix', t1 - t2, energy_corr, energy_new, delta_E
    3106            0 :             CALL m_flush(iw)
    3107              :          END IF
    3108              : 
    3109              :          ! update total density in AO basis with the AS contribution
    3110            0 :          CALL update_density_ao(active_space_env, rho_ao) ! rho_ao is updated
    3111              : 
    3112              :          ! calculate F_ks in AO basis (which contains Vxc) with the new density
    3113            0 :          CALL qs_rho_update_rho(rho, qs_env=qs_env) ! updates rho_r and rho_g using rho_ao
    3114            0 :          CALL qs_ks_did_change(qs_env%ks_env, rho_changed=.TRUE.) ! set flags about the change
    3115              :          ! Re-evaluate the traces between the density matrix and the core Hamiltonians
    3116            0 :          CALL evaluate_core_matrix_traces(qs_env)
    3117              :          ! the ks matrix will be rebuilt so this is fine now
    3118              :          ! CALL set_ks_env(qs_env%ks_env, potential_changed=.FALSE.)
    3119              :          CALL qs_ks_build_kohn_sham_matrix(qs_env, calculate_forces=.FALSE., &
    3120              :                                            just_energy=.FALSE., &
    3121            0 :                                            ext_xc_section=active_space_env%xc_section)
    3122              : 
    3123              :          ! update the reference energy
    3124            0 :          active_space_env%energy_ref = energy%total
    3125              : 
    3126              :          ! transform KS/Fock, Vxc and Hcore from AO to MO basis
    3127            0 :          CALL calculate_operators(mos_active, qs_env, active_space_env)
    3128              : 
    3129              :          ! calculate the new inactive energy and embedding potential
    3130            0 :          CALL subspace_fock_matrix(active_space_env, dft_control%roks)
    3131              : 
    3132              :          ! check if it is a one-shot correction
    3133            0 :          IF (.NOT. active_space_env%do_scf_embedding) THEN
    3134            0 :             IF (iw > 0) THEN
    3135              :                WRITE (UNIT=iw, FMT="(/,T3,A,I5,A)") &
    3136            0 :                   "*** one-shot embedding correction finished ***"
    3137              :             END IF
    3138              :             converged = .TRUE.
    3139              :             EXIT
    3140              :             ! check for convergence
    3141            0 :          ELSEIF (ABS(delta_E) <= eps_iter) THEN
    3142            0 :             IF (iw > 0) THEN
    3143              :                WRITE (UNIT=iw, FMT="(/,T3,A,I5,A)") &
    3144            0 :                   "*** rs-DFT embedding run converged in ", iter, " iteration(s) ***"
    3145              :             END IF
    3146              :             converged = .TRUE.
    3147              :             EXIT
    3148              :          END IF
    3149              :       END DO
    3150              : 
    3151              :       IF (.NOT. converged) THEN
    3152            0 :          IF (iw > 0) THEN
    3153              :             WRITE (UNIT=iw, FMT="(/,T3,A,I5,A)") &
    3154            0 :                "*** rs-DFT embedding did not converged after ", iter, " iteration(s) ***"
    3155              :          END IF
    3156              :       END IF
    3157              : 
    3158              :       ! update qs total energy to the final rs-DFT energy
    3159            0 :       energy%total = active_space_env%energy_total
    3160              : 
    3161              :       ! print final energy contributions
    3162            0 :       IF (iw > 0) THEN
    3163              :          WRITE (UNIT=iw, FMT="(/,T3,A)") &
    3164            0 :             "Final energy contributions:"
    3165              :          WRITE (UNIT=iw, FMT="(T6,A,T56,F20.10)") &
    3166            0 :             "Inactive energy:", active_space_env%energy_inactive
    3167              :          WRITE (UNIT=iw, FMT="(T6,A,T56,F20.10)") &
    3168            0 :             "Active energy:", active_space_env%energy_active
    3169              :          WRITE (UNIT=iw, FMT="(T6,A,T56,F20.10)") &
    3170            0 :             "Correlation energy:", energy_corr
    3171              :          WRITE (UNIT=iw, FMT="(T6,A,T56,F20.10)") &
    3172            0 :             "Total rs-DFT energy:", active_space_env%energy_total
    3173              :       END IF
    3174              : 
    3175              :       ! print the AS rdm and the natural orbital occupation numbers
    3176            0 :       CALL print_pmat_noon(active_space_env, iw)
    3177              : 
    3178            0 :       CALL finalize_socket(socket_fd, client_fd, as_input, ionode)
    3179            0 :       CALL para_env%sync()
    3180              : #endif
    3181              : 
    3182            0 :       CALL timestop(handle)
    3183              : 
    3184            0 :    END SUBROUTINE rsdft_embedding
    3185              : 
    3186              : #ifndef __NO_SOCKETS
    3187              : ! **************************************************************************************************
    3188              : !> \brief Creates the socket, spawns the client and connects to it
    3189              : !> \param socket_fd the socket file descriptor
    3190              : !> \param client_fd the client file descriptor
    3191              : !> \param as_input active space inpute section
    3192              : !> \param ionode logical flag indicating if the process is the master
    3193              : ! **************************************************************************************************
    3194            0 :    SUBROUTINE initialize_socket(socket_fd, client_fd, as_input, ionode)
    3195              :       INTEGER, INTENT(OUT)                               :: socket_fd, client_fd
    3196              :       TYPE(section_vals_type), INTENT(IN), POINTER       :: as_input
    3197              :       LOGICAL, INTENT(IN)                                :: ionode
    3198              : 
    3199              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'initialize_socket'
    3200              :       INTEGER, PARAMETER                                 :: backlog = 10
    3201              : 
    3202              :       CHARACTER(len=default_path_length)                 :: hostname
    3203              :       INTEGER                                            :: handle, iw, port, protocol
    3204              :       LOGICAL                                            :: inet
    3205              :       TYPE(cp_logger_type), POINTER                      :: logger
    3206              : 
    3207            0 :       CALL timeset(routineN, handle)
    3208              : 
    3209            0 :       logger => cp_get_default_logger()
    3210            0 :       iw = cp_logger_get_default_io_unit(logger)
    3211              : 
    3212              :       ! protocol == 0 for UNIX, protocol > 0 for INET
    3213            0 :       CALL section_vals_val_get(as_input, "SOCKET%INET", l_val=inet)
    3214            0 :       IF (inet) THEN
    3215            0 :          protocol = 1
    3216              :       ELSE
    3217            0 :          protocol = 0
    3218              :       END IF
    3219            0 :       CALL section_vals_val_get(as_input, "SOCKET%HOST", c_val=hostname)
    3220            0 :       CALL section_vals_val_get(as_input, "SOCKET%PORT", i_val=port)
    3221              : 
    3222            0 :       IF (ionode) THEN
    3223            0 :          CALL open_bind_socket(socket_fd, protocol, port, TRIM(hostname)//C_NULL_CHAR)
    3224            0 :          WRITE (iw, '(/,T2,A,A)') "@SERVER: Created socket with address ", TRIM(hostname)
    3225            0 :          CALL listen_socket(socket_fd, backlog)
    3226              : 
    3227              :          ! wait until a connetion request arrives
    3228            0 :          WRITE (iw, '(T2,A)') "@SERVER: Waiting for requests..."
    3229            0 :          CALL accept_socket(socket_fd, client_fd)
    3230            0 :          WRITE (iw, '(T2,A,I2)') "@SERVER: Accepted socket with fd ", client_fd
    3231              :       END IF
    3232              : 
    3233            0 :       CALL timestop(handle)
    3234              : 
    3235            0 :    END SUBROUTINE initialize_socket
    3236              : 
    3237              : ! **************************************************************************************************
    3238              : !> \brief Closes the connection to the socket and deletes the file
    3239              : !> \param socket_fd the socket file descriptor
    3240              : !> \param client_fd the client file descriptor
    3241              : !> \param as_input active space inpute section
    3242              : !> \param ionode logical flag indicating if the process is the master
    3243              : ! **************************************************************************************************
    3244            0 :    SUBROUTINE finalize_socket(socket_fd, client_fd, as_input, ionode)
    3245              :       INTEGER, INTENT(IN)                                :: socket_fd, client_fd
    3246              :       TYPE(section_vals_type), INTENT(IN), POINTER       :: as_input
    3247              :       LOGICAL, INTENT(IN)                                :: ionode
    3248              : 
    3249              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'finalize_socket'
    3250              :       INTEGER, PARAMETER                                 :: header_len = 12
    3251              : 
    3252              :       CHARACTER(len=default_path_length)                 :: hostname
    3253              :       INTEGER                                            :: handle
    3254              : 
    3255            0 :       CALL timeset(routineN, handle)
    3256              : 
    3257            0 :       CALL section_vals_val_get(as_input, "SOCKET%HOST", c_val=hostname)
    3258              : 
    3259            0 :       IF (ionode) THEN
    3260              :          ! signal the client to quit
    3261            0 :          CALL writebuffer(client_fd, "QUIT        ", header_len)
    3262              :          ! close the connection
    3263            0 :          CALL close_socket(client_fd)
    3264            0 :          CALL close_socket(socket_fd)
    3265              : 
    3266              :          ! delete the socket file
    3267            0 :          IF (file_exists(TRIM(hostname))) THEN
    3268            0 :             CALL remove_socket_file(TRIM(hostname)//C_NULL_CHAR)
    3269              :          END IF
    3270              :       END IF
    3271              : 
    3272            0 :       CALL timestop(handle)
    3273              : 
    3274            0 :    END SUBROUTINE finalize_socket
    3275              : 
    3276              : ! **************************************************************************************************
    3277              : !> \brief Sends the two-electron integrals to the client vie the socket
    3278              : !> \param client_fd the client file descriptor
    3279              : !> \param active_space_env active space environment
    3280              : !> \param para_env parallel environment
    3281              : ! **************************************************************************************************
    3282            0 :    SUBROUTINE send_eri_to_client(client_fd, active_space_env, para_env)
    3283              :       INTEGER, INTENT(IN)                                :: client_fd
    3284              :       TYPE(active_space_type), INTENT(IN), POINTER       :: active_space_env
    3285              :       TYPE(mp_para_env_type), INTENT(IN), POINTER        :: para_env
    3286              : 
    3287              :       CHARACTER(len=*), PARAMETER :: routineN = 'send_eri_to_client'
    3288              :       INTEGER, PARAMETER                                 :: header_len = 12
    3289              : 
    3290              :       CHARACTER(len=default_string_length)               :: header
    3291              :       INTEGER                                            :: handle, iw
    3292              :       LOGICAL                                            :: ionode
    3293            0 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: eri_aa, eri_ab, eri_bb, s_ab
    3294              :       TYPE(cp_logger_type), POINTER                      :: logger
    3295              : 
    3296            0 :       CALL timeset(routineN, handle)
    3297              : 
    3298            0 :       logger => cp_get_default_logger()
    3299            0 :       iw = cp_logger_get_default_io_unit(logger)
    3300            0 :       ionode = para_env%is_source()
    3301              : 
    3302            0 :       ALLOCATE (eri_aa(active_space_env%nmo_active**4))
    3303            0 :       CALL eri_to_array(active_space_env%eri, eri_aa, active_space_env%active_orbitals, 1, 1)
    3304            0 :       IF (active_space_env%nspins == 2) THEN
    3305            0 :          ALLOCATE (eri_ab(active_space_env%nmo_active**4))
    3306            0 :          CALL eri_to_array(active_space_env%eri, eri_ab, active_space_env%active_orbitals, 1, 2)
    3307            0 :          ALLOCATE (eri_bb(active_space_env%nmo_active**4))
    3308            0 :          CALL eri_to_array(active_space_env%eri, eri_bb, active_space_env%active_orbitals, 2, 2)
    3309              :          ! get the overlap_ab matrix into Fortran array
    3310            0 :          ALLOCATE (s_ab(active_space_env%nmo_active**2))
    3311              :          ASSOCIATE (act_indices_a => active_space_env%active_orbitals(:, 1), &
    3312              :                     act_indices_b => active_space_env%active_orbitals(:, 2))
    3313            0 :             CALL subspace_matrix_to_array(active_space_env%sab_sub(1), s_ab, act_indices_a, act_indices_b)
    3314              :          END ASSOCIATE
    3315              :       END IF
    3316              : 
    3317              :       ! ask the status of the client
    3318            0 :       IF (ionode) CALL writebuffer(client_fd, "STATUS      ", header_len)
    3319              :       DO
    3320            0 :          header = ""
    3321            0 :          CALL para_env%sync()
    3322            0 :          IF (ionode) THEN
    3323              :             ! IF (iw > 0) WRITE(iw, *) "@SERVER: Waiting for messages..."
    3324            0 :             CALL readbuffer(client_fd, header, header_len)
    3325              :          END IF
    3326            0 :          CALL para_env%bcast(header, para_env%source)
    3327              : 
    3328              :          ! IF (iw > 0) WRITE(iw, *) "@SERVER: Message from client: ", TRIM(header)
    3329              : 
    3330            0 :          IF (TRIM(header) == "READY") THEN
    3331              :             ! if the client is ready, send the data
    3332            0 :             CALL para_env%sync()
    3333            0 :             IF (ionode) THEN
    3334            0 :                CALL writebuffer(client_fd, "TWOBODY     ", header_len)
    3335            0 :                CALL writebuffer(client_fd, active_space_env%nspins)
    3336            0 :                CALL writebuffer(client_fd, active_space_env%nmo_active)
    3337            0 :                CALL writebuffer(client_fd, active_space_env%nelec_active)
    3338            0 :                CALL writebuffer(client_fd, active_space_env%multiplicity)
    3339              :                ! send the alpha component
    3340            0 :                CALL writebuffer(client_fd, eri_aa, SIZE(eri_aa))
    3341              :                ! send the beta part for unrestricted calculations
    3342            0 :                IF (active_space_env%nspins == 2) THEN
    3343            0 :                   CALL writebuffer(client_fd, eri_ab, SIZE(eri_ab))
    3344            0 :                   CALL writebuffer(client_fd, eri_bb, SIZE(eri_bb))
    3345            0 :                   CALL writebuffer(client_fd, s_ab, SIZE(s_ab))
    3346              :                END IF
    3347              :             END IF
    3348            0 :          ELSE IF (TRIM(header) == "RECEIVED") THEN
    3349              :             EXIT
    3350              :          END IF
    3351              :       END DO
    3352              : 
    3353            0 :       DEALLOCATE (eri_aa)
    3354            0 :       IF (active_space_env%nspins == 2) THEN
    3355            0 :          DEALLOCATE (eri_ab)
    3356            0 :          DEALLOCATE (eri_bb)
    3357            0 :          DEALLOCATE (s_ab)
    3358              :       END IF
    3359              : 
    3360            0 :       CALL para_env%sync()
    3361              : 
    3362            0 :       CALL timestop(handle)
    3363              : 
    3364            0 :    END SUBROUTINE send_eri_to_client
    3365              : 
    3366              : ! **************************************************************************************************
    3367              : !> \brief Sends the one-electron embedding potential and the inactive energy to the client
    3368              : !> \param client_fd the client file descriptor
    3369              : !> \param active_space_env active space environment
    3370              : !> \param para_env parallel environment
    3371              : ! **************************************************************************************************
    3372            0 :    SUBROUTINE send_fock_to_client(client_fd, active_space_env, para_env)
    3373              :       INTEGER, INTENT(IN)                                :: client_fd
    3374              :       TYPE(active_space_type), INTENT(IN), POINTER       :: active_space_env
    3375              :       TYPE(mp_para_env_type), INTENT(IN), POINTER        :: para_env
    3376              : 
    3377              :       CHARACTER(len=*), PARAMETER :: routineN = 'send_fock_to_client'
    3378              :       INTEGER, PARAMETER                                 :: header_len = 12
    3379              : 
    3380              :       CHARACTER(len=default_string_length)               :: header
    3381              :       INTEGER                                            :: handle, iw
    3382              :       LOGICAL                                            :: debug, ionode
    3383            0 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: fock_a, fock_b, p_act_mo_a, p_act_mo_b
    3384              :       TYPE(cp_logger_type), POINTER                      :: logger
    3385              : 
    3386            0 :       CALL timeset(routineN, handle)
    3387              : 
    3388              :       ! Set to .TRUE. to activate debug output
    3389            0 :       debug = .FALSE.
    3390              : 
    3391            0 :       logger => cp_get_default_logger()
    3392            0 :       iw = cp_logger_get_default_io_unit(logger)
    3393            0 :       ionode = para_env%is_source()
    3394              : 
    3395            0 :       ALLOCATE (p_act_mo_a(active_space_env%nmo_active**2))
    3396            0 :       ALLOCATE (fock_a(active_space_env%nmo_active**2))
    3397            0 :       IF (active_space_env%nspins == 2) THEN
    3398            0 :          ALLOCATE (p_act_mo_b(active_space_env%nmo_active**2))
    3399            0 :          ALLOCATE (fock_b(active_space_env%nmo_active**2))
    3400              :       END IF
    3401              : 
    3402              :       ! get the fock matrix into Fortran arrays
    3403              :       ASSOCIATE (act_indices => active_space_env%active_orbitals(:, 1))
    3404            0 :          CALL subspace_matrix_to_array(active_space_env%fock_sub(1), fock_a, act_indices, act_indices)
    3405              :       END ASSOCIATE
    3406              : 
    3407            0 :       IF (active_space_env%nspins == 2) THEN
    3408              :          ASSOCIATE (act_indices => active_space_env%active_orbitals(:, 2))
    3409            0 :             CALL subspace_matrix_to_array(active_space_env%fock_sub(2), fock_b, act_indices, act_indices)
    3410              :          END ASSOCIATE
    3411              :       END IF
    3412              : 
    3413              :       ! ask the status of the client
    3414            0 :       IF (ionode) CALL writebuffer(client_fd, "STATUS      ", header_len)
    3415              :       DO
    3416            0 :          header = ""
    3417              : 
    3418            0 :          CALL para_env%sync()
    3419            0 :          IF (ionode) THEN
    3420              :             IF (debug .AND. iw > 0) WRITE (iw, *) "@SERVER: Waiting for messages..."
    3421            0 :             CALL readbuffer(client_fd, header, header_len)
    3422              :          END IF
    3423            0 :          CALL para_env%bcast(header, para_env%source)
    3424              : 
    3425              :          IF (debug .AND. iw > 0) WRITE (iw, *) "@SERVER: Message from client: ", TRIM(header)
    3426              : 
    3427            0 :          IF (TRIM(header) == "READY") THEN
    3428              :             ! if the client is ready, send the data
    3429            0 :             CALL para_env%sync()
    3430            0 :             IF (ionode) THEN
    3431            0 :                CALL writebuffer(client_fd, "ONEBODY     ", header_len)
    3432            0 :                CALL writebuffer(client_fd, active_space_env%energy_inactive)
    3433              :                ! send the alpha component
    3434            0 :                CALL writebuffer(client_fd, fock_a, SIZE(fock_a))
    3435              :                ! send the beta part for unrestricted calculations
    3436            0 :                IF (active_space_env%nspins == 2) THEN
    3437            0 :                   CALL writebuffer(client_fd, fock_b, SIZE(fock_b))
    3438              :                END IF
    3439              :             END IF
    3440              : 
    3441            0 :          ELSE IF (TRIM(header) == "HAVEDATA") THEN
    3442              :             ! qiskit has data to transfer, let them know we want it and wait for it
    3443            0 :             CALL para_env%sync()
    3444            0 :             IF (ionode) THEN
    3445              :                IF (debug .AND. iw > 0) WRITE (iw, *) "@SERVER: Qiskit has data to transfer"
    3446            0 :                CALL writebuffer(client_fd, "GETDENSITY  ", header_len)
    3447              : 
    3448              :                ! read the active energy and density
    3449            0 :                CALL readbuffer(client_fd, active_space_env%energy_active)
    3450            0 :                CALL readbuffer(client_fd, p_act_mo_a, SIZE(p_act_mo_a))
    3451            0 :                IF (active_space_env%nspins == 2) THEN
    3452            0 :                   CALL readbuffer(client_fd, p_act_mo_b, SIZE(p_act_mo_b))
    3453              :                END IF
    3454              :             END IF
    3455              : 
    3456              :             ! broadcast the data to all processors
    3457            0 :             CALL para_env%bcast(active_space_env%energy_active, para_env%source)
    3458            0 :             CALL para_env%bcast(p_act_mo_a, para_env%source)
    3459            0 :             IF (active_space_env%nspins == 2) THEN
    3460            0 :                CALL para_env%bcast(p_act_mo_b, para_env%source)
    3461              :             END IF
    3462              : 
    3463              :             ! update total and reference energies in active space enviornment
    3464            0 :             active_space_env%energy_total = active_space_env%energy_inactive + active_space_env%energy_active
    3465              : 
    3466              :             ! update the active density matrix in the active space environment
    3467            0 :             CALL update_active_density(p_act_mo_a, active_space_env, 1)
    3468            0 :             IF (active_space_env%nspins == 2) THEN
    3469            0 :                CALL update_active_density(p_act_mo_b, active_space_env, 2)
    3470              :             END IF
    3471              : 
    3472              :             ! the non-iterative part is done, we can continue
    3473              :             EXIT
    3474              :          END IF
    3475              : 
    3476              :       END DO
    3477              : 
    3478            0 :       DEALLOCATE (p_act_mo_a)
    3479            0 :       DEALLOCATE (fock_a)
    3480            0 :       IF (active_space_env%nspins == 2) THEN
    3481            0 :          DEALLOCATE (p_act_mo_b)
    3482            0 :          DEALLOCATE (fock_b)
    3483              :       END IF
    3484              : 
    3485            0 :       CALL para_env%sync()
    3486              : 
    3487            0 :       CALL timestop(handle)
    3488              : 
    3489            0 :    END SUBROUTINE send_fock_to_client
    3490              : #endif
    3491              : 
    3492            0 : END MODULE qs_active_space_methods
        

Generated by: LCOV version 2.0-1