LCOV - code coverage report
Current view: top level - src - qs_energy.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:24d69ee) Lines: 94.9 % 59 56
Test Date: 2026-09-03 07:32:15 Functions: 100.0 % 2 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 Perform a QUICKSTEP wavefunction optimization (single point)
      10              : !> \par History
      11              : !>      none
      12              : !> \author MK (29.10.2002)
      13              : ! **************************************************************************************************
      14              : MODULE qs_energy
      15              :    USE almo_scf,                        ONLY: almo_entry_scf
      16              :    USE cp_control_types,                ONLY: dft_control_type
      17              :    USE cp_external_control,             ONLY: external_control
      18              :    USE dm_ls_scf,                       ONLY: ls_scf
      19              :    USE energy_corrections,              ONLY: energy_correction
      20              :    USE excited_states,                  ONLY: excited_state_energy
      21              :    USE input_constants,                 ONLY: smeagol_runtype_emtransport
      22              :    USE input_section_types,             ONLY: section_vals_get,&
      23              :                                               section_vals_get_subs_vals,&
      24              :                                               section_vals_type,&
      25              :                                               section_vals_val_get
      26              :    USE lri_environment_methods,         ONLY: lri_print_stat
      27              :    USE mp2,                             ONLY: mp2_main
      28              :    USE qs_active_space_methods,         ONLY: active_space_main
      29              :    USE qs_energy_init,                  ONLY: qs_energies_init
      30              :    USE qs_energy_types,                 ONLY: qs_energy_type
      31              :    USE qs_energy_utils,                 ONLY: qs_energies_properties
      32              :    USE qs_environment_methods,          ONLY: qs_env_rebuild_pw_env
      33              :    USE qs_environment_types,            ONLY: get_qs_env,&
      34              :                                               qs_environment_type
      35              :    USE qs_harris_methods,               ONLY: harris_energy_correction
      36              :    USE qs_harris_types,                 ONLY: harris_type
      37              :    USE qs_ks_methods,                   ONLY: qs_ks_update_qs_env
      38              :    USE qs_matrix_w,                     ONLY: compute_matrix_w
      39              :    USE qs_nonscf,                       ONLY: nonscf
      40              :    USE qs_scf,                          ONLY: scf
      41              :    USE qs_tddfpt2_smearing_methods,     ONLY: deallocate_fermi_params
      42              :    USE scf_control_types,               ONLY: scf_control_type
      43              : #include "./base/base_uses.f90"
      44              : 
      45              :    IMPLICIT NONE
      46              : 
      47              :    PRIVATE
      48              : 
      49              : ! *** Global parameters ***
      50              : 
      51              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_energy'
      52              : 
      53              :    PUBLIC :: qs_energies
      54              : 
      55              : CONTAINS
      56              : 
      57              : ! **************************************************************************************************
      58              : !> \brief   Driver routine for QUICKSTEP single point wavefunction optimization.
      59              : !> \param qs_env ...
      60              : !> \param consistent_energies ...
      61              : !> \param calc_forces ...
      62              : !> \date    29.10.2002
      63              : !> \par History
      64              : !>          - consistent_energies option added (25.08.2005, TdK)
      65              : !>          - introduced driver for energy in order to properly decide between
      66              : !>            SCF or RTP (fschiff 02.09)
      67              : !> \author  MK
      68              : !> \version 1.0
      69              : ! **************************************************************************************************
      70        29383 :    SUBROUTINE qs_energies(qs_env, consistent_energies, calc_forces)
      71              :       TYPE(qs_environment_type), POINTER                 :: qs_env
      72              :       LOGICAL, INTENT(IN), OPTIONAL                      :: consistent_energies, calc_forces
      73              : 
      74              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'qs_energies'
      75              : 
      76              :       INTEGER                                            :: handle
      77              :       LOGICAL                                            :: do_consistent_energies, &
      78              :                                                             do_excited_state, loverlap_deltat, &
      79              :                                                             my_calc_forces, run_rtp
      80              :       TYPE(dft_control_type), POINTER                    :: dft_control
      81              :       TYPE(harris_type), POINTER                         :: harris_env
      82              :       TYPE(qs_energy_type), POINTER                      :: energy
      83              :       TYPE(scf_control_type), POINTER                    :: scf_control
      84              :       TYPE(section_vals_type), POINTER                   :: excited_state_section
      85              : 
      86        29383 :       CALL timeset(routineN, handle)
      87              : 
      88        29383 :       my_calc_forces = .FALSE.
      89        29383 :       IF (PRESENT(calc_forces)) my_calc_forces = calc_forces
      90              : 
      91        29383 :       do_consistent_energies = .FALSE.
      92        29383 :       IF (PRESENT(consistent_energies)) do_consistent_energies = consistent_energies
      93              : 
      94        29383 :       CALL qs_env_rebuild_pw_env(qs_env)
      95              : 
      96        29383 :       CALL get_qs_env(qs_env=qs_env, run_rtp=run_rtp)
      97        29383 :       IF (.NOT. run_rtp) THEN
      98              : 
      99        28155 :          NULLIFY (dft_control, energy, harris_env)
     100        28155 :          CALL qs_energies_init(qs_env, my_calc_forces)
     101              :          CALL get_qs_env(qs_env=qs_env, dft_control=dft_control, scf_control=scf_control, energy=energy, &
     102        28155 :                          harris_env=harris_env)
     103        28155 :          IF (ASSOCIATED(harris_env)) THEN
     104        28155 :             IF (harris_env%direct_density_matrix_energy .AND. my_calc_forces) THEN
     105            0 :                CPABORT("Forces are not available for the direct fitted-density-matrix energy")
     106              :             END IF
     107              :          END IF
     108              : 
     109              :          ! *** check if only overlap matrix is needed for couplings
     110        28155 :          loverlap_deltat = .FALSE.
     111        28155 :          NULLIFY (excited_state_section)
     112        28155 :          excited_state_section => section_vals_get_subs_vals(qs_env%input, "DFT%EXCITED_STATES")
     113        28155 :          CALL section_vals_get(excited_state_section, explicit=do_excited_state)
     114        28155 :          IF (do_excited_state) THEN
     115              :             CALL section_vals_val_get(excited_state_section, "OVERLAP_DELTAT", &
     116         1176 :                                       l_val=loverlap_deltat)
     117              :          END IF
     118              : 
     119              :          ! *** Perform a SCF run ***
     120        28155 :          IF (.NOT. loverlap_deltat) THEN
     121        28155 :             IF (scf_control%non_selfconsistent .AND. .NOT. scf_control%force_scf_calculation) THEN
     122         2916 :                CALL nonscf(qs_env)
     123        25239 :             ELSE IF (dft_control%qs_control%do_ls_scf) THEN
     124          688 :                CALL ls_scf(qs_env)
     125        24551 :             ELSE IF (dft_control%qs_control%do_almo_scf) THEN
     126          122 :                CALL almo_entry_scf(qs_env, calc_forces=my_calc_forces)
     127              :             ELSE
     128              :                ! current-induced forces
     129        24429 :                IF (dft_control%smeagol_control%smeagol_enabled .AND. &
     130              :                    dft_control%smeagol_control%run_type == smeagol_runtype_emtransport) THEN
     131            0 :                   dft_control%smeagol_control%emforces = my_calc_forces
     132              :                END IF
     133              : 
     134        24429 :                CALL scf(qs_env)
     135              :             END IF
     136              :          END IF
     137              : 
     138        28155 :          IF (do_consistent_energies) THEN
     139        11302 :             CALL qs_ks_update_qs_env(qs_env, calculate_forces=.FALSE., just_energy=.FALSE.)
     140              :          END IF
     141              : 
     142        28155 :          IF (.NOT. (dft_control%qs_control%do_ls_scf .OR. dft_control%qs_control%do_almo_scf)) THEN
     143              :             ! Compute MP2 energy
     144        27269 :             CALL qs_energies_mp2(qs_env, my_calc_forces)
     145              : 
     146        27269 :             IF (.NOT. ASSOCIATED(qs_env%mp2_env)) THEN
     147              :                ! do not overwrite w matrix computed by SMEAGOL (current-induced forces)
     148        26553 :                IF (.NOT. (dft_control%smeagol_control%smeagol_enabled .AND. &
     149              :                           dft_control%smeagol_control%run_type == smeagol_runtype_emtransport)) THEN
     150              :                   ! if calculate forces, time to compute the w matrix
     151        26553 :                   CALL compute_matrix_w(qs_env, my_calc_forces)
     152              :                END IF
     153              :             END IF
     154              :          END IF
     155              : 
     156              :          ! Check for energy correction
     157        28155 :          IF (qs_env%harris_method) THEN
     158           64 :             CALL harris_energy_correction(qs_env, my_calc_forces)
     159              :          END IF
     160              : 
     161              :          ! Do active space calculation
     162        28155 :          CALL active_space_main(qs_env)
     163              : 
     164              :          ! Check for energy correction
     165        28155 :          IF (qs_env%energy_correction) THEN
     166          698 :             CALL energy_correction(qs_env, ec_init=.TRUE., calculate_forces=.FALSE.)
     167              :          END IF
     168              : 
     169        28155 :          IF (.NOT. loverlap_deltat) THEN
     170              :             ! Calculate energy, response vector and some contributions to the force
     171        28155 :             CALL qs_energies_properties(qs_env, calc_forces)
     172              : 
     173              :             ! Update total energy of the selected excited state
     174        28155 :             CALL excited_state_energy(qs_env, calculate_forces=.FALSE.)
     175              :          END IF
     176              : 
     177        28155 :          IF (dft_control%tddfpt2_control%do_smearing) THEN
     178            2 :             IF (.NOT. ASSOCIATED(dft_control%tddfpt2_control%smeared_occup)) THEN
     179            0 :                CPABORT("Smearing occupation not associated.")
     180              :             END IF
     181            2 :             CALL deallocate_fermi_params(dft_control%tddfpt2_control%smeared_occup)
     182              :          END IF
     183        28155 :          IF (dft_control%qs_control%lrigpw) THEN
     184           60 :             CALL lri_print_stat(qs_env)
     185              :          END IF
     186              : 
     187              :       END IF
     188              : 
     189        29383 :       CALL timestop(handle)
     190              : 
     191        29383 :    END SUBROUTINE qs_energies
     192              : 
     193              : ! **************************************************************************************************
     194              : !> \brief Enters the mp2 part of cp2k
     195              : !> \param qs_env ...
     196              : !> \param calc_forces ...
     197              : ! **************************************************************************************************
     198              : 
     199        27269 :    SUBROUTINE qs_energies_mp2(qs_env, calc_forces)
     200              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     201              :       LOGICAL, INTENT(IN)                                :: calc_forces
     202              : 
     203              :       LOGICAL                                            :: should_stop
     204              : 
     205              :       ! Compute MP2 energy
     206              : 
     207        27269 :       IF (ASSOCIATED(qs_env%mp2_env)) THEN
     208              : 
     209              :          CALL external_control(should_stop, "MP2", target_time=qs_env%target_time, &
     210          716 :                                start_time=qs_env%start_time)
     211              : 
     212          716 :          CALL mp2_main(qs_env=qs_env, calc_forces=calc_forces)
     213              :       END IF
     214              : 
     215        27269 :    END SUBROUTINE qs_energies_mp2
     216              : 
     217              : END MODULE qs_energy
        

Generated by: LCOV version 2.0-1