Line data Source code
1 : !--------------------------------------------------------------------------------------------------!
2 : ! CP2K: A general program to perform molecular dynamics simulations !
3 : ! Copyright 2000-2025 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_utils, ONLY: harris_energy_correction
36 : USE qs_ks_methods, ONLY: qs_ks_update_qs_env
37 : USE qs_matrix_w, ONLY: compute_matrix_w
38 : USE qs_nonscf, ONLY: nonscf
39 : USE qs_scf, ONLY: scf
40 : USE qs_tddfpt2_smearing_methods, ONLY: deallocate_fermi_params
41 : USE scf_control_types, ONLY: scf_control_type
42 : #include "./base/base_uses.f90"
43 :
44 : IMPLICIT NONE
45 :
46 : PRIVATE
47 :
48 : ! *** Global parameters ***
49 :
50 : CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_energy'
51 :
52 : PUBLIC :: qs_energies
53 :
54 : CONTAINS
55 :
56 : ! **************************************************************************************************
57 : !> \brief Driver routine for QUICKSTEP single point wavefunction optimization.
58 : !> \param qs_env ...
59 : !> \param consistent_energies ...
60 : !> \param calc_forces ...
61 : !> \date 29.10.2002
62 : !> \par History
63 : !> - consistent_energies option added (25.08.2005, TdK)
64 : !> - introduced driver for energy in order to properly decide between
65 : !> SCF or RTP (fschiff 02.09)
66 : !> \author MK
67 : !> \version 1.0
68 : ! **************************************************************************************************
69 22001 : SUBROUTINE qs_energies(qs_env, consistent_energies, calc_forces)
70 : TYPE(qs_environment_type), POINTER :: qs_env
71 : LOGICAL, INTENT(IN), OPTIONAL :: consistent_energies, calc_forces
72 :
73 : CHARACTER(len=*), PARAMETER :: routineN = 'qs_energies'
74 :
75 : INTEGER :: handle
76 : LOGICAL :: do_consistent_energies, &
77 : do_excited_state, loverlap_deltat, &
78 : my_calc_forces, run_rtp
79 : TYPE(dft_control_type), POINTER :: dft_control
80 : TYPE(qs_energy_type), POINTER :: energy
81 : TYPE(scf_control_type), POINTER :: scf_control
82 : TYPE(section_vals_type), POINTER :: excited_state_section
83 :
84 22001 : CALL timeset(routineN, handle)
85 :
86 22001 : my_calc_forces = .FALSE.
87 22001 : IF (PRESENT(calc_forces)) my_calc_forces = calc_forces
88 :
89 22001 : do_consistent_energies = .FALSE.
90 22001 : IF (PRESENT(consistent_energies)) do_consistent_energies = consistent_energies
91 :
92 22001 : CALL qs_env_rebuild_pw_env(qs_env)
93 :
94 22001 : CALL get_qs_env(qs_env=qs_env, run_rtp=run_rtp)
95 22001 : IF (.NOT. run_rtp) THEN
96 :
97 20779 : NULLIFY (dft_control, energy)
98 20779 : CALL qs_energies_init(qs_env, my_calc_forces)
99 20779 : CALL get_qs_env(qs_env=qs_env, dft_control=dft_control, scf_control=scf_control, energy=energy)
100 :
101 : ! *** check if only overlap matrix is needed for couplings
102 20779 : loverlap_deltat = .FALSE.
103 20779 : NULLIFY (excited_state_section)
104 20779 : excited_state_section => section_vals_get_subs_vals(qs_env%input, "DFT%EXCITED_STATES")
105 20779 : CALL section_vals_get(excited_state_section, explicit=do_excited_state)
106 20779 : IF (do_excited_state) THEN
107 : CALL section_vals_val_get(excited_state_section, "OVERLAP_DELTAT", &
108 916 : l_val=loverlap_deltat)
109 : END IF
110 :
111 : ! *** Perform a SCF run ***
112 20779 : IF (.NOT. loverlap_deltat) THEN
113 20779 : IF (scf_control%non_selfconsistent .AND. .NOT. scf_control%force_scf_calculation) THEN
114 2206 : CALL nonscf(qs_env)
115 18573 : ELSE IF (dft_control%qs_control%do_ls_scf) THEN
116 598 : CALL ls_scf(qs_env)
117 17975 : ELSE IF (dft_control%qs_control%do_almo_scf) THEN
118 116 : CALL almo_entry_scf(qs_env, calc_forces=my_calc_forces)
119 : ELSE
120 : ! current-induced forces
121 17859 : IF (dft_control%smeagol_control%smeagol_enabled .AND. &
122 : dft_control%smeagol_control%run_type == smeagol_runtype_emtransport) THEN
123 0 : dft_control%smeagol_control%emforces = my_calc_forces
124 : END IF
125 :
126 17859 : CALL scf(qs_env)
127 : END IF
128 : END IF
129 :
130 20779 : IF (do_consistent_energies) THEN
131 6242 : CALL qs_ks_update_qs_env(qs_env, calculate_forces=.FALSE., just_energy=.FALSE.)
132 : END IF
133 :
134 20779 : IF (.NOT. (dft_control%qs_control%do_ls_scf .OR. dft_control%qs_control%do_almo_scf)) THEN
135 : ! Compute MP2 energy
136 19999 : CALL qs_energies_mp2(qs_env, my_calc_forces)
137 :
138 19999 : IF (.NOT. ASSOCIATED(qs_env%mp2_env)) THEN
139 : ! do not overwrite w matrix computed by SMEAGOL (current-induced forces)
140 19305 : IF (.NOT. (dft_control%smeagol_control%smeagol_enabled .AND. &
141 : dft_control%smeagol_control%run_type == smeagol_runtype_emtransport)) THEN
142 : ! if calculate forces, time to compute the w matrix
143 19305 : CALL compute_matrix_w(qs_env, my_calc_forces)
144 : END IF
145 : END IF
146 : END IF
147 :
148 : ! Check for energy correction
149 20779 : IF (qs_env%harris_method) THEN
150 30 : CALL harris_energy_correction(qs_env, my_calc_forces)
151 : END IF
152 :
153 : ! Do active space calculation
154 20779 : CALL active_space_main(qs_env)
155 :
156 : ! Check for energy correction
157 20779 : IF (qs_env%energy_correction) THEN
158 530 : CALL energy_correction(qs_env, ec_init=.TRUE., calculate_forces=.FALSE.)
159 : END IF
160 :
161 20779 : IF (.NOT. loverlap_deltat) THEN
162 20779 : CALL qs_energies_properties(qs_env, calc_forces)
163 :
164 20779 : CALL excited_state_energy(qs_env, calculate_forces=.FALSE.)
165 : END IF
166 :
167 20779 : IF (dft_control%tddfpt2_control%do_smearing) THEN
168 2 : IF (.NOT. ASSOCIATED(dft_control%tddfpt2_control%smeared_occup)) &
169 0 : CPABORT("Smearing occupation not associated.")
170 2 : CALL deallocate_fermi_params(dft_control%tddfpt2_control%smeared_occup)
171 : END IF
172 20779 : IF (dft_control%qs_control%lrigpw) THEN
173 58 : CALL lri_print_stat(qs_env)
174 : END IF
175 :
176 : END IF
177 :
178 22001 : CALL timestop(handle)
179 :
180 22001 : END SUBROUTINE qs_energies
181 :
182 : ! **************************************************************************************************
183 : !> \brief Enters the mp2 part of cp2k
184 : !> \param qs_env ...
185 : !> \param calc_forces ...
186 : ! **************************************************************************************************
187 :
188 19999 : SUBROUTINE qs_energies_mp2(qs_env, calc_forces)
189 : TYPE(qs_environment_type), POINTER :: qs_env
190 : LOGICAL, INTENT(IN) :: calc_forces
191 :
192 : LOGICAL :: should_stop
193 :
194 : ! Compute MP2 energy
195 :
196 19999 : IF (ASSOCIATED(qs_env%mp2_env)) THEN
197 :
198 : CALL external_control(should_stop, "MP2", target_time=qs_env%target_time, &
199 694 : start_time=qs_env%start_time)
200 :
201 694 : CALL mp2_main(qs_env=qs_env, calc_forces=calc_forces)
202 : END IF
203 :
204 19999 : END SUBROUTINE qs_energies_mp2
205 :
206 : END MODULE qs_energy
|