LCOV - code coverage report
Current view: top level - src - qs_initial_guess.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:c24029e) Lines: 74.3 % 642 477
Test Date: 2026-07-04 06:36:57 Functions: 75.0 % 4 3

            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 to somehow generate an initial guess
      10              : !> \par History
      11              : !>       2006.03 Moved here from qs_scf.F [Joost VandeVondele]
      12              : ! **************************************************************************************************
      13              : MODULE qs_initial_guess
      14              :    USE atom_kind_orbitals,              ONLY: calculate_atomic_orbitals
      15              :    USE atomic_kind_types,               ONLY: atomic_kind_type,&
      16              :                                               get_atomic_kind,&
      17              :                                               get_atomic_kind_set
      18              :    USE basis_set_types,                 ONLY: get_gto_basis_set,&
      19              :                                               gto_basis_set_type
      20              :    USE cp_control_types,                ONLY: dft_control_type
      21              :    USE cp_dbcsr_api,                    ONLY: &
      22              :         dbcsr_copy, dbcsr_filter, dbcsr_get_info, dbcsr_get_num_blocks, dbcsr_get_occupation, &
      23              :         dbcsr_iterator_blocks_left, dbcsr_iterator_next_block, dbcsr_iterator_start, &
      24              :         dbcsr_iterator_stop, dbcsr_iterator_type, dbcsr_multiply, dbcsr_p_type, dbcsr_release, &
      25              :         dbcsr_scale, dbcsr_set, dbcsr_type, dbcsr_verify_matrix
      26              :    USE cp_dbcsr_contrib,                ONLY: dbcsr_checksum,&
      27              :                                               dbcsr_dot,&
      28              :                                               dbcsr_get_diag,&
      29              :                                               dbcsr_set_diag
      30              :    USE cp_dbcsr_operations,             ONLY: copy_dbcsr_to_fm,&
      31              :                                               copy_fm_to_dbcsr,&
      32              :                                               cp_dbcsr_sm_fm_multiply,&
      33              :                                               cp_fm_to_dbcsr_row_template
      34              :    USE cp_fm_cholesky,                  ONLY: cp_fm_cholesky_decompose
      35              :    USE cp_fm_struct,                    ONLY: cp_fm_struct_create,&
      36              :                                               cp_fm_struct_get,&
      37              :                                               cp_fm_struct_release,&
      38              :                                               cp_fm_struct_type
      39              :    USE cp_fm_types,                     ONLY: &
      40              :         cp_fm_create, cp_fm_get_info, cp_fm_get_submatrix, cp_fm_init_random, cp_fm_release, &
      41              :         cp_fm_set_all, cp_fm_set_submatrix, cp_fm_to_fm, cp_fm_type
      42              :    USE cp_log_handling,                 ONLY: cp_get_default_logger,&
      43              :                                               cp_logger_get_default_io_unit,&
      44              :                                               cp_logger_type,&
      45              :                                               cp_to_string
      46              :    USE cp_output_handling,              ONLY: cp_print_key_finished_output,&
      47              :                                               cp_print_key_unit_nr
      48              :    USE external_potential_types,        ONLY: all_potential_type,&
      49              :                                               gth_potential_type,&
      50              :                                               sgp_potential_type
      51              :    USE hfx_types,                       ONLY: hfx_type
      52              :    USE input_constants,                 ONLY: &
      53              :         atomic_guess, core_guess, eht_guess, history_guess, mopac_guess, no_guess, random_guess, &
      54              :         restart_guess, sparse_guess
      55              :    USE input_cp2k_hfx,                  ONLY: ri_mo
      56              :    USE input_section_types,             ONLY: section_vals_get_subs_vals,&
      57              :                                               section_vals_type,&
      58              :                                               section_vals_val_get
      59              :    USE kinds,                           ONLY: default_path_length,&
      60              :                                               dp
      61              :    USE kpoint_io,                       ONLY: read_kpoints_restart
      62              :    USE kpoint_types,                    ONLY: kpoint_type
      63              :    USE message_passing,                 ONLY: mp_para_env_type
      64              :    USE particle_methods,                ONLY: get_particle_set
      65              :    USE particle_types,                  ONLY: particle_type
      66              :    USE qs_atomic_block,                 ONLY: calculate_atomic_block_dm
      67              :    USE qs_cneo_types,                   ONLY: cneo_potential_type
      68              :    USE qs_density_matrices,             ONLY: calculate_density_matrix
      69              :    USE qs_dftb_utils,                   ONLY: get_dftb_atom_param
      70              :    USE qs_eht_guess,                    ONLY: calculate_eht_guess
      71              :    USE qs_environment_types,            ONLY: get_qs_env,&
      72              :                                               qs_environment_type
      73              :    USE qs_kind_types,                   ONLY: get_qs_kind,&
      74              :                                               get_qs_kind_set,&
      75              :                                               qs_kind_type
      76              :    USE qs_mo_io,                        ONLY: read_mo_set_from_restart,&
      77              :                                               wfn_restart_file_name
      78              :    USE qs_mo_methods,                   ONLY: make_basis_lowdin,&
      79              :                                               make_basis_simple,&
      80              :                                               make_basis_sm
      81              :    USE qs_mo_occupation,                ONLY: set_mo_occupation
      82              :    USE qs_mo_types,                     ONLY: get_mo_set,&
      83              :                                               mo_set_restrict,&
      84              :                                               mo_set_type,&
      85              :                                               reassign_allocated_mos
      86              :    USE qs_mom_methods,                  ONLY: do_mom_guess
      87              :    USE qs_rho_methods,                  ONLY: qs_rho_update_rho
      88              :    USE qs_rho_types,                    ONLY: qs_rho_get,&
      89              :                                               qs_rho_type
      90              :    USE qs_scf_methods,                  ONLY: eigensolver,&
      91              :                                               eigensolver_simple
      92              :    USE qs_scf_types,                    ONLY: block_davidson_diag_method_nr,&
      93              :                                               block_krylov_diag_method_nr,&
      94              :                                               general_diag_method_nr,&
      95              :                                               ot_diag_method_nr,&
      96              :                                               qs_scf_env_type
      97              :    USE qs_wf_history_methods,           ONLY: wfi_update
      98              :    USE scf_control_types,               ONLY: scf_control_type
      99              :    USE util,                            ONLY: sort
     100              :    USE xtb_types,                       ONLY: get_xtb_atom_param,&
     101              :                                               xtb_atom_type
     102              : #include "./base/base_uses.f90"
     103              : 
     104              :    IMPLICIT NONE
     105              : 
     106              :    PRIVATE
     107              : 
     108              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_initial_guess'
     109              : 
     110              :    PUBLIC ::  calculate_first_density_matrix, calculate_mopac_dm
     111              :    PUBLIC ::  calculate_atomic_fock_matrix
     112              : 
     113              :    TYPE atom_matrix_type
     114              :       REAL(KIND=dp), DIMENSION(:, :, :), POINTER   :: mat => NULL()
     115              :    END TYPE atom_matrix_type
     116              : 
     117              : CONTAINS
     118              : 
     119              : ! **************************************************************************************************
     120              : !> \brief can use a variety of methods to come up with an initial
     121              : !>      density matrix and optionally an initial wavefunction
     122              : !> \param scf_env  SCF environment information
     123              : !> \param qs_env   QS environment
     124              : !> \par History
     125              : !>      03.2006 moved here from qs_scf [Joost VandeVondele]
     126              : !>      06.2007 allow to skip the initial guess [jgh]
     127              : !>      08.2014 kpoints [JGH]
     128              : !>      10.2019 tot_corr_zeff, switch_surf_dip [SGh]
     129              : !> \note
     130              : !>      badly needs to be split in subroutines each doing one of the possible
     131              : !>      schemes
     132              : ! **************************************************************************************************
     133         9459 :    SUBROUTINE calculate_first_density_matrix(scf_env, qs_env)
     134              : 
     135              :       TYPE(qs_scf_env_type), POINTER                     :: scf_env
     136              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     137              : 
     138              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'calculate_first_density_matrix'
     139              : 
     140              :       CHARACTER(LEN=default_path_length)                 :: file_name, filename
     141              :       INTEGER :: atom_a, density_guess, handle, homo, i, iatom, ic, icol, id_nr, ikind, irow, &
     142              :          iseed(4), ispin, istart_col, istart_row, j, last_read, n, n_cols, n_rows, nao, natom, &
     143              :          natoms, natoms_tmp, nblocks, nelectron, nmo, nmo_tmp, not_read, nsgf, nspin, nvec, ounit, &
     144              :          safe_density_guess, size_atomic_kind_set, z
     145         9459 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: first_sgf, kind_of, last_sgf
     146              :       INTEGER, DIMENSION(2)                              :: nelectron_spin
     147         9459 :       INTEGER, DIMENSION(:), POINTER                     :: atom_list, elec_conf, nelec_kind, &
     148         9459 :                                                             sort_kind
     149              :       LOGICAL :: cneo_potential_present, did_guess, do_hfx_ri_mo, do_kpoints, do_std_diag, exist, &
     150              :          has_unit_metric, natom_mismatch, need_mos, need_wm, ofgpw, owns_ortho, print_history_log, &
     151              :          print_log
     152         9459 :       REAL(dp), ALLOCATABLE, DIMENSION(:, :)             :: buff, buff2
     153         9459 :       REAL(dp), DIMENSION(:, :), POINTER                 :: pdata
     154              :       REAL(KIND=dp)                                      :: checksum, eps, length, maxocc, occ, &
     155              :                                                             rscale, tot_corr_zeff, trps1, zeff
     156              :       REAL(KIND=dp), DIMENSION(0:3)                      :: edftb
     157         9459 :       TYPE(atom_matrix_type), DIMENSION(:), POINTER      :: pmat
     158         9459 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
     159              :       TYPE(atomic_kind_type), POINTER                    :: atomic_kind
     160              :       TYPE(cp_fm_struct_type), POINTER                   :: ao_ao_struct, ao_mo_struct
     161              :       TYPE(cp_fm_type)                                   :: sv
     162         9459 :       TYPE(cp_fm_type), DIMENSION(:), POINTER            :: work1
     163              :       TYPE(cp_fm_type), POINTER                          :: mo_coeff, moa, mob, ortho, work2
     164              :       TYPE(cp_logger_type), POINTER                      :: logger
     165              :       TYPE(dbcsr_iterator_type)                          :: iter
     166         9459 :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: h_core_sparse, matrix_ks, p_rmpv, &
     167         9459 :                                                             s_sparse
     168         9459 :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: matrix_h_kp, matrix_ks_kp, matrix_s_kp, &
     169         9459 :                                                             rho_ao_kp
     170              :       TYPE(dbcsr_type)                                   :: mo_dbcsr, mo_tmp_dbcsr
     171              :       TYPE(dft_control_type), POINTER                    :: dft_control
     172              :       TYPE(gto_basis_set_type), POINTER                  :: orb_basis_set
     173         9459 :       TYPE(hfx_type), DIMENSION(:, :), POINTER           :: x_data
     174              :       TYPE(kpoint_type), POINTER                         :: kpoints
     175         9459 :       TYPE(mo_set_type), DIMENSION(:), POINTER           :: mo_array, mos_last_converged
     176              :       TYPE(mp_para_env_type), POINTER                    :: para_env
     177         9459 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     178         9459 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
     179              :       TYPE(qs_kind_type), POINTER                        :: qs_kind
     180              :       TYPE(qs_rho_type), POINTER                         :: rho
     181              :       TYPE(scf_control_type), POINTER                    :: scf_control
     182              :       TYPE(section_vals_type), POINTER                   :: dft_section, input, subsys_section
     183              : 
     184        18918 :       logger => cp_get_default_logger()
     185         9459 :       NULLIFY (atomic_kind, qs_kind, mo_coeff, orb_basis_set, atomic_kind_set, &
     186         9459 :                qs_kind_set, particle_set, ortho, work2, work1, mo_array, s_sparse, &
     187         9459 :                scf_control, dft_control, p_rmpv, para_env, h_core_sparse, matrix_ks, rho, &
     188         9459 :                mos_last_converged)
     189         9459 :       NULLIFY (dft_section, input, subsys_section)
     190         9459 :       NULLIFY (matrix_s_kp, matrix_h_kp, matrix_ks_kp, rho_ao_kp)
     191         9459 :       NULLIFY (moa, mob)
     192         9459 :       NULLIFY (atom_list, elec_conf, kpoints)
     193              :       edftb = 0.0_dp
     194         9459 :       tot_corr_zeff = 0.0_dp
     195              : 
     196         9459 :       CALL timeset(routineN, handle)
     197              : 
     198              :       CALL get_qs_env(qs_env, &
     199              :                       atomic_kind_set=atomic_kind_set, &
     200              :                       qs_kind_set=qs_kind_set, &
     201              :                       particle_set=particle_set, &
     202              :                       mos=mo_array, &
     203              :                       matrix_s_kp=matrix_s_kp, &
     204              :                       matrix_h_kp=matrix_h_kp, &
     205              :                       matrix_ks_kp=matrix_ks_kp, &
     206              :                       input=input, &
     207              :                       scf_control=scf_control, &
     208              :                       dft_control=dft_control, &
     209              :                       has_unit_metric=has_unit_metric, &
     210              :                       do_kpoints=do_kpoints, &
     211              :                       kpoints=kpoints, &
     212              :                       rho=rho, &
     213              :                       nelectron_spin=nelectron_spin, &
     214              :                       para_env=para_env, &
     215         9459 :                       x_data=x_data)
     216              : 
     217         9459 :       CALL qs_rho_get(rho, rho_ao_kp=rho_ao_kp)
     218              : 
     219         9459 :       IF (dft_control%switch_surf_dip) THEN
     220            2 :          CALL get_qs_env(qs_env, mos_last_converged=mos_last_converged)
     221              :       END IF
     222              : 
     223              :       ! just initialize the first image, the other density are set to zero
     224        20603 :       DO ispin = 1, dft_control%nspins
     225       148871 :          DO ic = 1, SIZE(rho_ao_kp, 2)
     226       139412 :             CALL dbcsr_set(rho_ao_kp(ispin, ic)%matrix, 0.0_dp)
     227              :          END DO
     228              :       END DO
     229         9459 :       s_sparse => matrix_s_kp(:, 1)
     230         9459 :       h_core_sparse => matrix_h_kp(:, 1)
     231         9459 :       matrix_ks => matrix_ks_kp(:, 1)
     232         9459 :       p_rmpv => rho_ao_kp(:, 1)
     233              : 
     234         9459 :       work1 => scf_env%scf_work1
     235         9459 :       work2 => scf_env%scf_work2
     236         9459 :       ortho => scf_env%ortho
     237              : 
     238         9459 :       dft_section => section_vals_get_subs_vals(input, "DFT")
     239              : 
     240         9459 :       nspin = dft_control%nspins
     241         9459 :       ofgpw = dft_control%qs_control%ofgpw
     242         9459 :       density_guess = scf_control%density_guess
     243         9459 :       do_std_diag = .FALSE.
     244              : 
     245         9459 :       do_hfx_ri_mo = .FALSE.
     246         9459 :       IF (ASSOCIATED(x_data)) THEN
     247         1350 :          IF (x_data(1, 1)%do_hfx_ri) THEN
     248          128 :             IF (x_data(1, 1)%ri_data%flavor == ri_mo) do_hfx_ri_mo = .TRUE.
     249              :          END IF
     250              :       END IF
     251              : 
     252         9459 :       IF (ASSOCIATED(scf_env%krylov_space)) do_std_diag = (scf_env%krylov_space%eps_std_diag > 0.0_dp)
     253              : 
     254              :       need_mos = scf_control%use_ot .OR. scf_env%method == ot_diag_method_nr .OR. &
     255              :                  (scf_env%method == block_krylov_diag_method_nr .AND. .NOT. do_std_diag) &
     256              :                  .OR. dft_control%do_admm .OR. scf_env%method == block_davidson_diag_method_nr &
     257         9459 :                  .OR. do_hfx_ri_mo
     258              : 
     259         9459 :       safe_density_guess = atomic_guess
     260         9459 :       IF (dft_control%qs_control%semi_empirical .OR. dft_control%qs_control%dftb) THEN
     261         1302 :          IF (density_guess == atomic_guess) density_guess = mopac_guess
     262              :          safe_density_guess = mopac_guess
     263              :       END IF
     264         9459 :       IF (dft_control%qs_control%xtb) THEN
     265         2334 :          IF (do_kpoints) THEN
     266         1348 :             IF (density_guess == atomic_guess) density_guess = mopac_guess
     267              :             safe_density_guess = mopac_guess
     268              :          ELSE
     269          986 :             IF (density_guess == atomic_guess) density_guess = core_guess
     270              :             safe_density_guess = core_guess
     271              :          END IF
     272              :       END IF
     273              : 
     274         9459 :       IF (scf_control%use_ot .AND. &
     275              :           (.NOT. ((density_guess == random_guess) .OR. &
     276              :                   (density_guess == atomic_guess) .OR. &
     277              :                   (density_guess == core_guess) .OR. &
     278              :                   (density_guess == mopac_guess) .OR. &
     279              :                   (density_guess == eht_guess) .OR. &
     280              :                   (density_guess == sparse_guess) .OR. &
     281              :                   (((density_guess == restart_guess) .OR. &
     282              :                     (density_guess == history_guess)) .AND. &
     283              :                    (scf_control%level_shift == 0.0_dp))))) THEN
     284              :          CALL cp_abort(__LOCATION__, &
     285            0 :                        "OT needs GUESS ATOMIC / CORE / RANDOM / SPARSE / RESTART / HISTORY RESTART: other options NYI")
     286              :       END IF
     287              : 
     288              :       ! if a restart was requested, check that the file exists,
     289              :       ! if not we fall back to an atomic guess. No kidding, the file name should remain
     290              :       ! in sync with read_mo_set_from_restart
     291         9459 :       id_nr = 0
     292         9459 :       IF (density_guess == restart_guess) THEN
     293              :          ! only check existence on I/O node, otherwise if file exists there but
     294              :          ! not on compute nodes, everything goes crazy even though only I/O
     295              :          ! node actually reads the file
     296          616 :          IF (do_kpoints) THEN
     297           22 :             IF (para_env%is_source()) THEN
     298           11 :                CALL wfn_restart_file_name(file_name, exist, dft_section, logger, kp=.TRUE.)
     299              :             END IF
     300              :          ELSE
     301          594 :             IF (para_env%is_source()) THEN
     302          311 :                CALL wfn_restart_file_name(file_name, exist, dft_section, logger)
     303              :             END IF
     304              :          END IF
     305          616 :          CALL para_env%bcast(exist)
     306          616 :          CALL para_env%bcast(file_name)
     307          616 :          IF (.NOT. exist) THEN
     308              :             CALL cp_warn(__LOCATION__, &
     309              :                          "User requested to restart the wavefunction from the file named: "// &
     310              :                          TRIM(file_name)//". This file does not exist. Please check the existence of"// &
     311              :                          " the file or change properly the value of the keyword WFN_RESTART_FILE_NAME."// &
     312          124 :                          " Calculation continues using ATOMIC GUESS. ")
     313          124 :             density_guess = safe_density_guess
     314              :          END IF
     315         8843 :       ELSE IF (density_guess == history_guess) THEN
     316            2 :          IF (do_kpoints) THEN
     317            0 :             CPABORT("calculate_first_density_matrix: history_guess not implemented for k-points")
     318              :          END IF
     319            2 :          IF (para_env%is_source()) THEN
     320            1 :             CALL wfn_restart_file_name(file_name, exist, dft_section, logger)
     321              :          END IF
     322            2 :          CALL para_env%bcast(exist)
     323            2 :          CALL para_env%bcast(file_name)
     324            2 :          nvec = qs_env%wf_history%memory_depth
     325            2 :          not_read = nvec + 1
     326              :          ! At this level we read the saved backup RESTART files..
     327            6 :          DO i = 1, nvec
     328            4 :             j = i - 1
     329            4 :             filename = TRIM(file_name)
     330            4 :             IF (j /= 0) THEN
     331            2 :                filename = TRIM(file_name)//".bak-"//ADJUSTL(cp_to_string(j))
     332              :             END IF
     333            4 :             IF (para_env%is_source()) &
     334            2 :                INQUIRE (FILE=filename, exist=exist)
     335            4 :             CALL para_env%bcast(exist)
     336            6 :             IF ((.NOT. exist) .AND. (i < not_read)) THEN
     337              :                not_read = i
     338              :             END IF
     339              :          END DO
     340            2 :          IF (not_read == 1) THEN
     341            0 :             density_guess = restart_guess
     342            0 :             filename = TRIM(file_name)
     343            0 :             IF (para_env%is_source()) INQUIRE (FILE=filename, exist=exist)
     344            0 :             CALL para_env%bcast(exist)
     345            0 :             IF (.NOT. exist) THEN
     346              :                CALL cp_warn(__LOCATION__, &
     347              :                             "User requested to restart the wavefunction from a series of restart files named: "// &
     348              :                             TRIM(file_name)//" with extensions (.bak-n). These files do not exist."// &
     349              :                             " Even trying to switch to a plain restart wave-function failes because the"// &
     350              :                             " file named: "//TRIM(file_name)//" does not exist. Please check the existence of"// &
     351              :                             " the file or change properly the value of the keyword WFN_RESTART_FILE_NAME."// &
     352            0 :                             " Calculation continues using ATOMIC GUESS. ")
     353            0 :                density_guess = safe_density_guess
     354              :             END IF
     355              :          END IF
     356            2 :          last_read = not_read - 1
     357              :       END IF
     358              : 
     359         9459 :       did_guess = .FALSE.
     360              : 
     361         9459 :       IF (dft_control%correct_el_density_dip) THEN
     362            4 :          tot_corr_zeff = qs_env%total_zeff_corr
     363            4 :          IF ((ABS(tot_corr_zeff) > 0.0_dp) .AND. (density_guess /= restart_guess)) THEN
     364              :             CALL cp_warn(__LOCATION__, &
     365              :                          "Use SCF_GUESS RESTART in conjunction with "// &
     366              :                          "CORE_CORRECTION /= 0.0 and SURFACE_DIPOLE_CORRECTION TRUE. "// &
     367              :                          "It is always advisable to perform SURFACE_DIPOLE_CORRECTION "// &
     368              :                          "after a simulation without the surface dipole correction "// &
     369            4 :                          "and using the ensuing wavefunction restart file. ")
     370              :          END IF
     371              :       END IF
     372              : 
     373         9459 :       ounit = -1
     374         9459 :       print_log = .FALSE.
     375         9459 :       print_history_log = .FALSE.
     376         9459 :       IF (para_env%is_source()) THEN
     377              :          CALL section_vals_val_get(dft_section, &
     378              :                                    "SCF%PRINT%RESTART%LOG_PRINT_KEY", &
     379         4768 :                                    l_val=print_log)
     380              :          CALL section_vals_val_get(dft_section, &
     381              :                                    "SCF%PRINT%RESTART_HISTORY%LOG_PRINT_KEY", &
     382         4768 :                                    l_val=print_history_log)
     383         4768 :          IF (print_log .OR. print_history_log) THEN
     384           13 :             ounit = cp_logger_get_default_io_unit(logger)
     385              :          END IF
     386              :       END IF
     387              : 
     388         9459 :       IF (density_guess == restart_guess) THEN
     389          492 :          IF (ounit > 0) THEN
     390              :             WRITE (UNIT=ounit, FMT="(/,T2,A)") &
     391            4 :                "WFN_RESTART| Reading restart file"
     392              :          END IF
     393          492 :          IF (do_kpoints) THEN
     394           12 :             natoms = SIZE(particle_set)
     395              :             CALL read_kpoints_restart(rho_ao_kp, kpoints, work1, &
     396           12 :                                       natoms, para_env, id_nr, dft_section, natom_mismatch)
     397           12 :             IF (natom_mismatch) density_guess = safe_density_guess
     398              :          ELSE
     399              :             CALL read_mo_set_from_restart(mo_array, qs_kind_set, particle_set, para_env, &
     400              :                                           id_nr=id_nr, multiplicity=dft_control%multiplicity, &
     401              :                                           dft_section=dft_section, &
     402              :                                           natom_mismatch=natom_mismatch, &
     403          480 :                                           out_unit=ounit)
     404              : 
     405          480 :             IF (natom_mismatch) THEN
     406              :                density_guess = safe_density_guess
     407              :             ELSE
     408         1272 :                DO ispin = 1, nspin
     409          812 :                   IF (scf_control%level_shift /= 0.0_dp) THEN
     410            0 :                      CALL get_mo_set(mo_set=mo_array(ispin), mo_coeff=mo_coeff)
     411            0 :                      CALL cp_fm_to_fm(mo_coeff, ortho)
     412              :                   END IF
     413              : 
     414              :                   ! make all nmo vectors present orthonormal
     415              :                   CALL get_mo_set(mo_set=mo_array(ispin), &
     416          812 :                                   mo_coeff=mo_coeff, nmo=nmo, homo=homo)
     417              : 
     418          812 :                   IF (has_unit_metric) THEN
     419            4 :                      CALL make_basis_simple(mo_coeff, nmo)
     420          808 :                   ELSEIF (dft_control%smear) THEN
     421              :                      CALL make_basis_lowdin(vmatrix=mo_coeff, ncol=nmo, &
     422          104 :                                             matrix_s=s_sparse(1)%matrix)
     423              :                   ELSE
     424              :                      ! ortho so that one can restart for different positions (basis sets?)
     425          704 :                      CALL make_basis_sm(mo_coeff, homo, s_sparse(1)%matrix)
     426              :                   END IF
     427              :                   ! only alpha spin is kept for restricted
     428         2084 :                   IF (dft_control%restricted) EXIT
     429              :                END DO
     430          480 :                IF (dft_control%restricted) CALL mo_set_restrict(mo_array)
     431              : 
     432          480 :                IF (.NOT. scf_control%diagonalization%mom) THEN
     433          464 :                   IF (dft_control%correct_surf_dip) THEN
     434            0 :                      IF (ABS(tot_corr_zeff) > 0.0_dp) THEN
     435              :                         CALL set_mo_occupation(mo_array, smear=qs_env%scf_control%smear, &
     436            0 :                                                tot_zeff_corr=tot_corr_zeff)
     437              :                      ELSE
     438            0 :                         CALL set_mo_occupation(mo_array, smear=qs_env%scf_control%smear)
     439              :                      END IF
     440              :                   ELSE
     441          464 :                      CALL set_mo_occupation(mo_array, smear=qs_env%scf_control%smear)
     442              :                   END IF
     443              :                END IF
     444              : 
     445         1312 :                DO ispin = 1, nspin
     446              : 
     447          832 :                   IF (scf_control%use_ot .OR. scf_env%method == ot_diag_method_nr) THEN !fm->dbcsr
     448              :                      CALL copy_fm_to_dbcsr(mo_array(ispin)%mo_coeff, &
     449          570 :                                            mo_array(ispin)%mo_coeff_b) !fm->dbcsr
     450              :                   END IF !fm->dbcsr
     451              : 
     452              :                   CALL calculate_density_matrix(mo_array(ispin), &
     453         1312 :                                                 p_rmpv(ispin)%matrix)
     454              :                END DO
     455              :             END IF ! natom_mismatch
     456              : 
     457              :          END IF
     458              : 
     459              :          ! Maximum Overlap Method
     460          492 :          IF (scf_control%diagonalization%mom) THEN
     461           16 :             CALL do_mom_guess(nspin, mo_array, scf_control, p_rmpv)
     462              :          END IF
     463              : 
     464              :          did_guess = .TRUE.
     465              :       END IF
     466              : 
     467         9459 :       IF (density_guess == history_guess) THEN
     468            2 :          IF (not_read > 1) THEN
     469            2 :             IF (ounit > 0) THEN
     470              :                WRITE (UNIT=ounit, FMT="(/,T2,A)") &
     471            1 :                   "WFN_RESTART| Reading restart file history"
     472              :             END IF
     473            6 :             DO i = 1, last_read
     474            4 :                j = last_read - i
     475              :                CALL read_mo_set_from_restart(mo_array, qs_kind_set, particle_set, para_env, &
     476              :                                              id_nr=j, multiplicity=dft_control%multiplicity, &
     477            4 :                                              dft_section=dft_section, out_unit=ounit)
     478              : 
     479            8 :                DO ispin = 1, nspin
     480            4 :                   IF (scf_control%level_shift /= 0.0_dp) THEN
     481            0 :                      CALL get_mo_set(mo_set=mo_array(ispin), mo_coeff=mo_coeff)
     482            0 :                      CALL cp_fm_to_fm(mo_coeff, ortho)
     483              :                   END IF
     484              : 
     485              :                   ! make all nmo vectors present orthonormal
     486            4 :                   CALL get_mo_set(mo_set=mo_array(ispin), mo_coeff=mo_coeff, nmo=nmo, homo=homo)
     487              : 
     488            4 :                   IF (has_unit_metric) THEN
     489            0 :                      CALL make_basis_simple(mo_coeff, nmo)
     490              :                   ELSE
     491              :                      ! ortho so that one can restart for different positions (basis sets?)
     492            4 :                      CALL make_basis_sm(mo_coeff, homo, s_sparse(1)%matrix)
     493              :                   END IF
     494              :                   ! only alpha spin is kept for restricted
     495           12 :                   IF (dft_control%restricted) EXIT
     496              :                END DO
     497            4 :                IF (dft_control%restricted) CALL mo_set_restrict(mo_array)
     498              : 
     499            8 :                DO ispin = 1, nspin
     500              :                   CALL set_mo_occupation(mo_set=mo_array(ispin), &
     501            8 :                                          smear=qs_env%scf_control%smear)
     502              :                END DO
     503              : 
     504            8 :                DO ispin = 1, nspin
     505            4 :                   IF (scf_control%use_ot .OR. scf_env%method == ot_diag_method_nr) THEN !fm->dbcsr
     506              :                      CALL copy_fm_to_dbcsr(mo_array(ispin)%mo_coeff, &
     507            4 :                                            mo_array(ispin)%mo_coeff_b) !fm->dbcsr
     508              :                   END IF !fm->dbcsr
     509            8 :                   CALL calculate_density_matrix(mo_array(ispin), p_rmpv(ispin)%matrix)
     510              :                END DO
     511              : 
     512              :                ! Write to extrapolation pipeline
     513            6 :                CALL wfi_update(wf_history=qs_env%wf_history, qs_env=qs_env, dt=1.0_dp)
     514              :             END DO
     515              :          END IF
     516              : 
     517              :          did_guess = .TRUE.
     518              :       END IF
     519              : 
     520         9459 :       IF (density_guess == random_guess) THEN
     521              : 
     522           52 :          DO ispin = 1, nspin
     523              :             CALL get_mo_set(mo_set=mo_array(ispin), &
     524           30 :                             mo_coeff=mo_coeff, nmo=nmo)
     525           30 :             CALL cp_fm_init_random(mo_coeff, nmo)
     526           30 :             IF (has_unit_metric) THEN
     527            2 :                CALL make_basis_simple(mo_coeff, nmo)
     528              :             ELSE
     529           28 :                CALL make_basis_sm(mo_coeff, nmo, s_sparse(1)%matrix)
     530              :             END IF
     531              :             ! only alpha spin is kept for restricted
     532           82 :             IF (dft_control%restricted) EXIT
     533              :          END DO
     534           22 :          IF (dft_control%restricted) CALL mo_set_restrict(mo_array)
     535              : 
     536           52 :          DO ispin = 1, nspin
     537              :             CALL set_mo_occupation(mo_set=mo_array(ispin), &
     538           52 :                                    smear=qs_env%scf_control%smear)
     539              :          END DO
     540              : 
     541           52 :          DO ispin = 1, nspin
     542              : 
     543           30 :             IF (scf_control%use_ot .OR. scf_env%method == ot_diag_method_nr) THEN !fm->dbcsr
     544              :                CALL copy_fm_to_dbcsr(mo_array(ispin)%mo_coeff, &
     545           22 :                                      mo_array(ispin)%mo_coeff_b) !fm->dbcsr
     546              :             END IF !fm->dbcsr
     547              : 
     548           52 :             CALL calculate_density_matrix(mo_array(ispin), p_rmpv(ispin)%matrix)
     549              :          END DO
     550              : 
     551              :          did_guess = .TRUE.
     552              :       END IF
     553              : 
     554         9459 :       IF (density_guess == core_guess) THEN
     555              : 
     556          184 :          IF (do_kpoints) THEN
     557            0 :             CPABORT("calculate_first_density_matrix: core_guess not implemented for k-points")
     558              :          END IF
     559              : 
     560          184 :          CALL get_qs_kind_set(qs_kind_set, cneo_potential_present=cneo_potential_present)
     561          184 :          IF (cneo_potential_present) THEN
     562            0 :             CPABORT("calculate_first_density_matrix: core_guess not implemented for CNEO")
     563              :          END IF
     564              : 
     565          184 :          owns_ortho = .FALSE.
     566          184 :          IF (.NOT. ASSOCIATED(work1)) THEN
     567           46 :             need_wm = .TRUE.
     568           46 :             CPASSERT(.NOT. ASSOCIATED(work2))
     569           46 :             CPASSERT(.NOT. ASSOCIATED(ortho))
     570              :          ELSE
     571          138 :             need_wm = .FALSE.
     572          138 :             CPASSERT(ASSOCIATED(work2))
     573          138 :             IF (.NOT. ASSOCIATED(ortho)) THEN
     574            6 :                ALLOCATE (ortho)
     575              :                owns_ortho = .TRUE.
     576              :             END IF
     577              :          END IF
     578              : 
     579              :          IF (need_wm) THEN
     580           46 :             CALL get_mo_set(mo_set=mo_array(1), mo_coeff=moa)
     581           46 :             CALL cp_fm_get_info(moa, matrix_struct=ao_mo_struct)
     582           46 :             CALL cp_fm_struct_get(ao_mo_struct, nrow_global=nao, nrow_block=nblocks)
     583              :             CALL cp_fm_struct_create(fmstruct=ao_ao_struct, &
     584              :                                      nrow_block=nblocks, &
     585              :                                      ncol_block=nblocks, &
     586              :                                      nrow_global=nao, &
     587              :                                      ncol_global=nao, &
     588           46 :                                      template_fmstruct=ao_mo_struct)
     589           92 :             ALLOCATE (work1(1))
     590           46 :             ALLOCATE (work2, ortho)
     591           46 :             CALL cp_fm_create(work1(1), ao_ao_struct)
     592           46 :             CALL cp_fm_create(work2, ao_ao_struct)
     593           46 :             CALL cp_fm_create(ortho, ao_ao_struct)
     594           46 :             CALL copy_dbcsr_to_fm(matrix_s_kp(1, 1)%matrix, ortho)
     595           46 :             CALL cp_fm_cholesky_decompose(ortho)
     596          138 :             CALL cp_fm_struct_release(ao_ao_struct)
     597              :          END IF
     598              : 
     599          184 :          ispin = 1
     600              :          ! Load core Hamiltonian into work matrix
     601          184 :          CALL copy_dbcsr_to_fm(h_core_sparse(1)%matrix, work1(ispin))
     602              : 
     603              :          ! Diagonalize the core Hamiltonian matrix and retrieve a first set of
     604              :          ! molecular orbitals (MOs)
     605          184 :          IF (has_unit_metric) THEN
     606              :             CALL eigensolver_simple(matrix_ks=work1(ispin), &
     607              :                                     mo_set=mo_array(ispin), &
     608              :                                     work=work2, &
     609              :                                     do_level_shift=.FALSE., &
     610              :                                     level_shift=0.0_dp, &
     611            6 :                                     use_jacobi=.FALSE., jacobi_threshold=0._dp)
     612              :          ELSE
     613              :             CALL eigensolver(matrix_ks_fm=work1(ispin), &
     614              :                              mo_set=mo_array(ispin), &
     615              :                              ortho=ortho, &
     616              :                              work=work2, &
     617              :                              cholesky_method=scf_env%cholesky_method, &
     618              :                              do_level_shift=.FALSE., &
     619              :                              level_shift=0.0_dp, &
     620          178 :                              use_jacobi=.FALSE.)
     621              :          END IF
     622              : 
     623              :          ! Open shell case: copy alpha MOs to beta MOs
     624          184 :          IF (nspin == 2) THEN
     625           24 :             CALL get_mo_set(mo_set=mo_array(1), mo_coeff=moa)
     626           24 :             CALL get_mo_set(mo_set=mo_array(2), mo_coeff=mob, nmo=nmo)
     627           24 :             CALL cp_fm_to_fm(moa, mob, nmo)
     628              :          END IF
     629              : 
     630              :          ! Build an initial density matrix (for each spin in the case of
     631              :          ! an open shell calculation) from the first MOs set
     632          392 :          DO ispin = 1, nspin
     633          208 :             CALL set_mo_occupation(mo_set=mo_array(ispin), smear=scf_control%smear)
     634          392 :             CALL calculate_density_matrix(mo_array(ispin), p_rmpv(ispin)%matrix)
     635              :          END DO
     636              : 
     637              :          ! release intermediate matrices
     638          184 :          IF (need_wm) THEN
     639           46 :             CALL cp_fm_release(ortho)
     640           46 :             CALL cp_fm_release(work2)
     641           46 :             CALL cp_fm_release(work1(1))
     642           46 :             DEALLOCATE (ortho, work2)
     643           46 :             DEALLOCATE (work1)
     644           46 :             NULLIFY (work1, work2, ortho)
     645          138 :          ELSE IF (owns_ortho) THEN
     646            6 :             DEALLOCATE (ortho)
     647              :          END IF
     648              : 
     649              :          did_guess = .TRUE.
     650              :       END IF
     651              : 
     652         9459 :       IF (density_guess == atomic_guess) THEN
     653              : 
     654         5259 :          subsys_section => section_vals_get_subs_vals(input, "SUBSYS")
     655         5259 :          ounit = cp_print_key_unit_nr(logger, subsys_section, "PRINT%KINDS", extension=".Log")
     656         5259 :          IF (ounit > 0) THEN
     657              :             WRITE (UNIT=ounit, FMT="(/,(T2,A))") &
     658         1141 :                "Atomic guess: The first density matrix is obtained in terms of atomic orbitals", &
     659         2282 :                "              and electronic configurations assigned to each atomic kind"
     660              :          END IF
     661              : 
     662              :          CALL calculate_atomic_block_dm(p_rmpv, s_sparse(1)%matrix, atomic_kind_set, qs_kind_set, &
     663         5259 :                                         nspin, nelectron_spin, ounit, para_env)
     664              : 
     665        11583 :          DO ispin = 1, nspin
     666              : 
     667              :             ! The orbital transformation method (OT) requires not only an
     668              :             ! initial density matrix, but also an initial wavefunction (MO set)
     669        11583 :             IF (ofgpw .AND. (scf_control%use_ot .OR. scf_env%method == ot_diag_method_nr)) THEN
     670              :                ! get orbitals later
     671              :             ELSE
     672         6324 :                IF (need_mos) THEN
     673              : 
     674         2418 :                   IF (dft_control%restricted .AND. (ispin == 2)) THEN
     675           22 :                      CALL mo_set_restrict(mo_array)
     676              :                   ELSE
     677              :                      CALL get_mo_set(mo_set=mo_array(ispin), &
     678              :                                      mo_coeff=mo_coeff, &
     679         2396 :                                      nmo=nmo, nao=nao, homo=homo)
     680              : 
     681         2396 :                      CALL cp_fm_set_all(mo_coeff, 0.0_dp)
     682         2396 :                      CALL cp_fm_init_random(mo_coeff, nmo)
     683              : 
     684         2396 :                      CALL cp_fm_create(sv, mo_coeff%matrix_struct, "SV")
     685              :                      ! multiply times PS
     686         2396 :                      IF (has_unit_metric) THEN
     687            0 :                         CALL cp_fm_to_fm(mo_coeff, sv)
     688              :                      ELSE
     689              :                         ! PS*C(:,1:nomo)+C(:,nomo+1:nmo) (nomo=NINT(nelectron/maxocc))
     690         2396 :                         CALL cp_dbcsr_sm_fm_multiply(s_sparse(1)%matrix, mo_coeff, sv, nmo)
     691              :                      END IF
     692         2396 :                      CALL cp_dbcsr_sm_fm_multiply(p_rmpv(ispin)%matrix, sv, mo_coeff, homo)
     693              : 
     694         2396 :                      CALL cp_fm_release(sv)
     695              :                      ! and ortho the result
     696         2396 :                      IF (has_unit_metric) THEN
     697            0 :                         CALL make_basis_simple(mo_coeff, nmo)
     698              :                      ELSE
     699         2396 :                         CALL make_basis_sm(mo_coeff, nmo, s_sparse(1)%matrix)
     700              :                      END IF
     701              :                   END IF
     702              : 
     703              :                   CALL set_mo_occupation(mo_set=mo_array(ispin), &
     704         2418 :                                          smear=qs_env%scf_control%smear)
     705              : 
     706              :                   CALL copy_fm_to_dbcsr(mo_array(ispin)%mo_coeff, &
     707         2418 :                                         mo_array(ispin)%mo_coeff_b) !fm->dbcsr
     708              : 
     709              :                   CALL calculate_density_matrix(mo_array(ispin), &
     710         2418 :                                                 p_rmpv(ispin)%matrix)
     711              :                END IF
     712              :                ! adjust el_density in case surface_dipole_correction is switched
     713              :                ! on and CORE_CORRECTION is non-zero
     714         6324 :                IF (scf_env%method == general_diag_method_nr) THEN
     715         4200 :                   IF (dft_control%correct_surf_dip) THEN
     716            8 :                      IF (ABS(tot_corr_zeff) > 0.0_dp) THEN
     717              :                         CALL get_mo_set(mo_set=mo_array(ispin), &
     718              :                                         mo_coeff=mo_coeff, &
     719            6 :                                         nmo=nmo, nao=nao, homo=homo)
     720              : 
     721            6 :                         CALL cp_fm_set_all(mo_coeff, 0.0_dp)
     722            6 :                         CALL cp_fm_init_random(mo_coeff, nmo)
     723              : 
     724            6 :                         CALL cp_fm_create(sv, mo_coeff%matrix_struct, "SV")
     725              :                         ! multiply times PS
     726            6 :                         IF (has_unit_metric) THEN
     727            0 :                            CALL cp_fm_to_fm(mo_coeff, sv)
     728              :                         ELSE
     729              :                            ! PS*C(:,1:nomo)+C(:,nomo+1:nmo) (nomo=NINT(nelectron/maxocc))
     730            6 :                            CALL cp_dbcsr_sm_fm_multiply(s_sparse(1)%matrix, mo_coeff, sv, nmo)
     731              :                         END IF
     732            6 :                         CALL cp_dbcsr_sm_fm_multiply(p_rmpv(ispin)%matrix, sv, mo_coeff, homo)
     733              : 
     734            6 :                         CALL cp_fm_release(sv)
     735              :                         ! and ortho the result
     736            6 :                         IF (has_unit_metric) THEN
     737            0 :                            CALL make_basis_simple(mo_coeff, nmo)
     738              :                         ELSE
     739            6 :                            CALL make_basis_sm(mo_coeff, nmo, s_sparse(1)%matrix)
     740              :                         END IF
     741              : 
     742              :                         CALL set_mo_occupation(mo_set=mo_array(ispin), smear=qs_env%scf_control%smear, &
     743            6 :                                                tot_zeff_corr=tot_corr_zeff)
     744              : 
     745              :                         CALL calculate_density_matrix(mo_array(ispin), &
     746            6 :                                                       p_rmpv(ispin)%matrix)
     747              :                      END IF
     748              :                   END IF
     749              :                END IF
     750              : 
     751              :             END IF
     752              : 
     753              :          END DO
     754              : 
     755         5259 :          IF (ofgpw .AND. (scf_control%use_ot .OR. scf_env%method == ot_diag_method_nr)) THEN
     756              :             ! We fit a function to the square root of the density
     757            0 :             CALL qs_rho_update_rho(rho, qs_env)
     758         5259 :             CPASSERT(1 == 0)
     759              : !         CALL cp_fm_create(sv,mo_coeff%matrix_struct,"SV")
     760              : !         DO ispin=1,nspin
     761              : !           CALL integrate_ppl_rspace(qs%rho%rho_r(ispin),qs_env)
     762              : !           CALL cp_cfm_solve(overlap,mos)
     763              : !           CALL get_mo_set(mo_set=mo_array(ispin),&
     764              : !                           mo_coeff=mo_coeff, nmo=nmo, nao=nao)
     765              : !           CALL cp_fm_init_random(mo_coeff,nmo)
     766              : !         END DO
     767              : !         CALL cp_fm_release(sv)
     768              :          END IF
     769              : 
     770         5259 :          IF (scf_control%diagonalization%mom) THEN
     771            4 :             CALL do_mom_guess(nspin, mo_array, scf_control, p_rmpv)
     772              :          END IF
     773              : 
     774              :          CALL cp_print_key_finished_output(ounit, logger, subsys_section, &
     775         5259 :                                            "PRINT%KINDS")
     776              : 
     777         5259 :          did_guess = .TRUE.
     778              :       END IF
     779              : 
     780         9459 :       IF (density_guess == sparse_guess) THEN
     781              : 
     782            0 :          IF (ofgpw) THEN
     783            0 :             CPABORT("calculate_first_density_matrix: sparse_guess not implemented for OFGPW")
     784              :          END IF
     785            0 :          IF (.NOT. scf_control%use_ot) THEN
     786            0 :             CPABORT("calculate_first_density_matrix: sparse_guess implemented for OT only")
     787              :          END IF
     788            0 :          IF (do_kpoints) THEN
     789            0 :             CPABORT("calculate_first_density_matrix: sparse_guess not implemented for k-points")
     790              :          END IF
     791              : 
     792            0 :          eps = 1.0E-5_dp
     793              : 
     794            0 :          ounit = cp_logger_get_default_io_unit(logger)
     795            0 :          natoms = SIZE(particle_set)
     796            0 :          ALLOCATE (kind_of(natoms))
     797            0 :          ALLOCATE (first_sgf(natoms), last_sgf(natoms))
     798              : 
     799            0 :          checksum = dbcsr_checksum(s_sparse(1)%matrix)
     800            0 :          i = dbcsr_get_num_blocks(s_sparse(1)%matrix); CALL para_env%sum(i)
     801            0 :          IF (ounit > 0) WRITE (ounit, *) 'S nblks', i, ' checksum', checksum
     802            0 :          CALL dbcsr_filter(s_sparse(1)%matrix, eps)
     803            0 :          checksum = dbcsr_checksum(s_sparse(1)%matrix)
     804            0 :          i = dbcsr_get_num_blocks(s_sparse(1)%matrix); CALL para_env%sum(i)
     805            0 :          IF (ounit > 0) WRITE (ounit, *) 'S nblks', i, ' checksum', checksum
     806              : 
     807              :          CALL get_particle_set(particle_set, qs_kind_set, first_sgf=first_sgf, &
     808            0 :                                last_sgf=last_sgf)
     809            0 :          CALL get_atomic_kind_set(atomic_kind_set=atomic_kind_set, kind_of=kind_of)
     810              : 
     811            0 :          ALLOCATE (pmat(SIZE(atomic_kind_set)))
     812              : 
     813            0 :          rscale = 1._dp
     814            0 :          IF (nspin == 2) rscale = 0.5_dp
     815            0 :          DO ikind = 1, SIZE(atomic_kind_set)
     816            0 :             atomic_kind => atomic_kind_set(ikind)
     817            0 :             qs_kind => qs_kind_set(ikind)
     818            0 :             NULLIFY (pmat(ikind)%mat)
     819            0 :             CALL calculate_atomic_orbitals(atomic_kind, qs_kind, pmat=pmat(ikind)%mat)
     820            0 :             NULLIFY (atomic_kind)
     821              :          END DO
     822              : 
     823            0 :          DO ispin = 1, nspin
     824              :             CALL get_mo_set(mo_set=mo_array(ispin), &
     825              :                             maxocc=maxocc, &
     826            0 :                             nelectron=nelectron)
     827              :             !
     828            0 :             CALL dbcsr_iterator_start(iter, p_rmpv(ispin)%matrix)
     829            0 :             DO WHILE (dbcsr_iterator_blocks_left(iter))
     830            0 :                CALL dbcsr_iterator_next_block(iter, irow, icol, pdata)
     831            0 :                ikind = kind_of(irow)
     832            0 :                IF (icol == irow) THEN
     833            0 :                   IF (ispin == 1) THEN
     834              :                      pdata(:, :) = pmat(ikind)%mat(:, :, 1)*rscale + &
     835            0 :                                    pmat(ikind)%mat(:, :, 2)*rscale
     836              :                   ELSE
     837              :                      pdata(:, :) = pmat(ikind)%mat(:, :, 1)*rscale - &
     838            0 :                                    pmat(ikind)%mat(:, :, 2)*rscale
     839              :                   END IF
     840              :                END IF
     841              :             END DO
     842            0 :             CALL dbcsr_iterator_stop(iter)
     843              : 
     844              :             !CALL dbcsr_verify_matrix(p_rmpv(ispin)%matrix)
     845            0 :             checksum = dbcsr_checksum(p_rmpv(ispin)%matrix)
     846            0 :             occ = dbcsr_get_occupation(p_rmpv(ispin)%matrix)
     847            0 :             IF (ounit > 0) WRITE (ounit, *) 'P_init occ', occ, ' checksum', checksum
     848              :             ! so far p needs to have the same sparsity as S
     849              :             !CALL dbcsr_filter(p_rmpv(ispin)%matrix, eps)
     850              :             !CALL dbcsr_verify_matrix(p_rmpv(ispin)%matrix)
     851            0 :             checksum = dbcsr_checksum(p_rmpv(ispin)%matrix)
     852            0 :             occ = dbcsr_get_occupation(p_rmpv(ispin)%matrix)
     853            0 :             IF (ounit > 0) WRITE (ounit, *) 'P_init occ', occ, ' checksum', checksum
     854              : 
     855            0 :             CALL dbcsr_dot(p_rmpv(ispin)%matrix, s_sparse(1)%matrix, trps1)
     856            0 :             rscale = REAL(nelectron, dp)/trps1
     857            0 :             CALL dbcsr_scale(p_rmpv(ispin)%matrix, rscale)
     858              : 
     859              :             !CALL dbcsr_verify_matrix(p_rmpv(ispin)%matrix)
     860            0 :             checksum = dbcsr_checksum(p_rmpv(ispin)%matrix)
     861            0 :             occ = dbcsr_get_occupation(p_rmpv(ispin)%matrix)
     862            0 :             IF (ounit > 0) WRITE (ounit, *) 'P occ', occ, ' checksum', checksum
     863              :             !
     864              :             ! The orbital transformation method (OT) requires not only an
     865              :             ! initial density matrix, but also an initial wavefunction (MO set)
     866            0 :             IF (dft_control%restricted .AND. (ispin == 2)) THEN
     867            0 :                CALL mo_set_restrict(mo_array)
     868              :             ELSE
     869              :                CALL get_mo_set(mo_set=mo_array(ispin), &
     870              :                                mo_coeff=mo_coeff, &
     871            0 :                                nmo=nmo, nao=nao, homo=homo)
     872            0 :                CALL cp_fm_set_all(mo_coeff, 0.0_dp)
     873              : 
     874            0 :                n = MAXVAL(last_sgf - first_sgf) + 1
     875            0 :                size_atomic_kind_set = SIZE(atomic_kind_set)
     876              : 
     877            0 :                ALLOCATE (buff(n, n), sort_kind(size_atomic_kind_set), &
     878            0 :                          nelec_kind(size_atomic_kind_set))
     879              :                !
     880              :                ! sort kind vs nbr electron
     881            0 :                DO ikind = 1, size_atomic_kind_set
     882            0 :                   atomic_kind => atomic_kind_set(ikind)
     883            0 :                   qs_kind => qs_kind_set(ikind)
     884              :                   CALL get_atomic_kind(atomic_kind=atomic_kind, &
     885              :                                        natom=natom, &
     886              :                                        atom_list=atom_list, &
     887            0 :                                        z=z)
     888              :                   CALL get_qs_kind(qs_kind, nsgf=nsgf, elec_conf=elec_conf, &
     889            0 :                                    basis_set=orb_basis_set, zeff=zeff)
     890            0 :                   nelec_kind(ikind) = SUM(elec_conf)
     891              :                END DO
     892            0 :                CALL sort(nelec_kind, size_atomic_kind_set, sort_kind)
     893              :                !
     894              :                ! a -very- naive sparse guess
     895            0 :                nmo_tmp = nmo
     896            0 :                natoms_tmp = natoms
     897            0 :                istart_col = 1
     898            0 :                iseed(1) = 4; iseed(2) = 3; iseed(3) = 2; iseed(4) = 1 ! set the seed for dlarnv
     899            0 :                DO i = 1, size_atomic_kind_set
     900            0 :                   ikind = sort_kind(i)
     901            0 :                   atomic_kind => atomic_kind_set(ikind)
     902              :                   CALL get_atomic_kind(atomic_kind=atomic_kind, &
     903            0 :                                        natom=natom, atom_list=atom_list)
     904            0 :                   DO iatom = 1, natom
     905              :                      !
     906            0 :                      atom_a = atom_list(iatom)
     907            0 :                      istart_row = first_sgf(atom_a)
     908            0 :                      n_rows = last_sgf(atom_a) - first_sgf(atom_a) + 1
     909              :                      !
     910              :                      ! compute the "potential" nbr of states for this atom
     911            0 :                      n_cols = MAX(INT(REAL(nmo_tmp, dp)/REAL(natoms_tmp, dp)), 1)
     912            0 :                      IF (n_cols > n_rows) n_cols = n_rows
     913              :                      !
     914            0 :                      nmo_tmp = nmo_tmp - n_cols
     915            0 :                      natoms_tmp = natoms_tmp - 1
     916            0 :                      CPASSERT(nmo_tmp >= 0)
     917            0 :                      CPASSERT(natoms_tmp >= 0)
     918            0 :                      DO j = 1, n_cols
     919            0 :                         CALL dlarnv(1, iseed, n_rows, buff(1, j))
     920              :                      END DO
     921              :                      CALL cp_fm_set_submatrix(mo_coeff, buff, istart_row, istart_col, &
     922            0 :                                               n_rows, n_cols)
     923            0 :                      istart_col = istart_col + n_cols
     924              :                   END DO
     925              :                END DO
     926              : 
     927            0 :                CPASSERT(istart_col > nmo)
     928              : 
     929            0 :                DEALLOCATE (buff, nelec_kind, sort_kind)
     930              : 
     931              :                IF (.FALSE.) THEN
     932              :                   ALLOCATE (buff(nao, 1), buff2(nao, 1))
     933              :                   DO i = 1, nmo
     934              :                      CALL cp_fm_get_submatrix(mo_coeff, buff, 1, i, nao, 1)
     935              :                      IF (SUM(buff**2) < 1E-10_dp) THEN
     936              :                         IF (ounit > 0) THEN
     937              :                            WRITE (ounit, *) 'wrong', i, SUM(buff**2)
     938              :                         END IF
     939              :                      END IF
     940              :                      length = SQRT(DOT_PRODUCT(buff(:, 1), buff(:, 1)))
     941              :                      buff(:, :) = buff(:, :)/length
     942              :                      DO j = i + 1, nmo
     943              :                         CALL cp_fm_get_submatrix(mo_coeff, buff2, 1, j, nao, 1)
     944              :                         length = SQRT(DOT_PRODUCT(buff2(:, 1), buff2(:, 1)))
     945              :                         buff2(:, :) = buff2(:, :)/length
     946              :                         IF (ABS(DOT_PRODUCT(buff(:, 1), buff2(:, 1)) - 1.0_dp) < 1E-10_dp) THEN
     947              :                            IF (ounit > 0) THEN
     948              :                               WRITE (ounit, *) 'wrong2', i, j, DOT_PRODUCT(buff(:, 1), buff2(:, 1))
     949              :                               DO ikind = 1, nao
     950              :                                  IF (ABS(mo_coeff%local_data(ikind, i)) > 1e-10_dp) THEN
     951              :                                     WRITE (ounit, *) 'c1', ikind, mo_coeff%local_data(ikind, i)
     952              :                                  END IF
     953              :                                  IF (ABS(mo_coeff%local_data(ikind, j)) > 1e-10_dp) THEN
     954              :                                     WRITE (ounit, *) 'c2', ikind, mo_coeff%local_data(ikind, j)
     955              :                                  END IF
     956              :                               END DO
     957              :                            END IF
     958              :                            CPABORT("Something went wrong with sparse_guess!")
     959              :                         END IF
     960              :                      END DO
     961              :                   END DO
     962              :                   DEALLOCATE (buff, buff2)
     963              : 
     964              :                END IF
     965              :                !
     966            0 :                CALL cp_fm_to_dbcsr_row_template(mo_dbcsr, mo_coeff, s_sparse(1)%matrix)
     967              :                !CALL dbcsr_verify_matrix(mo_dbcsr)
     968            0 :                checksum = dbcsr_checksum(mo_dbcsr)
     969              : 
     970            0 :                occ = dbcsr_get_occupation(mo_dbcsr)
     971            0 :                IF (ounit > 0) WRITE (ounit, *) 'C occ', occ, ' checksum', checksum
     972            0 :                CALL dbcsr_filter(mo_dbcsr, eps)
     973              :                !CALL dbcsr_verify_matrix(mo_dbcsr)
     974            0 :                occ = dbcsr_get_occupation(mo_dbcsr)
     975            0 :                checksum = dbcsr_checksum(mo_dbcsr)
     976            0 :                IF (ounit > 0) WRITE (ounit, *) 'C occ', occ, ' checksum', checksum
     977              :                !
     978              :                ! multiply times PS
     979            0 :                IF (has_unit_metric) THEN
     980            0 :                   CPABORT("has_unit_metric will be removed soon")
     981              :                END IF
     982              :                !
     983              :                ! S*C
     984            0 :                CALL dbcsr_copy(mo_tmp_dbcsr, mo_dbcsr, name="mo_tmp")
     985              :                CALL dbcsr_multiply("N", "N", 1.0_dp, s_sparse(1)%matrix, mo_dbcsr, &
     986              :                                    0.0_dp, mo_tmp_dbcsr, &
     987            0 :                                    retain_sparsity=.TRUE.)
     988              :                !CALL dbcsr_verify_matrix(mo_tmp_dbcsr)
     989            0 :                checksum = dbcsr_checksum(mo_tmp_dbcsr)
     990            0 :                occ = dbcsr_get_occupation(mo_tmp_dbcsr)
     991            0 :                IF (ounit > 0) WRITE (ounit, *) 'S*C occ', occ, ' checksum', checksum
     992            0 :                CALL dbcsr_filter(mo_tmp_dbcsr, eps)
     993              :                !CALL dbcsr_verify_matrix(mo_tmp_dbcsr)
     994            0 :                checksum = dbcsr_checksum(mo_tmp_dbcsr)
     995            0 :                occ = dbcsr_get_occupation(mo_tmp_dbcsr)
     996            0 :                IF (ounit > 0) WRITE (ounit, *) 'S*C occ', occ, ' checksum', checksum
     997              :                !
     998              :                ! P*SC
     999              :                ! the destroy is needed for the moment to avoid memory leaks !
    1000              :                ! This one is not needed because _destroy takes care of zeroing.
    1001              :                CALL dbcsr_multiply("N", "N", 1.0_dp, p_rmpv(ispin)%matrix, &
    1002            0 :                                    mo_tmp_dbcsr, 0.0_dp, mo_dbcsr)
    1003              :                IF (.FALSE.) CALL dbcsr_verify_matrix(mo_dbcsr)
    1004            0 :                checksum = dbcsr_checksum(mo_dbcsr)
    1005            0 :                occ = dbcsr_get_occupation(mo_dbcsr)
    1006            0 :                IF (ounit > 0) WRITE (ounit, *) 'P*SC occ', occ, ' checksum', checksum
    1007            0 :                CALL dbcsr_filter(mo_dbcsr, eps)
    1008              :                !CALL dbcsr_verify_matrix(mo_dbcsr)
    1009            0 :                checksum = dbcsr_checksum(mo_dbcsr)
    1010            0 :                occ = dbcsr_get_occupation(mo_dbcsr)
    1011            0 :                IF (ounit > 0) WRITE (ounit, *) 'P*SC occ', occ, ' checksum', checksum
    1012              :                !
    1013            0 :                CALL copy_dbcsr_to_fm(mo_dbcsr, mo_coeff)
    1014              : 
    1015            0 :                CALL dbcsr_release(mo_dbcsr)
    1016            0 :                CALL dbcsr_release(mo_tmp_dbcsr)
    1017              : 
    1018              :                ! and ortho the result
    1019            0 :                CALL make_basis_sm(mo_coeff, nmo, s_sparse(1)%matrix)
    1020              :             END IF
    1021              : 
    1022              :             CALL set_mo_occupation(mo_set=mo_array(ispin), &
    1023            0 :                                    smear=qs_env%scf_control%smear)
    1024              : 
    1025              :             CALL copy_fm_to_dbcsr(mo_array(ispin)%mo_coeff, &
    1026            0 :                                   mo_array(ispin)%mo_coeff_b) !fm->dbcsr
    1027              : 
    1028              :             CALL calculate_density_matrix(mo_array(ispin), &
    1029            0 :                                           p_rmpv(ispin)%matrix)
    1030            0 :             DO ikind = 1, SIZE(atomic_kind_set)
    1031            0 :                IF (ASSOCIATED(pmat(ikind)%mat)) THEN
    1032            0 :                   DEALLOCATE (pmat(ikind)%mat)
    1033              :                END IF
    1034              :             END DO
    1035              :          END DO
    1036              : 
    1037            0 :          DEALLOCATE (pmat)
    1038              : 
    1039            0 :          DEALLOCATE (kind_of)
    1040              : 
    1041            0 :          DEALLOCATE (first_sgf, last_sgf)
    1042              : 
    1043            0 :          did_guess = .TRUE.
    1044              :       END IF
    1045         9459 :       IF (density_guess == mopac_guess) THEN
    1046              : 
    1047              :          CALL calculate_mopac_dm(p_rmpv, s_sparse(1)%matrix, has_unit_metric, dft_control, &
    1048              :                                  particle_set, atomic_kind_set, qs_kind_set, &
    1049         2572 :                                  nspin, nelectron_spin, para_env)
    1050              : 
    1051         5244 :          DO ispin = 1, nspin
    1052              :             ! The orbital transformation method (OT) requires not only an
    1053              :             ! initial density matrix, but also an initial wavefunction (MO set)
    1054         5244 :             IF (need_mos) THEN
    1055          228 :                IF (dft_control%restricted .AND. (ispin == 2)) THEN
    1056            2 :                   CALL mo_set_restrict(mo_array)
    1057              :                ELSE
    1058              :                   CALL get_mo_set(mo_set=mo_array(ispin), &
    1059              :                                   mo_coeff=mo_coeff, &
    1060          226 :                                   nmo=nmo, homo=homo)
    1061          226 :                   CALL cp_fm_init_random(mo_coeff, nmo)
    1062          226 :                   CALL cp_fm_create(sv, mo_coeff%matrix_struct, "SV")
    1063              :                   ! multiply times PS
    1064          226 :                   IF (has_unit_metric) THEN
    1065          180 :                      CALL cp_fm_to_fm(mo_coeff, sv)
    1066              :                   ELSE
    1067           46 :                      CALL cp_dbcsr_sm_fm_multiply(s_sparse(1)%matrix, mo_coeff, sv, nmo)
    1068              :                   END IF
    1069              :                   ! here we could easily multiply with the diag that we actually have replicated already
    1070          226 :                   CALL cp_dbcsr_sm_fm_multiply(p_rmpv(ispin)%matrix, sv, mo_coeff, homo)
    1071          226 :                   CALL cp_fm_release(sv)
    1072              :                   ! and ortho the result
    1073          226 :                   IF (has_unit_metric) THEN
    1074          180 :                      CALL make_basis_simple(mo_coeff, nmo)
    1075              :                   ELSE
    1076           46 :                      CALL make_basis_sm(mo_coeff, nmo, s_sparse(1)%matrix)
    1077              :                   END IF
    1078              :                END IF
    1079              : 
    1080              :                CALL set_mo_occupation(mo_set=mo_array(ispin), &
    1081          228 :                                       smear=qs_env%scf_control%smear)
    1082              :                CALL copy_fm_to_dbcsr(mo_array(ispin)%mo_coeff, &
    1083          228 :                                      mo_array(ispin)%mo_coeff_b)
    1084              : 
    1085              :                CALL calculate_density_matrix(mo_array(ispin), &
    1086          228 :                                              p_rmpv(ispin)%matrix)
    1087              :             END IF
    1088              :          END DO
    1089              : 
    1090              :          did_guess = .TRUE.
    1091              :       END IF
    1092              :       !
    1093              :       ! EHT guess (gfn0-xTB)
    1094         9459 :       IF (density_guess == eht_guess) THEN
    1095            4 :          CALL calculate_eht_guess(qs_env, mo_array)
    1096            8 :          DO ispin = 1, nspin
    1097            8 :             CALL calculate_density_matrix(mo_array(ispin), p_rmpv(ispin)%matrix)
    1098              :          END DO
    1099              :          did_guess = .TRUE.
    1100              :       END IF
    1101              :       ! switch_surf_dip [SGh]
    1102         9459 :       IF (dft_control%switch_surf_dip) THEN
    1103            4 :          DO ispin = 1, nspin
    1104              :             CALL reassign_allocated_mos(mos_last_converged(ispin), &
    1105            4 :                                         mo_array(ispin))
    1106              :          END DO
    1107              :       END IF
    1108              : 
    1109         9459 :       IF (density_guess == no_guess) THEN
    1110              :          did_guess = .TRUE.
    1111              :       END IF
    1112              : 
    1113         8535 :       IF (.NOT. did_guess) THEN
    1114            0 :          CPABORT("An invalid keyword for the initial density guess was specified")
    1115              :       END IF
    1116              : 
    1117         9459 :       CALL timestop(handle)
    1118              : 
    1119        18918 :    END SUBROUTINE calculate_first_density_matrix
    1120              : 
    1121              : ! **************************************************************************************************
    1122              : !> \brief returns a block diagonal fock matrix.
    1123              : !> \param matrix_f ...
    1124              : !> \param atomic_kind_set ...
    1125              : !> \param qs_kind_set ...
    1126              : !> \param ounit ...
    1127              : ! **************************************************************************************************
    1128           98 :    SUBROUTINE calculate_atomic_fock_matrix(matrix_f, atomic_kind_set, qs_kind_set, ounit)
    1129              :       TYPE(dbcsr_type), INTENT(INOUT)                    :: matrix_f
    1130              :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
    1131              :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
    1132              :       INTEGER, INTENT(IN)                                :: ounit
    1133              : 
    1134              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'calculate_atomic_fock_matrix'
    1135              : 
    1136              :       INTEGER                                            :: handle, icol, ikind, irow
    1137           98 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: kind_of
    1138           98 :       REAL(dp), DIMENSION(:, :), POINTER                 :: block
    1139           98 :       TYPE(atom_matrix_type), ALLOCATABLE, DIMENSION(:)  :: fmat
    1140              :       TYPE(atomic_kind_type), POINTER                    :: atomic_kind
    1141              :       TYPE(dbcsr_iterator_type)                          :: iter
    1142              :       TYPE(qs_kind_type), POINTER                        :: qs_kind
    1143              : 
    1144           98 :       CALL timeset(routineN, handle)
    1145              : 
    1146           98 :       CALL get_atomic_kind_set(atomic_kind_set=atomic_kind_set, kind_of=kind_of)
    1147          432 :       ALLOCATE (fmat(SIZE(atomic_kind_set)))
    1148              : 
    1149              :       ! precompute the atomic blocks for each atomic-kind
    1150          236 :       DO ikind = 1, SIZE(atomic_kind_set)
    1151          138 :          atomic_kind => atomic_kind_set(ikind)
    1152          138 :          qs_kind => qs_kind_set(ikind)
    1153          138 :          NULLIFY (fmat(ikind)%mat)
    1154          138 :          IF (ounit > 0) WRITE (UNIT=ounit, FMT="(/,T2,A)") &
    1155           69 :             "Calculating atomic Fock matrix for atomic kind: "//TRIM(atomic_kind%name)
    1156              : 
    1157              :          !Currently only ispin=1 is supported
    1158              :          CALL calculate_atomic_orbitals(atomic_kind, qs_kind, iunit=ounit, &
    1159          236 :                                         fmat=fmat(ikind)%mat)
    1160              :       END DO
    1161              : 
    1162              :       ! zero result matrix
    1163           98 :       CALL dbcsr_set(matrix_f, 0.0_dp)
    1164              : 
    1165              :       ! copy precomputed blocks onto diagonal of result matrix
    1166           98 :       CALL dbcsr_iterator_start(iter, matrix_f)
    1167          217 :       DO WHILE (dbcsr_iterator_blocks_left(iter))
    1168          119 :          CALL dbcsr_iterator_next_block(iter, irow, icol, block)
    1169          119 :          ikind = kind_of(irow)
    1170         6937 :          IF (icol == irow) block(:, :) = fmat(ikind)%mat(:, :, 1)
    1171              :       END DO
    1172           98 :       CALL dbcsr_iterator_stop(iter)
    1173              : 
    1174              :       ! cleanup
    1175          236 :       DO ikind = 1, SIZE(atomic_kind_set)
    1176          236 :          DEALLOCATE (fmat(ikind)%mat)
    1177              :       END DO
    1178           98 :       DEALLOCATE (fmat)
    1179              : 
    1180           98 :       CALL timestop(handle)
    1181              : 
    1182          294 :    END SUBROUTINE calculate_atomic_fock_matrix
    1183              : 
    1184              : ! **************************************************************************************************
    1185              : !> \brief returns a block diagonal density matrix. Blocks correspond to the mopac initial guess.
    1186              : !> \param pmat ...
    1187              : !> \param matrix_s ...
    1188              : !> \param has_unit_metric ...
    1189              : !> \param dft_control ...
    1190              : !> \param particle_set ...
    1191              : !> \param atomic_kind_set ...
    1192              : !> \param qs_kind_set ...
    1193              : !> \param nspin ...
    1194              : !> \param nelectron_spin ...
    1195              : !> \param para_env ...
    1196              : ! **************************************************************************************************
    1197         2666 :    SUBROUTINE calculate_mopac_dm(pmat, matrix_s, has_unit_metric, &
    1198              :                                  dft_control, particle_set, atomic_kind_set, qs_kind_set, &
    1199         2666 :                                  nspin, nelectron_spin, para_env)
    1200              :       TYPE(dbcsr_p_type), DIMENSION(:), INTENT(INOUT)    :: pmat
    1201              :       TYPE(dbcsr_type), INTENT(INOUT)                    :: matrix_s
    1202              :       LOGICAL                                            :: has_unit_metric
    1203              :       TYPE(dft_control_type), POINTER                    :: dft_control
    1204              :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
    1205              :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
    1206              :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
    1207              :       INTEGER, INTENT(IN)                                :: nspin
    1208              :       INTEGER, DIMENSION(:), INTENT(IN)                  :: nelectron_spin
    1209              :       TYPE(mp_para_env_type)                             :: para_env
    1210              : 
    1211              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'calculate_mopac_dm'
    1212              : 
    1213              :       INTEGER                                            :: atom_a, handle, iatom, ikind, iset, &
    1214              :                                                             isgf, isgfa, ishell, ispin, la, maxl, &
    1215              :                                                             maxll, na, nao, natom, ncount, nset, &
    1216              :                                                             nsgf, z
    1217              :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: first_sgf
    1218              :       INTEGER, DIMENSION(25)                             :: laox, naox
    1219              :       INTEGER, DIMENSION(5)                              :: occupation
    1220         2666 :       INTEGER, DIMENSION(:), POINTER                     :: atom_list, elec_conf, nshell
    1221         2666 :       INTEGER, DIMENSION(:, :), POINTER                  :: first_sgfa, l, last_sgfa
    1222              :       LOGICAL                                            :: has_pot
    1223              :       REAL(KIND=dp)                                      :: maxocc, my_sum, nelec, occ, paa, rscale, &
    1224              :                                                             trps1, trps2, yy, zeff
    1225         2666 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: econf, pdiag, sdiag
    1226              :       REAL(KIND=dp), DIMENSION(0:3)                      :: edftb
    1227              :       TYPE(all_potential_type), POINTER                  :: all_potential
    1228              :       TYPE(cneo_potential_type), POINTER                 :: cneo_potential
    1229              :       TYPE(dbcsr_type), POINTER                          :: matrix_p
    1230              :       TYPE(gth_potential_type), POINTER                  :: gth_potential
    1231              :       TYPE(gto_basis_set_type), POINTER                  :: orb_basis_set
    1232              :       TYPE(sgp_potential_type), POINTER                  :: sgp_potential
    1233              :       TYPE(xtb_atom_type), POINTER                       :: xtb_kind
    1234              : 
    1235         2666 :       CALL timeset(routineN, handle)
    1236              : 
    1237         5440 :       DO ispin = 1, nspin
    1238         2774 :          matrix_p => pmat(ispin)%matrix
    1239         5440 :          CALL dbcsr_set(matrix_p, 0.0_dp)
    1240              :       END DO
    1241              : 
    1242         2666 :       natom = SIZE(particle_set)
    1243         2666 :       CALL dbcsr_get_info(pmat(1)%matrix, nfullrows_total=nao)
    1244         2666 :       IF (nspin == 1) THEN
    1245              :          maxocc = 2.0_dp
    1246              :       ELSE
    1247          108 :          maxocc = 1.0_dp
    1248              :       END IF
    1249              : 
    1250         7998 :       ALLOCATE (first_sgf(natom))
    1251              : 
    1252         2666 :       CALL get_particle_set(particle_set, qs_kind_set, first_sgf=first_sgf)
    1253         2666 :       CALL get_qs_kind_set(qs_kind_set, maxlgto=maxl)
    1254              : 
    1255         7998 :       ALLOCATE (econf(0:maxl))
    1256              : 
    1257         7998 :       ALLOCATE (pdiag(nao))
    1258         2666 :       pdiag(:) = 0.0_dp
    1259              : 
    1260         5332 :       ALLOCATE (sdiag(nao))
    1261              : 
    1262         2666 :       sdiag(:) = 0.0_dp
    1263         2666 :       IF (has_unit_metric) THEN
    1264        12634 :          sdiag(:) = 1.0_dp
    1265              :       ELSE
    1266         2302 :          CALL dbcsr_get_diag(matrix_s, sdiag)
    1267         2302 :          CALL para_env%sum(sdiag)
    1268              :       END IF
    1269              : 
    1270         2666 :       ncount = 0
    1271         2666 :       trps1 = 0.0_dp
    1272         2666 :       trps2 = 0.0_dp
    1273         2666 :       pdiag(:) = 0.0_dp
    1274              : 
    1275         7998 :       IF (SUM(nelectron_spin) /= 0) THEN
    1276         7142 :          DO ikind = 1, SIZE(atomic_kind_set)
    1277              : 
    1278         4490 :             CALL get_atomic_kind(atomic_kind_set(ikind), natom=natom, atom_list=atom_list)
    1279              :             CALL get_qs_kind(qs_kind_set(ikind), basis_set=orb_basis_set, &
    1280              :                              all_potential=all_potential, &
    1281              :                              gth_potential=gth_potential, &
    1282              :                              sgp_potential=sgp_potential, &
    1283         4490 :                              cneo_potential=cneo_potential)
    1284              :             has_pot = ASSOCIATED(all_potential) .OR. ASSOCIATED(gth_potential) .OR. &
    1285         4490 :                       ASSOCIATED(sgp_potential) .OR. ASSOCIATED(cneo_potential)
    1286              : 
    1287         4490 :             IF (dft_control%qs_control%dftb) THEN
    1288              :                CALL get_dftb_atom_param(qs_kind_set(ikind)%dftb_parameter, &
    1289         1312 :                                         lmax=maxll, occupation=edftb)
    1290         1312 :                maxll = MIN(maxll, maxl)
    1291         4020 :                econf(0:maxl) = edftb(0:maxl)
    1292         3178 :             ELSEIF (dft_control%qs_control%xtb) THEN
    1293         2132 :                CALL get_qs_kind(qs_kind_set(ikind), xtb_parameter=xtb_kind)
    1294         2132 :                CALL get_xtb_atom_param(xtb_kind, z=z, natorb=nsgf, nao=naox, lao=laox, occupation=occupation)
    1295         1046 :             ELSEIF (has_pot) THEN
    1296         1046 :                CALL get_atomic_kind(atomic_kind_set(ikind), z=z)
    1297         1046 :                CALL get_qs_kind(qs_kind_set(ikind), nsgf=nsgf, elec_conf=elec_conf, zeff=zeff)
    1298         1046 :                maxll = MIN(SIZE(elec_conf) - 1, maxl)
    1299         1046 :                econf(:) = 0.0_dp
    1300         3288 :                econf(0:maxll) = 0.5_dp*maxocc*REAL(elec_conf(0:maxll), dp)
    1301              :             ELSE
    1302              :                CYCLE
    1303              :             END IF
    1304              : 
    1305              :             ! MOPAC TYPE GUESS
    1306        11632 :             IF (dft_control%qs_control%dftb) THEN
    1307         5660 :                DO iatom = 1, natom
    1308         4348 :                   atom_a = atom_list(iatom)
    1309         4348 :                   isgfa = first_sgf(atom_a)
    1310        12456 :                   DO la = 0, maxll
    1311         4348 :                      SELECT CASE (la)
    1312              :                      CASE (0)
    1313         4348 :                         pdiag(isgfa) = econf(0)
    1314              :                      CASE (1)
    1315         2112 :                         pdiag(isgfa + 1) = econf(1)/3._dp
    1316         2112 :                         pdiag(isgfa + 2) = econf(1)/3._dp
    1317         2112 :                         pdiag(isgfa + 3) = econf(1)/3._dp
    1318              :                      CASE (2)
    1319          336 :                         pdiag(isgfa + 4) = econf(2)/5._dp
    1320          336 :                         pdiag(isgfa + 5) = econf(2)/5._dp
    1321          336 :                         pdiag(isgfa + 6) = econf(2)/5._dp
    1322          336 :                         pdiag(isgfa + 7) = econf(2)/5._dp
    1323          336 :                         pdiag(isgfa + 8) = econf(2)/5._dp
    1324              :                      CASE (3)
    1325            0 :                         pdiag(isgfa + 9) = econf(3)/7._dp
    1326            0 :                         pdiag(isgfa + 10) = econf(3)/7._dp
    1327            0 :                         pdiag(isgfa + 11) = econf(3)/7._dp
    1328            0 :                         pdiag(isgfa + 12) = econf(3)/7._dp
    1329            0 :                         pdiag(isgfa + 13) = econf(3)/7._dp
    1330            0 :                         pdiag(isgfa + 14) = econf(3)/7._dp
    1331            0 :                         pdiag(isgfa + 15) = econf(3)/7._dp
    1332              :                      CASE DEFAULT
    1333         6796 :                         CPABORT("Only 0, 1, 2, 3 are supported as the value of la")
    1334              :                      END SELECT
    1335              :                   END DO
    1336              :                END DO
    1337         3178 :             ELSEIF (dft_control%qs_control%xtb) THEN
    1338        10422 :                DO iatom = 1, natom
    1339         8290 :                   atom_a = atom_list(iatom)
    1340         8290 :                   isgfa = first_sgf(atom_a)
    1341        10422 :                   IF (z == 1 .AND. nsgf == 2) THEN
    1342              :                      ! Hydrogen 2s basis
    1343         1874 :                      pdiag(isgfa) = 1.0_dp
    1344         1874 :                      pdiag(isgfa + 1) = 0.0_dp
    1345              :                   ELSE
    1346        58006 :                      DO isgf = 1, nsgf
    1347        51590 :                         na = naox(isgf)
    1348        51590 :                         la = laox(isgf)
    1349        51590 :                         occ = REAL(occupation(la + 1), dp)/REAL(2*la + 1, dp)
    1350        58006 :                         pdiag(isgfa + isgf - 1) = occ
    1351              :                      END DO
    1352              :                   END IF
    1353              :                END DO
    1354         1046 :             ELSEIF (dft_control%qs_control%semi_empirical) THEN
    1355          966 :                yy = REAL(dft_control%charge, KIND=dp)/REAL(nao, KIND=dp)
    1356         5522 :                DO iatom = 1, natom
    1357         4556 :                   atom_a = atom_list(iatom)
    1358         4556 :                   isgfa = first_sgf(atom_a)
    1359          966 :                   SELECT CASE (nsgf)
    1360              :                   CASE (1) ! s-basis
    1361         2212 :                      pdiag(isgfa) = (zeff - yy)*0.5_dp*maxocc
    1362              :                   CASE (4) ! sp-basis
    1363         2218 :                      IF (z == 1) THEN
    1364              :                         ! special case: hydrogen with sp basis
    1365          136 :                         pdiag(isgfa) = (zeff - yy)*0.5_dp*maxocc
    1366          136 :                         pdiag(isgfa + 1) = 0._dp
    1367          136 :                         pdiag(isgfa + 2) = 0._dp
    1368          136 :                         pdiag(isgfa + 3) = 0._dp
    1369              :                      ELSE
    1370         2082 :                         pdiag(isgfa) = (zeff*0.25_dp - yy)*0.5_dp*maxocc
    1371         2082 :                         pdiag(isgfa + 1) = (zeff*0.25_dp - yy)*0.5_dp*maxocc
    1372         2082 :                         pdiag(isgfa + 2) = (zeff*0.25_dp - yy)*0.5_dp*maxocc
    1373         2082 :                         pdiag(isgfa + 3) = (zeff*0.25_dp - yy)*0.5_dp*maxocc
    1374              :                      END IF
    1375              :                   CASE (9) ! spd-basis
    1376          126 :                      IF (z < 21 .OR. z > 30 .AND. z < 39 .OR. z > 48 .AND. z < 57) THEN
    1377              :                         !   Main Group Element:  The "d" shell is formally empty.
    1378           92 :                         pdiag(isgfa) = (zeff*0.25_dp - yy)*0.5_dp*maxocc
    1379           92 :                         pdiag(isgfa + 1) = (zeff*0.25_dp - yy)*0.5_dp*maxocc
    1380           92 :                         pdiag(isgfa + 2) = (zeff*0.25_dp - yy)*0.5_dp*maxocc
    1381           92 :                         pdiag(isgfa + 3) = (zeff*0.25_dp - yy)*0.5_dp*maxocc
    1382           92 :                         pdiag(isgfa + 4) = (-yy)*0.5_dp*maxocc
    1383           92 :                         pdiag(isgfa + 5) = (-yy)*0.5_dp*maxocc
    1384           92 :                         pdiag(isgfa + 6) = (-yy)*0.5_dp*maxocc
    1385           92 :                         pdiag(isgfa + 7) = (-yy)*0.5_dp*maxocc
    1386           92 :                         pdiag(isgfa + 8) = (-yy)*0.5_dp*maxocc
    1387           34 :                      ELSE IF (z < 99) THEN
    1388           34 :                         my_sum = zeff - 9.0_dp*yy
    1389              :                         !   First, put 2 electrons in the 's' shell
    1390           34 :                         pdiag(isgfa) = (MAX(0.0_dp, MIN(my_sum, 2.0_dp)))*0.5_dp*maxocc
    1391           34 :                         my_sum = my_sum - 2.0_dp
    1392           34 :                         IF (my_sum > 0.0_dp) THEN
    1393              :                            !   Now put as many electrons as possible into the 'd' shell
    1394           30 :                            pdiag(isgfa + 4) = (MAX(0.0_dp, MIN(my_sum*0.2_dp, 2.0_dp)))*0.5_dp*maxocc
    1395           30 :                            pdiag(isgfa + 5) = (MAX(0.0_dp, MIN(my_sum*0.2_dp, 2.0_dp)))*0.5_dp*maxocc
    1396           30 :                            pdiag(isgfa + 6) = (MAX(0.0_dp, MIN(my_sum*0.2_dp, 2.0_dp)))*0.5_dp*maxocc
    1397           30 :                            pdiag(isgfa + 7) = (MAX(0.0_dp, MIN(my_sum*0.2_dp, 2.0_dp)))*0.5_dp*maxocc
    1398           30 :                            pdiag(isgfa + 8) = (MAX(0.0_dp, MIN(my_sum*0.2_dp, 2.0_dp)))*0.5_dp*maxocc
    1399           30 :                            my_sum = MAX(0.0_dp, my_sum - 10.0_dp)
    1400              :                            !   Put the remaining electrons in the 'p' shell
    1401           30 :                            pdiag(isgfa + 1) = (my_sum/3.0_dp)*0.5_dp*maxocc
    1402           30 :                            pdiag(isgfa + 2) = (my_sum/3.0_dp)*0.5_dp*maxocc
    1403           30 :                            pdiag(isgfa + 3) = (my_sum/3.0_dp)*0.5_dp*maxocc
    1404              :                         END IF
    1405              :                      END IF
    1406              :                   CASE DEFAULT
    1407              :                      CALL cp_abort(__LOCATION__, &
    1408              :                                    "Only 1 for s-basis, 4 for sp-basis and 9 for spd-basis "// &
    1409              :                                    "are supported as the value of nsgf in the MOPAC type "// &
    1410         4556 :                                    "guess for semi-empirical methods")
    1411              :                   END SELECT
    1412              :                END DO
    1413              :             ELSE
    1414              :                CALL get_gto_basis_set(gto_basis_set=orb_basis_set, &
    1415              :                                       nset=nset, &
    1416              :                                       nshell=nshell, &
    1417              :                                       l=l, &
    1418              :                                       first_sgf=first_sgfa, &
    1419           80 :                                       last_sgf=last_sgfa)
    1420              : 
    1421          212 :                DO iset = 1, nset
    1422          516 :                   DO ishell = 1, nshell(iset)
    1423          304 :                      la = l(ishell, iset)
    1424          304 :                      nelec = maxocc*REAL(2*la + 1, dp)
    1425          436 :                      IF (econf(la) > 0.0_dp) THEN
    1426          148 :                         IF (econf(la) >= nelec) THEN
    1427           68 :                            paa = maxocc
    1428           68 :                            econf(la) = econf(la) - nelec
    1429              :                         ELSE
    1430           80 :                            paa = maxocc*econf(la)/nelec
    1431           80 :                            econf(la) = 0.0_dp
    1432           80 :                            ncount = ncount + NINT(nelec/maxocc)
    1433              :                         END IF
    1434          432 :                         DO isgfa = first_sgfa(ishell, iset), last_sgfa(ishell, iset)
    1435         2604 :                            DO iatom = 1, natom
    1436         2172 :                               atom_a = atom_list(iatom)
    1437         2172 :                               isgf = first_sgf(atom_a) + isgfa - 1
    1438         2172 :                               pdiag(isgf) = paa
    1439         2456 :                               IF (paa == maxocc) THEN
    1440          538 :                                  trps1 = trps1 + paa*sdiag(isgf)
    1441              :                               ELSE
    1442         1634 :                                  trps2 = trps2 + paa*sdiag(isgf)
    1443              :                               END IF
    1444              :                            END DO
    1445              :                         END DO
    1446              :                      END IF
    1447              :                   END DO ! ishell
    1448              :                END DO ! iset
    1449              :             END IF
    1450              :          END DO ! ikind
    1451              : 
    1452         2652 :          IF (trps2 == 0.0_dp) THEN
    1453        82514 :             DO isgf = 1, nao
    1454        82514 :                IF (sdiag(isgf) > 0.0_dp) pdiag(isgf) = pdiag(isgf)/sdiag(isgf)
    1455              :             END DO
    1456         5290 :             DO ispin = 1, nspin
    1457         5290 :                IF (nelectron_spin(ispin) /= 0) THEN
    1458         2692 :                   matrix_p => pmat(ispin)%matrix
    1459         2692 :                   CALL dbcsr_set_diag(matrix_p, pdiag)
    1460              :                END IF
    1461              :             END DO
    1462              :          ELSE
    1463          120 :             DO ispin = 1, nspin
    1464          120 :                IF (nelectron_spin(ispin) /= 0) THEN
    1465           62 :                   rscale = (REAL(nelectron_spin(ispin), dp) - trps1)/trps2
    1466         5674 :                   DO isgf = 1, nao
    1467         5674 :                      IF (pdiag(isgf) < maxocc) pdiag(isgf) = rscale*pdiag(isgf)
    1468              :                   END DO
    1469           62 :                   matrix_p => pmat(ispin)%matrix
    1470           62 :                   CALL dbcsr_set_diag(matrix_p, pdiag)
    1471         5674 :                   DO isgf = 1, nao
    1472         5674 :                      IF (pdiag(isgf) < maxocc) pdiag(isgf) = pdiag(isgf)/rscale
    1473              :                   END DO
    1474              :                END IF
    1475              :             END DO
    1476              :          END IF
    1477              :       END IF
    1478              : 
    1479         2666 :       DEALLOCATE (econf)
    1480              : 
    1481         2666 :       DEALLOCATE (first_sgf)
    1482              : 
    1483         2666 :       DEALLOCATE (pdiag)
    1484              : 
    1485         2666 :       DEALLOCATE (sdiag)
    1486              : 
    1487         2666 :       CALL timestop(handle)
    1488              : 
    1489         7998 :    END SUBROUTINE calculate_mopac_dm
    1490              : 
    1491            0 : END MODULE qs_initial_guess
        

Generated by: LCOV version 2.0-1