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 routines for qs_scf
10 : ! **************************************************************************************************
11 : MODULE qs_scf_initialization
12 : USE atom_kind_orbitals, ONLY: calculate_atomic_orbitals
13 : USE atomic_kind_types, ONLY: atomic_kind_type
14 : USE bibliography, ONLY: Hu2010,&
15 : cite_reference
16 : USE cp_control_types, ONLY: dft_control_type
17 : USE cp_dbcsr_api, ONLY: dbcsr_create,&
18 : dbcsr_init_p,&
19 : dbcsr_p_type,&
20 : dbcsr_set,&
21 : dbcsr_type,&
22 : dbcsr_type_no_symmetry,&
23 : dbcsr_type_symmetric
24 : USE cp_dbcsr_cp2k_link, ONLY: cp_dbcsr_alloc_block_from_nbl
25 : USE cp_dbcsr_operations, ONLY: copy_dbcsr_to_fm,&
26 : copy_fm_to_dbcsr,&
27 : cp_dbcsr_m_by_n_from_row_template,&
28 : cp_dbcsr_sm_fm_multiply,&
29 : dbcsr_allocate_matrix_set
30 : USE cp_dbcsr_output, ONLY: write_fm_with_basis_info
31 : USE cp_fm_basic_linalg, ONLY: cp_fm_column_scale,&
32 : cp_fm_row_scale,&
33 : cp_fm_transpose,&
34 : cp_fm_triangular_invert
35 : USE cp_fm_cholesky, ONLY: cp_fm_cholesky_decompose
36 : USE cp_fm_diag, ONLY: FM_DIAG_TYPE_CUSOLVER,&
37 : choose_eigv_solver,&
38 : cp_fm_power,&
39 : cusolver_n_min,&
40 : diag_type,&
41 : direct_generalized_diagonalization
42 : USE cp_fm_pool_types, ONLY: cp_fm_pool_p_type,&
43 : fm_pool_get_el_struct
44 : USE cp_fm_struct, ONLY: cp_fm_struct_create,&
45 : cp_fm_struct_get,&
46 : cp_fm_struct_release,&
47 : cp_fm_struct_type
48 : USE cp_fm_types, ONLY: cp_fm_create,&
49 : cp_fm_get_info,&
50 : cp_fm_release,&
51 : cp_fm_set_all,&
52 : cp_fm_to_fm,&
53 : cp_fm_to_fm_triangular,&
54 : cp_fm_type
55 : USE cp_log_handling, ONLY: cp_get_default_logger,&
56 : cp_logger_type,&
57 : cp_to_string
58 : USE cp_output_handling, ONLY: cp_p_file,&
59 : cp_print_key_finished_output,&
60 : cp_print_key_should_output,&
61 : cp_print_key_unit_nr
62 : USE dft_plus_u, ONLY: lowdin_kp_init,&
63 : lowdin_kp_initial_density
64 : USE hairy_probes, ONLY: AO_boundaries
65 : USE input_constants, ONLY: &
66 : broy_mix, cholesky_dbcsr, cholesky_inverse, cholesky_off, diag_block_davidson, &
67 : diag_block_krylov, diag_filter_matrix, diag_ot, diag_standard, diag_update_method_adiis, &
68 : direct_p_mix, external_density_guess, gfn2xtb, kerker_mix, modified_broy_mix, &
69 : multisec_mix, new_pulay_mix, no_mix, ot2cdft, outer_scf_none, plus_u_lowdin, &
70 : plus_u_tensorial, pulay_mix, smeagol_runtype_emtransport, wfi_frozen_method_nr, &
71 : wfi_ps_method_nr, wfi_use_guess_method_nr
72 : USE input_section_types, ONLY: section_vals_get_subs_vals,&
73 : section_vals_type,&
74 : section_vals_val_get
75 : USE kinds, ONLY: dp
76 : USE kpoint_types, ONLY: get_kpoint_info,&
77 : kpoint_clear_scf_smat,&
78 : kpoint_type
79 : USE memory_utilities, ONLY: reallocate
80 : USE message_passing, ONLY: mp_para_env_type
81 : USE parallel_gemm_api, ONLY: parallel_gemm
82 : USE particle_types, ONLY: particle_type
83 : USE pw_types, ONLY: pw_c1d_gs_type
84 : USE qmmm_image_charge, ONLY: conditional_calc_image_matrix
85 : USE qs_block_davidson_types, ONLY: block_davidson_allocate,&
86 : block_davidson_env_create
87 : USE qs_cdft_opt_types, ONLY: cdft_opt_type_copy
88 : USE qs_density_mixing_types, ONLY: direct_mixing_nr,&
89 : mixing_storage_create,&
90 : mixing_storage_release,&
91 : no_mixing_nr
92 : USE qs_environment_types, ONLY: get_qs_env,&
93 : qs_environment_type,&
94 : set_qs_env
95 : USE qs_external_density, ONLY: read_scf_guess_density
96 : USE qs_fb_distribution_methods, ONLY: fb_distribution_build
97 : USE qs_fb_env_methods, ONLY: fb_env_build_atomic_halos,&
98 : fb_env_build_rcut_auto,&
99 : fb_env_read_input,&
100 : fb_env_write_info
101 : USE qs_fb_env_types, ONLY: fb_env_create,&
102 : fb_env_has_data
103 : USE qs_harris_types, ONLY: harris_type
104 : USE qs_harris_utils, ONLY: harris_density_update
105 : USE qs_initial_guess, ONLY: calculate_first_density_matrix
106 : USE qs_kind_types, ONLY: get_qs_kind,&
107 : qs_kind_type,&
108 : set_qs_kind
109 : USE qs_ks_types, ONLY: qs_ks_did_change
110 : USE qs_matrix_pools, ONLY: mpools_get
111 : USE qs_mixing_utils, ONLY: charge_mixing_init,&
112 : mixing_allocate,&
113 : mixing_init
114 : USE qs_mo_occupation, ONLY: set_mo_occupation
115 : USE qs_mo_types, ONLY: get_mo_set,&
116 : init_mo_set,&
117 : mo_set_type,&
118 : set_mo_set
119 : USE qs_neighbor_list_types, ONLY: neighbor_list_set_p_type
120 : USE qs_outer_scf, ONLY: outer_loop_extrapolate,&
121 : outer_loop_switch,&
122 : outer_loop_variables_count
123 : USE qs_rho_atom_types, ONLY: rho_atom_type
124 : USE qs_rho_methods, ONLY: duplicate_rho_type,&
125 : qs_rho_update_rho
126 : USE qs_rho_types, ONLY: qs_rho_create,&
127 : qs_rho_get,&
128 : qs_rho_type
129 : USE qs_scf_diagonalization, ONLY: diag_kp_smat,&
130 : diag_subspace_allocate
131 : USE qs_scf_lanczos, ONLY: krylov_workspace_allocate
132 : USE qs_scf_output, ONLY: qs_scf_initial_info
133 : USE qs_scf_types, ONLY: &
134 : block_davidson_diag_method_nr, block_krylov_diag_method_nr, diag_subspace_env_create, &
135 : filter_matrix_diag_method_nr, general_diag_method_nr, krylov_settings_create, &
136 : ot_diag_method_nr, ot_method_nr, qs_scf_env_type, scf_env_create, smeagol_method_nr, &
137 : special_diag_method_nr
138 : USE qs_wf_history_methods, ONLY: reorthogonalize_vectors,&
139 : wfi_extrapolate,&
140 : wfi_get_method_label,&
141 : wfi_update
142 : USE scf_control_types, ONLY: scf_control_type
143 : USE xas_env_types, ONLY: xas_environment_type
144 : USE xas_restart, ONLY: xas_initialize_rho
145 : #include "./base/base_uses.f90"
146 :
147 : IMPLICIT NONE
148 :
149 : PRIVATE
150 :
151 : CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_scf_initialization'
152 :
153 : PUBLIC:: qs_scf_env_initialize, qs_scf_env_init_basic
154 :
155 : CONTAINS
156 :
157 : ! **************************************************************************************************
158 : !> \brief initializes input parameters if needed or restores values from
159 : !> previous runs to fill scf_env with the values required for scf
160 : !> \param qs_env the qs_environment where to perform the scf procedure
161 : !> \param scf_env ...
162 : !> \param scf_control ...
163 : !> \param scf_section ...
164 : ! **************************************************************************************************
165 27679 : SUBROUTINE qs_scf_env_initialize(qs_env, scf_env, scf_control, scf_section)
166 : TYPE(qs_environment_type), POINTER :: qs_env
167 : TYPE(qs_scf_env_type), POINTER :: scf_env
168 : TYPE(scf_control_type), OPTIONAL, POINTER :: scf_control
169 : TYPE(section_vals_type), OPTIONAL, POINTER :: scf_section
170 :
171 : INTEGER :: ip, np
172 27679 : TYPE(atomic_kind_type), POINTER :: atomic_kind_set(:)
173 : TYPE(dft_control_type), POINTER :: dft_control
174 27679 : TYPE(mo_set_type), DIMENSION(:), POINTER :: mos
175 27679 : TYPE(particle_type), POINTER :: particle_set(:)
176 27679 : TYPE(qs_kind_type), POINTER :: qs_kind_set(:)
177 : TYPE(scf_control_type), POINTER :: my_scf_control
178 : TYPE(section_vals_type), POINTER :: dft_section, input, my_scf_section
179 :
180 27679 : CALL get_qs_env(qs_env, input=input, dft_control=dft_control)
181 :
182 : !Initialize Hairy Probe calculation
183 27679 : IF (dft_control%hairy_probes .EQV. .TRUE.) THEN
184 : CALL get_qs_env(qs_env, &
185 : mos=mos, &
186 : atomic_kind_set=atomic_kind_set, &
187 : qs_kind_set=qs_kind_set, &
188 4 : particle_set=particle_set)
189 4 : np = SIZE(dft_control%probe)
190 12 : DO ip = 1, np
191 : CALL AO_boundaries(probe=dft_control%probe(ip), atomic_kind_set=atomic_kind_set, qs_kind_set=qs_kind_set, &
192 12 : particle_set=particle_set, nAO=mos(1)%nao) !FIX THIS!
193 : END DO
194 : END IF
195 :
196 27679 : IF (PRESENT(scf_control)) THEN
197 86 : my_scf_control => scf_control
198 : ELSE
199 27593 : CALL get_qs_env(qs_env, scf_control=my_scf_control)
200 : END IF
201 :
202 27679 : dft_section => section_vals_get_subs_vals(input, "DFT")
203 27679 : IF (PRESENT(scf_section)) THEN
204 86 : my_scf_section => scf_section
205 : ELSE
206 27593 : my_scf_section => section_vals_get_subs_vals(dft_section, "SCF")
207 : END IF
208 :
209 27679 : CALL qs_scf_ensure_scf_env(qs_env, scf_env)
210 :
211 27679 : CALL section_vals_val_get(my_scf_section, "CHOLESKY", i_val=scf_env%cholesky_method)
212 :
213 27679 : CALL qs_scf_ensure_mos(qs_env)
214 :
215 : ! set flags for diagonalization
216 : CALL qs_scf_ensure_diagonalization(scf_env, my_scf_section, qs_env, &
217 27679 : my_scf_control, qs_env%has_unit_metric)
218 : ! set parameters for mixing/DIIS during scf
219 27679 : CALL qs_scf_ensure_mixing(my_scf_control, my_scf_section, scf_env, dft_control)
220 :
221 27679 : CALL qs_scf_ensure_work_matrices(qs_env, scf_env)
222 :
223 27679 : CALL qs_scf_ensure_mixing_store(qs_env, scf_env, my_scf_control)
224 :
225 : ! Initialize outer loop variables: handle CDFT and regular outer loop separately
226 27679 : IF (dft_control%qs_control%cdft) THEN
227 : CALL qs_scf_ensure_cdft_loop_vars(qs_env, scf_env, dft_control, &
228 362 : scf_control=my_scf_control)
229 : ELSE
230 27317 : CALL qs_scf_ensure_outer_loop_vars(scf_env, my_scf_control)
231 : END IF
232 :
233 27679 : CALL init_scf_run(scf_env, qs_env, my_scf_section, my_scf_control)
234 :
235 27679 : END SUBROUTINE qs_scf_env_initialize
236 :
237 : ! **************************************************************************************************
238 : !> \brief initializes input parameters if needed for non-scf calclulations using diagonalization
239 : !> \param qs_env the qs_environment where to perform the scf procedure
240 : !> \param scf_env ...
241 : ! **************************************************************************************************
242 14 : SUBROUTINE qs_scf_env_init_basic(qs_env, scf_env)
243 : TYPE(qs_environment_type), POINTER :: qs_env
244 : TYPE(qs_scf_env_type), POINTER :: scf_env
245 :
246 : TYPE(dft_control_type), POINTER :: dft_control
247 : TYPE(scf_control_type), POINTER :: scf_control
248 : TYPE(section_vals_type), POINTER :: dft_section, input, scf_section
249 :
250 14 : CALL get_qs_env(qs_env, input=input, dft_control=dft_control)
251 :
252 14 : CALL get_qs_env(qs_env, scf_control=scf_control)
253 14 : dft_section => section_vals_get_subs_vals(input, "DFT")
254 14 : scf_section => section_vals_get_subs_vals(dft_section, "SCF")
255 :
256 14 : CALL qs_scf_ensure_scf_env(qs_env, scf_env)
257 :
258 14 : CALL section_vals_val_get(scf_section, "CHOLESKY", i_val=scf_env%cholesky_method)
259 14 : scf_control%use_diag = .TRUE.
260 14 : scf_control%diagonalization%method = diag_standard
261 :
262 14 : CALL qs_scf_ensure_mos(qs_env)
263 :
264 : ! set flags for diagonalization
265 : CALL qs_scf_ensure_diagonalization(scf_env, scf_section, qs_env, &
266 14 : scf_control, qs_env%has_unit_metric)
267 14 : CALL qs_scf_ensure_work_matrices(qs_env, scf_env)
268 :
269 14 : CALL init_scf_run(scf_env, qs_env, scf_section, scf_control)
270 :
271 14 : END SUBROUTINE qs_scf_env_init_basic
272 :
273 : ! **************************************************************************************************
274 : !> \brief makes sure scf_env is allocated (might already be from before)
275 : !> in case it is present the g-space mixing storage is reset
276 : !> \param qs_env ...
277 : !> \param scf_env ...
278 : ! **************************************************************************************************
279 27693 : SUBROUTINE qs_scf_ensure_scf_env(qs_env, scf_env)
280 : TYPE(qs_environment_type), POINTER :: qs_env
281 : TYPE(qs_scf_env_type), POINTER :: scf_env
282 :
283 27693 : TYPE(pw_c1d_gs_type), DIMENSION(:), POINTER :: rho_g
284 : TYPE(qs_rho_type), POINTER :: rho
285 :
286 27693 : NULLIFY (rho_g)
287 :
288 27693 : IF (.NOT. ASSOCIATED(scf_env)) THEN ! i.e. for MD this is associated on the second step (it so seems)
289 8267 : ALLOCATE (scf_env)
290 8267 : CALL scf_env_create(scf_env)
291 : ELSE
292 : ! Reallocate mixing store, if the g space grid (cell) has changed
293 19982 : SELECT CASE (scf_env%mixing_method)
294 : CASE (kerker_mix, pulay_mix, broy_mix, modified_broy_mix, multisec_mix, new_pulay_mix)
295 19426 : IF (ASSOCIATED(scf_env%mixing_store)) THEN
296 : ! The current mixing_store data structure does not allow for an unique
297 : ! grid comparison, but the probability that the 1d lengths of the old and
298 : ! the new grid are accidentily equal is rather low
299 556 : CALL get_qs_env(qs_env, rho=rho)
300 556 : CALL qs_rho_get(rho, rho_g=rho_g)
301 556 : IF (ASSOCIATED(scf_env%mixing_store%rhoin)) THEN
302 422 : IF (SIZE(rho_g(1)%pw_grid%gsq) /= SIZE(scf_env%mixing_store%rhoin(1)%cc)) THEN
303 36 : CALL mixing_storage_release(scf_env%mixing_store)
304 36 : DEALLOCATE (scf_env%mixing_store)
305 : END IF
306 : END IF
307 : END IF
308 : END SELECT
309 : END IF
310 :
311 27693 : END SUBROUTINE qs_scf_ensure_scf_env
312 :
313 : ! **************************************************************************************************
314 : !> \brief performs allocation of outer SCF variables
315 : !> \param scf_env the SCF environment which contains the outer SCF variables
316 : !> \param scf_control control settings for the outer SCF loop
317 : !> \param nvar (optional) set number of outer SCF variables externally if CDFT SCF is active
318 : ! **************************************************************************************************
319 27679 : SUBROUTINE qs_scf_ensure_outer_loop_vars(scf_env, scf_control, nvar)
320 : TYPE(qs_scf_env_type), POINTER :: scf_env
321 : TYPE(scf_control_type), POINTER :: scf_control
322 : INTEGER, OPTIONAL :: nvar
323 :
324 : INTEGER :: nhistory, nvariables
325 :
326 27679 : IF (scf_control%outer_scf%have_scf) THEN
327 4455 : nhistory = scf_control%outer_scf%max_scf + 1
328 4455 : IF (PRESENT(nvar)) THEN
329 362 : IF (nvar > 0) THEN
330 : nvariables = nvar
331 : ELSE
332 0 : nvariables = outer_loop_variables_count(scf_control)
333 : END IF
334 : ELSE
335 4093 : nvariables = outer_loop_variables_count(scf_control)
336 : END IF
337 17820 : ALLOCATE (scf_env%outer_scf%variables(nvariables, nhistory))
338 13365 : ALLOCATE (scf_env%outer_scf%count(nhistory))
339 84501 : scf_env%outer_scf%count = 0
340 13365 : ALLOCATE (scf_env%outer_scf%gradient(nvariables, nhistory))
341 13365 : ALLOCATE (scf_env%outer_scf%energy(nhistory))
342 : END IF
343 :
344 27679 : END SUBROUTINE qs_scf_ensure_outer_loop_vars
345 :
346 : ! **************************************************************************************************
347 : !> \brief performs allocation of CDFT SCF variables
348 : !> \param qs_env the qs_env where to perform the allocation
349 : !> \param scf_env the currently active scf_env
350 : !> \param dft_control the dft_control that holds the cdft_control type
351 : !> \param scf_control the currently active scf_control
352 : ! **************************************************************************************************
353 362 : SUBROUTINE qs_scf_ensure_cdft_loop_vars(qs_env, scf_env, dft_control, scf_control)
354 : TYPE(qs_environment_type), POINTER :: qs_env
355 : TYPE(qs_scf_env_type), POINTER :: scf_env
356 : TYPE(dft_control_type), POINTER :: dft_control
357 : TYPE(scf_control_type), POINTER :: scf_control
358 :
359 : INTEGER :: nhistory, nvariables
360 : LOGICAL :: do_kpoints
361 362 : REAL(KIND=dp), DIMENSION(:, :), POINTER :: gradient_history, outer_scf_history, &
362 362 : variable_history
363 :
364 362 : NULLIFY (outer_scf_history, gradient_history, variable_history)
365 362 : CALL get_qs_env(qs_env=qs_env, do_kpoints=do_kpoints)
366 : ! Test kpoints
367 362 : IF (do_kpoints) THEN
368 0 : CPABORT("CDFT calculation not possible with kpoints")
369 : END IF
370 : ! Check that OUTER_SCF section in DFT&SCF is active
371 : ! This section must always be active to facilitate
372 : ! switching of the CDFT and SCF control parameters in outer_loop_switch
373 362 : IF (.NOT. scf_control%outer_scf%have_scf) THEN
374 0 : CPABORT("Section SCF&OUTER_SCF must be active for CDFT calculations.")
375 : END IF
376 : ! Initialize CDFT and outer_loop variables (constraint settings active in scf_control)
377 362 : IF (dft_control%qs_control%cdft_control%constraint_control%have_scf) THEN
378 362 : nhistory = dft_control%qs_control%cdft_control%constraint_control%max_scf + 1
379 362 : IF (scf_control%outer_scf%type /= outer_scf_none) THEN
380 : nvariables = outer_loop_variables_count(scf_control, &
381 62 : dft_control%qs_control%cdft_control)
382 : ELSE
383 : ! First iteration: scf_control has not yet been updated
384 300 : nvariables = SIZE(dft_control%qs_control%cdft_control%target)
385 : END IF
386 1448 : ALLOCATE (dft_control%qs_control%cdft_control%constraint%variables(nvariables, nhistory))
387 1086 : ALLOCATE (dft_control%qs_control%cdft_control%constraint%count(nhistory))
388 2474 : dft_control%qs_control%cdft_control%constraint%count = 0
389 1086 : ALLOCATE (dft_control%qs_control%cdft_control%constraint%gradient(nvariables, nhistory))
390 1086 : ALLOCATE (dft_control%qs_control%cdft_control%constraint%energy(nhistory))
391 362 : CALL qs_scf_ensure_outer_loop_vars(scf_env, scf_control, nvariables)
392 : END IF
393 : ! Executed only on first call (OT settings active in scf_control)
394 : ! Save OT settings and constraint initial values in CDFT control
395 : ! Then switch to constraint outer_scf settings for proper initialization of history
396 362 : IF (scf_control%outer_scf%have_scf) THEN
397 362 : IF (scf_control%outer_scf%type == outer_scf_none) THEN
398 300 : dft_control%qs_control%cdft_control%ot_control%have_scf = .TRUE.
399 300 : dft_control%qs_control%cdft_control%ot_control%max_scf = scf_control%outer_scf%max_scf
400 300 : dft_control%qs_control%cdft_control%ot_control%eps_scf = scf_control%outer_scf%eps_scf
401 300 : dft_control%qs_control%cdft_control%ot_control%step_size = scf_control%outer_scf%step_size
402 300 : dft_control%qs_control%cdft_control%ot_control%type = scf_control%outer_scf%type
403 300 : dft_control%qs_control%cdft_control%ot_control%optimizer = scf_control%outer_scf%optimizer
404 300 : dft_control%qs_control%cdft_control%ot_control%diis_buffer_length = scf_control%outer_scf%diis_buffer_length
405 300 : dft_control%qs_control%cdft_control%ot_control%bisect_trust_count = scf_control%outer_scf%bisect_trust_count
406 : CALL cdft_opt_type_copy(dft_control%qs_control%cdft_control%ot_control%cdft_opt_control, &
407 300 : scf_control%outer_scf%cdft_opt_control)
408 : ! In case constraint and OT extrapolation orders are different, make sure to use former
409 300 : nvariables = SIZE(dft_control%qs_control%cdft_control%target)
410 : IF (scf_control%outer_scf%extrapolation_order /= &
411 : dft_control%qs_control%cdft_control%constraint_control%extrapolation_order &
412 300 : .OR. nvariables /= 1) THEN
413 262 : DEALLOCATE (qs_env%outer_scf_history)
414 262 : DEALLOCATE (qs_env%gradient_history)
415 262 : DEALLOCATE (qs_env%variable_history)
416 262 : nhistory = dft_control%qs_control%cdft_control%constraint_control%extrapolation_order
417 1048 : ALLOCATE (outer_scf_history(nvariables, nhistory))
418 786 : ALLOCATE (gradient_history(nvariables, 2))
419 1362 : gradient_history = 0.0_dp
420 524 : ALLOCATE (variable_history(nvariables, 2))
421 1362 : variable_history = 0.0_dp
422 : CALL set_qs_env(qs_env, outer_scf_history=outer_scf_history, &
423 262 : gradient_history=gradient_history, variable_history=variable_history)
424 : END IF
425 300 : CALL outer_loop_switch(scf_env, scf_control, dft_control%qs_control%cdft_control, ot2cdft)
426 : END IF
427 : END IF
428 :
429 362 : END SUBROUTINE qs_scf_ensure_cdft_loop_vars
430 :
431 : ! **************************************************************************************************
432 : !> \brief Allocate the candidate density produced by diagonalization.
433 : !> \param qs_env ...
434 : !> \param candidate Candidate density matrix set.
435 : !> \param nspins Number of spin channels.
436 : !>
437 : !> This workspace belongs to the generic diagonalization SCF driver. It is
438 : !> also used by density-mixing methods, but allocating it does not imply
439 : !> that a density mixer is active.
440 : ! **************************************************************************************************
441 12 : SUBROUTINE qs_scf_allocate_candidate_density(qs_env, candidate, nspins)
442 : TYPE(qs_environment_type), POINTER :: qs_env
443 : TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: candidate
444 : INTEGER, INTENT(IN) :: nspins
445 :
446 : INTEGER :: ic, ispin, nimg
447 12 : TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_s
448 : TYPE(dbcsr_type), POINTER :: refmatrix
449 : TYPE(dft_control_type), POINTER :: dft_control
450 : TYPE(neighbor_list_set_p_type), DIMENSION(:), &
451 12 : POINTER :: sab_orb
452 :
453 12 : NULLIFY (dft_control, matrix_s, refmatrix, sab_orb)
454 12 : CALL get_qs_env(qs_env=qs_env, matrix_s_kp=matrix_s, sab_orb=sab_orb, dft_control=dft_control)
455 :
456 12 : IF (ASSOCIATED(candidate)) RETURN
457 :
458 12 : refmatrix => matrix_s(1, 1)%matrix
459 12 : nimg = dft_control%nimages
460 12 : CALL dbcsr_allocate_matrix_set(candidate, nspins, nimg)
461 566 : DO ic = 1, nimg
462 1602 : DO ispin = 1, nspins
463 1036 : ALLOCATE (candidate(ispin, ic)%matrix)
464 : CALL dbcsr_create(matrix=candidate(ispin, ic)%matrix, template=refmatrix, &
465 1036 : name="SCF DENSITY", matrix_type=dbcsr_type_symmetric)
466 1036 : CALL cp_dbcsr_alloc_block_from_nbl(candidate(ispin, ic)%matrix, sab_orb)
467 1590 : CALL dbcsr_set(candidate(ispin, ic)%matrix, 0.0_dp)
468 : END DO
469 : END DO
470 :
471 12 : END SUBROUTINE qs_scf_allocate_candidate_density
472 :
473 : ! **************************************************************************************************
474 : !> \brief performs allocation of the mixing storage
475 : !> \param qs_env ...
476 : !> \param scf_env ...
477 : !> \param scf_control ...
478 : ! **************************************************************************************************
479 27679 : SUBROUTINE qs_scf_ensure_mixing_store(qs_env, scf_env, scf_control)
480 : TYPE(qs_environment_type), POINTER :: qs_env
481 : TYPE(qs_scf_env_type), POINTER :: scf_env
482 : TYPE(scf_control_type), POINTER :: scf_control
483 :
484 : TYPE(dft_control_type), POINTER :: dft_control
485 :
486 27679 : NULLIFY (dft_control)
487 27679 : CALL get_qs_env(qs_env=qs_env, dft_control=dft_control)
488 :
489 27679 : IF (scf_control%diagonalization%update_method == diag_update_method_adiis) THEN
490 12 : CALL qs_scf_allocate_candidate_density(qs_env, scf_env%p_mix_new, dft_control%nspins)
491 27667 : ELSE IF (scf_env%mixing_method > 0) THEN
492 : CALL mixing_allocate(qs_env, scf_env%mixing_method, scf_env%p_mix_new, &
493 : scf_env%p_delta, dft_control%nspins, &
494 20988 : scf_env%mixing_store)
495 : ELSE
496 6679 : NULLIFY (scf_env%p_mix_new)
497 : END IF
498 :
499 27679 : END SUBROUTINE qs_scf_ensure_mixing_store
500 :
501 : ! **************************************************************************************************
502 : !> \brief Performs allocation of the SCF work matrices
503 : !> In case of kpoints we probably don't need most of these matrices,
504 : !> maybe we have to initialize some matrices in the fm_pool in kpoints
505 : !> \param qs_env ...
506 : !> \param scf_env ...
507 : ! **************************************************************************************************
508 83079 : SUBROUTINE qs_scf_ensure_work_matrices(qs_env, scf_env)
509 :
510 : TYPE(qs_environment_type), POINTER :: qs_env
511 : TYPE(qs_scf_env_type), POINTER :: scf_env
512 :
513 : CHARACTER(LEN=*), PARAMETER :: routineN = 'qs_scf_ensure_work_matrices'
514 :
515 : INTEGER :: handle, is, nao, nrow_block, nw
516 : LOGICAL :: do_kpoints
517 27693 : TYPE(cp_fm_pool_p_type), DIMENSION(:), POINTER :: ao_mo_fm_pools
518 : TYPE(cp_fm_struct_type), POINTER :: ao_ao_fmstruct, ao_mo_fmstruct
519 27693 : TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_s
520 : TYPE(dbcsr_type), POINTER :: ref_matrix
521 : TYPE(dft_control_type), POINTER :: dft_control
522 27693 : TYPE(mo_set_type), DIMENSION(:), POINTER :: mos
523 : TYPE(scf_control_type), POINTER :: scf_control
524 :
525 27693 : CALL timeset(routineN, handle)
526 :
527 27693 : NULLIFY (ao_mo_fm_pools, ao_mo_fmstruct, ao_ao_fmstruct, dft_control, matrix_s, mos)
528 :
529 : CALL get_qs_env(qs_env=qs_env, &
530 : dft_control=dft_control, &
531 : matrix_s_kp=matrix_s, &
532 : mos=mos, &
533 : scf_control=scf_control, &
534 27693 : do_kpoints=do_kpoints)
535 27693 : CALL mpools_get(qs_env%mpools, ao_mo_fm_pools=ao_mo_fm_pools)
536 :
537 : ! create an ao_ao parallel matrix structure
538 27693 : ao_mo_fmstruct => fm_pool_get_el_struct(ao_mo_fm_pools(1)%pool)
539 27693 : CALL cp_fm_struct_get(ao_mo_fmstruct, nrow_block=nrow_block)
540 27693 : CALL get_mo_set(mos(1), nao=nao)
541 : CALL cp_fm_struct_create(fmstruct=ao_ao_fmstruct, &
542 : nrow_block=nrow_block, &
543 : ncol_block=nrow_block, &
544 : nrow_global=nao, &
545 : ncol_global=nao, &
546 27693 : template_fmstruct=ao_mo_fmstruct)
547 :
548 27693 : IF ((scf_env%method /= ot_method_nr) .AND. &
549 : (scf_env%method /= block_davidson_diag_method_nr)) THEN
550 20964 : IF (.NOT. ASSOCIATED(scf_env%scf_work1)) THEN
551 18864 : nw = dft_control%nspins
552 18864 : IF (do_kpoints) nw = 4
553 87592 : ALLOCATE (scf_env%scf_work1(nw))
554 49864 : DO is = 1, SIZE(scf_env%scf_work1)
555 : CALL cp_fm_create(scf_env%scf_work1(is), &
556 : matrix_struct=ao_ao_fmstruct, &
557 49864 : name="SCF-WORK_MATRIX-1-"//TRIM(ADJUSTL(cp_to_string(is))))
558 : END DO
559 : END IF
560 : IF ((.NOT. ASSOCIATED(scf_env%ortho)) .AND. &
561 : ((scf_env%method /= ot_diag_method_nr) .OR. &
562 : dft_control%qs_control%dftb .OR. dft_control%qs_control%xtb .OR. &
563 20964 : dft_control%qs_control%semi_empirical) .AND. &
564 : (scf_env%method /= special_diag_method_nr)) THEN
565 : ! Initialize fm matrix to store the Cholesky decomposition
566 16194 : ALLOCATE (scf_env%ortho)
567 : CALL cp_fm_create(scf_env%ortho, &
568 : matrix_struct=ao_ao_fmstruct, &
569 16194 : name="SCF-ORTHO_MATRIX")
570 : ! Initialize dbcsr matrix to store the Cholesky decomposition
571 16194 : IF (scf_env%cholesky_method == cholesky_dbcsr) THEN
572 58 : ref_matrix => matrix_s(1, 1)%matrix
573 58 : CALL dbcsr_init_p(scf_env%ortho_dbcsr)
574 : CALL dbcsr_create(scf_env%ortho_dbcsr, template=ref_matrix, &
575 58 : matrix_type=dbcsr_type_no_symmetry)
576 58 : CALL dbcsr_init_p(scf_env%buf1_dbcsr)
577 : CALL dbcsr_create(scf_env%buf1_dbcsr, template=ref_matrix, &
578 58 : matrix_type=dbcsr_type_no_symmetry)
579 58 : CALL dbcsr_init_p(scf_env%buf2_dbcsr)
580 : CALL dbcsr_create(scf_env%buf2_dbcsr, template=ref_matrix, &
581 58 : matrix_type=dbcsr_type_no_symmetry)
582 16136 : ELSE IF (scf_env%cholesky_method == cholesky_inverse .OR. &
583 : (scf_control%level_shift /= 0.0_dp .AND. &
584 : scf_env%cholesky_method == cholesky_off)) THEN
585 64 : ALLOCATE (scf_env%ortho_m1)
586 : CALL cp_fm_create(scf_env%ortho_m1, &
587 : matrix_struct=ao_ao_fmstruct, &
588 64 : name="SCF-ORTHO_MATRIX-1")
589 : END IF
590 : END IF
591 20964 : IF (.NOT. ASSOCIATED(scf_env%scf_work2)) THEN
592 18864 : ALLOCATE (scf_env%scf_work2)
593 : CALL cp_fm_create(scf_env%scf_work2, &
594 : matrix_struct=ao_ao_fmstruct, &
595 18864 : name="SCF-WORK_MATRIX-2")
596 : END IF
597 : END IF
598 :
599 27693 : IF (dft_control%dft_plus_u) THEN
600 732 : IF (dft_control%plus_u_method_id == plus_u_lowdin) THEN
601 356 : IF (.NOT. ASSOCIATED(scf_env%s_half)) THEN
602 36 : ALLOCATE (scf_env%s_half)
603 : CALL cp_fm_create(scf_env%s_half, &
604 : matrix_struct=ao_ao_fmstruct, &
605 36 : name="S**(1/2) MATRIX")
606 : END IF
607 : END IF
608 : END IF
609 :
610 27693 : IF (do_kpoints) THEN
611 4126 : IF (.NOT. ASSOCIATED(scf_env%scf_work1)) THEN
612 272 : nw = 4
613 1360 : ALLOCATE (scf_env%scf_work1(nw))
614 1360 : DO is = 1, SIZE(scf_env%scf_work1)
615 : CALL cp_fm_create(scf_env%scf_work1(is), &
616 : matrix_struct=ao_ao_fmstruct, &
617 1360 : name="SCF-WORK_MATRIX-1-"//TRIM(ADJUSTL(cp_to_string(is))))
618 : END DO
619 : END IF
620 : END IF
621 :
622 27693 : IF (.NOT. do_kpoints .AND. ASSOCIATED(scf_env%scf_work1)) THEN
623 17110 : IF (.NOT. ALLOCATED(scf_env%fm_copy_plan)) THEN
624 87778 : ALLOCATE (scf_env%fm_copy_plan(0:SIZE(scf_env%scf_work1)))
625 : END IF
626 : END IF
627 :
628 27693 : CALL cp_fm_struct_release(ao_ao_fmstruct)
629 :
630 27693 : CALL timestop(handle)
631 :
632 27693 : END SUBROUTINE qs_scf_ensure_work_matrices
633 :
634 : ! **************************************************************************************************
635 : !> \brief performs allocation of the MO matrices
636 : !> \param qs_env ...
637 : ! **************************************************************************************************
638 27693 : SUBROUTINE qs_scf_ensure_mos(qs_env)
639 : TYPE(qs_environment_type), POINTER :: qs_env
640 :
641 : CHARACTER(len=*), PARAMETER :: routineN = 'qs_scf_ensure_mos'
642 :
643 : INTEGER :: handle, ik, ikk, ispin, nmo, nmo_mat
644 27693 : TYPE(cp_fm_pool_p_type), DIMENSION(:), POINTER :: ao_mo_fm_pools
645 : TYPE(cp_fm_type), POINTER :: mo_coeff, mo_coeff_last
646 27693 : TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: mo_derivs
647 27693 : TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_s
648 : TYPE(dbcsr_type), POINTER :: mo_coeff_b
649 : TYPE(dft_control_type), POINTER :: dft_control
650 : TYPE(kpoint_type), POINTER :: kpoints
651 27693 : TYPE(mo_set_type), DIMENSION(:), POINTER :: mos, mos_k, mos_last_converged
652 : TYPE(xas_environment_type), POINTER :: xas_env
653 :
654 27693 : CALL timeset(routineN, handle)
655 :
656 27693 : NULLIFY (ao_mo_fm_pools, dft_control, mos, xas_env, matrix_s, mos_last_converged, mo_coeff_last)
657 :
658 : CALL get_qs_env(qs_env=qs_env, &
659 : dft_control=dft_control, &
660 : mos=mos, &
661 : matrix_s_kp=matrix_s, &
662 27693 : xas_env=xas_env)
663 27693 : CALL mpools_get(qs_env%mpools, ao_mo_fm_pools=ao_mo_fm_pools)
664 27693 : IF (dft_control%switch_surf_dip) THEN
665 2 : CALL get_qs_env(qs_env, mos_last_converged=mos_last_converged)
666 : END IF
667 :
668 27693 : nmo_mat = dft_control%nspins
669 27693 : IF (dft_control%restricted) nmo_mat = 1 ! right now, there might be more mos than needed derivs
670 :
671 : ! Finish initialization of the MOs
672 27693 : CPASSERT(ASSOCIATED(mos))
673 59482 : DO ispin = 1, SIZE(mos)
674 31789 : CALL get_mo_set(mos(ispin), mo_coeff=mo_coeff, mo_coeff_b=mo_coeff_b)
675 31789 : IF (.NOT. ASSOCIATED(mo_coeff)) THEN
676 : CALL init_mo_set(mos(ispin), &
677 : fm_pool=ao_mo_fm_pools(ispin)%pool, &
678 10104 : name="qs_env%mo"//TRIM(ADJUSTL(cp_to_string(ispin))))
679 : END IF
680 59482 : IF (.NOT. ASSOCIATED(mo_coeff_b)) THEN
681 10104 : CALL cp_fm_get_info(mos(ispin)%mo_coeff, ncol_global=nmo)
682 10104 : CALL dbcsr_init_p(mos(ispin)%mo_coeff_b)
683 : CALL cp_dbcsr_m_by_n_from_row_template(mos(ispin)%mo_coeff_b, template=matrix_s(1, 1)%matrix, n=nmo, &
684 10104 : sym=dbcsr_type_no_symmetry)
685 : END IF
686 : END DO
687 : ! Get the mo_derivs OK if needed
688 27693 : IF (qs_env%requires_mo_derivs) THEN
689 6555 : CALL get_qs_env(qs_env, mo_derivs=mo_derivs)
690 6555 : IF (.NOT. ASSOCIATED(mo_derivs)) THEN
691 10595 : ALLOCATE (mo_derivs(nmo_mat))
692 5645 : DO ispin = 1, nmo_mat
693 3170 : CALL get_mo_set(mos(ispin), mo_coeff_b=mo_coeff_b)
694 3170 : NULLIFY (mo_derivs(ispin)%matrix)
695 3170 : CALL dbcsr_init_p(mo_derivs(ispin)%matrix)
696 : CALL dbcsr_create(mo_derivs(ispin)%matrix, template=mo_coeff_b, &
697 5645 : name="mo_derivs", matrix_type=dbcsr_type_no_symmetry)
698 : END DO
699 2475 : CALL set_qs_env(qs_env, mo_derivs=mo_derivs)
700 : END IF
701 :
702 : ELSE
703 : ! nothing should be done
704 : END IF
705 :
706 : ! Finish initialization of the MOs for ADMM and derivs if needed ***
707 27693 : IF (dft_control%do_admm) THEN
708 992 : IF (dft_control%restricted) CPABORT("ROKS with ADMM is not implemented")
709 : END IF
710 :
711 : ! Finish initialization of mos_last_converged [SGh]
712 27693 : IF (dft_control%switch_surf_dip) THEN
713 2 : CPASSERT(ASSOCIATED(mos_last_converged))
714 4 : DO ispin = 1, SIZE(mos_last_converged)
715 2 : CALL get_mo_set(mos_last_converged(ispin), mo_coeff=mo_coeff_last)
716 4 : IF (.NOT. ASSOCIATED(mo_coeff_last)) THEN
717 : CALL init_mo_set(mos_last_converged(ispin), &
718 : fm_ref=mos(ispin)%mo_coeff, &
719 2 : name="qs_env%mos_last_converged"//TRIM(ADJUSTL(cp_to_string(ispin))))
720 : END IF
721 : END DO
722 : END IF
723 : ! kpoints: we have to initialize all the k-point MOs
724 27693 : CALL get_qs_env(qs_env=qs_env, kpoints=kpoints)
725 27693 : IF (kpoints%nkp /= 0) THEN
726 : ! check for some incompatible options
727 4126 : IF (qs_env%requires_mo_derivs) THEN
728 112 : CPWARN("MO derivative methods flag has been switched off for kpoint calculation")
729 : ! we switch it off to make band structure calculations
730 : ! possible for OT gamma point calculations
731 112 : qs_env%requires_mo_derivs = .FALSE.
732 : END IF
733 4126 : IF (dft_control%do_xas_calculation) THEN
734 0 : CPABORT("No XAS implemented with kpoints")
735 : END IF
736 4126 : IF (qs_env%do_rixs) THEN
737 0 : CPABORT("RIXS not implemented with kpoints")
738 : END IF
739 12552 : DO ik = 1, SIZE(kpoints%kp_env)
740 8426 : CALL mpools_get(kpoints%mpools, ao_mo_fm_pools=ao_mo_fm_pools)
741 8426 : mos_k => kpoints%kp_env(ik)%kpoint_env%mos
742 8426 : ikk = kpoints%kp_range(1) + ik - 1
743 8426 : CPASSERT(ASSOCIATED(mos_k))
744 22146 : DO ispin = 1, SIZE(mos_k)
745 9594 : CPASSERT(.NOT. ASSOCIATED(mos_k(ispin)%mo_coeff_b))
746 9594 : IF (kpoints%use_real_wfn) THEN
747 32 : IF (ASSOCIATED(mos_k(ispin)%mo_coeff)) CYCLE
748 : ELSE
749 9562 : IF (ASSOCIATED(mos_k(ispin)%cmo_coeff)) CYCLE
750 : END IF
751 : CALL init_mo_set(mos_k(ispin), fm_pool=ao_mo_fm_pools(ispin)%pool, &
752 : name="kpoints_"//TRIM(ADJUSTL(cp_to_string(ikk)))// &
753 : "%mo"//TRIM(ADJUSTL(cp_to_string(ispin))), &
754 18020 : complex_coeff=.NOT. kpoints%use_real_wfn)
755 : END DO
756 : END DO
757 : END IF
758 :
759 27693 : CALL timestop(handle)
760 :
761 27693 : END SUBROUTINE qs_scf_ensure_mos
762 :
763 : ! **************************************************************************************************
764 : !> \brief sets flag for mixing/DIIS during scf
765 : !> \param scf_control ...
766 : !> \param scf_section ...
767 : !> \param scf_env ...
768 : !> \param dft_control ...
769 : ! **************************************************************************************************
770 27679 : SUBROUTINE qs_scf_ensure_mixing(scf_control, scf_section, scf_env, dft_control)
771 : TYPE(scf_control_type), POINTER :: scf_control
772 : TYPE(section_vals_type), POINTER :: scf_section
773 : TYPE(qs_scf_env_type), POINTER :: scf_env
774 : TYPE(dft_control_type), POINTER :: dft_control
775 :
776 : TYPE(section_vals_type), POINTER :: mixing_section
777 :
778 27679 : IF (scf_control%diagonalization%update_method == diag_update_method_adiis) THEN
779 12 : CALL cite_reference(Hu2010)
780 : ! ADIIS is a Fock-space SCF method. It does not use density mixing or
781 : ! combine CDIIS with a separate density-mixing state.
782 12 : scf_env%mixing_method = no_mixing_nr
783 12 : scf_env%p_mix_alpha = 1.0_dp
784 : scf_env%skip_diis = scf_control%max_diis < 2 .OR. &
785 12 : scf_control%eps_diis < scf_control%eps_scf
786 12 : IF (scf_control%eps_diis < scf_control%eps_scf) THEN
787 0 : CPWARN("the ADIIS to DIIS switch is disabled, since EPS_DIIS < EPS_SCF")
788 : END IF
789 12 : RETURN
790 : END IF
791 :
792 27667 : SELECT CASE (scf_control%mixing_method)
793 : CASE (no_mix)
794 0 : scf_env%mixing_method = no_mixing_nr
795 0 : scf_env%p_mix_alpha = 1.0_dp
796 : CASE (direct_p_mix, kerker_mix, pulay_mix, broy_mix, modified_broy_mix, multisec_mix, &
797 : new_pulay_mix)
798 27667 : scf_env%mixing_method = scf_control%mixing_method
799 27667 : mixing_section => section_vals_get_subs_vals(scf_section, "MIXING")
800 27667 : IF (.NOT. ASSOCIATED(scf_env%mixing_store)) THEN
801 24831 : ALLOCATE (scf_env%mixing_store)
802 : CALL mixing_storage_create(scf_env%mixing_store, mixing_section, scf_env%mixing_method, &
803 8277 : dft_control%qs_control%cutoff)
804 : END IF
805 : CASE DEFAULT
806 27667 : CPABORT("Unknown mixing method")
807 : END SELECT
808 :
809 : ! Disable DIIS for OT and g-space density mixing methods
810 27667 : IF (scf_env%method == ot_method_nr) THEN
811 : ! No mixing is used with OT
812 6679 : scf_env%mixing_method = no_mixing_nr
813 6679 : scf_env%p_mix_alpha = 1.0_dp
814 6679 : scf_env%skip_diis = .TRUE.
815 : END IF
816 :
817 27667 : IF (scf_control%use_diag .AND. scf_env%mixing_method == no_mixing_nr) THEN
818 0 : CPABORT("Diagonalization procedures without mixing are not recommendable")
819 : END IF
820 :
821 27667 : IF (scf_env%mixing_method > direct_mixing_nr) THEN
822 1238 : scf_env%skip_diis = .TRUE.
823 1238 : scf_env%p_mix_alpha = scf_env%mixing_store%alpha
824 1238 : IF (scf_env%mixing_store%beta == 0.0_dp) THEN
825 0 : CPABORT("Mixing employing the Kerker damping factor needs BETA /= 0.0")
826 : END IF
827 : END IF
828 :
829 27667 : IF (scf_env%mixing_method == direct_mixing_nr) THEN
830 19750 : scf_env%p_mix_alpha = scf_env%mixing_store%alpha
831 19750 : IF (scf_control%eps_diis < scf_control%eps_scf) THEN
832 58 : scf_env%skip_diis = .TRUE.
833 58 : CPWARN("the DIIS scheme is disabled, since EPS_DIIS < EPS_SCF")
834 : END IF
835 : END IF
836 :
837 : END SUBROUTINE qs_scf_ensure_mixing
838 :
839 : ! **************************************************************************************************
840 : !> \brief sets flags for diagonalization and ensure that everything is
841 : !> allocated
842 : !> \param scf_env ...
843 : !> \param scf_section ...
844 : !> \param qs_env ...
845 : !> \param scf_control ...
846 : !> \param has_unit_metric ...
847 : ! **************************************************************************************************
848 27693 : SUBROUTINE qs_scf_ensure_diagonalization(scf_env, scf_section, qs_env, &
849 : scf_control, has_unit_metric)
850 : TYPE(qs_scf_env_type), POINTER :: scf_env
851 : TYPE(section_vals_type), POINTER :: scf_section
852 : TYPE(qs_environment_type), POINTER :: qs_env
853 : TYPE(scf_control_type), POINTER :: scf_control
854 : LOGICAL :: has_unit_metric
855 :
856 : INTEGER :: ispin, kplocal, nao, nmo
857 : INTEGER, DIMENSION(2) :: kp_range
858 : LOGICAL :: do_kpoints, need_coeff_b, not_se_or_tb, &
859 : ot_energies
860 : TYPE(cp_fm_type), POINTER :: mo_coeff
861 : TYPE(dft_control_type), POINTER :: dft_control
862 : TYPE(kpoint_type), POINTER :: kpoints
863 27693 : TYPE(mo_set_type), DIMENSION(:), POINTER :: mos
864 :
865 27693 : CALL get_qs_env(qs_env=qs_env, do_kpoints=do_kpoints, dft_control=dft_control, mos=mos)
866 : not_se_or_tb = .NOT. (dft_control%qs_control%dftb .OR. dft_control%qs_control%xtb .OR. &
867 27693 : dft_control%qs_control%semi_empirical)
868 27693 : need_coeff_b = .FALSE.
869 27693 : ot_energies = .FALSE.
870 27693 : scf_env%needs_ortho = .FALSE.
871 :
872 27693 : IF (dft_control%smeagol_control%smeagol_enabled .AND. &
873 : dft_control%smeagol_control%run_type == smeagol_runtype_emtransport) THEN
874 0 : scf_env%method = smeagol_method_nr
875 0 : scf_env%skip_diis = .TRUE.
876 0 : scf_control%use_diag = .FALSE.
877 :
878 0 : IF (.NOT. do_kpoints) THEN
879 0 : CPABORT("SMEAGOL requires kpoint calculations")
880 : END IF
881 0 : CPWARN_IF(scf_control%use_ot, "OT is irrelevant to NEGF method")
882 : END IF
883 :
884 27693 : IF (scf_control%use_diag) THEN
885 : ! sanity check whether combinations are allowed
886 21014 : IF (dft_control%restricted) THEN
887 0 : CPABORT("OT only for restricted (ROKS)")
888 : END IF
889 21096 : SELECT CASE (scf_control%diagonalization%method)
890 : CASE (diag_block_krylov, diag_block_davidson)
891 21014 : IF (.NOT. not_se_or_tb) THEN
892 0 : CPABORT("TB and SE not possible with block iterative diagonalization")
893 : END IF
894 : END SELECT
895 41912 : SELECT CASE (scf_control%diagonalization%method)
896 : ! Diagonalization: additional check whether we are in an orthonormal basis
897 : CASE (diag_standard)
898 20898 : scf_env%method = general_diag_method_nr
899 20898 : scf_env%needs_ortho = (.NOT. has_unit_metric) .AND. (.NOT. do_kpoints)
900 : IF (diag_type == FM_DIAG_TYPE_CUSOLVER .AND. &
901 : direct_generalized_diagonalization .AND. &
902 : scf_control%level_shift == 0.0_dp .AND. &
903 20898 : scf_env%cholesky_method /= cholesky_off .AND. &
904 : scf_env%cholesky_method /= cholesky_dbcsr) THEN
905 0 : CALL get_mo_set(mos(1), nao=nao)
906 0 : IF (nao >= cusolver_n_min) THEN
907 0 : scf_env%needs_ortho = .FALSE.
908 : END IF
909 : END IF
910 20898 : IF (has_unit_metric) THEN
911 2656 : scf_env%method = special_diag_method_nr
912 : END IF
913 : CASE (diag_ot)
914 24 : IF (dft_control%roks) THEN
915 0 : CPABORT("ROKS with OT diagonalization not possible")
916 : END IF
917 24 : scf_env%method = ot_diag_method_nr
918 24 : need_coeff_b = .TRUE.
919 : ! Block Krylov diagonlization: not possible with ROKS,
920 : ! allocation of additional matrices is needed
921 : CASE (diag_block_krylov)
922 32 : IF (dft_control%roks) THEN
923 0 : CPABORT("ROKS with block PF diagonalization not possible")
924 : END IF
925 32 : scf_env%method = block_krylov_diag_method_nr
926 32 : IF (.NOT. ASSOCIATED(scf_env%krylov_settings)) THEN
927 28 : CALL krylov_settings_create(scf_env%krylov_settings, scf_section)
928 : END IF
929 : ! the Gamma workspace carries the refinement buffers. The
930 : ! k-point world parses the settings only. The complex kernel
931 : ! takes a caller-owned workspace. Nothing is allocated from
932 : ! the Gamma MOS, and the k-point world never uses
933 : ! scf_env%ortho
934 32 : scf_env%needs_ortho = .NOT. do_kpoints
935 32 : IF (.NOT. do_kpoints) THEN
936 10 : CALL krylov_workspace_allocate(scf_env%krylov_space, scf_env%krylov_settings, mos)
937 : END IF
938 : ! Block davidson diagonlization: allocation of additional matrices is needed
939 : CASE (diag_block_davidson)
940 50 : scf_env%method = block_davidson_diag_method_nr
941 50 : IF (do_kpoints) THEN
942 32 : IF (dft_control%roks) THEN
943 0 : CPABORT("Block Davidson not possible with ROKS")
944 : END IF
945 : ! one channel per (local kpoint, spin); scratch matrices are local
946 : ! to generate_extended_space_c, so only the settings are stored
947 32 : IF (.NOT. ASSOCIATED(scf_env%block_davidson_env)) THEN
948 24 : CALL get_qs_env(qs_env=qs_env, kpoints=kpoints)
949 24 : CALL get_kpoint_info(kpoints, kp_range=kp_range)
950 24 : kplocal = kp_range(2) - kp_range(1) + 1
951 : CALL block_davidson_env_create(scf_env%block_davidson_env, &
952 24 : kplocal*dft_control%nspins, scf_section)
953 : END IF
954 : need_coeff_b = .FALSE.
955 : ELSE
956 18 : IF (.NOT. ASSOCIATED(scf_env%block_davidson_env)) THEN
957 : CALL block_davidson_env_create(scf_env%block_davidson_env, dft_control%nspins, &
958 14 : scf_section)
959 : END IF
960 38 : DO ispin = 1, dft_control%nspins
961 20 : CALL get_mo_set(mo_set=mos(ispin), mo_coeff=mo_coeff, nao=nao, nmo=nmo)
962 38 : CALL block_davidson_allocate(scf_env%block_davidson_env(ispin), mo_coeff, nao, nmo)
963 : END DO
964 : need_coeff_b = .TRUE.
965 : END IF
966 : ! Filter matrix diagonalisation method
967 : CASE (diag_filter_matrix)
968 10 : scf_env%method = filter_matrix_diag_method_nr
969 10 : IF (.NOT. fb_env_has_data(scf_env%filter_matrix_env)) THEN
970 10 : CALL fb_env_create(scf_env%filter_matrix_env)
971 : END IF
972 10 : CALL fb_env_read_input(scf_env%filter_matrix_env, scf_section)
973 10 : CALL fb_env_build_rcut_auto(scf_env%filter_matrix_env, qs_env)
974 10 : CALL fb_env_write_info(scf_env%filter_matrix_env, qs_env, scf_section)
975 10 : CALL fb_distribution_build(scf_env%filter_matrix_env, qs_env, scf_section)
976 10 : CALL fb_env_build_atomic_halos(scf_env%filter_matrix_env, qs_env, scf_section)
977 : CASE DEFAULT
978 21014 : CPABORT("Unknown diagonalization method")
979 : END SELECT
980 : ! Check if subspace diagonlization is requested: allocation of additional matrices is needed
981 21014 : IF (scf_control%do_diag_sub) THEN
982 2 : scf_env%needs_ortho = .TRUE.
983 2 : IF (.NOT. ASSOCIATED(scf_env%subspace_env)) THEN
984 : CALL diag_subspace_env_create(scf_env%subspace_env, scf_section, &
985 2 : dft_control%qs_control%cutoff)
986 : END IF
987 2 : CALL diag_subspace_allocate(scf_env%subspace_env, qs_env, mos)
988 2 : IF (do_kpoints) THEN
989 0 : CPABORT("No subspace diagonlization with kpoint calculation")
990 : END IF
991 : END IF
992 : ! OT: check if OT is used instead of diagonalization. Not possible with added MOS at the moment
993 6679 : ELSE IF (scf_control%use_ot) THEN
994 6679 : scf_env%method = ot_method_nr
995 6679 : need_coeff_b = .TRUE.
996 6679 : CALL section_vals_val_get(scf_section, "OT%ENERGIES", l_val=ot_energies)
997 20037 : IF (SUM(ABS(scf_control%added_mos)) > 0 .AND. &
998 : (.NOT. do_kpoints .OR. .NOT. ot_energies)) THEN
999 0 : CPABORT("OT with ADDED_MOS currently requires complex K points and OT%ENERGIES.")
1000 : END IF
1001 6679 : IF (dft_control%restricted .AND. dft_control%nspins /= 2) THEN
1002 0 : CPABORT("nspin must be 2 for restricted (ROKS)")
1003 : END IF
1004 : ! K-point OT is allowed to reach the spin/k-point channel allocation stub.
1005 0 : ELSE IF (scf_env%method /= smeagol_method_nr) THEN
1006 0 : CPABORT("OT or DIAGONALIZATION have to be set")
1007 : END IF
1008 59482 : DO ispin = 1, dft_control%nspins
1009 59482 : mos(ispin)%use_mo_coeff_b = need_coeff_b
1010 : END DO
1011 :
1012 27693 : END SUBROUTINE qs_scf_ensure_diagonalization
1013 :
1014 : ! **************************************************************************************************
1015 : !> \brief performs those initialisations that need to be done only once
1016 : !> (e.g. that only depend on the atomic positions)
1017 : !> this will be called in scf
1018 : !> \param scf_env ...
1019 : !> \param qs_env ...
1020 : !> \param scf_section ...
1021 : !> \param scf_control ...
1022 : !> \par History
1023 : !> 03.2006 created [Joost VandeVondele]
1024 : ! **************************************************************************************************
1025 27693 : SUBROUTINE init_scf_run(scf_env, qs_env, scf_section, scf_control)
1026 :
1027 : TYPE(qs_scf_env_type), POINTER :: scf_env
1028 : TYPE(qs_environment_type), POINTER :: qs_env
1029 : TYPE(section_vals_type), POINTER :: scf_section
1030 : TYPE(scf_control_type), POINTER :: scf_control
1031 :
1032 : CHARACTER(LEN=*), PARAMETER :: routineN = 'init_scf_run'
1033 :
1034 : INTEGER :: after, handle, homo, ii, ikind, ispin, &
1035 : iw, nao, ndep, needed_evals, nmo, &
1036 : output_unit
1037 : LOGICAL :: dft_plus_u_atom, do_kpoints, &
1038 : init_u_ramping_each_scf, omit_headers, &
1039 : s_minus_half_available
1040 : REAL(KIND=dp) :: u_ramping
1041 27693 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: evals
1042 27693 : TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
1043 : TYPE(cp_fm_struct_type), POINTER :: fm_struct
1044 : TYPE(cp_fm_type) :: evecs, fm_w
1045 : TYPE(cp_fm_type), POINTER :: mo_coeff
1046 : TYPE(cp_logger_type), POINTER :: logger
1047 27693 : TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_s
1048 27693 : TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_s_kp, matrix_w_kp
1049 : TYPE(dft_control_type), POINTER :: dft_control
1050 : TYPE(kpoint_type), POINTER :: kpoints
1051 27693 : TYPE(mo_set_type), DIMENSION(:), POINTER :: mos
1052 : TYPE(mp_para_env_type), POINTER :: para_env
1053 27693 : TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
1054 : TYPE(qs_kind_type), POINTER :: qs_kind
1055 : TYPE(qs_rho_type), POINTER :: rho
1056 : TYPE(xas_environment_type), POINTER :: xas_env
1057 :
1058 27693 : CALL timeset(routineN, handle)
1059 :
1060 27693 : NULLIFY (qs_kind_set, matrix_s, dft_control, mos, qs_kind, rho, xas_env, mo_coeff)
1061 :
1062 27693 : logger => cp_get_default_logger()
1063 :
1064 27693 : CPASSERT(ASSOCIATED(scf_env))
1065 27693 : CPASSERT(ASSOCIATED(qs_env))
1066 27693 : NULLIFY (para_env)
1067 :
1068 27693 : s_minus_half_available = .FALSE.
1069 : CALL get_qs_env(qs_env, &
1070 : dft_control=dft_control, &
1071 : qs_kind_set=qs_kind_set, &
1072 : mos=mos, &
1073 : rho=rho, &
1074 : nelectron_total=scf_env%nelectron, &
1075 : do_kpoints=do_kpoints, &
1076 : para_env=para_env, &
1077 27693 : xas_env=xas_env)
1078 :
1079 : ! A new SCF run may use a different geometry, cell or basis.
1080 27693 : IF (do_kpoints) THEN
1081 4126 : CALL get_qs_env(qs_env, kpoints=kpoints)
1082 4126 : CALL kpoint_clear_scf_smat(kpoints)
1083 : END IF
1084 :
1085 : ! Check restricted optimizers available for tblite library
1086 27693 : IF (dft_control%qs_control%xtb_control%do_tblite) THEN
1087 2608 : IF (scf_env%method == ot_method_nr) THEN
1088 : CALL cp_warn(__LOCATION__, &
1089 : "CP2K/tblite with OT updates the tblite SCC variables directly from the OT density; "// &
1090 18 : "XTB/SCC_MIXER is ignored.")
1091 18 : IF (scf_control%smear%do_smear .AND. &
1092 : dft_control%qs_control%xtb_control%tblite_method == gfn2xtb) THEN
1093 : CALL cp_warn(__LOCATION__, &
1094 : "Direct GFN2/tblite smearing has indefinite shell and multipole SCC modes. "// &
1095 0 : "Use SCF/DIAGONALIZATION ALGORITHM OT with XTB/SCC_MIXER TBLITE or CP2K.")
1096 0 : CPABORT("Use fixed-H OT for GFN2 smearing")
1097 : END IF
1098 : END IF
1099 : END IF
1100 :
1101 : ! Calculate ortho matrix
1102 27693 : ndep = 0
1103 27693 : IF (scf_env%needs_ortho) THEN
1104 14430 : CALL get_qs_env(qs_env, matrix_s=matrix_s)
1105 14430 : CALL copy_dbcsr_to_fm(matrix_s(1)%matrix, scf_env%ortho)
1106 14430 : IF (scf_env%cholesky_method > cholesky_off) THEN
1107 14368 : CALL cp_fm_cholesky_decompose(scf_env%ortho)
1108 14368 : IF (scf_env%cholesky_method == cholesky_dbcsr) THEN
1109 58 : CALL cp_fm_triangular_invert(scf_env%ortho)
1110 58 : CALL cp_fm_set_all(scf_env%scf_work2, 0.0_dp)
1111 58 : CALL cp_fm_to_fm_triangular(scf_env%ortho, scf_env%scf_work2, "U")
1112 58 : CALL copy_fm_to_dbcsr(scf_env%scf_work2, scf_env%ortho_dbcsr)
1113 14310 : ELSE IF (scf_env%cholesky_method == cholesky_inverse) THEN
1114 46 : CALL cp_fm_to_fm(scf_env%ortho, scf_env%ortho_m1)
1115 46 : CALL cp_fm_triangular_invert(scf_env%ortho_m1)
1116 : END IF
1117 : ELSE
1118 62 : CALL cp_fm_get_info(scf_env%ortho, ncol_global=nao)
1119 186 : ALLOCATE (evals(nao))
1120 62 : evals = 0
1121 :
1122 62 : CALL cp_fm_create(evecs, scf_env%ortho%matrix_struct)
1123 :
1124 : ! Perform an EVD
1125 62 : CALL choose_eigv_solver(scf_env%ortho, evecs, evals)
1126 :
1127 : ! Determine the number of neglectable eigenvalues assuming that the eigenvalues are in ascending order
1128 : ! (Required by Lapack)
1129 : ndep = 0
1130 152 : DO ii = 1, nao
1131 152 : IF (evals(ii) > scf_control%eps_eigval) THEN
1132 62 : ndep = ii - 1
1133 62 : EXIT
1134 : END IF
1135 : END DO
1136 62 : needed_evals = nao - ndep
1137 :
1138 : ! Set the eigenvalue of the eigenvectors belonging to the linear subspace to zero
1139 152 : evals(1:ndep) = 0.0_dp
1140 : ! Determine the eigenvalues of the inverse square root
1141 1884 : evals(ndep + 1:nao) = 1.0_dp/SQRT(evals(ndep + 1:nao))
1142 :
1143 : ! Create reduced matrices
1144 62 : NULLIFY (fm_struct)
1145 : CALL cp_fm_struct_create(fm_struct, template_fmstruct=scf_env%ortho%matrix_struct, &
1146 62 : nrow_global=nao, ncol_global=needed_evals)
1147 :
1148 62 : ALLOCATE (scf_env%ortho_red, scf_env%scf_work2_red)
1149 62 : CALL cp_fm_create(scf_env%ortho_red, fm_struct)
1150 62 : CALL cp_fm_create(scf_env%scf_work2_red, fm_struct)
1151 62 : CALL cp_fm_struct_release(fm_struct)
1152 :
1153 62 : IF (scf_control%level_shift /= 0.0_dp) THEN
1154 : CALL cp_fm_struct_create(fm_struct, template_fmstruct=scf_env%ortho%matrix_struct, &
1155 6 : nrow_global=needed_evals, ncol_global=nao)
1156 :
1157 6 : ALLOCATE (scf_env%ortho_m1_red)
1158 6 : CALL cp_fm_create(scf_env%ortho_m1_red, fm_struct)
1159 6 : CALL cp_fm_struct_release(fm_struct)
1160 : END IF
1161 :
1162 276 : ALLOCATE (scf_env%scf_work1_red(SIZE(scf_env%scf_work1)))
1163 152 : DO ispin = 1, SIZE(scf_env%scf_work1)
1164 : CALL cp_fm_struct_create(fm_struct, template_fmstruct=scf_env%ortho%matrix_struct, &
1165 90 : nrow_global=needed_evals, ncol_global=needed_evals)
1166 90 : CALL cp_fm_create(scf_env%scf_work1_red(ispin), fm_struct)
1167 152 : CALL cp_fm_struct_release(fm_struct)
1168 : END DO
1169 :
1170 : ! Scale the eigenvalues and copy them to
1171 62 : CALL cp_fm_to_fm(evecs, scf_env%ortho_red, needed_evals, ndep + 1, 1)
1172 :
1173 62 : IF (scf_control%level_shift /= 0.0_dp) THEN
1174 6 : CALL cp_fm_transpose(scf_env%ortho_red, scf_env%ortho_m1_red)
1175 : END IF
1176 :
1177 62 : CALL cp_fm_column_scale(scf_env%ortho_red, evals(ndep + 1:))
1178 :
1179 : ! Keep the dimensions of the eigenvalue and occupation vectors consistent with nmo
1180 : ! when reducing the number of MOs due to linear dependence in the basis.
1181 152 : DO ispin = 1, SIZE(mos)
1182 90 : CALL get_mo_set(mos(ispin), nmo=nmo, mo_coeff=mo_coeff, homo=homo)
1183 90 : IF (needed_evals < nmo) THEN
1184 22 : IF (needed_evals < homo) THEN
1185 : CALL cp_abort(__LOCATION__, &
1186 : "The numerical rank of the overlap matrix is lower than the "// &
1187 : "number of orbitals to be occupied! Check the geometry or increase "// &
1188 0 : "EPS_DEFAULT or EPS_PGF_ORB!")
1189 : END IF
1190 : CALL cp_warn(__LOCATION__, &
1191 : "The numerical rank of the overlap matrix is lower than the number of requested MOs! "// &
1192 : "Reduce the number of MOs to the number of available MOs. If necessary, "// &
1193 22 : "request a lower number of MOs or increase EPS_DEFAULT or EPS_PGF_ORB.")
1194 22 : CALL reallocate(mos(ispin)%eigenvalues, 1, needed_evals)
1195 22 : CALL reallocate(mos(ispin)%occupation_numbers, 1, needed_evals)
1196 22 : CALL set_mo_set(mos(ispin), nmo=needed_evals)
1197 : END IF
1198 : ! Copy the last columns to mo_coeff if the container is large enough
1199 242 : CALL cp_fm_to_fm(evecs, mo_coeff, MIN(ndep, MAX(0, nmo - needed_evals)), 1, needed_evals + 1)
1200 : END DO
1201 :
1202 : ! Obtain ortho from (P)DGEMM, skip the linear dependent columns
1203 : CALL parallel_gemm("N", "T", nao, nao, needed_evals, 1.0_dp, scf_env%ortho_red, evecs, &
1204 62 : 0.0_dp, scf_env%ortho, b_first_col=ndep + 1)
1205 :
1206 62 : IF (scf_control%level_shift /= 0.0_dp) THEN
1207 : ! We need SQRT(evals) of the eigenvalues of H, so 1/SQRT(evals) of ortho_red
1208 168 : evals(ndep + 1:nao) = 1.0_dp/evals(ndep + 1:nao)
1209 6 : CALL cp_fm_row_scale(scf_env%ortho_m1_red, evals(ndep + 1:))
1210 :
1211 : CALL parallel_gemm("T", "T", nao, nao, needed_evals, 1.0_dp, scf_env%ortho_m1_red, evecs, &
1212 6 : 0.0_dp, scf_env%ortho_m1, b_first_col=ndep + 1)
1213 : END IF
1214 :
1215 62 : CALL cp_fm_release(evecs)
1216 :
1217 186 : s_minus_half_available = .TRUE.
1218 : END IF
1219 :
1220 14430 : IF (BTEST(cp_print_key_should_output(logger%iter_info, &
1221 : qs_env%input, "DFT%PRINT%AO_MATRICES/ORTHO"), cp_p_file)) THEN
1222 : iw = cp_print_key_unit_nr(logger, qs_env%input, "DFT%PRINT%AO_MATRICES/ORTHO", &
1223 4 : extension=".Log")
1224 4 : CALL section_vals_val_get(qs_env%input, "DFT%PRINT%AO_MATRICES%NDIGITS", i_val=after)
1225 4 : CALL section_vals_val_get(qs_env%input, "DFT%PRINT%AO_MATRICES%OMIT_HEADERS", l_val=omit_headers)
1226 4 : after = MIN(MAX(after, 1), 16)
1227 : CALL write_fm_with_basis_info(scf_env%ortho, 4, after, qs_env, &
1228 4 : para_env, output_unit=iw, omit_headers=omit_headers)
1229 : CALL cp_print_key_finished_output(iw, logger, qs_env%input, &
1230 4 : "DFT%PRINT%AO_MATRICES/ORTHO")
1231 : END IF
1232 : END IF
1233 :
1234 27693 : CALL get_mo_set(mo_set=mos(1), nao=nao)
1235 :
1236 : ! DFT+U methods based on Lowdin charges need S^(1/2)
1237 27693 : IF (dft_control%dft_plus_u) THEN
1238 732 : IF (dft_control%plus_u_method_id == plus_u_lowdin) THEN
1239 356 : IF (do_kpoints) THEN
1240 : ! The active SCF environment is bound to qs_env only after first initialization.
1241 122 : IF (scf_env%method /= general_diag_method_nr .AND. scf_env%method /= ot_method_nr) THEN
1242 : CALL cp_abort(__LOCATION__, &
1243 0 : "Lowdin DFT+U with k-points requires STANDARD diagonalization or OT")
1244 : END IF
1245 122 : CALL get_qs_env(qs_env, kpoints=kpoints, matrix_s_kp=matrix_s_kp, matrix_w_kp=matrix_w_kp)
1246 122 : IF (scf_control%use_ot) THEN
1247 46 : IF (kpoints%use_real_wfn .OR. dft_control%restricted) THEN
1248 : CALL cp_abort(__LOCATION__, &
1249 0 : "Lowdin DFT+U with k-point OT requires complex RKS/UKS wavefunctions")
1250 : END IF
1251 : END IF
1252 122 : CALL diag_kp_smat(matrix_s_kp, kpoints, scf_env%scf_work1, lowdin_forces=ASSOCIATED(matrix_w_kp))
1253 122 : CALL lowdin_kp_init(qs_env)
1254 : ELSE
1255 234 : CALL get_qs_env(qs_env, matrix_s=matrix_s)
1256 234 : IF (s_minus_half_available) THEN
1257 : CALL cp_dbcsr_sm_fm_multiply(matrix_s(1)%matrix, scf_env%ortho, &
1258 0 : scf_env%s_half, nao)
1259 : ELSE
1260 234 : CALL copy_dbcsr_to_fm(matrix_s(1)%matrix, scf_env%s_half)
1261 234 : CALL cp_fm_create(fm_w, scf_env%s_half%matrix_struct)
1262 234 : CALL cp_fm_power(scf_env%s_half, fm_w, 0.5_dp, scf_control%eps_eigval, ndep)
1263 234 : CALL cp_fm_release(fm_w)
1264 : END IF
1265 : END IF
1266 : END IF
1267 1994 : DO ikind = 1, SIZE(qs_kind_set)
1268 1262 : qs_kind => qs_kind_set(ikind)
1269 : CALL get_qs_kind(qs_kind=qs_kind, &
1270 : dft_plus_u_atom=dft_plus_u_atom, &
1271 : u_ramping=u_ramping, &
1272 1262 : init_u_ramping_each_scf=init_u_ramping_each_scf)
1273 1994 : IF (dft_plus_u_atom .AND. (u_ramping /= 0.0_dp)) THEN
1274 24 : IF (init_u_ramping_each_scf) THEN
1275 12 : CALL set_qs_kind(qs_kind=qs_kind, u_minus_j=0.0_dp)
1276 : END IF
1277 : END IF
1278 : END DO
1279 : END IF
1280 :
1281 27693 : IF (dft_control%dft_plus_u) THEN
1282 732 : IF (dft_control%plus_u_method_id == plus_u_tensorial) THEN
1283 56 : CALL get_qs_env(qs_env, atomic_kind_set=atomic_kind_set, qs_kind_set=qs_kind_set)
1284 116 : DO ikind = 1, SIZE(atomic_kind_set)
1285 60 : qs_kind => qs_kind_set(ikind)
1286 60 : CALL get_qs_kind(qs_kind=qs_kind, dft_plus_u_atom=dft_plus_u_atom)
1287 60 : IF (.NOT. dft_plus_u_atom) CYCLE
1288 : CALL calculate_atomic_orbitals(atomic_kind=atomic_kind_set(ikind), &
1289 : qs_kind=qs_kind_set(ikind), &
1290 : which_l=qs_kind%dft_plus_u%l, &
1291 : which_n=qs_kind%dft_plus_u%n, &
1292 : proj_shell_charge=qs_kind%dft_plus_u%proj_shell_charge, &
1293 172 : ao_coef=qs_kind%dft_plus_u%ao_coef)
1294 : END DO
1295 : END IF
1296 : END IF
1297 :
1298 : ! extrapolate outer loop variables
1299 27693 : IF (scf_control%outer_scf%have_scf) THEN
1300 4457 : CALL outer_loop_extrapolate(qs_env)
1301 : END IF
1302 :
1303 : ! initializes rho and the mos
1304 27693 : IF (ASSOCIATED(qs_env%xas_env)) THEN
1305 : ! if just optimized wfn, e.g. ground state
1306 : ! changes come from a perturbation, e.g., the occupation numbers
1307 : ! it could be generalized for other cases, at the moment used only for core level spectroscopy
1308 : ! initialize the density with the localized mos
1309 86 : CALL xas_initialize_rho(qs_env, scf_env, scf_control)
1310 86 : IF (do_kpoints .AND. dft_control%dft_plus_u .AND. dft_control%plus_u_method_id == plus_u_lowdin) THEN
1311 0 : CALL lowdin_kp_initial_density(qs_env, scf_env%scf_work1(1))
1312 : END IF
1313 : ELSE
1314 : CALL scf_env_initial_rho_setup(scf_env, qs_env=qs_env, &
1315 27607 : scf_section=scf_section, scf_control=scf_control)
1316 : END IF
1317 :
1318 : ! Frozen density approximation
1319 27693 : IF (ASSOCIATED(qs_env%wf_history)) THEN
1320 27693 : IF (qs_env%wf_history%interpolation_method_nr == wfi_frozen_method_nr) THEN
1321 12 : IF (.NOT. ASSOCIATED(qs_env%wf_history%past_states(1)%snapshot)) THEN
1322 4 : CALL wfi_update(qs_env%wf_history, qs_env=qs_env, dt=1.0_dp)
1323 4 : ALLOCATE (qs_env%wf_history%past_states(1)%snapshot%rho_frozen)
1324 4 : CALL qs_rho_create(qs_env%wf_history%past_states(1)%snapshot%rho_frozen)
1325 : CALL duplicate_rho_type(rho_input=rho, &
1326 : rho_output=qs_env%wf_history%past_states(1)%snapshot%rho_frozen, &
1327 4 : qs_env=qs_env)
1328 : END IF
1329 : END IF
1330 : END IF
1331 :
1332 : !image charge method, calculate image_matrix if required
1333 27693 : IF (qs_env%qmmm) THEN
1334 4022 : IF (qs_env%qmmm .AND. qs_env%qmmm_env_qm%image_charge) THEN
1335 : CALL conditional_calc_image_matrix(qs_env=qs_env, &
1336 20 : qmmm_env=qs_env%qmmm_env_qm)
1337 : END IF
1338 : END IF
1339 :
1340 : output_unit = cp_print_key_unit_nr(logger, scf_section, "PRINT%PROGRAM_RUN_INFO", &
1341 27693 : extension=".scfLog")
1342 27693 : CALL qs_scf_initial_info(output_unit, mos, dft_control, ndep)
1343 : CALL cp_print_key_finished_output(output_unit, logger, scf_section, &
1344 27693 : "PRINT%PROGRAM_RUN_INFO")
1345 :
1346 27693 : CALL timestop(handle)
1347 :
1348 55386 : END SUBROUTINE init_scf_run
1349 :
1350 : ! **************************************************************************************************
1351 : !> \brief Initializes rho and the mos, so that an scf cycle can start
1352 : !> \param scf_env the scf env in which to do the scf
1353 : !> \param qs_env the qs env the scf_env lives in
1354 : !> \param scf_section ...
1355 : !> \param scf_control ...
1356 : !> \par History
1357 : !> 02.2003 created [fawzi]
1358 : !> \author fawzi
1359 : ! **************************************************************************************************
1360 27607 : SUBROUTINE scf_env_initial_rho_setup(scf_env, qs_env, scf_section, scf_control)
1361 : TYPE(qs_scf_env_type), POINTER :: scf_env
1362 : TYPE(qs_environment_type), POINTER :: qs_env
1363 : TYPE(section_vals_type), POINTER :: scf_section
1364 : TYPE(scf_control_type), POINTER :: scf_control
1365 :
1366 : CHARACTER(len=*), PARAMETER :: routineN = 'scf_env_initial_rho_setup'
1367 :
1368 : INTEGER :: extrapolation_method_nr, handle, ispin, &
1369 : nmo, output_unit
1370 : LOGICAL :: do_harris, do_kpoints, orthogonal_wf
1371 27607 : REAL(KIND=dp), POINTER :: auxiliary(:, :)
1372 : TYPE(cp_fm_type), POINTER :: mo_coeff
1373 : TYPE(cp_logger_type), POINTER :: logger
1374 : TYPE(dft_control_type), POINTER :: dft_control
1375 : TYPE(harris_type), POINTER :: harris_env
1376 : TYPE(kpoint_type), POINTER :: kpoints
1377 27607 : TYPE(mo_set_type), DIMENSION(:), POINTER :: mos
1378 : TYPE(mp_para_env_type), POINTER :: para_env
1379 : TYPE(qs_rho_type), POINTER :: rho
1380 27607 : TYPE(rho_atom_type), DIMENSION(:), POINTER :: rho_atom
1381 :
1382 27607 : CALL timeset(routineN, handle)
1383 27607 : NULLIFY (mo_coeff, rho, dft_control, para_env, mos)
1384 27607 : logger => cp_get_default_logger()
1385 27607 : CPASSERT(ASSOCIATED(scf_env))
1386 27607 : CPASSERT(ASSOCIATED(qs_env))
1387 :
1388 : CALL get_qs_env(qs_env, &
1389 : rho=rho, &
1390 : mos=mos, &
1391 : dft_control=dft_control, &
1392 : do_kpoints=do_kpoints, &
1393 27607 : para_env=para_env)
1394 :
1395 27607 : do_harris = qs_env%harris_method
1396 27607 : IF (do_harris .AND. scf_control%density_guess == external_density_guess) THEN
1397 0 : CPABORT("EXTERNAL_DENSITY SCF guess is incompatible with HARRIS_METHOD")
1398 : END IF
1399 :
1400 27607 : extrapolation_method_nr = wfi_use_guess_method_nr
1401 27607 : IF (ASSOCIATED(qs_env%wf_history)) THEN
1402 : CALL wfi_extrapolate(qs_env%wf_history, &
1403 : qs_env=qs_env, dt=1.0_dp, &
1404 : extrapolation_method_nr=extrapolation_method_nr, &
1405 27607 : orthogonal_wf=orthogonal_wf)
1406 : ! wfi_use_guess_method_nr the wavefunctions are not yet initialized
1407 : IF ((.NOT. orthogonal_wf) .AND. &
1408 : (scf_env%method == ot_method_nr) .AND. &
1409 27607 : (.NOT. do_kpoints) .AND. &
1410 : (.NOT. (extrapolation_method_nr == wfi_use_guess_method_nr))) THEN
1411 0 : DO ispin = 1, SIZE(mos)
1412 0 : CALL get_mo_set(mos(ispin), mo_coeff=mo_coeff, nmo=nmo)
1413 0 : CALL reorthogonalize_vectors(qs_env, v_matrix=mo_coeff, n_col=nmo)
1414 0 : IF (dft_control%hairy_probes .EQV. .TRUE.) THEN
1415 0 : scf_control%smear%do_smear = .FALSE.
1416 : CALL set_mo_occupation(mo_set=mos(ispin), &
1417 0 : smear=scf_control%smear, probe=dft_control%probe)
1418 : ELSE
1419 : CALL set_mo_occupation(mo_set=mos(ispin), &
1420 0 : smear=scf_control%smear)
1421 : END IF
1422 : END DO
1423 : END IF
1424 : END IF
1425 :
1426 27607 : IF (.NOT. do_harris) THEN
1427 : output_unit = cp_print_key_unit_nr(logger, scf_section, "PRINT%PROGRAM_RUN_INFO", &
1428 27571 : extension=".scfLog")
1429 27571 : IF (output_unit > 0) THEN
1430 : WRITE (UNIT=output_unit, FMT="(/,T2,A,I0)") &
1431 : "Extrapolation method: "// &
1432 13940 : TRIM(wfi_get_method_label(extrapolation_method_nr))
1433 13940 : IF (extrapolation_method_nr == wfi_ps_method_nr) THEN
1434 : WRITE (UNIT=output_unit, FMT="(T2,A,I0,A)") &
1435 154 : "Extrapolation order: ", &
1436 308 : MAX((MIN(qs_env%wf_history%memory_depth, qs_env%wf_history%snapshot_count) - 1), 0)
1437 : END IF
1438 : END IF
1439 : CALL cp_print_key_finished_output(output_unit, logger, scf_section, &
1440 27571 : "PRINT%PROGRAM_RUN_INFO")
1441 : END IF
1442 :
1443 : IF (do_harris) THEN
1444 36 : CALL get_qs_env(qs_env, harris_env=harris_env)
1445 36 : CALL harris_density_update(qs_env, harris_env)
1446 36 : CALL qs_rho_update_rho(rho, qs_env=qs_env)
1447 36 : CALL qs_ks_did_change(qs_env%ks_env, rho_changed=.TRUE.)
1448 27571 : ELSE IF (extrapolation_method_nr == wfi_use_guess_method_nr) THEN
1449 10613 : CALL calculate_first_density_matrix(scf_env=scf_env, qs_env=qs_env)
1450 10613 : IF (scf_control%density_guess == external_density_guess) THEN
1451 2 : CALL read_scf_guess_density(qs_env, scf_control%external_density_file_name)
1452 : ELSE
1453 10611 : CALL qs_rho_update_rho(rho, qs_env=qs_env)
1454 : END IF
1455 10613 : CALL qs_ks_did_change(qs_env%ks_env, rho_changed=.TRUE.)
1456 : END IF
1457 :
1458 : ! Some preparation for the mixing
1459 27607 : NULLIFY (auxiliary)
1460 27607 : IF (do_kpoints .AND. dft_control%dft_plus_u) THEN
1461 366 : IF (dft_control%plus_u_method_id == plus_u_lowdin) THEN
1462 122 : CALL lowdin_kp_initial_density(qs_env, scf_env%scf_work1(1))
1463 122 : CALL get_qs_env(qs_env, kpoints=kpoints)
1464 122 : auxiliary => kpoints%lowdin_q
1465 : END IF
1466 : END IF
1467 27607 : IF (scf_env%mixing_method > 1) THEN
1468 1232 : IF (dft_control%qs_control%gapw) THEN
1469 140 : CALL get_qs_env(qs_env=qs_env, rho_atom_set=rho_atom)
1470 : CALL mixing_init(scf_env%mixing_method, rho, scf_env%mixing_store, &
1471 140 : para_env, rho_atom=rho_atom, auxiliary=auxiliary)
1472 1092 : ELSE IF (dft_control%qs_control%dftb .OR. dft_control%qs_control%xtb) THEN
1473 180 : CALL charge_mixing_init(scf_env%mixing_store)
1474 912 : ELSE IF (dft_control%qs_control%semi_empirical) THEN
1475 0 : CPABORT('SE Code not possible')
1476 : ELSE
1477 : CALL mixing_init(scf_env%mixing_method, rho, scf_env%mixing_store, &
1478 912 : para_env, auxiliary=auxiliary)
1479 : END IF
1480 : END IF
1481 :
1482 59224 : DO ispin = 1, SIZE(mos) !fm->dbcsr
1483 59224 : IF (mos(ispin)%use_mo_coeff_b) THEN
1484 : CALL copy_fm_to_dbcsr(mos(ispin)%mo_coeff, &
1485 8003 : mos(ispin)%mo_coeff_b) !fm->dbcsr
1486 : END IF
1487 : END DO !fm->dbcsr
1488 :
1489 27607 : CALL timestop(handle)
1490 :
1491 27607 : END SUBROUTINE scf_env_initial_rho_setup
1492 :
1493 : END MODULE qs_scf_initialization
|