LCOV - code coverage report
Current view: top level - src - s_square_methods.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:1be8651) Lines: 60.3 % 174 105
Test Date: 2026-11-24 01:22:08 Functions: 50.0 % 4 2

            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 related to (\cal S)^2 (i.e. spin)
      10              : !> \par History
      11              : !>      03.2006 copied compute_s_square from qs_scf_post (Joost VandeVondele)
      12              : !>      08.2021 revised (Matthias Krack, MK)
      13              : !> \author Joost VandeVondele
      14              : ! **************************************************************************************************
      15              : MODULE s_square_methods
      16              : 
      17              :    USE cp_blacs_env,                    ONLY: cp_blacs_env_type
      18              :    USE cp_cfm_basic_linalg,             ONLY: cp_cfm_schur_product_cc
      19              :    USE cp_cfm_types,                    ONLY: cp_cfm_create,&
      20              :                                               cp_cfm_get_info,&
      21              :                                               cp_cfm_release,&
      22              :                                               cp_cfm_to_fm,&
      23              :                                               cp_cfm_type
      24              :    USE cp_control_types,                ONLY: s2_restraint_type
      25              :    USE cp_dbcsr_api,                    ONLY: dbcsr_p_type
      26              :    USE cp_dbcsr_operations,             ONLY: cp_dbcsr_sm_fm_multiply
      27              :    USE cp_fm_basic_linalg,              ONLY: cp_fm_column_scale,&
      28              :                                               cp_fm_row_scale,&
      29              :                                               cp_fm_schur_product
      30              :    USE cp_fm_pool_types,                ONLY: cp_fm_pool_type,&
      31              :                                               fm_pool_create_fm,&
      32              :                                               fm_pool_give_back_fm
      33              :    USE cp_fm_struct,                    ONLY: cp_fm_struct_create,&
      34              :                                               cp_fm_struct_release,&
      35              :                                               cp_fm_struct_type
      36              :    USE cp_fm_types,                     ONLY: cp_fm_create,&
      37              :                                               cp_fm_get_info,&
      38              :                                               cp_fm_release,&
      39              :                                               cp_fm_sumall,&
      40              :                                               cp_fm_type
      41              :    USE input_constants,                 ONLY: do_s2_constraint,&
      42              :                                               do_s2_restraint
      43              :    USE kinds,                           ONLY: dp
      44              :    USE kpoint_transform,                ONLY: rskp_prepared_transform_type,&
      45              :                                               rskp_transform_evaluate,&
      46              :                                               rskp_transform_prepare,&
      47              :                                               rskp_transform_release
      48              :    USE kpoint_types,                    ONLY: get_kpoint_env,&
      49              :                                               get_kpoint_info,&
      50              :                                               kpoint_env_type,&
      51              :                                               kpoint_type
      52              :    USE mathconstants,                   ONLY: one,&
      53              :                                               z_one,&
      54              :                                               z_zero,&
      55              :                                               zero
      56              :    USE message_passing,                 ONLY: mp_para_env_type
      57              :    USE parallel_gemm_api,               ONLY: parallel_gemm
      58              :    USE qs_matrix_pools,                 ONLY: mpools_get,&
      59              :                                               qs_matrix_pools_type
      60              :    USE qs_mo_types,                     ONLY: get_mo_set,&
      61              :                                               has_uniform_occupation,&
      62              :                                               mo_set_type
      63              :    USE qs_neighbor_list_types,          ONLY: neighbor_list_set_p_type
      64              : #include "./base/base_uses.f90"
      65              : 
      66              :    IMPLICIT NONE
      67              : 
      68              :    PRIVATE
      69              : 
      70              :    ! Global parameters
      71              : 
      72              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 's_square_methods'
      73              : 
      74              :    PUBLIC :: compute_s_square, compute_s_square_kp
      75              :    PUBLIC :: s2_restraint
      76              : 
      77              : CONTAINS
      78              : 
      79              : ! **************************************************************************************************
      80              : !> \brief Compute the expectation value <(\cal S)^2> of the single determinant defined by the
      81              : !>        spin up (alpha) and spin down (beta) orbitals
      82              : !> \param mos [in] MO set with all MO information including the alpha and beta MO coefficients
      83              : !> \param matrix_s [in] AO overlap matrix S (do not mix with the spin operator (\cal S))
      84              : !> \param s_square [out] <(\cal S)^2> including potential spin contaminations
      85              : !> \param s_square_ideal [out] Ideal value for <(\cal S)^2> without any spin contaminations
      86              : !> \par History
      87              : !>      07.2004 created (Joost VandeVondele)
      88              : !>      08.2021 revised (Matthias Krack, MK)
      89              : !>      10.2026 adapted and generalized to partial occupation and k-points
      90              : !> \note
      91              : !>      See Eqs. 2.271 and 2.272 in Modern Quantum Chemistry by A. Szabo and N. S. Ostlund
      92              : ! **************************************************************************************************
      93         2512 :    SUBROUTINE compute_s_square(mos, matrix_s, s_square, s_square_ideal)
      94              : 
      95              :       TYPE(mo_set_type), DIMENSION(:), INTENT(IN)        :: mos
      96              :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: matrix_s
      97              :       REAL(KIND=dp), INTENT(OUT)                         :: s_square, s_square_ideal
      98              : 
      99              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'compute_s_square'
     100              : 
     101              :       INTEGER                                            :: handle, nalpha, nao, nao_beta, nbeta
     102              :       REAL(KIND=dp)                                      :: nela, nelb, s2
     103         2512 :       REAL(KIND=dp), CONTIGUOUS, DIMENSION(:), POINTER   :: occa, occb
     104              :       TYPE(cp_fm_struct_type), POINTER                   :: fm_struct
     105              :       TYPE(cp_fm_type)                                   :: asb, asb2, scb
     106              :       TYPE(cp_fm_type), POINTER                          :: c_alpha, c_beta
     107              : 
     108         2512 :       CALL timeset(routineN, handle)
     109              : 
     110         2512 :       CALL get_mo_set(mo_set=mos(1), mo_coeff=c_alpha, nmo=nalpha, nao=nao)
     111         2512 :       CALL get_mo_set(mo_set=mos(2), mo_coeff=c_beta, nmo=nbeta, nao=nao_beta)
     112         2512 :       CPASSERT(nao == nao_beta)
     113         2512 :       CALL get_mo_set(mo_set=mos(1), n_el_f=nela, occupation_numbers=occa)
     114         2512 :       CALL get_mo_set(mo_set=mos(2), n_el_f=nelb, occupation_numbers=occb)
     115              :       ! Eq. 2.272 in Modern Quantum Chemistry by A. Szabo and N. S. Ostlund
     116         2512 :       s_square_ideal = REAL((nela - nelb)*(nela - nelb + 2), KIND=dp)/4.0_dp
     117              :       ! Create MO overlap matrix
     118         2512 :       CALL cp_fm_get_info(c_beta, matrix_struct=fm_struct)
     119              :       ! Prepare C(alpha)^T*S*C(beta)
     120         2512 :       CALL cp_fm_create(asb, fm_struct, nrow=nalpha, ncol=nbeta, set_zero=.TRUE.)
     121         2512 :       CALL cp_fm_create(asb2, fm_struct, nrow=nalpha, ncol=nbeta, set_zero=.TRUE.)
     122         2512 :       CALL cp_fm_create(scb, fm_struct, set_zero=.TRUE.)
     123              :       ! Compute S*C(beta)
     124         2512 :       CALL cp_dbcsr_sm_fm_multiply(matrix_s(1)%matrix, c_beta, scb, nbeta)
     125              :       ! Compute C(alpha)^T*S*C(beta)
     126         2512 :       CALL parallel_gemm('T', 'N', nalpha, nbeta, nao, one, c_alpha, scb, zero, asb)
     127              :       ! square the matrix elements
     128         2512 :       CALL cp_fm_schur_product(asb, asb, asb2)
     129              :       ! occupation number weighting
     130         2512 :       CALL cp_fm_row_scale(asb2, occa)
     131         2512 :       CALL cp_fm_column_scale(asb2, occb)
     132         2512 :       s2 = cp_fm_sumall(asb2)
     133         2512 :       s_square = s_square_ideal + nelb - s2
     134         2512 :       CALL cp_fm_release(scb)
     135         2512 :       CALL cp_fm_release(asb)
     136         2512 :       CALL cp_fm_release(asb2)
     137              : 
     138         2512 :       CALL timestop(handle)
     139              : 
     140         2512 :    END SUBROUTINE compute_s_square
     141              : 
     142              : ! **************************************************************************************************
     143              : !> \brief Compute the expectation value <(\cal S)^2> of the single determinant defined by the
     144              : !>        spin up (alpha) and spin down (beta) orbitals
     145              : !> \param mosin ...
     146              : !> \param kpoints ...
     147              : !> \param matrix_s ...
     148              : !> \param s_square [out] <(\cal S)^2> including potential spin contaminations
     149              : !> \param s_square_ideal [out] Ideal value for <(\cal S)^2> without any spin contaminations
     150              : !> \par History
     151              : !>      10.2026 adapted and generalized to partial occupation and k-points
     152              : ! **************************************************************************************************
     153          226 :    SUBROUTINE compute_s_square_kp(mosin, kpoints, matrix_s, s_square, s_square_ideal)
     154              : 
     155              :       TYPE(mo_set_type), DIMENSION(:), INTENT(IN)        :: mosin
     156              :       TYPE(kpoint_type), INTENT(IN), POINTER             :: kpoints
     157              :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: matrix_s
     158              :       REAL(KIND=dp), INTENT(OUT)                         :: s_square, s_square_ideal
     159              : 
     160              :       CHARACTER(len=*), PARAMETER :: routineN = 'compute_s_square_kp'
     161              : 
     162              :       INTEGER                                            :: handle, ik, ikp, kplocal, nalpha, nao, &
     163              :                                                             nbeta
     164              :       INTEGER, DIMENSION(2)                              :: kp_range
     165          226 :       INTEGER, DIMENSION(:, :), POINTER                  :: kp_dist
     166              :       LOGICAL                                            :: wfn_real
     167              :       REAL(KIND=dp)                                      :: nela, nelb, s2, wkp
     168          226 :       REAL(KIND=dp), CONTIGUOUS, DIMENSION(:), POINTER   :: occa, occb
     169              :       TYPE(cp_cfm_type)                                  :: casb, casb2, cscb, csmat
     170              :       TYPE(cp_cfm_type), POINTER                         :: c_alpha, c_beta
     171              :       TYPE(cp_fm_pool_type), POINTER                     :: ao_ao_fm_pool
     172              :       TYPE(cp_fm_struct_type), POINTER                   :: fm_struct, matrix_struct
     173              :       TYPE(cp_fm_type)                                   :: asb, asb2, fmlocal, rsmat, scb
     174              :       TYPE(cp_fm_type), POINTER                          :: r_alpha, r_beta
     175              :       TYPE(kpoint_env_type), POINTER                     :: kp
     176          226 :       TYPE(mo_set_type), DIMENSION(:), POINTER           :: mos
     177              :       TYPE(mp_para_env_type), POINTER                    :: para_env_inter_kp, para_env_kp
     178              :       TYPE(neighbor_list_set_p_type), DIMENSION(:), &
     179          226 :          POINTER                                         :: sab_nl
     180              :       TYPE(qs_matrix_pools_type), POINTER                :: mpools
     181          904 :       TYPE(rskp_prepared_transform_type)                 :: transform
     182              : 
     183          226 :       CALL timeset(routineN, handle)
     184              : 
     185              :       CALL get_kpoint_info(kpoints, use_real_wfn=wfn_real, kp_range=kp_range, &
     186              :                            kp_dist=kp_dist, sab_nl=sab_nl, para_env_kp=para_env_kp, &
     187          226 :                            para_env_inter_kp=para_env_inter_kp)
     188          226 :       CPASSERT(ASSOCIATED(sab_nl))
     189              : 
     190          226 :       kplocal = kp_range(2) - kp_range(1) + 1
     191              : 
     192              :       ! fm pools to be used within a kpoint group
     193          226 :       CALL get_kpoint_info(kpoints, mpools=mpools)
     194          226 :       CALL mpools_get(mpools, ao_ao_fm_pool=ao_ao_fm_pool)
     195          226 :       CALL fm_pool_create_fm(ao_ao_fm_pool, fmlocal)
     196          226 :       CALL cp_fm_get_info(fmlocal, matrix_struct=matrix_struct)
     197          226 :       IF (wfn_real) THEN
     198            2 :          CALL cp_fm_create(rsmat, matrix_struct)
     199              :       ELSE
     200          224 :          CALL cp_cfm_create(csmat, matrix_struct)
     201              :       END IF
     202          226 :       CALL fm_pool_give_back_fm(ao_ao_fm_pool, fmlocal)
     203              : 
     204              :       ! The overlap has one spin-free real-space row shared by both spins.
     205          226 :       CALL get_mo_set(mo_set=mosin(1), nao=nao)
     206              :       CALL rskp_transform_prepare(transform, matrix_s, [1], sab_nl, kpoints%cell_to_index, &
     207              :                                   matrix_struct, kpoints%blacs_env_all%para_env, para_env_kp, &
     208              :                                   para_env_inter_kp, kpoints%rskp_transform, kpoints%xkp, &
     209          226 :                                   kpoints%nkp_grid, kp_dist)
     210              : 
     211          226 :       s2 = 0.0_dp
     212         1670 :       DO ik = 1, SIZE(kpoints%xkp, 2)
     213         1444 :          ikp = ik - kp_range(1) + 1
     214         1444 :          IF (wfn_real) THEN
     215            2 :             CALL rskp_transform_evaluate(transform, 1, ik, kpoints%xkp, fm_re=rsmat)
     216              :          ELSE
     217         1442 :             CALL rskp_transform_evaluate(transform, 1, ik, kpoints%xkp, cfm=csmat)
     218              :          END IF
     219         1444 :          IF (ikp < 1 .OR. ikp > kplocal) CYCLE
     220              : 
     221          762 :          kp => kpoints%kp_env(ikp)%kpoint_env
     222          762 :          CALL get_kpoint_env(kp, wkp=wkp, mos=mos)
     223          762 :          CALL get_mo_set(mo_set=mos(1), occupation_numbers=occa)
     224          762 :          CALL get_mo_set(mo_set=mos(2), occupation_numbers=occb)
     225              : 
     226          762 :          IF (wfn_real) THEN
     227            2 :             CALL get_mo_set(mo_set=mos(1), mo_coeff=r_alpha, nmo=nalpha)
     228            2 :             CALL get_mo_set(mo_set=mos(2), mo_coeff=r_beta, nmo=nbeta)
     229            2 :             CALL cp_fm_get_info(r_beta, matrix_struct=fm_struct)
     230            2 :             CALL cp_fm_create(scb, fm_struct, set_zero=.TRUE.)
     231            2 :             CALL cp_fm_create(asb, fm_struct, nrow=nalpha, ncol=nbeta, set_zero=.TRUE.)
     232            2 :             CALL cp_fm_create(asb2, fm_struct, nrow=nalpha, ncol=nbeta, set_zero=.TRUE.)
     233            2 :             CALL parallel_gemm('N', 'N', nao, nbeta, nao, one, rsmat, r_beta, zero, scb)
     234            2 :             CALL parallel_gemm('T', 'N', nalpha, nbeta, nao, one, r_alpha, scb, zero, asb)
     235            2 :             CALL cp_fm_schur_product(asb, asb, asb2)
     236            2 :             CALL cp_fm_release(scb)
     237            2 :             CALL cp_fm_release(asb)
     238              :          ELSE
     239          760 :             CALL get_mo_set(mo_set=mos(1), cmo_coeff=c_alpha, nmo=nalpha)
     240          760 :             CALL get_mo_set(mo_set=mos(2), cmo_coeff=c_beta, nmo=nbeta)
     241          760 :             CALL cp_cfm_get_info(c_beta, matrix_struct=fm_struct)
     242          760 :             CALL cp_cfm_create(cscb, fm_struct, set_zero=.TRUE.)
     243          760 :             CALL cp_cfm_create(casb, fm_struct, nrow=nalpha, ncol=nbeta, set_zero=.TRUE.)
     244          760 :             CALL cp_cfm_create(casb2, fm_struct, nrow=nalpha, ncol=nbeta, set_zero=.TRUE.)
     245          760 :             CALL cp_fm_create(asb2, fm_struct, nrow=nalpha, ncol=nbeta, set_zero=.TRUE.)
     246          760 :             CALL parallel_gemm('N', 'N', nao, nbeta, nao, z_one, csmat, c_beta, z_zero, cscb)
     247          760 :             CALL parallel_gemm('C', 'N', nalpha, nbeta, nao, z_one, c_alpha, cscb, z_zero, casb)
     248          760 :             CALL cp_cfm_schur_product_cc(casb, casb, casb2)
     249          760 :             CALL cp_cfm_to_fm(casb2, asb2)
     250          760 :             CALL cp_cfm_release(cscb)
     251          760 :             CALL cp_cfm_release(casb)
     252          760 :             CALL cp_cfm_release(casb2)
     253              :          END IF
     254          762 :          CALL cp_fm_row_scale(asb2, occa)
     255          762 :          CALL cp_fm_column_scale(asb2, occb)
     256          762 :          s2 = s2 + wkp*cp_fm_sumall(asb2)
     257         1670 :          CALL cp_fm_release(asb2)
     258              : 
     259              :       END DO
     260              : 
     261              :       !sum s2 over kp group
     262          226 :       CALL kpoints%para_env_inter_kp%sum(s2)
     263              : 
     264          226 :       ikp = 1
     265          226 :       IF (ikp <= kplocal) THEN
     266          226 :          kp => kpoints%kp_env(ikp)%kpoint_env
     267          226 :          CALL get_kpoint_env(kp, mos=mos)
     268          226 :          CALL get_mo_set(mo_set=mos(1), occupation_numbers=occa)
     269          226 :          CALL get_mo_set(mo_set=mos(2), occupation_numbers=occb)
     270         1482 :          nela = SUM(occa)
     271         1482 :          nelb = SUM(occb)
     272              :       ELSE
     273            0 :          nela = 0
     274            0 :          nelb = 0
     275              :       END IF
     276              :       !
     277          226 :       CALL kpoints%para_env_kp%max(nela)
     278          226 :       CALL kpoints%para_env_kp%max(nelb)
     279              :       !
     280          226 :       s_square_ideal = REAL((nela - nelb)*(nela - nelb + 2), KIND=dp)/4.0_dp
     281          226 :       s_square = s_square_ideal + nelb - s2
     282              : 
     283          226 :       CALL rskp_transform_release(transform)
     284              : 
     285          226 :       IF (wfn_real) THEN
     286            2 :          CALL cp_fm_release(rsmat)
     287              :       ELSE
     288          224 :          CALL cp_cfm_release(csmat)
     289              :       END IF
     290              : 
     291          226 :       CALL timestop(handle)
     292              : 
     293          452 :    END SUBROUTINE compute_s_square_kp
     294              : 
     295              : ! **************************************************************************************************
     296              : !> \brief restrains/constrains the value of s2 in a calculation
     297              : !> \param mos input
     298              : !> \param matrix_s input
     299              : !> \param mo_derivs inout if present, add the derivative of s_square wrt mos to mo_derivs
     300              : !> \param energy ...
     301              : !> \param s2_restraint_control ...
     302              : !> \param just_energy ...
     303              : !> \par History
     304              : !>      07.2004 created [ Joost VandeVondele ]
     305              : ! **************************************************************************************************
     306            0 :    SUBROUTINE s2_restraint(mos, matrix_s, mo_derivs, energy, &
     307              :                            s2_restraint_control, just_energy)
     308              : 
     309              :       TYPE(mo_set_type), DIMENSION(:), INTENT(IN)        :: mos
     310              :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: matrix_s
     311              :       TYPE(cp_fm_type), DIMENSION(:), INTENT(INOUT)      :: mo_derivs
     312              :       REAL(kind=dp)                                      :: energy
     313              :       TYPE(s2_restraint_type), POINTER                   :: s2_restraint_control
     314              :       LOGICAL                                            :: just_energy
     315              : 
     316              :       CHARACTER(len=*), PARAMETER                        :: routineN = 's2_restraint'
     317              : 
     318              :       INTEGER                                            :: handle
     319              :       REAL(kind=dp)                                      :: s_square, s_square_ideal
     320              : 
     321            0 :       CALL timeset(routineN, handle)
     322              : 
     323            0 :       SELECT CASE (s2_restraint_control%functional_form)
     324              :       CASE (do_s2_constraint)
     325            0 :          IF (just_energy) THEN
     326            0 :             CALL compute_s2_energyforce(mos, matrix_s, s_square, s_square_ideal)
     327              :          ELSE
     328              :             CALL compute_s2_energyforce(mos, matrix_s, s_square, s_square_ideal, &
     329            0 :                                         mo_derivs, s2_restraint_control%strength)
     330              :          END IF
     331            0 :          energy = s2_restraint_control%strength*(s_square - s2_restraint_control%target)
     332            0 :          s2_restraint_control%s2_order_p = s_square
     333              :       CASE (do_s2_restraint) ! not yet implemented
     334            0 :          CPABORT("do_s2_restraint for <S**2> not yet implemented")
     335              :       CASE DEFAULT
     336            0 :          CPABORT("Unknown <S**2> constraint/restraint form")
     337              :       END SELECT
     338              : 
     339            0 :       CALL timestop(handle)
     340              : 
     341            0 :    END SUBROUTINE s2_restraint
     342              : 
     343              : ! **************************************************************************************************
     344              : !> \brief Compute the expectation value <(\cal S)^2> of the single determinant defined by the
     345              : !>        spin up (alpha) and spin down (beta) orbitals
     346              : !> \param mos [in] MO set with all MO information including the alpha and beta MO coefficients
     347              : !> \param matrix_s [in] AO overlap matrix S (do not mix with the spin operator (\cal S))
     348              : !> \param s_square [out] <(\cal S)^2> including potential spin contaminations
     349              : !> \param s_square_ideal [out] Ideal value for <(\cal S)^2> without any spin contaminations
     350              : !> \param mo_derivs [inout] If present, add the derivative of s_square wrt the MOs to mo_derivs
     351              : !> \param strength [in] Strength for constraining or restraining (\cal S)^2
     352              : !> \par History
     353              : !>      07.2004 created (Joost VandeVondele)
     354              : !>      08.2021 revised (Matthias Krack, MK)
     355              : !> \note
     356              : !>      See Eqs. 2.271 and 2.272 in Modern Quantum Chemistry by A. Szabo and N. S. Ostlund
     357              : ! **************************************************************************************************
     358            0 :    SUBROUTINE compute_s2_energyforce(mos, matrix_s, s_square, s_square_ideal, mo_derivs, strength)
     359              : 
     360              :       TYPE(mo_set_type), DIMENSION(:), INTENT(IN)        :: mos
     361              :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: matrix_s
     362              :       REAL(KIND=dp), INTENT(OUT)                         :: s_square, s_square_ideal
     363              :       TYPE(cp_fm_type), DIMENSION(:), INTENT(INOUT), &
     364              :          OPTIONAL                                        :: mo_derivs
     365              :       REAL(KIND=dp), INTENT(IN), OPTIONAL                :: strength
     366              : 
     367              :       CHARACTER(len=*), PARAMETER :: routineN = 'compute_s2_energyforce'
     368              : 
     369              :       INTEGER                                            :: handle, i, j, nalpha, nao, nao_beta, &
     370              :                                                             nbeta, ncol_local, nmo_alpha, &
     371              :                                                             nmo_beta, nrow_local
     372              :       LOGICAL                                            :: has_uniform_occupation_alpha, &
     373              :                                                             has_uniform_occupation_beta
     374              :       REAL(KIND=dp)                                      :: s2
     375              :       REAL(KIND=dp), CONTIGUOUS, DIMENSION(:, :), &
     376            0 :          POINTER                                         :: local_data
     377              :       TYPE(cp_blacs_env_type), POINTER                   :: context
     378              :       TYPE(cp_fm_struct_type), POINTER                   :: fm_struct_tmp
     379              :       TYPE(cp_fm_type)                                   :: catscb, sca, scb
     380              :       TYPE(cp_fm_type), POINTER                          :: c_alpha, c_beta
     381              :       TYPE(mp_para_env_type), POINTER                    :: para_env
     382              : 
     383            0 :       CALL timeset(routineN, handle)
     384              : 
     385            0 :       NULLIFY (context)
     386            0 :       NULLIFY (fm_struct_tmp)
     387            0 :       NULLIFY (local_data)
     388            0 :       NULLIFY (para_env)
     389              : 
     390            0 :       SELECT CASE (SIZE(mos))
     391              :       CASE (1)
     392              :          ! Spin restricted case, i.e. nothing to do
     393            0 :          s_square = 0.0_dp
     394            0 :          s_square_ideal = 0.0_dp
     395              :          ! Restraining or constraining (\cal S) does not make sense
     396            0 :          CPASSERT(PRESENT(mo_derivs) .OR. PRESENT(strength))
     397              :       CASE (2)
     398            0 :          CALL get_mo_set(mo_set=mos(1), mo_coeff=c_alpha, homo=nalpha, nmo=nmo_alpha, nao=nao)
     399            0 :          CALL get_mo_set(mo_set=mos(2), mo_coeff=c_beta, homo=nbeta, nmo=nmo_beta, nao=nao_beta)
     400            0 :          CPASSERT(nao == nao_beta)
     401            0 :          has_uniform_occupation_alpha = has_uniform_occupation(mo_set=mos(1), last_mo=nalpha)
     402            0 :          has_uniform_occupation_beta = has_uniform_occupation(mo_set=mos(2), last_mo=nbeta)
     403              :          ! This makes only sense if we have uniform occupations for the alpha and beta spin MOs while
     404              :          ! ignoring potentially added MOs with zero occupation
     405            0 :          IF (has_uniform_occupation_alpha .AND. has_uniform_occupation_beta) THEN
     406              :             ! Eq. 2.272 in Modern Quantum Chemistry by A. Szabo and N. S. Ostlund
     407            0 :             s_square_ideal = REAL((nalpha - nbeta)*(nalpha - nbeta + 2), KIND=dp)/4.0_dp
     408              :             ! Create overlap matrix
     409            0 :             CALL cp_fm_get_info(c_alpha, para_env=para_env, context=context)
     410              :             CALL cp_fm_struct_create(fm_struct_tmp, para_env=para_env, context=context, &
     411            0 :                                      nrow_global=nalpha, ncol_global=nbeta)
     412              :             ! Prepare C(alpha)^T*S*C(beta)
     413            0 :             CALL cp_fm_create(catscb, fm_struct_tmp, name="C(alpha)^T*S*C(beta)")
     414            0 :             CALL cp_fm_struct_release(fm_struct_tmp)
     415              :             ! Create S*C(beta)
     416            0 :             CALL cp_fm_get_info(c_beta, matrix_struct=fm_struct_tmp)
     417            0 :             CALL cp_fm_create(scb, fm_struct_tmp, name="S*C(beta)", set_zero=.TRUE.)
     418              :             ! Compute S*C(beta)
     419            0 :             CALL cp_dbcsr_sm_fm_multiply(matrix_s(1)%matrix, c_beta, scb, nbeta)
     420              :             ! Compute C(alpha)^T*S*C(beta)
     421            0 :             CALL parallel_gemm('T', 'N', nalpha, nbeta, nao, 1.0_dp, c_alpha, scb, 0.0_dp, catscb)
     422              :             ! Eq. 2.271 in Modern Quantum Chemistry by A. Szabo and N. S. Ostlund
     423            0 :             CALL cp_fm_get_info(catscb, local_data=local_data, nrow_local=nrow_local, ncol_local=ncol_local)
     424            0 :             s2 = 0.0_dp
     425            0 :             DO j = 1, ncol_local
     426            0 :                DO i = 1, nrow_local
     427            0 :                   s2 = s2 + local_data(i, j)**2
     428              :                END DO
     429              :             END DO
     430            0 :             CALL para_env%sum(s2)
     431            0 :             s_square = s_square_ideal + nbeta - s2
     432              :             ! Only needed for constraining or restraining (\cal S)
     433            0 :             IF (PRESENT(mo_derivs)) THEN
     434            0 :                CPASSERT(SIZE(mo_derivs, 1) == 2)
     435              :                ! This gets really wrong for fractional occupations
     436            0 :                CALL get_mo_set(mo_set=mos(1), uniform_occupation=has_uniform_occupation_alpha)
     437            0 :                CPASSERT(has_uniform_occupation_alpha)
     438            0 :                CALL get_mo_set(mo_set=mos(2), uniform_occupation=has_uniform_occupation_beta)
     439            0 :                CPASSERT(has_uniform_occupation_beta)
     440              :                ! Add -strength*S*C(beta)*(C(alpha)^T*S*C(beta))^T to the alpha MO derivatives
     441            0 :                CALL parallel_gemm('N', 'T', nao, nalpha, nbeta, -strength, scb, catscb, 1.0_dp, mo_derivs(1))
     442              :                ! Create S*C(alpha)
     443            0 :                CALL cp_fm_get_info(c_alpha, matrix_struct=fm_struct_tmp)
     444            0 :                CALL cp_fm_create(sca, fm_struct_tmp, name="S*C(alpha)", set_zero=.TRUE.)
     445              :                ! Compute S*C(alpha)
     446            0 :                CALL cp_dbcsr_sm_fm_multiply(matrix_s(1)%matrix, c_alpha, sca, nalpha)
     447              :                ! Add -strength*S*C(alpha)*C(alpha)^T*S*C(beta) to the beta MO derivatives
     448            0 :                CALL parallel_gemm('N', 'N', nao, nbeta, nalpha, -strength, sca, catscb, 1.0_dp, mo_derivs(2))
     449            0 :                CALL cp_fm_release(sca)
     450              :             END IF
     451            0 :             CALL cp_fm_release(scb)
     452            0 :             CALL cp_fm_release(catscb)
     453              :          ELSE
     454            0 :             IF (.NOT. has_uniform_occupation_alpha) THEN
     455            0 :                CPHINT("The alpha orbitals (MOs) have a non-uniform occupation")
     456              :             END IF
     457            0 :             IF (.NOT. has_uniform_occupation_beta) THEN
     458            0 :                CPHINT("The beta orbitals (MOs) have a non-uniform occupation")
     459              :             END IF
     460            0 :             CPHINT("Skipping S**2 calculation")
     461              :          END IF
     462              :       CASE DEFAULT
     463              :          ! We should never get here
     464            0 :          CPABORT("Alpha, beta, what else?")
     465              :       END SELECT
     466              : 
     467            0 :       CALL timestop(handle)
     468              : 
     469            0 :    END SUBROUTINE compute_s2_energyforce
     470              : 
     471              : END MODULE s_square_methods
        

Generated by: LCOV version 2.0-1