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