LCOV - code coverage report
Current view: top level - src - almo_scf.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:1be8651) Lines: 83.5 % 928 775
Test Date: 2026-11-24 01:22:08 Functions: 93.8 % 16 15

            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 Routines for all ALMO-based SCF methods
      10              : !>        'RZK-warning' marks unresolved issues
      11              : !> \par History
      12              : !>       2011.05 created [Rustam Z Khaliullin]
      13              : !> \author Rustam Z Khaliullin
      14              : ! **************************************************************************************************
      15              : MODULE almo_scf
      16              :    USE almo_scf_methods,                ONLY: almo_scf_p_blk_to_t_blk,&
      17              :                                               almo_scf_t_rescaling,&
      18              :                                               almo_scf_t_to_proj,&
      19              :                                               distribute_domains,&
      20              :                                               orthogonalize_mos
      21              :    USE almo_scf_optimizer,              ONLY: almo_scf_block_diagonal,&
      22              :                                               almo_scf_construct_nlmos,&
      23              :                                               almo_scf_xalmo_eigensolver,&
      24              :                                               almo_scf_xalmo_pcg,&
      25              :                                               almo_scf_xalmo_trustr
      26              :    USE almo_scf_qs,                     ONLY: almo_dm_to_almo_ks,&
      27              :                                               almo_scf_construct_quencher,&
      28              :                                               calculate_w_matrix_almo,&
      29              :                                               construct_qs_mos,&
      30              :                                               init_almo_ks_matrix_via_qs,&
      31              :                                               matrix_almo_create,&
      32              :                                               matrix_qs_to_almo
      33              :    USE almo_scf_types,                  ONLY: almo_mat_dim_aobasis,&
      34              :                                               almo_mat_dim_occ,&
      35              :                                               almo_mat_dim_virt,&
      36              :                                               almo_mat_dim_virt_disc,&
      37              :                                               almo_mat_dim_virt_full,&
      38              :                                               almo_scf_env_type,&
      39              :                                               optimizer_options_type,&
      40              :                                               print_optimizer_options
      41              :    USE atomic_kind_types,               ONLY: atomic_kind_type
      42              :    USE bibliography,                    ONLY: Khaliullin2013,&
      43              :                                               Kolafa2004,&
      44              :                                               Kuhne2007,&
      45              :                                               Rullan2026,&
      46              :                                               Scheiber2018,&
      47              :                                               Staub2019,&
      48              :                                               cite_reference
      49              :    USE cp_blacs_env,                    ONLY: cp_blacs_env_release
      50              :    USE cp_control_types,                ONLY: dft_control_type
      51              :    USE cp_dbcsr_api,                    ONLY: &
      52              :         dbcsr_add, dbcsr_copy, dbcsr_create, dbcsr_distribution_type, dbcsr_filter, &
      53              :         dbcsr_finalize, dbcsr_get_info, dbcsr_iterator_blocks_left, dbcsr_iterator_next_block, &
      54              :         dbcsr_iterator_start, dbcsr_iterator_stop, dbcsr_iterator_type, dbcsr_multiply, &
      55              :         dbcsr_p_type, dbcsr_release, dbcsr_scale, dbcsr_set, dbcsr_type, dbcsr_type_no_symmetry, &
      56              :         dbcsr_type_symmetric, dbcsr_work_create
      57              :    USE cp_dbcsr_contrib,                ONLY: dbcsr_add_on_diag,&
      58              :                                               dbcsr_checksum,&
      59              :                                               dbcsr_init_random,&
      60              :                                               dbcsr_reserve_all_blocks
      61              :    USE cp_dbcsr_diag,                   ONLY: cp_dbcsr_syevd
      62              :    USE cp_dbcsr_io,                     ONLY: dbcsr_binary_read
      63              :    USE cp_dbcsr_operations,             ONLY: copy_dbcsr_to_fm
      64              :    USE cp_fm_types,                     ONLY: cp_fm_type
      65              :    USE cp_log_handling,                 ONLY: cp_get_default_logger,&
      66              :                                               cp_logger_get_default_unit_nr,&
      67              :                                               cp_logger_type
      68              :    USE domain_submatrix_methods,        ONLY: init_submatrices,&
      69              :                                               release_submatrices
      70              :    USE input_constants,                 ONLY: &
      71              :         almo_deloc_none, almo_deloc_scf, almo_deloc_x, almo_deloc_x_then_scf, &
      72              :         almo_deloc_xalmo_1diag, almo_deloc_xalmo_scf, almo_deloc_xalmo_x, almo_deloc_xk, &
      73              :         almo_domain_layout_molecular, almo_mat_distr_atomic, almo_mat_distr_molecular, &
      74              :         almo_scf_diag, almo_scf_dm_sign, almo_scf_pcg, almo_scf_skip, almo_scf_trustr, &
      75              :         atomic_guess, molecular_guess, optimizer_diis, optimizer_lin_eq_pcg, optimizer_pcg, &
      76              :         optimizer_trustr, restart_guess, smear_fermi_dirac, virt_full, virt_number, virt_occ_size, &
      77              :         xalmo_case_block_diag, xalmo_case_fully_deloc, xalmo_case_normal, xalmo_trial_r0_out
      78              :    USE input_section_types,             ONLY: section_vals_type
      79              :    USE iterate_matrix,                  ONLY: invert_Hotelling,&
      80              :                                               matrix_sqrt_Newton_Schulz
      81              :    USE kinds,                           ONLY: default_path_length,&
      82              :                                               dp
      83              :    USE mathlib,                         ONLY: binomial
      84              :    USE message_passing,                 ONLY: mp_comm_type,&
      85              :                                               mp_para_env_release,&
      86              :                                               mp_para_env_type
      87              :    USE molecule_types,                  ONLY: get_molecule_set_info,&
      88              :                                               molecule_type
      89              :    USE mscfg_types,                     ONLY: get_matrix_from_submatrices,&
      90              :                                               molecular_scf_guess_env_type
      91              :    USE particle_types,                  ONLY: particle_type
      92              :    USE qs_atomic_block,                 ONLY: calculate_atomic_block_dm
      93              :    USE qs_environment_types,            ONLY: get_qs_env,&
      94              :                                               qs_environment_type
      95              :    USE qs_initial_guess,                ONLY: calculate_mopac_dm
      96              :    USE qs_kind_types,                   ONLY: qs_kind_type
      97              :    USE qs_mo_types,                     ONLY: get_mo_set,&
      98              :                                               mo_set_type
      99              :    USE qs_rho_types,                    ONLY: qs_rho_get,&
     100              :                                               qs_rho_type
     101              :    USE qs_scf_post_scf,                 ONLY: qs_scf_compute_properties
     102              :    USE qs_scf_types,                    ONLY: qs_scf_env_type
     103              : #include "./base/base_uses.f90"
     104              : 
     105              :    IMPLICIT NONE
     106              : 
     107              :    PRIVATE
     108              : 
     109              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'almo_scf'
     110              : 
     111              :    PUBLIC :: almo_entry_scf
     112              : 
     113              :    LOGICAL, PARAMETER :: debug_mode = .FALSE.
     114              :    LOGICAL, PARAMETER :: safe_mode = .FALSE.
     115              : 
     116              : CONTAINS
     117              : 
     118              : ! **************************************************************************************************
     119              : !> \brief The entry point into ALMO SCF routines
     120              : !> \param qs_env   pointer to the QS environment
     121              : !> \param calc_forces   calculate forces?
     122              : !> \par History
     123              : !>       2011.05 created [Rustam Z Khaliullin]
     124              : !> \author Rustam Z Khaliullin
     125              : ! **************************************************************************************************
     126          122 :    SUBROUTINE almo_entry_scf(qs_env, calc_forces)
     127              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     128              :       LOGICAL, INTENT(IN)                                :: calc_forces
     129              : 
     130              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'almo_entry_scf'
     131              : 
     132              :       INTEGER                                            :: handle
     133              :       TYPE(almo_scf_env_type), POINTER                   :: almo_scf_env
     134              : 
     135          122 :       CALL timeset(routineN, handle)
     136              : 
     137          122 :       CALL cite_reference(Khaliullin2013)
     138              : 
     139              :       ! get a pointer to the almo environment
     140          122 :       CALL get_qs_env(qs_env, almo_scf_env=almo_scf_env)
     141              : 
     142              :       ! initialize scf
     143          122 :       CALL almo_scf_init(qs_env, almo_scf_env, calc_forces)
     144              : 
     145              :       ! create the initial guess for ALMOs
     146          122 :       CALL almo_scf_initial_guess(qs_env, almo_scf_env)
     147              : 
     148              :       ! perform SCF for block diagonal ALMOs
     149          122 :       CALL almo_scf_main(qs_env, almo_scf_env)
     150              : 
     151              :       ! allow electron delocalization
     152          122 :       CALL almo_scf_delocalization(qs_env, almo_scf_env)
     153              : 
     154              :       ! construct NLMOs
     155          122 :       CALL construct_nlmos(qs_env, almo_scf_env)
     156              : 
     157              :       ! electron correlation methods
     158              :       !CALL almo_correlation_main(qs_env,almo_scf_env)
     159              : 
     160              :       ! do post scf processing
     161          122 :       CALL almo_scf_post(qs_env, almo_scf_env)
     162              : 
     163              :       ! clean up the mess
     164          122 :       CALL almo_scf_clean_up(almo_scf_env)
     165              : 
     166          122 :       CALL timestop(handle)
     167              : 
     168          122 :    END SUBROUTINE almo_entry_scf
     169              : 
     170              : ! **************************************************************************************************
     171              : !> \brief Initialization of the almo_scf_env_type.
     172              : !> \param qs_env ...
     173              : !> \param almo_scf_env ...
     174              : !> \param calc_forces ...
     175              : !> \par History
     176              : !>       2011.05 created [Rustam Z Khaliullin]
     177              : !>       2018.09 smearing support [Ruben Staub]
     178              : !> \author Rustam Z Khaliullin
     179              : ! **************************************************************************************************
     180          122 :    SUBROUTINE almo_scf_init(qs_env, almo_scf_env, calc_forces)
     181              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     182              :       TYPE(almo_scf_env_type), INTENT(INOUT)             :: almo_scf_env
     183              :       LOGICAL, INTENT(IN)                                :: calc_forces
     184              : 
     185              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'almo_scf_init'
     186              : 
     187              :       INTEGER                                            :: ao, handle, i, iao, idomain, ispin, &
     188              :                                                             multip, naos, natoms, ndomains, nelec, &
     189              :                                                             nelec_a, nelec_b, nmols, nspins, &
     190              :                                                             unit_nr
     191              :       TYPE(cp_logger_type), POINTER                      :: logger
     192          122 :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: matrix_s
     193              :       TYPE(dft_control_type), POINTER                    :: dft_control
     194          122 :       TYPE(molecule_type), DIMENSION(:), POINTER         :: molecule_set
     195              :       TYPE(section_vals_type), POINTER                   :: input
     196              : 
     197          122 :       CALL timeset(routineN, handle)
     198              : 
     199              :       ! define the output_unit
     200          122 :       logger => cp_get_default_logger()
     201          122 :       IF (logger%para_env%is_source()) THEN
     202           61 :          unit_nr = cp_logger_get_default_unit_nr(logger, local=.TRUE.)
     203              :       ELSE
     204           61 :          unit_nr = -1
     205              :       END IF
     206              : 
     207              :       ! set optimizers' types
     208          122 :       almo_scf_env%opt_block_diag_diis%optimizer_type = optimizer_diis
     209          122 :       almo_scf_env%opt_block_diag_pcg%optimizer_type = optimizer_pcg
     210          122 :       almo_scf_env%opt_xalmo_diis%optimizer_type = optimizer_diis
     211          122 :       almo_scf_env%opt_xalmo_pcg%optimizer_type = optimizer_pcg
     212          122 :       almo_scf_env%opt_xalmo_trustr%optimizer_type = optimizer_trustr
     213          122 :       almo_scf_env%opt_nlmo_pcg%optimizer_type = optimizer_pcg
     214          122 :       almo_scf_env%opt_block_diag_trustr%optimizer_type = optimizer_trustr
     215          122 :       almo_scf_env%opt_xalmo_newton_pcg_solver%optimizer_type = optimizer_lin_eq_pcg
     216              : 
     217              :       ! get info from the qs_env
     218              :       CALL get_qs_env(qs_env, &
     219              :                       nelectron_total=almo_scf_env%nelectrons_total, &
     220              :                       matrix_s=matrix_s, &
     221              :                       dft_control=dft_control, &
     222              :                       molecule_set=molecule_set, &
     223              :                       input=input, &
     224              :                       has_unit_metric=almo_scf_env%orthogonal_basis, &
     225              :                       para_env=almo_scf_env%para_env, &
     226              :                       blacs_env=almo_scf_env%blacs_env, &
     227          122 :                       nelectron_spin=almo_scf_env%nelectrons_spin)
     228          122 :       CALL almo_scf_env%para_env%retain()
     229          122 :       CALL almo_scf_env%blacs_env%retain()
     230              : 
     231              :       ! copy basic quantities
     232          122 :       almo_scf_env%nspins = dft_control%nspins
     233          122 :       almo_scf_env%nmolecules = SIZE(molecule_set)
     234              :       CALL dbcsr_get_info(matrix_s(1)%matrix, &
     235          122 :                           nfullrows_total=naos, nblkrows_total=almo_scf_env%natoms)
     236          122 :       almo_scf_env%naos = naos
     237              :       !! retrieve smearing parameters, and check compatibility of methods requested
     238          122 :       almo_scf_env%smear = dft_control%smear
     239          122 :       IF (almo_scf_env%smear) THEN
     240            4 :          CALL cite_reference(Staub2019)
     241            4 :          IF ((almo_scf_env%almo_update_algorithm /= almo_scf_diag) .OR. &
     242              :              ((almo_scf_env%deloc_method /= almo_deloc_none) .AND. &
     243              :               (almo_scf_env%xalmo_update_algorithm /= almo_scf_diag))) THEN
     244            0 :             CPABORT("ALMO smearing is currently implemented for DIAG algorithm only")
     245              :          END IF
     246            4 :          IF (qs_env%scf_control%smear%method /= smear_fermi_dirac) THEN
     247            0 :             CPABORT("Only Fermi-Dirac smearing is currently compatible with ALMO")
     248              :          END IF
     249            4 :          almo_scf_env%smear_e_temp = qs_env%scf_control%smear%electronic_temperature
     250            4 :          IF ((almo_scf_env%mat_distr_aos /= almo_mat_distr_molecular) .OR. &
     251              :              (almo_scf_env%domain_layout_mos /= almo_domain_layout_molecular)) THEN
     252            0 :             CPABORT("ALMO smearing was designed to work with molecular fragments only")
     253              :          END IF
     254              :       END IF
     255              : 
     256              :       ! convenient local varibales
     257          122 :       nmols = almo_scf_env%nmolecules
     258          122 :       natoms = almo_scf_env%natoms
     259              : 
     260              :       ! Define groups: either atomic or molecular
     261          122 :       IF (almo_scf_env%domain_layout_mos == almo_domain_layout_molecular) THEN
     262          122 :          almo_scf_env%ndomains = almo_scf_env%nmolecules
     263              :       ELSE
     264            0 :          almo_scf_env%ndomains = almo_scf_env%natoms
     265              :       END IF
     266              : 
     267          122 :       IF (ALLOCATED(almo_scf_env%activate)) THEN
     268           72 :          IF (almo_scf_env%activate(1) > 1) THEN
     269            0 :             DEALLOCATE (almo_scf_env%activate)
     270              :          END IF
     271              :       END IF
     272              : 
     273          122 :       IF (.NOT. ALLOCATED(almo_scf_env%activate)) THEN
     274           50 :          ALLOCATE (almo_scf_env%activate(1))
     275          100 :          almo_scf_env%activate = 0
     276              :       END IF
     277              : 
     278          122 :       IF (almo_scf_env%activate(1) == 1) THEN
     279            6 :          CALL cite_reference(Rullan2026)
     280            6 :          ndomains = SIZE(almo_scf_env%multiplicity_of_domain)
     281            6 :          nspins = SIZE(almo_scf_env%multiplicity_of_domain)
     282              :       ELSE
     283          116 :          nspins = almo_scf_env%nspins
     284          116 :          ndomains = almo_scf_env%ndomains
     285              :       END IF
     286              : 
     287          122 :       IF (almo_scf_env%activate(1) == 0) THEN
     288              : 
     289          348 :          ALLOCATE (almo_scf_env%charge_of_domain(ndomains))
     290          232 :          ALLOCATE (almo_scf_env%multiplicity_of_domain(ndomains))
     291              :       END IF
     292              : 
     293              :       ! allocate domain descriptors
     294              : 
     295          366 :       ALLOCATE (almo_scf_env%domain_index_of_atom(natoms))
     296          366 :       ALLOCATE (almo_scf_env%domain_index_of_ao(naos))
     297          366 :       ALLOCATE (almo_scf_env%first_atom_of_domain(ndomains))
     298          244 :       ALLOCATE (almo_scf_env%last_atom_of_domain(ndomains))
     299          244 :       ALLOCATE (almo_scf_env%nbasis_of_domain(ndomains))
     300          488 :       ALLOCATE (almo_scf_env%nocc_of_domain(ndomains, nspins)) !! with smearing, nb of available orbitals for occupation
     301          488 :       ALLOCATE (almo_scf_env%real_ne_of_domain(ndomains, nspins)) !! with smearing, nb of fully-occupied orbitals
     302          366 :       ALLOCATE (almo_scf_env%nvirt_full_of_domain(ndomains, nspins))
     303          366 :       ALLOCATE (almo_scf_env%nvirt_of_domain(ndomains, nspins))
     304          366 :       ALLOCATE (almo_scf_env%nvirt_disc_of_domain(ndomains, nspins))
     305          366 :       ALLOCATE (almo_scf_env%mu_of_domain(ndomains, nspins))
     306          244 :       ALLOCATE (almo_scf_env%cpu_of_domain(ndomains))
     307              : 
     308              :       ! fill out domain descriptors and group descriptors
     309          122 :       IF (almo_scf_env%domain_layout_mos == almo_domain_layout_molecular) THEN
     310              :          ! get domain info from molecule_set
     311          122 :          IF (almo_scf_env%activate(1) == 1) THEN
     312              :             CALL get_molecule_set_info(molecule_set, &
     313              :                                        atom_to_mol=almo_scf_env%domain_index_of_atom, &
     314              :                                        mol_to_first_atom=almo_scf_env%first_atom_of_domain, &
     315              :                                        mol_to_last_atom=almo_scf_env%last_atom_of_domain, &
     316              :                                        mol_to_nelectrons=almo_scf_env%nocc_of_domain(1:ndomains, 1), &
     317            6 :                                        mol_to_nbasis=almo_scf_env%nbasis_of_domain)
     318              : 
     319              :          ELSE
     320              :             CALL get_molecule_set_info(molecule_set, &
     321              :                                        atom_to_mol=almo_scf_env%domain_index_of_atom, &
     322              :                                        mol_to_first_atom=almo_scf_env%first_atom_of_domain, &
     323              :                                        mol_to_last_atom=almo_scf_env%last_atom_of_domain, &
     324              :                                        mol_to_nelectrons=almo_scf_env%nocc_of_domain(1:ndomains, 1), &
     325              :                                        mol_to_nbasis=almo_scf_env%nbasis_of_domain, &
     326              :                                        mol_to_charge=almo_scf_env%charge_of_domain, &
     327          116 :                                        mol_to_multiplicity=almo_scf_env%multiplicity_of_domain)
     328              :          END IF
     329              :          ! calculate number of alpha and beta occupied orbitals from
     330              :          ! the number of electrons and multiplicity of each molecule
     331              :          ! Na + Nb = Ne
     332              :          ! Na - Nb = Mult - 1 (assume Na > Nb as we do not have more info from get_molecule_set_info)
     333          944 :          DO idomain = 1, ndomains
     334          822 :             IF (almo_scf_env%activate(1) == 1) THEN
     335           12 :                nelec = almo_scf_env%nocc_of_domain(idomain, 1) - almo_scf_env%charge_of_domain(idomain)
     336              :             ELSE
     337          810 :                nelec = almo_scf_env%nocc_of_domain(idomain, 1)
     338              :             END IF
     339              : 
     340          822 :             multip = almo_scf_env%multiplicity_of_domain(idomain)
     341          822 :             nelec_a = (nelec + multip - 1)/2
     342              : 
     343              :             !! Initializing an occupation-rescaling trick if smearing is on
     344          944 :             IF (almo_scf_env%smear) THEN
     345            8 :                CPWARN_IF(multip > 1, "BEWARE: Non singlet state detected, treating it as closed-shell")
     346              :                !! Save real number of electrons of each spin, as it is required for Fermi-dirac smearing
     347              :                !! BEWARE : Non singlet states are allowed but treated as closed-shell
     348           16 :                almo_scf_env%real_ne_of_domain(idomain, :) = REAL(nelec, KIND=dp)/2.0_dp
     349              :                !! Add a number of added_mos equal to the number of atoms in domain
     350              :                !! (since fragments were computed this way with smearing)
     351              :                almo_scf_env%nocc_of_domain(idomain, :) = CEILING(almo_scf_env%real_ne_of_domain(idomain, :)) &
     352              :                                                          + (almo_scf_env%last_atom_of_domain(idomain) &
     353           16 :                                                             - almo_scf_env%first_atom_of_domain(idomain) + 1)
     354              :             ELSE
     355          814 :                almo_scf_env%nocc_of_domain(idomain, 1) = nelec_a
     356          814 :                nelec_b = nelec - nelec_a
     357          814 :                IF (almo_scf_env%activate(1) == 1) THEN
     358           12 :                   almo_scf_env%nocc_of_domain(idomain, 2) = nelec_b
     359              :                END IF
     360              : 
     361          814 :                IF (nelec_a /= nelec_b) THEN
     362            4 :                   IF (nspins == 1) THEN
     363              : 
     364            0 :                      CPABORT("odd e- -- use unrestricted methods")
     365              :                   END IF
     366              : 
     367              :                END IF
     368              :             END IF
     369              :          END DO
     370          250 :          DO ispin = 1, nspins
     371              :             ! take care of the full virtual subspace
     372              :             almo_scf_env%nvirt_full_of_domain(:, ispin) = &
     373              :                almo_scf_env%nbasis_of_domain(:) - &
     374          962 :                almo_scf_env%nocc_of_domain(:, ispin)
     375              :             ! and the truncated virtual subspace
     376          122 :             SELECT CASE (almo_scf_env%deloc_truncate_virt)
     377              :             CASE (virt_full)
     378              :                almo_scf_env%nvirt_of_domain(:, ispin) = &
     379          962 :                   almo_scf_env%nvirt_full_of_domain(:, ispin)
     380          962 :                almo_scf_env%nvirt_disc_of_domain(:, ispin) = 0
     381              :             CASE (virt_number)
     382            0 :                DO idomain = 1, ndomains
     383              :                   almo_scf_env%nvirt_of_domain(idomain, ispin) = &
     384              :                      MIN(almo_scf_env%deloc_virt_per_domain, &
     385            0 :                          almo_scf_env%nvirt_full_of_domain(idomain, ispin))
     386              :                   almo_scf_env%nvirt_disc_of_domain(idomain, ispin) = &
     387              :                      almo_scf_env%nvirt_full_of_domain(idomain, ispin) - &
     388            0 :                      almo_scf_env%nvirt_of_domain(idomain, ispin)
     389              :                END DO
     390              :             CASE (virt_occ_size)
     391            0 :                DO idomain = 1, ndomains
     392              :                   almo_scf_env%nvirt_of_domain(idomain, ispin) = &
     393              :                      MIN(almo_scf_env%nocc_of_domain(idomain, ispin), &
     394            0 :                          almo_scf_env%nvirt_full_of_domain(idomain, ispin))
     395              :                   almo_scf_env%nvirt_disc_of_domain(idomain, ispin) = &
     396              :                      almo_scf_env%nvirt_full_of_domain(idomain, ispin) - &
     397            0 :                      almo_scf_env%nvirt_of_domain(idomain, ispin)
     398              :                END DO
     399              :             CASE DEFAULT
     400          128 :                CPABORT("illegal method for virtual space truncation")
     401              :             END SELECT
     402              :          END DO ! spin
     403              :       ELSE ! domains are atomic
     404              :          ! RZK-warning do the same for atomic domains/groups
     405            0 :          almo_scf_env%domain_index_of_atom(1:natoms) = [(i, i=1, natoms)]
     406              :       END IF
     407              : 
     408              :       ao = 1
     409          944 :       DO idomain = 1, ndomains
     410         9340 :          DO iao = 1, almo_scf_env%nbasis_of_domain(idomain)
     411         8396 :             almo_scf_env%domain_index_of_ao(ao) = idomain
     412         9218 :             ao = ao + 1
     413              :          END DO
     414              :       END DO
     415              : 
     416         1084 :       almo_scf_env%mu_of_domain(:, :) = almo_scf_env%mu
     417              : 
     418              :       ! build domain (i.e. layout) indices for distribution blocks
     419              :       ! ao blocks
     420          122 :       IF (almo_scf_env%mat_distr_aos == almo_mat_distr_atomic) THEN
     421            0 :          ALLOCATE (almo_scf_env%domain_index_of_ao_block(natoms))
     422              :          almo_scf_env%domain_index_of_ao_block(:) = &
     423            0 :             almo_scf_env%domain_index_of_atom(:)
     424          122 :       ELSE IF (almo_scf_env%mat_distr_aos == almo_mat_distr_molecular) THEN
     425          366 :          ALLOCATE (almo_scf_env%domain_index_of_ao_block(nmols))
     426              :          ! if distr blocks are molecular then domain layout is also molecular
     427         1766 :          almo_scf_env%domain_index_of_ao_block(:) = [(i, i=1, nmols)]
     428              :       END IF
     429              :       ! mo blocks
     430          122 :       IF (almo_scf_env%mat_distr_mos == almo_mat_distr_atomic) THEN
     431            0 :          ALLOCATE (almo_scf_env%domain_index_of_mo_block(natoms))
     432              :          almo_scf_env%domain_index_of_mo_block(:) = &
     433            0 :             almo_scf_env%domain_index_of_atom(:)
     434          122 :       ELSE IF (almo_scf_env%mat_distr_mos == almo_mat_distr_molecular) THEN
     435          366 :          ALLOCATE (almo_scf_env%domain_index_of_mo_block(nmols))
     436              :          ! if distr blocks are molecular then domain layout is also molecular
     437         1766 :          almo_scf_env%domain_index_of_mo_block(:) = [(i, i=1, nmols)]
     438              :       END IF
     439              : 
     440              :       ! set all flags
     441              :       !almo_scf_env%need_previous_ks=.FALSE.
     442              :       !IF (almo_scf_env%deloc_method==almo_deloc_harris) THEN
     443          122 :       almo_scf_env%need_previous_ks = .TRUE.
     444              :       !ENDIF
     445              : 
     446              :       !almo_scf_env%need_virtuals=.FALSE.
     447              :       !almo_scf_env%need_orbital_energies=.FALSE.
     448              :       !IF (almo_scf_env%almo_update_algorithm==almo_scf_diag) THEN
     449          122 :       almo_scf_env%need_virtuals = .TRUE.
     450          122 :       almo_scf_env%need_orbital_energies = .TRUE.
     451              :       !ENDIF
     452              : 
     453          122 :       almo_scf_env%calc_forces = calc_forces
     454          122 :       IF (calc_forces) THEN
     455           66 :          CALL cite_reference(Scheiber2018)
     456              :          IF (almo_scf_env%deloc_method == almo_deloc_x .OR. &
     457           66 :              almo_scf_env%deloc_method == almo_deloc_xalmo_x .OR. &
     458              :              almo_scf_env%deloc_method == almo_deloc_xalmo_1diag) THEN
     459            0 :             CPABORT("Forces for perturbative methods are NYI. Change DELOCALIZE_METHOD")
     460              :          END IF
     461              :          ! switch to ASPC after a certain number of exact steps is done
     462           66 :          IF (almo_scf_env%almo_history%istore > (almo_scf_env%almo_history%nstore + 1)) THEN
     463            2 :             IF (almo_scf_env%opt_block_diag_pcg%eps_error_early > 0.0_dp) THEN
     464            0 :                almo_scf_env%opt_block_diag_pcg%eps_error = almo_scf_env%opt_block_diag_pcg%eps_error_early
     465            0 :                almo_scf_env%opt_block_diag_pcg%early_stopping_on = .TRUE.
     466            0 :                IF (unit_nr > 0) WRITE (unit_nr, "(/,T2,A)") "ALMO_OPTIMIZER_PCG: EPS_ERROR_EARLY is on"
     467              :             END IF
     468            2 :             IF (almo_scf_env%opt_block_diag_diis%eps_error_early > 0.0_dp) THEN
     469            0 :                almo_scf_env%opt_block_diag_diis%eps_error = almo_scf_env%opt_block_diag_diis%eps_error_early
     470            0 :                almo_scf_env%opt_block_diag_diis%early_stopping_on = .TRUE.
     471            0 :                IF (unit_nr > 0) WRITE (unit_nr, "(/,T2,A)") "ALMO_OPTIMIZER_DIIS: EPS_ERROR_EARLY is on"
     472              :             END IF
     473            2 :             IF (almo_scf_env%opt_block_diag_pcg%max_iter_early > 0) THEN
     474            0 :                almo_scf_env%opt_block_diag_pcg%max_iter = almo_scf_env%opt_block_diag_pcg%max_iter_early
     475            0 :                almo_scf_env%opt_block_diag_pcg%early_stopping_on = .TRUE.
     476            0 :                IF (unit_nr > 0) WRITE (unit_nr, "(/,T2,A)") "ALMO_OPTIMIZER_PCG: MAX_ITER_EARLY is on"
     477              :             END IF
     478            2 :             IF (almo_scf_env%opt_block_diag_diis%max_iter_early > 0) THEN
     479            0 :                almo_scf_env%opt_block_diag_diis%max_iter = almo_scf_env%opt_block_diag_diis%max_iter_early
     480            0 :                almo_scf_env%opt_block_diag_diis%early_stopping_on = .TRUE.
     481            0 :                IF (unit_nr > 0) WRITE (unit_nr, "(/,T2,A)") "ALMO_OPTIMIZER_DIIS: MAX_ITER_EARLY is on"
     482              :             END IF
     483              :          ELSE
     484           64 :             almo_scf_env%opt_block_diag_diis%early_stopping_on = .FALSE.
     485           64 :             almo_scf_env%opt_block_diag_pcg%early_stopping_on = .FALSE.
     486              :          END IF
     487           66 :          IF (almo_scf_env%xalmo_history%istore > (almo_scf_env%xalmo_history%nstore + 1)) THEN
     488            4 :             IF (almo_scf_env%opt_xalmo_pcg%eps_error_early > 0.0_dp) THEN
     489            0 :                almo_scf_env%opt_xalmo_pcg%eps_error = almo_scf_env%opt_xalmo_pcg%eps_error_early
     490            0 :                almo_scf_env%opt_xalmo_pcg%early_stopping_on = .TRUE.
     491            0 :                IF (unit_nr > 0) WRITE (unit_nr, "(/,T2,A)") "XALMO_OPTIMIZER_PCG: EPS_ERROR_EARLY is on"
     492              :             END IF
     493            4 :             IF (almo_scf_env%opt_xalmo_pcg%max_iter_early > 0.0_dp) THEN
     494            0 :                almo_scf_env%opt_xalmo_pcg%max_iter = almo_scf_env%opt_xalmo_pcg%max_iter_early
     495            0 :                almo_scf_env%opt_xalmo_pcg%early_stopping_on = .TRUE.
     496            0 :                IF (unit_nr > 0) WRITE (unit_nr, "(/,T2,A)") "XALMO_OPTIMIZER_PCG: MAX_ITER_EARLY is on"
     497              :             END IF
     498              :          ELSE
     499           62 :             almo_scf_env%opt_xalmo_pcg%early_stopping_on = .FALSE.
     500              :          END IF
     501              :       END IF
     502              : 
     503              :       ! create all matrices
     504          122 :       CALL almo_scf_env_create_matrices(almo_scf_env, matrix_s(1)%matrix)
     505              : 
     506              :       ! set up matrix S and all required functions of S
     507          122 :       almo_scf_env%s_inv_done = .FALSE.
     508          122 :       almo_scf_env%s_sqrt_done = .FALSE.
     509          122 :       CALL almo_scf_init_ao_overlap(matrix_s(1)%matrix, almo_scf_env)
     510              : 
     511              :       ! create the quencher (imposes sparsity template)
     512          122 :       CALL almo_scf_construct_quencher(qs_env, almo_scf_env)
     513          122 :       CALL distribute_domains(almo_scf_env)
     514              : 
     515              :       ! FINISH setting job parameters here, print out job info
     516          122 :       CALL almo_scf_print_job_info(almo_scf_env, unit_nr)
     517              : 
     518              :       ! allocate and init the domain preconditioner
     519         1450 :       ALLOCATE (almo_scf_env%domain_preconditioner(ndomains, nspins))
     520          122 :       CALL init_submatrices(almo_scf_env%domain_preconditioner)
     521              : 
     522              :       ! allocate and init projected KS for domains
     523         1328 :       ALLOCATE (almo_scf_env%domain_ks_xx(ndomains, nspins))
     524          122 :       CALL init_submatrices(almo_scf_env%domain_ks_xx)
     525              : 
     526              :       ! init ao-overlap subblocks
     527         1328 :       ALLOCATE (almo_scf_env%domain_s_inv(ndomains, nspins))
     528          122 :       CALL init_submatrices(almo_scf_env%domain_s_inv)
     529         1328 :       ALLOCATE (almo_scf_env%domain_s_sqrt_inv(ndomains, nspins))
     530          122 :       CALL init_submatrices(almo_scf_env%domain_s_sqrt_inv)
     531         1328 :       ALLOCATE (almo_scf_env%domain_s_sqrt(ndomains, nspins))
     532          122 :       CALL init_submatrices(almo_scf_env%domain_s_sqrt)
     533         1328 :       ALLOCATE (almo_scf_env%domain_t(ndomains, nspins))
     534          122 :       CALL init_submatrices(almo_scf_env%domain_t)
     535         1328 :       ALLOCATE (almo_scf_env%domain_err(ndomains, nspins))
     536          122 :       CALL init_submatrices(almo_scf_env%domain_err)
     537         1328 :       ALLOCATE (almo_scf_env%domain_r_down_up(ndomains, nspins))
     538          122 :       CALL init_submatrices(almo_scf_env%domain_r_down_up)
     539              : 
     540              :       ! initialization of the KS matrix
     541              :       CALL init_almo_ks_matrix_via_qs(qs_env, &
     542              :                                       almo_scf_env%matrix_ks, &
     543              :                                       almo_scf_env%mat_distr_aos, &
     544          122 :                                       almo_scf_env%eps_filter)
     545          122 :       CALL construct_qs_mos(qs_env, almo_scf_env)
     546              : 
     547          122 :       CALL timestop(handle)
     548              : 
     549          244 :    END SUBROUTINE almo_scf_init
     550              : 
     551              : ! **************************************************************************************************
     552              : !> \brief create the scf initial guess for ALMOs
     553              : !> \param qs_env ...
     554              : !> \param almo_scf_env ...
     555              : !> \par History
     556              : !>       2016.11 created [Rustam Z Khaliullin]
     557              : !>       2018.09 smearing support [Ruben Staub]
     558              : !> \author Rustam Z Khaliullin
     559              : ! **************************************************************************************************
     560          122 :    SUBROUTINE almo_scf_initial_guess(qs_env, almo_scf_env)
     561              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     562              :       TYPE(almo_scf_env_type), INTENT(INOUT)             :: almo_scf_env
     563              : 
     564              :       CHARACTER(len=*), PARAMETER :: routineN = 'almo_scf_initial_guess'
     565              : 
     566              :       CHARACTER(LEN=default_path_length)                 :: file_name, project_name
     567              :       INTEGER                                            :: handle, iaspc, ispin, istore, naspc, &
     568              :                                                             nspins, unit_nr
     569              :       INTEGER, DIMENSION(2)                              :: nelectron_spin
     570              :       LOGICAL                                            :: aspc_guess, has_unit_metric
     571              :       REAL(KIND=dp)                                      :: alpha, cs_pos, energy, kTS_sum
     572          122 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
     573              :       TYPE(cp_logger_type), POINTER                      :: logger
     574              :       TYPE(dbcsr_distribution_type)                      :: dist
     575          122 :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: matrix_s, rho_ao
     576              :       TYPE(dft_control_type), POINTER                    :: dft_control
     577              :       TYPE(molecular_scf_guess_env_type), POINTER        :: mscfg_env
     578              :       TYPE(mp_para_env_type), POINTER                    :: para_env
     579          122 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     580          122 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
     581              :       TYPE(qs_rho_type), POINTER                         :: rho
     582              : 
     583          122 :       CALL timeset(routineN, handle)
     584              : 
     585          122 :       NULLIFY (rho, rho_ao)
     586              : 
     587              :       ! get a useful output_unit
     588          122 :       logger => cp_get_default_logger()
     589          122 :       IF (logger%para_env%is_source()) THEN
     590           61 :          unit_nr = cp_logger_get_default_unit_nr(logger, local=.TRUE.)
     591              :       ELSE
     592           61 :          unit_nr = -1
     593              :       END IF
     594              : 
     595              :       ! get basic quantities from the qs_env
     596              :       CALL get_qs_env(qs_env, &
     597              :                       dft_control=dft_control, &
     598              :                       matrix_s=matrix_s, &
     599              :                       atomic_kind_set=atomic_kind_set, &
     600              :                       qs_kind_set=qs_kind_set, &
     601              :                       particle_set=particle_set, &
     602              :                       has_unit_metric=has_unit_metric, &
     603              :                       para_env=para_env, &
     604              :                       nelectron_spin=nelectron_spin, &
     605              :                       mscfg_env=mscfg_env, &
     606          122 :                       rho=rho)
     607              : 
     608          122 :       CALL qs_rho_get(rho, rho_ao=rho_ao)
     609          122 :       CPASSERT(ASSOCIATED(mscfg_env))
     610              : 
     611              :       ! initial guess on the first simulation step is determined by almo_scf_env%almo_scf_guess
     612              :       ! the subsequent simulation steps are determined by extrapolation_order
     613              :       ! if extrapolation order is zero then again almo_scf_env%almo_scf_guess is used
     614              :       ! ... the number of stored history points will remain zero if extrapolation order is zero
     615          122 :       IF (almo_scf_env%almo_history%istore == 0) THEN
     616              :          aspc_guess = .FALSE.
     617              :       ELSE
     618           46 :          aspc_guess = .TRUE.
     619              :       END IF
     620              : 
     621          122 :       nspins = almo_scf_env%nspins
     622              : 
     623              :       ! create an initial guess
     624          122 :       IF (.NOT. aspc_guess) THEN
     625              : 
     626           92 :          SELECT CASE (almo_scf_env%almo_scf_guess)
     627              :          CASE (molecular_guess)
     628              : 
     629           38 :             DO ispin = 1, nspins
     630              : 
     631              :                ! the calculations on "isolated" molecules has already been done
     632              :                ! all we need to do is convert the MOs of molecules into
     633              :                ! the ALMO matrix taking into account different distributions
     634              :                CALL get_matrix_from_submatrices(mscfg_env, &
     635           22 :                                                 almo_scf_env%matrix_t_blk(ispin), ispin)
     636              :                CALL dbcsr_filter(almo_scf_env%matrix_t_blk(ispin), &
     637           38 :                                  almo_scf_env%eps_filter)
     638              : 
     639              :             END DO
     640              : 
     641              :          CASE (atomic_guess)
     642              : 
     643           60 :             IF (dft_control%qs_control%dftb .OR. dft_control%qs_control%semi_empirical .OR. &
     644              :                 dft_control%qs_control%xtb) THEN
     645              :                CALL calculate_mopac_dm(rho_ao, &
     646              :                                        matrix_s(1)%matrix, has_unit_metric, &
     647              :                                        dft_control, particle_set, atomic_kind_set, qs_kind_set, &
     648              :                                        nspins, nelectron_spin, &
     649            0 :                                        para_env)
     650              :             ELSE
     651              :                CALL calculate_atomic_block_dm(rho_ao, matrix_s(1)%matrix, atomic_kind_set, qs_kind_set, &
     652           60 :                                               nspins, nelectron_spin, unit_nr, para_env)
     653              :             END IF
     654              : 
     655          120 :             DO ispin = 1, nspins
     656              :                ! copy the atomic-block dm into matrix_p_blk
     657              :                CALL matrix_qs_to_almo(rho_ao(ispin)%matrix, &
     658           60 :                                       almo_scf_env%matrix_p_blk(ispin), almo_scf_env%mat_distr_aos)
     659              :                CALL dbcsr_filter(almo_scf_env%matrix_p_blk(ispin), &
     660          120 :                                  almo_scf_env%eps_filter)
     661              :             END DO ! ispin
     662              : 
     663              :             ! obtain orbitals from the density matrix
     664              :             ! (the current version of ALMO SCF needs orbitals)
     665           60 :             CALL almo_scf_p_blk_to_t_blk(almo_scf_env, ionic=.FALSE.)
     666              : 
     667              :          CASE (restart_guess)
     668              : 
     669            0 :             project_name = logger%iter_info%project_name
     670              : 
     671           76 :             DO ispin = 1, nspins
     672            0 :                WRITE (file_name, '(A,I0,A)') TRIM(project_name)//"_ALMO_SPIN_", ispin, "_RESTART.mo"
     673            0 :                CALL dbcsr_get_info(almo_scf_env%matrix_t_blk(ispin), distribution=dist)
     674            0 :                CALL dbcsr_binary_read(file_name, distribution=dist, matrix_new=almo_scf_env%matrix_t_blk(ispin))
     675            0 :                cs_pos = dbcsr_checksum(almo_scf_env%matrix_t_blk(ispin), pos=.TRUE.)
     676            0 :                IF (unit_nr > 0) THEN
     677            0 :                   WRITE (unit_nr, '(T2,A,E20.8)') "Read restart ALMO "//TRIM(file_name)//" with checksum: ", cs_pos
     678              :                END IF
     679              :             END DO
     680              :          END SELECT
     681              : 
     682              :       ELSE !aspc_guess
     683              : 
     684           46 :          CALL cite_reference(Kolafa2004)
     685           46 :          CALL cite_reference(Kuhne2007)
     686              : 
     687           46 :          naspc = MIN(almo_scf_env%almo_history%istore, almo_scf_env%almo_history%nstore)
     688           46 :          IF (unit_nr > 0) THEN
     689              :             WRITE (unit_nr, FMT="(/,T2,A,/,/,T3,A,I0)") &
     690           23 :                "Parameters for the always stable predictor-corrector (ASPC) method:", &
     691           46 :                "ASPC order: ", naspc
     692              :          END IF
     693              : 
     694           92 :          DO ispin = 1, nspins
     695              : 
     696              :             ! extrapolation
     697          186 :             DO iaspc = 1, naspc
     698           94 :                istore = MOD(almo_scf_env%almo_history%istore - iaspc, almo_scf_env%almo_history%nstore) + 1
     699              :                alpha = (-1.0_dp)**(iaspc + 1)*REAL(iaspc, KIND=dp)* &
     700           94 :                        binomial(2*naspc, naspc - iaspc)/binomial(2*naspc - 2, naspc - 1)
     701           94 :                IF (unit_nr > 0) THEN
     702              :                   WRITE (unit_nr, FMT="(T3,A2,I0,A4,F10.6)") &
     703           47 :                      "B(", iaspc, ") = ", alpha
     704              :                END IF
     705          140 :                IF (iaspc == 1) THEN
     706              :                   CALL dbcsr_copy(almo_scf_env%matrix_t_blk(ispin), &
     707              :                                   almo_scf_env%almo_history%matrix_t(ispin), &
     708           46 :                                   keep_sparsity=.TRUE.)
     709           46 :                   CALL dbcsr_scale(almo_scf_env%matrix_t_blk(ispin), alpha)
     710              :                ELSE
     711              :                   CALL dbcsr_multiply("N", "N", alpha, &
     712              :                                       almo_scf_env%almo_history%matrix_p_up_down(ispin, istore), &
     713              :                                       almo_scf_env%almo_history%matrix_t(ispin), &
     714              :                                       1.0_dp, almo_scf_env%matrix_t_blk(ispin), &
     715           48 :                                       retain_sparsity=.TRUE.)
     716              :                END IF
     717              :             END DO !iaspc
     718              : 
     719              :          END DO !ispin
     720              : 
     721              :       END IF !aspc_guess?
     722              : 
     723          250 :       DO ispin = 1, nspins
     724              : 
     725              :          CALL orthogonalize_mos(ket=almo_scf_env%matrix_t_blk(ispin), &
     726              :                                 overlap=almo_scf_env%matrix_sigma_blk(ispin), &
     727              :                                 metric=almo_scf_env%matrix_s_blk(1), &
     728              :                                 retain_locality=.TRUE., &
     729              :                                 only_normalize=.FALSE., &
     730              :                                 nocc_of_domain=almo_scf_env%nocc_of_domain(:, ispin), &
     731              :                                 eps_filter=almo_scf_env%eps_filter, &
     732              :                                 order_lanczos=almo_scf_env%order_lanczos, &
     733              :                                 eps_lanczos=almo_scf_env%eps_lanczos, &
     734          128 :                                 max_iter_lanczos=almo_scf_env%max_iter_lanczos)
     735              : 
     736              :          !! Application of an occupation-rescaling trick for smearing, if requested
     737          128 :          IF (almo_scf_env%smear) THEN
     738              :             CALL almo_scf_t_rescaling(matrix_t=almo_scf_env%matrix_t_blk(ispin), &
     739              :                                       mo_energies=almo_scf_env%mo_energies(:, ispin), &
     740              :                                       mu_of_domain=almo_scf_env%mu_of_domain(:, ispin), &
     741              :                                       real_ne_of_domain=almo_scf_env%real_ne_of_domain(:, ispin), &
     742              :                                       spin_kTS=almo_scf_env%kTS(ispin), &
     743              :                                       smear_e_temp=almo_scf_env%smear_e_temp, &
     744              :                                       ndomains=almo_scf_env%ndomains, &
     745            4 :                                       nocc_of_domain=almo_scf_env%nocc_of_domain(:, ispin))
     746              :          END IF
     747              : 
     748              :          CALL almo_scf_t_to_proj(t=almo_scf_env%matrix_t_blk(ispin), &
     749              :                                  p=almo_scf_env%matrix_p(ispin), &
     750              :                                  eps_filter=almo_scf_env%eps_filter, &
     751              :                                  orthog_orbs=.FALSE., &
     752              :                                  nocc_of_domain=almo_scf_env%nocc_of_domain(:, ispin), &
     753              :                                  s=almo_scf_env%matrix_s(1), &
     754              :                                  sigma=almo_scf_env%matrix_sigma(ispin), &
     755              :                                  sigma_inv=almo_scf_env%matrix_sigma_inv(ispin), &
     756              :                                  use_guess=.FALSE., &
     757              :                                  smear=almo_scf_env%smear, &
     758              :                                  algorithm=almo_scf_env%sigma_inv_algorithm, &
     759              :                                  eps_lanczos=almo_scf_env%eps_lanczos, &
     760              :                                  max_iter_lanczos=almo_scf_env%max_iter_lanczos, &
     761              :                                  inv_eps_factor=almo_scf_env%matrix_iter_eps_error_factor, &
     762              :                                  para_env=almo_scf_env%para_env, &
     763          250 :                                  blacs_env=almo_scf_env%blacs_env)
     764              : 
     765              :       END DO
     766              : 
     767              :       ! compute dm from the projector(s)
     768          122 :       IF (nspins == 1) THEN
     769          116 :          CALL dbcsr_scale(almo_scf_env%matrix_p(1), 2.0_dp)
     770              :          !! Rescaling electronic entropy contribution by spin_factor
     771          116 :          IF (almo_scf_env%smear) THEN
     772            4 :             almo_scf_env%kTS(1) = almo_scf_env%kTS(1)*2.0_dp
     773              :          END IF
     774              :       END IF
     775              : 
     776          122 :       IF (almo_scf_env%smear) THEN
     777            8 :          kTS_sum = SUM(almo_scf_env%kTS)
     778              :       ELSE
     779          118 :          kTS_sum = 0.0_dp
     780              :       END IF
     781              : 
     782              :       CALL almo_dm_to_almo_ks(qs_env, &
     783              :                               almo_scf_env%matrix_p, &
     784              :                               almo_scf_env%matrix_ks, &
     785              :                               energy, &
     786              :                               almo_scf_env%eps_filter, &
     787              :                               almo_scf_env%mat_distr_aos, &
     788              :                               smear=almo_scf_env%smear, &
     789          122 :                               kTS_sum=kTS_sum)
     790              : 
     791          122 :       IF (unit_nr > 0) THEN
     792           61 :          IF (almo_scf_env%almo_scf_guess == molecular_guess) THEN
     793            8 :             WRITE (unit_nr, '(T2,A38,F40.10)') "Single-molecule energy:", &
     794           38 :                SUM(mscfg_env%energy_of_frag)
     795              :          END IF
     796           61 :          WRITE (unit_nr, '(T2,A38,F40.10)') "Energy of the initial guess:", energy
     797           61 :          WRITE (unit_nr, '()')
     798              :       END IF
     799              : 
     800          122 :       CALL timestop(handle)
     801              : 
     802          122 :    END SUBROUTINE almo_scf_initial_guess
     803              : 
     804              : ! **************************************************************************************************
     805              : !> \brief store a history of matrices for later use in almo_scf_initial_guess
     806              : !> \param almo_scf_env ...
     807              : !> \par History
     808              : !>       2016.11 created [Rustam Z Khaliullin]
     809              : !> \author Rustam Khaliullin
     810              : ! **************************************************************************************************
     811          122 :    SUBROUTINE almo_scf_store_extrapolation_data(almo_scf_env)
     812              :       TYPE(almo_scf_env_type), INTENT(INOUT)             :: almo_scf_env
     813              : 
     814              :       CHARACTER(len=*), PARAMETER :: routineN = 'almo_scf_store_extrapolation_data'
     815              : 
     816              :       INTEGER                                            :: handle, ispin, istore, unit_nr
     817              :       LOGICAL :: delocalization_uses_extrapolation
     818              :       TYPE(cp_logger_type), POINTER                      :: logger
     819              :       TYPE(dbcsr_type)                                   :: matrix_no_tmp1, matrix_no_tmp2, &
     820              :                                                             matrix_no_tmp3, matrix_no_tmp4
     821              : 
     822          122 :       CALL timeset(routineN, handle)
     823              : 
     824              :       ! get a useful output_unit
     825          122 :       logger => cp_get_default_logger()
     826          122 :       IF (logger%para_env%is_source()) THEN
     827           61 :          unit_nr = cp_logger_get_default_unit_nr(logger, local=.TRUE.)
     828              :       ELSE
     829              :          unit_nr = -1
     830              :       END IF
     831              : 
     832          122 :       IF (almo_scf_env%almo_history%nstore > 0) THEN
     833              : 
     834          116 :          almo_scf_env%almo_history%istore = almo_scf_env%almo_history%istore + 1
     835              : 
     836          238 :          DO ispin = 1, SIZE(almo_scf_env%matrix_t_blk)
     837              : 
     838          122 :             istore = MOD(almo_scf_env%almo_history%istore - 1, almo_scf_env%almo_history%nstore) + 1
     839              : 
     840          122 :             IF (almo_scf_env%almo_history%istore == 1) THEN
     841              :                CALL dbcsr_create(almo_scf_env%almo_history%matrix_t(ispin), &
     842              :                                  template=almo_scf_env%matrix_t_blk(ispin), &
     843           76 :                                  matrix_type=dbcsr_type_no_symmetry)
     844              :             END IF
     845              :             CALL dbcsr_copy(almo_scf_env%almo_history%matrix_t(ispin), &
     846          122 :                             almo_scf_env%matrix_t_blk(ispin))
     847              : 
     848          122 :             IF (almo_scf_env%almo_history%istore <= almo_scf_env%almo_history%nstore) THEN
     849              :                CALL dbcsr_create(almo_scf_env%almo_history%matrix_p_up_down(ispin, istore), &
     850              :                                  template=almo_scf_env%matrix_s(1), &
     851          100 :                                  matrix_type=dbcsr_type_no_symmetry)
     852              :             END IF
     853              : 
     854              :             CALL dbcsr_create(matrix_no_tmp1, template=almo_scf_env%matrix_t_blk(ispin), &
     855          122 :                               matrix_type=dbcsr_type_no_symmetry)
     856              :             CALL dbcsr_create(matrix_no_tmp2, template=almo_scf_env%matrix_t_blk(ispin), &
     857          122 :                               matrix_type=dbcsr_type_no_symmetry)
     858              : 
     859              :             ! compute contra-covariant density matrix
     860              :             CALL dbcsr_multiply("N", "N", 1.0_dp, almo_scf_env%matrix_s(1), &
     861              :                                 almo_scf_env%matrix_t_blk(ispin), &
     862              :                                 0.0_dp, matrix_no_tmp1, &
     863          122 :                                 filter_eps=almo_scf_env%eps_filter)
     864              :             CALL dbcsr_multiply("N", "N", 1.0_dp, matrix_no_tmp1, &
     865              :                                 almo_scf_env%matrix_sigma_inv_0deloc(ispin), &
     866              :                                 0.0_dp, matrix_no_tmp2, &
     867          122 :                                 filter_eps=almo_scf_env%eps_filter)
     868              :             CALL dbcsr_multiply("N", "T", 1.0_dp, &
     869              :                                 almo_scf_env%matrix_t_blk(ispin), &
     870              :                                 matrix_no_tmp2, &
     871              :                                 0.0_dp, almo_scf_env%almo_history%matrix_p_up_down(ispin, istore), &
     872          122 :                                 filter_eps=almo_scf_env%eps_filter)
     873              : 
     874          122 :             CALL dbcsr_release(matrix_no_tmp1)
     875          238 :             CALL dbcsr_release(matrix_no_tmp2)
     876              : 
     877              :          END DO
     878              : 
     879              :       END IF
     880              : 
     881              :       ! exrapolate xalmos?
     882              :       delocalization_uses_extrapolation = &
     883              :          .NOT. ((almo_scf_env%deloc_method == almo_deloc_none) .OR. &
     884          122 :                 (almo_scf_env%deloc_method == almo_deloc_xalmo_1diag))
     885          122 :       IF (almo_scf_env%xalmo_history%nstore > 0 .AND. &
     886              :           delocalization_uses_extrapolation) THEN
     887              : 
     888           44 :          almo_scf_env%xalmo_history%istore = almo_scf_env%xalmo_history%istore + 1
     889              : 
     890           88 :          DO ispin = 1, SIZE(almo_scf_env%matrix_t)
     891              : 
     892           44 :             istore = MOD(almo_scf_env%xalmo_history%istore - 1, almo_scf_env%xalmo_history%nstore) + 1
     893              : 
     894           44 :             IF (almo_scf_env%xalmo_history%istore == 1) THEN
     895              :                CALL dbcsr_create(almo_scf_env%xalmo_history%matrix_t(ispin), &
     896              :                                  template=almo_scf_env%matrix_t(ispin), &
     897           10 :                                  matrix_type=dbcsr_type_no_symmetry)
     898              :             END IF
     899              :             CALL dbcsr_copy(almo_scf_env%xalmo_history%matrix_t(ispin), &
     900           44 :                             almo_scf_env%matrix_t(ispin))
     901              : 
     902           44 :             IF (almo_scf_env%xalmo_history%istore <= almo_scf_env%xalmo_history%nstore) THEN
     903              :                !CALL dbcsr_init(almo_scf_env%xalmo_history%matrix_x(ispin, istore))
     904              :                !CALL dbcsr_create(almo_scf_env%xalmo_history%matrix_x(ispin, istore), &
     905              :                !        template=almo_scf_env%matrix_t(ispin), &
     906              :                !        matrix_type=dbcsr_type_no_symmetry)
     907              :                CALL dbcsr_create(almo_scf_env%xalmo_history%matrix_p_up_down(ispin, istore), &
     908              :                                  template=almo_scf_env%matrix_s(1), &
     909           24 :                                  matrix_type=dbcsr_type_no_symmetry)
     910              :             END IF
     911              : 
     912              :             CALL dbcsr_create(matrix_no_tmp3, template=almo_scf_env%matrix_t(ispin), &
     913           44 :                               matrix_type=dbcsr_type_no_symmetry)
     914              :             CALL dbcsr_create(matrix_no_tmp4, template=almo_scf_env%matrix_t(ispin), &
     915           44 :                               matrix_type=dbcsr_type_no_symmetry)
     916              : 
     917              :             ! compute contra-covariant density matrix
     918              :             CALL dbcsr_multiply("N", "N", 1.0_dp, almo_scf_env%matrix_s(1), &
     919              :                                 almo_scf_env%matrix_t(ispin), &
     920              :                                 0.0_dp, matrix_no_tmp3, &
     921           44 :                                 filter_eps=almo_scf_env%eps_filter)
     922              :             CALL dbcsr_multiply("N", "N", 1.0_dp, matrix_no_tmp3, &
     923              :                                 almo_scf_env%matrix_sigma_inv(ispin), &
     924              :                                 0.0_dp, matrix_no_tmp4, &
     925           44 :                                 filter_eps=almo_scf_env%eps_filter)
     926              :             CALL dbcsr_multiply("N", "T", 1.0_dp, &
     927              :                                 almo_scf_env%matrix_t(ispin), &
     928              :                                 matrix_no_tmp4, &
     929              :                                 0.0_dp, almo_scf_env%xalmo_history%matrix_p_up_down(ispin, istore), &
     930           44 :                                 filter_eps=almo_scf_env%eps_filter)
     931              : 
     932              :             ! store the difference between t and t0
     933              :             !CALL dbcsr_copy(almo_scf_env%xalmo_history%matrix_x(ispin, istore),&
     934              :             !        almo_scf_env%matrix_t(ispin))
     935              :             !CALL dbcsr_add(almo_scf_env%xalmo_history%matrix_x(ispin, istore),&
     936              :             !        almo_scf_env%matrix_t_blk(ispin),1.0_dp,-1.0_dp)
     937              : 
     938           44 :             CALL dbcsr_release(matrix_no_tmp3)
     939           88 :             CALL dbcsr_release(matrix_no_tmp4)
     940              : 
     941              :          END DO
     942              : 
     943              :       END IF
     944              : 
     945          122 :       CALL timestop(handle)
     946              : 
     947          122 :    END SUBROUTINE almo_scf_store_extrapolation_data
     948              : 
     949              : ! **************************************************************************************************
     950              : !> \brief Prints out a short summary about the ALMO SCF job
     951              : !> \param almo_scf_env ...
     952              : !> \param unit_nr ...
     953              : !> \par History
     954              : !>       2011.10 created [Rustam Z Khaliullin]
     955              : !> \author Rustam Z Khaliullin
     956              : ! **************************************************************************************************
     957          122 :    SUBROUTINE almo_scf_print_job_info(almo_scf_env, unit_nr)
     958              : 
     959              :       TYPE(almo_scf_env_type), INTENT(INOUT)             :: almo_scf_env
     960              :       INTEGER, INTENT(IN)                                :: unit_nr
     961              : 
     962              :       CHARACTER(len=*), PARAMETER :: routineN = 'almo_scf_print_job_info'
     963              : 
     964              :       CHARACTER(len=13)                                  :: neig_string
     965              :       CHARACTER(len=33)                                  :: deloc_method_string
     966              :       INTEGER                                            :: handle, idomain, index1_prev, sum_temp
     967          122 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: nneighbors
     968              : 
     969          122 :       CALL timeset(routineN, handle)
     970              : 
     971          122 :       IF (unit_nr > 0) THEN
     972           61 :          WRITE (unit_nr, '()')
     973           61 :          WRITE (unit_nr, '(T2,A,A,A)') REPEAT("-", 32), " ALMO SETTINGS ", REPEAT("-", 32)
     974              : 
     975           61 :          WRITE (unit_nr, '(T2,A,T48,E33.3)') "eps_filter:", almo_scf_env%eps_filter
     976              : 
     977           61 :          IF (almo_scf_env%almo_update_algorithm == almo_scf_skip) THEN
     978           17 :             WRITE (unit_nr, '(T2,A)') "skip optimization of block-diagonal ALMOs"
     979              :          ELSE
     980           44 :             WRITE (unit_nr, '(T2,A)') "optimization of block-diagonal ALMOs:"
     981           82 :             SELECT CASE (almo_scf_env%almo_update_algorithm)
     982              :             CASE (almo_scf_diag)
     983              :                ! the DIIS algorith is the only choice for the diagonlaization-based algorithm
     984           38 :                CALL print_optimizer_options(almo_scf_env%opt_block_diag_diis, unit_nr)
     985              :             CASE (almo_scf_pcg)
     986              :                ! print out PCG options
     987            5 :                CALL print_optimizer_options(almo_scf_env%opt_block_diag_pcg, unit_nr)
     988              :             CASE (almo_scf_trustr)
     989              :                ! print out TRUST REGION options
     990           44 :                CALL print_optimizer_options(almo_scf_env%opt_block_diag_trustr, unit_nr)
     991              :             END SELECT
     992              :          END IF
     993              : 
     994           79 :          SELECT CASE (almo_scf_env%deloc_method)
     995              :          CASE (almo_deloc_none)
     996           18 :             deloc_method_string = "NONE"
     997              :          CASE (almo_deloc_x)
     998            2 :             deloc_method_string = "FULL_X"
     999              :          CASE (almo_deloc_scf)
    1000            6 :             deloc_method_string = "FULL_SCF"
    1001              :          CASE (almo_deloc_x_then_scf)
    1002            7 :             deloc_method_string = "FULL_X_THEN_SCF"
    1003              :          CASE (almo_deloc_xalmo_1diag)
    1004            1 :             deloc_method_string = "XALMO_1DIAG"
    1005              :          CASE (almo_deloc_xalmo_x)
    1006            3 :             deloc_method_string = "XALMO_X"
    1007              :          CASE (almo_deloc_xalmo_scf)
    1008           61 :             deloc_method_string = "XALMO_SCF"
    1009              :          END SELECT
    1010           61 :          WRITE (unit_nr, '(T2,A,T48,A33)') "delocalization:", TRIM(deloc_method_string)
    1011              : 
    1012           61 :          IF (almo_scf_env%deloc_method /= almo_deloc_none) THEN
    1013              : 
    1014           15 :             SELECT CASE (almo_scf_env%deloc_method)
    1015              :             CASE (almo_deloc_x, almo_deloc_scf, almo_deloc_x_then_scf)
    1016           15 :                WRITE (unit_nr, '(T2,A,T48,A33)') "delocalization cutoff radius:", &
    1017           30 :                   "infinite"
    1018           15 :                deloc_method_string = "FULL_X_THEN_SCF"
    1019              :             CASE (almo_deloc_xalmo_1diag, almo_deloc_xalmo_x, almo_deloc_xalmo_scf)
    1020           28 :                WRITE (unit_nr, '(T2,A,T48,F33.5)') "XALMO cutoff radius:", &
    1021           71 :                   almo_scf_env%quencher_r0_factor
    1022              :             END SELECT
    1023              : 
    1024           43 :             IF (almo_scf_env%deloc_method == almo_deloc_xalmo_1diag) THEN
    1025              :                ! print nothing because no actual optimization is done
    1026              :             ELSE
    1027           42 :                WRITE (unit_nr, '(T2,A)') "optimization of extended orbitals:"
    1028           42 :                SELECT CASE (almo_scf_env%xalmo_update_algorithm)
    1029              :                CASE (almo_scf_diag)
    1030            0 :                   CALL print_optimizer_options(almo_scf_env%opt_xalmo_diis, unit_nr)
    1031              :                CASE (almo_scf_trustr)
    1032            8 :                   CALL print_optimizer_options(almo_scf_env%opt_xalmo_trustr, unit_nr)
    1033              :                CASE (almo_scf_pcg)
    1034           42 :                   CALL print_optimizer_options(almo_scf_env%opt_xalmo_pcg, unit_nr)
    1035              :                END SELECT
    1036              :             END IF
    1037              : 
    1038              :          END IF
    1039              : 
    1040              :          !SELECT CASE(almo_scf_env%domain_layout_mos)
    1041              :          !CASE(almo_domain_layout_orbital)
    1042              :          !    WRITE(unit_nr,'(T2,A,T48,A33)') "Delocalization constraints","ORBITAL"
    1043              :          !CASE(almo_domain_layout_atomic)
    1044              :          !    WRITE(unit_nr,'(T2,A,T48,A33)') "Delocalization constraints","ATOMIC"
    1045              :          !CASE(almo_domain_layout_molecular)
    1046              :          !    WRITE(unit_nr,'(T2,A,T48,A33)') "Delocalization constraints","MOLECULAR"
    1047              :          !END SELECT
    1048              : 
    1049              :          !SELECT CASE(almo_scf_env%domain_layout_aos)
    1050              :          !CASE(almo_domain_layout_atomic)
    1051              :          !    WRITE(unit_nr,'(T2,A,T48,A33)') "Basis function domains","ATOMIC"
    1052              :          !CASE(almo_domain_layout_molecular)
    1053              :          !    WRITE(unit_nr,'(T2,A,T48,A33)') "Basis function domains","MOLECULAR"
    1054              :          !END SELECT
    1055              : 
    1056              :          !SELECT CASE(almo_scf_env%mat_distr_aos)
    1057              :          !CASE(almo_mat_distr_atomic)
    1058              :          !    WRITE(unit_nr,'(T2,A,T48,A33)') "Parallel distribution for AOs","ATOMIC"
    1059              :          !CASE(almo_mat_distr_molecular)
    1060              :          !    WRITE(unit_nr,'(T2,A,T48,A33)') "Parallel distribution for AOs","MOLECULAR"
    1061              :          !END SELECT
    1062              : 
    1063              :          !SELECT CASE(almo_scf_env%mat_distr_mos)
    1064              :          !CASE(almo_mat_distr_atomic)
    1065              :          !    WRITE(unit_nr,'(T2,A,T48,A33)') "Parallel distribution for MOs","ATOMIC"
    1066              :          !CASE(almo_mat_distr_molecular)
    1067              :          !    WRITE(unit_nr,'(T2,A,T48,A33)') "Parallel distribution for MOs","MOLECULAR"
    1068              :          !END SELECT
    1069              : 
    1070              :          ! print fragment's statistics
    1071           61 :          WRITE (unit_nr, '(T2,A)') REPEAT("-", 79)
    1072           61 :          WRITE (unit_nr, '(T2,A,T48,I33)') "Total fragments:", &
    1073          122 :             almo_scf_env%ndomains
    1074              : 
    1075          472 :          sum_temp = SUM(almo_scf_env%nbasis_of_domain(:))
    1076              :          WRITE (unit_nr, '(T2,A,T53,I5,F9.2,I5,I9)') &
    1077           61 :             "Basis set size per fragment (min, av, max, total):", &
    1078          472 :             MINVAL(almo_scf_env%nbasis_of_domain(:)), &
    1079           61 :             (1.0_dp*sum_temp)/almo_scf_env%ndomains, &
    1080          472 :             MAXVAL(almo_scf_env%nbasis_of_domain(:)), &
    1081          122 :             sum_temp
    1082              :          !WRITE (unit_nr, '(T2,I13,F13.3,I13,I13)') &
    1083              :          !         MINVAL(almo_scf_env%nbasis_of_domain(:)), &
    1084              :          !         (1.0_dp*sum_temp) / almo_scf_env%ndomains, &
    1085              :          !         MAXVAL(almo_scf_env%nbasis_of_domain(:)), &
    1086              :          !         sum_temp
    1087              : 
    1088          542 :          sum_temp = SUM(almo_scf_env%nocc_of_domain(:, :))
    1089              :          WRITE (unit_nr, '(T2,A,T53,I5,F9.2,I5,I9)') &
    1090           61 :             "Occupied MOs per fragment (min, av, max, total):", &
    1091          889 :             MINVAL(SUM(almo_scf_env%nocc_of_domain, DIM=2)), &
    1092           61 :             (1.0_dp*sum_temp)/almo_scf_env%ndomains, &
    1093          889 :             MAXVAL(SUM(almo_scf_env%nocc_of_domain, DIM=2)), &
    1094          122 :             sum_temp
    1095              :          !WRITE (unit_nr, '(T2,I13,F13.3,I13,I13)') &
    1096              :          !         MINVAL( SUM(almo_scf_env%nocc_of_domain, DIM=2) ), &
    1097              :          !         (1.0_dp*sum_temp) / almo_scf_env%ndomains, &
    1098              :          !         MAXVAL( SUM(almo_scf_env%nocc_of_domain, DIM=2) ), &
    1099              :          !         sum_temp
    1100              : 
    1101          542 :          sum_temp = SUM(almo_scf_env%nvirt_of_domain(:, :))
    1102              :          WRITE (unit_nr, '(T2,A,T53,I5,F9.2,I5,I9)') &
    1103           61 :             "Virtual MOs per fragment (min, av, max, total):", &
    1104          889 :             MINVAL(SUM(almo_scf_env%nvirt_of_domain, DIM=2)), &
    1105           61 :             (1.0_dp*sum_temp)/almo_scf_env%ndomains, &
    1106          889 :             MAXVAL(SUM(almo_scf_env%nvirt_of_domain, DIM=2)), &
    1107          122 :             sum_temp
    1108              :          !WRITE (unit_nr, '(T2,I13,F13.3,I13,I13)') &
    1109              :          !         MINVAL( SUM(almo_scf_env%nvirt_of_domain, DIM=2) ), &
    1110              :          !         (1.0_dp*sum_temp) / almo_scf_env%ndomains, &
    1111              :          !         MAXVAL( SUM(almo_scf_env%nvirt_of_domain, DIM=2) ), &
    1112              :          !         sum_temp
    1113              : 
    1114          472 :          sum_temp = SUM(almo_scf_env%charge_of_domain(:))
    1115              :          WRITE (unit_nr, '(T2,A,T53,I5,F9.2,I5,I9)') &
    1116           61 :             "Charges per fragment (min, av, max, total):", &
    1117          472 :             MINVAL(almo_scf_env%charge_of_domain(:)), &
    1118           61 :             (1.0_dp*sum_temp)/almo_scf_env%ndomains, &
    1119          472 :             MAXVAL(almo_scf_env%charge_of_domain(:)), &
    1120          122 :             sum_temp
    1121              :          !WRITE (unit_nr, '(T2,I13,F13.3,I13,I13)') &
    1122              :          !         MINVAL(almo_scf_env%charge_of_domain(:)), &
    1123              :          !         (1.0_dp*sum_temp) / almo_scf_env%ndomains, &
    1124              :          !         MAXVAL(almo_scf_env%charge_of_domain(:)), &
    1125              :          !         sum_temp
    1126              : 
    1127              :          ! compute the number of neighbors of each fragment
    1128          183 :          ALLOCATE (nneighbors(almo_scf_env%ndomains))
    1129              : 
    1130          472 :          DO idomain = 1, almo_scf_env%ndomains
    1131              : 
    1132          411 :             IF (idomain == 1) THEN
    1133              :                index1_prev = 1
    1134              :             ELSE
    1135          350 :                index1_prev = almo_scf_env%domain_map(1)%index1(idomain - 1)
    1136              :             END IF
    1137              : 
    1138           61 :             SELECT CASE (almo_scf_env%deloc_method)
    1139              :             CASE (almo_deloc_none)
    1140          114 :                nneighbors(idomain) = 0
    1141              :             CASE (almo_deloc_x, almo_deloc_scf, almo_deloc_x_then_scf)
    1142          113 :                nneighbors(idomain) = almo_scf_env%ndomains - 1 ! minus self
    1143              :             CASE (almo_deloc_xalmo_1diag, almo_deloc_xalmo_x, almo_deloc_xalmo_scf)
    1144          184 :                nneighbors(idomain) = almo_scf_env%domain_map(1)%index1(idomain) - index1_prev - 1 ! minus self
    1145              :             CASE DEFAULT
    1146          411 :                nneighbors(idomain) = -1
    1147              :             END SELECT
    1148              : 
    1149              :          END DO ! cycle over domains
    1150              : 
    1151          472 :          sum_temp = SUM(nneighbors(:))
    1152              :          WRITE (unit_nr, '(T2,A,T53,I5,F9.2,I5,I9)') &
    1153           61 :             "Deloc. neighbors of fragment (min, av, max, total):", &
    1154          472 :             MINVAL(nneighbors(:)), &
    1155           61 :             (1.0_dp*sum_temp)/almo_scf_env%ndomains, &
    1156          472 :             MAXVAL(nneighbors(:)), &
    1157          122 :             sum_temp
    1158              : 
    1159           61 :          WRITE (unit_nr, '(T2,A)') REPEAT("-", 79)
    1160           61 :          WRITE (unit_nr, '()')
    1161              : 
    1162           61 :          IF (almo_scf_env%ndomains <= 64) THEN
    1163              : 
    1164              :             ! print fragment info
    1165              :             WRITE (unit_nr, '(T2,A10,A13,A13,A13,A13,A13)') &
    1166           61 :                "Fragment", "Basis Set", "Occupied", "Virtual", "Charge", "Deloc Neig" !,"Discarded Virt"
    1167           61 :             WRITE (unit_nr, '(T2,A)') REPEAT("-", 79)
    1168          472 :             DO idomain = 1, almo_scf_env%ndomains
    1169              : 
    1170          525 :                SELECT CASE (almo_scf_env%deloc_method)
    1171              :                CASE (almo_deloc_none)
    1172          114 :                   neig_string = "NONE"
    1173              :                CASE (almo_deloc_x, almo_deloc_scf, almo_deloc_x_then_scf)
    1174          113 :                   neig_string = "ALL"
    1175              :                CASE (almo_deloc_xalmo_1diag, almo_deloc_xalmo_x, almo_deloc_xalmo_scf)
    1176          184 :                   WRITE (neig_string, '(I13)') nneighbors(idomain)
    1177              :                CASE DEFAULT
    1178          411 :                   neig_string = "N/A"
    1179              :                END SELECT
    1180              : 
    1181              :                WRITE (unit_nr, '(T2,I10,I13,I13,I13,I13,A13)') &
    1182          411 :                   idomain, almo_scf_env%nbasis_of_domain(idomain), &
    1183          828 :                   SUM(almo_scf_env%nocc_of_domain(idomain, :)), &
    1184          828 :                   SUM(almo_scf_env%nvirt_of_domain(idomain, :)), &
    1185              :                   !SUM(almo_scf_env%nvirt_disc_of_domain(idomain,:)),&
    1186          411 :                   almo_scf_env%charge_of_domain(idomain), &
    1187          883 :                   ADJUSTR(TRIM(neig_string))
    1188              : 
    1189              :             END DO ! cycle over domains
    1190              : 
    1191           89 :             SELECT CASE (almo_scf_env%deloc_method)
    1192              :             CASE (almo_deloc_xalmo_1diag, almo_deloc_xalmo_x, almo_deloc_xalmo_scf)
    1193              : 
    1194           28 :                WRITE (unit_nr, '(T2,A)') REPEAT("-", 79)
    1195              : 
    1196              :                ! print fragment neighbors
    1197              :                WRITE (unit_nr, '(T2,A78)') &
    1198           28 :                   "Neighbor lists (including self)"
    1199           28 :                WRITE (unit_nr, '(T2,A)') REPEAT("-", 79)
    1200          273 :                DO idomain = 1, almo_scf_env%ndomains
    1201              : 
    1202          184 :                   IF (idomain == 1) THEN
    1203              :                      index1_prev = 1
    1204              :                   ELSE
    1205          156 :                      index1_prev = almo_scf_env%domain_map(1)%index1(idomain - 1)
    1206              :                   END IF
    1207              : 
    1208          184 :                   WRITE (unit_nr, '(T2,I10,":")') idomain
    1209              :                   WRITE (unit_nr, '(T12,11I6)') &
    1210              :                      almo_scf_env%domain_map(1)%pairs &
    1211         1046 :                      (index1_prev:almo_scf_env%domain_map(1)%index1(idomain) - 1, 1) ! includes self
    1212              : 
    1213              :                END DO ! cycle over domains
    1214              : 
    1215              :             END SELECT
    1216              : 
    1217              :          ELSE ! too big to print details for each fragment
    1218              : 
    1219            0 :             WRITE (unit_nr, '(T2,A)') "The system is too big to print details for each fragment."
    1220              : 
    1221              :          END IF ! how many fragments?
    1222              : 
    1223           61 :          WRITE (unit_nr, '(T2,A)') REPEAT("-", 79)
    1224              : 
    1225           61 :          WRITE (unit_nr, '()')
    1226              : 
    1227           61 :          DEALLOCATE (nneighbors)
    1228              : 
    1229              :       END IF ! unit_nr > 0
    1230              : 
    1231          122 :       CALL timestop(handle)
    1232              : 
    1233          122 :    END SUBROUTINE almo_scf_print_job_info
    1234              : 
    1235              : ! **************************************************************************************************
    1236              : !> \brief Initializes the ALMO SCF copy of the AO overlap matrix
    1237              : !>        and all necessary functions (sqrt, inverse...)
    1238              : !> \param matrix_s ...
    1239              : !> \param almo_scf_env ...
    1240              : !> \par History
    1241              : !>       2011.06 created [Rustam Z Khaliullin]
    1242              : !> \author Rustam Z Khaliullin
    1243              : ! **************************************************************************************************
    1244          122 :    SUBROUTINE almo_scf_init_ao_overlap(matrix_s, almo_scf_env)
    1245              :       TYPE(dbcsr_type), INTENT(IN)                       :: matrix_s
    1246              :       TYPE(almo_scf_env_type), INTENT(INOUT)             :: almo_scf_env
    1247              : 
    1248              :       CHARACTER(len=*), PARAMETER :: routineN = 'almo_scf_init_ao_overlap'
    1249              : 
    1250              :       INTEGER                                            :: handle, unit_nr
    1251              :       TYPE(cp_logger_type), POINTER                      :: logger
    1252              : 
    1253          122 :       CALL timeset(routineN, handle)
    1254              : 
    1255              :       ! get a useful output_unit
    1256          122 :       logger => cp_get_default_logger()
    1257          122 :       IF (logger%para_env%is_source()) THEN
    1258           61 :          unit_nr = cp_logger_get_default_unit_nr(logger, local=.TRUE.)
    1259              :       ELSE
    1260              :          unit_nr = -1
    1261              :       END IF
    1262              : 
    1263              :       ! make almo copy of S
    1264              :       ! also copy S to S_blk (i.e. to S with the domain structure imposed)
    1265          122 :       IF (almo_scf_env%orthogonal_basis) THEN
    1266            0 :          CALL dbcsr_set(almo_scf_env%matrix_s(1), 0.0_dp)
    1267            0 :          CALL dbcsr_add_on_diag(almo_scf_env%matrix_s(1), 1.0_dp)
    1268            0 :          CALL dbcsr_set(almo_scf_env%matrix_s_blk(1), 0.0_dp)
    1269            0 :          CALL dbcsr_add_on_diag(almo_scf_env%matrix_s_blk(1), 1.0_dp)
    1270              :       ELSE
    1271          122 :          CALL matrix_qs_to_almo(matrix_s, almo_scf_env%matrix_s(1), almo_scf_env%mat_distr_aos)
    1272              :          CALL dbcsr_copy(almo_scf_env%matrix_s_blk(1), &
    1273          122 :                          almo_scf_env%matrix_s(1), keep_sparsity=.TRUE.)
    1274              :       END IF
    1275              : 
    1276          122 :       CALL dbcsr_filter(almo_scf_env%matrix_s(1), almo_scf_env%eps_filter)
    1277          122 :       CALL dbcsr_filter(almo_scf_env%matrix_s_blk(1), almo_scf_env%eps_filter)
    1278              : 
    1279          122 :       IF (almo_scf_env%almo_update_algorithm == almo_scf_diag) THEN
    1280              :          CALL matrix_sqrt_Newton_Schulz(almo_scf_env%matrix_s_blk_sqrt(1), &
    1281              :                                         almo_scf_env%matrix_s_blk_sqrt_inv(1), &
    1282              :                                         almo_scf_env%matrix_s_blk(1), &
    1283              :                                         threshold=almo_scf_env%eps_filter, &
    1284              :                                         order=almo_scf_env%order_lanczos, &
    1285              :                                         !order=0, &
    1286              :                                         eps_lanczos=almo_scf_env%eps_lanczos, &
    1287           76 :                                         max_iter_lanczos=almo_scf_env%max_iter_lanczos)
    1288           46 :       ELSE IF (almo_scf_env%almo_update_algorithm == almo_scf_dm_sign) THEN
    1289              :          CALL invert_Hotelling(almo_scf_env%matrix_s_blk_inv(1), &
    1290              :                                almo_scf_env%matrix_s_blk(1), &
    1291              :                                threshold=almo_scf_env%eps_filter, &
    1292            0 :                                filter_eps=almo_scf_env%eps_filter)
    1293              :       END IF
    1294              : 
    1295          122 :       CALL timestop(handle)
    1296              : 
    1297          122 :    END SUBROUTINE almo_scf_init_ao_overlap
    1298              : 
    1299              : ! **************************************************************************************************
    1300              : !> \brief Selects the subroutine for the optimization of block-daigonal ALMOs.
    1301              : !>        Keep it short and clean.
    1302              : !> \param qs_env ...
    1303              : !> \param almo_scf_env ...
    1304              : !> \par History
    1305              : !>       2011.11 created [Rustam Z Khaliullin]
    1306              : !> \author Rustam Z Khaliullin
    1307              : ! **************************************************************************************************
    1308          122 :    SUBROUTINE almo_scf_main(qs_env, almo_scf_env)
    1309              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1310              :       TYPE(almo_scf_env_type), INTENT(INOUT)             :: almo_scf_env
    1311              : 
    1312              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'almo_scf_main'
    1313              : 
    1314              :       INTEGER                                            :: handle, ispin, unit_nr
    1315              :       TYPE(cp_logger_type), POINTER                      :: logger
    1316              : 
    1317          122 :       CALL timeset(routineN, handle)
    1318              : 
    1319              :       ! get a useful output_unit
    1320          122 :       logger => cp_get_default_logger()
    1321          122 :       IF (logger%para_env%is_source()) THEN
    1322           61 :          unit_nr = cp_logger_get_default_unit_nr(logger, local=.TRUE.)
    1323              :       ELSE
    1324              :          unit_nr = -1
    1325              :       END IF
    1326              : 
    1327          168 :       SELECT CASE (almo_scf_env%almo_update_algorithm)
    1328              :       CASE (almo_scf_pcg, almo_scf_trustr, almo_scf_skip)
    1329              : 
    1330           10 :          SELECT CASE (almo_scf_env%almo_update_algorithm)
    1331              :          CASE (almo_scf_pcg)
    1332              : 
    1333              :             ! ALMO PCG optimizer as a special case of XALMO PCG
    1334              :             CALL almo_scf_xalmo_pcg(qs_env=qs_env, &
    1335              :                                     almo_scf_env=almo_scf_env, &
    1336              :                                     optimizer=almo_scf_env%opt_block_diag_pcg, &
    1337              :                                     quench_t=almo_scf_env%quench_t_blk, &
    1338              :                                     matrix_t_in=almo_scf_env%matrix_t_blk, &
    1339              :                                     matrix_t_out=almo_scf_env%matrix_t_blk, &
    1340              :                                     assume_t0_q0x=.FALSE., &
    1341              :                                     perturbation_only=.FALSE., &
    1342           10 :                                     special_case=xalmo_case_block_diag)
    1343              : 
    1344              :          CASE (almo_scf_trustr)
    1345              : 
    1346              :             CALL almo_scf_xalmo_trustr(qs_env=qs_env, &
    1347              :                                        almo_scf_env=almo_scf_env, &
    1348              :                                        optimizer=almo_scf_env%opt_block_diag_trustr, &
    1349              :                                        quench_t=almo_scf_env%quench_t_blk, &
    1350              :                                        matrix_t_in=almo_scf_env%matrix_t_blk, &
    1351              :                                        matrix_t_out=almo_scf_env%matrix_t_blk, &
    1352              :                                        perturbation_only=.FALSE., &
    1353           46 :                                        special_case=xalmo_case_block_diag)
    1354              : 
    1355              :          END SELECT
    1356              : 
    1357           98 :          DO ispin = 1, almo_scf_env%nspins
    1358              :             CALL orthogonalize_mos(ket=almo_scf_env%matrix_t_blk(ispin), &
    1359              :                                    overlap=almo_scf_env%matrix_sigma_blk(ispin), &
    1360              :                                    metric=almo_scf_env%matrix_s_blk(1), &
    1361              :                                    retain_locality=.TRUE., &
    1362              :                                    only_normalize=.FALSE., &
    1363              :                                    nocc_of_domain=almo_scf_env%nocc_of_domain(:, ispin), &
    1364              :                                    eps_filter=almo_scf_env%eps_filter, &
    1365              :                                    order_lanczos=almo_scf_env%order_lanczos, &
    1366              :                                    eps_lanczos=almo_scf_env%eps_lanczos, &
    1367           98 :                                    max_iter_lanczos=almo_scf_env%max_iter_lanczos)
    1368              :          END DO
    1369              : 
    1370              :       CASE (almo_scf_diag)
    1371              : 
    1372              :          ! mixing/DIIS optimizer
    1373              :          CALL almo_scf_block_diagonal(qs_env, almo_scf_env, &
    1374          122 :                                       almo_scf_env%opt_block_diag_diis)
    1375              : 
    1376              :       END SELECT
    1377              : 
    1378              :       ! we might need a copy of the converged KS and sigma_inv
    1379          250 :       DO ispin = 1, almo_scf_env%nspins
    1380              :          CALL dbcsr_copy(almo_scf_env%matrix_ks_0deloc(ispin), &
    1381          128 :                          almo_scf_env%matrix_ks(ispin))
    1382              :          CALL dbcsr_copy(almo_scf_env%matrix_sigma_inv_0deloc(ispin), &
    1383          250 :                          almo_scf_env%matrix_sigma_inv(ispin))
    1384              :       END DO
    1385              : 
    1386          122 :       CALL timestop(handle)
    1387              : 
    1388          122 :    END SUBROUTINE almo_scf_main
    1389              : 
    1390              : ! **************************************************************************************************
    1391              : !> \brief selects various post scf routines
    1392              : !> \param qs_env ...
    1393              : !> \param almo_scf_env ...
    1394              : !> \par History
    1395              : !>       2011.06 created [Rustam Z Khaliullin]
    1396              : !> \author Rustam Z Khaliullin
    1397              : ! **************************************************************************************************
    1398          122 :    SUBROUTINE almo_scf_delocalization(qs_env, almo_scf_env)
    1399              : 
    1400              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1401              :       TYPE(almo_scf_env_type), INTENT(INOUT)             :: almo_scf_env
    1402              : 
    1403              :       CHARACTER(len=*), PARAMETER :: routineN = 'almo_scf_delocalization'
    1404              : 
    1405              :       INTEGER                                            :: handle, ispin, unit_nr
    1406              :       TYPE(cp_logger_type), POINTER                      :: logger
    1407          122 :       TYPE(dbcsr_type), ALLOCATABLE, DIMENSION(:)        :: no_quench
    1408              :       TYPE(optimizer_options_type)                       :: arbitrary_optimizer
    1409              : 
    1410          122 :       CALL timeset(routineN, handle)
    1411              : 
    1412              :       ! get a useful output_unit
    1413          122 :       logger => cp_get_default_logger()
    1414          122 :       IF (logger%para_env%is_source()) THEN
    1415           61 :          unit_nr = cp_logger_get_default_unit_nr(logger, local=.TRUE.)
    1416              :       ELSE
    1417              :          unit_nr = -1
    1418              :       END IF
    1419              : 
    1420              :       ! create a local optimizer that handles XALMO DIIS
    1421              :       ! the options of this optimizer are arbitrary because
    1422              :       ! XALMO DIIS SCF does not converge for yet unknown reasons and
    1423              :       ! currently used in the code to get perturbative estimates only
    1424          122 :       arbitrary_optimizer%optimizer_type = optimizer_diis
    1425          122 :       arbitrary_optimizer%max_iter = 3
    1426          122 :       arbitrary_optimizer%eps_error = 1.0E-6_dp
    1427          122 :       arbitrary_optimizer%ndiis = 2
    1428              : 
    1429          152 :       SELECT CASE (almo_scf_env%deloc_method)
    1430              :       CASE (almo_deloc_x, almo_deloc_scf, almo_deloc_x_then_scf)
    1431              : 
    1432              :          ! RZK-warning hack into the quenched routine:
    1433              :          ! create a quench matrix with all-all-all blocks 1.0
    1434              :          ! it is a waste of memory but since matrices are distributed
    1435              :          ! we can tolerate it for now
    1436          120 :          ALLOCATE (no_quench(almo_scf_env%nspins))
    1437              :          CALL dbcsr_create(no_quench(1), &
    1438              :                            template=almo_scf_env%matrix_t(1), &
    1439           30 :                            matrix_type=dbcsr_type_no_symmetry)
    1440           30 :          CALL dbcsr_reserve_all_blocks(no_quench(1))
    1441           30 :          CALL dbcsr_set(no_quench(1), 1.0_dp)
    1442          152 :          IF (almo_scf_env%nspins > 1) THEN
    1443            0 :             DO ispin = 2, almo_scf_env%nspins
    1444              :                CALL dbcsr_create(no_quench(ispin), &
    1445              :                                  template=almo_scf_env%matrix_t(1), &
    1446            0 :                                  matrix_type=dbcsr_type_no_symmetry)
    1447            0 :                CALL dbcsr_copy(no_quench(ispin), no_quench(1))
    1448              :             END DO
    1449              :          END IF
    1450              : 
    1451              :       END SELECT
    1452              : 
    1453          170 :       SELECT CASE (almo_scf_env%deloc_method)
    1454              :       CASE (almo_deloc_none, almo_deloc_scf)
    1455              : 
    1456          102 :          DO ispin = 1, almo_scf_env%nspins
    1457              :             CALL dbcsr_copy(almo_scf_env%matrix_t(ispin), &
    1458          102 :                             almo_scf_env%matrix_t_blk(ispin))
    1459              :          END DO
    1460              : 
    1461              :       CASE (almo_deloc_x, almo_deloc_xk, almo_deloc_x_then_scf)
    1462              : 
    1463              :          !!!! RZK-warning a whole class of delocalization methods
    1464              :          !!!! are commented out at the moment because some of their
    1465              :          !!!! routines have not been thoroughly tested.
    1466              : 
    1467           18 :          IF (almo_scf_env%xalmo_update_algorithm == almo_scf_pcg) THEN
    1468              : 
    1469              :             CALL almo_scf_xalmo_pcg(qs_env=qs_env, &
    1470              :                                     almo_scf_env=almo_scf_env, &
    1471              :                                     optimizer=almo_scf_env%opt_xalmo_pcg, &
    1472              :                                     quench_t=no_quench, &
    1473              :                                     matrix_t_in=almo_scf_env%matrix_t_blk, &
    1474              :                                     matrix_t_out=almo_scf_env%matrix_t, &
    1475              :                                     assume_t0_q0x=(almo_scf_env%xalmo_trial_wf == xalmo_trial_r0_out), &
    1476              :                                     perturbation_only=.TRUE., &
    1477           18 :                                     special_case=xalmo_case_fully_deloc)
    1478              : 
    1479            0 :          ELSE IF (almo_scf_env%xalmo_update_algorithm == almo_scf_trustr) THEN
    1480              : 
    1481              :             CALL almo_scf_xalmo_trustr(qs_env=qs_env, &
    1482              :                                        almo_scf_env=almo_scf_env, &
    1483              :                                        optimizer=almo_scf_env%opt_xalmo_trustr, &
    1484              :                                        quench_t=no_quench, &
    1485              :                                        matrix_t_in=almo_scf_env%matrix_t_blk, &
    1486              :                                        matrix_t_out=almo_scf_env%matrix_t, &
    1487              :                                        perturbation_only=.TRUE., &
    1488            0 :                                        special_case=xalmo_case_fully_deloc)
    1489              : 
    1490              :          ELSE
    1491              : 
    1492            0 :             CPABORT("Other algorithms do not exist")
    1493              : 
    1494              :          END IF
    1495              : 
    1496              :       CASE (almo_deloc_xalmo_1diag)
    1497              : 
    1498            2 :          IF (almo_scf_env%xalmo_update_algorithm == almo_scf_diag) THEN
    1499              : 
    1500            2 :             almo_scf_env%perturbative_delocalization = .TRUE.
    1501            4 :             DO ispin = 1, almo_scf_env%nspins
    1502              :                CALL dbcsr_copy(almo_scf_env%matrix_t(ispin), &
    1503            4 :                                almo_scf_env%matrix_t_blk(ispin))
    1504              :             END DO
    1505              :             CALL almo_scf_xalmo_eigensolver(qs_env, almo_scf_env, &
    1506            2 :                                             arbitrary_optimizer)
    1507              : 
    1508              :          ELSE
    1509              : 
    1510            0 :             CPABORT("Other algorithms do not exist")
    1511              : 
    1512              :          END IF
    1513              : 
    1514              :       CASE (almo_deloc_xalmo_x)
    1515              : 
    1516            6 :          IF (almo_scf_env%xalmo_update_algorithm == almo_scf_pcg) THEN
    1517              : 
    1518              :             CALL almo_scf_xalmo_pcg(qs_env=qs_env, &
    1519              :                                     almo_scf_env=almo_scf_env, &
    1520              :                                     optimizer=almo_scf_env%opt_xalmo_pcg, &
    1521              :                                     quench_t=almo_scf_env%quench_t, &
    1522              :                                     matrix_t_in=almo_scf_env%matrix_t_blk, &
    1523              :                                     matrix_t_out=almo_scf_env%matrix_t, &
    1524              :                                     assume_t0_q0x=(almo_scf_env%xalmo_trial_wf == xalmo_trial_r0_out), &
    1525              :                                     perturbation_only=.TRUE., &
    1526            6 :                                     special_case=xalmo_case_normal)
    1527              : 
    1528            0 :          ELSE IF (almo_scf_env%xalmo_update_algorithm == almo_scf_trustr) THEN
    1529              : 
    1530              :             CALL almo_scf_xalmo_trustr(qs_env=qs_env, &
    1531              :                                        almo_scf_env=almo_scf_env, &
    1532              :                                        optimizer=almo_scf_env%opt_xalmo_trustr, &
    1533              :                                        quench_t=almo_scf_env%quench_t, &
    1534              :                                        matrix_t_in=almo_scf_env%matrix_t_blk, &
    1535              :                                        matrix_t_out=almo_scf_env%matrix_t, &
    1536              :                                        perturbation_only=.TRUE., &
    1537            0 :                                        special_case=xalmo_case_normal)
    1538              : 
    1539              :          ELSE
    1540              : 
    1541            0 :             CPABORT("Other algorithms do not exist")
    1542              : 
    1543              :          END IF
    1544              : 
    1545              :       CASE (almo_deloc_xalmo_scf)
    1546              : 
    1547           48 :          IF (almo_scf_env%xalmo_update_algorithm == almo_scf_diag) THEN
    1548              : 
    1549            0 :             CPABORT("Should not be here: convergence will fail!")
    1550              : 
    1551            0 :             almo_scf_env%perturbative_delocalization = .FALSE.
    1552            0 :             DO ispin = 1, almo_scf_env%nspins
    1553              :                CALL dbcsr_copy(almo_scf_env%matrix_t(ispin), &
    1554            0 :                                almo_scf_env%matrix_t_blk(ispin))
    1555              :             END DO
    1556              :             CALL almo_scf_xalmo_eigensolver(qs_env, almo_scf_env, &
    1557            0 :                                             arbitrary_optimizer)
    1558              : 
    1559           48 :          ELSE IF (almo_scf_env%xalmo_update_algorithm == almo_scf_pcg) THEN
    1560              : 
    1561              :             CALL almo_scf_xalmo_pcg(qs_env=qs_env, &
    1562              :                                     almo_scf_env=almo_scf_env, &
    1563              :                                     optimizer=almo_scf_env%opt_xalmo_pcg, &
    1564              :                                     quench_t=almo_scf_env%quench_t, &
    1565              :                                     matrix_t_in=almo_scf_env%matrix_t_blk, &
    1566              :                                     matrix_t_out=almo_scf_env%matrix_t, &
    1567              :                                     assume_t0_q0x=(almo_scf_env%xalmo_trial_wf == xalmo_trial_r0_out), &
    1568              :                                     perturbation_only=.FALSE., &
    1569           32 :                                     special_case=xalmo_case_normal)
    1570              : 
    1571           16 :          ELSE IF (almo_scf_env%xalmo_update_algorithm == almo_scf_trustr) THEN
    1572              : 
    1573              :             CALL almo_scf_xalmo_trustr(qs_env=qs_env, &
    1574              :                                        almo_scf_env=almo_scf_env, &
    1575              :                                        optimizer=almo_scf_env%opt_xalmo_trustr, &
    1576              :                                        quench_t=almo_scf_env%quench_t, &
    1577              :                                        matrix_t_in=almo_scf_env%matrix_t_blk, &
    1578              :                                        matrix_t_out=almo_scf_env%matrix_t, &
    1579              :                                        perturbation_only=.FALSE., &
    1580           16 :                                        special_case=xalmo_case_normal)
    1581              : 
    1582              :          ELSE
    1583              : 
    1584            0 :             CPABORT("Other algorithms do not exist")
    1585              : 
    1586              :          END IF
    1587              : 
    1588              :       CASE DEFAULT
    1589              : 
    1590          122 :          CPABORT("Illegal delocalization method")
    1591              : 
    1592              :       END SELECT
    1593              : 
    1594          148 :       SELECT CASE (almo_scf_env%deloc_method)
    1595              :       CASE (almo_deloc_scf, almo_deloc_x_then_scf)
    1596              : 
    1597           26 :          IF (almo_scf_env%deloc_truncate_virt /= virt_full) THEN
    1598            0 :             CPABORT("full scf is NYI for truncated virtual space")
    1599              :          END IF
    1600              : 
    1601          148 :          IF (almo_scf_env%xalmo_update_algorithm == almo_scf_pcg) THEN
    1602              : 
    1603              :             CALL almo_scf_xalmo_pcg(qs_env=qs_env, &
    1604              :                                     almo_scf_env=almo_scf_env, &
    1605              :                                     optimizer=almo_scf_env%opt_xalmo_pcg, &
    1606              :                                     quench_t=no_quench, &
    1607              :                                     matrix_t_in=almo_scf_env%matrix_t, &
    1608              :                                     matrix_t_out=almo_scf_env%matrix_t, &
    1609              :                                     assume_t0_q0x=.FALSE., &
    1610              :                                     perturbation_only=.FALSE., &
    1611           26 :                                     special_case=xalmo_case_fully_deloc)
    1612              : 
    1613            0 :          ELSE IF (almo_scf_env%xalmo_update_algorithm == almo_scf_trustr) THEN
    1614              : 
    1615              :             CALL almo_scf_xalmo_trustr(qs_env=qs_env, &
    1616              :                                        almo_scf_env=almo_scf_env, &
    1617              :                                        optimizer=almo_scf_env%opt_xalmo_trustr, &
    1618              :                                        quench_t=no_quench, &
    1619              :                                        matrix_t_in=almo_scf_env%matrix_t, &
    1620              :                                        matrix_t_out=almo_scf_env%matrix_t, &
    1621              :                                        perturbation_only=.FALSE., &
    1622            0 :                                        special_case=xalmo_case_fully_deloc)
    1623              : 
    1624              :          ELSE
    1625              : 
    1626            0 :             CPABORT("Other algorithms do not exist")
    1627              : 
    1628              :          END IF
    1629              : 
    1630              :       END SELECT
    1631              : 
    1632              :       ! clean up
    1633          152 :       SELECT CASE (almo_scf_env%deloc_method)
    1634              :       CASE (almo_deloc_x, almo_deloc_scf, almo_deloc_x_then_scf)
    1635           60 :          DO ispin = 1, almo_scf_env%nspins
    1636           60 :             CALL dbcsr_release(no_quench(ispin))
    1637              :          END DO
    1638          152 :          DEALLOCATE (no_quench)
    1639              :       END SELECT
    1640              : 
    1641          122 :       CALL timestop(handle)
    1642              : 
    1643          244 :    END SUBROUTINE almo_scf_delocalization
    1644              : 
    1645              : ! **************************************************************************************************
    1646              : !> \brief orbital localization
    1647              : !> \param qs_env ...
    1648              : !> \param almo_scf_env ...
    1649              : !> \par History
    1650              : !>       2018.09 created [Ziling Luo]
    1651              : !> \author Ziling Luo
    1652              : ! **************************************************************************************************
    1653          122 :    SUBROUTINE construct_nlmos(qs_env, almo_scf_env)
    1654              : 
    1655              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1656              :       TYPE(almo_scf_env_type), INTENT(INOUT)             :: almo_scf_env
    1657              : 
    1658              :       INTEGER                                            :: ispin
    1659              : 
    1660          122 :       IF (almo_scf_env%construct_nlmos) THEN
    1661              : 
    1662            8 :          DO ispin = 1, almo_scf_env%nspins
    1663              : 
    1664              :             CALL orthogonalize_mos(ket=almo_scf_env%matrix_t(ispin), &
    1665              :                                    overlap=almo_scf_env%matrix_sigma(ispin), &
    1666              :                                    metric=almo_scf_env%matrix_s(1), &
    1667              :                                    retain_locality=.FALSE., &
    1668              :                                    only_normalize=.FALSE., &
    1669              :                                    nocc_of_domain=almo_scf_env%nocc_of_domain(:, ispin), &
    1670              :                                    eps_filter=almo_scf_env%eps_filter, &
    1671              :                                    order_lanczos=almo_scf_env%order_lanczos, &
    1672              :                                    eps_lanczos=almo_scf_env%eps_lanczos, &
    1673            8 :                                    max_iter_lanczos=almo_scf_env%max_iter_lanczos)
    1674              :          END DO
    1675              : 
    1676            4 :          CALL construct_nlmos_wrapper(qs_env, almo_scf_env, virtuals=.FALSE.)
    1677              : 
    1678            4 :          IF (almo_scf_env%opt_nlmo_pcg%opt_penalty%virtual_nlmos) THEN
    1679            0 :             CALL construct_virtuals(almo_scf_env)
    1680            0 :             CALL construct_nlmos_wrapper(qs_env, almo_scf_env, virtuals=.TRUE.)
    1681              :          END IF
    1682              : 
    1683            4 :          IF (almo_scf_env%opt_nlmo_pcg%opt_penalty%compactification_filter_start > 0.0_dp) THEN
    1684            2 :             CALL nlmo_compactification(qs_env, almo_scf_env, almo_scf_env%matrix_t)
    1685              :          END IF
    1686              : 
    1687              :       END IF
    1688              : 
    1689          122 :    END SUBROUTINE construct_nlmos
    1690              : 
    1691              : ! **************************************************************************************************
    1692              : !> \brief Calls NLMO optimization
    1693              : !> \param qs_env ...
    1694              : !> \param almo_scf_env ...
    1695              : !> \param virtuals ...
    1696              : !> \par History
    1697              : !>       2019.10 created [Ziling Luo]
    1698              : !> \author Ziling Luo
    1699              : ! **************************************************************************************************
    1700            4 :    SUBROUTINE construct_nlmos_wrapper(qs_env, almo_scf_env, virtuals)
    1701              : 
    1702              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1703              :       TYPE(almo_scf_env_type), INTENT(INOUT)             :: almo_scf_env
    1704              :       LOGICAL, INTENT(IN)                                :: virtuals
    1705              : 
    1706              :       REAL(KIND=dp)                                      :: det_diff, prev_determinant
    1707              : 
    1708            4 :       almo_scf_env%overlap_determinant = 1.0_dp
    1709              :       ! KEEP: initial_vol_coeff = almo_scf_env%opt_nlmo_pcg%opt_penalty%penalty_strength
    1710              :       almo_scf_env%opt_nlmo_pcg%opt_penalty%penalty_strength = &
    1711            4 :          -1.0_dp*almo_scf_env%opt_nlmo_pcg%opt_penalty%penalty_strength !NEW1
    1712              : 
    1713              :       ! loop over the strength of the orthogonalization penalty
    1714            4 :       prev_determinant = 10.0_dp
    1715           10 :       DO WHILE (almo_scf_env%overlap_determinant > almo_scf_env%opt_nlmo_pcg%opt_penalty%final_determinant)
    1716              : 
    1717            8 :          IF (.NOT. virtuals) THEN
    1718              :             CALL almo_scf_construct_nlmos(qs_env=qs_env, &
    1719              :                                           optimizer=almo_scf_env%opt_nlmo_pcg, &
    1720              :                                           matrix_s=almo_scf_env%matrix_s(1), &
    1721              :                                           matrix_mo_in=almo_scf_env%matrix_t, &
    1722              :                                           matrix_mo_out=almo_scf_env%matrix_t, &
    1723              :                                           template_matrix_sigma=almo_scf_env%matrix_sigma_inv, &
    1724              :                                           overlap_determinant=almo_scf_env%overlap_determinant, &
    1725              :                                           mat_distr_aos=almo_scf_env%mat_distr_aos, &
    1726              :                                           virtuals=virtuals, &
    1727            8 :                                           eps_filter=almo_scf_env%eps_filter)
    1728              :          ELSE
    1729              :             CALL almo_scf_construct_nlmos(qs_env=qs_env, &
    1730              :                                           optimizer=almo_scf_env%opt_nlmo_pcg, &
    1731              :                                           matrix_s=almo_scf_env%matrix_s(1), &
    1732              :                                           matrix_mo_in=almo_scf_env%matrix_v, &
    1733              :                                           matrix_mo_out=almo_scf_env%matrix_v, &
    1734              :                                           template_matrix_sigma=almo_scf_env%matrix_sigma_vv, &
    1735              :                                           overlap_determinant=almo_scf_env%overlap_determinant, &
    1736              :                                           mat_distr_aos=almo_scf_env%mat_distr_aos, &
    1737              :                                           virtuals=virtuals, &
    1738            0 :                                           eps_filter=almo_scf_env%eps_filter)
    1739              : 
    1740              :          END IF
    1741              : 
    1742            8 :          det_diff = prev_determinant - almo_scf_env%overlap_determinant
    1743              :          almo_scf_env%opt_nlmo_pcg%opt_penalty%penalty_strength = &
    1744              :             almo_scf_env%opt_nlmo_pcg%opt_penalty%penalty_strength/ &
    1745            8 :             ABS(almo_scf_env%opt_nlmo_pcg%opt_penalty%penalty_strength_dec_factor)
    1746              : 
    1747            8 :          IF (det_diff < almo_scf_env%opt_nlmo_pcg%opt_penalty%determinant_tolerance) THEN
    1748              :             EXIT
    1749              :          END IF
    1750            4 :          prev_determinant = almo_scf_env%overlap_determinant
    1751              : 
    1752              :       END DO
    1753              : 
    1754            4 :    END SUBROUTINE construct_nlmos_wrapper
    1755              : 
    1756              : ! **************************************************************************************************
    1757              : !> \brief Construct virtual orbitals
    1758              : !> \param almo_scf_env ...
    1759              : !> \par History
    1760              : !>       2019.10 created [Ziling Luo]
    1761              : !> \author Ziling Luo
    1762              : ! **************************************************************************************************
    1763            0 :    SUBROUTINE construct_virtuals(almo_scf_env)
    1764              : 
    1765              :       TYPE(almo_scf_env_type), INTENT(INOUT)             :: almo_scf_env
    1766              : 
    1767              :       INTEGER                                            :: ispin, n
    1768            0 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: eigenvalues
    1769              :       TYPE(dbcsr_type)                                   :: tempNV1, tempVOcc1, tempVOcc2, tempVV1, &
    1770              :                                                             tempVV2
    1771              : 
    1772            0 :       DO ispin = 1, almo_scf_env%nspins
    1773              : 
    1774              :          CALL dbcsr_create(tempNV1, &
    1775              :                            template=almo_scf_env%matrix_v(ispin), &
    1776            0 :                            matrix_type=dbcsr_type_no_symmetry)
    1777              :          CALL dbcsr_create(tempVOcc1, &
    1778              :                            template=almo_scf_env%matrix_vo(ispin), &
    1779            0 :                            matrix_type=dbcsr_type_no_symmetry)
    1780              :          CALL dbcsr_create(tempVOcc2, &
    1781              :                            template=almo_scf_env%matrix_vo(ispin), &
    1782            0 :                            matrix_type=dbcsr_type_no_symmetry)
    1783              :          CALL dbcsr_create(tempVV1, &
    1784              :                            template=almo_scf_env%matrix_sigma_vv(ispin), &
    1785            0 :                            matrix_type=dbcsr_type_no_symmetry)
    1786              :          CALL dbcsr_create(tempVV2, &
    1787              :                            template=almo_scf_env%matrix_sigma_vv(ispin), &
    1788            0 :                            matrix_type=dbcsr_type_no_symmetry)
    1789              : 
    1790              :          ! Generate random virtual matrix
    1791              :          CALL dbcsr_init_random(almo_scf_env%matrix_v(ispin), &
    1792            0 :                                 keep_sparsity=.FALSE.)
    1793              : 
    1794              :          ! Project the orbital subspace out
    1795              :          CALL dbcsr_multiply("N", "N", 1.0_dp, &
    1796              :                              almo_scf_env%matrix_s(1), &
    1797              :                              almo_scf_env%matrix_v(ispin), &
    1798              :                              0.0_dp, tempNV1, &
    1799            0 :                              filter_eps=almo_scf_env%eps_filter)
    1800              : 
    1801              :          CALL dbcsr_multiply("T", "N", 1.0_dp, &
    1802              :                              tempNV1, &
    1803              :                              almo_scf_env%matrix_t(ispin), &
    1804              :                              0.0_dp, tempVOcc1, &
    1805            0 :                              filter_eps=almo_scf_env%eps_filter)
    1806              : 
    1807              :          CALL dbcsr_multiply("N", "N", 1.0_dp, &
    1808              :                              tempVOcc1, &
    1809              :                              almo_scf_env%matrix_sigma_inv(ispin), &
    1810              :                              0.0_dp, tempVOcc2, &
    1811            0 :                              filter_eps=almo_scf_env%eps_filter)
    1812              : 
    1813              :          CALL dbcsr_multiply("N", "T", 1.0_dp, &
    1814              :                              almo_scf_env%matrix_t(ispin), &
    1815              :                              tempVOcc2, &
    1816              :                              0.0_dp, tempNV1, &
    1817            0 :                              filter_eps=almo_scf_env%eps_filter)
    1818              : 
    1819            0 :          CALL dbcsr_add(almo_scf_env%matrix_v(ispin), tempNV1, 1.0_dp, -1.0_dp)
    1820              : 
    1821              :          ! compute VxV overlap
    1822              :          CALL dbcsr_multiply("N", "N", 1.0_dp, &
    1823              :                              almo_scf_env%matrix_s(1), &
    1824              :                              almo_scf_env%matrix_v(ispin), &
    1825              :                              0.0_dp, tempNV1, &
    1826            0 :                              filter_eps=almo_scf_env%eps_filter)
    1827              : 
    1828              :          CALL dbcsr_multiply("T", "N", 1.0_dp, &
    1829              :                              almo_scf_env%matrix_v(ispin), &
    1830              :                              tempNV1, &
    1831              :                              0.0_dp, tempVV1, &
    1832            0 :                              filter_eps=almo_scf_env%eps_filter)
    1833              : 
    1834              :          CALL orthogonalize_mos(ket=almo_scf_env%matrix_v(ispin), &
    1835              :                                 overlap=tempVV1, &
    1836              :                                 metric=almo_scf_env%matrix_s(1), &
    1837              :                                 retain_locality=.FALSE., &
    1838              :                                 only_normalize=.FALSE., &
    1839              :                                 nocc_of_domain=almo_scf_env%nocc_of_domain(:, ispin), &
    1840              :                                 eps_filter=almo_scf_env%eps_filter, &
    1841              :                                 order_lanczos=almo_scf_env%order_lanczos, &
    1842              :                                 eps_lanczos=almo_scf_env%eps_lanczos, &
    1843            0 :                                 max_iter_lanczos=almo_scf_env%max_iter_lanczos)
    1844              : 
    1845              :          ! compute VxV block of the KS matrix
    1846              :          CALL dbcsr_multiply("N", "N", 1.0_dp, &
    1847              :                              almo_scf_env%matrix_ks(ispin), &
    1848              :                              almo_scf_env%matrix_v(ispin), &
    1849              :                              0.0_dp, tempNV1, &
    1850            0 :                              filter_eps=almo_scf_env%eps_filter)
    1851              : 
    1852              :          CALL dbcsr_multiply("T", "N", 1.0_dp, &
    1853              :                              almo_scf_env%matrix_v(ispin), &
    1854              :                              tempNV1, &
    1855              :                              0.0_dp, tempVV1, &
    1856            0 :                              filter_eps=almo_scf_env%eps_filter)
    1857              : 
    1858            0 :          CALL dbcsr_get_info(tempVV1, nfullrows_total=n)
    1859            0 :          ALLOCATE (eigenvalues(n))
    1860              :          CALL cp_dbcsr_syevd(tempVV1, tempVV2, &
    1861              :                              eigenvalues, &
    1862              :                              para_env=almo_scf_env%para_env, &
    1863            0 :                              blacs_env=almo_scf_env%blacs_env)
    1864            0 :          DEALLOCATE (eigenvalues)
    1865              : 
    1866              :          CALL dbcsr_multiply("N", "N", 1.0_dp, &
    1867              :                              almo_scf_env%matrix_v(ispin), &
    1868              :                              tempVV2, &
    1869              :                              0.0_dp, tempNV1, &
    1870            0 :                              filter_eps=almo_scf_env%eps_filter)
    1871              : 
    1872            0 :          CALL dbcsr_copy(almo_scf_env%matrix_v(ispin), tempNV1)
    1873              : 
    1874            0 :          CALL dbcsr_release(tempNV1)
    1875            0 :          CALL dbcsr_release(tempVOcc1)
    1876            0 :          CALL dbcsr_release(tempVOcc2)
    1877            0 :          CALL dbcsr_release(tempVV1)
    1878            0 :          CALL dbcsr_release(tempVV2)
    1879              : 
    1880              :       END DO
    1881              : 
    1882            0 :    END SUBROUTINE construct_virtuals
    1883              : 
    1884              : ! **************************************************************************************************
    1885              : !> \brief Compactify (set small blocks to zero) orbitals
    1886              : !> \param qs_env ...
    1887              : !> \param almo_scf_env ...
    1888              : !> \param matrix ...
    1889              : !> \par History
    1890              : !>       2019.10 created [Ziling Luo]
    1891              : !> \author Ziling Luo
    1892              : ! **************************************************************************************************
    1893            2 :    SUBROUTINE nlmo_compactification(qs_env, almo_scf_env, matrix)
    1894              : 
    1895              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1896              :       TYPE(almo_scf_env_type), INTENT(INOUT)             :: almo_scf_env
    1897              :       TYPE(dbcsr_type), ALLOCATABLE, DIMENSION(:), &
    1898              :          INTENT(IN)                                      :: matrix
    1899              : 
    1900              :       INTEGER                                            :: iblock_col, iblock_col_size, iblock_row, &
    1901              :                                                             iblock_row_size, icol, irow, ispin, &
    1902              :                                                             Ncols, Nrows, nspins, unit_nr
    1903              :       LOGICAL                                            :: element_by_element
    1904              :       REAL(KIND=dp)                                      :: energy, eps_local, eps_start, &
    1905              :                                                             max_element, spin_factor
    1906            2 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: occ, retained
    1907            2 :       REAL(kind=dp), DIMENSION(:, :), POINTER            :: data_p
    1908              :       TYPE(cp_logger_type), POINTER                      :: logger
    1909              :       TYPE(dbcsr_iterator_type)                          :: iter
    1910            2 :       TYPE(dbcsr_type), ALLOCATABLE, DIMENSION(:)        :: matrix_p_tmp, matrix_t_tmp
    1911              :       TYPE(mp_comm_type)                                 :: group
    1912              : 
    1913              :       ! define the output_unit
    1914            4 :       logger => cp_get_default_logger()
    1915            2 :       IF (logger%para_env%is_source()) THEN
    1916            1 :          unit_nr = cp_logger_get_default_unit_nr(logger, local=.TRUE.)
    1917              :       ELSE
    1918              :          unit_nr = -1
    1919              :       END IF
    1920              : 
    1921            2 :       nspins = SIZE(matrix)
    1922            2 :       element_by_element = .FALSE.
    1923              : 
    1924            2 :       IF (nspins == 1) THEN
    1925            2 :          spin_factor = 2.0_dp
    1926              :       ELSE
    1927            0 :          spin_factor = 1.0_dp
    1928              :       END IF
    1929              : 
    1930            8 :       ALLOCATE (matrix_t_tmp(nspins))
    1931            6 :       ALLOCATE (matrix_p_tmp(nspins))
    1932            6 :       ALLOCATE (retained(nspins))
    1933            2 :       ALLOCATE (occ(2))
    1934              : 
    1935            4 :       DO ispin = 1, nspins
    1936              : 
    1937              :          ! init temporary storage
    1938              :          CALL dbcsr_create(matrix_t_tmp(ispin), &
    1939              :                            template=matrix(ispin), &
    1940            2 :                            matrix_type=dbcsr_type_no_symmetry)
    1941            2 :          CALL dbcsr_copy(matrix_t_tmp(ispin), matrix(ispin))
    1942              : 
    1943              :          CALL dbcsr_create(matrix_p_tmp(ispin), &
    1944              :                            template=almo_scf_env%matrix_p(ispin), &
    1945            2 :                            matrix_type=dbcsr_type_no_symmetry)
    1946            4 :          CALL dbcsr_copy(matrix_p_tmp(ispin), almo_scf_env%matrix_p(ispin))
    1947              : 
    1948              :       END DO
    1949              : 
    1950            2 :       IF (unit_nr > 0) THEN
    1951            1 :          WRITE (unit_nr, *)
    1952              :          WRITE (unit_nr, '(T2,A)') &
    1953            1 :             "Energy dependence on the (block-by-block) filtering of the NLMO coefficients"
    1954              :          IF (unit_nr > 0) WRITE (unit_nr, '(T2,A13,A20,A20,A25)') &
    1955            1 :             "EPS filter", "Occupation Alpha", "Occupation Beta", "Energy"
    1956              :       END IF
    1957              : 
    1958            2 :       eps_start = almo_scf_env%opt_nlmo_pcg%opt_penalty%compactification_filter_start
    1959            2 :       eps_local = MAX(eps_start, 10E-14_dp)
    1960              : 
    1961            8 :       DO
    1962              : 
    1963           10 :          IF (eps_local > 0.11_dp) EXIT
    1964              : 
    1965           16 :          DO ispin = 1, nspins
    1966              : 
    1967            8 :             retained(ispin) = 0
    1968            8 :             CALL dbcsr_work_create(matrix_t_tmp(ispin), work_mutable=.TRUE.)
    1969            8 :             CALL dbcsr_iterator_start(iter, matrix_t_tmp(ispin))
    1970          264 :             DO WHILE (dbcsr_iterator_blocks_left(iter))
    1971              :                CALL dbcsr_iterator_next_block(iter, iblock_row, iblock_col, data_p, &
    1972          256 :                                               row_size=iblock_row_size, col_size=iblock_col_size)
    1973          776 :                DO icol = 1, iblock_col_size
    1974              : 
    1975          256 :                   IF (element_by_element) THEN
    1976              : 
    1977              :                      DO irow = 1, iblock_row_size
    1978              :                         IF (ABS(data_p(irow, icol)) < eps_local) THEN
    1979              :                            data_p(irow, icol) = 0.0_dp
    1980              :                         ELSE
    1981              :                            retained(ispin) = retained(ispin) + 1
    1982              :                         END IF
    1983              :                      END DO
    1984              : 
    1985              :                   ELSE ! rows are blocked
    1986              : 
    1987          512 :                      max_element = 0.0_dp
    1988         2560 :                      DO irow = 1, iblock_row_size
    1989         2560 :                         IF (ABS(data_p(irow, icol)) > max_element) THEN
    1990              :                            max_element = ABS(data_p(irow, icol))
    1991              :                         END IF
    1992              :                      END DO
    1993          512 :                      IF (max_element < eps_local) THEN
    1994          155 :                         DO irow = 1, iblock_row_size
    1995          155 :                            data_p(irow, icol) = 0.0_dp
    1996              :                         END DO
    1997              :                      ELSE
    1998          481 :                         retained(ispin) = retained(ispin) + iblock_row_size
    1999              :                      END IF
    2000              : 
    2001              :                   END IF ! block rows?
    2002              :                END DO ! icol
    2003              : 
    2004              :             END DO ! iterator
    2005            8 :             CALL dbcsr_iterator_stop(iter)
    2006            8 :             CALL dbcsr_finalize(matrix_t_tmp(ispin))
    2007            8 :             CALL dbcsr_filter(matrix_t_tmp(ispin), eps_local)
    2008              : 
    2009              :             CALL dbcsr_get_info(matrix_t_tmp(ispin), group=group, &
    2010              :                                 nfullrows_total=Nrows, &
    2011            8 :                                 nfullcols_total=Ncols)
    2012            8 :             CALL group%sum(retained(ispin))
    2013              : 
    2014              :             !devide by the total no. elements
    2015            8 :             occ(ispin) = retained(ispin)/Nrows/Ncols
    2016              : 
    2017              :             ! compute the global projectors (for the density matrix)
    2018              :             CALL almo_scf_t_to_proj( &
    2019              :                t=matrix_t_tmp(ispin), &
    2020              :                p=matrix_p_tmp(ispin), &
    2021              :                eps_filter=almo_scf_env%eps_filter, &
    2022              :                orthog_orbs=.FALSE., &
    2023              :                nocc_of_domain=almo_scf_env%nocc_of_domain(:, ispin), &
    2024              :                s=almo_scf_env%matrix_s(1), &
    2025              :                sigma=almo_scf_env%matrix_sigma(ispin), &
    2026              :                sigma_inv=almo_scf_env%matrix_sigma_inv(ispin), &
    2027              :                use_guess=.FALSE., &
    2028              :                algorithm=almo_scf_env%sigma_inv_algorithm, &
    2029              :                inv_eps_factor=almo_scf_env%matrix_iter_eps_error_factor, &
    2030              :                inverse_accelerator=almo_scf_env%order_lanczos, &
    2031              :                eps_lanczos=almo_scf_env%eps_lanczos, &
    2032              :                max_iter_lanczos=almo_scf_env%max_iter_lanczos, &
    2033              :                para_env=almo_scf_env%para_env, &
    2034            8 :                blacs_env=almo_scf_env%blacs_env)
    2035              : 
    2036              :             ! compute dm from the projector(s)
    2037           32 :             CALL dbcsr_scale(matrix_p_tmp(ispin), spin_factor)
    2038              : 
    2039              :          END DO
    2040              : 
    2041              :          ! the KS matrix is updated outside the spin loop
    2042              :          CALL almo_dm_to_almo_ks(qs_env, &
    2043              :                                  matrix_p_tmp, &
    2044              :                                  almo_scf_env%matrix_ks, &
    2045              :                                  energy, &
    2046              :                                  almo_scf_env%eps_filter, &
    2047            8 :                                  almo_scf_env%mat_distr_aos)
    2048              : 
    2049            8 :          IF (nspins < 2) occ(2) = occ(1)
    2050            8 :          IF (unit_nr > 0) WRITE (unit_nr, '(T2,E13.3,F20.10,F20.10,F25.15)') &
    2051            4 :             eps_local, occ(1), occ(2), energy
    2052              : 
    2053            8 :          eps_local = 2.0_dp*eps_local
    2054              : 
    2055              :       END DO
    2056              : 
    2057            4 :       DO ispin = 1, nspins
    2058              : 
    2059            2 :          CALL dbcsr_release(matrix_t_tmp(ispin))
    2060            4 :          CALL dbcsr_release(matrix_p_tmp(ispin))
    2061              : 
    2062              :       END DO
    2063              : 
    2064            2 :       DEALLOCATE (matrix_t_tmp)
    2065            2 :       DEALLOCATE (matrix_p_tmp)
    2066            2 :       DEALLOCATE (occ)
    2067            2 :       DEALLOCATE (retained)
    2068              : 
    2069            2 :    END SUBROUTINE nlmo_compactification
    2070              : 
    2071              : ! *****************************************************************************
    2072              : !> \brief after SCF we have the final density and KS matrices compute various
    2073              : !>        post-scf quantities
    2074              : !> \param qs_env ...
    2075              : !> \param almo_scf_env ...
    2076              : !> \par History
    2077              : !>       2015.03 created  [Rustam Z. Khaliullin]
    2078              : !> \author Rustam Z. Khaliullin
    2079              : ! **************************************************************************************************
    2080          122 :    SUBROUTINE almo_scf_post(qs_env, almo_scf_env)
    2081              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    2082              :       TYPE(almo_scf_env_type), INTENT(INOUT)             :: almo_scf_env
    2083              : 
    2084              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'almo_scf_post'
    2085              : 
    2086              :       INTEGER                                            :: handle, ispin
    2087              :       TYPE(cp_fm_type), POINTER                          :: mo_coeff
    2088          122 :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: matrix_w
    2089          122 :       TYPE(dbcsr_type), ALLOCATABLE, DIMENSION(:)        :: matrix_t_processed
    2090          122 :       TYPE(mo_set_type), DIMENSION(:), POINTER           :: mos
    2091              :       TYPE(qs_scf_env_type), POINTER                     :: scf_env
    2092              : 
    2093          122 :       CALL timeset(routineN, handle)
    2094              : 
    2095              :       ! store matrices to speed up the next scf run
    2096          122 :       CALL almo_scf_store_extrapolation_data(almo_scf_env)
    2097              : 
    2098              :       ! orthogonalize orbitals before returning them to QS
    2099          494 :       ALLOCATE (matrix_t_processed(almo_scf_env%nspins))
    2100              :       !ALLOCATE (matrix_v_processed(almo_scf_env%nspins))
    2101              : 
    2102          250 :       DO ispin = 1, almo_scf_env%nspins
    2103              : 
    2104              :          CALL dbcsr_create(matrix_t_processed(ispin), &
    2105              :                            template=almo_scf_env%matrix_t(ispin), &
    2106          128 :                            matrix_type=dbcsr_type_no_symmetry)
    2107              : 
    2108              :          CALL dbcsr_copy(matrix_t_processed(ispin), &
    2109          128 :                          almo_scf_env%matrix_t(ispin))
    2110              : 
    2111          250 :          IF (almo_scf_env%return_orthogonalized_mos) THEN
    2112              : 
    2113              :             CALL orthogonalize_mos(ket=matrix_t_processed(ispin), &
    2114              :                                    overlap=almo_scf_env%matrix_sigma(ispin), &
    2115              :                                    metric=almo_scf_env%matrix_s(1), &
    2116              :                                    retain_locality=.FALSE., &
    2117              :                                    only_normalize=.FALSE., &
    2118              :                                    nocc_of_domain=almo_scf_env%nocc_of_domain(:, ispin), &
    2119              :                                    eps_filter=almo_scf_env%eps_filter, &
    2120              :                                    order_lanczos=almo_scf_env%order_lanczos, &
    2121              :                                    eps_lanczos=almo_scf_env%eps_lanczos, &
    2122              :                                    max_iter_lanczos=almo_scf_env%max_iter_lanczos, &
    2123          106 :                                    smear=almo_scf_env%smear)
    2124              :          END IF
    2125              : 
    2126              :       END DO
    2127              : 
    2128              :       !! RS-WARNING: If smearing ALMO is requested, rescaled fully-occupied orbitals are returned to QS
    2129              :       !! RS-WARNING: Beware that QS will not be informed about electronic entropy.
    2130              :       !!             If you want a quick and dirty transfer to QS energy, uncomment the following hack:
    2131              :       !! IF (almo_scf_env%smear) THEN
    2132              :       !!    qs_env%energy%kTS = 0.0_dp
    2133              :       !!    DO ispin = 1, almo_scf_env%nspins
    2134              :       !!       qs_env%energy%kTS = qs_env%energy%kTS + almo_scf_env%kTS(ispin)
    2135              :       !!    END DO
    2136              :       !! END IF
    2137              : 
    2138              :       ! return orbitals to QS
    2139          122 :       NULLIFY (mos, mo_coeff, scf_env)
    2140              : 
    2141          122 :       CALL get_qs_env(qs_env, mos=mos, scf_env=scf_env)
    2142              : 
    2143          250 :       DO ispin = 1, almo_scf_env%nspins
    2144              : 
    2145              :          ! Currently only fm version of mo_set is usable.
    2146              :          ! First transform the matrix_t to fm version
    2147          128 :          CALL get_mo_set(mos(ispin), mo_coeff=mo_coeff)
    2148          128 :          CALL copy_dbcsr_to_fm(matrix_t_processed(ispin), mo_coeff)
    2149          250 :          CALL dbcsr_release(matrix_t_processed(ispin))
    2150              :       END DO
    2151          250 :       DO ispin = 1, almo_scf_env%nspins
    2152          250 :          CALL dbcsr_release(matrix_t_processed(ispin))
    2153              :       END DO
    2154          122 :       DEALLOCATE (matrix_t_processed)
    2155              : 
    2156              :       ! calculate post scf properties
    2157              : 
    2158          122 :       CALL almo_post_scf_compute_properties(qs_env)
    2159              : 
    2160              :       ! compute the W matrix if associated
    2161          122 :       IF (almo_scf_env%calc_forces) THEN
    2162           66 :          CALL get_qs_env(qs_env, matrix_w=matrix_w)
    2163           66 :          IF (ASSOCIATED(matrix_w)) THEN
    2164           66 :             CALL calculate_w_matrix_almo(matrix_w, almo_scf_env)
    2165              :          ELSE
    2166            0 :             CPABORT("Matrix W is needed but not associated")
    2167              :          END IF
    2168              :       END IF
    2169              : 
    2170          122 :       CALL timestop(handle)
    2171              : 
    2172          122 :    END SUBROUTINE almo_scf_post
    2173              : 
    2174              : ! **************************************************************************************************
    2175              : !> \brief create various matrices
    2176              : !> \param almo_scf_env ...
    2177              : !> \param matrix_s0 ...
    2178              : !> \par History
    2179              : !>       2011.07 created [Rustam Z Khaliullin]
    2180              : !> \author Rustam Z Khaliullin
    2181              : ! **************************************************************************************************
    2182          122 :    SUBROUTINE almo_scf_env_create_matrices(almo_scf_env, matrix_s0)
    2183              : 
    2184              :       TYPE(almo_scf_env_type), INTENT(INOUT)             :: almo_scf_env
    2185              :       TYPE(dbcsr_type), INTENT(IN)                       :: matrix_s0
    2186              : 
    2187              :       CHARACTER(len=*), PARAMETER :: routineN = 'almo_scf_env_create_matrices'
    2188              : 
    2189              :       INTEGER                                            :: handle, ispin, nspins
    2190              : 
    2191          122 :       CALL timeset(routineN, handle)
    2192              : 
    2193          122 :       nspins = almo_scf_env%nspins
    2194              : 
    2195              :       ! AO overlap matrix and its various functions
    2196              :       CALL matrix_almo_create(matrix_new=almo_scf_env%matrix_s(1), &
    2197              :                               matrix_qs=matrix_s0, &
    2198              :                               almo_scf_env=almo_scf_env, &
    2199              :                               name_new="S", &
    2200              :                               size_keys=[almo_mat_dim_aobasis, almo_mat_dim_aobasis], &
    2201              :                               symmetry_new=dbcsr_type_symmetric, &
    2202              :                               spin_key=0, &
    2203          122 :                               init_domains=.FALSE.)
    2204              :       CALL matrix_almo_create(matrix_new=almo_scf_env%matrix_s_blk(1), &
    2205              :                               matrix_qs=matrix_s0, &
    2206              :                               almo_scf_env=almo_scf_env, &
    2207              :                               name_new="S_BLK", &
    2208              :                               size_keys=[almo_mat_dim_aobasis, almo_mat_dim_aobasis], &
    2209              :                               symmetry_new=dbcsr_type_symmetric, &
    2210              :                               spin_key=0, &
    2211          122 :                               init_domains=.TRUE.)
    2212          122 :       IF (almo_scf_env%almo_update_algorithm == almo_scf_diag) THEN
    2213              :          CALL matrix_almo_create(matrix_new=almo_scf_env%matrix_s_blk_sqrt_inv(1), &
    2214              :                                  matrix_qs=matrix_s0, &
    2215              :                                  almo_scf_env=almo_scf_env, &
    2216              :                                  name_new="S_BLK_SQRT_INV", &
    2217              :                                  size_keys=[almo_mat_dim_aobasis, almo_mat_dim_aobasis], &
    2218              :                                  symmetry_new=dbcsr_type_symmetric, &
    2219              :                                  spin_key=0, &
    2220           76 :                                  init_domains=.TRUE.)
    2221              :          CALL matrix_almo_create(matrix_new=almo_scf_env%matrix_s_blk_sqrt(1), &
    2222              :                                  matrix_qs=matrix_s0, &
    2223              :                                  almo_scf_env=almo_scf_env, &
    2224              :                                  name_new="S_BLK_SQRT", &
    2225              :                                  size_keys=[almo_mat_dim_aobasis, almo_mat_dim_aobasis], &
    2226              :                                  symmetry_new=dbcsr_type_symmetric, &
    2227              :                                  spin_key=0, &
    2228           76 :                                  init_domains=.TRUE.)
    2229           46 :       ELSE IF (almo_scf_env%almo_update_algorithm == almo_scf_dm_sign) THEN
    2230              :          CALL matrix_almo_create(matrix_new=almo_scf_env%matrix_s_blk_inv(1), &
    2231              :                                  matrix_qs=matrix_s0, &
    2232              :                                  almo_scf_env=almo_scf_env, &
    2233              :                                  name_new="S_BLK_INV", &
    2234              :                                  size_keys=[almo_mat_dim_aobasis, almo_mat_dim_aobasis], &
    2235              :                                  symmetry_new=dbcsr_type_symmetric, &
    2236              :                                  spin_key=0, &
    2237            0 :                                  init_domains=.TRUE.)
    2238              :       END IF
    2239              : 
    2240              :       ! MO coeff matrices and their derivatives
    2241          494 :       ALLOCATE (almo_scf_env%matrix_t_blk(nspins))
    2242          372 :       ALLOCATE (almo_scf_env%quench_t_blk(nspins))
    2243          372 :       ALLOCATE (almo_scf_env%matrix_err_blk(nspins))
    2244          372 :       ALLOCATE (almo_scf_env%matrix_err_xx(nspins))
    2245          372 :       ALLOCATE (almo_scf_env%matrix_sigma(nspins))
    2246          372 :       ALLOCATE (almo_scf_env%matrix_sigma_inv(nspins))
    2247          372 :       ALLOCATE (almo_scf_env%matrix_sigma_sqrt(nspins))
    2248          372 :       ALLOCATE (almo_scf_env%matrix_sigma_sqrt_inv(nspins))
    2249          372 :       ALLOCATE (almo_scf_env%matrix_sigma_blk(nspins))
    2250          372 :       ALLOCATE (almo_scf_env%matrix_sigma_inv_0deloc(nspins))
    2251          372 :       ALLOCATE (almo_scf_env%matrix_t(nspins))
    2252          372 :       ALLOCATE (almo_scf_env%matrix_t_tr(nspins))
    2253          250 :       DO ispin = 1, nspins
    2254              :          ! create the blocked quencher
    2255              :          CALL matrix_almo_create(matrix_new=almo_scf_env%quench_t_blk(ispin), &
    2256              :                                  matrix_qs=matrix_s0, &
    2257              :                                  almo_scf_env=almo_scf_env, &
    2258              :                                  name_new="Q_BLK", &
    2259              :                                  size_keys=[almo_mat_dim_aobasis, almo_mat_dim_occ], &
    2260              :                                  symmetry_new=dbcsr_type_no_symmetry, &
    2261              :                                  spin_key=ispin, &
    2262          128 :                                  init_domains=.TRUE.)
    2263              :          ! create ALMO coefficient matrix
    2264              :          CALL matrix_almo_create(matrix_new=almo_scf_env%matrix_t_blk(ispin), &
    2265              :                                  matrix_qs=matrix_s0, &
    2266              :                                  almo_scf_env=almo_scf_env, &
    2267              :                                  name_new="T_BLK", &
    2268              :                                  size_keys=[almo_mat_dim_aobasis, almo_mat_dim_occ], &
    2269              :                                  symmetry_new=dbcsr_type_no_symmetry, &
    2270              :                                  spin_key=ispin, &
    2271          128 :                                  init_domains=.TRUE.)
    2272              :          ! create the error matrix
    2273              :          CALL matrix_almo_create(matrix_new=almo_scf_env%matrix_err_blk(ispin), &
    2274              :                                  matrix_qs=matrix_s0, &
    2275              :                                  almo_scf_env=almo_scf_env, &
    2276              :                                  name_new="ERR_BLK", &
    2277              :                                  size_keys=[almo_mat_dim_aobasis, almo_mat_dim_aobasis], &
    2278              :                                  symmetry_new=dbcsr_type_no_symmetry, &
    2279              :                                  spin_key=ispin, &
    2280          128 :                                  init_domains=.TRUE.)
    2281              :          ! create the error matrix for the quenched ALMOs
    2282              :          CALL matrix_almo_create(matrix_new=almo_scf_env%matrix_err_xx(ispin), &
    2283              :                                  matrix_qs=matrix_s0, &
    2284              :                                  almo_scf_env=almo_scf_env, &
    2285              :                                  name_new="ERR_XX", &
    2286              :                                  size_keys=[almo_mat_dim_aobasis, almo_mat_dim_occ], &
    2287              :                                  symmetry_new=dbcsr_type_no_symmetry, &
    2288              :                                  spin_key=ispin, &
    2289          128 :                                  init_domains=.FALSE.)
    2290              :          ! create a matrix with dimensions of a transposed mo coefficient matrix
    2291              :          ! it might be necessary to perform the correction step using cayley
    2292              :          CALL matrix_almo_create(matrix_new=almo_scf_env%matrix_t_tr(ispin), &
    2293              :                                  matrix_qs=matrix_s0, &
    2294              :                                  almo_scf_env=almo_scf_env, &
    2295              :                                  name_new="T_TR", &
    2296              :                                  size_keys=[almo_mat_dim_occ, almo_mat_dim_aobasis], &
    2297              :                                  symmetry_new=dbcsr_type_no_symmetry, &
    2298              :                                  spin_key=ispin, &
    2299          128 :                                  init_domains=.FALSE.)
    2300              :          ! create mo overlap matrix
    2301              :          CALL matrix_almo_create(matrix_new=almo_scf_env%matrix_sigma(ispin), &
    2302              :                                  matrix_qs=matrix_s0, &
    2303              :                                  almo_scf_env=almo_scf_env, &
    2304              :                                  name_new="SIG", &
    2305              :                                  size_keys=[almo_mat_dim_occ, almo_mat_dim_occ], &
    2306              :                                  symmetry_new=dbcsr_type_symmetric, &
    2307              :                                  spin_key=ispin, &
    2308          128 :                                  init_domains=.FALSE.)
    2309              :          ! create blocked mo overlap matrix
    2310              :          CALL matrix_almo_create(matrix_new=almo_scf_env%matrix_sigma_blk(ispin), &
    2311              :                                  matrix_qs=matrix_s0, &
    2312              :                                  almo_scf_env=almo_scf_env, &
    2313              :                                  name_new="SIG_BLK", &
    2314              :                                  size_keys=[almo_mat_dim_occ, almo_mat_dim_occ], &
    2315              :                                  symmetry_new=dbcsr_type_symmetric, &
    2316              :                                  spin_key=ispin, &
    2317          128 :                                  init_domains=.TRUE.)
    2318              :          ! create blocked inverse mo overlap matrix
    2319              :          CALL matrix_almo_create(matrix_new=almo_scf_env%matrix_sigma_inv_0deloc(ispin), &
    2320              :                                  matrix_qs=matrix_s0, &
    2321              :                                  almo_scf_env=almo_scf_env, &
    2322              :                                  name_new="SIGINV_BLK", &
    2323              :                                  size_keys=[almo_mat_dim_occ, almo_mat_dim_occ], &
    2324              :                                  symmetry_new=dbcsr_type_symmetric, &
    2325              :                                  spin_key=ispin, &
    2326          128 :                                  init_domains=.TRUE.)
    2327              :          ! create inverse mo overlap matrix
    2328              :          CALL matrix_almo_create( &
    2329              :             matrix_new=almo_scf_env%matrix_sigma_inv(ispin), &
    2330              :             matrix_qs=matrix_s0, &
    2331              :             almo_scf_env=almo_scf_env, &
    2332              :             name_new="SIGINV", &
    2333              :             size_keys=[almo_mat_dim_occ, almo_mat_dim_occ], &
    2334              :             symmetry_new=dbcsr_type_symmetric, &
    2335              :             spin_key=ispin, &
    2336          128 :             init_domains=.FALSE.)
    2337              :          ! create various templates that will be necessary later
    2338              :          CALL matrix_almo_create( &
    2339              :             matrix_new=almo_scf_env%matrix_t(ispin), &
    2340              :             matrix_qs=matrix_s0, &
    2341              :             almo_scf_env=almo_scf_env, &
    2342              :             name_new="T", &
    2343              :             size_keys=[almo_mat_dim_aobasis, almo_mat_dim_occ], &
    2344              :             symmetry_new=dbcsr_type_no_symmetry, &
    2345              :             spin_key=ispin, &
    2346          128 :             init_domains=.FALSE.)
    2347              :          CALL dbcsr_create(almo_scf_env%matrix_sigma_sqrt(ispin), &
    2348              :                            template=almo_scf_env%matrix_sigma(ispin), &
    2349          128 :                            matrix_type=dbcsr_type_no_symmetry)
    2350              :          CALL dbcsr_create(almo_scf_env%matrix_sigma_sqrt_inv(ispin), &
    2351              :                            template=almo_scf_env%matrix_sigma(ispin), &
    2352          250 :                            matrix_type=dbcsr_type_no_symmetry)
    2353              :       END DO
    2354              : 
    2355              :       ! create virtual orbitals if necessary
    2356          122 :       IF (almo_scf_env%need_virtuals) THEN
    2357          372 :          ALLOCATE (almo_scf_env%matrix_v_blk(nspins))
    2358          372 :          ALLOCATE (almo_scf_env%matrix_v_full_blk(nspins))
    2359          372 :          ALLOCATE (almo_scf_env%matrix_v(nspins))
    2360          372 :          ALLOCATE (almo_scf_env%matrix_vo(nspins))
    2361          372 :          ALLOCATE (almo_scf_env%matrix_x(nspins))
    2362          372 :          ALLOCATE (almo_scf_env%matrix_ov(nspins))
    2363          372 :          ALLOCATE (almo_scf_env%matrix_ov_full(nspins))
    2364          372 :          ALLOCATE (almo_scf_env%matrix_sigma_vv(nspins))
    2365          372 :          ALLOCATE (almo_scf_env%matrix_sigma_vv_blk(nspins))
    2366          372 :          ALLOCATE (almo_scf_env%matrix_sigma_vv_sqrt(nspins))
    2367          372 :          ALLOCATE (almo_scf_env%matrix_sigma_vv_sqrt_inv(nspins))
    2368          372 :          ALLOCATE (almo_scf_env%matrix_vv_full_blk(nspins))
    2369              : 
    2370          122 :          IF (almo_scf_env%deloc_truncate_virt /= virt_full) THEN
    2371            0 :             ALLOCATE (almo_scf_env%matrix_k_blk(nspins))
    2372            0 :             ALLOCATE (almo_scf_env%matrix_k_blk_ones(nspins))
    2373            0 :             ALLOCATE (almo_scf_env%matrix_k_tr(nspins))
    2374            0 :             ALLOCATE (almo_scf_env%matrix_v_disc(nspins))
    2375            0 :             ALLOCATE (almo_scf_env%matrix_v_disc_blk(nspins))
    2376            0 :             ALLOCATE (almo_scf_env%matrix_ov_disc(nspins))
    2377            0 :             ALLOCATE (almo_scf_env%matrix_vv_disc_blk(nspins))
    2378            0 :             ALLOCATE (almo_scf_env%matrix_vv_disc(nspins))
    2379            0 :             ALLOCATE (almo_scf_env%opt_k_t_dd(nspins))
    2380            0 :             ALLOCATE (almo_scf_env%opt_k_t_rr(nspins))
    2381            0 :             ALLOCATE (almo_scf_env%opt_k_denom(nspins))
    2382              :          END IF
    2383              : 
    2384          250 :          DO ispin = 1, nspins
    2385              :             CALL matrix_almo_create(matrix_new=almo_scf_env%matrix_v_full_blk(ispin), &
    2386              :                                     matrix_qs=matrix_s0, &
    2387              :                                     almo_scf_env=almo_scf_env, &
    2388              :                                     name_new="V_FULL_BLK", &
    2389              :                                     size_keys=[almo_mat_dim_aobasis, almo_mat_dim_virt_full], &
    2390              :                                     symmetry_new=dbcsr_type_no_symmetry, &
    2391              :                                     spin_key=ispin, &
    2392          128 :                                     init_domains=.FALSE.)
    2393              :             CALL matrix_almo_create(matrix_new=almo_scf_env%matrix_v_blk(ispin), &
    2394              :                                     matrix_qs=matrix_s0, &
    2395              :                                     almo_scf_env=almo_scf_env, &
    2396              :                                     name_new="V_BLK", &
    2397              :                                     size_keys=[almo_mat_dim_aobasis, almo_mat_dim_virt], &
    2398              :                                     symmetry_new=dbcsr_type_no_symmetry, &
    2399              :                                     spin_key=ispin, &
    2400          128 :                                     init_domains=.FALSE.)
    2401              :             CALL matrix_almo_create(matrix_new=almo_scf_env%matrix_v(ispin), &
    2402              :                                     matrix_qs=matrix_s0, &
    2403              :                                     almo_scf_env=almo_scf_env, &
    2404              :                                     name_new="V", &
    2405              :                                     size_keys=[almo_mat_dim_aobasis, almo_mat_dim_virt], &
    2406              :                                     symmetry_new=dbcsr_type_no_symmetry, &
    2407              :                                     spin_key=ispin, &
    2408          128 :                                     init_domains=.FALSE.)
    2409              :             CALL matrix_almo_create(matrix_new=almo_scf_env%matrix_ov_full(ispin), &
    2410              :                                     matrix_qs=matrix_s0, &
    2411              :                                     almo_scf_env=almo_scf_env, &
    2412              :                                     name_new="OV_FULL", &
    2413              :                                     size_keys=[almo_mat_dim_occ, almo_mat_dim_virt_full], &
    2414              :                                     symmetry_new=dbcsr_type_no_symmetry, &
    2415              :                                     spin_key=ispin, &
    2416          128 :                                     init_domains=.FALSE.)
    2417              :             CALL matrix_almo_create(matrix_new=almo_scf_env%matrix_ov(ispin), &
    2418              :                                     matrix_qs=matrix_s0, &
    2419              :                                     almo_scf_env=almo_scf_env, &
    2420              :                                     name_new="OV", &
    2421              :                                     size_keys=[almo_mat_dim_occ, almo_mat_dim_virt], &
    2422              :                                     symmetry_new=dbcsr_type_no_symmetry, &
    2423              :                                     spin_key=ispin, &
    2424          128 :                                     init_domains=.FALSE.)
    2425              :             CALL matrix_almo_create(matrix_new=almo_scf_env%matrix_vo(ispin), &
    2426              :                                     matrix_qs=matrix_s0, &
    2427              :                                     almo_scf_env=almo_scf_env, &
    2428              :                                     name_new="VO", &
    2429              :                                     size_keys=[almo_mat_dim_virt, almo_mat_dim_occ], &
    2430              :                                     symmetry_new=dbcsr_type_no_symmetry, &
    2431              :                                     spin_key=ispin, &
    2432          128 :                                     init_domains=.FALSE.)
    2433              :             CALL matrix_almo_create(matrix_new=almo_scf_env%matrix_x(ispin), &
    2434              :                                     matrix_qs=matrix_s0, &
    2435              :                                     almo_scf_env=almo_scf_env, &
    2436              :                                     name_new="VO", &
    2437              :                                     size_keys=[almo_mat_dim_virt, almo_mat_dim_occ], &
    2438              :                                     symmetry_new=dbcsr_type_no_symmetry, &
    2439              :                                     spin_key=ispin, &
    2440          128 :                                     init_domains=.FALSE.)
    2441              :             CALL matrix_almo_create(matrix_new=almo_scf_env%matrix_sigma_vv(ispin), &
    2442              :                                     matrix_qs=matrix_s0, &
    2443              :                                     almo_scf_env=almo_scf_env, &
    2444              :                                     name_new="SIG_VV", &
    2445              :                                     size_keys=[almo_mat_dim_virt, almo_mat_dim_virt], &
    2446              :                                     symmetry_new=dbcsr_type_symmetric, &
    2447              :                                     spin_key=ispin, &
    2448          128 :                                     init_domains=.FALSE.)
    2449              :             CALL matrix_almo_create(matrix_new=almo_scf_env%matrix_vv_full_blk(ispin), &
    2450              :                                     matrix_qs=matrix_s0, &
    2451              :                                     almo_scf_env=almo_scf_env, &
    2452              :                                     name_new="VV_FULL_BLK", &
    2453              :                                     size_keys=[almo_mat_dim_virt_full, almo_mat_dim_virt_full], &
    2454              :                                     symmetry_new=dbcsr_type_no_symmetry, &
    2455              :                                     spin_key=ispin, &
    2456          128 :                                     init_domains=.TRUE.)
    2457              :             CALL matrix_almo_create(matrix_new=almo_scf_env%matrix_sigma_vv_blk(ispin), &
    2458              :                                     matrix_qs=matrix_s0, &
    2459              :                                     almo_scf_env=almo_scf_env, &
    2460              :                                     name_new="SIG_VV_BLK", &
    2461              :                                     size_keys=[almo_mat_dim_virt, almo_mat_dim_virt], &
    2462              :                                     symmetry_new=dbcsr_type_symmetric, &
    2463              :                                     spin_key=ispin, &
    2464          128 :                                     init_domains=.TRUE.)
    2465              :             CALL dbcsr_create(almo_scf_env%matrix_sigma_vv_sqrt(ispin), &
    2466              :                               template=almo_scf_env%matrix_sigma_vv(ispin), &
    2467          128 :                               matrix_type=dbcsr_type_no_symmetry)
    2468              :             CALL dbcsr_create(almo_scf_env%matrix_sigma_vv_sqrt_inv(ispin), &
    2469              :                               template=almo_scf_env%matrix_sigma_vv(ispin), &
    2470          128 :                               matrix_type=dbcsr_type_no_symmetry)
    2471              : 
    2472          250 :             IF (almo_scf_env%deloc_truncate_virt /= virt_full) THEN
    2473              :                CALL matrix_almo_create(matrix_new=almo_scf_env%opt_k_t_rr(ispin), &
    2474              :                                        matrix_qs=matrix_s0, &
    2475              :                                        almo_scf_env=almo_scf_env, &
    2476              :                                        name_new="OPT_K_U_RR", &
    2477              :                                        size_keys=[almo_mat_dim_virt, almo_mat_dim_virt], &
    2478              :                                        symmetry_new=dbcsr_type_no_symmetry, &
    2479              :                                        spin_key=ispin, &
    2480            0 :                                        init_domains=.FALSE.)
    2481              :                CALL matrix_almo_create(matrix_new=almo_scf_env%matrix_vv_disc(ispin), &
    2482              :                                        matrix_qs=matrix_s0, &
    2483              :                                        almo_scf_env=almo_scf_env, &
    2484              :                                        name_new="VV_DISC", &
    2485              :                                        size_keys=[almo_mat_dim_virt_disc, almo_mat_dim_virt_disc], &
    2486              :                                        symmetry_new=dbcsr_type_symmetric, &
    2487              :                                        spin_key=ispin, &
    2488            0 :                                        init_domains=.FALSE.)
    2489              :                CALL matrix_almo_create(matrix_new=almo_scf_env%opt_k_t_dd(ispin), &
    2490              :                                        matrix_qs=matrix_s0, &
    2491              :                                        almo_scf_env=almo_scf_env, &
    2492              :                                        name_new="OPT_K_U_DD", &
    2493              :                                        size_keys=[almo_mat_dim_virt_disc, almo_mat_dim_virt_disc], &
    2494              :                                        symmetry_new=dbcsr_type_no_symmetry, &
    2495              :                                        spin_key=ispin, &
    2496            0 :                                        init_domains=.FALSE.)
    2497              :                CALL matrix_almo_create(matrix_new=almo_scf_env%matrix_vv_disc_blk(ispin), &
    2498              :                                        matrix_qs=matrix_s0, &
    2499              :                                        almo_scf_env=almo_scf_env, &
    2500              :                                        name_new="VV_DISC_BLK", &
    2501              :                                        size_keys=[almo_mat_dim_virt_disc, almo_mat_dim_virt_disc], &
    2502              :                                        symmetry_new=dbcsr_type_symmetric, &
    2503              :                                        spin_key=ispin, &
    2504            0 :                                        init_domains=.TRUE.)
    2505              :                CALL matrix_almo_create(matrix_new=almo_scf_env%matrix_k_blk(ispin), &
    2506              :                                        matrix_qs=matrix_s0, &
    2507              :                                        almo_scf_env=almo_scf_env, &
    2508              :                                        name_new="K_BLK", &
    2509              :                                        size_keys=[almo_mat_dim_virt_disc, almo_mat_dim_virt], &
    2510              :                                        symmetry_new=dbcsr_type_no_symmetry, &
    2511              :                                        spin_key=ispin, &
    2512            0 :                                        init_domains=.TRUE.)
    2513              :                CALL matrix_almo_create(matrix_new=almo_scf_env%matrix_k_blk_ones(ispin), &
    2514              :                                        matrix_qs=matrix_s0, &
    2515              :                                        almo_scf_env=almo_scf_env, &
    2516              :                                        name_new="K_BLK_1", &
    2517              :                                        size_keys=[almo_mat_dim_virt_disc, almo_mat_dim_virt], &
    2518              :                                        symmetry_new=dbcsr_type_no_symmetry, &
    2519              :                                        spin_key=ispin, &
    2520            0 :                                        init_domains=.TRUE.)
    2521              :                CALL matrix_almo_create(matrix_new=almo_scf_env%opt_k_denom(ispin), &
    2522              :                                        matrix_qs=matrix_s0, &
    2523              :                                        almo_scf_env=almo_scf_env, &
    2524              :                                        name_new="OPT_K_DENOM", &
    2525              :                                        size_keys=[almo_mat_dim_virt_disc, almo_mat_dim_virt], &
    2526              :                                        symmetry_new=dbcsr_type_no_symmetry, &
    2527              :                                        spin_key=ispin, &
    2528            0 :                                        init_domains=.FALSE.)
    2529              :                CALL matrix_almo_create(matrix_new=almo_scf_env%matrix_k_tr(ispin), &
    2530              :                                        matrix_qs=matrix_s0, &
    2531              :                                        almo_scf_env=almo_scf_env, &
    2532              :                                        name_new="K_TR", &
    2533              :                                        size_keys=[almo_mat_dim_virt, almo_mat_dim_virt_disc], &
    2534              :                                        symmetry_new=dbcsr_type_no_symmetry, &
    2535              :                                        spin_key=ispin, &
    2536            0 :                                        init_domains=.FALSE.)
    2537              :                CALL matrix_almo_create(matrix_new=almo_scf_env%matrix_v_disc_blk(ispin), &
    2538              :                                        matrix_qs=matrix_s0, &
    2539              :                                        almo_scf_env=almo_scf_env, &
    2540              :                                        name_new="V_DISC_BLK", &
    2541              :                                        size_keys=[almo_mat_dim_aobasis, almo_mat_dim_virt_disc], &
    2542              :                                        symmetry_new=dbcsr_type_no_symmetry, &
    2543              :                                        spin_key=ispin, &
    2544            0 :                                        init_domains=.FALSE.)
    2545              :                CALL matrix_almo_create(matrix_new=almo_scf_env%matrix_v_disc(ispin), &
    2546              :                                        matrix_qs=matrix_s0, &
    2547              :                                        almo_scf_env=almo_scf_env, &
    2548              :                                        name_new="V_DISC", &
    2549              :                                        size_keys=[almo_mat_dim_aobasis, almo_mat_dim_virt_disc], &
    2550              :                                        symmetry_new=dbcsr_type_no_symmetry, &
    2551              :                                        spin_key=ispin, &
    2552            0 :                                        init_domains=.FALSE.)
    2553              :                CALL matrix_almo_create(matrix_new=almo_scf_env%matrix_ov_disc(ispin), &
    2554              :                                        matrix_qs=matrix_s0, &
    2555              :                                        almo_scf_env=almo_scf_env, &
    2556              :                                        name_new="OV_DISC", &
    2557              :                                        size_keys=[almo_mat_dim_occ, almo_mat_dim_virt_disc], &
    2558              :                                        symmetry_new=dbcsr_type_no_symmetry, &
    2559              :                                        spin_key=ispin, &
    2560            0 :                                        init_domains=.FALSE.)
    2561              : 
    2562              :             END IF ! end need_discarded_virtuals
    2563              : 
    2564              :          END DO ! spin
    2565              :       END IF
    2566              : 
    2567              :       ! create matrices of orbital energies if necessary
    2568          122 :       IF (almo_scf_env%need_orbital_energies) THEN
    2569          372 :          ALLOCATE (almo_scf_env%matrix_eoo(nspins))
    2570          372 :          ALLOCATE (almo_scf_env%matrix_evv_full(nspins))
    2571          250 :          DO ispin = 1, nspins
    2572              :             CALL matrix_almo_create(matrix_new=almo_scf_env%matrix_eoo(ispin), &
    2573              :                                     matrix_qs=matrix_s0, &
    2574              :                                     almo_scf_env=almo_scf_env, &
    2575              :                                     name_new="E_OCC", &
    2576              :                                     size_keys=[almo_mat_dim_occ, almo_mat_dim_occ], &
    2577              :                                     symmetry_new=dbcsr_type_no_symmetry, &
    2578              :                                     spin_key=ispin, &
    2579          128 :                                     init_domains=.FALSE.)
    2580              :             CALL matrix_almo_create(matrix_new=almo_scf_env%matrix_evv_full(ispin), &
    2581              :                                     matrix_qs=matrix_s0, &
    2582              :                                     almo_scf_env=almo_scf_env, &
    2583              :                                     name_new="E_VIRT", &
    2584              :                                     size_keys=[almo_mat_dim_virt_full, almo_mat_dim_virt_full], &
    2585              :                                     symmetry_new=dbcsr_type_no_symmetry, &
    2586              :                                     spin_key=ispin, &
    2587          250 :                                     init_domains=.FALSE.)
    2588              :          END DO
    2589              :       END IF
    2590              : 
    2591              :       ! Density and KS matrices
    2592          372 :       ALLOCATE (almo_scf_env%matrix_p(nspins))
    2593          372 :       ALLOCATE (almo_scf_env%matrix_p_blk(nspins))
    2594          372 :       ALLOCATE (almo_scf_env%matrix_ks(nspins))
    2595          372 :       ALLOCATE (almo_scf_env%matrix_ks_blk(nspins))
    2596          122 :       IF (almo_scf_env%need_previous_ks) THEN
    2597          372 :          ALLOCATE (almo_scf_env%matrix_ks_0deloc(nspins))
    2598              :       END IF
    2599          250 :       DO ispin = 1, nspins
    2600              :          ! RZK-warning copy with symmery but remember that this might cause problems
    2601              :          CALL dbcsr_create(almo_scf_env%matrix_p(ispin), &
    2602              :                            template=almo_scf_env%matrix_s(1), &
    2603          128 :                            matrix_type=dbcsr_type_symmetric)
    2604              :          CALL dbcsr_create(almo_scf_env%matrix_ks(ispin), &
    2605              :                            template=almo_scf_env%matrix_s(1), &
    2606          128 :                            matrix_type=dbcsr_type_symmetric)
    2607          128 :          IF (almo_scf_env%need_previous_ks) THEN
    2608              :             CALL dbcsr_create(almo_scf_env%matrix_ks_0deloc(ispin), &
    2609              :                               template=almo_scf_env%matrix_s(1), &
    2610          128 :                               matrix_type=dbcsr_type_symmetric)
    2611              :          END IF
    2612              :          CALL matrix_almo_create(matrix_new=almo_scf_env%matrix_p_blk(ispin), &
    2613              :                                  matrix_qs=matrix_s0, &
    2614              :                                  almo_scf_env=almo_scf_env, &
    2615              :                                  name_new="P_BLK", &
    2616              :                                  size_keys=[almo_mat_dim_aobasis, almo_mat_dim_aobasis], &
    2617              :                                  symmetry_new=dbcsr_type_symmetric, &
    2618              :                                  spin_key=ispin, &
    2619          128 :                                  init_domains=.TRUE.)
    2620              :          CALL matrix_almo_create(matrix_new=almo_scf_env%matrix_ks_blk(ispin), &
    2621              :                                  matrix_qs=matrix_s0, &
    2622              :                                  almo_scf_env=almo_scf_env, &
    2623              :                                  name_new="KS_BLK", &
    2624              :                                  size_keys=[almo_mat_dim_aobasis, almo_mat_dim_aobasis], &
    2625              :                                  symmetry_new=dbcsr_type_symmetric, &
    2626              :                                  spin_key=ispin, &
    2627          250 :                                  init_domains=.TRUE.)
    2628              :       END DO
    2629              : 
    2630          122 :       CALL timestop(handle)
    2631              : 
    2632          122 :    END SUBROUTINE almo_scf_env_create_matrices
    2633              : 
    2634              : ! **************************************************************************************************
    2635              : !> \brief clean up procedures for almo scf
    2636              : !> \param almo_scf_env ...
    2637              : !> \par History
    2638              : !>       2011.06 created [Rustam Z Khaliullin]
    2639              : !>       2018.09 smearing support [Ruben Staub]
    2640              : !> \author Rustam Z Khaliullin
    2641              : ! **************************************************************************************************
    2642          122 :    SUBROUTINE almo_scf_clean_up(almo_scf_env)
    2643              : 
    2644              :       TYPE(almo_scf_env_type), INTENT(INOUT)             :: almo_scf_env
    2645              : 
    2646              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'almo_scf_clean_up'
    2647              : 
    2648              :       INTEGER                                            :: handle, ispin, unit_nr
    2649              :       TYPE(cp_logger_type), POINTER                      :: logger
    2650              : 
    2651          122 :       CALL timeset(routineN, handle)
    2652              : 
    2653              :       ! get a useful output_unit
    2654          122 :       logger => cp_get_default_logger()
    2655          122 :       IF (logger%para_env%is_source()) THEN
    2656           61 :          unit_nr = cp_logger_get_default_unit_nr(logger, local=.TRUE.)
    2657              :       ELSE
    2658              :          unit_nr = -1
    2659              :       END IF
    2660              : 
    2661              :       ! release matrices
    2662          122 :       CALL dbcsr_release(almo_scf_env%matrix_s(1))
    2663          122 :       CALL dbcsr_release(almo_scf_env%matrix_s_blk(1))
    2664          122 :       IF (almo_scf_env%almo_update_algorithm == almo_scf_diag) THEN
    2665           76 :          CALL dbcsr_release(almo_scf_env%matrix_s_blk_sqrt_inv(1))
    2666           76 :          CALL dbcsr_release(almo_scf_env%matrix_s_blk_sqrt(1))
    2667           46 :       ELSE IF (almo_scf_env%almo_update_algorithm == almo_scf_dm_sign) THEN
    2668            0 :          CALL dbcsr_release(almo_scf_env%matrix_s_blk_inv(1))
    2669              :       END IF
    2670          250 :       DO ispin = 1, almo_scf_env%nspins
    2671          128 :          CALL dbcsr_release(almo_scf_env%quench_t(ispin))
    2672          128 :          CALL dbcsr_release(almo_scf_env%quench_t_blk(ispin))
    2673          128 :          CALL dbcsr_release(almo_scf_env%matrix_t_blk(ispin))
    2674          128 :          CALL dbcsr_release(almo_scf_env%matrix_err_blk(ispin))
    2675          128 :          CALL dbcsr_release(almo_scf_env%matrix_err_xx(ispin))
    2676          128 :          CALL dbcsr_release(almo_scf_env%matrix_t_tr(ispin))
    2677          128 :          CALL dbcsr_release(almo_scf_env%matrix_sigma(ispin))
    2678          128 :          CALL dbcsr_release(almo_scf_env%matrix_sigma_blk(ispin))
    2679          128 :          CALL dbcsr_release(almo_scf_env%matrix_sigma_inv_0deloc(ispin))
    2680          128 :          CALL dbcsr_release(almo_scf_env%matrix_sigma_inv(ispin))
    2681          128 :          CALL dbcsr_release(almo_scf_env%matrix_t(ispin))
    2682          128 :          CALL dbcsr_release(almo_scf_env%matrix_sigma_sqrt(ispin))
    2683          128 :          CALL dbcsr_release(almo_scf_env%matrix_sigma_sqrt_inv(ispin))
    2684          128 :          CALL dbcsr_release(almo_scf_env%matrix_p(ispin))
    2685          128 :          CALL dbcsr_release(almo_scf_env%matrix_ks(ispin))
    2686          128 :          CALL dbcsr_release(almo_scf_env%matrix_p_blk(ispin))
    2687          128 :          CALL dbcsr_release(almo_scf_env%matrix_ks_blk(ispin))
    2688          128 :          IF (almo_scf_env%need_previous_ks) THEN
    2689          128 :             CALL dbcsr_release(almo_scf_env%matrix_ks_0deloc(ispin))
    2690              :          END IF
    2691          128 :          IF (almo_scf_env%need_virtuals) THEN
    2692          128 :             CALL dbcsr_release(almo_scf_env%matrix_v_blk(ispin))
    2693          128 :             CALL dbcsr_release(almo_scf_env%matrix_v_full_blk(ispin))
    2694          128 :             CALL dbcsr_release(almo_scf_env%matrix_v(ispin))
    2695          128 :             CALL dbcsr_release(almo_scf_env%matrix_vo(ispin))
    2696          128 :             CALL dbcsr_release(almo_scf_env%matrix_x(ispin))
    2697          128 :             CALL dbcsr_release(almo_scf_env%matrix_ov(ispin))
    2698          128 :             CALL dbcsr_release(almo_scf_env%matrix_ov_full(ispin))
    2699          128 :             CALL dbcsr_release(almo_scf_env%matrix_sigma_vv(ispin))
    2700          128 :             CALL dbcsr_release(almo_scf_env%matrix_sigma_vv_blk(ispin))
    2701          128 :             CALL dbcsr_release(almo_scf_env%matrix_sigma_vv_sqrt(ispin))
    2702          128 :             CALL dbcsr_release(almo_scf_env%matrix_sigma_vv_sqrt_inv(ispin))
    2703          128 :             CALL dbcsr_release(almo_scf_env%matrix_vv_full_blk(ispin))
    2704          128 :             IF (almo_scf_env%deloc_truncate_virt /= virt_full) THEN
    2705            0 :                CALL dbcsr_release(almo_scf_env%matrix_k_tr(ispin))
    2706            0 :                CALL dbcsr_release(almo_scf_env%matrix_k_blk(ispin))
    2707            0 :                CALL dbcsr_release(almo_scf_env%matrix_k_blk_ones(ispin))
    2708            0 :                CALL dbcsr_release(almo_scf_env%matrix_v_disc(ispin))
    2709            0 :                CALL dbcsr_release(almo_scf_env%matrix_v_disc_blk(ispin))
    2710            0 :                CALL dbcsr_release(almo_scf_env%matrix_ov_disc(ispin))
    2711            0 :                CALL dbcsr_release(almo_scf_env%matrix_vv_disc_blk(ispin))
    2712            0 :                CALL dbcsr_release(almo_scf_env%matrix_vv_disc(ispin))
    2713            0 :                CALL dbcsr_release(almo_scf_env%opt_k_t_dd(ispin))
    2714            0 :                CALL dbcsr_release(almo_scf_env%opt_k_t_rr(ispin))
    2715            0 :                CALL dbcsr_release(almo_scf_env%opt_k_denom(ispin))
    2716              :             END IF
    2717              :          END IF
    2718          250 :          IF (almo_scf_env%need_orbital_energies) THEN
    2719          128 :             CALL dbcsr_release(almo_scf_env%matrix_eoo(ispin))
    2720          128 :             CALL dbcsr_release(almo_scf_env%matrix_evv_full(ispin))
    2721              :          END IF
    2722              :       END DO
    2723              : 
    2724              :       ! deallocate matrices
    2725          122 :       DEALLOCATE (almo_scf_env%matrix_p)
    2726          122 :       DEALLOCATE (almo_scf_env%matrix_p_blk)
    2727          122 :       DEALLOCATE (almo_scf_env%matrix_ks)
    2728          122 :       DEALLOCATE (almo_scf_env%matrix_ks_blk)
    2729          122 :       DEALLOCATE (almo_scf_env%matrix_t_blk)
    2730          122 :       DEALLOCATE (almo_scf_env%matrix_err_blk)
    2731          122 :       DEALLOCATE (almo_scf_env%matrix_err_xx)
    2732          122 :       DEALLOCATE (almo_scf_env%matrix_t)
    2733          122 :       DEALLOCATE (almo_scf_env%matrix_t_tr)
    2734          122 :       DEALLOCATE (almo_scf_env%matrix_sigma)
    2735          122 :       DEALLOCATE (almo_scf_env%matrix_sigma_blk)
    2736          122 :       DEALLOCATE (almo_scf_env%matrix_sigma_inv_0deloc)
    2737          122 :       DEALLOCATE (almo_scf_env%matrix_sigma_sqrt)
    2738          122 :       DEALLOCATE (almo_scf_env%matrix_sigma_sqrt_inv)
    2739          122 :       DEALLOCATE (almo_scf_env%matrix_sigma_inv)
    2740          122 :       DEALLOCATE (almo_scf_env%quench_t)
    2741          122 :       DEALLOCATE (almo_scf_env%quench_t_blk)
    2742          122 :       IF (almo_scf_env%need_virtuals) THEN
    2743          122 :          DEALLOCATE (almo_scf_env%matrix_v_blk)
    2744          122 :          DEALLOCATE (almo_scf_env%matrix_v_full_blk)
    2745          122 :          DEALLOCATE (almo_scf_env%matrix_v)
    2746          122 :          DEALLOCATE (almo_scf_env%matrix_vo)
    2747          122 :          DEALLOCATE (almo_scf_env%matrix_x)
    2748          122 :          DEALLOCATE (almo_scf_env%matrix_ov)
    2749          122 :          DEALLOCATE (almo_scf_env%matrix_ov_full)
    2750          122 :          DEALLOCATE (almo_scf_env%matrix_sigma_vv)
    2751          122 :          DEALLOCATE (almo_scf_env%matrix_sigma_vv_blk)
    2752          122 :          DEALLOCATE (almo_scf_env%matrix_sigma_vv_sqrt)
    2753          122 :          DEALLOCATE (almo_scf_env%matrix_sigma_vv_sqrt_inv)
    2754          122 :          DEALLOCATE (almo_scf_env%matrix_vv_full_blk)
    2755          122 :          IF (almo_scf_env%deloc_truncate_virt /= virt_full) THEN
    2756            0 :             DEALLOCATE (almo_scf_env%matrix_k_tr)
    2757            0 :             DEALLOCATE (almo_scf_env%matrix_k_blk)
    2758            0 :             DEALLOCATE (almo_scf_env%matrix_v_disc)
    2759            0 :             DEALLOCATE (almo_scf_env%matrix_v_disc_blk)
    2760            0 :             DEALLOCATE (almo_scf_env%matrix_ov_disc)
    2761            0 :             DEALLOCATE (almo_scf_env%matrix_vv_disc_blk)
    2762            0 :             DEALLOCATE (almo_scf_env%matrix_vv_disc)
    2763            0 :             DEALLOCATE (almo_scf_env%matrix_k_blk_ones)
    2764            0 :             DEALLOCATE (almo_scf_env%opt_k_t_dd)
    2765            0 :             DEALLOCATE (almo_scf_env%opt_k_t_rr)
    2766            0 :             DEALLOCATE (almo_scf_env%opt_k_denom)
    2767              :          END IF
    2768              :       END IF
    2769          122 :       IF (almo_scf_env%need_previous_ks) THEN
    2770          122 :          DEALLOCATE (almo_scf_env%matrix_ks_0deloc)
    2771              :       END IF
    2772          122 :       IF (almo_scf_env%need_orbital_energies) THEN
    2773          122 :          DEALLOCATE (almo_scf_env%matrix_eoo)
    2774          122 :          DEALLOCATE (almo_scf_env%matrix_evv_full)
    2775              :       END IF
    2776              : 
    2777              :       ! clean up other variables
    2778          250 :       DO ispin = 1, almo_scf_env%nspins
    2779              :          CALL release_submatrices( &
    2780          128 :             almo_scf_env%domain_preconditioner(:, ispin))
    2781          128 :          CALL release_submatrices(almo_scf_env%domain_s_inv(:, ispin))
    2782          128 :          CALL release_submatrices(almo_scf_env%domain_s_sqrt_inv(:, ispin))
    2783          128 :          CALL release_submatrices(almo_scf_env%domain_s_sqrt(:, ispin))
    2784          128 :          CALL release_submatrices(almo_scf_env%domain_ks_xx(:, ispin))
    2785          128 :          CALL release_submatrices(almo_scf_env%domain_t(:, ispin))
    2786          128 :          CALL release_submatrices(almo_scf_env%domain_err(:, ispin))
    2787          250 :          CALL release_submatrices(almo_scf_env%domain_r_down_up(:, ispin))
    2788              :       END DO
    2789          956 :       DEALLOCATE (almo_scf_env%domain_preconditioner)
    2790          956 :       DEALLOCATE (almo_scf_env%domain_s_inv)
    2791          956 :       DEALLOCATE (almo_scf_env%domain_s_sqrt_inv)
    2792          956 :       DEALLOCATE (almo_scf_env%domain_s_sqrt)
    2793          956 :       DEALLOCATE (almo_scf_env%domain_ks_xx)
    2794          956 :       DEALLOCATE (almo_scf_env%domain_t)
    2795          956 :       DEALLOCATE (almo_scf_env%domain_err)
    2796          956 :       DEALLOCATE (almo_scf_env%domain_r_down_up)
    2797          250 :       DO ispin = 1, almo_scf_env%nspins
    2798          128 :          DEALLOCATE (almo_scf_env%domain_map(ispin)%pairs)
    2799          250 :          DEALLOCATE (almo_scf_env%domain_map(ispin)%index1)
    2800              :       END DO
    2801          250 :       DEALLOCATE (almo_scf_env%domain_map)
    2802          122 :       DEALLOCATE (almo_scf_env%domain_index_of_ao)
    2803          122 :       DEALLOCATE (almo_scf_env%domain_index_of_atom)
    2804          122 :       DEALLOCATE (almo_scf_env%first_atom_of_domain)
    2805          122 :       DEALLOCATE (almo_scf_env%last_atom_of_domain)
    2806          122 :       DEALLOCATE (almo_scf_env%nbasis_of_domain)
    2807          122 :       IF (ALLOCATED(almo_scf_env%nocc_of_domain)) THEN
    2808          122 :          DEALLOCATE (almo_scf_env%nocc_of_domain)
    2809              :       END IF
    2810          122 :       DEALLOCATE (almo_scf_env%real_ne_of_domain)
    2811          122 :       DEALLOCATE (almo_scf_env%nvirt_full_of_domain)
    2812          122 :       DEALLOCATE (almo_scf_env%nvirt_of_domain)
    2813          122 :       DEALLOCATE (almo_scf_env%nvirt_disc_of_domain)
    2814          122 :       DEALLOCATE (almo_scf_env%mu_of_domain)
    2815          122 :       DEALLOCATE (almo_scf_env%cpu_of_domain)
    2816          122 :       DEALLOCATE (almo_scf_env%charge_of_domain)
    2817          122 :       DEALLOCATE (almo_scf_env%multiplicity_of_domain)
    2818          122 :       DEALLOCATE (almo_scf_env%activate)
    2819          122 :       IF (almo_scf_env%smear) THEN
    2820            4 :          DEALLOCATE (almo_scf_env%mo_energies)
    2821            4 :          DEALLOCATE (almo_scf_env%kTS)
    2822              :       END IF
    2823              : 
    2824          122 :       DEALLOCATE (almo_scf_env%domain_index_of_ao_block)
    2825          122 :       DEALLOCATE (almo_scf_env%domain_index_of_mo_block)
    2826              : 
    2827          122 :       CALL mp_para_env_release(almo_scf_env%para_env)
    2828          122 :       CALL cp_blacs_env_release(almo_scf_env%blacs_env)
    2829              : 
    2830          122 :       CALL timestop(handle)
    2831              : 
    2832          122 :    END SUBROUTINE almo_scf_clean_up
    2833              : 
    2834              : ! **************************************************************************************************
    2835              : !> \brief Do post scf calculations with ALMO
    2836              : !>        WARNING: ALMO post scf calculation may not work for certain quantities,
    2837              : !>        like forces, since ALMO wave function is only 'partially' optimized
    2838              : !> \param qs_env ...
    2839              : !> \par History
    2840              : !>       2016.12 created [Yifei Shi]
    2841              : !> \author Yifei Shi
    2842              : ! **************************************************************************************************
    2843          122 :    SUBROUTINE almo_post_scf_compute_properties(qs_env)
    2844              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    2845              : 
    2846          122 :       CALL qs_scf_compute_properties(qs_env)
    2847              : 
    2848          122 :    END SUBROUTINE almo_post_scf_compute_properties
    2849              : 
    2850              : END MODULE almo_scf
    2851              : 
        

Generated by: LCOV version 2.0-1