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