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