LCOV - code coverage report
Current view: top level - src - ec_methods.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:24d69ee) Lines: 77.8 % 45 35
Test Date: 2026-09-03 07:32:15 Functions: 100.0 % 1 1

            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 used for Harris functional
      10              : !>        Kohn-Sham calculation
      11              : !> \par History
      12              : !>       10.2020 created
      13              : !> \author Fabian Belleflamme
      14              : ! **************************************************************************************************
      15              : MODULE ec_methods
      16              :    USE cp_blacs_env,                    ONLY: cp_blacs_env_type
      17              :    USE cp_control_types,                ONLY: dft_control_type
      18              :    USE cp_dbcsr_api,                    ONLY: dbcsr_init_p,&
      19              :                                               dbcsr_type,&
      20              :                                               dbcsr_type_no_symmetry
      21              :    USE cp_dbcsr_operations,             ONLY: cp_dbcsr_m_by_n_from_row_template
      22              :    USE cp_fm_struct,                    ONLY: cp_fm_struct_create,&
      23              :                                               cp_fm_struct_release,&
      24              :                                               cp_fm_struct_type
      25              :    USE cp_fm_types,                     ONLY: cp_fm_get_info,&
      26              :                                               cp_fm_type
      27              :    USE cp_log_handling,                 ONLY: cp_to_string
      28              :    USE kinds,                           ONLY: dp
      29              :    USE message_passing,                 ONLY: mp_para_env_type
      30              :    USE qs_environment_types,            ONLY: get_qs_env,&
      31              :                                               qs_environment_type,&
      32              :                                               set_qs_env
      33              :    USE qs_kind_types,                   ONLY: get_qs_kind_set,&
      34              :                                               qs_kind_type
      35              :    USE qs_matrix_pools,                 ONLY: mpools_release,&
      36              :                                               qs_matrix_pools_type
      37              :    USE qs_mo_types,                     ONLY: allocate_mo_set,&
      38              :                                               get_mo_set,&
      39              :                                               init_mo_set,&
      40              :                                               mo_set_type
      41              : #include "./base/base_uses.f90"
      42              : 
      43              :    IMPLICIT NONE
      44              : 
      45              :    PRIVATE
      46              : 
      47              : ! *** Global parameters ***
      48              : 
      49              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'ec_methods'
      50              : 
      51              :    PUBLIC :: ec_mos_init
      52              : 
      53              : CONTAINS
      54              : 
      55              : ! **************************************************************************************************
      56              : !> \brief Allocate and initiate molecular orbitals environment
      57              : !>
      58              : !> \param qs_env ...
      59              : !> \param matrix_s Used as template
      60              : !> \param
      61              : !>
      62              : !> \par History
      63              : !>       2020.10 created [Fabian Belleflamme]
      64              : !> \author Fabian Belleflamme
      65              : ! **************************************************************************************************
      66           10 :    SUBROUTINE ec_mos_init(qs_env, matrix_s)
      67              :       TYPE(qs_environment_type), POINTER                 :: qs_env
      68              :       TYPE(dbcsr_type)                                   :: matrix_s
      69              : 
      70              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'ec_mos_init'
      71              : 
      72              :       INTEGER                                            :: handle, ispin, multiplicity, n_ao, &
      73              :                                                             nelectron, nmo, nspins
      74              :       INTEGER, DIMENSION(2)                              :: n_mo, nelectron_spin
      75              :       REAL(dp)                                           :: maxocc
      76              :       TYPE(cp_blacs_env_type), POINTER                   :: blacs_env
      77              :       TYPE(cp_fm_struct_type), POINTER                   :: fm_struct
      78              :       TYPE(cp_fm_type), POINTER                          :: mo_coeff
      79              :       TYPE(dbcsr_type), POINTER                          :: mo_coeff_b
      80              :       TYPE(dft_control_type), POINTER                    :: dft_control
      81           10 :       TYPE(mo_set_type), DIMENSION(:), POINTER           :: mos
      82              :       TYPE(mp_para_env_type), POINTER                    :: para_env
      83           10 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
      84              :       TYPE(qs_matrix_pools_type), POINTER                :: my_mpools
      85              : 
      86           10 :       CALL timeset(routineN, handle)
      87              : 
      88           10 :       NULLIFY (blacs_env, dft_control, mo_coeff, mo_coeff_b, mos, my_mpools, qs_kind_set)
      89              : 
      90              :       CALL get_qs_env(qs_env=qs_env, &
      91              :                       dft_control=dft_control, &
      92              :                       blacs_env=blacs_env, &
      93              :                       qs_kind_set=qs_kind_set, &
      94              :                       nelectron_spin=nelectron_spin, &
      95           10 :                       para_env=para_env)
      96           10 :       nspins = dft_control%nspins
      97              : 
      98              :       ! Start setup
      99           10 :       CALL get_qs_kind_set(qs_kind_set, nsgf=n_ao, nelectron=nelectron)
     100              : 
     101              :       ! the total number of electrons
     102           10 :       nelectron = nelectron - dft_control%charge
     103           10 :       multiplicity = dft_control%multiplicity
     104              : 
     105              :       ! setting maxocc and n_mo
     106           10 :       IF (dft_control%nspins == 1) THEN
     107           10 :          maxocc = 2.0_dp
     108           10 :          nelectron_spin(1) = nelectron
     109           10 :          nelectron_spin(2) = 0
     110           10 :          IF (MODULO(nelectron, 2) == 0) THEN
     111           10 :             n_mo(1) = nelectron/2
     112              :          ELSE
     113            0 :             n_mo(1) = INT(nelectron/2._dp) + 1
     114              :          END IF
     115           10 :          n_mo(2) = 0
     116              :       ELSE
     117            0 :          maxocc = 1.0_dp
     118              : 
     119              :          ! The simplist spin distribution is written here. Special cases will
     120              :          ! need additional user input
     121            0 :          IF (MODULO(nelectron + multiplicity - 1, 2) /= 0) THEN
     122            0 :             CPABORT("LSD: try to use a different multiplicity")
     123              :          END IF
     124              : 
     125            0 :          nelectron_spin(1) = (nelectron + multiplicity - 1)/2
     126            0 :          nelectron_spin(2) = (nelectron - multiplicity + 1)/2
     127              : 
     128            0 :          IF (nelectron_spin(2) < 0) THEN
     129            0 :             CPABORT("LSD: too few electrons for this multiplicity")
     130              :          END IF
     131              : 
     132            0 :          n_mo(1) = nelectron_spin(1)
     133            0 :          n_mo(2) = nelectron_spin(2)
     134              : 
     135              :       END IF
     136              : 
     137              :       ! Allocate MO set
     138           40 :       ALLOCATE (mos(nspins))
     139           20 :       DO ispin = 1, nspins
     140              :          CALL allocate_mo_set(mo_set=mos(ispin), &
     141              :                               nao=n_ao, &
     142              :                               nmo=n_mo(ispin), &
     143              :                               nelectron=nelectron_spin(ispin), &
     144              :                               n_el_f=REAL(nelectron_spin(ispin), dp), &
     145              :                               maxocc=maxocc, &
     146           20 :                               flexible_electron_count=dft_control%relax_multiplicity)
     147              :       END DO
     148              : 
     149           10 :       CALL set_qs_env(qs_env, mos=mos)
     150              : 
     151              :       ! finish initialization of the MOs
     152           10 :       NULLIFY (mo_coeff, mo_coeff_b)
     153           20 :       DO ispin = 1, SIZE(mos)
     154              :          CALL get_mo_set(mos(ispin), mo_coeff=mo_coeff, mo_coeff_b=mo_coeff_b, &
     155           10 :                          nmo=nmo, nao=n_ao)
     156              : 
     157           10 :          IF (.NOT. ASSOCIATED(mo_coeff)) THEN
     158              :             CALL cp_fm_struct_create(fm_struct, nrow_global=n_ao, &
     159              :                                      ncol_global=nmo, para_env=para_env, &
     160           10 :                                      context=blacs_env)
     161              : 
     162              :             CALL init_mo_set(mos(ispin), &
     163              :                              fm_struct=fm_struct, &
     164           10 :                              name="qs_env%mo"//TRIM(ADJUSTL(cp_to_string(ispin))))
     165           10 :             CALL cp_fm_struct_release(fm_struct)
     166              :          END IF
     167              : 
     168           30 :          IF (.NOT. ASSOCIATED(mo_coeff_b)) THEN
     169           10 :             CALL cp_fm_get_info(mos(ispin)%mo_coeff, ncol_global=nmo)
     170           10 :             CALL dbcsr_init_p(mos(ispin)%mo_coeff_b)
     171              :             CALL cp_dbcsr_m_by_n_from_row_template(mos(ispin)%mo_coeff_b, &
     172              :                                                    template=matrix_s, &
     173              :                                                    n=nmo, &
     174           10 :                                                    sym=dbcsr_type_no_symmetry)
     175              :          END IF
     176              :       END DO
     177              : 
     178           10 :       CALL mpools_release(mpools=my_mpools)
     179              : 
     180           10 :       CALL timestop(handle)
     181              : 
     182           20 :    END SUBROUTINE ec_mos_init
     183              : 
     184              : END MODULE ec_methods
        

Generated by: LCOV version 2.0-1