LCOV - code coverage report
Current view: top level - src - pexsi_methods.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:1be8651) Lines: 0.0 % 190 0
Test Date: 2026-11-24 01:22:08 Functions: 0.0 % 6 0

            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 Methods using the PEXSI library to calculate the density matrix and
      10              : !>        related quantities using the Kohn-Sham and overlap matrices from the
      11              : !>        linear scaling quickstep SCF environment.
      12              : !> \par History
      13              : !>       2014.11 created [Patrick Seewald]
      14              : !> \author Patrick Seewald
      15              : ! **************************************************************************************************
      16              : 
      17              : MODULE pexsi_methods
      18              :    USE arnoldi_api,                     ONLY: arnoldi_env_type,&
      19              :                                               arnoldi_ev,&
      20              :                                               deallocate_arnoldi_env,&
      21              :                                               get_selected_ritz_val,&
      22              :                                               get_selected_ritz_vector,&
      23              :                                               set_arnoldi_initial_vector,&
      24              :                                               setup_arnoldi_env
      25              :    USE cp_dbcsr_api,                    ONLY: &
      26              :         dbcsr_copy, dbcsr_create, dbcsr_desymmetrize, dbcsr_distribution_get, &
      27              :         dbcsr_distribution_type, dbcsr_get_info, dbcsr_has_symmetry, dbcsr_p_type, dbcsr_release, &
      28              :         dbcsr_scale, dbcsr_set, dbcsr_type, dbcsr_type_no_symmetry
      29              :    USE cp_dbcsr_cp2k_link,              ONLY: cp_dbcsr_to_csr_screening
      30              :    USE cp_dbcsr_csr,                    ONLY: dbcsr_convert_csr_to_dbcsr,&
      31              :                                               dbcsr_convert_dbcsr_to_csr,&
      32              :                                               dbcsr_csr_create,&
      33              :                                               dbcsr_csr_create_from_dbcsr,&
      34              :                                               dbcsr_csr_destroy,&
      35              :                                               dbcsr_csr_eqrow_floor_dist,&
      36              :                                               dbcsr_csr_print_sparsity,&
      37              :                                               dbcsr_csr_type_real_8
      38              :    USE cp_dbcsr_operations,             ONLY: dbcsr_allocate_matrix_set
      39              :    USE cp_log_handling,                 ONLY: cp_get_default_logger,&
      40              :                                               cp_logger_get_default_unit_nr,&
      41              :                                               cp_logger_type
      42              :    USE dm_ls_scf_qs,                    ONLY: matrix_ls_to_qs
      43              :    USE dm_ls_scf_types,                 ONLY: ls_scf_env_type
      44              :    USE input_section_types,             ONLY: section_vals_type,&
      45              :                                               section_vals_val_get
      46              :    USE kinds,                           ONLY: dp,&
      47              :                                               int_4,&
      48              :                                               int_8
      49              :    USE pexsi_interface,                 ONLY: cp_pexsi_dft_driver,&
      50              :                                               cp_pexsi_get_options,&
      51              :                                               cp_pexsi_load_real_hs_matrix,&
      52              :                                               cp_pexsi_retrieve_real_dft_matrix,&
      53              :                                               cp_pexsi_set_default_options,&
      54              :                                               cp_pexsi_set_options
      55              :    USE pexsi_types,                     ONLY: convert_nspin_cp2k_pexsi,&
      56              :                                               cp2k_to_pexsi,&
      57              :                                               lib_pexsi_env,&
      58              :                                               pexsi_to_cp2k
      59              :    USE qs_energy_types,                 ONLY: qs_energy_type
      60              :    USE qs_environment_types,            ONLY: get_qs_env,&
      61              :                                               qs_environment_type
      62              :    USE qs_ks_types,                     ONLY: qs_ks_env_type
      63              : #include "./base/base_uses.f90"
      64              : 
      65              :    IMPLICIT NONE
      66              :    PRIVATE
      67              : 
      68              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'pexsi_methods'
      69              : 
      70              :    LOGICAL, PARAMETER, PRIVATE          :: careful_mod = .FALSE.
      71              : 
      72              :    PUBLIC :: density_matrix_pexsi, pexsi_init_read_input, pexsi_to_qs, pexsi_init_scf, pexsi_finalize_scf, &
      73              :              pexsi_set_convergence_tolerance
      74              : 
      75              : CONTAINS
      76              : 
      77              : ! **************************************************************************************************
      78              : !> \brief Read CP2K input section PEXSI and pass it to the PEXSI environment
      79              : !> \param pexsi_section ...
      80              : !> \param pexsi_env ...
      81              : !> \par History
      82              : !>      11.2014 created [Patrick Seewald]
      83              : !> \author Patrick Seewald
      84              : ! **************************************************************************************************
      85            0 :    SUBROUTINE pexsi_init_read_input(pexsi_section, pexsi_env)
      86              :       TYPE(section_vals_type), INTENT(IN), POINTER       :: pexsi_section
      87              :       TYPE(lib_pexsi_env), INTENT(INOUT)                 :: pexsi_env
      88              : 
      89              :       INTEGER                                            :: isInertiaCount_int, maxPEXSIIter, &
      90              :                                                             min_ranks_per_pole, npSymbFact, &
      91              :                                                             numPole, ordering, rowOrdering, &
      92              :                                                             verbosity
      93              :       LOGICAL                                            :: csr_screening, isInertiaCount
      94              :       REAL(KIND=dp) :: gap, muInertiaExpansion, muInertiaTolerance, muMax0, muMin0, &
      95              :          muPEXSISafeGuard, numElectronInitialTolerance, numElectronTargetTolerance, temperature
      96              : 
      97              : ! Note: omitting the following PEXSI options: deltaE (estimated by Arnoldi
      98              : ! before invoking PEXSI), mu0 (taken from previous SCF step), matrixType
      99              : ! (not implemented in PEXSI yet), isSymbolicFactorize (not needed because
     100              : ! of fixed sparsity pattern)
     101              : 
     102              :       CALL section_vals_val_get(pexsi_section, "TEMPERATURE", &
     103            0 :                                 r_val=temperature)
     104              :       CALL section_vals_val_get(pexsi_section, "GAP", &
     105            0 :                                 r_val=gap)
     106              :       CALL section_vals_val_get(pexsi_section, "NUM_POLE", &
     107            0 :                                 i_val=numPole)
     108              :       CALL section_vals_val_get(pexsi_section, "IS_INERTIA_COUNT", &
     109            0 :                                 l_val=isInertiaCount)
     110              :       CALL section_vals_val_get(pexsi_section, "MAX_PEXSI_ITER", &
     111            0 :                                 i_val=maxPEXSIIter)
     112              :       CALL section_vals_val_get(pexsi_section, "MU_MIN_0", &
     113            0 :                                 r_val=muMin0)
     114              :       CALL section_vals_val_get(pexsi_section, "MU_MAX_0", &
     115            0 :                                 r_val=muMax0)
     116              :       CALL section_vals_val_get(pexsi_section, "MU_INERTIA_TOLERANCE", &
     117            0 :                                 r_val=muInertiaTolerance)
     118              :       CALL section_vals_val_get(pexsi_section, "MU_INERTIA_EXPANSION", &
     119            0 :                                 r_val=muInertiaExpansion)
     120              :       CALL section_vals_val_get(pexsi_section, "MU_PEXSI_SAFE_GUARD", &
     121            0 :                                 r_val=muPEXSISafeGuard)
     122              :       CALL section_vals_val_get(pexsi_section, "NUM_ELECTRON_INITIAL_TOLERANCE", &
     123            0 :                                 r_val=numElectronInitialTolerance)
     124              :       CALL section_vals_val_get(pexsi_section, "NUM_ELECTRON_PEXSI_TOLERANCE", &
     125            0 :                                 r_val=numElectronTargetTolerance)
     126              :       CALL section_vals_val_get(pexsi_section, "ORDERING", &
     127            0 :                                 i_val=ordering)
     128              :       CALL section_vals_val_get(pexsi_section, "ROW_ORDERING", &
     129            0 :                                 i_val=rowOrdering)
     130              :       CALL section_vals_val_get(pexsi_section, "NP_SYMB_FACT", &
     131            0 :                                 i_val=npSymbFact)
     132              :       CALL section_vals_val_get(pexsi_section, "VERBOSITY", &
     133            0 :                                 i_val=verbosity)
     134              :       CALL section_vals_val_get(pexsi_section, "MIN_RANKS_PER_POLE", &
     135            0 :                                 i_val=min_ranks_per_pole)
     136              :       CALL section_vals_val_get(pexsi_section, "CSR_SCREENING", &
     137            0 :                                 l_val=csr_screening)
     138              : 
     139            0 :       isInertiaCount_int = MERGE(1, 0, isInertiaCount) ! is integer in PEXSI
     140              : 
     141              :       ! Set default options inside PEXSI
     142            0 :       CALL cp_pexsi_set_default_options(pexsi_env%options)
     143              : 
     144              :       ! Pass CP2K input to PEXSI options
     145              :       CALL cp_pexsi_set_options(pexsi_env%options, temperature=temperature, gap=gap, &
     146              :                                 numPole=numPole, isInertiaCount=isInertiaCount_int, maxPEXSIIter=maxPEXSIIter, &
     147              :                                 muMin0=muMin0, muMax0=muMax0, muInertiaTolerance=muInertiaTolerance, &
     148              :                                 muInertiaExpansion=muInertiaExpansion, muPEXSISafeGuard=muPEXSISafeGuard, &
     149            0 :                                 ordering=ordering, rowOrdering=rowOrdering, npSymbFact=npSymbFact, verbosity=verbosity)
     150              : 
     151            0 :       pexsi_env%num_ranks_per_pole = min_ranks_per_pole ! not a PEXSI option
     152            0 :       pexsi_env%csr_screening = csr_screening
     153              : 
     154            0 :       IF (numElectronInitialTolerance < numElectronTargetTolerance) THEN
     155            0 :          numElectronInitialTolerance = numElectronTargetTolerance
     156              :       END IF
     157              : 
     158            0 :       pexsi_env%tol_nel_initial = numElectronInitialTolerance
     159            0 :       pexsi_env%tol_nel_target = numElectronTargetTolerance
     160              : 
     161            0 :    END SUBROUTINE pexsi_init_read_input
     162              : 
     163              : ! **************************************************************************************************
     164              : !> \brief Initializations needed before SCF
     165              : !> \param ks_env ...
     166              : !> \param pexsi_env ...
     167              : !> \param template_matrix DBCSR matrix that defines the block structure and
     168              : !>        sparsity pattern of all matrices that are sent to PEXSI
     169              : ! **************************************************************************************************
     170            0 :    SUBROUTINE pexsi_init_scf(ks_env, pexsi_env, template_matrix)
     171              :       TYPE(qs_ks_env_type), POINTER                      :: ks_env
     172              :       TYPE(lib_pexsi_env), INTENT(INOUT)                 :: pexsi_env
     173              :       TYPE(dbcsr_type), INTENT(IN)                       :: template_matrix
     174              : 
     175              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'pexsi_init_scf'
     176              : 
     177              :       INTEGER                                            :: handle, ispin, unit_nr
     178              :       TYPE(cp_logger_type), POINTER                      :: logger
     179              : 
     180            0 :       CALL timeset(routineN, handle)
     181              : 
     182            0 :       logger => cp_get_default_logger()
     183            0 :       IF (logger%para_env%is_source()) THEN
     184            0 :          unit_nr = cp_logger_get_default_unit_nr(logger, local=.TRUE.)
     185              :       ELSE
     186            0 :          unit_nr = -1
     187              :       END IF
     188              : 
     189              :       ! Create template matrices fixing sparsity pattern for PEXSI
     190              : 
     191            0 :       IF (dbcsr_has_symmetry(template_matrix)) THEN
     192              :          CALL dbcsr_copy(pexsi_env%dbcsr_template_matrix_sym, template_matrix, &
     193            0 :                          "symmetric template matrix for CSR conversion")
     194              :          CALL dbcsr_desymmetrize(pexsi_env%dbcsr_template_matrix_sym, &
     195            0 :                                  pexsi_env%dbcsr_template_matrix_nonsym)
     196              :       ELSE
     197              :          CALL dbcsr_copy(pexsi_env%dbcsr_template_matrix_nonsym, template_matrix, &
     198            0 :                          "non-symmetric template matrix for CSR conversion")
     199              :          CALL dbcsr_copy(pexsi_env%dbcsr_template_matrix_sym, template_matrix, &
     200            0 :                          "symmetric template matrix for CSR conversion")
     201              :       END IF
     202              : 
     203              :       CALL dbcsr_create(pexsi_env%csr_sparsity, "CSR sparsity", &
     204            0 :                         template=pexsi_env%dbcsr_template_matrix_sym)
     205            0 :       CALL dbcsr_copy(pexsi_env%csr_sparsity, pexsi_env%dbcsr_template_matrix_sym)
     206              : 
     207            0 :       CALL cp_dbcsr_to_csr_screening(ks_env, pexsi_env%csr_sparsity)
     208              : 
     209            0 :       IF (.NOT. pexsi_env%csr_screening) CALL dbcsr_set(pexsi_env%csr_sparsity, 1.0_dp)
     210              :       CALL dbcsr_csr_create_from_dbcsr(pexsi_env%dbcsr_template_matrix_nonsym, &
     211              :                                        pexsi_env%csr_mat_s, &
     212              :                                        dbcsr_csr_eqrow_floor_dist, &
     213              :                                        csr_sparsity=pexsi_env%csr_sparsity, &
     214            0 :                                        numnodes=pexsi_env%num_ranks_per_pole)
     215              : 
     216            0 :       IF (unit_nr > 0) WRITE (unit_nr, "(/T2,A)") "SPARSITY OF THE OVERLAP MATRIX IN CSR FORMAT"
     217            0 :       CALL dbcsr_csr_print_sparsity(pexsi_env%csr_mat_s, unit_nr)
     218              : 
     219            0 :       CALL dbcsr_convert_dbcsr_to_csr(pexsi_env%dbcsr_template_matrix_nonsym, pexsi_env%csr_mat_s)
     220              : 
     221            0 :       CALL dbcsr_csr_create(pexsi_env%csr_mat_ks, pexsi_env%csr_mat_s)
     222            0 :       CALL dbcsr_csr_create(pexsi_env%csr_mat_p, pexsi_env%csr_mat_s)
     223            0 :       CALL dbcsr_csr_create(pexsi_env%csr_mat_E, pexsi_env%csr_mat_s)
     224            0 :       CALL dbcsr_csr_create(pexsi_env%csr_mat_F, pexsi_env%csr_mat_s)
     225              : 
     226            0 :       DO ispin = 1, pexsi_env%nspin
     227              :          ! max_ev_vector only initialised to avoid warning in case max_scf==0
     228              :          CALL dbcsr_create(pexsi_env%matrix_w(ispin)%matrix, "W matrix", &
     229            0 :                            template=template_matrix, matrix_type=dbcsr_type_no_symmetry)
     230              :       END DO
     231              : 
     232            0 :       CALL cp_pexsi_set_options(pexsi_env%options, numElectronPEXSITolerance=pexsi_env%tol_nel_initial)
     233              : 
     234            0 :       CALL timestop(handle)
     235              : 
     236            0 :    END SUBROUTINE pexsi_init_scf
     237              : 
     238              : ! **************************************************************************************************
     239              : !> \brief Deallocations and post-processing after SCF
     240              : !> \param pexsi_env ...
     241              : !> \param mu_spin ...
     242              : ! **************************************************************************************************
     243            0 :    SUBROUTINE pexsi_finalize_scf(pexsi_env, mu_spin)
     244              :       TYPE(lib_pexsi_env), INTENT(INOUT)                 :: pexsi_env
     245              :       REAL(KIND=dp), DIMENSION(2), INTENT(IN)            :: mu_spin
     246              : 
     247              :       CHARACTER(len=*), PARAMETER :: routineN = 'pexsi_finalize_scf'
     248              : 
     249              :       INTEGER                                            :: handle, ispin, unit_nr
     250              :       REAL(KIND=dp)                                      :: kTS_total, mu_total
     251              :       TYPE(cp_logger_type), POINTER                      :: logger
     252              : 
     253            0 :       CALL timeset(routineN, handle)
     254              : 
     255            0 :       logger => cp_get_default_logger()
     256            0 :       IF (logger%para_env%is_source()) THEN
     257            0 :          unit_nr = cp_logger_get_default_unit_nr(logger, local=.TRUE.)
     258              :       ELSE
     259              :          unit_nr = -1
     260              :       END IF
     261              : 
     262            0 :       mu_total = SUM(mu_spin(1:pexsi_env%nspin))/REAL(pexsi_env%nspin, KIND=dp)
     263            0 :       kTS_total = SUM(pexsi_env%kTS)
     264            0 :       IF (pexsi_env%nspin == 1) kTS_total = kTS_total*2.0_dp
     265              : 
     266            0 :       IF (unit_nr > 0) THEN
     267              :          WRITE (unit_nr, "(/A,T55,F26.15)") &
     268            0 :             " PEXSI| Electronic entropic energy (a.u.):", kTS_total
     269              :          WRITE (unit_nr, "(A,T55,F26.15)") &
     270            0 :             " PEXSI| Chemical potential (a.u.):", mu_total
     271              :       END IF
     272              : 
     273            0 :       CALL dbcsr_release(pexsi_env%dbcsr_template_matrix_sym)
     274            0 :       CALL dbcsr_release(pexsi_env%dbcsr_template_matrix_nonsym)
     275            0 :       CALL dbcsr_release(pexsi_env%csr_sparsity)
     276            0 :       CALL dbcsr_csr_destroy(pexsi_env%csr_mat_p)
     277            0 :       CALL dbcsr_csr_destroy(pexsi_env%csr_mat_ks)
     278            0 :       CALL dbcsr_csr_destroy(pexsi_env%csr_mat_s)
     279            0 :       CALL dbcsr_csr_destroy(pexsi_env%csr_mat_E)
     280            0 :       CALL dbcsr_csr_destroy(pexsi_env%csr_mat_F)
     281            0 :       DO ispin = 1, pexsi_env%nspin
     282            0 :          CALL dbcsr_release(pexsi_env%max_ev_vector(ispin))
     283            0 :          CALL dbcsr_release(pexsi_env%matrix_w(ispin)%matrix)
     284              :       END DO
     285            0 :       CALL timestop(handle)
     286            0 :       pexsi_env%tol_nel_initial = pexsi_env%tol_nel_target ! Turn off adaptive threshold for subsequent SCF cycles
     287            0 :    END SUBROUTINE pexsi_finalize_scf
     288              : 
     289              : ! **************************************************************************************************
     290              : !> \brief Calculate density matrix, energy-weighted density matrix and entropic
     291              : !>        energy contribution with the DFT driver of the PEXSI library.
     292              : !> \param[in,out] pexsi_env     PEXSI environment
     293              : !> \param[in,out] matrix_p      density matrix returned by PEXSI
     294              : !> \param[in,out] matrix_w      energy-weighted density matrix returned by PEXSI
     295              : !> \param[out] kTS              entropic energy contribution returned by PEXSI
     296              : !> \param[in] matrix_ks         Kohn-Sham matrix from linear scaling QS environment
     297              : !> \param[in] matrix_s          overlap matrix from linear scaling QS environment
     298              : !> \param[in] nelectron_exact   exact number of electrons
     299              : !> \param[out] mu               chemical potential calculated by PEXSI
     300              : !> \param[in] iscf              SCF step
     301              : !> \param[in] ispin             Number of spin
     302              : !> \par History
     303              : !>      11.2014 created [Patrick Seewald]
     304              : !> \author Patrick Seewald
     305              : ! **************************************************************************************************
     306            0 :    SUBROUTINE density_matrix_pexsi(pexsi_env, matrix_p, matrix_w, kTS, matrix_ks, matrix_s, &
     307              :                                    nelectron_exact, mu, iscf, ispin)
     308              :       TYPE(lib_pexsi_env), INTENT(INOUT)                 :: pexsi_env
     309              :       TYPE(dbcsr_type), INTENT(INOUT)                    :: matrix_p
     310              :       TYPE(dbcsr_p_type), INTENT(INOUT)                  :: matrix_w
     311              :       REAL(KIND=dp), INTENT(OUT)                         :: kTS
     312              :       TYPE(dbcsr_type), INTENT(IN), TARGET               :: matrix_ks, matrix_s
     313              :       INTEGER, INTENT(IN)                                :: nelectron_exact
     314              :       REAL(KIND=dp), INTENT(OUT)                         :: mu
     315              :       INTEGER, INTENT(IN)                                :: iscf, ispin
     316              : 
     317              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'density_matrix_pexsi'
     318              :       INTEGER, PARAMETER                                 :: S_not_identity = 0
     319              : 
     320              :       INTEGER :: handle, is_symbolic_factorize, isInertiaCount, isInertiaCount_out, mynode, &
     321              :          n_total_inertia_iter, n_total_pexsi_iter, unit_nr
     322              :       LOGICAL                                            :: first_call, pexsi_convergence
     323              :       REAL(KIND=dp) :: delta_E, energy_H, energy_S, free_energy, mu_max_in, mu_max_out, mu_min_in, &
     324              :          mu_min_out, nel_tol, nelectron_diff, nelectron_exact_pexsi, nelectron_out
     325              :       TYPE(arnoldi_env_type)                             :: arnoldi_env
     326              :       TYPE(cp_logger_type), POINTER                      :: logger
     327              :       TYPE(dbcsr_distribution_type)                      :: dist
     328            0 :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: arnoldi_matrices
     329              : 
     330            0 :       CALL timeset(routineN, handle)
     331              : 
     332              :       ! get a useful output_unit
     333            0 :       logger => cp_get_default_logger()
     334            0 :       IF (logger%para_env%is_source()) THEN
     335            0 :          unit_nr = cp_logger_get_default_unit_nr(logger, local=.TRUE.)
     336              :       ELSE
     337              :          unit_nr = -1
     338              :       END IF
     339              : 
     340            0 :       first_call = (iscf == 1) .AND. (ispin == 1)
     341              : 
     342              :       ! Assert a few things the first time PEXSI is called
     343              :       IF (first_call) THEN
     344              :          ! Assertion that matrices have the expected symmetry (both should be symmetric if no
     345              :          ! S preconditioning and no molecular clustering)
     346            0 :          IF (.NOT. dbcsr_has_symmetry(matrix_ks)) THEN
     347            0 :             CPABORT("PEXSI interface expects a non-symmetric DBCSR Kohn-Sham matrix")
     348              :          END IF
     349            0 :          IF (.NOT. dbcsr_has_symmetry(matrix_s)) THEN
     350            0 :             CPABORT("PEXSI interface expects a non-symmetric DBCSR overlap matrix")
     351              :          END IF
     352              : 
     353              :          ! Assertion on datatype
     354              :          IF ((pexsi_env%csr_mat_s%nzval_local%data_type /= dbcsr_csr_type_real_8) &
     355            0 :              .OR. (pexsi_env%csr_mat_ks%nzval_local%data_type /= dbcsr_csr_type_real_8)) THEN
     356            0 :             CPABORT("Complex data type not supported by PEXSI")
     357              :          END IF
     358              : 
     359              :          ! Assertion on number of non-zero elements
     360              :          !(TODO: update when PEXSI changes to Long Int)
     361            0 :          IF (pexsi_env%csr_mat_s%nze_total >= INT(2, kind=int_8)**31) THEN
     362            0 :             CPABORT("Total number of non-zero elements of CSR matrix is too large to be handled by PEXSI")
     363              :          END IF
     364              :       END IF
     365              : 
     366            0 :       CALL dbcsr_get_info(matrix_ks, distribution=dist)
     367            0 :       CALL dbcsr_distribution_get(dist, mynode=mynode)
     368              : 
     369              :       ! Convert DBCSR matrices to PEXSI CSR format. Intermediate step to template matrix
     370              :       ! needed in order to retain the initial sparsity pattern that is required for the
     371              :       ! conversion to CSR format.
     372            0 :       CALL dbcsr_copy(pexsi_env%dbcsr_template_matrix_sym, matrix_s, keep_sparsity=.TRUE.)
     373              :       CALL dbcsr_convert_dbcsr_to_csr(pexsi_env%dbcsr_template_matrix_sym, &
     374            0 :                                       pexsi_env%csr_mat_s)
     375              : 
     376            0 :       CALL dbcsr_copy(pexsi_env%dbcsr_template_matrix_sym, matrix_ks, keep_sparsity=.TRUE.)
     377              :       CALL dbcsr_convert_dbcsr_to_csr(pexsi_env%dbcsr_template_matrix_sym, &
     378            0 :                                       pexsi_env%csr_mat_ks)
     379              : 
     380              :       ! Get PEXSI input delta_E (upper bound for largest eigenvalue) using Arnoldi
     381            0 :       NULLIFY (arnoldi_matrices)
     382            0 :       CALL dbcsr_allocate_matrix_set(arnoldi_matrices, 2)
     383            0 :       arnoldi_matrices(1)%matrix => matrix_ks
     384            0 :       arnoldi_matrices(2)%matrix => matrix_s
     385              :       CALL setup_arnoldi_env(arnoldi_env, arnoldi_matrices, max_iter=20, &
     386              :                              threshold=1.0E-2_dp, selection_crit=2, nval_request=1, nrestarts=21, &
     387            0 :                              generalized_ev=.TRUE., iram=.FALSE.)
     388            0 :       IF (iscf > 1) CALL set_arnoldi_initial_vector(arnoldi_env, pexsi_env%max_ev_vector(ispin))
     389            0 :       CALL arnoldi_ev(arnoldi_matrices, arnoldi_env)
     390            0 :       delta_E = REAL(get_selected_ritz_val(arnoldi_env, 1), dp)
     391              :       ! increase delta_E a bit to make sure that it really is an upper bound
     392            0 :       delta_E = delta_E + 1.0E-2_dp*ABS(delta_E)
     393              :       CALL get_selected_ritz_vector(arnoldi_env, 1, arnoldi_matrices(1)%matrix, &
     394            0 :                                     pexsi_env%max_ev_vector(ispin))
     395            0 :       CALL deallocate_arnoldi_env(arnoldi_env)
     396            0 :       DEALLOCATE (arnoldi_matrices)
     397              : 
     398            0 :       nelectron_exact_pexsi = nelectron_exact
     399              : 
     400            0 :       CALL cp_pexsi_set_options(pexsi_env%options, deltaE=delta_E)
     401              : 
     402              :       ! Set PEXSI options appropriately for first SCF iteration
     403            0 :       IF (iscf == 1) THEN
     404              :          ! Get option isInertiaCount to reset it later on and set it to 1 for first SCF iteration
     405            0 :          CALL cp_pexsi_get_options(pexsi_env%options, isInertiaCount=isInertiaCount)
     406              :          CALL cp_pexsi_set_options(pexsi_env%options, isInertiaCount=1, &
     407            0 :                                    isSymbolicFactorize=1)
     408              :       END IF
     409              : 
     410              :       ! Write PEXSI options to output
     411              :       CALL cp_pexsi_get_options(pexsi_env%options, isInertiaCount=isInertiaCount_out, &
     412              :                                 isSymbolicFactorize=is_symbolic_factorize, &
     413              :                                 muMin0=mu_min_in, muMax0=mu_max_in, &
     414            0 :                                 NumElectronPEXSITolerance=nel_tol)
     415              : 
     416              : !    IF(unit_nr>0) WRITE(unit_nr,'(/A,I4,A,I4)') " PEXSI| SCF", iscf, &
     417              : !                                                ", spin component", ispin
     418              : 
     419            0 :       IF (unit_nr > 0) WRITE (unit_nr, '(/A,T41,L20)') " PEXSI| Do inertia counting?", &
     420            0 :          isInertiaCount_out == 1
     421              :       IF (unit_nr > 0) WRITE (unit_nr, '(A,T50,F5.2,T56,F5.2)') &
     422            0 :          " PEXSI| Guess for min mu, max mu", mu_min_in, mu_max_in
     423              : 
     424              :       IF (unit_nr > 0) WRITE (unit_nr, '(A,T41,E20.3)') &
     425            0 :          " PEXSI| Tolerance in number of electrons", nel_tol
     426              : 
     427              : !    IF(unit_nr>0) WRITE(unit_nr,'(A,T41,L20)') &
     428              : !                  " PEXSI|   Do symbolic factorization?", is_symbolic_factorize==1
     429              : 
     430              :       IF (unit_nr > 0) WRITE (unit_nr, '(A,T41,F20.2)') &
     431            0 :          " PEXSI| Arnoldi est. spectral radius", delta_E
     432              : 
     433              :       ! Load data into PEXSI
     434              :       CALL cp_pexsi_load_real_hs_matrix( &
     435              :          pexsi_env%plan, &
     436              :          pexsi_env%options, &
     437              :          pexsi_env%csr_mat_ks%nrows_total, &
     438              :          INT(pexsi_env%csr_mat_ks%nze_total, kind=int_4), & ! TODO: update when PEXSI changes to Long Int
     439              :          pexsi_env%csr_mat_ks%nze_local, &
     440              :          pexsi_env%csr_mat_ks%nrows_local, &
     441              :          pexsi_env%csr_mat_ks%rowptr_local, &
     442              :          pexsi_env%csr_mat_ks%colind_local, &
     443              :          pexsi_env%csr_mat_ks%nzval_local%r_dp, &
     444              :          S_not_identity, &
     445            0 :          pexsi_env%csr_mat_s%nzval_local%r_dp)
     446              : 
     447              :       ! convert to spin restricted before passing number of electrons to PEXSI
     448              :       CALL convert_nspin_cp2k_pexsi(cp2k_to_pexsi, &
     449            0 :                                     numElectron=nelectron_exact_pexsi)
     450              : 
     451              :       ! Call DFT driver of PEXSI doing the actual calculation
     452              :       CALL cp_pexsi_dft_driver(pexsi_env%plan, pexsi_env%options, &
     453              :                                nelectron_exact_pexsi, mu, nelectron_out, mu_min_out, mu_max_out, &
     454            0 :                                n_total_inertia_iter, n_total_pexsi_iter)
     455              : 
     456              :       ! Check convergence
     457            0 :       nelectron_diff = nelectron_out - nelectron_exact_pexsi
     458            0 :       pexsi_convergence = ABS(nelectron_diff) < nel_tol
     459              : 
     460            0 :       IF (unit_nr > 0) THEN
     461            0 :          IF (pexsi_convergence) THEN
     462            0 :             WRITE (unit_nr, '(/A)') " PEXSI| Converged"
     463              :          ELSE
     464            0 :             WRITE (unit_nr, '(/A)') " PEXSI| PEXSI did not converge!"
     465              :          END IF
     466              : 
     467              : !      WRITE(unit_nr,'(A,T41,F20.10,T61,F20.10)') " PEXSI|   Number of electrons", &
     468              : !                      nelectron_out/pexsi_env%nspin, nelectron_diff/pexsi_env%nspin
     469              : 
     470            0 :          WRITE (unit_nr, '(A,T41,F20.6)') " PEXSI|   Chemical potential", mu
     471              : 
     472            0 :          WRITE (unit_nr, '(A,T41,I20)') " PEXSI|   PEXSI iterations", n_total_pexsi_iter
     473            0 :          WRITE (unit_nr, '(A,T41,I20/)') " PEXSI|   Inertia counting iterations", &
     474            0 :             n_total_inertia_iter
     475              :       END IF
     476              : 
     477            0 :       IF (.NOT. pexsi_convergence) THEN
     478            0 :          CPABORT("PEXSI did not converge. Consider logPEXSI0 for more information.")
     479              :       END IF
     480              : 
     481              :       ! Retrieve results from PEXSI
     482            0 :       IF (mynode < pexsi_env%mp_dims(1)*pexsi_env%mp_dims(2)) THEN
     483              :          CALL cp_pexsi_retrieve_real_dft_matrix( &
     484              :             pexsi_env%plan, &
     485              :             pexsi_env%csr_mat_p%nzval_local%r_dp, &
     486              :             pexsi_env%csr_mat_E%nzval_local%r_dp, &
     487              :             pexsi_env%csr_mat_F%nzval_local%r_dp, &
     488            0 :             energy_H, energy_S, free_energy)
     489              :          ! calculate entropic energy contribution -TS = A - U
     490            0 :          kTS = (free_energy - energy_H)
     491              :       END IF
     492              : 
     493              :       ! send kTS to all nodes:
     494            0 :       CALL pexsi_env%mp_group%bcast(kTS, 0)
     495              : 
     496              :       ! Convert PEXSI CSR matrices to DBCSR matrices
     497              :       CALL dbcsr_convert_csr_to_dbcsr(pexsi_env%dbcsr_template_matrix_nonsym, &
     498            0 :                                       pexsi_env%csr_mat_p)
     499            0 :       CALL dbcsr_copy(matrix_p, pexsi_env%dbcsr_template_matrix_nonsym)
     500              :       CALL dbcsr_convert_csr_to_dbcsr(pexsi_env%dbcsr_template_matrix_nonsym, &
     501            0 :                                       pexsi_env%csr_mat_E)
     502            0 :       CALL dbcsr_copy(matrix_w%matrix, pexsi_env%dbcsr_template_matrix_nonsym)
     503              : 
     504              :       ! Convert to spin unrestricted
     505              :       CALL convert_nspin_cp2k_pexsi(pexsi_to_cp2k, matrix_p=matrix_p, &
     506            0 :                                     matrix_w=matrix_w, kTS=kTS)
     507              : 
     508              :       ! Pass resulting mu as initial guess for next SCF to PEXSI
     509              :       CALL cp_pexsi_set_options(pexsi_env%options, mu0=mu, muMin0=mu_min_out, &
     510            0 :                                 muMax0=mu_max_out)
     511              : 
     512              :       ! Reset isInertiaCount according to user input
     513            0 :       IF (iscf == 1) THEN
     514              :          CALL cp_pexsi_set_options(pexsi_env%options, isInertiaCount= &
     515            0 :                                    isInertiaCount)
     516              :       END IF
     517              : 
     518              :       ! Turn off symbolic factorization for subsequent calls
     519            0 :       IF (first_call) THEN
     520            0 :          CALL cp_pexsi_set_options(pexsi_env%options, isSymbolicFactorize=0)
     521              :       END IF
     522              : 
     523            0 :       CALL timestop(handle)
     524            0 :    END SUBROUTINE density_matrix_pexsi
     525              : 
     526              : ! **************************************************************************************************
     527              : !> \brief Set PEXSI convergence tolerance (numElectronPEXSITolerance), adapted
     528              : !>        to the convergence error of the previous SCF step. The tolerance is
     529              : !>        calculated using an initial convergence threshold (tol_nel_initial)
     530              : !>        and a target convergence threshold (tol_nel_target):
     531              : !>        numElectronPEXSITolerance(delta_scf) = alpha*delta_scf+beta
     532              : !>        where alpha and beta are chosen such that
     533              : !>        numElectronPEXSITolerance(delta_scf_0) = tol_nel_initial
     534              : !>        numElectronPEXSITolerance(eps_scf) = tol_nel_target
     535              : !>        and delta_scf_0 is the initial SCF convergence error.
     536              : !> \param pexsi_env ...
     537              : !> \param delta_scf convergence error of previous SCF step
     538              : !> \param eps_scf SCF convergence criterion
     539              : !> \param initialize whether or not adaptive thresholing should be initialized
     540              : !>        with delta_scf as initial convergence error
     541              : !> \param check_convergence is set to .FALSE. if convergence in number of electrons
     542              : !>        will not be achieved in next SCF step
     543              : ! **************************************************************************************************
     544            0 :    SUBROUTINE pexsi_set_convergence_tolerance(pexsi_env, delta_scf, eps_scf, initialize, &
     545              :                                               check_convergence)
     546              :       TYPE(lib_pexsi_env), INTENT(INOUT)                 :: pexsi_env
     547              :       REAL(KIND=dp), INTENT(IN)                          :: delta_scf, eps_scf
     548              :       LOGICAL, INTENT(IN)                                :: initialize
     549              :       LOGICAL, INTENT(OUT)                               :: check_convergence
     550              : 
     551              :       CHARACTER(len=*), PARAMETER :: routineN = 'pexsi_set_convergence_tolerance'
     552              : 
     553              :       INTEGER                                            :: handle
     554              :       REAL(KIND=dp)                                      :: tol_nel
     555              : 
     556            0 :       CALL timeset(routineN, handle)
     557              : 
     558            0 :       tol_nel = pexsi_env%tol_nel_initial
     559              : 
     560            0 :       IF (initialize) THEN
     561              :          pexsi_env%adaptive_nel_alpha = &
     562            0 :             (pexsi_env%tol_nel_initial - pexsi_env%tol_nel_target)/(ABS(delta_scf) - eps_scf)
     563              :          pexsi_env%adaptive_nel_beta = &
     564            0 :             pexsi_env%tol_nel_initial - pexsi_env%adaptive_nel_alpha*ABS(delta_scf)
     565            0 :          pexsi_env%do_adaptive_tol_nel = .TRUE.
     566              :       END IF
     567            0 :       IF (pexsi_env%do_adaptive_tol_nel) THEN
     568            0 :          tol_nel = pexsi_env%adaptive_nel_alpha*ABS(delta_scf) + pexsi_env%adaptive_nel_beta
     569            0 :          tol_nel = MAX(tol_nel, pexsi_env%tol_nel_target)
     570            0 :          tol_nel = MIN(tol_nel, pexsi_env%tol_nel_initial)
     571              :       END IF
     572              : 
     573            0 :       check_convergence = (tol_nel <= pexsi_env%tol_nel_target)
     574              : 
     575            0 :       CALL cp_pexsi_set_options(pexsi_env%options, numElectronPEXSITolerance=tol_nel)
     576            0 :       CALL timestop(handle)
     577            0 :    END SUBROUTINE pexsi_set_convergence_tolerance
     578              : 
     579              : ! **************************************************************************************************
     580              : !> \brief Pass energy weighted density matrix and entropic energy contribution
     581              : !>        to QS environment
     582              : !> \param ls_scf_env ...
     583              : !> \param qs_env ...
     584              : !> \param kTS ...
     585              : !> \param matrix_w ...
     586              : !> \par History
     587              : !>      12.2014 created [Patrick Seewald]
     588              : !> \author Patrick Seewald
     589              : ! **************************************************************************************************
     590            0 :    SUBROUTINE pexsi_to_qs(ls_scf_env, qs_env, kTS, matrix_w)
     591              :       TYPE(ls_scf_env_type)                              :: ls_scf_env
     592              :       TYPE(qs_environment_type), INTENT(INOUT), POINTER  :: qs_env
     593              :       REAL(KIND=dp), DIMENSION(:), INTENT(IN), OPTIONAL  :: kTS
     594              :       TYPE(dbcsr_p_type), DIMENSION(:), INTENT(IN), &
     595              :          OPTIONAL                                        :: matrix_w
     596              : 
     597              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'pexsi_to_qs'
     598              : 
     599              :       INTEGER                                            :: handle, ispin, unit_nr
     600              :       REAL(KIND=dp)                                      :: kTS_total
     601              :       TYPE(cp_logger_type), POINTER                      :: logger
     602            0 :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: matrix_w_qs
     603              :       TYPE(qs_energy_type), POINTER                      :: energy
     604              : 
     605            0 :       CALL timeset(routineN, handle)
     606              : 
     607            0 :       NULLIFY (energy)
     608              : 
     609              :       ! get a useful output_unit
     610            0 :       logger => cp_get_default_logger()
     611            0 :       IF (logger%para_env%is_source()) THEN
     612            0 :          unit_nr = cp_logger_get_default_unit_nr(logger, local=.TRUE.)
     613              :       ELSE
     614              :          unit_nr = -1
     615              :       END IF
     616              : 
     617            0 :       CALL get_qs_env(qs_env, energy=energy, matrix_w=matrix_w_qs)
     618              : 
     619            0 :       IF (PRESENT(matrix_w)) THEN
     620            0 :          DO ispin = 1, ls_scf_env%nspins
     621              :             CALL matrix_ls_to_qs(matrix_w_qs(ispin)%matrix, matrix_w(ispin)%matrix, &
     622            0 :                                  ls_scf_env%ls_mstruct, covariant=.FALSE.)
     623            0 :             IF (ls_scf_env%nspins == 1) CALL dbcsr_scale(matrix_w_qs(ispin)%matrix, 2.0_dp)
     624              :          END DO
     625              :       END IF
     626              : 
     627            0 :       IF (PRESENT(kTS)) THEN
     628            0 :          kTS_total = SUM(kTS)
     629            0 :          IF (ls_scf_env%nspins == 1) kTS_total = kTS_total*2.0_dp
     630            0 :          energy%kTS = kTS_total
     631              :       END IF
     632              : 
     633            0 :       CALL timestop(handle)
     634            0 :    END SUBROUTINE pexsi_to_qs
     635              : 
     636              : END MODULE pexsi_methods
        

Generated by: LCOV version 2.0-1