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 Utility subroutine for qs energy calculation
10 : !> \par History
11 : !> 11.2016 split out from qs_energy_utils
12 : !> \author MK (29.10.2002)
13 : ! **************************************************************************************************
14 : MODULE qs_energy_init
15 : USE cp_control_types, ONLY: dft_control_type
16 : USE cp_dbcsr_api, ONLY: dbcsr_copy,&
17 : dbcsr_p_type,&
18 : dbcsr_set,&
19 : dbcsr_type
20 : USE cp_dbcsr_operations, ONLY: dbcsr_allocate_matrix_set
21 : USE efield_utils, ONLY: calculate_ecore_efield
22 : USE input_constants, ONLY: kg_tnadd_atomic,&
23 : kg_tnadd_embed,&
24 : kg_tnadd_embed_ri,&
25 : kg_tnadd_none
26 : USE input_section_types, ONLY: section_vals_type
27 : USE kg_environment, ONLY: kg_build_neighborlist,&
28 : kg_build_subsets
29 : USE kg_environment_types, ONLY: kg_environment_type
30 : USE kinds, ONLY: dp
31 : USE kpoint_methods, ONLY: kpoint_init_cell_index
32 : USE kpoint_types, ONLY: kpoint_type,&
33 : set_kpoint_info
34 : USE lri_environment_methods, ONLY: build_lri_matrices,&
35 : calculate_lri_integrals
36 : USE lri_environment_types, ONLY: lri_environment_type
37 : USE message_passing, ONLY: mp_para_env_type
38 : USE molecule_types, ONLY: molecule_of_atom,&
39 : molecule_type
40 : USE optimize_embedding_potential, ONLY: given_embed_pot
41 : USE qs_core_energies, ONLY: calculate_ecore_overlap,&
42 : calculate_ecore_self
43 : USE qs_core_hamiltonian, ONLY: build_core_hamiltonian_matrix
44 : USE qs_dftb_dispersion, ONLY: calculate_dftb_dispersion
45 : USE qs_dftb_matrices, ONLY: build_dftb_matrices
46 : USE qs_dispersion_pairpot, ONLY: calculate_dispersion_pairpot
47 : USE qs_dispersion_types, ONLY: qs_dispersion_type
48 : USE qs_energy_types, ONLY: qs_energy_type
49 : USE qs_environment_types, ONLY: get_qs_env,&
50 : qs_environment_type
51 : USE qs_external_density, ONLY: external_read_density
52 : USE qs_external_potential, ONLY: external_c_potential,&
53 : external_e_potential
54 : USE qs_gcp_method, ONLY: calculate_gcp_pairpot
55 : USE qs_gcp_types, ONLY: qs_gcp_type
56 : USE qs_ks_methods, ONLY: qs_ks_allocate_basics
57 : USE qs_ks_types, ONLY: get_ks_env,&
58 : qs_ks_env_type,&
59 : set_ks_env
60 : USE qs_neighbor_list_types, ONLY: neighbor_list_set_p_type
61 : USE qs_neighbor_lists, ONLY: build_qs_neighbor_lists
62 : USE qs_update_s_mstruct, ONLY: qs_env_update_s_mstruct
63 : USE ri_environment_methods, ONLY: build_ri_matrices
64 : USE se_core_core, ONLY: se_core_core_interaction
65 : USE se_core_matrix, ONLY: build_se_core_matrix
66 : USE tblite_interface, ONLY: build_tblite_matrices,&
67 : tb_init_ham
68 : USE xtb_matrices, ONLY: build_xtb_matrices
69 : #include "./base/base_uses.f90"
70 :
71 : IMPLICIT NONE
72 :
73 : PRIVATE
74 :
75 : ! *** Global parameters ***
76 :
77 : CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_energy_init'
78 :
79 : PUBLIC :: qs_energies_init
80 :
81 : CONTAINS
82 :
83 : ! **************************************************************************************************
84 : !> \brief Refactoring of qs_energies_scf. Driver routine for the initial
85 : !> setup and calculations for a qs energy calculation
86 : !> \param qs_env ...
87 : !> \param calc_forces ...
88 : !> \par History
89 : !> 05.2013 created [Florian Schiffmann]
90 : ! **************************************************************************************************
91 :
92 29910 : SUBROUTINE qs_energies_init(qs_env, calc_forces)
93 : TYPE(qs_environment_type), POINTER :: qs_env
94 : LOGICAL, INTENT(IN) :: calc_forces
95 :
96 : INTEGER :: img, ispin, nimg, nspin
97 : LOGICAL :: has_unit_metric, ks_is_complex, &
98 : molecule_only
99 29910 : TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_s, matrix_w
100 : TYPE(dbcsr_type), POINTER :: matrix
101 : TYPE(dft_control_type), POINTER :: dft_control
102 : TYPE(qs_ks_env_type), POINTER :: ks_env
103 :
104 29910 : NULLIFY (ks_env, matrix_w, matrix_s, dft_control)
105 :
106 29910 : CALL get_qs_env(qs_env, dft_control=dft_control, ks_env=ks_env)
107 29910 : IF (dft_control%qs_control%do_kg) THEN
108 128 : molecule_only = .TRUE.
109 128 : CALL qs_energies_init_kg(qs_env)
110 : ELSE
111 29782 : molecule_only = .FALSE.
112 : END IF
113 29910 : CALL qs_energies_init_hamiltonians(qs_env, calc_forces, molecule_only)
114 29910 : CALL get_ks_env(ks_env, complex_ks=ks_is_complex)
115 29910 : CALL qs_ks_allocate_basics(qs_env, is_complex=ks_is_complex)
116 :
117 : ! if need forces allocate energy weighted density matrices
118 29910 : CALL get_qs_env(qs_env, has_unit_metric=has_unit_metric)
119 29910 : IF (calc_forces .AND. .NOT. has_unit_metric) THEN
120 : CALL get_qs_env(qs_env, &
121 : ks_env=ks_env, &
122 7177 : matrix_s_kp=matrix_s)
123 7177 : nspin = dft_control%nspins
124 7177 : nimg = dft_control%nimages
125 7177 : matrix => matrix_s(1, 1)%matrix
126 7177 : CALL dbcsr_allocate_matrix_set(matrix_w, nspin, nimg)
127 15206 : DO ispin = 1, nspin
128 83187 : DO img = 1, nimg
129 67981 : ALLOCATE (matrix_w(ispin, img)%matrix)
130 67981 : CALL dbcsr_copy(matrix_w(ispin, img)%matrix, matrix, name="W MATRIX")
131 76010 : CALL dbcsr_set(matrix_w(ispin, img)%matrix, 0.0_dp)
132 : END DO
133 : END DO
134 7177 : CALL set_ks_env(ks_env, matrix_w_kp=matrix_w)
135 : END IF
136 :
137 29910 : END SUBROUTINE qs_energies_init
138 :
139 : ! **************************************************************************************************
140 : !> \brief Refactoring of qs_energies_scf. Puts initialization of the Kim-Gordon
141 : !> settings into separate subroutine
142 : !> \param qs_env ...
143 : !> \par History
144 : !> 05.2013 created [Florian Schiffmann]
145 : ! **************************************************************************************************
146 :
147 128 : SUBROUTINE qs_energies_init_kg(qs_env)
148 : TYPE(qs_environment_type), POINTER :: qs_env
149 :
150 : CHARACTER(len=*), PARAMETER :: routineN = 'qs_energies_init_kg'
151 :
152 : INTEGER :: handle, isubset, natom
153 : TYPE(dft_control_type), POINTER :: dft_control
154 : TYPE(kg_environment_type), POINTER :: kg_env
155 128 : TYPE(molecule_type), DIMENSION(:), POINTER :: molecule_set
156 : TYPE(mp_para_env_type), POINTER :: para_env
157 : TYPE(neighbor_list_set_p_type), DIMENSION(:), &
158 128 : POINTER :: soo_list, soo_list1
159 :
160 128 : CALL timeset(routineN, handle)
161 :
162 128 : CALL get_qs_env(qs_env, dft_control=dft_control, para_env=para_env)
163 128 : CPASSERT(dft_control%qs_control%do_kg)
164 :
165 128 : kg_env => qs_env%kg_env
166 :
167 : ! get the set of molecules
168 128 : CALL get_qs_env(qs_env=qs_env, molecule_set=molecule_set, natom=natom)
169 128 : kg_env%natom = natom
170 : ! store set of molecules in kg_env
171 128 : kg_env%molecule_set => molecule_set
172 : ! build the (new) full neighborlist
173 128 : CALL kg_build_neighborlist(qs_env, sab_orb=kg_env%sab_orb_full)
174 :
175 128 : IF (.NOT. ALLOCATED(kg_env%atom_to_molecule)) THEN
176 240 : ALLOCATE (kg_env%atom_to_molecule(natom))
177 : ! get the mapping from atoms to molecules
178 80 : CALL molecule_of_atom(molecule_set, atom_to_mol=kg_env%atom_to_molecule)
179 : END IF
180 :
181 216 : SELECT CASE (kg_env%tnadd_method)
182 : CASE (kg_tnadd_embed)
183 : ! allocate the subset list
184 88 : IF (.NOT. ASSOCIATED(kg_env%subset_of_mol)) THEN
185 168 : ALLOCATE (kg_env%subset_of_mol(SIZE(molecule_set)))
186 : END IF
187 : !
188 88 : CALL kg_build_subsets(kg_env, para_env)
189 : !
190 278 : DO isubset = 1, kg_env%nsubsets
191 : ! build the (new) molecular neighborlist of the current subset
192 : CALL kg_build_neighborlist(qs_env, sab_orb=kg_env%subset(isubset)%sab_orb, molecular=.TRUE., &
193 278 : subset_of_mol=kg_env%subset_of_mol, current_subset=isubset)
194 : END DO
195 : CASE (kg_tnadd_embed_ri)
196 : ! should be deleted as soon as atomic grids work
197 : ! allocate the subset list
198 6 : IF (.NOT. ASSOCIATED(kg_env%subset_of_mol)) THEN
199 18 : ALLOCATE (kg_env%subset_of_mol(SIZE(molecule_set)))
200 : END IF
201 : !
202 6 : CALL kg_build_subsets(kg_env, para_env)
203 : !
204 18 : DO isubset = 1, kg_env%nsubsets
205 : ! build the (new) molecular neighborlist of the current subset
206 : CALL kg_build_neighborlist(qs_env, sab_orb=kg_env%subset(isubset)%sab_orb, molecular=.TRUE., &
207 18 : subset_of_mol=kg_env%subset_of_mol, current_subset=isubset)
208 : END DO
209 : !
210 : ! LRI neighborlist
211 6 : NULLIFY (soo_list)
212 6 : CALL kg_build_neighborlist(qs_env, sab_orb=soo_list, molecular=.TRUE.)
213 6 : kg_env%lri_env%soo_list => soo_list
214 6 : CALL calculate_lri_integrals(kg_env%lri_env, qs_env)
215 6 : IF (qs_env%energy_correction) THEN
216 0 : NULLIFY (soo_list1)
217 0 : CALL kg_build_neighborlist(qs_env, sab_orb=soo_list1, molecular=.TRUE.)
218 0 : kg_env%lri_env1%soo_list => soo_list1
219 0 : CALL calculate_lri_integrals(kg_env%lri_env1, qs_env)
220 : END IF
221 :
222 : ! Atomic grids
223 : CASE (kg_tnadd_atomic)
224 : ! build the A-C list for the nonadditive kinetic energy potential
225 26 : CALL kg_build_neighborlist(qs_env, sac_kin=kg_env%sac_kin)
226 : CASE (kg_tnadd_none)
227 : ! nothing to do
228 : CASE DEFAULT
229 128 : CPABORT("KG:TNADD METHOD")
230 : END SELECT
231 :
232 128 : CALL timestop(handle)
233 :
234 128 : END SUBROUTINE qs_energies_init_kg
235 :
236 : ! **************************************************************************************************
237 : !> \brief Refactoring of qs_energies_scf. Moves computation of the different
238 : !> core hamiltonians into separate subroutine
239 : !> \param qs_env QS environment
240 : !> \param calc_forces Calculate forces
241 : !> \param molecule_only restrict neighbor list to molecules
242 : !> \par History
243 : !> 05.2013 created [Florian Schiffmann]
244 : !> 08.2014 Kpoints [JGH]
245 : ! **************************************************************************************************
246 :
247 29910 : SUBROUTINE qs_energies_init_hamiltonians(qs_env, calc_forces, molecule_only)
248 : TYPE(qs_environment_type), POINTER :: qs_env
249 : LOGICAL, INTENT(IN) :: calc_forces
250 : LOGICAL :: molecule_only
251 :
252 : CHARACTER(len=*), PARAMETER :: routineN = 'qs_energies_init_hamiltonians'
253 :
254 : INTEGER :: handle
255 : LOGICAL :: do_kpoints
256 : TYPE(dft_control_type), POINTER :: dft_control
257 : TYPE(kpoint_type), POINTER :: kpoints
258 : TYPE(lri_environment_type), POINTER :: lri_env
259 : TYPE(mp_para_env_type), POINTER :: para_env
260 : TYPE(neighbor_list_set_p_type), DIMENSION(:), &
261 29910 : POINTER :: sab_nl, sab_nl_nosym
262 : TYPE(qs_dispersion_type), POINTER :: dispersion_env
263 : TYPE(qs_energy_type), POINTER :: energy
264 : TYPE(qs_gcp_type), POINTER :: gcp_env
265 : TYPE(section_vals_type), POINTER :: input
266 :
267 29910 : CALL timeset(routineN, handle)
268 :
269 : CALL get_qs_env(qs_env, &
270 : input=input, &
271 : dft_control=dft_control, &
272 : para_env=para_env, &
273 : kpoints=kpoints, &
274 29910 : do_kpoints=do_kpoints)
275 :
276 : ! create neighbor lists for standard use in QS
277 : CALL build_qs_neighbor_lists(qs_env, para_env, molecular=molecule_only, &
278 29910 : force_env_section=input)
279 :
280 : ! calculate cell index for k-point calculations
281 29910 : IF (do_kpoints) THEN
282 4126 : CALL get_qs_env(qs_env, sab_kp=sab_nl, sab_kp_nosym=sab_nl_nosym)
283 4126 : CALL kpoint_init_cell_index(kpoints, sab_nl, para_env, dft_control%nimages)
284 4126 : CALL set_kpoint_info(kpoints, sab_nl_nosym=sab_nl_nosym)
285 : END IF
286 29910 : IF (dft_control%qs_control%cdft) THEN
287 362 : IF (.NOT. (dft_control%qs_control%cdft_control%external_control)) THEN
288 326 : dft_control%qs_control%cdft_control%need_pot = .TRUE.
289 : END IF
290 362 : IF (ASSOCIATED(dft_control%qs_control%cdft_control%group)) THEN
291 : ! In case CDFT weight function was built beforehand (in mixed force_eval)
292 362 : IF (ASSOCIATED(dft_control%qs_control%cdft_control%group(1)%weight)) THEN
293 122 : dft_control%qs_control%cdft_control%need_pot = .FALSE.
294 : END IF
295 : END IF
296 : END IF
297 :
298 : ! Calculate the overlap and the core Hamiltonian integral matrix
299 29910 : IF (dft_control%qs_control%semi_empirical) THEN
300 : CALL build_se_core_matrix(qs_env=qs_env, para_env=para_env, &
301 4356 : calculate_forces=.FALSE.)
302 4356 : CALL qs_env_update_s_mstruct(qs_env)
303 4356 : CALL se_core_core_interaction(qs_env, para_env, calculate_forces=.FALSE.)
304 4356 : CALL get_qs_env(qs_env=qs_env, dispersion_env=dispersion_env, energy=energy)
305 4356 : CALL calculate_dispersion_pairpot(qs_env, dispersion_env, energy%dispersion, calc_forces)
306 25554 : ELSE IF (dft_control%qs_control%dftb) THEN
307 : CALL build_dftb_matrices(qs_env=qs_env, para_env=para_env, &
308 3420 : calculate_forces=.FALSE.)
309 : CALL calculate_dftb_dispersion(qs_env=qs_env, para_env=para_env, &
310 3420 : calculate_forces=.FALSE.)
311 3420 : CALL qs_env_update_s_mstruct(qs_env)
312 22134 : ELSE IF (dft_control%qs_control%xtb) THEN
313 8244 : IF (dft_control%qs_control%xtb_control%do_tblite) THEN
314 2616 : CALL tb_init_ham(qs_env, calc_forces)
315 2616 : CALL build_tblite_matrices(qs_env=qs_env, calculate_forces=.FALSE.)
316 : ELSE
317 5628 : CALL build_xtb_matrices(qs_env=qs_env, calculate_forces=.FALSE.)
318 : END IF
319 8244 : CALL qs_env_update_s_mstruct(qs_env)
320 : ELSE
321 13890 : CALL build_core_hamiltonian_matrix(qs_env=qs_env, calculate_forces=.FALSE.)
322 13890 : CALL qs_env_update_s_mstruct(qs_env)
323 13890 : CALL calculate_ecore_self(qs_env)
324 13890 : CALL calculate_ecore_efield(qs_env, calculate_forces=.FALSE.)
325 13890 : CALL calculate_ecore_overlap(qs_env, para_env, calculate_forces=.FALSE.)
326 : !swap external_e_potential before external_c_potential, to ensure
327 : !that external potential on grid is loaded before calculating energy of cores
328 13890 : CALL external_e_potential(qs_env)
329 13890 : IF (.NOT. dft_control%qs_control%gapw) THEN
330 11500 : CALL external_c_potential(qs_env, calculate_forces=.FALSE.)
331 : END IF
332 : ! LRIGPW/RIGPW matrices
333 13890 : IF (dft_control%qs_control%lrigpw) THEN
334 60 : CALL get_qs_env(qs_env=qs_env, lri_env=lri_env)
335 60 : CALL build_lri_matrices(lri_env, qs_env)
336 13830 : ELSE IF (dft_control%qs_control%rigpw) THEN
337 2 : CALL get_qs_env(qs_env=qs_env, lri_env=lri_env)
338 2 : CALL build_ri_matrices(lri_env, qs_env, calculate_forces=.FALSE.)
339 : END IF
340 :
341 : ! ZMP addition to read external density
342 13890 : CALL external_read_density(qs_env)
343 :
344 : ! Add possible pair potential dispersion energy - Evaluate first so we can print
345 : ! energy info at the end of the SCF
346 13890 : CALL get_qs_env(qs_env=qs_env, dispersion_env=dispersion_env, energy=energy)
347 13890 : CALL calculate_dispersion_pairpot(qs_env, dispersion_env, energy%dispersion, calc_forces)
348 : ! Add possible pair potential gCP energy - Evaluate first so we can print
349 : ! energy info at the end of the SCF
350 13890 : CALL get_qs_env(qs_env=qs_env, gcp_env=gcp_env, energy=energy)
351 13890 : IF (ASSOCIATED(gcp_env)) THEN
352 13890 : CALL calculate_gcp_pairpot(qs_env, gcp_env, energy%gcp, calc_forces)
353 : END IF
354 :
355 : END IF
356 : ! Embedding potential
357 29910 : IF (dft_control%qs_control%dfet_embedded) THEN
358 2 : dft_control%apply_embed_pot = .TRUE.
359 2 : CALL given_embed_pot(qs_env)
360 : END IF
361 : ! Matrix embedding potential
362 29910 : IF (dft_control%qs_control%dmfet_embedded) THEN
363 0 : dft_control%apply_dmfet_pot = .TRUE.
364 : END IF
365 :
366 29910 : CALL timestop(handle)
367 :
368 29910 : END SUBROUTINE qs_energies_init_hamiltonians
369 :
370 : END MODULE qs_energy_init
|