Line data Source code
1 : !--------------------------------------------------------------------------------------------------!
2 : ! CP2K: A general program to perform molecular dynamics simulations !
3 : ! Copyright 2000-2025 CP2K developers group <https://cp2k.org> !
4 : ! !
5 : ! SPDX-License-Identifier: GPL-2.0-or-later !
6 : !--------------------------------------------------------------------------------------------------!
7 :
8 : ! **************************************************************************************************
9 : !> \brief Routines to somehow generate an initial guess
10 : !> \par History
11 : !> 2006.03 Moved here from qs_scf.F [Joost VandeVondele]
12 : ! **************************************************************************************************
13 : MODULE qs_initial_guess
14 : USE atom_kind_orbitals, ONLY: calculate_atomic_orbitals
15 : USE atomic_kind_types, ONLY: atomic_kind_type,&
16 : get_atomic_kind,&
17 : get_atomic_kind_set
18 : USE basis_set_types, ONLY: get_gto_basis_set,&
19 : gto_basis_set_type
20 : USE cp_control_types, ONLY: dft_control_type
21 : USE cp_dbcsr_api, ONLY: &
22 : dbcsr_copy, dbcsr_filter, dbcsr_get_info, dbcsr_get_num_blocks, dbcsr_get_occupation, &
23 : dbcsr_iterator_blocks_left, dbcsr_iterator_next_block, dbcsr_iterator_start, &
24 : dbcsr_iterator_stop, dbcsr_iterator_type, dbcsr_multiply, dbcsr_p_type, dbcsr_release, &
25 : dbcsr_scale, dbcsr_set, dbcsr_type, dbcsr_verify_matrix
26 : USE cp_dbcsr_contrib, ONLY: dbcsr_checksum,&
27 : dbcsr_dot,&
28 : dbcsr_get_diag,&
29 : dbcsr_set_diag
30 : USE cp_dbcsr_operations, ONLY: copy_dbcsr_to_fm,&
31 : copy_fm_to_dbcsr,&
32 : cp_dbcsr_sm_fm_multiply,&
33 : cp_fm_to_dbcsr_row_template
34 : USE cp_fm_cholesky, ONLY: cp_fm_cholesky_decompose
35 : USE cp_fm_struct, ONLY: cp_fm_struct_create,&
36 : cp_fm_struct_get,&
37 : cp_fm_struct_release,&
38 : cp_fm_struct_type
39 : USE cp_fm_types, ONLY: &
40 : cp_fm_create, cp_fm_get_info, cp_fm_get_submatrix, cp_fm_init_random, cp_fm_release, &
41 : cp_fm_set_all, cp_fm_set_submatrix, cp_fm_to_fm, cp_fm_type
42 : USE cp_log_handling, ONLY: cp_get_default_logger,&
43 : cp_logger_get_default_io_unit,&
44 : cp_logger_type,&
45 : cp_to_string
46 : USE cp_output_handling, ONLY: cp_print_key_finished_output,&
47 : cp_print_key_unit_nr
48 : USE external_potential_types, ONLY: all_potential_type,&
49 : gth_potential_type,&
50 : sgp_potential_type
51 : USE hfx_types, ONLY: hfx_type
52 : USE input_constants, ONLY: &
53 : atomic_guess, core_guess, eht_guess, history_guess, mopac_guess, no_guess, random_guess, &
54 : restart_guess, sparse_guess
55 : USE input_cp2k_hfx, ONLY: ri_mo
56 : USE input_section_types, ONLY: section_vals_get_subs_vals,&
57 : section_vals_type,&
58 : section_vals_val_get
59 : USE kinds, ONLY: default_path_length,&
60 : dp
61 : USE kpoint_io, ONLY: read_kpoints_restart
62 : USE kpoint_types, ONLY: kpoint_type
63 : USE message_passing, ONLY: mp_para_env_type
64 : USE particle_methods, ONLY: get_particle_set
65 : USE particle_types, ONLY: particle_type
66 : USE qs_atomic_block, ONLY: calculate_atomic_block_dm
67 : USE qs_cneo_types, ONLY: cneo_potential_type
68 : USE qs_density_matrices, ONLY: calculate_density_matrix
69 : USE qs_dftb_utils, ONLY: get_dftb_atom_param
70 : USE qs_eht_guess, ONLY: calculate_eht_guess
71 : USE qs_environment_types, ONLY: get_qs_env,&
72 : qs_environment_type
73 : USE qs_kind_types, ONLY: get_qs_kind,&
74 : get_qs_kind_set,&
75 : qs_kind_type
76 : USE qs_mo_io, ONLY: read_mo_set_from_restart,&
77 : wfn_restart_file_name
78 : USE qs_mo_methods, ONLY: make_basis_lowdin,&
79 : make_basis_simple,&
80 : make_basis_sm
81 : USE qs_mo_occupation, ONLY: set_mo_occupation
82 : USE qs_mo_types, ONLY: get_mo_set,&
83 : mo_set_restrict,&
84 : mo_set_type,&
85 : reassign_allocated_mos
86 : USE qs_mom_methods, ONLY: do_mom_guess
87 : USE qs_rho_methods, ONLY: qs_rho_update_rho
88 : USE qs_rho_types, ONLY: qs_rho_get,&
89 : qs_rho_type
90 : USE qs_scf_methods, ONLY: eigensolver,&
91 : eigensolver_simple
92 : USE qs_scf_types, ONLY: block_davidson_diag_method_nr,&
93 : block_krylov_diag_method_nr,&
94 : general_diag_method_nr,&
95 : ot_diag_method_nr,&
96 : qs_scf_env_type
97 : USE qs_wf_history_methods, ONLY: wfi_update
98 : USE scf_control_types, ONLY: scf_control_type
99 : USE util, ONLY: sort
100 : USE xtb_types, ONLY: get_xtb_atom_param,&
101 : xtb_atom_type
102 : #include "./base/base_uses.f90"
103 :
104 : IMPLICIT NONE
105 :
106 : PRIVATE
107 :
108 : CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_initial_guess'
109 :
110 : PUBLIC :: calculate_first_density_matrix, calculate_mopac_dm
111 : PUBLIC :: calculate_atomic_fock_matrix
112 :
113 : TYPE atom_matrix_type
114 : REAL(KIND=dp), DIMENSION(:, :, :), POINTER :: mat => NULL()
115 : END TYPE atom_matrix_type
116 :
117 : CONTAINS
118 :
119 : ! **************************************************************************************************
120 : !> \brief can use a variety of methods to come up with an initial
121 : !> density matrix and optionally an initial wavefunction
122 : !> \param scf_env SCF environment information
123 : !> \param qs_env QS environment
124 : !> \par History
125 : !> 03.2006 moved here from qs_scf [Joost VandeVondele]
126 : !> 06.2007 allow to skip the initial guess [jgh]
127 : !> 08.2014 kpoints [JGH]
128 : !> 10.2019 tot_corr_zeff, switch_surf_dip [SGh]
129 : !> \note
130 : !> badly needs to be split in subroutines each doing one of the possible
131 : !> schemes
132 : ! **************************************************************************************************
133 6881 : SUBROUTINE calculate_first_density_matrix(scf_env, qs_env)
134 :
135 : TYPE(qs_scf_env_type), POINTER :: scf_env
136 : TYPE(qs_environment_type), POINTER :: qs_env
137 :
138 : CHARACTER(LEN=*), PARAMETER :: routineN = 'calculate_first_density_matrix'
139 :
140 : CHARACTER(LEN=default_path_length) :: file_name, filename
141 : INTEGER :: atom_a, density_guess, handle, homo, i, iatom, ic, icol, id_nr, ikind, irow, &
142 : iseed(4), ispin, istart_col, istart_row, j, last_read, n, n_cols, n_rows, nao, natom, &
143 : natoms, natoms_tmp, nblocks, nelectron, nmo, nmo_tmp, not_read, nsgf, nspin, nvec, ounit, &
144 : safe_density_guess, size_atomic_kind_set, z
145 6881 : INTEGER, ALLOCATABLE, DIMENSION(:) :: first_sgf, kind_of, last_sgf
146 : INTEGER, DIMENSION(2) :: nelectron_spin
147 6881 : INTEGER, DIMENSION(:), POINTER :: atom_list, elec_conf, nelec_kind, &
148 6881 : sort_kind
149 : LOGICAL :: cneo_potential_present, did_guess, do_hfx_ri_mo, do_kpoints, do_std_diag, exist, &
150 : has_unit_metric, natom_mismatch, need_mos, need_wm, ofgpw, owns_ortho, print_history_log, &
151 : print_log
152 6881 : REAL(dp), ALLOCATABLE, DIMENSION(:, :) :: buff, buff2
153 6881 : REAL(dp), DIMENSION(:, :), POINTER :: pdata
154 : REAL(KIND=dp) :: checksum, eps, length, maxocc, occ, &
155 : rscale, tot_corr_zeff, trps1, zeff
156 : REAL(KIND=dp), DIMENSION(0:3) :: edftb
157 6881 : TYPE(atom_matrix_type), DIMENSION(:), POINTER :: pmat
158 6881 : TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
159 : TYPE(atomic_kind_type), POINTER :: atomic_kind
160 : TYPE(cp_fm_struct_type), POINTER :: ao_ao_struct, ao_mo_struct
161 : TYPE(cp_fm_type) :: sv
162 6881 : TYPE(cp_fm_type), DIMENSION(:), POINTER :: work1
163 : TYPE(cp_fm_type), POINTER :: mo_coeff, moa, mob, ortho, work2
164 : TYPE(cp_logger_type), POINTER :: logger
165 : TYPE(dbcsr_iterator_type) :: iter
166 6881 : TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: h_core_sparse, matrix_ks, p_rmpv, &
167 6881 : s_sparse
168 6881 : TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_h_kp, matrix_ks_kp, matrix_s_kp, &
169 6881 : rho_ao_kp
170 : TYPE(dbcsr_type) :: mo_dbcsr, mo_tmp_dbcsr
171 : TYPE(dft_control_type), POINTER :: dft_control
172 : TYPE(gto_basis_set_type), POINTER :: orb_basis_set
173 6881 : TYPE(hfx_type), DIMENSION(:, :), POINTER :: x_data
174 : TYPE(kpoint_type), POINTER :: kpoints
175 6881 : TYPE(mo_set_type), DIMENSION(:), POINTER :: mo_array, mos_last_converged
176 : TYPE(mp_para_env_type), POINTER :: para_env
177 6881 : TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
178 6881 : TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
179 : TYPE(qs_kind_type), POINTER :: qs_kind
180 : TYPE(qs_rho_type), POINTER :: rho
181 : TYPE(scf_control_type), POINTER :: scf_control
182 : TYPE(section_vals_type), POINTER :: dft_section, input, subsys_section
183 :
184 13762 : logger => cp_get_default_logger()
185 6881 : NULLIFY (atomic_kind, qs_kind, mo_coeff, orb_basis_set, atomic_kind_set, &
186 6881 : qs_kind_set, particle_set, ortho, work2, work1, mo_array, s_sparse, &
187 6881 : scf_control, dft_control, p_rmpv, para_env, h_core_sparse, matrix_ks, rho, &
188 6881 : mos_last_converged)
189 6881 : NULLIFY (dft_section, input, subsys_section)
190 6881 : NULLIFY (matrix_s_kp, matrix_h_kp, matrix_ks_kp, rho_ao_kp)
191 6881 : NULLIFY (moa, mob)
192 6881 : NULLIFY (atom_list, elec_conf, kpoints)
193 : edftb = 0.0_dp
194 6881 : tot_corr_zeff = 0.0_dp
195 :
196 6881 : CALL timeset(routineN, handle)
197 :
198 : CALL get_qs_env(qs_env, &
199 : atomic_kind_set=atomic_kind_set, &
200 : qs_kind_set=qs_kind_set, &
201 : particle_set=particle_set, &
202 : mos=mo_array, &
203 : matrix_s_kp=matrix_s_kp, &
204 : matrix_h_kp=matrix_h_kp, &
205 : matrix_ks_kp=matrix_ks_kp, &
206 : input=input, &
207 : scf_control=scf_control, &
208 : dft_control=dft_control, &
209 : has_unit_metric=has_unit_metric, &
210 : do_kpoints=do_kpoints, &
211 : kpoints=kpoints, &
212 : rho=rho, &
213 : nelectron_spin=nelectron_spin, &
214 : para_env=para_env, &
215 6881 : x_data=x_data)
216 :
217 6881 : CALL qs_rho_get(rho, rho_ao_kp=rho_ao_kp)
218 :
219 6881 : IF (dft_control%switch_surf_dip) THEN
220 2 : CALL get_qs_env(qs_env, mos_last_converged=mos_last_converged)
221 : END IF
222 :
223 : ! just initialize the first image, the other density are set to zero
224 15325 : DO ispin = 1, dft_control%nspins
225 91281 : DO ic = 1, SIZE(rho_ao_kp, 2)
226 84400 : CALL dbcsr_set(rho_ao_kp(ispin, ic)%matrix, 0.0_dp)
227 : END DO
228 : END DO
229 6881 : s_sparse => matrix_s_kp(:, 1)
230 6881 : h_core_sparse => matrix_h_kp(:, 1)
231 6881 : matrix_ks => matrix_ks_kp(:, 1)
232 6881 : p_rmpv => rho_ao_kp(:, 1)
233 :
234 6881 : work1 => scf_env%scf_work1
235 6881 : work2 => scf_env%scf_work2
236 6881 : ortho => scf_env%ortho
237 :
238 6881 : dft_section => section_vals_get_subs_vals(input, "DFT")
239 :
240 6881 : nspin = dft_control%nspins
241 6881 : ofgpw = dft_control%qs_control%ofgpw
242 6881 : density_guess = scf_control%density_guess
243 6881 : do_std_diag = .FALSE.
244 :
245 6881 : do_hfx_ri_mo = .FALSE.
246 6881 : IF (ASSOCIATED(x_data)) THEN
247 1222 : IF (x_data(1, 1)%do_hfx_ri) THEN
248 128 : IF (x_data(1, 1)%ri_data%flavor == ri_mo) do_hfx_ri_mo = .TRUE.
249 : END IF
250 : END IF
251 :
252 6881 : IF (ASSOCIATED(scf_env%krylov_space)) do_std_diag = (scf_env%krylov_space%eps_std_diag > 0.0_dp)
253 :
254 : need_mos = scf_control%use_ot .OR. scf_env%method == ot_diag_method_nr .OR. &
255 : (scf_env%method == block_krylov_diag_method_nr .AND. .NOT. do_std_diag) &
256 : .OR. dft_control%do_admm .OR. scf_env%method == block_davidson_diag_method_nr &
257 6881 : .OR. do_hfx_ri_mo
258 :
259 6881 : safe_density_guess = atomic_guess
260 6881 : IF (dft_control%qs_control%semi_empirical .OR. dft_control%qs_control%dftb) THEN
261 802 : IF (density_guess == atomic_guess) density_guess = mopac_guess
262 : safe_density_guess = mopac_guess
263 : END IF
264 6881 : IF (dft_control%qs_control%xtb) THEN
265 1096 : IF (do_kpoints) THEN
266 220 : IF (density_guess == atomic_guess) density_guess = mopac_guess
267 : safe_density_guess = mopac_guess
268 : ELSE
269 876 : IF (density_guess == atomic_guess) density_guess = core_guess
270 : safe_density_guess = core_guess
271 : END IF
272 : END IF
273 :
274 6881 : IF (scf_control%use_ot .AND. &
275 : (.NOT. ((density_guess == random_guess) .OR. &
276 : (density_guess == atomic_guess) .OR. &
277 : (density_guess == core_guess) .OR. &
278 : (density_guess == mopac_guess) .OR. &
279 : (density_guess == eht_guess) .OR. &
280 : (density_guess == sparse_guess) .OR. &
281 : (((density_guess == restart_guess) .OR. &
282 : (density_guess == history_guess)) .AND. &
283 : (scf_control%level_shift == 0.0_dp))))) THEN
284 : CALL cp_abort(__LOCATION__, &
285 0 : "OT needs GUESS ATOMIC / CORE / RANDOM / SPARSE / RESTART / HISTORY RESTART: other options NYI")
286 : END IF
287 :
288 : ! if a restart was requested, check that the file exists,
289 : ! if not we fall back to an atomic guess. No kidding, the file name should remain
290 : ! in sync with read_mo_set_from_restart
291 6881 : id_nr = 0
292 6881 : IF (density_guess == restart_guess) THEN
293 : ! only check existence on I/O node, otherwise if file exists there but
294 : ! not on compute nodes, everything goes crazy even though only I/O
295 : ! node actually reads the file
296 588 : IF (do_kpoints) THEN
297 18 : IF (para_env%is_source()) THEN
298 9 : CALL wfn_restart_file_name(file_name, exist, dft_section, logger, kp=.TRUE.)
299 : END IF
300 : ELSE
301 570 : IF (para_env%is_source()) THEN
302 299 : CALL wfn_restart_file_name(file_name, exist, dft_section, logger)
303 : END IF
304 : END IF
305 588 : CALL para_env%bcast(exist)
306 588 : CALL para_env%bcast(file_name)
307 588 : IF (.NOT. exist) THEN
308 : CALL cp_warn(__LOCATION__, &
309 : "User requested to restart the wavefunction from the file named: "// &
310 : TRIM(file_name)//". This file does not exist. Please check the existence of"// &
311 : " the file or change properly the value of the keyword WFN_RESTART_FILE_NAME."// &
312 104 : " Calculation continues using ATOMIC GUESS. ")
313 104 : density_guess = safe_density_guess
314 : END IF
315 6293 : ELSE IF (density_guess == history_guess) THEN
316 2 : IF (do_kpoints) THEN
317 0 : CPABORT("calculate_first_density_matrix: history_guess not implemented for k-points")
318 : END IF
319 2 : IF (para_env%is_source()) THEN
320 1 : CALL wfn_restart_file_name(file_name, exist, dft_section, logger)
321 : END IF
322 2 : CALL para_env%bcast(exist)
323 2 : CALL para_env%bcast(file_name)
324 2 : nvec = qs_env%wf_history%memory_depth
325 2 : not_read = nvec + 1
326 : ! At this level we read the saved backup RESTART files..
327 6 : DO i = 1, nvec
328 4 : j = i - 1
329 4 : filename = TRIM(file_name)
330 4 : IF (j /= 0) THEN
331 2 : filename = TRIM(file_name)//".bak-"//ADJUSTL(cp_to_string(j))
332 : END IF
333 4 : IF (para_env%is_source()) &
334 2 : INQUIRE (FILE=filename, exist=exist)
335 4 : CALL para_env%bcast(exist)
336 6 : IF ((.NOT. exist) .AND. (i < not_read)) THEN
337 : not_read = i
338 : END IF
339 : END DO
340 2 : IF (not_read == 1) THEN
341 0 : density_guess = restart_guess
342 0 : filename = TRIM(file_name)
343 0 : IF (para_env%is_source()) INQUIRE (FILE=filename, exist=exist)
344 0 : CALL para_env%bcast(exist)
345 0 : IF (.NOT. exist) THEN
346 : CALL cp_warn(__LOCATION__, &
347 : "User requested to restart the wavefunction from a series of restart files named: "// &
348 : TRIM(file_name)//" with extensions (.bak-n). These files do not exist."// &
349 : " Even trying to switch to a plain restart wave-function failes because the"// &
350 : " file named: "//TRIM(file_name)//" does not exist. Please check the existence of"// &
351 : " the file or change properly the value of the keyword WFN_RESTART_FILE_NAME."// &
352 0 : " Calculation continues using ATOMIC GUESS. ")
353 0 : density_guess = safe_density_guess
354 : END IF
355 : END IF
356 2 : last_read = not_read - 1
357 : END IF
358 :
359 6881 : did_guess = .FALSE.
360 :
361 6881 : IF (dft_control%correct_el_density_dip) THEN
362 4 : tot_corr_zeff = qs_env%total_zeff_corr
363 : !WRITE(*,*) "tot_corr_zeff = ", tot_corr_zeff
364 4 : IF ((ABS(tot_corr_zeff) > 0.0_dp) .AND. (density_guess /= restart_guess)) THEN
365 : CALL cp_warn(__LOCATION__, &
366 : "Use SCF_GUESS RESTART in conjunction with "// &
367 : "CORE_CORRECTION /= 0.0 and SURFACE_DIPOLE_CORRECTION TRUE. "// &
368 : "It is always advisable to perform SURFACE_DIPOLE_CORRECTION "// &
369 : "after a simulation without the surface dipole correction "// &
370 4 : "and using the ensuing wavefunction restart file. ")
371 : END IF
372 : END IF
373 :
374 6881 : ounit = -1
375 6881 : print_log = .FALSE.
376 6881 : print_history_log = .FALSE.
377 6881 : IF (para_env%is_source()) THEN
378 : CALL section_vals_val_get(dft_section, &
379 : "SCF%PRINT%RESTART%LOG_PRINT_KEY", &
380 3478 : l_val=print_log)
381 : CALL section_vals_val_get(dft_section, &
382 : "SCF%PRINT%RESTART_HISTORY%LOG_PRINT_KEY", &
383 3478 : l_val=print_history_log)
384 3478 : IF (print_log .OR. print_history_log) THEN
385 13 : ounit = cp_logger_get_default_io_unit(logger)
386 : END IF
387 : END IF
388 :
389 6881 : IF (density_guess == restart_guess) THEN
390 484 : IF (ounit > 0) THEN
391 : WRITE (UNIT=ounit, FMT="(/,T2,A)") &
392 4 : "WFN_RESTART| Reading restart file"
393 : END IF
394 484 : IF (do_kpoints) THEN
395 10 : natoms = SIZE(particle_set)
396 : CALL read_kpoints_restart(rho_ao_kp, kpoints, work1, &
397 10 : natoms, para_env, id_nr, dft_section, natom_mismatch)
398 10 : IF (natom_mismatch) density_guess = safe_density_guess
399 : ELSE
400 : CALL read_mo_set_from_restart(mo_array, qs_kind_set, particle_set, para_env, &
401 : id_nr=id_nr, multiplicity=dft_control%multiplicity, &
402 : dft_section=dft_section, &
403 : natom_mismatch=natom_mismatch, &
404 474 : out_unit=ounit)
405 :
406 474 : IF (natom_mismatch) THEN
407 : density_guess = safe_density_guess
408 : ELSE
409 1260 : DO ispin = 1, nspin
410 806 : IF (scf_control%level_shift /= 0.0_dp) THEN
411 0 : CALL get_mo_set(mo_set=mo_array(ispin), mo_coeff=mo_coeff)
412 0 : CALL cp_fm_to_fm(mo_coeff, ortho)
413 : END IF
414 :
415 : ! make all nmo vectors present orthonormal
416 : CALL get_mo_set(mo_set=mo_array(ispin), &
417 806 : mo_coeff=mo_coeff, nmo=nmo, homo=homo)
418 :
419 806 : IF (has_unit_metric) THEN
420 4 : CALL make_basis_simple(mo_coeff, nmo)
421 802 : ELSEIF (dft_control%smear) THEN
422 : CALL make_basis_lowdin(vmatrix=mo_coeff, ncol=nmo, &
423 104 : matrix_s=s_sparse(1)%matrix)
424 : ELSE
425 : ! ortho so that one can restart for different positions (basis sets?)
426 698 : CALL make_basis_sm(mo_coeff, homo, s_sparse(1)%matrix)
427 : END IF
428 : ! only alpha spin is kept for restricted
429 2066 : IF (dft_control%restricted) EXIT
430 : END DO
431 474 : IF (dft_control%restricted) CALL mo_set_restrict(mo_array)
432 :
433 474 : IF (.NOT. scf_control%diagonalization%mom) THEN
434 458 : IF (dft_control%correct_surf_dip) THEN
435 0 : IF (ABS(tot_corr_zeff) > 0.0_dp) THEN
436 : CALL set_mo_occupation(mo_array, smear=qs_env%scf_control%smear, &
437 0 : tot_zeff_corr=tot_corr_zeff)
438 : ELSE
439 0 : CALL set_mo_occupation(mo_array, smear=qs_env%scf_control%smear)
440 : END IF
441 : ELSE
442 458 : CALL set_mo_occupation(mo_array, smear=qs_env%scf_control%smear)
443 : END IF
444 : END IF
445 :
446 1300 : DO ispin = 1, nspin
447 :
448 826 : IF (scf_control%use_ot .OR. scf_env%method == ot_diag_method_nr) THEN !fm->dbcsr
449 : CALL copy_fm_to_dbcsr(mo_array(ispin)%mo_coeff, &
450 564 : mo_array(ispin)%mo_coeff_b) !fm->dbcsr
451 : END IF !fm->dbcsr
452 :
453 : CALL calculate_density_matrix(mo_array(ispin), &
454 1300 : p_rmpv(ispin)%matrix)
455 : END DO
456 : END IF ! natom_mismatch
457 :
458 : END IF
459 :
460 : ! Maximum Overlap Method
461 484 : IF (scf_control%diagonalization%mom) THEN
462 16 : CALL do_mom_guess(nspin, mo_array, scf_control, p_rmpv)
463 : END IF
464 :
465 : did_guess = .TRUE.
466 : END IF
467 :
468 6881 : IF (density_guess == history_guess) THEN
469 2 : IF (not_read > 1) THEN
470 2 : IF (ounit > 0) THEN
471 : WRITE (UNIT=ounit, FMT="(/,T2,A)") &
472 1 : "WFN_RESTART| Reading restart file history"
473 : END IF
474 6 : DO i = 1, last_read
475 4 : j = last_read - i
476 : CALL read_mo_set_from_restart(mo_array, qs_kind_set, particle_set, para_env, &
477 : id_nr=j, multiplicity=dft_control%multiplicity, &
478 4 : dft_section=dft_section, out_unit=ounit)
479 :
480 8 : DO ispin = 1, nspin
481 4 : IF (scf_control%level_shift /= 0.0_dp) THEN
482 0 : CALL get_mo_set(mo_set=mo_array(ispin), mo_coeff=mo_coeff)
483 0 : CALL cp_fm_to_fm(mo_coeff, ortho)
484 : END IF
485 :
486 : ! make all nmo vectors present orthonormal
487 4 : CALL get_mo_set(mo_set=mo_array(ispin), mo_coeff=mo_coeff, nmo=nmo, homo=homo)
488 :
489 4 : IF (has_unit_metric) THEN
490 0 : CALL make_basis_simple(mo_coeff, nmo)
491 : ELSE
492 : ! ortho so that one can restart for different positions (basis sets?)
493 4 : CALL make_basis_sm(mo_coeff, homo, s_sparse(1)%matrix)
494 : END IF
495 : ! only alpha spin is kept for restricted
496 12 : IF (dft_control%restricted) EXIT
497 : END DO
498 4 : IF (dft_control%restricted) CALL mo_set_restrict(mo_array)
499 :
500 8 : DO ispin = 1, nspin
501 : CALL set_mo_occupation(mo_set=mo_array(ispin), &
502 8 : smear=qs_env%scf_control%smear)
503 : END DO
504 :
505 8 : DO ispin = 1, nspin
506 4 : IF (scf_control%use_ot .OR. scf_env%method == ot_diag_method_nr) THEN !fm->dbcsr
507 : CALL copy_fm_to_dbcsr(mo_array(ispin)%mo_coeff, &
508 4 : mo_array(ispin)%mo_coeff_b) !fm->dbcsr
509 : END IF !fm->dbcsr
510 8 : CALL calculate_density_matrix(mo_array(ispin), p_rmpv(ispin)%matrix)
511 : END DO
512 :
513 : ! Write to extrapolation pipeline
514 6 : CALL wfi_update(wf_history=qs_env%wf_history, qs_env=qs_env, dt=1.0_dp)
515 : END DO
516 : END IF
517 :
518 : did_guess = .TRUE.
519 : END IF
520 :
521 6881 : IF (density_guess == random_guess) THEN
522 :
523 52 : DO ispin = 1, nspin
524 : CALL get_mo_set(mo_set=mo_array(ispin), &
525 30 : mo_coeff=mo_coeff, nmo=nmo)
526 30 : CALL cp_fm_init_random(mo_coeff, nmo)
527 30 : IF (has_unit_metric) THEN
528 2 : CALL make_basis_simple(mo_coeff, nmo)
529 : ELSE
530 28 : CALL make_basis_sm(mo_coeff, nmo, s_sparse(1)%matrix)
531 : END IF
532 : ! only alpha spin is kept for restricted
533 82 : IF (dft_control%restricted) EXIT
534 : END DO
535 22 : IF (dft_control%restricted) CALL mo_set_restrict(mo_array)
536 :
537 52 : DO ispin = 1, nspin
538 : CALL set_mo_occupation(mo_set=mo_array(ispin), &
539 52 : smear=qs_env%scf_control%smear)
540 : END DO
541 :
542 52 : DO ispin = 1, nspin
543 :
544 30 : IF (scf_control%use_ot .OR. scf_env%method == ot_diag_method_nr) THEN !fm->dbcsr
545 : CALL copy_fm_to_dbcsr(mo_array(ispin)%mo_coeff, &
546 22 : mo_array(ispin)%mo_coeff_b) !fm->dbcsr
547 : END IF !fm->dbcsr
548 :
549 52 : CALL calculate_density_matrix(mo_array(ispin), p_rmpv(ispin)%matrix)
550 : END DO
551 :
552 : did_guess = .TRUE.
553 : END IF
554 :
555 6881 : IF (density_guess == core_guess) THEN
556 :
557 162 : IF (do_kpoints) THEN
558 0 : CPABORT("calculate_first_density_matrix: core_guess not implemented for k-points")
559 : END IF
560 :
561 162 : CALL get_qs_kind_set(qs_kind_set, cneo_potential_present=cneo_potential_present)
562 162 : IF (cneo_potential_present) THEN
563 0 : CPABORT("calculate_first_density_matrix: core_guess not implemented for CNEO")
564 : END IF
565 :
566 162 : owns_ortho = .FALSE.
567 162 : IF (.NOT. ASSOCIATED(work1)) THEN
568 44 : need_wm = .TRUE.
569 44 : CPASSERT(.NOT. ASSOCIATED(work2))
570 44 : CPASSERT(.NOT. ASSOCIATED(ortho))
571 : ELSE
572 118 : need_wm = .FALSE.
573 118 : CPASSERT(ASSOCIATED(work2))
574 118 : IF (.NOT. ASSOCIATED(ortho)) THEN
575 6 : ALLOCATE (ortho)
576 6 : owns_ortho = .TRUE.
577 : END IF
578 : END IF
579 :
580 162 : IF (need_wm) THEN
581 44 : CALL get_mo_set(mo_set=mo_array(1), mo_coeff=moa)
582 44 : CALL cp_fm_get_info(moa, matrix_struct=ao_mo_struct)
583 44 : CALL cp_fm_struct_get(ao_mo_struct, nrow_global=nao, nrow_block=nblocks)
584 : CALL cp_fm_struct_create(fmstruct=ao_ao_struct, &
585 : nrow_block=nblocks, &
586 : ncol_block=nblocks, &
587 : nrow_global=nao, &
588 : ncol_global=nao, &
589 44 : template_fmstruct=ao_mo_struct)
590 88 : ALLOCATE (work1(1))
591 44 : ALLOCATE (work2, ortho)
592 44 : CALL cp_fm_create(work1(1), ao_ao_struct)
593 44 : CALL cp_fm_create(work2, ao_ao_struct)
594 44 : CALL cp_fm_create(ortho, ao_ao_struct)
595 44 : CALL copy_dbcsr_to_fm(matrix_s_kp(1, 1)%matrix, ortho)
596 44 : CALL cp_fm_cholesky_decompose(ortho)
597 132 : CALL cp_fm_struct_release(ao_ao_struct)
598 : END IF
599 :
600 162 : ispin = 1
601 : ! Load core Hamiltonian into work matrix
602 162 : CALL copy_dbcsr_to_fm(h_core_sparse(1)%matrix, work1(ispin))
603 :
604 : ! Diagonalize the core Hamiltonian matrix and retrieve a first set of
605 : ! molecular orbitals (MOs)
606 162 : IF (has_unit_metric) THEN
607 : CALL eigensolver_simple(matrix_ks=work1(ispin), &
608 : mo_set=mo_array(ispin), &
609 : work=work2, &
610 : do_level_shift=.FALSE., &
611 : level_shift=0.0_dp, &
612 6 : use_jacobi=.FALSE., jacobi_threshold=0._dp)
613 : ELSE
614 : CALL eigensolver(matrix_ks_fm=work1(ispin), &
615 : mo_set=mo_array(ispin), &
616 : ortho=ortho, &
617 : work=work2, &
618 : cholesky_method=scf_env%cholesky_method, &
619 : do_level_shift=.FALSE., &
620 : level_shift=0.0_dp, &
621 156 : use_jacobi=.FALSE.)
622 : END IF
623 :
624 : ! Open shell case: copy alpha MOs to beta MOs
625 162 : IF (nspin == 2) THEN
626 22 : CALL get_mo_set(mo_set=mo_array(1), mo_coeff=moa)
627 22 : CALL get_mo_set(mo_set=mo_array(2), mo_coeff=mob, nmo=nmo)
628 22 : CALL cp_fm_to_fm(moa, mob, nmo)
629 : END IF
630 :
631 : ! Build an initial density matrix (for each spin in the case of
632 : ! an open shell calculation) from the first MOs set
633 346 : DO ispin = 1, nspin
634 184 : CALL set_mo_occupation(mo_set=mo_array(ispin), smear=scf_control%smear)
635 346 : CALL calculate_density_matrix(mo_array(ispin), p_rmpv(ispin)%matrix)
636 : END DO
637 :
638 : ! release intermediate matrices
639 162 : IF (need_wm) THEN
640 44 : CALL cp_fm_release(ortho)
641 44 : CALL cp_fm_release(work2)
642 44 : CALL cp_fm_release(work1(1))
643 44 : DEALLOCATE (ortho, work2)
644 44 : DEALLOCATE (work1)
645 44 : NULLIFY (work1, work2, ortho)
646 118 : ELSE IF (owns_ortho) THEN
647 6 : DEALLOCATE (ortho)
648 : END IF
649 :
650 : did_guess = .TRUE.
651 : END IF
652 :
653 6881 : IF (density_guess == atomic_guess) THEN
654 :
655 4429 : subsys_section => section_vals_get_subs_vals(input, "SUBSYS")
656 4429 : ounit = cp_print_key_unit_nr(logger, subsys_section, "PRINT%KINDS", extension=".Log")
657 4429 : IF (ounit > 0) THEN
658 : WRITE (UNIT=ounit, FMT="(/,(T2,A))") &
659 1004 : "Atomic guess: The first density matrix is obtained in terms of atomic orbitals", &
660 2008 : " and electronic configurations assigned to each atomic kind"
661 : END IF
662 :
663 : CALL calculate_atomic_block_dm(p_rmpv, s_sparse(1)%matrix, atomic_kind_set, qs_kind_set, &
664 4429 : nspin, nelectron_spin, ounit, para_env)
665 :
666 9833 : DO ispin = 1, nspin
667 :
668 : ! The orbital transformation method (OT) requires not only an
669 : ! initial density matrix, but also an initial wavefunction (MO set)
670 9833 : IF (ofgpw .AND. (scf_control%use_ot .OR. scf_env%method == ot_diag_method_nr)) THEN
671 : ! get orbitals later
672 : ELSE
673 5404 : IF (need_mos) THEN
674 :
675 2142 : IF (dft_control%restricted .AND. (ispin == 2)) THEN
676 22 : CALL mo_set_restrict(mo_array)
677 : ELSE
678 : CALL get_mo_set(mo_set=mo_array(ispin), &
679 : mo_coeff=mo_coeff, &
680 2120 : nmo=nmo, nao=nao, homo=homo)
681 :
682 2120 : CALL cp_fm_set_all(mo_coeff, 0.0_dp)
683 2120 : CALL cp_fm_init_random(mo_coeff, nmo)
684 :
685 2120 : CALL cp_fm_create(sv, mo_coeff%matrix_struct, "SV")
686 : ! multiply times PS
687 2120 : IF (has_unit_metric) THEN
688 0 : CALL cp_fm_to_fm(mo_coeff, sv)
689 : ELSE
690 : ! PS*C(:,1:nomo)+C(:,nomo+1:nmo) (nomo=NINT(nelectron/maxocc))
691 2120 : CALL cp_dbcsr_sm_fm_multiply(s_sparse(1)%matrix, mo_coeff, sv, nmo)
692 : END IF
693 2120 : CALL cp_dbcsr_sm_fm_multiply(p_rmpv(ispin)%matrix, sv, mo_coeff, homo)
694 :
695 2120 : CALL cp_fm_release(sv)
696 : ! and ortho the result
697 2120 : IF (has_unit_metric) THEN
698 0 : CALL make_basis_simple(mo_coeff, nmo)
699 : ELSE
700 2120 : CALL make_basis_sm(mo_coeff, nmo, s_sparse(1)%matrix)
701 : END IF
702 : END IF
703 :
704 : CALL set_mo_occupation(mo_set=mo_array(ispin), &
705 2142 : smear=qs_env%scf_control%smear)
706 :
707 : CALL copy_fm_to_dbcsr(mo_array(ispin)%mo_coeff, &
708 2142 : mo_array(ispin)%mo_coeff_b) !fm->dbcsr
709 :
710 : CALL calculate_density_matrix(mo_array(ispin), &
711 2142 : p_rmpv(ispin)%matrix)
712 : END IF
713 : ! adjust el_density in case surface_dipole_correction is switched
714 : ! on and CORE_CORRECTION is non-zero
715 5404 : IF (scf_env%method == general_diag_method_nr) THEN
716 3520 : IF (dft_control%correct_surf_dip) THEN
717 8 : IF (ABS(tot_corr_zeff) > 0.0_dp) THEN
718 : CALL get_mo_set(mo_set=mo_array(ispin), &
719 : mo_coeff=mo_coeff, &
720 6 : nmo=nmo, nao=nao, homo=homo)
721 :
722 6 : CALL cp_fm_set_all(mo_coeff, 0.0_dp)
723 6 : CALL cp_fm_init_random(mo_coeff, nmo)
724 :
725 6 : CALL cp_fm_create(sv, mo_coeff%matrix_struct, "SV")
726 : ! multiply times PS
727 6 : IF (has_unit_metric) THEN
728 0 : CALL cp_fm_to_fm(mo_coeff, sv)
729 : ELSE
730 : ! PS*C(:,1:nomo)+C(:,nomo+1:nmo) (nomo=NINT(nelectron/maxocc))
731 6 : CALL cp_dbcsr_sm_fm_multiply(s_sparse(1)%matrix, mo_coeff, sv, nmo)
732 : END IF
733 6 : CALL cp_dbcsr_sm_fm_multiply(p_rmpv(ispin)%matrix, sv, mo_coeff, homo)
734 :
735 6 : CALL cp_fm_release(sv)
736 : ! and ortho the result
737 6 : IF (has_unit_metric) THEN
738 0 : CALL make_basis_simple(mo_coeff, nmo)
739 : ELSE
740 6 : CALL make_basis_sm(mo_coeff, nmo, s_sparse(1)%matrix)
741 : END IF
742 :
743 : CALL set_mo_occupation(mo_set=mo_array(ispin), smear=qs_env%scf_control%smear, &
744 6 : tot_zeff_corr=tot_corr_zeff)
745 :
746 : CALL calculate_density_matrix(mo_array(ispin), &
747 6 : p_rmpv(ispin)%matrix)
748 : END IF
749 : END IF
750 : END IF
751 :
752 : END IF
753 :
754 : END DO
755 :
756 4429 : IF (ofgpw .AND. (scf_control%use_ot .OR. scf_env%method == ot_diag_method_nr)) THEN
757 : ! We fit a function to the square root of the density
758 0 : CALL qs_rho_update_rho(rho, qs_env)
759 4429 : CPASSERT(1 == 0)
760 : ! CALL cp_fm_create(sv,mo_coeff%matrix_struct,"SV")
761 : ! DO ispin=1,nspin
762 : ! CALL integrate_ppl_rspace(qs%rho%rho_r(ispin),qs_env)
763 : ! CALL cp_cfm_solve(overlap,mos)
764 : ! CALL get_mo_set(mo_set=mo_array(ispin),&
765 : ! mo_coeff=mo_coeff, nmo=nmo, nao=nao)
766 : ! CALL cp_fm_init_random(mo_coeff,nmo)
767 : ! END DO
768 : ! CALL cp_fm_release(sv)
769 : END IF
770 :
771 4429 : IF (scf_control%diagonalization%mom) THEN
772 4 : CALL do_mom_guess(nspin, mo_array, scf_control, p_rmpv)
773 : END IF
774 :
775 : CALL cp_print_key_finished_output(ounit, logger, subsys_section, &
776 4429 : "PRINT%KINDS")
777 :
778 4429 : did_guess = .TRUE.
779 : END IF
780 :
781 6881 : IF (density_guess == sparse_guess) THEN
782 :
783 0 : IF (ofgpw) CPABORT("SPARSE_GUESS not implemented for OFGPW")
784 0 : IF (.NOT. scf_control%use_ot) CPABORT("OT needed!")
785 0 : IF (do_kpoints) THEN
786 0 : CPABORT("calculate_first_density_matrix: sparse_guess not implemented for k-points")
787 : END IF
788 :
789 0 : eps = 1.0E-5_dp
790 :
791 0 : ounit = cp_logger_get_default_io_unit(logger)
792 0 : natoms = SIZE(particle_set)
793 0 : ALLOCATE (kind_of(natoms))
794 0 : ALLOCATE (first_sgf(natoms), last_sgf(natoms))
795 :
796 0 : checksum = dbcsr_checksum(s_sparse(1)%matrix)
797 0 : i = dbcsr_get_num_blocks(s_sparse(1)%matrix); CALL para_env%sum(i)
798 0 : IF (ounit > 0) WRITE (ounit, *) 'S nblks', i, ' checksum', checksum
799 0 : CALL dbcsr_filter(s_sparse(1)%matrix, eps)
800 0 : checksum = dbcsr_checksum(s_sparse(1)%matrix)
801 0 : i = dbcsr_get_num_blocks(s_sparse(1)%matrix); CALL para_env%sum(i)
802 0 : IF (ounit > 0) WRITE (ounit, *) 'S nblks', i, ' checksum', checksum
803 :
804 : CALL get_particle_set(particle_set, qs_kind_set, first_sgf=first_sgf, &
805 0 : last_sgf=last_sgf)
806 0 : CALL get_atomic_kind_set(atomic_kind_set=atomic_kind_set, kind_of=kind_of)
807 :
808 0 : ALLOCATE (pmat(SIZE(atomic_kind_set)))
809 :
810 0 : rscale = 1._dp
811 0 : IF (nspin == 2) rscale = 0.5_dp
812 0 : DO ikind = 1, SIZE(atomic_kind_set)
813 0 : atomic_kind => atomic_kind_set(ikind)
814 0 : qs_kind => qs_kind_set(ikind)
815 0 : NULLIFY (pmat(ikind)%mat)
816 0 : CALL calculate_atomic_orbitals(atomic_kind, qs_kind, pmat=pmat(ikind)%mat)
817 0 : NULLIFY (atomic_kind)
818 : END DO
819 :
820 0 : DO ispin = 1, nspin
821 : CALL get_mo_set(mo_set=mo_array(ispin), &
822 : maxocc=maxocc, &
823 0 : nelectron=nelectron)
824 : !
825 0 : CALL dbcsr_iterator_start(iter, p_rmpv(ispin)%matrix)
826 0 : DO WHILE (dbcsr_iterator_blocks_left(iter))
827 0 : CALL dbcsr_iterator_next_block(iter, irow, icol, pdata)
828 0 : ikind = kind_of(irow)
829 0 : IF (icol == irow) THEN
830 0 : IF (ispin == 1) THEN
831 : pdata(:, :) = pmat(ikind)%mat(:, :, 1)*rscale + &
832 0 : pmat(ikind)%mat(:, :, 2)*rscale
833 : ELSE
834 : pdata(:, :) = pmat(ikind)%mat(:, :, 1)*rscale - &
835 0 : pmat(ikind)%mat(:, :, 2)*rscale
836 : END IF
837 : END IF
838 : END DO
839 0 : CALL dbcsr_iterator_stop(iter)
840 :
841 : !CALL dbcsr_verify_matrix(p_rmpv(ispin)%matrix)
842 0 : checksum = dbcsr_checksum(p_rmpv(ispin)%matrix)
843 0 : occ = dbcsr_get_occupation(p_rmpv(ispin)%matrix)
844 0 : IF (ounit > 0) WRITE (ounit, *) 'P_init occ', occ, ' checksum', checksum
845 : ! so far p needs to have the same sparsity as S
846 : !CALL dbcsr_filter(p_rmpv(ispin)%matrix, eps)
847 : !CALL dbcsr_verify_matrix(p_rmpv(ispin)%matrix)
848 0 : checksum = dbcsr_checksum(p_rmpv(ispin)%matrix)
849 0 : occ = dbcsr_get_occupation(p_rmpv(ispin)%matrix)
850 0 : IF (ounit > 0) WRITE (ounit, *) 'P_init occ', occ, ' checksum', checksum
851 :
852 0 : CALL dbcsr_dot(p_rmpv(ispin)%matrix, s_sparse(1)%matrix, trps1)
853 0 : rscale = REAL(nelectron, dp)/trps1
854 0 : CALL dbcsr_scale(p_rmpv(ispin)%matrix, rscale)
855 :
856 : !CALL dbcsr_verify_matrix(p_rmpv(ispin)%matrix)
857 0 : checksum = dbcsr_checksum(p_rmpv(ispin)%matrix)
858 0 : occ = dbcsr_get_occupation(p_rmpv(ispin)%matrix)
859 0 : IF (ounit > 0) WRITE (ounit, *) 'P occ', occ, ' checksum', checksum
860 : !
861 : ! The orbital transformation method (OT) requires not only an
862 : ! initial density matrix, but also an initial wavefunction (MO set)
863 0 : IF (dft_control%restricted .AND. (ispin == 2)) THEN
864 0 : CALL mo_set_restrict(mo_array)
865 : ELSE
866 : CALL get_mo_set(mo_set=mo_array(ispin), &
867 : mo_coeff=mo_coeff, &
868 0 : nmo=nmo, nao=nao, homo=homo)
869 0 : CALL cp_fm_set_all(mo_coeff, 0.0_dp)
870 :
871 0 : n = MAXVAL(last_sgf - first_sgf) + 1
872 0 : size_atomic_kind_set = SIZE(atomic_kind_set)
873 :
874 0 : ALLOCATE (buff(n, n), sort_kind(size_atomic_kind_set), &
875 0 : nelec_kind(size_atomic_kind_set))
876 : !
877 : ! sort kind vs nbr electron
878 0 : DO ikind = 1, size_atomic_kind_set
879 0 : atomic_kind => atomic_kind_set(ikind)
880 0 : qs_kind => qs_kind_set(ikind)
881 : CALL get_atomic_kind(atomic_kind=atomic_kind, &
882 : natom=natom, &
883 : atom_list=atom_list, &
884 0 : z=z)
885 : CALL get_qs_kind(qs_kind, nsgf=nsgf, elec_conf=elec_conf, &
886 0 : basis_set=orb_basis_set, zeff=zeff)
887 0 : nelec_kind(ikind) = SUM(elec_conf)
888 : END DO
889 0 : CALL sort(nelec_kind, size_atomic_kind_set, sort_kind)
890 : !
891 : ! a -very- naive sparse guess
892 0 : nmo_tmp = nmo
893 0 : natoms_tmp = natoms
894 0 : istart_col = 1
895 0 : iseed(1) = 4; iseed(2) = 3; iseed(3) = 2; iseed(4) = 1 ! set the seed for dlarnv
896 0 : DO i = 1, size_atomic_kind_set
897 0 : ikind = sort_kind(i)
898 0 : atomic_kind => atomic_kind_set(ikind)
899 : CALL get_atomic_kind(atomic_kind=atomic_kind, &
900 0 : natom=natom, atom_list=atom_list)
901 0 : DO iatom = 1, natom
902 : !
903 0 : atom_a = atom_list(iatom)
904 0 : istart_row = first_sgf(atom_a)
905 0 : n_rows = last_sgf(atom_a) - first_sgf(atom_a) + 1
906 : !
907 : ! compute the "potential" nbr of states for this atom
908 0 : n_cols = MAX(INT(REAL(nmo_tmp, dp)/REAL(natoms_tmp, dp)), 1)
909 0 : IF (n_cols > n_rows) n_cols = n_rows
910 : !
911 0 : nmo_tmp = nmo_tmp - n_cols
912 0 : natoms_tmp = natoms_tmp - 1
913 0 : IF (nmo_tmp < 0 .OR. natoms_tmp < 0) THEN
914 0 : CPABORT("Wrong1!")
915 : END IF
916 0 : DO j = 1, n_cols
917 0 : CALL dlarnv(1, iseed, n_rows, buff(1, j))
918 : END DO
919 : CALL cp_fm_set_submatrix(mo_coeff, buff, istart_row, istart_col, &
920 0 : n_rows, n_cols)
921 0 : istart_col = istart_col + n_cols
922 : END DO
923 : END DO
924 :
925 0 : IF (istart_col <= nmo) THEN
926 0 : CPABORT("Wrong2!")
927 : END IF
928 :
929 0 : DEALLOCATE (buff, nelec_kind, sort_kind)
930 :
931 : IF (.FALSE.) THEN
932 : ALLOCATE (buff(nao, 1), buff2(nao, 1))
933 : DO i = 1, nmo
934 : CALL cp_fm_get_submatrix(mo_coeff, buff, 1, i, nao, 1)
935 : IF (SUM(buff**2) < 1E-10_dp) THEN
936 : WRITE (*, *) 'wrong', i, SUM(buff**2)
937 : END IF
938 : length = SQRT(DOT_PRODUCT(buff(:, 1), buff(:, 1)))
939 : buff(:, :) = buff(:, :)/length
940 : DO j = i + 1, nmo
941 : CALL cp_fm_get_submatrix(mo_coeff, buff2, 1, j, nao, 1)
942 : length = SQRT(DOT_PRODUCT(buff2(:, 1), buff2(:, 1)))
943 : buff2(:, :) = buff2(:, :)/length
944 : IF (ABS(DOT_PRODUCT(buff(:, 1), buff2(:, 1)) - 1.0_dp) < 1E-10_dp) THEN
945 : WRITE (*, *) 'wrong2', i, j, DOT_PRODUCT(buff(:, 1), buff2(:, 1))
946 : DO ikind = 1, nao
947 : IF (ABS(mo_coeff%local_data(ikind, i)) > 1e-10_dp) THEN
948 : WRITE (*, *) 'c1', ikind, mo_coeff%local_data(ikind, i)
949 : END IF
950 : IF (ABS(mo_coeff%local_data(ikind, j)) > 1e-10_dp) THEN
951 : WRITE (*, *) 'c2', ikind, mo_coeff%local_data(ikind, j)
952 : END IF
953 : END DO
954 : CPABORT("")
955 : END IF
956 : END DO
957 : END DO
958 : DEALLOCATE (buff, buff2)
959 :
960 : END IF
961 : !
962 0 : CALL cp_fm_to_dbcsr_row_template(mo_dbcsr, mo_coeff, s_sparse(1)%matrix)
963 : !CALL dbcsr_verify_matrix(mo_dbcsr)
964 0 : checksum = dbcsr_checksum(mo_dbcsr)
965 :
966 0 : occ = dbcsr_get_occupation(mo_dbcsr)
967 0 : IF (ounit > 0) WRITE (ounit, *) 'C occ', occ, ' checksum', checksum
968 0 : CALL dbcsr_filter(mo_dbcsr, eps)
969 : !CALL dbcsr_verify_matrix(mo_dbcsr)
970 0 : occ = dbcsr_get_occupation(mo_dbcsr)
971 0 : checksum = dbcsr_checksum(mo_dbcsr)
972 0 : IF (ounit > 0) WRITE (ounit, *) 'C occ', occ, ' checksum', checksum
973 : !
974 : ! multiply times PS
975 0 : IF (has_unit_metric) THEN
976 0 : CPABORT("has_unit_metric will be removed soon")
977 : END IF
978 : !
979 : ! S*C
980 0 : CALL dbcsr_copy(mo_tmp_dbcsr, mo_dbcsr, name="mo_tmp")
981 : CALL dbcsr_multiply("N", "N", 1.0_dp, s_sparse(1)%matrix, mo_dbcsr, &
982 : 0.0_dp, mo_tmp_dbcsr, &
983 0 : retain_sparsity=.TRUE.)
984 : !CALL dbcsr_verify_matrix(mo_tmp_dbcsr)
985 0 : checksum = dbcsr_checksum(mo_tmp_dbcsr)
986 0 : occ = dbcsr_get_occupation(mo_tmp_dbcsr)
987 0 : IF (ounit > 0) WRITE (ounit, *) 'S*C occ', occ, ' checksum', checksum
988 0 : CALL dbcsr_filter(mo_tmp_dbcsr, eps)
989 : !CALL dbcsr_verify_matrix(mo_tmp_dbcsr)
990 0 : checksum = dbcsr_checksum(mo_tmp_dbcsr)
991 0 : occ = dbcsr_get_occupation(mo_tmp_dbcsr)
992 0 : IF (ounit > 0) WRITE (ounit, *) 'S*C occ', occ, ' checksum', checksum
993 : !
994 : ! P*SC
995 : ! the destroy is needed for the moment to avoid memory leaks !
996 : ! This one is not needed because _destroy takes care of zeroing.
997 : CALL dbcsr_multiply("N", "N", 1.0_dp, p_rmpv(ispin)%matrix, &
998 0 : mo_tmp_dbcsr, 0.0_dp, mo_dbcsr)
999 : IF (.FALSE.) CALL dbcsr_verify_matrix(mo_dbcsr)
1000 0 : checksum = dbcsr_checksum(mo_dbcsr)
1001 0 : occ = dbcsr_get_occupation(mo_dbcsr)
1002 0 : IF (ounit > 0) WRITE (ounit, *) 'P*SC occ', occ, ' checksum', checksum
1003 0 : CALL dbcsr_filter(mo_dbcsr, eps)
1004 : !CALL dbcsr_verify_matrix(mo_dbcsr)
1005 0 : checksum = dbcsr_checksum(mo_dbcsr)
1006 0 : occ = dbcsr_get_occupation(mo_dbcsr)
1007 0 : IF (ounit > 0) WRITE (ounit, *) 'P*SC occ', occ, ' checksum', checksum
1008 : !
1009 0 : CALL copy_dbcsr_to_fm(mo_dbcsr, mo_coeff)
1010 :
1011 0 : CALL dbcsr_release(mo_dbcsr)
1012 0 : CALL dbcsr_release(mo_tmp_dbcsr)
1013 :
1014 : ! and ortho the result
1015 0 : CALL make_basis_sm(mo_coeff, nmo, s_sparse(1)%matrix)
1016 : END IF
1017 :
1018 : CALL set_mo_occupation(mo_set=mo_array(ispin), &
1019 0 : smear=qs_env%scf_control%smear)
1020 :
1021 : CALL copy_fm_to_dbcsr(mo_array(ispin)%mo_coeff, &
1022 0 : mo_array(ispin)%mo_coeff_b) !fm->dbcsr
1023 :
1024 : CALL calculate_density_matrix(mo_array(ispin), &
1025 0 : p_rmpv(ispin)%matrix)
1026 0 : DO ikind = 1, SIZE(atomic_kind_set)
1027 0 : IF (ASSOCIATED(pmat(ikind)%mat)) THEN
1028 0 : DEALLOCATE (pmat(ikind)%mat)
1029 : END IF
1030 : END DO
1031 : END DO
1032 :
1033 0 : DEALLOCATE (pmat)
1034 :
1035 0 : DEALLOCATE (kind_of)
1036 :
1037 0 : DEALLOCATE (first_sgf, last_sgf)
1038 :
1039 0 : did_guess = .TRUE.
1040 : END IF
1041 6881 : IF (density_guess == mopac_guess) THEN
1042 :
1043 : CALL calculate_mopac_dm(p_rmpv, s_sparse(1)%matrix, has_unit_metric, dft_control, &
1044 : particle_set, atomic_kind_set, qs_kind_set, &
1045 842 : nspin, nelectron_spin, para_env)
1046 :
1047 1754 : DO ispin = 1, nspin
1048 : ! The orbital transformation method (OT) requires not only an
1049 : ! initial density matrix, but also an initial wavefunction (MO set)
1050 1754 : IF (need_mos) THEN
1051 224 : IF (dft_control%restricted .AND. (ispin == 2)) THEN
1052 2 : CALL mo_set_restrict(mo_array)
1053 : ELSE
1054 : CALL get_mo_set(mo_set=mo_array(ispin), &
1055 : mo_coeff=mo_coeff, &
1056 222 : nmo=nmo, homo=homo)
1057 222 : CALL cp_fm_init_random(mo_coeff, nmo)
1058 222 : CALL cp_fm_create(sv, mo_coeff%matrix_struct, "SV")
1059 : ! multiply times PS
1060 222 : IF (has_unit_metric) THEN
1061 178 : CALL cp_fm_to_fm(mo_coeff, sv)
1062 : ELSE
1063 44 : CALL cp_dbcsr_sm_fm_multiply(s_sparse(1)%matrix, mo_coeff, sv, nmo)
1064 : END IF
1065 : ! here we could easily multiply with the diag that we actually have replicated already
1066 222 : CALL cp_dbcsr_sm_fm_multiply(p_rmpv(ispin)%matrix, sv, mo_coeff, homo)
1067 222 : CALL cp_fm_release(sv)
1068 : ! and ortho the result
1069 222 : IF (has_unit_metric) THEN
1070 178 : CALL make_basis_simple(mo_coeff, nmo)
1071 : ELSE
1072 44 : CALL make_basis_sm(mo_coeff, nmo, s_sparse(1)%matrix)
1073 : END IF
1074 : END IF
1075 :
1076 : CALL set_mo_occupation(mo_set=mo_array(ispin), &
1077 224 : smear=qs_env%scf_control%smear)
1078 : CALL copy_fm_to_dbcsr(mo_array(ispin)%mo_coeff, &
1079 224 : mo_array(ispin)%mo_coeff_b)
1080 :
1081 : CALL calculate_density_matrix(mo_array(ispin), &
1082 224 : p_rmpv(ispin)%matrix)
1083 : END IF
1084 : END DO
1085 :
1086 : did_guess = .TRUE.
1087 : END IF
1088 : !
1089 : ! EHT guess (gfn0-xTB)
1090 6881 : IF (density_guess == eht_guess) THEN
1091 4 : CALL calculate_eht_guess(qs_env, mo_array)
1092 8 : DO ispin = 1, nspin
1093 8 : CALL calculate_density_matrix(mo_array(ispin), p_rmpv(ispin)%matrix)
1094 : END DO
1095 : did_guess = .TRUE.
1096 : END IF
1097 : ! switch_surf_dip [SGh]
1098 6881 : IF (dft_control%switch_surf_dip) THEN
1099 4 : DO ispin = 1, nspin
1100 : CALL reassign_allocated_mos(mos_last_converged(ispin), &
1101 4 : mo_array(ispin))
1102 : END DO
1103 : END IF
1104 :
1105 6881 : IF (density_guess == no_guess) THEN
1106 : did_guess = .TRUE.
1107 : END IF
1108 :
1109 5945 : IF (.NOT. did_guess) THEN
1110 0 : CPABORT("An invalid keyword for the initial density guess was specified")
1111 : END IF
1112 :
1113 6881 : CALL timestop(handle)
1114 :
1115 13762 : END SUBROUTINE calculate_first_density_matrix
1116 :
1117 : ! **************************************************************************************************
1118 : !> \brief returns a block diagonal fock matrix.
1119 : !> \param matrix_f ...
1120 : !> \param atomic_kind_set ...
1121 : !> \param qs_kind_set ...
1122 : !> \param ounit ...
1123 : ! **************************************************************************************************
1124 98 : SUBROUTINE calculate_atomic_fock_matrix(matrix_f, atomic_kind_set, qs_kind_set, ounit)
1125 : TYPE(dbcsr_type), INTENT(INOUT) :: matrix_f
1126 : TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
1127 : TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
1128 : INTEGER, INTENT(IN) :: ounit
1129 :
1130 : CHARACTER(LEN=*), PARAMETER :: routineN = 'calculate_atomic_fock_matrix'
1131 :
1132 : INTEGER :: handle, icol, ikind, irow
1133 98 : INTEGER, ALLOCATABLE, DIMENSION(:) :: kind_of
1134 98 : REAL(dp), DIMENSION(:, :), POINTER :: block
1135 98 : TYPE(atom_matrix_type), ALLOCATABLE, DIMENSION(:) :: fmat
1136 : TYPE(atomic_kind_type), POINTER :: atomic_kind
1137 : TYPE(dbcsr_iterator_type) :: iter
1138 : TYPE(qs_kind_type), POINTER :: qs_kind
1139 :
1140 98 : CALL timeset(routineN, handle)
1141 :
1142 98 : CALL get_atomic_kind_set(atomic_kind_set=atomic_kind_set, kind_of=kind_of)
1143 432 : ALLOCATE (fmat(SIZE(atomic_kind_set)))
1144 :
1145 : ! precompute the atomic blocks for each atomic-kind
1146 236 : DO ikind = 1, SIZE(atomic_kind_set)
1147 138 : atomic_kind => atomic_kind_set(ikind)
1148 138 : qs_kind => qs_kind_set(ikind)
1149 138 : NULLIFY (fmat(ikind)%mat)
1150 138 : IF (ounit > 0) WRITE (UNIT=ounit, FMT="(/,T2,A)") &
1151 69 : "Calculating atomic Fock matrix for atomic kind: "//TRIM(atomic_kind%name)
1152 :
1153 : !Currently only ispin=1 is supported
1154 : CALL calculate_atomic_orbitals(atomic_kind, qs_kind, iunit=ounit, &
1155 236 : fmat=fmat(ikind)%mat)
1156 : END DO
1157 :
1158 : ! zero result matrix
1159 98 : CALL dbcsr_set(matrix_f, 0.0_dp)
1160 :
1161 : ! copy precomputed blocks onto diagonal of result matrix
1162 98 : CALL dbcsr_iterator_start(iter, matrix_f)
1163 217 : DO WHILE (dbcsr_iterator_blocks_left(iter))
1164 119 : CALL dbcsr_iterator_next_block(iter, irow, icol, block)
1165 119 : ikind = kind_of(irow)
1166 6937 : IF (icol == irow) block(:, :) = fmat(ikind)%mat(:, :, 1)
1167 : END DO
1168 98 : CALL dbcsr_iterator_stop(iter)
1169 :
1170 : ! cleanup
1171 236 : DO ikind = 1, SIZE(atomic_kind_set)
1172 236 : DEALLOCATE (fmat(ikind)%mat)
1173 : END DO
1174 98 : DEALLOCATE (fmat)
1175 :
1176 98 : CALL timestop(handle)
1177 :
1178 294 : END SUBROUTINE calculate_atomic_fock_matrix
1179 :
1180 : ! **************************************************************************************************
1181 : !> \brief returns a block diagonal density matrix. Blocks correspond to the mopac initial guess.
1182 : !> \param pmat ...
1183 : !> \param matrix_s ...
1184 : !> \param has_unit_metric ...
1185 : !> \param dft_control ...
1186 : !> \param particle_set ...
1187 : !> \param atomic_kind_set ...
1188 : !> \param qs_kind_set ...
1189 : !> \param nspin ...
1190 : !> \param nelectron_spin ...
1191 : !> \param para_env ...
1192 : ! **************************************************************************************************
1193 918 : SUBROUTINE calculate_mopac_dm(pmat, matrix_s, has_unit_metric, &
1194 : dft_control, particle_set, atomic_kind_set, qs_kind_set, &
1195 918 : nspin, nelectron_spin, para_env)
1196 : TYPE(dbcsr_p_type), DIMENSION(:), INTENT(INOUT) :: pmat
1197 : TYPE(dbcsr_type), INTENT(INOUT) :: matrix_s
1198 : LOGICAL :: has_unit_metric
1199 : TYPE(dft_control_type), POINTER :: dft_control
1200 : TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
1201 : TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
1202 : TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
1203 : INTEGER, INTENT(IN) :: nspin
1204 : INTEGER, DIMENSION(:), INTENT(IN) :: nelectron_spin
1205 : TYPE(mp_para_env_type) :: para_env
1206 :
1207 : CHARACTER(LEN=*), PARAMETER :: routineN = 'calculate_mopac_dm'
1208 :
1209 : INTEGER :: atom_a, handle, iatom, ikind, iset, &
1210 : isgf, isgfa, ishell, ispin, la, maxl, &
1211 : maxll, na, nao, natom, ncount, nset, &
1212 : nsgf, z
1213 : INTEGER, ALLOCATABLE, DIMENSION(:) :: first_sgf
1214 : INTEGER, DIMENSION(25) :: laox, naox
1215 : INTEGER, DIMENSION(5) :: occupation
1216 918 : INTEGER, DIMENSION(:), POINTER :: atom_list, elec_conf, nshell
1217 918 : INTEGER, DIMENSION(:, :), POINTER :: first_sgfa, l, last_sgfa
1218 : LOGICAL :: has_pot
1219 : REAL(KIND=dp) :: maxocc, my_sum, nelec, occ, paa, rscale, &
1220 : trps1, trps2, yy, zeff
1221 918 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: econf, pdiag, sdiag
1222 : REAL(KIND=dp), DIMENSION(0:3) :: edftb
1223 : TYPE(all_potential_type), POINTER :: all_potential
1224 : TYPE(cneo_potential_type), POINTER :: cneo_potential
1225 : TYPE(dbcsr_type), POINTER :: matrix_p
1226 : TYPE(gth_potential_type), POINTER :: gth_potential
1227 : TYPE(gto_basis_set_type), POINTER :: orb_basis_set
1228 : TYPE(sgp_potential_type), POINTER :: sgp_potential
1229 : TYPE(xtb_atom_type), POINTER :: xtb_kind
1230 :
1231 918 : CALL timeset(routineN, handle)
1232 :
1233 1906 : DO ispin = 1, nspin
1234 988 : matrix_p => pmat(ispin)%matrix
1235 1906 : CALL dbcsr_set(matrix_p, 0.0_dp)
1236 : END DO
1237 :
1238 918 : natom = SIZE(particle_set)
1239 918 : CALL dbcsr_get_info(pmat(1)%matrix, nfullrows_total=nao)
1240 918 : IF (nspin == 1) THEN
1241 : maxocc = 2.0_dp
1242 : ELSE
1243 70 : maxocc = 1.0_dp
1244 : END IF
1245 :
1246 2754 : ALLOCATE (first_sgf(natom))
1247 :
1248 918 : CALL get_particle_set(particle_set, qs_kind_set, first_sgf=first_sgf)
1249 918 : CALL get_qs_kind_set(qs_kind_set, maxlgto=maxl)
1250 :
1251 2754 : ALLOCATE (econf(0:maxl))
1252 :
1253 2754 : ALLOCATE (pdiag(nao))
1254 33922 : pdiag(:) = 0.0_dp
1255 :
1256 1836 : ALLOCATE (sdiag(nao))
1257 :
1258 33922 : sdiag(:) = 0.0_dp
1259 918 : IF (has_unit_metric) THEN
1260 12560 : sdiag(:) = 1.0_dp
1261 : ELSE
1262 556 : CALL dbcsr_get_diag(matrix_s, sdiag)
1263 556 : CALL para_env%sum(sdiag)
1264 : END IF
1265 :
1266 : ncount = 0
1267 33922 : trps1 = 0.0_dp
1268 33922 : trps2 = 0.0_dp
1269 33922 : pdiag(:) = 0.0_dp
1270 :
1271 2754 : IF (SUM(nelectron_spin) /= 0) THEN
1272 3038 : DO ikind = 1, SIZE(atomic_kind_set)
1273 :
1274 2134 : CALL get_atomic_kind(atomic_kind_set(ikind), natom=natom, atom_list=atom_list)
1275 : CALL get_qs_kind(qs_kind_set(ikind), basis_set=orb_basis_set, &
1276 : all_potential=all_potential, &
1277 : gth_potential=gth_potential, &
1278 : sgp_potential=sgp_potential, &
1279 2134 : cneo_potential=cneo_potential)
1280 : has_pot = ASSOCIATED(all_potential) .OR. ASSOCIATED(gth_potential) .OR. &
1281 2134 : ASSOCIATED(sgp_potential) .OR. ASSOCIATED(cneo_potential)
1282 :
1283 2134 : IF (dft_control%qs_control%dftb) THEN
1284 : CALL get_dftb_atom_param(qs_kind_set(ikind)%dftb_parameter, &
1285 588 : lmax=maxll, occupation=edftb)
1286 588 : maxll = MIN(maxll, maxl)
1287 1764 : econf(0:maxl) = edftb(0:maxl)
1288 1546 : ELSEIF (dft_control%qs_control%xtb) THEN
1289 510 : CALL get_qs_kind(qs_kind_set(ikind), xtb_parameter=xtb_kind)
1290 510 : CALL get_xtb_atom_param(xtb_kind, z=z, natorb=nsgf, nao=naox, lao=laox, occupation=occupation)
1291 1036 : ELSEIF (has_pot) THEN
1292 1036 : CALL get_atomic_kind(atomic_kind_set(ikind), z=z)
1293 1036 : CALL get_qs_kind(qs_kind_set(ikind), nsgf=nsgf, elec_conf=elec_conf, zeff=zeff)
1294 1036 : maxll = MIN(SIZE(elec_conf) - 1, maxl)
1295 3324 : econf(:) = 0.0_dp
1296 3262 : econf(0:maxll) = 0.5_dp*maxocc*REAL(elec_conf(0:maxll), dp)
1297 : ELSE
1298 : CYCLE
1299 : END IF
1300 :
1301 : ! MOPAC TYPE GUESS
1302 5172 : IF (dft_control%qs_control%dftb) THEN
1303 4038 : DO iatom = 1, natom
1304 3450 : atom_a = atom_list(iatom)
1305 3450 : isgfa = first_sgf(atom_a)
1306 8738 : DO la = 0, maxll
1307 3450 : SELECT CASE (la)
1308 : CASE (0)
1309 3450 : pdiag(isgfa) = econf(0)
1310 : CASE (1)
1311 1250 : pdiag(isgfa + 1) = econf(1)/3._dp
1312 1250 : pdiag(isgfa + 2) = econf(1)/3._dp
1313 1250 : pdiag(isgfa + 3) = econf(1)/3._dp
1314 : CASE (2)
1315 0 : pdiag(isgfa + 4) = econf(2)/5._dp
1316 0 : pdiag(isgfa + 5) = econf(2)/5._dp
1317 0 : pdiag(isgfa + 6) = econf(2)/5._dp
1318 0 : pdiag(isgfa + 7) = econf(2)/5._dp
1319 0 : pdiag(isgfa + 8) = econf(2)/5._dp
1320 : CASE (3)
1321 0 : pdiag(isgfa + 9) = econf(3)/7._dp
1322 0 : pdiag(isgfa + 10) = econf(3)/7._dp
1323 0 : pdiag(isgfa + 11) = econf(3)/7._dp
1324 0 : pdiag(isgfa + 12) = econf(3)/7._dp
1325 0 : pdiag(isgfa + 13) = econf(3)/7._dp
1326 0 : pdiag(isgfa + 14) = econf(3)/7._dp
1327 0 : pdiag(isgfa + 15) = econf(3)/7._dp
1328 : CASE DEFAULT
1329 4700 : CPABORT("")
1330 : END SELECT
1331 : END DO
1332 : END DO
1333 1546 : ELSEIF (dft_control%qs_control%xtb) THEN
1334 3042 : DO iatom = 1, natom
1335 2532 : atom_a = atom_list(iatom)
1336 2532 : isgfa = first_sgf(atom_a)
1337 3042 : IF (z == 1 .AND. nsgf == 2) THEN
1338 : ! Hydrogen 2s basis
1339 1482 : pdiag(isgfa) = 1.0_dp
1340 1482 : pdiag(isgfa + 1) = 0.0_dp
1341 : ELSE
1342 6312 : DO isgf = 1, nsgf
1343 5262 : na = naox(isgf)
1344 5262 : la = laox(isgf)
1345 5262 : occ = REAL(occupation(la + 1), dp)/REAL(2*la + 1, dp)
1346 6312 : pdiag(isgfa + isgf - 1) = occ
1347 : END DO
1348 : END IF
1349 : END DO
1350 1036 : ELSEIF (dft_control%qs_control%semi_empirical) THEN
1351 962 : yy = REAL(dft_control%charge, KIND=dp)/REAL(nao, KIND=dp)
1352 5482 : DO iatom = 1, natom
1353 4520 : atom_a = atom_list(iatom)
1354 4520 : isgfa = first_sgf(atom_a)
1355 962 : SELECT CASE (nsgf)
1356 : CASE (1) ! s-basis
1357 2188 : pdiag(isgfa) = (zeff - yy)*0.5_dp*maxocc
1358 : CASE (4) ! sp-basis
1359 2206 : IF (z == 1) THEN
1360 : ! special case: hydrogen with sp basis
1361 136 : pdiag(isgfa) = (zeff - yy)*0.5_dp*maxocc
1362 136 : pdiag(isgfa + 1) = 0._dp
1363 136 : pdiag(isgfa + 2) = 0._dp
1364 136 : pdiag(isgfa + 3) = 0._dp
1365 : ELSE
1366 2070 : pdiag(isgfa) = (zeff*0.25_dp - yy)*0.5_dp*maxocc
1367 2070 : pdiag(isgfa + 1) = (zeff*0.25_dp - yy)*0.5_dp*maxocc
1368 2070 : pdiag(isgfa + 2) = (zeff*0.25_dp - yy)*0.5_dp*maxocc
1369 2070 : pdiag(isgfa + 3) = (zeff*0.25_dp - yy)*0.5_dp*maxocc
1370 : END IF
1371 : CASE (9) ! spd-basis
1372 126 : IF (z < 21 .OR. z > 30 .AND. z < 39 .OR. z > 48 .AND. z < 57) THEN
1373 : ! Main Group Element: The "d" shell is formally empty.
1374 92 : pdiag(isgfa) = (zeff*0.25_dp - yy)*0.5_dp*maxocc
1375 92 : pdiag(isgfa + 1) = (zeff*0.25_dp - yy)*0.5_dp*maxocc
1376 92 : pdiag(isgfa + 2) = (zeff*0.25_dp - yy)*0.5_dp*maxocc
1377 92 : pdiag(isgfa + 3) = (zeff*0.25_dp - yy)*0.5_dp*maxocc
1378 92 : pdiag(isgfa + 4) = (-yy)*0.5_dp*maxocc
1379 92 : pdiag(isgfa + 5) = (-yy)*0.5_dp*maxocc
1380 92 : pdiag(isgfa + 6) = (-yy)*0.5_dp*maxocc
1381 92 : pdiag(isgfa + 7) = (-yy)*0.5_dp*maxocc
1382 92 : pdiag(isgfa + 8) = (-yy)*0.5_dp*maxocc
1383 34 : ELSE IF (z < 99) THEN
1384 34 : my_sum = zeff - 9.0_dp*yy
1385 : ! First, put 2 electrons in the 's' shell
1386 34 : pdiag(isgfa) = (MAX(0.0_dp, MIN(my_sum, 2.0_dp)))*0.5_dp*maxocc
1387 34 : my_sum = my_sum - 2.0_dp
1388 34 : IF (my_sum > 0.0_dp) THEN
1389 : ! Now put as many electrons as possible into the 'd' shell
1390 30 : pdiag(isgfa + 4) = (MAX(0.0_dp, MIN(my_sum*0.2_dp, 2.0_dp)))*0.5_dp*maxocc
1391 30 : pdiag(isgfa + 5) = (MAX(0.0_dp, MIN(my_sum*0.2_dp, 2.0_dp)))*0.5_dp*maxocc
1392 30 : pdiag(isgfa + 6) = (MAX(0.0_dp, MIN(my_sum*0.2_dp, 2.0_dp)))*0.5_dp*maxocc
1393 30 : pdiag(isgfa + 7) = (MAX(0.0_dp, MIN(my_sum*0.2_dp, 2.0_dp)))*0.5_dp*maxocc
1394 30 : pdiag(isgfa + 8) = (MAX(0.0_dp, MIN(my_sum*0.2_dp, 2.0_dp)))*0.5_dp*maxocc
1395 30 : my_sum = MAX(0.0_dp, my_sum - 10.0_dp)
1396 : ! Put the remaining electrons in the 'p' shell
1397 30 : pdiag(isgfa + 1) = (my_sum/3.0_dp)*0.5_dp*maxocc
1398 30 : pdiag(isgfa + 2) = (my_sum/3.0_dp)*0.5_dp*maxocc
1399 30 : pdiag(isgfa + 3) = (my_sum/3.0_dp)*0.5_dp*maxocc
1400 : END IF
1401 : END IF
1402 : CASE DEFAULT
1403 4520 : CPABORT("")
1404 : END SELECT
1405 : END DO
1406 : ELSE
1407 : CALL get_gto_basis_set(gto_basis_set=orb_basis_set, &
1408 : nset=nset, &
1409 : nshell=nshell, &
1410 : l=l, &
1411 : first_sgf=first_sgfa, &
1412 74 : last_sgf=last_sgfa)
1413 :
1414 202 : DO iset = 1, nset
1415 504 : DO ishell = 1, nshell(iset)
1416 302 : la = l(ishell, iset)
1417 302 : nelec = maxocc*REAL(2*la + 1, dp)
1418 430 : IF (econf(la) > 0.0_dp) THEN
1419 140 : IF (econf(la) >= nelec) THEN
1420 66 : paa = maxocc
1421 66 : econf(la) = econf(la) - nelec
1422 : ELSE
1423 74 : paa = maxocc*econf(la)/nelec
1424 74 : econf(la) = 0.0_dp
1425 : ncount = ncount + NINT(nelec/maxocc)
1426 : END IF
1427 412 : DO isgfa = first_sgfa(ishell, iset), last_sgfa(ishell, iset)
1428 2624 : DO iatom = 1, natom
1429 2212 : atom_a = atom_list(iatom)
1430 2212 : isgf = first_sgf(atom_a) + isgfa - 1
1431 2212 : pdiag(isgf) = paa
1432 2484 : IF (paa == maxocc) THEN
1433 550 : trps1 = trps1 + paa*sdiag(isgf)
1434 : ELSE
1435 1662 : trps2 = trps2 + paa*sdiag(isgf)
1436 : END IF
1437 : END DO
1438 : END DO
1439 : END IF
1440 : END DO ! ishell
1441 : END DO ! iset
1442 : END IF
1443 : END DO ! ikind
1444 :
1445 904 : IF (trps2 == 0.0_dp) THEN
1446 28420 : DO isgf = 1, nao
1447 28420 : IF (sdiag(isgf) > 0.0_dp) pdiag(isgf) = pdiag(isgf)/sdiag(isgf)
1448 : END DO
1449 1760 : DO ispin = 1, nspin
1450 1760 : IF (nelectron_spin(ispin) /= 0) THEN
1451 908 : matrix_p => pmat(ispin)%matrix
1452 908 : CALL dbcsr_set_diag(matrix_p, pdiag)
1453 : END IF
1454 : END DO
1455 : ELSE
1456 116 : DO ispin = 1, nspin
1457 116 : IF (nelectron_spin(ispin) /= 0) THEN
1458 60 : rscale = (REAL(nelectron_spin(ispin), dp) - trps1)/trps2
1459 5856 : DO isgf = 1, nao
1460 5856 : IF (pdiag(isgf) < maxocc) pdiag(isgf) = rscale*pdiag(isgf)
1461 : END DO
1462 60 : matrix_p => pmat(ispin)%matrix
1463 60 : CALL dbcsr_set_diag(matrix_p, pdiag)
1464 5856 : DO isgf = 1, nao
1465 5856 : IF (pdiag(isgf) < maxocc) pdiag(isgf) = pdiag(isgf)/rscale
1466 : END DO
1467 : END IF
1468 : END DO
1469 : END IF
1470 : END IF
1471 :
1472 918 : DEALLOCATE (econf)
1473 :
1474 918 : DEALLOCATE (first_sgf)
1475 :
1476 918 : DEALLOCATE (pdiag)
1477 :
1478 918 : DEALLOCATE (sdiag)
1479 :
1480 918 : CALL timestop(handle)
1481 :
1482 2754 : END SUBROUTINE calculate_mopac_dm
1483 :
1484 0 : END MODULE qs_initial_guess
|