LCOV - code coverage report
Current view: top level - src - qs_energy.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:2c0d679) Lines: 95.1 % 61 58
Test Date: 2026-09-25 00:58:37 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        29743 :    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        29743 :       CALL timeset(routineN, handle)
      87              : 
      88              :       ! Alternative electronic solvers must not inherit a previous SCF status.
      89        29743 :       qs_env%scf_convergence_available = .FALSE.
      90        29743 :       qs_env%scf_converged = .FALSE.
      91              : 
      92        29743 :       my_calc_forces = .FALSE.
      93        29743 :       IF (PRESENT(calc_forces)) my_calc_forces = calc_forces
      94              : 
      95        29743 :       do_consistent_energies = .FALSE.
      96        29743 :       IF (PRESENT(consistent_energies)) do_consistent_energies = consistent_energies
      97              : 
      98        29743 :       CALL qs_env_rebuild_pw_env(qs_env)
      99              : 
     100        29743 :       CALL get_qs_env(qs_env=qs_env, run_rtp=run_rtp)
     101        29743 :       IF (.NOT. run_rtp) THEN
     102              : 
     103        28515 :          NULLIFY (dft_control, energy, harris_env)
     104        28515 :          CALL qs_energies_init(qs_env, my_calc_forces)
     105              :          CALL get_qs_env(qs_env=qs_env, dft_control=dft_control, scf_control=scf_control, energy=energy, &
     106        28515 :                          harris_env=harris_env)
     107        28515 :          IF (ASSOCIATED(harris_env)) THEN
     108        28515 :             IF (harris_env%direct_density_matrix_energy .AND. my_calc_forces) THEN
     109            0 :                CPABORT("Forces are not available for the direct fitted-density-matrix energy")
     110              :             END IF
     111              :          END IF
     112              : 
     113              :          ! *** check if only overlap matrix is needed for couplings
     114        28515 :          loverlap_deltat = .FALSE.
     115        28515 :          NULLIFY (excited_state_section)
     116        28515 :          excited_state_section => section_vals_get_subs_vals(qs_env%input, "DFT%EXCITED_STATES")
     117        28515 :          CALL section_vals_get(excited_state_section, explicit=do_excited_state)
     118        28515 :          IF (do_excited_state) THEN
     119              :             CALL section_vals_val_get(excited_state_section, "OVERLAP_DELTAT", &
     120         1210 :                                       l_val=loverlap_deltat)
     121              :          END IF
     122              : 
     123              :          ! *** Perform a SCF run ***
     124        28515 :          IF (.NOT. loverlap_deltat) THEN
     125        28515 :             IF (scf_control%non_selfconsistent .AND. .NOT. scf_control%force_scf_calculation) THEN
     126         2916 :                CALL nonscf(qs_env)
     127        25599 :             ELSE IF (dft_control%qs_control%do_ls_scf) THEN
     128          690 :                CALL ls_scf(qs_env)
     129        24909 :             ELSE IF (dft_control%qs_control%do_almo_scf) THEN
     130          122 :                CALL almo_entry_scf(qs_env, calc_forces=my_calc_forces)
     131              :             ELSE
     132              :                ! current-induced forces
     133        24787 :                IF (dft_control%smeagol_control%smeagol_enabled .AND. &
     134              :                    dft_control%smeagol_control%run_type == smeagol_runtype_emtransport) THEN
     135            0 :                   dft_control%smeagol_control%emforces = my_calc_forces
     136              :                END IF
     137              : 
     138        24787 :                CALL scf(qs_env)
     139              :             END IF
     140              :          END IF
     141              : 
     142        28515 :          IF (do_consistent_energies) THEN
     143        11302 :             CALL qs_ks_update_qs_env(qs_env, calculate_forces=.FALSE., just_energy=.FALSE.)
     144              :          END IF
     145              : 
     146        28515 :          IF (.NOT. (dft_control%qs_control%do_ls_scf .OR. dft_control%qs_control%do_almo_scf)) THEN
     147              :             ! Compute MP2 energy
     148        27627 :             CALL qs_energies_mp2(qs_env, my_calc_forces)
     149              : 
     150        27627 :             IF (.NOT. ASSOCIATED(qs_env%mp2_env)) THEN
     151              :                ! do not overwrite w matrix computed by SMEAGOL (current-induced forces)
     152        26905 :                IF (.NOT. (dft_control%smeagol_control%smeagol_enabled .AND. &
     153              :                           dft_control%smeagol_control%run_type == smeagol_runtype_emtransport)) THEN
     154              :                   ! if calculate forces, time to compute the w matrix
     155        26905 :                   CALL compute_matrix_w(qs_env, my_calc_forces)
     156              :                END IF
     157              :             END IF
     158              :          END IF
     159              : 
     160              :          ! Check for energy correction
     161        28515 :          IF (qs_env%harris_method) THEN
     162           64 :             CALL harris_energy_correction(qs_env, my_calc_forces)
     163              :          END IF
     164              : 
     165              :          ! Do active space calculation
     166        28515 :          CALL active_space_main(qs_env)
     167              : 
     168              :          ! Check for energy correction
     169        28515 :          IF (qs_env%energy_correction) THEN
     170          698 :             CALL energy_correction(qs_env, ec_init=.TRUE., calculate_forces=.FALSE.)
     171              :          END IF
     172              : 
     173        28515 :          IF (.NOT. loverlap_deltat) THEN
     174              :             ! Calculate energy, response vector and some contributions to the force
     175        28515 :             CALL qs_energies_properties(qs_env, calc_forces)
     176              : 
     177              :             ! Update total energy of the selected excited state
     178        28515 :             CALL excited_state_energy(qs_env, calculate_forces=.FALSE.)
     179              :          END IF
     180              : 
     181        28515 :          IF (dft_control%tddfpt2_control%do_smearing) THEN
     182            2 :             IF (.NOT. ASSOCIATED(dft_control%tddfpt2_control%smeared_occup)) THEN
     183            0 :                CPABORT("Smearing occupation not associated.")
     184              :             END IF
     185            2 :             CALL deallocate_fermi_params(dft_control%tddfpt2_control%smeared_occup)
     186              :          END IF
     187        28515 :          IF (dft_control%qs_control%lrigpw) THEN
     188           60 :             CALL lri_print_stat(qs_env)
     189              :          END IF
     190              : 
     191              :       END IF
     192              : 
     193        29743 :       CALL timestop(handle)
     194              : 
     195        29743 :    END SUBROUTINE qs_energies
     196              : 
     197              : ! **************************************************************************************************
     198              : !> \brief Enters the mp2 part of cp2k
     199              : !> \param qs_env ...
     200              : !> \param calc_forces ...
     201              : ! **************************************************************************************************
     202              : 
     203        27627 :    SUBROUTINE qs_energies_mp2(qs_env, calc_forces)
     204              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     205              :       LOGICAL, INTENT(IN)                                :: calc_forces
     206              : 
     207              :       LOGICAL                                            :: should_stop
     208              : 
     209              :       ! Compute MP2 energy
     210              : 
     211        27627 :       IF (ASSOCIATED(qs_env%mp2_env)) THEN
     212              : 
     213              :          CALL external_control(should_stop, "MP2", target_time=qs_env%target_time, &
     214          722 :                                start_time=qs_env%start_time)
     215              : 
     216          722 :          CALL mp2_main(qs_env=qs_env, calc_forces=calc_forces)
     217              :       END IF
     218              : 
     219        27627 :    END SUBROUTINE qs_energies_mp2
     220              : 
     221              : END MODULE qs_energy
        

Generated by: LCOV version 2.0-1