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 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 9881 : 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 9881 : INTEGER, ALLOCATABLE, DIMENSION(:) :: first_sgf, kind_of, last_sgf
146 : INTEGER, DIMENSION(2) :: nelectron_spin
147 9881 : INTEGER, DIMENSION(:), POINTER :: atom_list, elec_conf, nelec_kind, &
148 9881 : 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 9881 : REAL(dp), ALLOCATABLE, DIMENSION(:, :) :: buff, buff2
153 9881 : 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 9881 : TYPE(atom_matrix_type), DIMENSION(:), POINTER :: pmat
158 9881 : 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 9881 : 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 9881 : TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: h_core_sparse, matrix_ks, p_rmpv, &
167 9881 : s_sparse
168 9881 : TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_h_kp, matrix_ks_kp, matrix_s_kp, &
169 9881 : 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 9881 : TYPE(hfx_type), DIMENSION(:, :), POINTER :: x_data
174 : TYPE(kpoint_type), POINTER :: kpoints
175 9881 : TYPE(mo_set_type), DIMENSION(:), POINTER :: mo_array, mos_last_converged
176 : TYPE(mp_para_env_type), POINTER :: para_env
177 9881 : TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
178 9881 : 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 19762 : logger => cp_get_default_logger()
185 9881 : NULLIFY (atomic_kind, qs_kind, mo_coeff, orb_basis_set, atomic_kind_set, &
186 9881 : qs_kind_set, particle_set, ortho, work2, work1, mo_array, s_sparse, &
187 9881 : scf_control, dft_control, p_rmpv, para_env, h_core_sparse, matrix_ks, rho, &
188 9881 : mos_last_converged)
189 9881 : NULLIFY (dft_section, input, subsys_section)
190 9881 : NULLIFY (matrix_s_kp, matrix_h_kp, matrix_ks_kp, rho_ao_kp)
191 9881 : NULLIFY (moa, mob)
192 9881 : NULLIFY (atom_list, elec_conf, kpoints)
193 : edftb = 0.0_dp
194 9881 : tot_corr_zeff = 0.0_dp
195 :
196 9881 : 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 9881 : x_data=x_data)
216 :
217 9881 : CALL qs_rho_get(rho, rho_ao_kp=rho_ao_kp)
218 :
219 9881 : 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 21587 : DO ispin = 1, dft_control%nspins
225 175033 : DO ic = 1, SIZE(rho_ao_kp, 2)
226 165152 : CALL dbcsr_set(rho_ao_kp(ispin, ic)%matrix, 0.0_dp)
227 : END DO
228 : END DO
229 9881 : s_sparse => matrix_s_kp(:, 1)
230 9881 : h_core_sparse => matrix_h_kp(:, 1)
231 9881 : matrix_ks => matrix_ks_kp(:, 1)
232 9881 : p_rmpv => rho_ao_kp(:, 1)
233 :
234 9881 : work1 => scf_env%scf_work1
235 9881 : work2 => scf_env%scf_work2
236 9881 : ortho => scf_env%ortho
237 :
238 9881 : dft_section => section_vals_get_subs_vals(input, "DFT")
239 :
240 9881 : nspin = dft_control%nspins
241 9881 : ofgpw = dft_control%qs_control%ofgpw
242 9881 : density_guess = scf_control%density_guess
243 9881 : do_std_diag = .FALSE.
244 :
245 9881 : do_hfx_ri_mo = .FALSE.
246 9881 : IF (ASSOCIATED(x_data)) THEN
247 1350 : 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 9881 : 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 9881 : .OR. do_hfx_ri_mo
258 :
259 9881 : safe_density_guess = atomic_guess
260 9881 : IF (dft_control%qs_control%semi_empirical .OR. dft_control%qs_control%dftb) THEN
261 1308 : IF (density_guess == atomic_guess) density_guess = mopac_guess
262 : safe_density_guess = mopac_guess
263 : END IF
264 9881 : IF (dft_control%qs_control%xtb) THEN
265 2672 : IF (do_kpoints) THEN
266 1652 : IF (density_guess == atomic_guess) density_guess = mopac_guess
267 : safe_density_guess = mopac_guess
268 : ELSE
269 1020 : IF (density_guess == atomic_guess) density_guess = core_guess
270 : safe_density_guess = core_guess
271 : END IF
272 : END IF
273 :
274 9881 : 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 9881 : id_nr = 0
292 9881 : 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 618 : IF (do_kpoints) THEN
297 22 : IF (para_env%is_source()) THEN
298 11 : CALL wfn_restart_file_name(file_name, exist, dft_section, logger, kp=.TRUE.)
299 : END IF
300 : ELSE
301 596 : IF (para_env%is_source()) THEN
302 312 : CALL wfn_restart_file_name(file_name, exist, dft_section, logger)
303 : END IF
304 : END IF
305 618 : CALL para_env%bcast(exist)
306 618 : CALL para_env%bcast(file_name)
307 618 : 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 126 : " Calculation continues using ATOMIC GUESS. ")
313 126 : density_guess = safe_density_guess
314 : END IF
315 9263 : 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()) THEN
334 2 : INQUIRE (FILE=filename, exist=exist)
335 : END IF
336 4 : CALL para_env%bcast(exist)
337 6 : IF ((.NOT. exist) .AND. (i < not_read)) THEN
338 : not_read = i
339 : END IF
340 : END DO
341 2 : IF (not_read == 1) THEN
342 0 : density_guess = restart_guess
343 0 : filename = TRIM(file_name)
344 0 : IF (para_env%is_source()) INQUIRE (FILE=filename, exist=exist)
345 0 : CALL para_env%bcast(exist)
346 0 : IF (.NOT. exist) THEN
347 : CALL cp_warn(__LOCATION__, &
348 : "User requested to restart the wavefunction from a series of restart files named: "// &
349 : TRIM(file_name)//" with extensions (.bak-n). These files do not exist."// &
350 : " Even trying to switch to a plain restart wave-function failes because the"// &
351 : " file named: "//TRIM(file_name)//" does not exist. Please check the existence of"// &
352 : " the file or change properly the value of the keyword WFN_RESTART_FILE_NAME."// &
353 0 : " Calculation continues using ATOMIC GUESS. ")
354 0 : density_guess = safe_density_guess
355 : END IF
356 : END IF
357 2 : last_read = not_read - 1
358 : END IF
359 :
360 9881 : did_guess = .FALSE.
361 :
362 9881 : IF (dft_control%correct_el_density_dip) THEN
363 4 : tot_corr_zeff = qs_env%total_zeff_corr
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 9881 : ounit = -1
375 9881 : print_log = .FALSE.
376 9881 : print_history_log = .FALSE.
377 9881 : IF (para_env%is_source()) THEN
378 : CALL section_vals_val_get(dft_section, &
379 : "SCF%PRINT%RESTART%LOG_PRINT_KEY", &
380 4979 : l_val=print_log)
381 : CALL section_vals_val_get(dft_section, &
382 : "SCF%PRINT%RESTART_HISTORY%LOG_PRINT_KEY", &
383 4979 : l_val=print_history_log)
384 4979 : 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 9881 : IF (density_guess == restart_guess) THEN
390 492 : IF (ounit > 0) THEN
391 : WRITE (UNIT=ounit, FMT="(/,T2,A)") &
392 4 : "WFN_RESTART| Reading restart file"
393 : END IF
394 492 : IF (do_kpoints) THEN
395 12 : natoms = SIZE(particle_set)
396 : CALL read_kpoints_restart(rho_ao_kp, kpoints, work1, &
397 12 : natoms, para_env, id_nr, dft_section, natom_mismatch)
398 12 : 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 480 : out_unit=ounit)
405 :
406 480 : IF (natom_mismatch) THEN
407 : density_guess = safe_density_guess
408 : ELSE
409 1272 : DO ispin = 1, nspin
410 812 : 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 812 : mo_coeff=mo_coeff, nmo=nmo, homo=homo)
418 :
419 812 : IF (has_unit_metric) THEN
420 4 : CALL make_basis_simple(mo_coeff, nmo)
421 808 : ELSE IF (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 704 : CALL make_basis_sm(mo_coeff, homo, s_sparse(1)%matrix)
427 : END IF
428 : ! only alpha spin is kept for restricted
429 2084 : IF (dft_control%restricted) EXIT
430 : END DO
431 480 : IF (dft_control%restricted) CALL mo_set_restrict(mo_array)
432 :
433 480 : IF (.NOT. scf_control%diagonalization%mom) THEN
434 464 : 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 464 : CALL set_mo_occupation(mo_array, smear=qs_env%scf_control%smear)
443 : END IF
444 : END IF
445 :
446 1312 : DO ispin = 1, nspin
447 :
448 832 : 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 570 : mo_array(ispin)%mo_coeff_b) !fm->dbcsr
451 : END IF !fm->dbcsr
452 :
453 : CALL calculate_density_matrix(mo_array(ispin), &
454 1312 : p_rmpv(ispin)%matrix)
455 : END DO
456 : END IF ! natom_mismatch
457 :
458 : END IF
459 :
460 : ! Maximum Overlap Method
461 492 : 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 9881 : 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 9881 : 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 9881 : IF (density_guess == core_guess) THEN
556 :
557 194 : IF (do_kpoints) THEN
558 0 : CPABORT("calculate_first_density_matrix: core_guess not implemented for k-points")
559 : END IF
560 :
561 194 : CALL get_qs_kind_set(qs_kind_set, cneo_potential_present=cneo_potential_present)
562 194 : IF (cneo_potential_present) THEN
563 0 : CPABORT("calculate_first_density_matrix: core_guess not implemented for CNEO")
564 : END IF
565 :
566 194 : owns_ortho = .FALSE.
567 194 : IF (.NOT. ASSOCIATED(work1)) THEN
568 54 : need_wm = .TRUE.
569 54 : CPASSERT(.NOT. ASSOCIATED(work2))
570 54 : CPASSERT(.NOT. ASSOCIATED(ortho))
571 : ELSE
572 140 : need_wm = .FALSE.
573 140 : CPASSERT(ASSOCIATED(work2))
574 140 : IF (.NOT. ASSOCIATED(ortho)) THEN
575 6 : ALLOCATE (ortho)
576 : owns_ortho = .TRUE.
577 : END IF
578 : END IF
579 :
580 : IF (need_wm) THEN
581 54 : CALL get_mo_set(mo_set=mo_array(1), mo_coeff=moa)
582 54 : CALL cp_fm_get_info(moa, matrix_struct=ao_mo_struct)
583 54 : 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 54 : template_fmstruct=ao_mo_struct)
590 108 : ALLOCATE (work1(1))
591 54 : ALLOCATE (work2, ortho)
592 54 : CALL cp_fm_create(work1(1), ao_ao_struct)
593 54 : CALL cp_fm_create(work2, ao_ao_struct)
594 54 : CALL cp_fm_create(ortho, ao_ao_struct)
595 54 : CALL copy_dbcsr_to_fm(matrix_s_kp(1, 1)%matrix, ortho)
596 54 : CALL cp_fm_cholesky_decompose(ortho)
597 162 : CALL cp_fm_struct_release(ao_ao_struct)
598 : END IF
599 :
600 194 : ispin = 1
601 : ! Load core Hamiltonian into work matrix
602 194 : 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 194 : 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 188 : use_jacobi=.FALSE.)
622 : END IF
623 :
624 : ! Open shell case: copy alpha MOs to beta MOs
625 194 : IF (nspin == 2) THEN
626 32 : CALL get_mo_set(mo_set=mo_array(1), mo_coeff=moa)
627 32 : CALL get_mo_set(mo_set=mo_array(2), mo_coeff=mob, nmo=nmo)
628 32 : 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 420 : DO ispin = 1, nspin
634 226 : CALL set_mo_occupation(mo_set=mo_array(ispin), smear=scf_control%smear)
635 420 : CALL calculate_density_matrix(mo_array(ispin), p_rmpv(ispin)%matrix)
636 : END DO
637 :
638 : ! release intermediate matrices
639 194 : IF (need_wm) THEN
640 54 : CALL cp_fm_release(ortho)
641 54 : CALL cp_fm_release(work2)
642 54 : CALL cp_fm_release(work1(1))
643 54 : DEALLOCATE (ortho, work2)
644 54 : DEALLOCATE (work1)
645 54 : NULLIFY (work1, work2, ortho)
646 140 : ELSE IF (owns_ortho) THEN
647 6 : DEALLOCATE (ortho)
648 : END IF
649 :
650 : did_guess = .TRUE.
651 : END IF
652 :
653 9881 : IF (density_guess == atomic_guess) THEN
654 :
655 5337 : subsys_section => section_vals_get_subs_vals(input, "SUBSYS")
656 5337 : ounit = cp_print_key_unit_nr(logger, subsys_section, "PRINT%KINDS", extension=".Log")
657 5337 : IF (ounit > 0) THEN
658 : WRITE (UNIT=ounit, FMT="(/,(T2,A))") &
659 1164 : "Atomic guess: The first density matrix is obtained in terms of atomic orbitals", &
660 2328 : " 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 5337 : nspin, nelectron_spin, ounit, para_env)
665 :
666 11739 : 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 11739 : IF (ofgpw .AND. (scf_control%use_ot .OR. scf_env%method == ot_diag_method_nr)) THEN
671 : ! get orbitals later
672 : ELSE
673 6402 : IF (need_mos) THEN
674 :
675 2420 : 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 2398 : nmo=nmo, nao=nao, homo=homo)
681 :
682 2398 : CALL cp_fm_set_all(mo_coeff, 0.0_dp)
683 2398 : CALL cp_fm_init_random(mo_coeff, nmo)
684 :
685 2398 : CALL cp_fm_create(sv, mo_coeff%matrix_struct, "SV")
686 : ! multiply times PS
687 2398 : 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 2398 : CALL cp_dbcsr_sm_fm_multiply(s_sparse(1)%matrix, mo_coeff, sv, nmo)
692 : END IF
693 2398 : CALL cp_dbcsr_sm_fm_multiply(p_rmpv(ispin)%matrix, sv, mo_coeff, homo)
694 :
695 2398 : CALL cp_fm_release(sv)
696 : ! and ortho the result
697 2398 : IF (has_unit_metric) THEN
698 0 : CALL make_basis_simple(mo_coeff, nmo)
699 : ELSE
700 2398 : 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 2420 : smear=qs_env%scf_control%smear)
706 :
707 : CALL copy_fm_to_dbcsr(mo_array(ispin)%mo_coeff, &
708 2420 : mo_array(ispin)%mo_coeff_b) !fm->dbcsr
709 :
710 : CALL calculate_density_matrix(mo_array(ispin), &
711 2420 : 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 6402 : IF (scf_env%method == general_diag_method_nr) THEN
716 4274 : 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 5337 : 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 5337 : 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 5337 : 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 5337 : "PRINT%KINDS")
777 :
778 5337 : did_guess = .TRUE.
779 : END IF
780 :
781 9881 : IF (density_guess == sparse_guess) THEN
782 :
783 0 : IF (ofgpw) THEN
784 0 : CPABORT("calculate_first_density_matrix: sparse_guess not implemented for OFGPW")
785 : END IF
786 0 : IF (.NOT. scf_control%use_ot) THEN
787 0 : CPABORT("calculate_first_density_matrix: sparse_guess implemented for OT only")
788 : END IF
789 0 : IF (do_kpoints) THEN
790 0 : CPABORT("calculate_first_density_matrix: sparse_guess not implemented for k-points")
791 : END IF
792 :
793 0 : eps = 1.0E-5_dp
794 :
795 0 : ounit = cp_logger_get_default_io_unit(logger)
796 0 : natoms = SIZE(particle_set)
797 0 : ALLOCATE (kind_of(natoms))
798 0 : ALLOCATE (first_sgf(natoms), last_sgf(natoms))
799 :
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 0 : CALL dbcsr_filter(s_sparse(1)%matrix, eps)
804 0 : checksum = dbcsr_checksum(s_sparse(1)%matrix)
805 0 : i = dbcsr_get_num_blocks(s_sparse(1)%matrix); CALL para_env%sum(i)
806 0 : IF (ounit > 0) WRITE (ounit, *) 'S nblks', i, ' checksum', checksum
807 :
808 : CALL get_particle_set(particle_set, qs_kind_set, first_sgf=first_sgf, &
809 0 : last_sgf=last_sgf)
810 0 : CALL get_atomic_kind_set(atomic_kind_set=atomic_kind_set, kind_of=kind_of)
811 :
812 0 : ALLOCATE (pmat(SIZE(atomic_kind_set)))
813 :
814 0 : rscale = 1._dp
815 0 : IF (nspin == 2) rscale = 0.5_dp
816 0 : DO ikind = 1, SIZE(atomic_kind_set)
817 0 : atomic_kind => atomic_kind_set(ikind)
818 0 : qs_kind => qs_kind_set(ikind)
819 0 : NULLIFY (pmat(ikind)%mat)
820 0 : CALL calculate_atomic_orbitals(atomic_kind, qs_kind, pmat=pmat(ikind)%mat)
821 0 : NULLIFY (atomic_kind)
822 : END DO
823 :
824 0 : DO ispin = 1, nspin
825 : CALL get_mo_set(mo_set=mo_array(ispin), &
826 : maxocc=maxocc, &
827 0 : nelectron=nelectron)
828 : !
829 0 : CALL dbcsr_iterator_start(iter, p_rmpv(ispin)%matrix)
830 0 : DO WHILE (dbcsr_iterator_blocks_left(iter))
831 0 : CALL dbcsr_iterator_next_block(iter, irow, icol, pdata)
832 0 : ikind = kind_of(irow)
833 0 : IF (icol == irow) THEN
834 0 : IF (ispin == 1) THEN
835 : pdata(:, :) = pmat(ikind)%mat(:, :, 1)*rscale + &
836 0 : pmat(ikind)%mat(:, :, 2)*rscale
837 : ELSE
838 : pdata(:, :) = pmat(ikind)%mat(:, :, 1)*rscale - &
839 0 : pmat(ikind)%mat(:, :, 2)*rscale
840 : END IF
841 : END IF
842 : END DO
843 0 : CALL dbcsr_iterator_stop(iter)
844 :
845 : !CALL dbcsr_verify_matrix(p_rmpv(ispin)%matrix)
846 0 : checksum = dbcsr_checksum(p_rmpv(ispin)%matrix)
847 0 : occ = dbcsr_get_occupation(p_rmpv(ispin)%matrix)
848 0 : IF (ounit > 0) WRITE (ounit, *) 'P_init occ', occ, ' checksum', checksum
849 : ! so far p needs to have the same sparsity as S
850 : !CALL dbcsr_filter(p_rmpv(ispin)%matrix, eps)
851 : !CALL dbcsr_verify_matrix(p_rmpv(ispin)%matrix)
852 0 : checksum = dbcsr_checksum(p_rmpv(ispin)%matrix)
853 0 : occ = dbcsr_get_occupation(p_rmpv(ispin)%matrix)
854 0 : IF (ounit > 0) WRITE (ounit, *) 'P_init occ', occ, ' checksum', checksum
855 :
856 0 : CALL dbcsr_dot(p_rmpv(ispin)%matrix, s_sparse(1)%matrix, trps1)
857 0 : rscale = REAL(nelectron, dp)/trps1
858 0 : CALL dbcsr_scale(p_rmpv(ispin)%matrix, rscale)
859 :
860 : !CALL dbcsr_verify_matrix(p_rmpv(ispin)%matrix)
861 0 : checksum = dbcsr_checksum(p_rmpv(ispin)%matrix)
862 0 : occ = dbcsr_get_occupation(p_rmpv(ispin)%matrix)
863 0 : IF (ounit > 0) WRITE (ounit, *) 'P occ', occ, ' checksum', checksum
864 : !
865 : ! The orbital transformation method (OT) requires not only an
866 : ! initial density matrix, but also an initial wavefunction (MO set)
867 0 : IF (dft_control%restricted .AND. (ispin == 2)) THEN
868 0 : CALL mo_set_restrict(mo_array)
869 : ELSE
870 : CALL get_mo_set(mo_set=mo_array(ispin), &
871 : mo_coeff=mo_coeff, &
872 0 : nmo=nmo, nao=nao, homo=homo)
873 0 : CALL cp_fm_set_all(mo_coeff, 0.0_dp)
874 :
875 0 : n = MAXVAL(last_sgf - first_sgf) + 1
876 0 : size_atomic_kind_set = SIZE(atomic_kind_set)
877 :
878 0 : ALLOCATE (buff(n, n), sort_kind(size_atomic_kind_set), &
879 0 : nelec_kind(size_atomic_kind_set))
880 : !
881 : ! sort kind vs nbr electron
882 0 : DO ikind = 1, size_atomic_kind_set
883 0 : atomic_kind => atomic_kind_set(ikind)
884 0 : qs_kind => qs_kind_set(ikind)
885 : CALL get_atomic_kind(atomic_kind=atomic_kind, &
886 : natom=natom, &
887 : atom_list=atom_list, &
888 0 : z=z)
889 : CALL get_qs_kind(qs_kind, nsgf=nsgf, elec_conf=elec_conf, &
890 0 : basis_set=orb_basis_set, zeff=zeff)
891 0 : nelec_kind(ikind) = SUM(elec_conf)
892 : END DO
893 0 : CALL sort(nelec_kind, size_atomic_kind_set, sort_kind)
894 : !
895 : ! a -very- naive sparse guess
896 0 : nmo_tmp = nmo
897 0 : natoms_tmp = natoms
898 0 : istart_col = 1
899 0 : iseed(1) = 4; iseed(2) = 3; iseed(3) = 2; iseed(4) = 1 ! set the seed for dlarnv
900 0 : DO i = 1, size_atomic_kind_set
901 0 : ikind = sort_kind(i)
902 0 : atomic_kind => atomic_kind_set(ikind)
903 : CALL get_atomic_kind(atomic_kind=atomic_kind, &
904 0 : natom=natom, atom_list=atom_list)
905 0 : DO iatom = 1, natom
906 : !
907 0 : atom_a = atom_list(iatom)
908 0 : istart_row = first_sgf(atom_a)
909 0 : n_rows = last_sgf(atom_a) - first_sgf(atom_a) + 1
910 : !
911 : ! compute the "potential" nbr of states for this atom
912 0 : n_cols = MAX(INT(REAL(nmo_tmp, dp)/REAL(natoms_tmp, dp)), 1)
913 0 : IF (n_cols > n_rows) n_cols = n_rows
914 : !
915 0 : nmo_tmp = nmo_tmp - n_cols
916 0 : natoms_tmp = natoms_tmp - 1
917 0 : CPASSERT(nmo_tmp >= 0)
918 0 : CPASSERT(natoms_tmp >= 0)
919 0 : DO j = 1, n_cols
920 0 : CALL dlarnv(1, iseed, n_rows, buff(1, j))
921 : END DO
922 : CALL cp_fm_set_submatrix(mo_coeff, buff, istart_row, istart_col, &
923 0 : n_rows, n_cols)
924 0 : istart_col = istart_col + n_cols
925 : END DO
926 : END DO
927 :
928 0 : CPASSERT(istart_col > nmo)
929 :
930 0 : DEALLOCATE (buff, nelec_kind, sort_kind)
931 :
932 : IF (.FALSE.) THEN
933 : ALLOCATE (buff(nao, 1), buff2(nao, 1))
934 : DO i = 1, nmo
935 : CALL cp_fm_get_submatrix(mo_coeff, buff, 1, i, nao, 1)
936 : IF (SUM(buff**2) < 1E-10_dp) THEN
937 : IF (ounit > 0) THEN
938 : WRITE (ounit, *) 'wrong', i, SUM(buff**2)
939 : END IF
940 : END IF
941 : length = NORM2(buff(:, 1))
942 : buff(:, :) = buff(:, :)/length
943 : DO j = i + 1, nmo
944 : CALL cp_fm_get_submatrix(mo_coeff, buff2, 1, j, nao, 1)
945 : length = NORM2(buff2(:, 1))
946 : buff2(:, :) = buff2(:, :)/length
947 : IF (ABS(DOT_PRODUCT(buff(:, 1), buff2(:, 1)) - 1.0_dp) < 1E-10_dp) THEN
948 : IF (ounit > 0) THEN
949 : WRITE (ounit, *) 'wrong2', i, j, DOT_PRODUCT(buff(:, 1), buff2(:, 1))
950 : DO ikind = 1, nao
951 : IF (ABS(mo_coeff%local_data(ikind, i)) > 1e-10_dp) THEN
952 : WRITE (ounit, *) 'c1', ikind, mo_coeff%local_data(ikind, i)
953 : END IF
954 : IF (ABS(mo_coeff%local_data(ikind, j)) > 1e-10_dp) THEN
955 : WRITE (ounit, *) 'c2', ikind, mo_coeff%local_data(ikind, j)
956 : END IF
957 : END DO
958 : END IF
959 : CPABORT("Something went wrong with sparse_guess!")
960 : END IF
961 : END DO
962 : END DO
963 : DEALLOCATE (buff, buff2)
964 :
965 : END IF
966 : !
967 0 : CALL cp_fm_to_dbcsr_row_template(mo_dbcsr, mo_coeff, s_sparse(1)%matrix)
968 : !CALL dbcsr_verify_matrix(mo_dbcsr)
969 0 : checksum = dbcsr_checksum(mo_dbcsr)
970 :
971 0 : occ = dbcsr_get_occupation(mo_dbcsr)
972 0 : IF (ounit > 0) WRITE (ounit, *) 'C occ', occ, ' checksum', checksum
973 0 : CALL dbcsr_filter(mo_dbcsr, eps)
974 : !CALL dbcsr_verify_matrix(mo_dbcsr)
975 0 : occ = dbcsr_get_occupation(mo_dbcsr)
976 0 : checksum = dbcsr_checksum(mo_dbcsr)
977 0 : IF (ounit > 0) WRITE (ounit, *) 'C occ', occ, ' checksum', checksum
978 : !
979 : ! multiply times PS
980 0 : IF (has_unit_metric) THEN
981 0 : CPABORT("has_unit_metric will be removed soon")
982 : END IF
983 : !
984 : ! S*C
985 0 : CALL dbcsr_copy(mo_tmp_dbcsr, mo_dbcsr, name="mo_tmp")
986 : CALL dbcsr_multiply("N", "N", 1.0_dp, s_sparse(1)%matrix, mo_dbcsr, &
987 : 0.0_dp, mo_tmp_dbcsr, &
988 0 : retain_sparsity=.TRUE.)
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 0 : CALL dbcsr_filter(mo_tmp_dbcsr, eps)
994 : !CALL dbcsr_verify_matrix(mo_tmp_dbcsr)
995 0 : checksum = dbcsr_checksum(mo_tmp_dbcsr)
996 0 : occ = dbcsr_get_occupation(mo_tmp_dbcsr)
997 0 : IF (ounit > 0) WRITE (ounit, *) 'S*C occ', occ, ' checksum', checksum
998 : !
999 : ! P*SC
1000 : ! the destroy is needed for the moment to avoid memory leaks !
1001 : ! This one is not needed because _destroy takes care of zeroing.
1002 : CALL dbcsr_multiply("N", "N", 1.0_dp, p_rmpv(ispin)%matrix, &
1003 0 : mo_tmp_dbcsr, 0.0_dp, mo_dbcsr)
1004 : IF (.FALSE.) 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 0 : CALL dbcsr_filter(mo_dbcsr, eps)
1009 : !CALL dbcsr_verify_matrix(mo_dbcsr)
1010 0 : checksum = dbcsr_checksum(mo_dbcsr)
1011 0 : occ = dbcsr_get_occupation(mo_dbcsr)
1012 0 : IF (ounit > 0) WRITE (ounit, *) 'P*SC occ', occ, ' checksum', checksum
1013 : !
1014 0 : CALL copy_dbcsr_to_fm(mo_dbcsr, mo_coeff)
1015 :
1016 0 : CALL dbcsr_release(mo_dbcsr)
1017 0 : CALL dbcsr_release(mo_tmp_dbcsr)
1018 :
1019 : ! and ortho the result
1020 0 : CALL make_basis_sm(mo_coeff, nmo, s_sparse(1)%matrix)
1021 : END IF
1022 :
1023 : CALL set_mo_occupation(mo_set=mo_array(ispin), &
1024 0 : smear=qs_env%scf_control%smear)
1025 :
1026 : CALL copy_fm_to_dbcsr(mo_array(ispin)%mo_coeff, &
1027 0 : mo_array(ispin)%mo_coeff_b) !fm->dbcsr
1028 :
1029 : CALL calculate_density_matrix(mo_array(ispin), &
1030 0 : p_rmpv(ispin)%matrix)
1031 0 : DO ikind = 1, SIZE(atomic_kind_set)
1032 0 : IF (ASSOCIATED(pmat(ikind)%mat)) THEN
1033 0 : DEALLOCATE (pmat(ikind)%mat)
1034 : END IF
1035 : END DO
1036 : END DO
1037 :
1038 0 : DEALLOCATE (pmat)
1039 :
1040 0 : DEALLOCATE (kind_of)
1041 :
1042 0 : DEALLOCATE (first_sgf, last_sgf)
1043 :
1044 0 : did_guess = .TRUE.
1045 : END IF
1046 9881 : IF (density_guess == mopac_guess) THEN
1047 :
1048 : CALL calculate_mopac_dm(p_rmpv, s_sparse(1)%matrix, has_unit_metric, dft_control, &
1049 : particle_set, atomic_kind_set, qs_kind_set, &
1050 2904 : nspin, nelectron_spin, para_env)
1051 :
1052 6040 : DO ispin = 1, nspin
1053 : ! The orbital transformation method (OT) requires not only an
1054 : ! initial density matrix, but also an initial wavefunction (MO set)
1055 6040 : IF (need_mos) THEN
1056 228 : IF (dft_control%restricted .AND. (ispin == 2)) THEN
1057 2 : CALL mo_set_restrict(mo_array)
1058 : ELSE
1059 : CALL get_mo_set(mo_set=mo_array(ispin), &
1060 : mo_coeff=mo_coeff, &
1061 226 : nmo=nmo, homo=homo)
1062 226 : CALL cp_fm_init_random(mo_coeff, nmo)
1063 226 : CALL cp_fm_create(sv, mo_coeff%matrix_struct, "SV")
1064 : ! multiply times PS
1065 226 : IF (has_unit_metric) THEN
1066 180 : CALL cp_fm_to_fm(mo_coeff, sv)
1067 : ELSE
1068 46 : CALL cp_dbcsr_sm_fm_multiply(s_sparse(1)%matrix, mo_coeff, sv, nmo)
1069 : END IF
1070 : ! here we could easily multiply with the diag that we actually have replicated already
1071 226 : CALL cp_dbcsr_sm_fm_multiply(p_rmpv(ispin)%matrix, sv, mo_coeff, homo)
1072 226 : CALL cp_fm_release(sv)
1073 : ! and ortho the result
1074 226 : IF (has_unit_metric) THEN
1075 180 : CALL make_basis_simple(mo_coeff, nmo)
1076 : ELSE
1077 46 : CALL make_basis_sm(mo_coeff, nmo, s_sparse(1)%matrix)
1078 : END IF
1079 : END IF
1080 :
1081 : CALL set_mo_occupation(mo_set=mo_array(ispin), &
1082 228 : smear=qs_env%scf_control%smear)
1083 : CALL copy_fm_to_dbcsr(mo_array(ispin)%mo_coeff, &
1084 228 : mo_array(ispin)%mo_coeff_b)
1085 :
1086 : CALL calculate_density_matrix(mo_array(ispin), &
1087 228 : p_rmpv(ispin)%matrix)
1088 : END IF
1089 : END DO
1090 :
1091 : did_guess = .TRUE.
1092 : END IF
1093 : !
1094 : ! EHT guess (gfn0-xTB)
1095 9881 : IF (density_guess == eht_guess) THEN
1096 4 : CALL calculate_eht_guess(qs_env, mo_array)
1097 8 : DO ispin = 1, nspin
1098 8 : CALL calculate_density_matrix(mo_array(ispin), p_rmpv(ispin)%matrix)
1099 : END DO
1100 : did_guess = .TRUE.
1101 : END IF
1102 : ! switch_surf_dip [SGh]
1103 9881 : IF (dft_control%switch_surf_dip) THEN
1104 4 : DO ispin = 1, nspin
1105 : CALL reassign_allocated_mos(mos_last_converged(ispin), &
1106 4 : mo_array(ispin))
1107 : END DO
1108 : END IF
1109 :
1110 9881 : IF (density_guess == no_guess) THEN
1111 : did_guess = .TRUE.
1112 : END IF
1113 :
1114 8955 : IF (.NOT. did_guess) THEN
1115 0 : CPABORT("An invalid keyword for the initial density guess was specified")
1116 : END IF
1117 :
1118 9881 : CALL timestop(handle)
1119 :
1120 19762 : END SUBROUTINE calculate_first_density_matrix
1121 :
1122 : ! **************************************************************************************************
1123 : !> \brief returns a block diagonal fock matrix.
1124 : !> \param matrix_f ...
1125 : !> \param atomic_kind_set ...
1126 : !> \param qs_kind_set ...
1127 : !> \param ounit ...
1128 : ! **************************************************************************************************
1129 98 : SUBROUTINE calculate_atomic_fock_matrix(matrix_f, atomic_kind_set, qs_kind_set, ounit)
1130 : TYPE(dbcsr_type), INTENT(INOUT) :: matrix_f
1131 : TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
1132 : TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
1133 : INTEGER, INTENT(IN) :: ounit
1134 :
1135 : CHARACTER(LEN=*), PARAMETER :: routineN = 'calculate_atomic_fock_matrix'
1136 :
1137 : INTEGER :: handle, icol, ikind, irow
1138 98 : INTEGER, ALLOCATABLE, DIMENSION(:) :: kind_of
1139 98 : REAL(dp), DIMENSION(:, :), POINTER :: block
1140 98 : TYPE(atom_matrix_type), ALLOCATABLE, DIMENSION(:) :: fmat
1141 : TYPE(atomic_kind_type), POINTER :: atomic_kind
1142 : TYPE(dbcsr_iterator_type) :: iter
1143 : TYPE(qs_kind_type), POINTER :: qs_kind
1144 :
1145 98 : CALL timeset(routineN, handle)
1146 :
1147 98 : CALL get_atomic_kind_set(atomic_kind_set=atomic_kind_set, kind_of=kind_of)
1148 432 : ALLOCATE (fmat(SIZE(atomic_kind_set)))
1149 :
1150 : ! precompute the atomic blocks for each atomic-kind
1151 236 : DO ikind = 1, SIZE(atomic_kind_set)
1152 138 : atomic_kind => atomic_kind_set(ikind)
1153 138 : qs_kind => qs_kind_set(ikind)
1154 138 : NULLIFY (fmat(ikind)%mat)
1155 138 : IF (ounit > 0) WRITE (UNIT=ounit, FMT="(/,T2,A)") &
1156 69 : "Calculating atomic Fock matrix for atomic kind: "//TRIM(atomic_kind%name)
1157 :
1158 : !Currently only ispin=1 is supported
1159 : CALL calculate_atomic_orbitals(atomic_kind, qs_kind, iunit=ounit, &
1160 236 : fmat=fmat(ikind)%mat)
1161 : END DO
1162 :
1163 : ! zero result matrix
1164 98 : CALL dbcsr_set(matrix_f, 0.0_dp)
1165 :
1166 : ! copy precomputed blocks onto diagonal of result matrix
1167 98 : CALL dbcsr_iterator_start(iter, matrix_f)
1168 217 : DO WHILE (dbcsr_iterator_blocks_left(iter))
1169 119 : CALL dbcsr_iterator_next_block(iter, irow, icol, block)
1170 119 : ikind = kind_of(irow)
1171 6937 : IF (icol == irow) block(:, :) = fmat(ikind)%mat(:, :, 1)
1172 : END DO
1173 98 : CALL dbcsr_iterator_stop(iter)
1174 :
1175 : ! cleanup
1176 236 : DO ikind = 1, SIZE(atomic_kind_set)
1177 236 : DEALLOCATE (fmat(ikind)%mat)
1178 : END DO
1179 98 : DEALLOCATE (fmat)
1180 :
1181 98 : CALL timestop(handle)
1182 :
1183 294 : END SUBROUTINE calculate_atomic_fock_matrix
1184 :
1185 : ! **************************************************************************************************
1186 : !> \brief returns a block diagonal density matrix. Blocks correspond to the mopac initial guess.
1187 : !> \param pmat ...
1188 : !> \param matrix_s ...
1189 : !> \param has_unit_metric ...
1190 : !> \param dft_control ...
1191 : !> \param particle_set ...
1192 : !> \param atomic_kind_set ...
1193 : !> \param qs_kind_set ...
1194 : !> \param nspin ...
1195 : !> \param nelectron_spin ...
1196 : !> \param para_env ...
1197 : ! **************************************************************************************************
1198 2998 : SUBROUTINE calculate_mopac_dm(pmat, matrix_s, has_unit_metric, &
1199 : dft_control, particle_set, atomic_kind_set, qs_kind_set, &
1200 2998 : nspin, nelectron_spin, para_env)
1201 : TYPE(dbcsr_p_type), DIMENSION(:), INTENT(INOUT) :: pmat
1202 : TYPE(dbcsr_type), INTENT(INOUT) :: matrix_s
1203 : LOGICAL :: has_unit_metric
1204 : TYPE(dft_control_type), POINTER :: dft_control
1205 : TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
1206 : TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
1207 : TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
1208 : INTEGER, INTENT(IN) :: nspin
1209 : INTEGER, DIMENSION(:), INTENT(IN) :: nelectron_spin
1210 : TYPE(mp_para_env_type) :: para_env
1211 :
1212 : CHARACTER(LEN=*), PARAMETER :: routineN = 'calculate_mopac_dm'
1213 :
1214 : INTEGER :: atom_a, handle, iatom, ikind, iset, &
1215 : isgf, isgfa, ishell, ispin, la, maxl, &
1216 : maxll, na, nao, natom, ncount, nset, &
1217 : nsgf, z
1218 : INTEGER, ALLOCATABLE, DIMENSION(:) :: first_sgf
1219 : INTEGER, DIMENSION(25) :: laox, naox
1220 : INTEGER, DIMENSION(5) :: occupation
1221 2998 : INTEGER, DIMENSION(:), POINTER :: atom_list, elec_conf, nshell
1222 2998 : INTEGER, DIMENSION(:, :), POINTER :: first_sgfa, l, last_sgfa
1223 : LOGICAL :: has_pot
1224 : REAL(KIND=dp) :: maxocc, my_sum, nelec, occ, paa, rscale, &
1225 : trps1, trps2, yy, zeff
1226 2998 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: econf, pdiag, sdiag
1227 : REAL(KIND=dp), DIMENSION(0:3) :: edftb
1228 : TYPE(all_potential_type), POINTER :: all_potential
1229 : TYPE(cneo_potential_type), POINTER :: cneo_potential
1230 : TYPE(dbcsr_type), POINTER :: matrix_p
1231 : TYPE(gth_potential_type), POINTER :: gth_potential
1232 : TYPE(gto_basis_set_type), POINTER :: orb_basis_set
1233 : TYPE(sgp_potential_type), POINTER :: sgp_potential
1234 : TYPE(xtb_atom_type), POINTER :: xtb_kind
1235 :
1236 2998 : CALL timeset(routineN, handle)
1237 :
1238 6236 : DO ispin = 1, nspin
1239 3238 : matrix_p => pmat(ispin)%matrix
1240 6236 : CALL dbcsr_set(matrix_p, 0.0_dp)
1241 : END DO
1242 :
1243 2998 : natom = SIZE(particle_set)
1244 2998 : CALL dbcsr_get_info(pmat(1)%matrix, nfullrows_total=nao)
1245 2998 : IF (nspin == 1) THEN
1246 : maxocc = 2.0_dp
1247 : ELSE
1248 240 : maxocc = 1.0_dp
1249 : END IF
1250 :
1251 8994 : ALLOCATE (first_sgf(natom))
1252 :
1253 2998 : CALL get_particle_set(particle_set, qs_kind_set, first_sgf=first_sgf)
1254 2998 : CALL get_qs_kind_set(qs_kind_set, maxlgto=maxl)
1255 :
1256 8994 : ALLOCATE (econf(0:maxl))
1257 :
1258 8994 : ALLOCATE (pdiag(nao))
1259 2998 : pdiag(:) = 0.0_dp
1260 :
1261 5996 : ALLOCATE (sdiag(nao))
1262 2998 : sdiag(:) = 0.0_dp
1263 2998 : IF (has_unit_metric) THEN
1264 12634 : sdiag(:) = 1.0_dp
1265 : ELSE
1266 2634 : CALL dbcsr_get_diag(matrix_s, sdiag)
1267 2634 : CALL para_env%sum(sdiag)
1268 : END IF
1269 :
1270 2998 : ncount = 0
1271 2998 : trps1 = 0.0_dp
1272 2998 : trps2 = 0.0_dp
1273 2998 : pdiag(:) = 0.0_dp
1274 :
1275 8994 : IF (SUM(nelectron_spin) /= 0) THEN
1276 8168 : DO ikind = 1, SIZE(atomic_kind_set)
1277 :
1278 5184 : CALL get_atomic_kind(atomic_kind_set(ikind), natom=natom, atom_list=atom_list)
1279 : CALL get_qs_kind(qs_kind_set(ikind), basis_set=orb_basis_set, &
1280 : all_potential=all_potential, &
1281 : gth_potential=gth_potential, &
1282 : sgp_potential=sgp_potential, &
1283 5184 : cneo_potential=cneo_potential)
1284 : has_pot = ASSOCIATED(all_potential) .OR. ASSOCIATED(gth_potential) .OR. &
1285 5184 : ASSOCIATED(sgp_potential) .OR. ASSOCIATED(cneo_potential)
1286 :
1287 5184 : IF (dft_control%qs_control%dftb) THEN
1288 : CALL get_dftb_atom_param(qs_kind_set(ikind)%dftb_parameter, &
1289 1320 : lmax=maxll, occupation=edftb)
1290 1320 : maxll = MIN(maxll, maxl)
1291 4044 : econf(0:maxl) = edftb(0:maxl)
1292 3864 : ELSE IF (dft_control%qs_control%xtb) THEN
1293 2818 : CALL get_qs_kind(qs_kind_set(ikind), xtb_parameter=xtb_kind)
1294 2818 : CALL get_xtb_atom_param(xtb_kind, z=z, natorb=nsgf, nao=naox, lao=laox, occupation=occupation)
1295 1046 : ELSE IF (has_pot) THEN
1296 1046 : CALL get_atomic_kind(atomic_kind_set(ikind), z=z)
1297 1046 : CALL get_qs_kind(qs_kind_set(ikind), nsgf=nsgf, elec_conf=elec_conf, zeff=zeff)
1298 1046 : maxll = MIN(SIZE(elec_conf) - 1, maxl)
1299 1046 : econf(:) = 0.0_dp
1300 3288 : econf(0:maxll) = 0.5_dp*maxocc*REAL(elec_conf(0:maxll), dp)
1301 : ELSE
1302 : CYCLE
1303 : END IF
1304 :
1305 : ! MOPAC TYPE GUESS
1306 13352 : IF (dft_control%qs_control%dftb) THEN
1307 5680 : DO iatom = 1, natom
1308 4360 : atom_a = atom_list(iatom)
1309 4360 : isgfa = first_sgf(atom_a)
1310 12492 : DO la = 0, maxll
1311 4360 : SELECT CASE (la)
1312 : CASE (0)
1313 4360 : pdiag(isgfa) = econf(0)
1314 : CASE (1)
1315 2116 : pdiag(isgfa + 1) = econf(1)/3._dp
1316 2116 : pdiag(isgfa + 2) = econf(1)/3._dp
1317 2116 : pdiag(isgfa + 3) = econf(1)/3._dp
1318 : CASE (2)
1319 336 : pdiag(isgfa + 4) = econf(2)/5._dp
1320 336 : pdiag(isgfa + 5) = econf(2)/5._dp
1321 336 : pdiag(isgfa + 6) = econf(2)/5._dp
1322 336 : pdiag(isgfa + 7) = econf(2)/5._dp
1323 336 : pdiag(isgfa + 8) = econf(2)/5._dp
1324 : CASE (3)
1325 0 : pdiag(isgfa + 9) = econf(3)/7._dp
1326 0 : pdiag(isgfa + 10) = econf(3)/7._dp
1327 0 : pdiag(isgfa + 11) = econf(3)/7._dp
1328 0 : pdiag(isgfa + 12) = econf(3)/7._dp
1329 0 : pdiag(isgfa + 13) = econf(3)/7._dp
1330 0 : pdiag(isgfa + 14) = econf(3)/7._dp
1331 0 : pdiag(isgfa + 15) = econf(3)/7._dp
1332 : CASE DEFAULT
1333 6812 : CPABORT("Only 0, 1, 2, 3 are supported as the value of la")
1334 : END SELECT
1335 : END DO
1336 : END DO
1337 3864 : ELSE IF (dft_control%qs_control%xtb) THEN
1338 12612 : DO iatom = 1, natom
1339 9794 : atom_a = atom_list(iatom)
1340 9794 : isgfa = first_sgf(atom_a)
1341 12612 : IF (z == 1 .AND. nsgf == 2) THEN
1342 : ! Hydrogen 2s basis
1343 2446 : pdiag(isgfa) = 1.0_dp/REAL(nspin, dp)
1344 2446 : pdiag(isgfa + 1) = 0.0_dp
1345 : ELSE
1346 62236 : DO isgf = 1, nsgf
1347 54888 : na = naox(isgf)
1348 54888 : la = laox(isgf)
1349 54888 : occ = REAL(occupation(la + 1), dp)/REAL(2*la + 1, dp)
1350 54888 : occ = occ/REAL(nspin, dp)
1351 62236 : pdiag(isgfa + isgf - 1) = occ
1352 : END DO
1353 : END IF
1354 : END DO
1355 1046 : ELSE IF (dft_control%qs_control%semi_empirical) THEN
1356 966 : yy = REAL(dft_control%charge, KIND=dp)/REAL(nao, KIND=dp)
1357 5522 : DO iatom = 1, natom
1358 4556 : atom_a = atom_list(iatom)
1359 4556 : isgfa = first_sgf(atom_a)
1360 966 : SELECT CASE (nsgf)
1361 : CASE (1) ! s-basis
1362 2212 : pdiag(isgfa) = (zeff - yy)*0.5_dp*maxocc
1363 : CASE (4) ! sp-basis
1364 2218 : IF (z == 1) THEN
1365 : ! special case: hydrogen with sp basis
1366 136 : pdiag(isgfa) = (zeff - yy)*0.5_dp*maxocc
1367 136 : pdiag(isgfa + 1) = 0._dp
1368 136 : pdiag(isgfa + 2) = 0._dp
1369 136 : pdiag(isgfa + 3) = 0._dp
1370 : ELSE
1371 2082 : pdiag(isgfa) = (zeff*0.25_dp - yy)*0.5_dp*maxocc
1372 2082 : pdiag(isgfa + 1) = (zeff*0.25_dp - yy)*0.5_dp*maxocc
1373 2082 : pdiag(isgfa + 2) = (zeff*0.25_dp - yy)*0.5_dp*maxocc
1374 2082 : pdiag(isgfa + 3) = (zeff*0.25_dp - yy)*0.5_dp*maxocc
1375 : END IF
1376 : CASE (9) ! spd-basis
1377 126 : IF (z < 21 .OR. z > 30 .AND. z < 39 .OR. z > 48 .AND. z < 57) THEN
1378 : ! Main Group Element: The "d" shell is formally empty.
1379 92 : pdiag(isgfa) = (zeff*0.25_dp - yy)*0.5_dp*maxocc
1380 92 : pdiag(isgfa + 1) = (zeff*0.25_dp - yy)*0.5_dp*maxocc
1381 92 : pdiag(isgfa + 2) = (zeff*0.25_dp - yy)*0.5_dp*maxocc
1382 92 : pdiag(isgfa + 3) = (zeff*0.25_dp - yy)*0.5_dp*maxocc
1383 92 : pdiag(isgfa + 4) = (-yy)*0.5_dp*maxocc
1384 92 : pdiag(isgfa + 5) = (-yy)*0.5_dp*maxocc
1385 92 : pdiag(isgfa + 6) = (-yy)*0.5_dp*maxocc
1386 92 : pdiag(isgfa + 7) = (-yy)*0.5_dp*maxocc
1387 92 : pdiag(isgfa + 8) = (-yy)*0.5_dp*maxocc
1388 34 : ELSE IF (z < 99) THEN
1389 34 : my_sum = zeff - 9.0_dp*yy
1390 : ! First, put 2 electrons in the 's' shell
1391 34 : pdiag(isgfa) = (MAX(0.0_dp, MIN(my_sum, 2.0_dp)))*0.5_dp*maxocc
1392 34 : my_sum = my_sum - 2.0_dp
1393 34 : IF (my_sum > 0.0_dp) THEN
1394 : ! Now put as many electrons as possible into the 'd' shell
1395 30 : pdiag(isgfa + 4) = (MAX(0.0_dp, MIN(my_sum*0.2_dp, 2.0_dp)))*0.5_dp*maxocc
1396 30 : pdiag(isgfa + 5) = (MAX(0.0_dp, MIN(my_sum*0.2_dp, 2.0_dp)))*0.5_dp*maxocc
1397 30 : pdiag(isgfa + 6) = (MAX(0.0_dp, MIN(my_sum*0.2_dp, 2.0_dp)))*0.5_dp*maxocc
1398 30 : pdiag(isgfa + 7) = (MAX(0.0_dp, MIN(my_sum*0.2_dp, 2.0_dp)))*0.5_dp*maxocc
1399 30 : pdiag(isgfa + 8) = (MAX(0.0_dp, MIN(my_sum*0.2_dp, 2.0_dp)))*0.5_dp*maxocc
1400 30 : my_sum = MAX(0.0_dp, my_sum - 10.0_dp)
1401 : ! Put the remaining electrons in the 'p' shell
1402 30 : pdiag(isgfa + 1) = (my_sum/3.0_dp)*0.5_dp*maxocc
1403 30 : pdiag(isgfa + 2) = (my_sum/3.0_dp)*0.5_dp*maxocc
1404 30 : pdiag(isgfa + 3) = (my_sum/3.0_dp)*0.5_dp*maxocc
1405 : END IF
1406 : END IF
1407 : CASE DEFAULT
1408 : CALL cp_abort(__LOCATION__, &
1409 : "Only 1 for s-basis, 4 for sp-basis and 9 for spd-basis "// &
1410 : "are supported as the value of nsgf in the MOPAC type "// &
1411 4556 : "guess for semi-empirical methods")
1412 : END SELECT
1413 : END DO
1414 : ELSE
1415 : CALL get_gto_basis_set(gto_basis_set=orb_basis_set, &
1416 : nset=nset, &
1417 : nshell=nshell, &
1418 : l=l, &
1419 : first_sgf=first_sgfa, &
1420 80 : last_sgf=last_sgfa)
1421 :
1422 212 : DO iset = 1, nset
1423 516 : DO ishell = 1, nshell(iset)
1424 304 : la = l(ishell, iset)
1425 304 : nelec = maxocc*REAL(2*la + 1, dp)
1426 436 : IF (econf(la) > 0.0_dp) THEN
1427 148 : IF (econf(la) >= nelec) THEN
1428 68 : paa = maxocc
1429 68 : econf(la) = econf(la) - nelec
1430 : ELSE
1431 80 : paa = maxocc*econf(la)/nelec
1432 80 : econf(la) = 0.0_dp
1433 80 : ncount = ncount + NINT(nelec/maxocc)
1434 : END IF
1435 432 : DO isgfa = first_sgfa(ishell, iset), last_sgfa(ishell, iset)
1436 2604 : DO iatom = 1, natom
1437 2172 : atom_a = atom_list(iatom)
1438 2172 : isgf = first_sgf(atom_a) + isgfa - 1
1439 2172 : pdiag(isgf) = paa
1440 2456 : IF (paa == maxocc) THEN
1441 538 : trps1 = trps1 + paa*sdiag(isgf)
1442 : ELSE
1443 1634 : trps2 = trps2 + paa*sdiag(isgf)
1444 : END IF
1445 : END DO
1446 : END DO
1447 : END IF
1448 : END DO ! ishell
1449 : END DO ! iset
1450 : END IF
1451 : END DO ! ikind
1452 :
1453 2984 : IF (trps2 == 0.0_dp) THEN
1454 87312 : DO isgf = 1, nao
1455 87312 : IF (sdiag(isgf) > 0.0_dp) pdiag(isgf) = pdiag(isgf)/sdiag(isgf)
1456 : END DO
1457 6086 : DO ispin = 1, nspin
1458 6086 : IF (nelectron_spin(ispin) /= 0) THEN
1459 90350 : rscale = SUM(pdiag)/REAL(nelectron_spin(ispin), dp)
1460 3156 : matrix_p => pmat(ispin)%matrix
1461 90350 : pdiag = pdiag/rscale
1462 3156 : CALL dbcsr_set_diag(matrix_p, pdiag)
1463 90350 : pdiag = pdiag*rscale
1464 : END IF
1465 : END DO
1466 : ELSE
1467 120 : DO ispin = 1, nspin
1468 120 : IF (nelectron_spin(ispin) /= 0) THEN
1469 62 : rscale = (REAL(nelectron_spin(ispin), dp) - trps1)/trps2
1470 5674 : DO isgf = 1, nao
1471 5674 : IF (pdiag(isgf) < maxocc) pdiag(isgf) = rscale*pdiag(isgf)
1472 : END DO
1473 62 : matrix_p => pmat(ispin)%matrix
1474 62 : CALL dbcsr_set_diag(matrix_p, pdiag)
1475 5674 : DO isgf = 1, nao
1476 5674 : IF (pdiag(isgf) < maxocc) pdiag(isgf) = pdiag(isgf)/rscale
1477 : END DO
1478 : END IF
1479 : END DO
1480 : END IF
1481 : END IF
1482 :
1483 2998 : DEALLOCATE (econf)
1484 :
1485 2998 : DEALLOCATE (first_sgf)
1486 :
1487 2998 : DEALLOCATE (pdiag)
1488 :
1489 2998 : DEALLOCATE (sdiag)
1490 :
1491 2998 : CALL timestop(handle)
1492 :
1493 8994 : END SUBROUTINE calculate_mopac_dm
1494 :
1495 0 : END MODULE qs_initial_guess
|