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 Determine active space Hamiltonian
10 : !> \par History
11 : !> 04.2016 created [JGH]
12 : !> \author JGH
13 : ! **************************************************************************************************
14 : MODULE qs_active_space_methods
15 : USE admm_types, ONLY: admm_type, &
16 : get_admm_env, &
17 : admm_env_release
18 : USE atomic_kind_types, ONLY: atomic_kind_type
19 : USE basis_set_types, ONLY: allocate_sto_basis_set, &
20 : create_gto_from_sto_basis, &
21 : deallocate_sto_basis_set, &
22 : gto_basis_set_type, &
23 : init_orb_basis_set, &
24 : set_sto_basis_set, &
25 : srules, &
26 : sto_basis_set_type
27 : USE cell_types, ONLY: cell_type, use_perd_none, use_perd_xyz
28 : USE cell_methods, ONLY: init_cell, set_cell_param, write_cell_low
29 : USE cp_blacs_env, ONLY: cp_blacs_env_type, cp_blacs_env_create, cp_blacs_env_release, BLACS_GRID_SQUARE
30 : USE cp_control_types, ONLY: dft_control_type, qs_control_type
31 : USE cp_dbcsr_operations, ONLY: cp_dbcsr_plus_fm_fm_t, &
32 : cp_dbcsr_sm_fm_multiply, &
33 : dbcsr_allocate_matrix_set, &
34 : cp_dbcsr_m_by_n_from_template, copy_dbcsr_to_fm
35 : USE cp_dbcsr_output, ONLY: cp_dbcsr_write_sparse_matrix
36 : USE cp_files, ONLY: close_file, &
37 : file_exists, &
38 : open_file
39 : USE cp_fm_basic_linalg, ONLY: cp_fm_column_scale
40 : USE cp_fm_struct, ONLY: cp_fm_struct_create, &
41 : cp_fm_struct_release, &
42 : cp_fm_struct_type
43 : USE cp_fm_types, ONLY: &
44 : cp_fm_create, cp_fm_get_element, cp_fm_get_info, cp_fm_init_random, cp_fm_release, &
45 : cp_fm_set_all, cp_fm_set_element, cp_fm_to_fm, cp_fm_type, cp_fm_write_formatted
46 : USE cp_log_handling, ONLY: cp_get_default_logger, &
47 : cp_logger_get_default_io_unit, &
48 : cp_logger_type
49 : USE cp_output_handling, ONLY: &
50 : cp_p_file, cp_print_key_finished_output, cp_print_key_should_output, cp_print_key_unit_nr, &
51 : debug_print_level, high_print_level, low_print_level, medium_print_level, &
52 : silent_print_level
53 : USE cp_realspace_grid_cube, ONLY: cp_pw_to_cube
54 : USE cp_dbcsr_api, ONLY: &
55 : dbcsr_copy, dbcsr_csr_create, dbcsr_csr_type, dbcsr_p_type, dbcsr_type, dbcsr_release, &
56 : dbcsr_type_no_symmetry, dbcsr_create, dbcsr_set, dbcsr_multiply, dbcsr_iterator_next_block, &
57 : dbcsr_iterator_start, dbcsr_iterator_stop, dbcsr_iterator_blocks_left, &
58 : dbcsr_iterator_type, dbcsr_type_symmetric, dbcsr_get_occupation, dbcsr_get_info
59 : USE erf_complex, ONLY: erfz_fast
60 : USE group_dist_types, ONLY: get_group_dist, release_group_dist, group_dist_d1_type
61 : USE input_constants, ONLY: &
62 : casci_canonical, eri_method_full_gpw, eri_method_gpw_ht, eri_operator_coulomb, &
63 : eri_operator_erf, eri_operator_erfc, eri_operator_gaussian, eri_operator_yukawa, &
64 : eri_operator_trunc, eri_operator_lr_trunc, &
65 : manual_selection, mao_projection, no_solver, qiskit_solver, wannier_projection, &
66 : eri_poisson_analytic, eri_poisson_periodic, eri_poisson_mt, high_spin_roks
67 : USE input_section_types, ONLY: section_vals_get, section_vals_get_subs_vals, &
68 : section_vals_set_subs_vals, section_vals_type, &
69 : section_vals_val_get, &
70 : section_vals_val_set
71 : USE ISO_C_BINDING, ONLY: c_null_char
72 : USE kinds, ONLY: default_path_length, &
73 : default_string_length, &
74 : dp, &
75 : int_8
76 : USE hfx_types, ONLY: hfx_create, hfx_release
77 : USE machine, ONLY: m_walltime, m_flush
78 : USE mathlib, ONLY: diamat_all
79 : USE mathconstants, ONLY: fourpi, twopi, pi, rootpi
80 : USE memory_utilities, ONLY: reallocate
81 : USE message_passing, ONLY: mp_comm_type, &
82 : mp_para_env_type, &
83 : mp_para_env_release
84 : USE mp2_gpw, ONLY: create_mat_munu, grep_rows_in_subgroups, build_dbcsr_from_rows
85 : USE mt_util, ONLY: MT0D
86 : USE parallel_gemm_api, ONLY: parallel_gemm
87 : USE particle_list_types, ONLY: particle_list_type
88 : USE particle_types, ONLY: particle_type
89 : USE periodic_table, ONLY: ptable
90 : USE physcon, ONLY: angstrom, bohr
91 : USE preconditioner_types, ONLY: preconditioner_type
92 : USE pw_env_methods, ONLY: pw_env_create, &
93 : pw_env_rebuild
94 : USE pw_env_types, ONLY: pw_env_get, &
95 : pw_env_release, &
96 : pw_env_type
97 : USE pw_methods, ONLY: pw_integrate_function, &
98 : pw_multiply, &
99 : pw_multiply_with, &
100 : pw_transfer, &
101 : pw_zero, pw_integral_ab, pw_scale, &
102 : pw_gauss_damp, pw_compl_gauss_damp
103 : USE pw_poisson_methods, ONLY: pw_poisson_rebuild, &
104 : pw_poisson_solve
105 : USE pw_poisson_types, ONLY: ANALYTIC0D, &
106 : PERIODIC3D, &
107 : greens_fn_type, &
108 : pw_poisson_analytic, &
109 : pw_poisson_periodic, &
110 : pw_poisson_type
111 : USE pw_pool_types, ONLY: &
112 : pw_pool_type
113 : USE pw_types, ONLY: &
114 : pw_c1d_gs_type, &
115 : pw_r3d_rs_type
116 : USE qcschema, ONLY: qcschema_env_create, &
117 : qcschema_env_release, &
118 : qcschema_to_hdf5, &
119 : qcschema_type
120 : USE qs_active_space_types, ONLY: active_space_type, &
121 : create_active_space_type, &
122 : csr_idx_from_combined, &
123 : csr_idx_to_combined, &
124 : eri_type, &
125 : eri_type_eri_element_func, &
126 : get_irange_csr
127 : USE qs_active_space_utils, ONLY: eri_to_array, &
128 : subspace_matrix_to_array
129 : USE qs_collocate_density, ONLY: calculate_wavefunction
130 : USE qs_density_matrices, ONLY: calculate_density_matrix
131 : USE qs_energy_types, ONLY: qs_energy_type
132 : USE qs_environment_types, ONLY: get_qs_env, &
133 : qs_environment_type, &
134 : set_qs_env
135 : USE qs_integrate_potential, ONLY: integrate_v_rspace
136 : USE qs_kind_types, ONLY: qs_kind_type
137 : USE qs_ks_methods, ONLY: qs_ks_update_qs_env, qs_ks_build_kohn_sham_matrix, &
138 : evaluate_core_matrix_traces
139 : USE qs_ks_types, ONLY: qs_ks_did_change, &
140 : qs_ks_env_type, set_ks_env
141 : USE qs_mo_io, ONLY: write_mo_set_to_output_unit
142 : USE qs_mo_methods, ONLY: calculate_subspace_eigenvalues
143 : USE qs_mo_types, ONLY: allocate_mo_set, &
144 : get_mo_set, &
145 : init_mo_set, &
146 : mo_set_type
147 : USE qs_neighbor_list_types, ONLY: neighbor_list_set_p_type, release_neighbor_list_sets
148 : USE qs_ot_eigensolver, ONLY: ot_eigensolver
149 : USE qs_rho_methods, ONLY: qs_rho_update_rho
150 : USE qs_rho_types, ONLY: qs_rho_get, &
151 : qs_rho_type
152 : USE qs_subsys_types, ONLY: qs_subsys_get, &
153 : qs_subsys_type
154 : USE qs_scf_post_scf, ONLY: qs_scf_compute_properties
155 : USE scf_control_types, ONLY: scf_control_type
156 : #ifndef __NO_SOCKETS
157 : USE sockets_interface, ONLY: accept_socket, &
158 : close_socket, &
159 : listen_socket, &
160 : open_bind_socket, &
161 : readbuffer, &
162 : remove_socket_file, &
163 : writebuffer
164 : #endif
165 : USE task_list_methods, ONLY: generate_qs_task_list
166 : USE task_list_types, ONLY: allocate_task_list, &
167 : deallocate_task_list, &
168 : task_list_type
169 : USE util, ONLY: get_limit
170 : #include "./base/base_uses.f90"
171 :
172 : IMPLICIT NONE
173 : PRIVATE
174 :
175 : CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_active_space_methods'
176 :
177 : PUBLIC :: active_space_main
178 :
179 : TYPE, EXTENDS(eri_type_eri_element_func) :: eri_fcidump_print
180 : INTEGER :: unit_nr = -1, bra_start = -1, ket_start = -1
181 : CONTAINS
182 : PROCEDURE :: func => eri_fcidump_print_func
183 : END TYPE eri_fcidump_print
184 :
185 : TYPE, EXTENDS(eri_type_eri_element_func) :: eri_fcidump_checksum
186 : INTEGER :: bra_start = 0, ket_start = 0
187 : REAL(KIND=dp) :: checksum = 0.0_dp
188 : CONTAINS
189 : PROCEDURE, PASS :: set => eri_fcidump_set
190 : PROCEDURE :: func => eri_fcidump_checksum_func
191 : END TYPE eri_fcidump_checksum
192 :
193 : CONTAINS
194 :
195 : ! **************************************************************************************************
196 : !> \brief Sets the starting indices of the bra and ket.
197 : !> \param this object reference
198 : !> \param bra_start starting index of the bra
199 : !> \param ket_start starting index of the ket
200 : ! **************************************************************************************************
201 92 : SUBROUTINE eri_fcidump_set(this, bra_start, ket_start)
202 : CLASS(eri_fcidump_checksum) :: this
203 : INTEGER, INTENT(IN) :: bra_start, ket_start
204 92 : this%bra_start = bra_start
205 92 : this%ket_start = ket_start
206 92 : END SUBROUTINE eri_fcidump_set
207 :
208 : ! **************************************************************************************************
209 : !> \brief Main method for determining the active space Hamiltonian
210 : !> \param qs_env ...
211 : ! **************************************************************************************************
212 22541 : SUBROUTINE active_space_main(qs_env)
213 : TYPE(qs_environment_type), POINTER :: qs_env
214 :
215 : CHARACTER(len=*), PARAMETER :: routineN = 'active_space_main'
216 :
217 : CHARACTER(len=10) :: cshell, lnam(5)
218 : CHARACTER(len=default_path_length) :: qcschema_filename
219 : CHARACTER(LEN=default_string_length) :: basis_type
220 : INTEGER :: as_solver, eri_method, eri_operator, eri_print, group_size, handle, i, iatom, &
221 : ishell, isp, ispin, iw, j, jm, m, max_orb_ind, mselect, n1, n2, nao, natom, nel, &
222 : nelec_active, nelec_inactive, nelec_total, nmo, nmo_active, nmo_available, nmo_inactive, &
223 : nmo_inactive_remaining, nmo_occ, nmo_virtual, nn1, nn2, nrow_global, nspins
224 : INTEGER, DIMENSION(5) :: nshell
225 22541 : INTEGER, DIMENSION(:), POINTER :: invals
226 : LOGICAL :: do_ddapc, do_kpoints, ex_omega, &
227 : ex_operator, ex_perd, ex_rcut, &
228 : explicit, stop_after_print, store_wfn
229 : REAL(KIND=dp) :: alpha, eri_eps_filter, eri_eps_grid, eri_eps_int, eri_gpw_cutoff, &
230 : eri_op_omega, eri_rcut, eri_rel_cutoff, fel, focc, maxocc, nze_percentage
231 22541 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: eigenvalues
232 22541 : REAL(KIND=dp), DIMENSION(:), POINTER :: evals_virtual
233 : TYPE(active_space_type), POINTER :: active_space_env
234 22541 : TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
235 : TYPE(cell_type), POINTER :: cell
236 : TYPE(cp_blacs_env_type), POINTER :: context
237 : TYPE(cp_fm_struct_type), POINTER :: fm_struct_tmp
238 : TYPE(cp_fm_type) :: fm_dummy, mo_virtual
239 : TYPE(cp_fm_type), POINTER :: fm_target_active, fm_target_inactive, &
240 : fmat, mo_coeff, mo_ref, mo_target
241 : TYPE(cp_logger_type), POINTER :: logger
242 : TYPE(dbcsr_csr_type), POINTER :: eri_mat
243 45082 : TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: ks_matrix, rho_ao, s_matrix
244 : TYPE(dbcsr_type), POINTER :: denmat
245 : TYPE(dft_control_type), POINTER :: dft_control
246 22541 : TYPE(mo_set_type), DIMENSION(:), POINTER :: mos
247 : TYPE(mo_set_type), POINTER :: mo_set, mo_set_active, mo_set_inactive
248 : TYPE(mp_para_env_type), POINTER :: para_env
249 22541 : TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
250 : TYPE(preconditioner_type), POINTER :: local_preconditioner
251 90164 : TYPE(qcschema_type) :: qcschema_env
252 : TYPE(qs_energy_type), POINTER :: energy
253 22541 : TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
254 : TYPE(qs_ks_env_type), POINTER :: ks_env
255 : TYPE(qs_rho_type), POINTER :: rho
256 : TYPE(scf_control_type), POINTER :: scf_control
257 : TYPE(section_vals_type), POINTER :: adiabatic_rescaling, as_input, &
258 : hfx_section, input, loc_print, &
259 : loc_section, print_orb, xc_section
260 :
261 : !--------------------------------------------------------------------------------------------!
262 :
263 22541 : CALL get_qs_env(qs_env, input=input)
264 22541 : as_input => section_vals_get_subs_vals(input, "DFT%ACTIVE_SPACE")
265 22541 : CALL section_vals_get(as_input, explicit=explicit)
266 22541 : IF (.NOT. explicit) RETURN
267 68 : CALL timeset(routineN, handle)
268 :
269 68 : logger => cp_get_default_logger()
270 68 : iw = cp_logger_get_default_io_unit(logger)
271 :
272 68 : IF (iw > 0) THEN
273 : WRITE (iw, '(/,T2,A)') &
274 34 : '!-----------------------------------------------------------------------------!'
275 34 : WRITE (iw, '(T26,A)') "Active Space Embedding Module"
276 : WRITE (iw, '(T2,A)') &
277 34 : '!-----------------------------------------------------------------------------!'
278 : END IF
279 :
280 : ! k-points?
281 68 : CALL get_qs_env(qs_env, do_kpoints=do_kpoints, dft_control=dft_control)
282 68 : IF (do_kpoints) THEN
283 0 : CALL cp_abort(__LOCATION__, "k-points not supported in active space module")
284 : END IF
285 :
286 : ! adiabatic rescaling?
287 68 : adiabatic_rescaling => section_vals_get_subs_vals(input, "DFT%XC%ADIABATIC_RESCALING")
288 68 : CALL section_vals_get(adiabatic_rescaling, explicit=explicit)
289 68 : IF (explicit) THEN
290 0 : CALL cp_abort(__LOCATION__, "Adiabatic rescaling not supported in active space module")
291 : END IF
292 :
293 : ! Setup the possible usage of DDAPC charges
294 : do_ddapc = dft_control%qs_control%ddapc_restraint .OR. &
295 : qs_env%cp_ddapc_ewald%do_decoupling .OR. &
296 : qs_env%cp_ddapc_ewald%do_qmmm_periodic_decpl .OR. &
297 68 : qs_env%cp_ddapc_ewald%do_solvation
298 : IF (do_ddapc) THEN
299 0 : CALL cp_abort(__LOCATION__, "DDAPC charges are not supported in the active space module")
300 : END IF
301 68 : IF (dft_control%do_sccs) THEN
302 0 : CALL cp_abort(__LOCATION__, "SCCS is not supported in the active space module")
303 : END IF
304 68 : IF (dft_control%correct_surf_dip) THEN
305 0 : IF (dft_control%surf_dip_correct_switch) THEN
306 0 : CALL cp_abort(__LOCATION__, "Surface dipole correction not supported in the AS module")
307 : END IF
308 : END IF
309 68 : IF (dft_control%smeagol_control%smeagol_enabled) THEN
310 0 : CALL cp_abort(__LOCATION__, "SMEAGOL is not supported in the active space module")
311 : END IF
312 68 : IF (dft_control%qs_control%do_kg) THEN
313 0 : CALL cp_abort(__LOCATION__, "KG correction not supported in the active space module")
314 : END IF
315 :
316 68 : NULLIFY (active_space_env)
317 68 : CALL create_active_space_type(active_space_env)
318 68 : active_space_env%energy_total = 0.0_dp
319 68 : active_space_env%energy_ref = 0.0_dp
320 68 : active_space_env%energy_inactive = 0.0_dp
321 68 : active_space_env%energy_active = 0.0_dp
322 :
323 : ! input options
324 :
325 : ! figure out what needs to be printed/stored
326 68 : IF (BTEST(cp_print_key_should_output(logger%iter_info, as_input, "FCIDUMP"), cp_p_file)) THEN
327 68 : active_space_env%fcidump = .TRUE.
328 : END IF
329 :
330 68 : CALL section_vals_val_get(as_input, "QCSCHEMA", c_val=qcschema_filename, explicit=explicit)
331 68 : IF (explicit) THEN
332 0 : active_space_env%qcschema = .TRUE.
333 0 : active_space_env%qcschema_filename = qcschema_filename
334 : END IF
335 :
336 68 : CALL section_vals_val_get(as_input, "ACTIVE_ELECTRONS", i_val=nelec_active)
337 68 : CALL get_qs_env(qs_env, nelectron_total=nelec_total)
338 :
339 68 : IF (nelec_active <= 0) CPABORT("Specify a positive number of active electrons.")
340 68 : IF (nelec_active > nelec_total) CPABORT("More active electrons than total electrons.")
341 :
342 68 : nelec_inactive = nelec_total - nelec_active
343 68 : IF (MOD(nelec_inactive, 2) /= 0) THEN
344 0 : CPABORT("The remaining number of inactive electrons has to be even.")
345 : END IF
346 :
347 68 : CALL section_vals_val_get(as_input, "ALPHA", r_val=alpha)
348 68 : IF (alpha < 0.0 .OR. alpha > 1.0) CPABORT("Specify a damping factor between 0 and 1.")
349 68 : active_space_env%alpha = alpha
350 :
351 68 : IF (iw > 0) THEN
352 34 : WRITE (iw, '(T3,A,T70,I10)') "Total number of electrons", nelec_total
353 34 : WRITE (iw, '(T3,A,T70,I10)') "Number of inactive electrons", nelec_inactive
354 34 : WRITE (iw, '(T3,A,T70,I10)') "Number of active electrons", nelec_active
355 : END IF
356 :
357 68 : CALL get_qs_env(qs_env, dft_control=dft_control)
358 68 : nspins = dft_control%nspins
359 :
360 68 : active_space_env%nelec_active = nelec_active
361 68 : active_space_env%nelec_inactive = nelec_inactive
362 68 : active_space_env%nelec_total = nelec_total
363 68 : active_space_env%nspins = nspins
364 68 : active_space_env%multiplicity = dft_control%multiplicity
365 :
366 : ! define the active/inactive space orbitals
367 68 : CALL section_vals_val_get(as_input, "ACTIVE_ORBITALS", explicit=explicit, i_val=nmo_active)
368 68 : IF (.NOT. explicit) THEN
369 0 : CALL cp_abort(__LOCATION__, "Number of Active Orbitals has to be specified.")
370 : END IF
371 68 : active_space_env%nmo_active = nmo_active
372 : ! this is safe because nelec_inactive is always even
373 68 : nmo_inactive = nelec_inactive/2
374 68 : active_space_env%nmo_inactive = nmo_inactive
375 :
376 68 : CALL section_vals_val_get(as_input, "ORBITAL_SELECTION", i_val=mselect)
377 68 : IF (iw > 0) THEN
378 0 : SELECT CASE (mselect)
379 : CASE DEFAULT
380 0 : CPABORT("Unknown orbital selection method")
381 : CASE (casci_canonical)
382 : WRITE (iw, '(/,T3,A)') &
383 28 : "Active space orbitals selected using energy ordered canonical orbitals"
384 : CASE (wannier_projection)
385 : WRITE (iw, '(/,T3,A)') &
386 0 : "Active space orbitals selected using projected Wannier orbitals"
387 : CASE (mao_projection)
388 : WRITE (iw, '(/,T3,A)') &
389 0 : "Active space orbitals selected using modified atomic orbitals (MAO)"
390 : CASE (manual_selection)
391 : WRITE (iw, '(/,T3,A)') &
392 34 : "Active space orbitals selected manually"
393 : END SELECT
394 :
395 34 : WRITE (iw, '(T3,A,T70,I10)') "Number of inactive orbitals", nmo_inactive
396 34 : WRITE (iw, '(T3,A,T70,I10)') "Number of active orbitals", nmo_active
397 : END IF
398 :
399 : ! get projection spaces
400 68 : CALL section_vals_val_get(as_input, "SUBSPACE_ATOM", i_val=iatom, explicit=explicit)
401 68 : IF (explicit) THEN
402 0 : CALL get_qs_env(qs_env, natom=natom)
403 0 : IF (iatom <= 0 .OR. iatom > natom) THEN
404 0 : IF (iw > 0) THEN
405 0 : WRITE (iw, '(/,T3,A,I3)') "ERROR: SUBSPACE_ATOM number is not valid", iatom
406 : END IF
407 0 : CPABORT("Select a valid SUBSPACE_ATOM")
408 : END IF
409 : END IF
410 68 : CALL section_vals_val_get(as_input, "SUBSPACE_SHELL", c_val=cshell, explicit=explicit)
411 68 : nshell = 0
412 408 : lnam = ""
413 68 : IF (explicit) THEN
414 0 : cshell = ADJUSTL(cshell)
415 0 : n1 = 1
416 0 : DO i = 1, 5
417 0 : ishell = i
418 0 : IF (cshell(n1:n1) == " ") THEN
419 68 : ishell = ishell - 1
420 : EXIT
421 : END IF
422 0 : READ (cshell(n1:), "(I1,A1)") nshell(i), lnam(i)
423 0 : n1 = n1 + 2
424 : END DO
425 : END IF
426 :
427 : ! generate orbitals
428 0 : SELECT CASE (mselect)
429 : CASE DEFAULT
430 0 : CPABORT("Unknown orbital selection method")
431 : CASE (casci_canonical)
432 56 : CALL get_qs_env(qs_env, mos=mos)
433 :
434 : ! total number of occupied orbitals, i.e. inactive plus active MOs
435 56 : nmo_occ = nmo_inactive + nmo_active
436 :
437 : ! set inactive orbital indices, these are trivially 1...nmo_inactive
438 178 : ALLOCATE (active_space_env%inactive_orbitals(nmo_inactive, nspins))
439 120 : DO ispin = 1, nspins
440 160 : DO i = 1, nmo_inactive
441 104 : active_space_env%inactive_orbitals(i, ispin) = i
442 : END DO
443 : END DO
444 :
445 : ! set active orbital indices, these are shifted by nmo_inactive
446 224 : ALLOCATE (active_space_env%active_orbitals(nmo_active, nspins))
447 120 : DO ispin = 1, nspins
448 296 : DO i = 1, nmo_active
449 240 : active_space_env%active_orbitals(i, ispin) = nmo_inactive + i
450 : END DO
451 : END DO
452 :
453 : ! allocate and initialize inactive and active mo coefficients.
454 : ! These are stored in a data structure for the full occupied space:
455 : ! for inactive mos, the active subset is set to zero, vice versa for the active mos
456 : ! TODO: allocate data structures only for the eaxct number MOs
457 56 : maxocc = 2.0_dp
458 56 : IF (nspins > 1) maxocc = 1.0_dp
459 232 : ALLOCATE (active_space_env%mos_active(nspins))
460 176 : ALLOCATE (active_space_env%mos_inactive(nspins))
461 120 : DO ispin = 1, nspins
462 64 : CALL get_mo_set(mos(ispin), mo_coeff=mo_ref, nao=nao)
463 64 : CALL cp_fm_get_info(mo_ref, context=context, para_env=para_env, nrow_global=nrow_global)
464 : ! the right number of active electrons per spin channel is initialized further down
465 64 : CALL allocate_mo_set(active_space_env%mos_active(ispin), nao, nmo_occ, 0, 0.0_dp, maxocc, 0.0_dp)
466 : CALL cp_fm_struct_create(fm_struct_tmp, para_env=para_env, context=context, &
467 64 : nrow_global=nrow_global, ncol_global=nmo_occ)
468 64 : CALL init_mo_set(active_space_env%mos_active(ispin), fm_struct=fm_struct_tmp, name="Active Space MO")
469 64 : CALL cp_fm_struct_release(fm_struct_tmp)
470 64 : IF (nspins == 2) THEN
471 16 : nel = nelec_inactive/2
472 : ELSE
473 48 : nel = nelec_inactive
474 : END IF
475 : CALL allocate_mo_set(active_space_env%mos_inactive(ispin), nao, nmo_occ, nel, &
476 64 : REAL(nel, KIND=dp), maxocc, 0.0_dp)
477 : CALL cp_fm_struct_create(fm_struct_tmp, para_env=para_env, context=context, &
478 64 : nrow_global=nrow_global, ncol_global=nmo_occ)
479 64 : CALL init_mo_set(active_space_env%mos_inactive(ispin), fm_struct=fm_struct_tmp, name="Inactive Space MO")
480 184 : CALL cp_fm_struct_release(fm_struct_tmp)
481 : END DO
482 :
483 : ! create canonical orbitals
484 56 : CALL get_qs_env(qs_env, scf_control=scf_control)
485 56 : IF (dft_control%roks .AND. scf_control%roks_scheme /= high_spin_roks) THEN
486 0 : CPABORT("Unclear how we define MOs in the general restricted case ... stopping")
487 : ELSE
488 56 : IF (dft_control%do_admm) THEN
489 0 : IF (dft_control%do_admm_mo) THEN
490 0 : CPABORT("ADMM currently possible only with purification none_dm")
491 : END IF
492 : END IF
493 :
494 224 : ALLOCATE (eigenvalues(nmo_occ, nspins))
495 336 : eigenvalues = 0.0_dp
496 56 : CALL get_qs_env(qs_env, matrix_ks=ks_matrix, matrix_s=s_matrix, scf_control=scf_control)
497 :
498 : ! calculate virtual MOs and copy inactive and active orbitals
499 56 : IF (iw > 0) THEN
500 28 : WRITE (iw, '(/,T3,A)') "Calculating virtual MOs..."
501 : END IF
502 120 : DO ispin = 1, nspins
503 : ! nmo_available is the number of MOs available from the SCF calculation:
504 : ! this is at least the number of occupied orbitals in the SCF, plus
505 : ! any number of added MOs (virtuals) requested in the SCF section
506 64 : CALL get_mo_set(mos(ispin), mo_coeff=mo_ref, nmo=nmo_available)
507 :
508 : ! calculate how many extra MOs we still have to compute
509 64 : nmo_virtual = nmo_occ - nmo_available
510 64 : nmo_virtual = MAX(nmo_virtual, 0)
511 :
512 : NULLIFY (evals_virtual)
513 128 : ALLOCATE (evals_virtual(nmo_virtual))
514 :
515 : CALL cp_fm_get_info(mo_ref, context=context, para_env=para_env, &
516 64 : nrow_global=nrow_global)
517 :
518 : CALL cp_fm_struct_create(fm_struct_tmp, para_env=para_env, context=context, &
519 64 : nrow_global=nrow_global, ncol_global=nmo_virtual)
520 64 : CALL cp_fm_create(mo_virtual, fm_struct_tmp, name="virtual")
521 64 : CALL cp_fm_struct_release(fm_struct_tmp)
522 64 : CALL cp_fm_init_random(mo_virtual, nmo_virtual)
523 :
524 64 : NULLIFY (local_preconditioner)
525 :
526 : ! compute missing virtual MOs
527 : CALL ot_eigensolver(matrix_h=ks_matrix(ispin)%matrix, matrix_s=s_matrix(1)%matrix, &
528 : matrix_c_fm=mo_virtual, matrix_orthogonal_space_fm=mo_ref, &
529 : eps_gradient=scf_control%eps_lumos, &
530 : preconditioner=local_preconditioner, &
531 : iter_max=scf_control%max_iter_lumos, &
532 64 : size_ortho_space=nmo_available)
533 :
534 : ! get the eigenvalues
535 64 : CALL calculate_subspace_eigenvalues(mo_virtual, ks_matrix(ispin)%matrix, evals_virtual)
536 :
537 : ! we need to send the copy of MOs to preserve the sign
538 64 : CALL cp_fm_create(fm_dummy, mo_ref%matrix_struct)
539 64 : CALL cp_fm_to_fm(mo_ref, fm_dummy)
540 : CALL calculate_subspace_eigenvalues(fm_dummy, ks_matrix(ispin)%matrix, &
541 64 : evals_arg=eigenvalues(:, ispin), do_rotation=.TRUE.)
542 :
543 : ! copy inactive orbitals
544 64 : mo_set => active_space_env%mos_inactive(ispin)
545 64 : CALL get_mo_set(mo_set, mo_coeff=mo_target)
546 104 : DO i = 1, SIZE(active_space_env%inactive_orbitals, 1)
547 40 : m = active_space_env%inactive_orbitals(i, ispin)
548 40 : CALL cp_fm_to_fm(mo_ref, mo_target, 1, m, m)
549 40 : mo_set%eigenvalues(m) = eigenvalues(m, ispin)
550 104 : IF (nspins > 1) THEN
551 28 : mo_set%occupation_numbers(m) = 1.0
552 : ELSE
553 12 : mo_set%occupation_numbers(m) = 2.0
554 : END IF
555 : END DO
556 :
557 : ! copy active orbitals
558 64 : mo_set => active_space_env%mos_active(ispin)
559 64 : CALL get_mo_set(mo_set, mo_coeff=mo_target)
560 : ! for mult > 1, put the polarized electrons in the alpha channel
561 64 : IF (nspins == 2) THEN
562 16 : IF (ispin == 1) THEN
563 8 : nel = (nelec_active + active_space_env%multiplicity - 1)/2
564 : ELSE
565 8 : nel = (nelec_active - active_space_env%multiplicity + 1)/2
566 : END IF
567 : ELSE
568 48 : nel = nelec_active
569 : END IF
570 64 : mo_set%nelectron = nel
571 64 : mo_set%n_el_f = REAL(nel, KIND=dp)
572 240 : DO i = 1, nmo_active
573 176 : m = active_space_env%active_orbitals(i, ispin)
574 176 : IF (m > nmo_available) THEN
575 0 : CALL cp_fm_to_fm(mo_virtual, mo_target, 1, m - nmo_available, m)
576 0 : eigenvalues(m, ispin) = evals_virtual(m - nmo_available)
577 0 : mo_set%occupation_numbers(m) = 0.0
578 : ELSE
579 176 : CALL cp_fm_to_fm(mo_ref, mo_target, 1, m, m)
580 176 : mo_set%occupation_numbers(m) = mos(ispin)%occupation_numbers(m)
581 : END IF
582 240 : mo_set%eigenvalues(m) = eigenvalues(m, ispin)
583 : END DO
584 : ! Release
585 64 : DEALLOCATE (evals_virtual)
586 64 : CALL cp_fm_release(fm_dummy)
587 376 : CALL cp_fm_release(mo_virtual)
588 : END DO
589 :
590 56 : IF (iw > 0) THEN
591 60 : DO ispin = 1, nspins
592 32 : WRITE (iw, '(/,T3,A,I3,T66,A)') "Canonical Orbital Selection for spin", ispin, &
593 64 : "[atomic units]"
594 40 : DO i = 1, nmo_inactive, 4
595 8 : jm = MIN(3, nmo_inactive - i)
596 60 : WRITE (iw, '(T3,4(F14.6,A5))') (eigenvalues(i + j, ispin), " [I]", j=0, jm)
597 : END DO
598 65 : DO i = nmo_inactive + 1, nmo_inactive + nmo_active, 4
599 33 : jm = MIN(3, nmo_inactive + nmo_active - i)
600 153 : WRITE (iw, '(T3,4(F14.6,A5))') (eigenvalues(i + j, ispin), " [A]", j=0, jm)
601 : END DO
602 32 : WRITE (iw, '(/,T3,A,I3)') "Active Orbital Indices for spin", ispin
603 93 : DO i = 1, SIZE(active_space_env%active_orbitals, 1), 4
604 33 : jm = MIN(3, SIZE(active_space_env%active_orbitals, 1) - i)
605 153 : WRITE (iw, '(T3,4(I4))') (active_space_env%active_orbitals(i + j, ispin), j=0, jm)
606 : END DO
607 : END DO
608 : END IF
609 56 : DEALLOCATE (eigenvalues)
610 : END IF
611 :
612 : CASE (manual_selection)
613 : ! create canonical orbitals
614 12 : IF (dft_control%roks) THEN
615 0 : CPABORT("Unclear how we define MOs in the restricted case ... stopping")
616 : ELSE
617 12 : IF (dft_control%do_admm) THEN
618 : ! For admm_mo, the auxiliary density is computed from the MOs, which never change
619 : ! in the rs-dft embedding, therefore the energy is wrong as the LR HFX never changes.
620 : ! For admm_dm, the auxiliary density is computed from the density matrix, which is
621 : ! updated at each iteration and therefore works.
622 0 : IF (dft_control%do_admm_mo) THEN
623 0 : CPABORT("ADMM currently possible only with purification none_dm")
624 : END IF
625 : END IF
626 :
627 12 : CALL section_vals_val_get(as_input, "ACTIVE_ORBITAL_INDICES", explicit=explicit, i_vals=invals)
628 12 : IF (.NOT. explicit) THEN
629 : CALL cp_abort(__LOCATION__, "Manual orbital selection requires to explicitly "// &
630 0 : "set the active orbital indices via ACTIVE_ORBITAL_INDICES")
631 : END IF
632 :
633 12 : IF (nspins == 1) THEN
634 6 : CPASSERT(SIZE(invals) == nmo_active)
635 : ELSE
636 6 : CPASSERT(SIZE(invals) == 2*nmo_active)
637 : END IF
638 36 : ALLOCATE (active_space_env%inactive_orbitals(nmo_inactive, nspins))
639 48 : ALLOCATE (active_space_env%active_orbitals(nmo_active, nspins))
640 :
641 30 : DO ispin = 1, nspins
642 66 : DO i = 1, nmo_active
643 54 : active_space_env%active_orbitals(i, ispin) = invals(i + (ispin - 1)*nmo_active)
644 : END DO
645 : END DO
646 :
647 12 : CALL get_qs_env(qs_env, mos=mos)
648 :
649 : ! include MOs up to the largest index in the list
650 48 : max_orb_ind = MAXVAL(invals)
651 12 : maxocc = 2.0_dp
652 12 : IF (nspins > 1) maxocc = 1.0_dp
653 54 : ALLOCATE (active_space_env%mos_active(nspins))
654 42 : ALLOCATE (active_space_env%mos_inactive(nspins))
655 30 : DO ispin = 1, nspins
656 : ! init active orbitals
657 18 : CALL get_mo_set(mos(ispin), mo_coeff=mo_ref, nao=nao)
658 18 : CALL cp_fm_get_info(mo_ref, context=context, para_env=para_env, nrow_global=nrow_global)
659 18 : CALL allocate_mo_set(active_space_env%mos_active(ispin), nao, max_orb_ind, 0, 0.0_dp, maxocc, 0.0_dp)
660 : CALL cp_fm_struct_create(fm_struct_tmp, para_env=para_env, context=context, &
661 18 : nrow_global=nrow_global, ncol_global=max_orb_ind)
662 18 : CALL init_mo_set(active_space_env%mos_active(ispin), fm_struct=fm_struct_tmp, name="Active Space MO")
663 18 : CALL cp_fm_struct_release(fm_struct_tmp)
664 :
665 : ! init inactive orbitals
666 18 : IF (nspins == 2) THEN
667 12 : nel = nelec_inactive/2
668 : ELSE
669 6 : nel = nelec_inactive
670 : END IF
671 18 : CALL allocate_mo_set(active_space_env%mos_inactive(ispin), nao, max_orb_ind, nel, REAL(nel, KIND=dp), maxocc, 0.0_dp)
672 : CALL cp_fm_struct_create(fm_struct_tmp, para_env=para_env, context=context, &
673 18 : nrow_global=nrow_global, ncol_global=max_orb_ind)
674 18 : CALL init_mo_set(active_space_env%mos_inactive(ispin), fm_struct=fm_struct_tmp, name="Inactive Space MO")
675 : ! small hack: set the correct inactive occupations down below
676 68 : active_space_env%mos_inactive(ispin)%occupation_numbers = 0.0_dp
677 48 : CALL cp_fm_struct_release(fm_struct_tmp)
678 : END DO
679 :
680 48 : ALLOCATE (eigenvalues(max_orb_ind, nspins))
681 80 : eigenvalues = 0.0_dp
682 12 : CALL get_qs_env(qs_env, matrix_ks=ks_matrix, matrix_s=s_matrix, scf_control=scf_control)
683 :
684 : ! calculate virtual MOs and copy inactive and active orbitals
685 12 : IF (iw > 0) THEN
686 6 : WRITE (iw, '(/,T3,A)') "Calculating virtual MOs..."
687 : END IF
688 30 : DO ispin = 1, nspins
689 18 : CALL get_mo_set(mos(ispin), mo_coeff=mo_ref, nmo=nmo_available)
690 18 : nmo_virtual = max_orb_ind - nmo_available
691 18 : nmo_virtual = MAX(nmo_virtual, 0)
692 :
693 : NULLIFY (evals_virtual)
694 36 : ALLOCATE (evals_virtual(nmo_virtual))
695 :
696 : CALL cp_fm_get_info(mo_ref, context=context, para_env=para_env, &
697 18 : nrow_global=nrow_global)
698 :
699 : CALL cp_fm_struct_create(fm_struct_tmp, para_env=para_env, context=context, &
700 18 : nrow_global=nrow_global, ncol_global=nmo_virtual)
701 18 : CALL cp_fm_create(mo_virtual, fm_struct_tmp, name="virtual")
702 18 : CALL cp_fm_struct_release(fm_struct_tmp)
703 18 : CALL cp_fm_init_random(mo_virtual, nmo_virtual)
704 :
705 18 : NULLIFY (local_preconditioner)
706 :
707 : CALL ot_eigensolver(matrix_h=ks_matrix(ispin)%matrix, matrix_s=s_matrix(1)%matrix, &
708 : matrix_c_fm=mo_virtual, matrix_orthogonal_space_fm=mo_ref, &
709 : eps_gradient=scf_control%eps_lumos, &
710 : preconditioner=local_preconditioner, &
711 : iter_max=scf_control%max_iter_lumos, &
712 18 : size_ortho_space=nmo_available)
713 :
714 : CALL calculate_subspace_eigenvalues(mo_virtual, ks_matrix(ispin)%matrix, &
715 18 : evals_virtual)
716 :
717 : ! We need to send the copy of MOs to preserve the sign
718 18 : CALL cp_fm_create(fm_dummy, mo_ref%matrix_struct)
719 18 : CALL cp_fm_to_fm(mo_ref, fm_dummy)
720 :
721 : CALL calculate_subspace_eigenvalues(fm_dummy, ks_matrix(ispin)%matrix, &
722 18 : evals_arg=eigenvalues(:, ispin), do_rotation=.TRUE.)
723 :
724 18 : mo_set_active => active_space_env%mos_active(ispin)
725 18 : CALL get_mo_set(mo_set_active, mo_coeff=fm_target_active)
726 18 : mo_set_inactive => active_space_env%mos_inactive(ispin)
727 18 : CALL get_mo_set(mo_set_inactive, mo_coeff=fm_target_inactive)
728 :
729 : ! copy orbitals
730 18 : nmo_inactive_remaining = nmo_inactive
731 68 : DO i = 1, max_orb_ind
732 : ! case for i being an active orbital
733 114 : IF (ANY(active_space_env%active_orbitals(:, ispin) == i)) THEN
734 36 : IF (i > nmo_available) THEN
735 0 : CALL cp_fm_to_fm(mo_virtual, fm_target_active, 1, i - nmo_available, i)
736 0 : eigenvalues(i, ispin) = evals_virtual(i - nmo_available)
737 0 : mo_set_active%occupation_numbers(i) = 0.0
738 : ELSE
739 36 : CALL cp_fm_to_fm(fm_dummy, fm_target_active, 1, i, i)
740 36 : mo_set_active%occupation_numbers(i) = mos(ispin)%occupation_numbers(i)
741 : END IF
742 36 : mo_set_active%eigenvalues(i) = eigenvalues(i, ispin)
743 : ! if it was not an active orbital, check whether it is an inactive orbital
744 14 : ELSEIF (nmo_inactive_remaining > 0) THEN
745 0 : CALL cp_fm_to_fm(fm_dummy, fm_target_inactive, 1, i, i)
746 : ! store on the fly the mapping of inactive orbitals
747 0 : active_space_env%inactive_orbitals(nmo_inactive - nmo_inactive_remaining + 1, ispin) = i
748 0 : mo_set_inactive%eigenvalues(i) = eigenvalues(i, ispin)
749 0 : mo_set_inactive%occupation_numbers(i) = mos(ispin)%occupation_numbers(i)
750 : ! hack: set homo and lumo manually
751 0 : IF (nmo_inactive_remaining == 1) THEN
752 0 : mo_set_inactive%homo = i
753 0 : mo_set_inactive%lfomo = i + 1
754 : END IF
755 0 : nmo_inactive_remaining = nmo_inactive_remaining - 1
756 : ELSE
757 14 : CYCLE
758 : END IF
759 : END DO
760 :
761 : ! Release
762 18 : DEALLOCATE (evals_virtual)
763 18 : CALL cp_fm_release(fm_dummy)
764 102 : CALL cp_fm_release(mo_virtual)
765 : END DO
766 :
767 12 : IF (iw > 0) THEN
768 15 : DO ispin = 1, nspins
769 9 : WRITE (iw, '(/,T3,A,I3,T66,A)') "Orbital Energies and Selection for spin", ispin, "[atomic units]"
770 :
771 18 : DO i = 1, max_orb_ind, 4
772 9 : jm = MIN(3, max_orb_ind - i)
773 9 : WRITE (iw, '(T4)', advance="no")
774 34 : DO j = 0, jm
775 57 : IF (ANY(active_space_env%active_orbitals(:, ispin) == i + j)) THEN
776 18 : WRITE (iw, '(T3,F12.6,A5)', advance="no") eigenvalues(i + j, ispin), " [A]"
777 7 : ELSEIF (ANY(active_space_env%inactive_orbitals(:, ispin) == i + j)) THEN
778 0 : WRITE (iw, '(T3,F12.6,A5)', advance="no") eigenvalues(i + j, ispin), " [I]"
779 : ELSE
780 7 : WRITE (iw, '(T3,F12.6,A5)', advance="no") eigenvalues(i + j, ispin), " [V]"
781 : END IF
782 : END DO
783 18 : WRITE (iw, *)
784 : END DO
785 9 : WRITE (iw, '(/,T3,A,I3)') "Active Orbital Indices for spin", ispin
786 24 : DO i = 1, SIZE(active_space_env%active_orbitals, 1), 4
787 9 : jm = MIN(3, SIZE(active_space_env%active_orbitals, 1) - i)
788 36 : WRITE (iw, '(T3,4(I4))') (active_space_env%active_orbitals(i + j, ispin), j=0, jm)
789 : END DO
790 : END DO
791 : END IF
792 24 : DEALLOCATE (eigenvalues)
793 : END IF
794 :
795 : CASE (wannier_projection)
796 0 : NULLIFY (loc_section, loc_print)
797 0 : loc_section => section_vals_get_subs_vals(as_input, "LOCALIZE")
798 0 : CPASSERT(ASSOCIATED(loc_section))
799 0 : loc_print => section_vals_get_subs_vals(as_input, "LOCALIZE%PRINT")
800 : !
801 0 : CPABORT("not yet available")
802 : !
803 : CASE (mao_projection)
804 : !
805 68 : CPABORT("not yet available")
806 : !
807 : END SELECT
808 :
809 : ! Print orbitals on Cube files
810 68 : print_orb => section_vals_get_subs_vals(as_input, "PRINT_ORBITAL_CUBES")
811 68 : CALL section_vals_get(print_orb, explicit=explicit)
812 68 : CALL section_vals_val_get(print_orb, "STOP_AFTER_CUBES", l_val=stop_after_print)
813 68 : IF (explicit) THEN
814 : !
815 2 : CALL print_orbital_cubes(print_orb, qs_env, active_space_env%mos_active)
816 : !
817 2 : IF (stop_after_print) THEN
818 :
819 0 : IF (iw > 0) THEN
820 : WRITE (iw, '(/,T2,A)') &
821 0 : '!----------------- Early End of Active Space Interface -----------------------!'
822 : END IF
823 :
824 0 : CALL timestop(handle)
825 :
826 0 : RETURN
827 : END IF
828 : END IF
829 :
830 : ! calculate inactive density matrix
831 68 : CALL get_qs_env(qs_env, rho=rho)
832 68 : CALL qs_rho_get(rho, rho_ao=rho_ao)
833 68 : CPASSERT(ASSOCIATED(rho_ao))
834 68 : CALL dbcsr_allocate_matrix_set(active_space_env%pmat_inactive, nspins)
835 150 : DO ispin = 1, nspins
836 82 : ALLOCATE (denmat)
837 82 : CALL dbcsr_copy(denmat, rho_ao(ispin)%matrix)
838 82 : mo_set => active_space_env%mos_inactive(ispin)
839 82 : CALL calculate_density_matrix(mo_set, denmat)
840 150 : active_space_env%pmat_inactive(ispin)%matrix => denmat
841 : END DO
842 :
843 : ! read in ERI parameters
844 68 : CALL section_vals_val_get(as_input, "ERI%METHOD", i_val=eri_method)
845 68 : active_space_env%eri%method = eri_method
846 68 : CALL section_vals_val_get(as_input, "ERI%OPERATOR", i_val=eri_operator, explicit=ex_operator)
847 68 : active_space_env%eri%operator = eri_operator
848 68 : CALL section_vals_val_get(as_input, "ERI%OMEGA", r_val=eri_op_omega, explicit=ex_omega)
849 68 : active_space_env%eri%omega = eri_op_omega
850 68 : CALL section_vals_val_get(as_input, "ERI%CUTOFF_RADIUS", r_val=eri_rcut, explicit=ex_rcut)
851 68 : active_space_env%eri%cutoff_radius = eri_rcut ! this is already converted to bohr!
852 68 : CALL section_vals_val_get(as_input, "ERI%PERIODICITY", i_vals=invals, explicit=ex_perd)
853 68 : CALL section_vals_val_get(as_input, "ERI%EPS_INTEGRAL", r_val=eri_eps_int)
854 68 : active_space_env%eri%eps_integral = eri_eps_int
855 : ! if eri periodicity is explicitly set, we use it, otherwise we use the cell periodicity
856 68 : IF (ex_perd) THEN
857 64 : IF (SIZE(invals) == 1) THEN
858 0 : active_space_env%eri%periodicity(1:3) = invals(1)
859 : ELSE
860 448 : active_space_env%eri%periodicity(1:3) = invals(1:3)
861 : END IF
862 : ELSE
863 4 : CALL get_qs_env(qs_env, cell=cell)
864 28 : active_space_env%eri%periodicity(1:3) = cell%perd(1:3)
865 : END IF
866 68 : IF (iw > 0) THEN
867 34 : WRITE (iw, '(/,T3,A)') "Calculation of Electron Repulsion Integrals"
868 :
869 27 : SELECT CASE (eri_method)
870 : CASE (eri_method_full_gpw)
871 27 : WRITE (iw, '(T3,A,T50,A)') "Integration method", "GPW Fourier transform over MOs"
872 : CASE (eri_method_gpw_ht)
873 7 : WRITE (iw, '(T3,A,T44,A)') "Integration method", "Half transformed integrals from GPW"
874 : CASE DEFAULT
875 34 : CPABORT("Unknown ERI method")
876 : END SELECT
877 :
878 25 : SELECT CASE (eri_operator)
879 : CASE (eri_operator_coulomb)
880 25 : WRITE (iw, '(T3,A,T73,A)') "ERI operator", "Coulomb"
881 :
882 : CASE (eri_operator_yukawa)
883 0 : WRITE (iw, '(T3,A,T74,A)') "ERI operator", "Yukawa"
884 0 : IF (.NOT. ex_omega) CALL cp_abort(__LOCATION__, &
885 0 : "Yukawa operator requires OMEGA to be explicitly set")
886 0 : WRITE (iw, '(T3,A,T66,F14.3)') "ERI operator parameter OMEGA", eri_op_omega
887 :
888 : CASE (eri_operator_erf)
889 7 : WRITE (iw, '(T3,A,T63,A)') "ERI operator", "Longrange Coulomb"
890 7 : IF (.NOT. ex_omega) CALL cp_abort(__LOCATION__, &
891 0 : "Longrange operator requires OMEGA to be explicitly set")
892 7 : WRITE (iw, '(T3,A,T66,F14.3)') "ERI operator parameter OMEGA", eri_op_omega
893 :
894 : CASE (eri_operator_erfc)
895 0 : WRITE (iw, '(T3,A,T62,A)') "ERI operator", "Shortrange Coulomb"
896 0 : IF (.NOT. ex_omega) CALL cp_abort(__LOCATION__, &
897 0 : "Shortrange operator requires OMEGA to be explicitly set")
898 0 : WRITE (iw, '(T3,A,T66,F14.3)') "ERI operator parameter OMEGA", eri_op_omega
899 :
900 : CASE (eri_operator_trunc)
901 0 : WRITE (iw, '(T3,A,T63,A)') "ERI operator", "Truncated Coulomb"
902 0 : IF (.NOT. ex_rcut) CALL cp_abort(__LOCATION__, &
903 0 : "Cutoff radius not specified for trunc. Coulomb operator")
904 0 : WRITE (iw, '(T3,A,T66,F14.3)') "ERI operator cutoff radius (au)", eri_rcut
905 :
906 : CASE (eri_operator_lr_trunc)
907 2 : WRITE (iw, '(T3,A,T53,A)') "ERI operator", "Longrange truncated Coulomb"
908 2 : IF (.NOT. ex_rcut) CALL cp_abort(__LOCATION__, &
909 0 : "Cutoff radius not specified for trunc. longrange operator")
910 2 : WRITE (iw, '(T3,A,T66,F14.3)') "ERI operator cutoff radius (au)", eri_rcut
911 2 : IF (.NOT. ex_omega) CALL cp_abort(__LOCATION__, &
912 0 : "LR truncated operator requires OMEGA to be explicitly set")
913 2 : WRITE (iw, '(T3,A,T66,F14.3)') "ERI operator parameter OMEGA", eri_op_omega
914 2 : IF (eri_op_omega < 0.01_dp) THEN
915 0 : CPABORT("LR truncated operator requires OMEGA >= 0.01 to be stable")
916 : END IF
917 :
918 : CASE DEFAULT
919 34 : CPABORT("Unknown ERI operator")
920 :
921 : END SELECT
922 :
923 34 : WRITE (iw, '(T3,A,T68,E12.4)') "Accuracy of ERIs", eri_eps_int
924 34 : WRITE (iw, '(T3,A,T71,3I3)') "Periodicity", active_space_env%eri%periodicity(1:3)
925 :
926 : ! TODO: should be moved after ERI calculation, as it depends on screening
927 34 : IF (nspins < 2) THEN
928 27 : WRITE (iw, '(T3,A,T68,I12)') "Total Number of ERI", (nmo_active**4)/8
929 : ELSE
930 7 : WRITE (iw, '(T3,A,T68,I12)') "Total Number of ERI (aa|aa)", (nmo_active**4)/8
931 7 : WRITE (iw, '(T3,A,T68,I12)') "Total Number of ERI (bb|bb)", (nmo_active**4)/8
932 7 : WRITE (iw, '(T3,A,T68,I12)') "Total Number of ERI (aa|bb)", (nmo_active**4)/4
933 : END IF
934 : END IF
935 :
936 : ! allocate container for integrals (CSR matrix)
937 68 : CALL get_qs_env(qs_env, para_env=para_env)
938 68 : m = (nspins*(nspins + 1))/2
939 : ! With ROHF/ROKS, we need ERIs from only a single set of orbitals
940 68 : IF (dft_control%roks) m = 1
941 296 : ALLOCATE (active_space_env%eri%eri(m))
942 160 : DO i = 1, m
943 92 : CALL get_mo_set(active_space_env%mos_active(1), nmo=nmo)
944 92 : ALLOCATE (active_space_env%eri%eri(i)%csr_mat)
945 92 : eri_mat => active_space_env%eri%eri(i)%csr_mat
946 92 : IF (i == 1) THEN
947 68 : n1 = nmo
948 68 : n2 = nmo
949 24 : ELSEIF (i == 2) THEN
950 12 : n1 = nmo
951 12 : n2 = nmo
952 : ELSE
953 12 : n1 = nmo
954 12 : n2 = nmo
955 : END IF
956 92 : nn1 = (n1*(n1 + 1))/2
957 92 : nn2 = (n2*(n2 + 1))/2
958 92 : CALL dbcsr_csr_create(eri_mat, nn1, nn2, 0_int_8, 0, 0, para_env%get_handle())
959 252 : active_space_env%eri%norb = nmo
960 : END DO
961 :
962 68 : SELECT CASE (eri_method)
963 : CASE (eri_method_full_gpw, eri_method_gpw_ht)
964 68 : CALL section_vals_val_get(as_input, "ERI_GPW%EPS_GRID", r_val=eri_eps_grid)
965 68 : active_space_env%eri%eri_gpw%eps_grid = eri_eps_grid
966 68 : CALL section_vals_val_get(as_input, "ERI_GPW%EPS_FILTER", r_val=eri_eps_filter)
967 68 : active_space_env%eri%eri_gpw%eps_filter = eri_eps_filter
968 68 : CALL section_vals_val_get(as_input, "ERI_GPW%CUTOFF", r_val=eri_gpw_cutoff)
969 68 : active_space_env%eri%eri_gpw%cutoff = eri_gpw_cutoff
970 68 : CALL section_vals_val_get(as_input, "ERI_GPW%REL_CUTOFF", r_val=eri_rel_cutoff)
971 68 : active_space_env%eri%eri_gpw%rel_cutoff = eri_rel_cutoff
972 68 : CALL section_vals_val_get(as_input, "ERI_GPW%PRINT_LEVEL", i_val=eri_print)
973 68 : active_space_env%eri%eri_gpw%print_level = eri_print
974 68 : CALL section_vals_val_get(as_input, "ERI_GPW%STORE_WFN", l_val=store_wfn)
975 68 : active_space_env%eri%eri_gpw%store_wfn = store_wfn
976 68 : CALL section_vals_val_get(as_input, "ERI_GPW%GROUP_SIZE", i_val=group_size)
977 68 : active_space_env%eri%eri_gpw%group_size = group_size
978 : ! Always redo Poisson solver for now
979 68 : active_space_env%eri%eri_gpw%redo_poisson = .TRUE.
980 : ! active_space_env%eri%eri_gpw%redo_poisson = (ex_operator .OR. ex_perd)
981 68 : IF (iw > 0) THEN
982 34 : WRITE (iw, '(/,T2,A,T71,F10.1)') "ERI_GPW| Energy cutoff [Ry]", eri_gpw_cutoff
983 34 : WRITE (iw, '(T2,A,T71,F10.1)') "ERI_GPW| Relative energy cutoff [Ry]", eri_rel_cutoff
984 : END IF
985 : !
986 : CALL calculate_eri_gpw(active_space_env%mos_active, active_space_env%active_orbitals, active_space_env%eri, qs_env, iw, &
987 68 : dft_control%roks)
988 : !
989 : CASE DEFAULT
990 68 : CPABORT("Unknown ERI method")
991 : END SELECT
992 68 : IF (iw > 0) THEN
993 80 : DO isp = 1, SIZE(active_space_env%eri%eri)
994 46 : eri_mat => active_space_env%eri%eri(isp)%csr_mat
995 : nze_percentage = 100.0_dp*(REAL(eri_mat%nze_total, KIND=dp) &
996 46 : /REAL(eri_mat%nrows_total, KIND=dp))/REAL(eri_mat%ncols_total, KIND=dp)
997 46 : WRITE (iw, '(/,T2,A,I2,T30,A,T68,I12)') "ERI_GPW| Spinmatrix:", isp, &
998 92 : "Number of CSR non-zero elements:", eri_mat%nze_total
999 46 : WRITE (iw, '(T2,A,I2,T30,A,T68,F12.4)') "ERI_GPW| Spinmatrix:", isp, &
1000 92 : "Percentage CSR non-zero elements:", nze_percentage
1001 46 : WRITE (iw, '(T2,A,I2,T30,A,T68,I12)') "ERI_GPW| Spinmatrix:", isp, &
1002 92 : "nrows_total", eri_mat%nrows_total
1003 46 : WRITE (iw, '(T2,A,I2,T30,A,T68,I12)') "ERI_GPW| Spinmatrix:", isp, &
1004 92 : "ncols_total", eri_mat%ncols_total
1005 46 : WRITE (iw, '(T2,A,I2,T30,A,T68,I12)') "ERI_GPW| Spinmatrix:", isp, &
1006 126 : "nrows_local", eri_mat%nrows_local
1007 : END DO
1008 34 : CALL m_flush(iw)
1009 : END IF
1010 68 : CALL para_env%sync()
1011 :
1012 : ! set the reference active space density matrix
1013 68 : nspins = active_space_env%nspins
1014 286 : ALLOCATE (active_space_env%p_active(nspins))
1015 150 : DO isp = 1, nspins
1016 82 : mo_set => active_space_env%mos_active(isp)
1017 82 : CALL get_mo_set(mo_set, mo_coeff=mo_coeff, nmo=nmo)
1018 150 : CALL create_subspace_matrix(mo_coeff, active_space_env%p_active(isp), nmo)
1019 : END DO
1020 0 : SELECT CASE (mselect)
1021 : CASE DEFAULT
1022 0 : CPABORT("Unknown orbital selection method")
1023 : CASE (casci_canonical, manual_selection)
1024 68 : focc = 2.0_dp
1025 68 : IF (nspins == 2) focc = 1.0_dp
1026 150 : DO isp = 1, nspins
1027 82 : fmat => active_space_env%p_active(isp)
1028 82 : CALL cp_fm_set_all(fmat, alpha=0.0_dp)
1029 82 : IF (nspins == 2) THEN
1030 28 : IF (isp == 1) THEN
1031 14 : nel = (active_space_env%nelec_active + active_space_env%multiplicity - 1)/2
1032 : ELSE
1033 14 : nel = (active_space_env%nelec_active - active_space_env%multiplicity + 1)/2
1034 : END IF
1035 : ELSE
1036 54 : nel = active_space_env%nelec_active
1037 : END IF
1038 362 : DO i = 1, nmo_active
1039 212 : m = active_space_env%active_orbitals(i, isp)
1040 212 : fel = MIN(focc, REAL(nel, KIND=dp))
1041 212 : CALL cp_fm_set_element(fmat, m, m, fel)
1042 212 : nel = nel - NINT(fel)
1043 294 : nel = MAX(nel, 0)
1044 : END DO
1045 : END DO
1046 : CASE (wannier_projection)
1047 0 : CPABORT("NOT IMPLEMENTED")
1048 : CASE (mao_projection)
1049 68 : CPABORT("NOT IMPLEMENTED")
1050 : END SELECT
1051 :
1052 : ! compute alpha-beta overlap matrix in case of spin-polarized calculation
1053 68 : CALL calculate_spin_pol_overlap(active_space_env%mos_active, qs_env, active_space_env)
1054 :
1055 : ! figure out if we have a new xc section for the AS
1056 68 : xc_section => section_vals_get_subs_vals(input, "DFT%ACTIVE_SPACE%XC")
1057 68 : explicit = .FALSE.
1058 68 : IF (ASSOCIATED(xc_section)) CALL section_vals_get(xc_section, explicit=explicit)
1059 :
1060 : ! rebuild KS matrix if needed
1061 68 : IF (explicit) THEN
1062 : ! release the hfx data if it was part of the SCF functional
1063 2 : IF (ASSOCIATED(qs_env%x_data)) CALL hfx_release(qs_env%x_data)
1064 : ! also release the admm environment in case we are using admm
1065 2 : IF (ASSOCIATED(qs_env%admm_env)) CALL admm_env_release(qs_env%admm_env)
1066 :
1067 : CALL get_qs_env(qs_env, atomic_kind_set=atomic_kind_set, qs_kind_set=qs_kind_set, &
1068 2 : particle_set=particle_set, cell=cell, ks_env=ks_env)
1069 2 : IF (dft_control%do_admm) THEN
1070 0 : basis_type = 'AUX_FIT'
1071 : ELSE
1072 2 : basis_type = 'ORB'
1073 : END IF
1074 2 : hfx_section => section_vals_get_subs_vals(xc_section, "HF")
1075 : CALL hfx_create(qs_env%x_data, para_env, hfx_section, atomic_kind_set, &
1076 : qs_kind_set, particle_set, dft_control, cell, orb_basis=basis_type, &
1077 2 : nelectron_total=nelec_total)
1078 :
1079 2 : qs_env%requires_matrix_vxc = .TRUE. ! needs to be set only once
1080 :
1081 : ! a bit of a hack: this forces a new re-init of HFX
1082 2 : CALL set_ks_env(ks_env, s_mstruct_changed=.TRUE.)
1083 : CALL qs_ks_build_kohn_sham_matrix(qs_env, calculate_forces=.FALSE., &
1084 : just_energy=.FALSE., &
1085 2 : ext_xc_section=xc_section)
1086 : ! we need to reset it to false
1087 2 : CALL set_ks_env(ks_env, s_mstruct_changed=.FALSE.)
1088 : ELSE
1089 66 : xc_section => section_vals_get_subs_vals(input, "DFT%XC")
1090 : END IF
1091 : ! set the xc_section
1092 68 : active_space_env%xc_section => xc_section
1093 :
1094 68 : CALL get_qs_env(qs_env, energy=energy)
1095 : ! transform KS/Fock, Vxc and Hcore to AS MO basis
1096 68 : CALL calculate_operators(active_space_env%mos_active, qs_env, active_space_env)
1097 : ! set the reference energy in the active space
1098 68 : active_space_env%energy_ref = energy%total
1099 : ! calculate inactive energy and embedding potential
1100 68 : CALL subspace_fock_matrix(active_space_env, dft_control%roks)
1101 :
1102 : ! associate the active space environment with the qs environment
1103 68 : CALL set_qs_env(qs_env, active_space=active_space_env)
1104 :
1105 : ! Perform the embedding calculation only if qiskit is specified
1106 68 : CALL section_vals_val_get(as_input, "AS_SOLVER", i_val=as_solver)
1107 68 : SELECT CASE (as_solver)
1108 : CASE (no_solver)
1109 68 : IF (iw > 0) THEN
1110 34 : WRITE (iw, '(/,T3,A)') "No active space solver specified, skipping embedding calculation"
1111 34 : CALL m_flush(iw)
1112 : END IF
1113 68 : CALL para_env%sync()
1114 : CASE (qiskit_solver)
1115 0 : CALL rsdft_embedding(qs_env, active_space_env, as_input)
1116 0 : CALL qs_scf_compute_properties(qs_env, wf_type="MC-DFT", do_mp2=.FALSE.)
1117 : CASE DEFAULT
1118 68 : CPABORT("Unknown active space solver")
1119 : END SELECT
1120 :
1121 : ! Output a FCIDUMP file if requested
1122 68 : IF (active_space_env%fcidump) CALL fcidump(active_space_env, as_input, dft_control%roks)
1123 :
1124 : ! Output a QCSchema file if requested
1125 68 : IF (active_space_env%qcschema) THEN
1126 0 : CALL qcschema_env_create(qcschema_env, qs_env)
1127 0 : CALL qcschema_to_hdf5(qcschema_env, active_space_env%qcschema_filename)
1128 0 : CALL qcschema_env_release(qcschema_env)
1129 : END IF
1130 :
1131 68 : IF (iw > 0) THEN
1132 : WRITE (iw, '(/,T2,A)') &
1133 34 : '!-------------------- End of Active Space Interface --------------------------!'
1134 34 : CALL m_flush(iw)
1135 : END IF
1136 68 : CALL para_env%sync()
1137 :
1138 68 : CALL timestop(handle)
1139 :
1140 90980 : END SUBROUTINE active_space_main
1141 :
1142 : ! **************************************************************************************************
1143 : !> \brief computes the alpha-beta overlap within the active subspace
1144 : !> \param mos the molecular orbital set within the active subspace
1145 : !> \param qs_env ...
1146 : !> \param active_space_env ...
1147 : !> \par History
1148 : !> 04.2016 created [JGH]
1149 : ! **************************************************************************************************
1150 68 : SUBROUTINE calculate_spin_pol_overlap(mos, qs_env, active_space_env)
1151 :
1152 : TYPE(mo_set_type), DIMENSION(:), INTENT(IN) :: mos
1153 : TYPE(qs_environment_type), POINTER :: qs_env
1154 : TYPE(active_space_type), POINTER :: active_space_env
1155 :
1156 : CHARACTER(len=*), PARAMETER :: routineN = 'calculate_spin_pol_overlap'
1157 :
1158 : INTEGER :: handle, nmo, nspins
1159 : TYPE(cp_fm_type), POINTER :: mo_coeff_a, mo_coeff_b
1160 68 : TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: s_matrix
1161 :
1162 68 : CALL timeset(routineN, handle)
1163 :
1164 68 : nspins = active_space_env%nspins
1165 :
1166 : ! overlap in AO
1167 68 : IF (nspins > 1) THEN
1168 14 : CALL get_qs_env(qs_env, matrix_s=s_matrix)
1169 28 : ALLOCATE (active_space_env%sab_sub(1))
1170 :
1171 14 : CALL get_mo_set(mo_set=mos(1), mo_coeff=mo_coeff_a, nmo=nmo)
1172 14 : CALL get_mo_set(mo_set=mos(2), mo_coeff=mo_coeff_b, nmo=nmo)
1173 14 : CALL subspace_operator(mo_coeff_a, nmo, s_matrix(1)%matrix, active_space_env%sab_sub(1), mo_coeff_b)
1174 : END IF
1175 :
1176 68 : CALL timestop(handle)
1177 :
1178 68 : END SUBROUTINE calculate_spin_pol_overlap
1179 :
1180 : ! **************************************************************************************************
1181 : !> \brief computes the one-electron operators in the subspace of the provided orbital set
1182 : !> \param mos the molecular orbital set within the active subspace
1183 : !> \param qs_env ...
1184 : !> \param active_space_env ...
1185 : !> \par History
1186 : !> 04.2016 created [JGH]
1187 : ! **************************************************************************************************
1188 68 : SUBROUTINE calculate_operators(mos, qs_env, active_space_env)
1189 :
1190 : TYPE(mo_set_type), DIMENSION(:), INTENT(IN) :: mos
1191 : TYPE(qs_environment_type), POINTER :: qs_env
1192 : TYPE(active_space_type), POINTER :: active_space_env
1193 :
1194 : CHARACTER(len=*), PARAMETER :: routineN = 'calculate_operators'
1195 :
1196 : INTEGER :: handle, ispin, nmo, nspins
1197 : TYPE(cp_fm_type), POINTER :: mo_coeff
1198 68 : TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: h_matrix, ks_matrix
1199 :
1200 68 : CALL timeset(routineN, handle)
1201 :
1202 68 : nspins = active_space_env%nspins
1203 :
1204 : ! Kohn-Sham / Fock operator
1205 68 : CALL cp_fm_release(active_space_env%ks_sub)
1206 68 : CALL get_qs_env(qs_env, matrix_ks_kp=ks_matrix)
1207 286 : ALLOCATE (active_space_env%ks_sub(nspins))
1208 150 : DO ispin = 1, nspins
1209 82 : CALL get_mo_set(mo_set=mos(ispin), mo_coeff=mo_coeff, nmo=nmo)
1210 150 : CALL subspace_operator(mo_coeff, nmo, ks_matrix(ispin, 1)%matrix, active_space_env%ks_sub(ispin))
1211 : END DO
1212 :
1213 : ! Core Hamiltonian
1214 68 : CALL cp_fm_release(active_space_env%h_sub)
1215 :
1216 68 : NULLIFY (h_matrix)
1217 68 : CALL get_qs_env(qs_env=qs_env, matrix_h_kp=h_matrix)
1218 286 : ALLOCATE (active_space_env%h_sub(nspins))
1219 150 : DO ispin = 1, nspins
1220 82 : CALL get_mo_set(mo_set=mos(ispin), mo_coeff=mo_coeff, nmo=nmo)
1221 150 : CALL subspace_operator(mo_coeff, nmo, h_matrix(1, 1)%matrix, active_space_env%h_sub(ispin))
1222 : END DO
1223 :
1224 68 : CALL timestop(handle)
1225 :
1226 68 : END SUBROUTINE calculate_operators
1227 :
1228 : ! **************************************************************************************************
1229 : !> \brief computes a one-electron operator in the subspace of the provided orbital set
1230 : !> \param mo_coeff the orbital coefficient matrix
1231 : !> \param nmo the number of subspace orbitals
1232 : !> \param op_matrix operator matrix in AO basis
1233 : !> \param op_sub operator in orbital basis
1234 : !> \param mo_coeff_b the beta orbital coefficients
1235 : !> \par History
1236 : !> 04.2016 created [JGH]
1237 : ! **************************************************************************************************
1238 356 : SUBROUTINE subspace_operator(mo_coeff, nmo, op_matrix, op_sub, mo_coeff_b)
1239 :
1240 : TYPE(cp_fm_type), INTENT(IN) :: mo_coeff
1241 : INTEGER, INTENT(IN) :: nmo
1242 : TYPE(dbcsr_type), POINTER :: op_matrix
1243 : TYPE(cp_fm_type), INTENT(INOUT) :: op_sub
1244 : TYPE(cp_fm_type), INTENT(IN), OPTIONAL :: mo_coeff_b
1245 :
1246 : CHARACTER(len=*), PARAMETER :: routineN = 'subspace_operator'
1247 :
1248 : INTEGER :: handle, ncol, nrow
1249 : TYPE(cp_fm_type) :: vectors
1250 :
1251 178 : CALL timeset(routineN, handle)
1252 :
1253 178 : CALL cp_fm_get_info(matrix=mo_coeff, ncol_global=ncol, nrow_global=nrow)
1254 178 : CPASSERT(nmo <= ncol)
1255 :
1256 178 : IF (nmo > 0) THEN
1257 178 : CALL cp_fm_create(vectors, mo_coeff%matrix_struct, "vectors")
1258 178 : CALL create_subspace_matrix(mo_coeff, op_sub, nmo)
1259 :
1260 178 : IF (PRESENT(mo_coeff_b)) THEN
1261 : ! if beta orbitals are present, compute the cross alpha_beta term
1262 14 : CALL cp_dbcsr_sm_fm_multiply(op_matrix, mo_coeff_b, vectors, nmo)
1263 : ELSE
1264 : ! otherwise the same spin, whatever that is
1265 164 : CALL cp_dbcsr_sm_fm_multiply(op_matrix, mo_coeff, vectors, nmo)
1266 : END IF
1267 :
1268 178 : CALL parallel_gemm('T', 'N', nmo, nmo, nrow, 1.0_dp, mo_coeff, vectors, 0.0_dp, op_sub)
1269 178 : CALL cp_fm_release(vectors)
1270 : END IF
1271 :
1272 178 : CALL timestop(handle)
1273 :
1274 178 : END SUBROUTINE subspace_operator
1275 :
1276 : ! **************************************************************************************************
1277 : !> \brief creates a matrix of subspace size
1278 : !> \param orbitals the orbital coefficient matrix
1279 : !> \param op_sub operator in orbital basis
1280 : !> \param n the number of orbitals
1281 : !> \par History
1282 : !> 04.2016 created [JGH]
1283 : ! **************************************************************************************************
1284 260 : SUBROUTINE create_subspace_matrix(orbitals, op_sub, n)
1285 :
1286 : TYPE(cp_fm_type), INTENT(IN) :: orbitals
1287 : TYPE(cp_fm_type), INTENT(OUT) :: op_sub
1288 : INTEGER, INTENT(IN) :: n
1289 :
1290 : TYPE(cp_fm_struct_type), POINTER :: fm_struct
1291 :
1292 260 : IF (n > 0) THEN
1293 :
1294 260 : NULLIFY (fm_struct)
1295 : CALL cp_fm_struct_create(fm_struct, nrow_global=n, ncol_global=n, &
1296 : para_env=orbitals%matrix_struct%para_env, &
1297 260 : context=orbitals%matrix_struct%context)
1298 260 : CALL cp_fm_create(op_sub, fm_struct, name="Subspace operator")
1299 260 : CALL cp_fm_struct_release(fm_struct)
1300 :
1301 : END IF
1302 :
1303 260 : END SUBROUTINE create_subspace_matrix
1304 :
1305 : ! **************************************************************************************************
1306 : !> \brief computes the electron repulsion integrals using the GPW technology
1307 : !> \param mos the molecular orbital set within the active subspace
1308 : !> \param orbitals ...
1309 : !> \param eri_env ...
1310 : !> \param qs_env ...
1311 : !> \param iw ...
1312 : !> \param restricted ...
1313 : !> \par History
1314 : !> 04.2016 created [JGH]
1315 : ! **************************************************************************************************
1316 68 : SUBROUTINE calculate_eri_gpw(mos, orbitals, eri_env, qs_env, iw, restricted)
1317 :
1318 : TYPE(mo_set_type), DIMENSION(:), INTENT(IN) :: mos
1319 : INTEGER, DIMENSION(:, :), POINTER :: orbitals
1320 : TYPE(eri_type) :: eri_env
1321 : TYPE(qs_environment_type), POINTER :: qs_env
1322 : INTEGER, INTENT(IN) :: iw
1323 : LOGICAL, INTENT(IN) :: restricted
1324 :
1325 : CHARACTER(len=*), PARAMETER :: routineN = 'calculate_eri_gpw'
1326 :
1327 : INTEGER :: col_local, color, handle, i1, i2, i3, i4, i_multigrid, icount2, intcount, &
1328 : irange(2), isp, isp1, isp2, ispin, iwa1, iwa12, iwa2, iwb1, iwb12, iwb2, iwbs, iwbt, &
1329 : iwfn, n_multigrid, ncol_global, ncol_local, nmm, nmo, nmo1, nmo2, nrow_global, &
1330 : nrow_local, nspins, number_of_subgroups, nx, row_local, stored_integrals
1331 68 : INTEGER, ALLOCATABLE, DIMENSION(:) :: eri_index
1332 68 : INTEGER, DIMENSION(:), POINTER :: col_indices, row_indices
1333 : LOGICAL :: print1, print2, &
1334 : skip_load_balance_distributed
1335 : REAL(KIND=dp) :: dvol, erint, pair_int, &
1336 : progression_factor, rc, rsize, t1, t2
1337 68 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: eri
1338 68 : TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
1339 : TYPE(cell_type), POINTER :: cell
1340 : TYPE(cp_blacs_env_type), POINTER :: blacs_env, blacs_env_sub
1341 : TYPE(cp_fm_struct_type), POINTER :: fm_struct
1342 68 : TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:) :: fm_matrix_pq_rnu, fm_matrix_pq_rs, &
1343 68 : fm_mo_coeff_as
1344 : TYPE(cp_fm_type), POINTER :: mo_coeff
1345 : TYPE(dbcsr_p_type) :: mat_munu
1346 68 : TYPE(dbcsr_type), ALLOCATABLE, DIMENSION(:) :: matrix_pq_rnu, mo_coeff_as
1347 : TYPE(dft_control_type), POINTER :: dft_control
1348 : TYPE(mp_para_env_type), POINTER :: para_env
1349 : TYPE(neighbor_list_set_p_type), DIMENSION(:), &
1350 68 : POINTER :: sab_orb_sub
1351 68 : TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
1352 : TYPE(pw_c1d_gs_type) :: pot_g, rho_g
1353 : TYPE(pw_env_type), POINTER :: pw_env_sub
1354 : TYPE(pw_poisson_type), POINTER :: poisson_env
1355 : TYPE(pw_pool_type), POINTER :: auxbas_pw_pool
1356 : TYPE(pw_r3d_rs_type) :: rho_r, wfn_r
1357 : TYPE(pw_r3d_rs_type), ALLOCATABLE, &
1358 68 : DIMENSION(:, :), TARGET :: wfn_a
1359 : TYPE(pw_r3d_rs_type), POINTER :: wfn1, wfn2, wfn3, wfn4
1360 : TYPE(qs_control_type), POINTER :: qs_control, qs_control_old
1361 68 : TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
1362 : TYPE(qs_ks_env_type), POINTER :: ks_env
1363 : TYPE(task_list_type), POINTER :: task_list_sub
1364 :
1365 68 : CALL timeset(routineN, handle)
1366 :
1367 68 : IF (iw > 0) t1 = m_walltime()
1368 :
1369 : ! print levels
1370 132 : SELECT CASE (eri_env%eri_gpw%print_level)
1371 : CASE (silent_print_level)
1372 64 : print1 = .FALSE.
1373 64 : print2 = .FALSE.
1374 : CASE (low_print_level)
1375 2 : print1 = .FALSE.
1376 2 : print2 = .FALSE.
1377 : CASE (medium_print_level)
1378 2 : print1 = .TRUE.
1379 2 : print2 = .FALSE.
1380 : CASE (high_print_level)
1381 0 : print1 = .TRUE.
1382 0 : print2 = .TRUE.
1383 : CASE (debug_print_level)
1384 0 : print1 = .TRUE.
1385 68 : print2 = .TRUE.
1386 : CASE DEFAULT
1387 : ! do nothing
1388 : END SELECT
1389 :
1390 : ! Check the input group
1391 68 : CALL get_qs_env(qs_env, para_env=para_env, blacs_env=blacs_env)
1392 68 : IF (eri_env%eri_gpw%group_size < 1) eri_env%eri_gpw%group_size = para_env%num_pe
1393 68 : IF (MOD(para_env%num_pe, eri_env%eri_gpw%group_size) /= 0) &
1394 0 : CPABORT("Group size must be a divisor of the total number of processes!")
1395 : ! Create a new para_env or reuse the old one
1396 68 : IF (eri_env%eri_gpw%group_size == para_env%num_pe) THEN
1397 62 : eri_env%para_env_sub => para_env
1398 62 : CALL eri_env%para_env_sub%retain()
1399 62 : blacs_env_sub => blacs_env
1400 62 : CALL blacs_env_sub%retain()
1401 62 : number_of_subgroups = 1
1402 62 : color = 0
1403 : ELSE
1404 6 : number_of_subgroups = para_env%num_pe/eri_env%eri_gpw%group_size
1405 6 : color = para_env%mepos/eri_env%eri_gpw%group_size
1406 6 : ALLOCATE (eri_env%para_env_sub)
1407 6 : CALL eri_env%para_env_sub%from_split(para_env, color)
1408 6 : NULLIFY (blacs_env_sub)
1409 6 : CALL cp_blacs_env_create(blacs_env_sub, eri_env%para_env_sub, BLACS_GRID_SQUARE, .TRUE.)
1410 : END IF
1411 68 : CALL eri_env%comm_exchange%from_split(para_env, eri_env%para_env_sub%mepos)
1412 :
1413 : ! This should be done differently! Copied from MP2 code
1414 68 : CALL get_qs_env(qs_env, dft_control=dft_control)
1415 272 : ALLOCATE (qs_control)
1416 68 : qs_control_old => dft_control%qs_control
1417 68 : qs_control = qs_control_old
1418 68 : dft_control%qs_control => qs_control
1419 68 : progression_factor = qs_control%progression_factor
1420 68 : n_multigrid = SIZE(qs_control%e_cutoff)
1421 68 : nspins = SIZE(mos)
1422 : ! In case of ROHF/ROKS, we assume the orbital coefficients in both spin channels to be the same
1423 : ! and save operations by calculating ERIs from only one spin channel
1424 68 : IF (restricted) nspins = 1
1425 : ! Allocate new cutoffs (just in private qs_control, not in qs_control_old)
1426 204 : ALLOCATE (qs_control%e_cutoff(n_multigrid))
1427 :
1428 68 : qs_control%cutoff = eri_env%eri_gpw%cutoff*0.5_dp
1429 68 : qs_control%e_cutoff(1) = qs_control%cutoff
1430 272 : DO i_multigrid = 2, n_multigrid
1431 : qs_control%e_cutoff(i_multigrid) = qs_control%e_cutoff(i_multigrid - 1) &
1432 272 : /progression_factor
1433 : END DO
1434 68 : qs_control%relative_cutoff = eri_env%eri_gpw%rel_cutoff*0.5_dp
1435 :
1436 : ! For now, we will distribute neighbor lists etc. within the global communicator
1437 68 : CALL get_qs_env(qs_env, ks_env=ks_env)
1438 : CALL create_mat_munu(mat_munu, qs_env, eri_env%eri_gpw%eps_grid, blacs_env_sub, sab_orb_sub=sab_orb_sub, &
1439 68 : do_alloc_blocks_from_nbl=.TRUE., dbcsr_sym_type=dbcsr_type_symmetric)
1440 68 : CALL dbcsr_set(mat_munu%matrix, 0.0_dp)
1441 :
1442 : ! Generate the appropriate pw_env
1443 68 : NULLIFY (pw_env_sub)
1444 68 : CALL pw_env_create(pw_env_sub)
1445 68 : CALL pw_env_rebuild(pw_env_sub, qs_env, external_para_env=eri_env%para_env_sub)
1446 68 : CALL pw_env_get(pw_env_sub, auxbas_pw_pool=auxbas_pw_pool, poisson_env=poisson_env)
1447 :
1448 : ! TODO: maybe we can let `pw_env_rebuild` do what we manually overwrite here?
1449 68 : IF (eri_env%eri_gpw%redo_poisson) THEN
1450 : ! We need to rebuild the Poisson solver on the fly
1451 272 : IF (SUM(eri_env%periodicity) /= 0) THEN
1452 6 : poisson_env%parameters%solver = pw_poisson_periodic
1453 : ELSE
1454 62 : poisson_env%parameters%solver = pw_poisson_analytic
1455 : END IF
1456 272 : poisson_env%parameters%periodic = eri_env%periodicity
1457 :
1458 : ! Rebuilds the poisson green (influence) function according
1459 : ! to the poisson solver and parameters set so far.
1460 : ! Also sets the variable poisson_env%rebuild to .FALSE.
1461 68 : CALL pw_poisson_rebuild(poisson_env)
1462 :
1463 : ! set the cutoff radius for the Greens function in case we use ANALYTIC Poisson solver
1464 68 : CALL get_qs_env(qs_env, cell=cell)
1465 68 : rc = cell%hmat(1, 1)
1466 272 : DO iwa1 = 1, 3
1467 : ! TODO: I think this is not the largest possible radius inscribed in the cell
1468 272 : rc = MIN(rc, 0.5_dp*cell%hmat(iwa1, iwa1))
1469 : END DO
1470 68 : poisson_env%green_fft%radius = rc
1471 :
1472 : ! Overwrite the Greens function with the one we want
1473 68 : CALL pw_eri_green_create(poisson_env%green_fft, eri_env)
1474 :
1475 68 : IF (iw > 0) THEN
1476 34 : CALL get_qs_env(qs_env, cell=cell)
1477 272 : IF (SUM(cell%perd) /= SUM(eri_env%periodicity)) THEN
1478 0 : IF (SUM(eri_env%periodicity) /= 0) THEN
1479 : WRITE (UNIT=iw, FMT="(/,T2,A,T51,A30)") &
1480 0 : "ERI_GPW| Switching Poisson solver to", "PERIODIC"
1481 : ELSE
1482 : WRITE (UNIT=iw, FMT="(/,T2,A,T51,A30)") &
1483 0 : "ERI_GPW| Switching Poisson solver to", "ANALYTIC"
1484 : END IF
1485 : END IF
1486 : ! print out the Greens function to check it matches the Poisson solver
1487 37 : SELECT CASE (poisson_env%green_fft%method)
1488 : CASE (PERIODIC3D)
1489 : WRITE (UNIT=iw, FMT="(T2,A,T51,A30)") &
1490 3 : "ERI_GPW| Poisson Greens function", "PERIODIC"
1491 : CASE (ANALYTIC0D)
1492 : WRITE (UNIT=iw, FMT="(T2,A,T51,A30)") &
1493 31 : "ERI_GPW| Poisson Greens function", "ANALYTIC"
1494 31 : WRITE (UNIT=iw, FMT="(T2,A,T71,F10.4)") "ERI_GPW| Poisson cutoff radius", &
1495 62 : poisson_env%green_fft%radius*angstrom
1496 : CASE DEFAULT
1497 34 : CPABORT("Wrong Greens function setup")
1498 : END SELECT
1499 : END IF
1500 : END IF
1501 :
1502 500 : ALLOCATE (mo_coeff_as(nspins), fm_mo_coeff_as(nspins))
1503 148 : DO ispin = 1, nspins
1504 240 : BLOCK
1505 80 : REAL(KIND=dp), DIMENSION(:, :), ALLOCATABLE :: C
1506 : INTEGER :: nmo
1507 80 : TYPE(group_dist_d1_type) :: gd_array
1508 : TYPE(cp_fm_type), POINTER :: mo_coeff
1509 80 : CALL get_mo_set(mos(ispin), mo_coeff=mo_coeff, nmo=nmo)
1510 : CALL grep_rows_in_subgroups(para_env, eri_env%para_env_sub, mo_coeff, gd_array, C)
1511 :
1512 : CALL build_dbcsr_from_rows(eri_env%para_env_sub, mo_coeff_as(ispin), &
1513 80 : C(:, :nmo), mat_munu%matrix, gd_array, eri_env%eri_gpw%eps_filter)
1514 80 : CALL release_group_dist(gd_array)
1515 240 : DEALLOCATE (C)
1516 : END BLOCK
1517 :
1518 80 : CALL dbcsr_get_info(mo_coeff_as(ispin), nfullrows_total=nrow_global, nfullcols_total=ncol_global)
1519 :
1520 80 : NULLIFY (fm_struct)
1521 : CALL cp_fm_struct_create(fm_struct, context=blacs_env_sub, para_env=eri_env%para_env_sub, &
1522 80 : nrow_global=nrow_global, ncol_global=ncol_global)
1523 80 : CALL cp_fm_create(fm_mo_coeff_as(ispin), fm_struct)
1524 80 : CALL cp_fm_struct_release(fm_struct)
1525 :
1526 228 : CALL copy_dbcsr_to_fm(mo_coeff_as(ispin), fm_mo_coeff_as(ispin))
1527 : END DO
1528 :
1529 68 : IF (eri_env%method == eri_method_gpw_ht) THEN
1530 : ! We need a task list
1531 14 : NULLIFY (task_list_sub)
1532 14 : skip_load_balance_distributed = dft_control%qs_control%skip_load_balance_distributed
1533 14 : CALL allocate_task_list(task_list_sub)
1534 : CALL generate_qs_task_list(ks_env, task_list_sub, basis_type="ORB", &
1535 : reorder_rs_grid_ranks=.TRUE., &
1536 : skip_load_balance_distributed=skip_load_balance_distributed, &
1537 14 : pw_env_external=pw_env_sub, sab_orb_external=sab_orb_sub)
1538 :
1539 : ! Create sparse matrices carrying the matrix products, Code borrowed from the MP2 GPW method
1540 : ! Create equal distributions for them (no sparsity present)
1541 : ! We use the routines from mp2 suggesting that one may replicate the grids later for better performance
1542 98 : ALLOCATE (matrix_pq_rnu(nspins), fm_matrix_pq_rnu(nspins), fm_matrix_pq_rs(nspins))
1543 28 : DO ispin = 1, nspins
1544 14 : CALL dbcsr_create(matrix_pq_rnu(ispin), template=mo_coeff_as(ispin))
1545 14 : CALL dbcsr_set(matrix_pq_rnu(ispin), 0.0_dp)
1546 :
1547 14 : CALL dbcsr_get_info(matrix_pq_rnu(ispin), nfullrows_total=nrow_global, nfullcols_total=ncol_global)
1548 :
1549 14 : NULLIFY (fm_struct)
1550 : CALL cp_fm_struct_create(fm_struct, context=blacs_env_sub, para_env=eri_env%para_env_sub, &
1551 14 : nrow_global=nrow_global, ncol_global=ncol_global)
1552 14 : CALL cp_fm_create(fm_matrix_pq_rnu(ispin), fm_struct)
1553 14 : CALL cp_fm_struct_release(fm_struct)
1554 :
1555 14 : NULLIFY (fm_struct)
1556 : CALL cp_fm_struct_create(fm_struct, context=blacs_env_sub, para_env=eri_env%para_env_sub, &
1557 14 : nrow_global=ncol_global, ncol_global=ncol_global)
1558 14 : CALL cp_fm_create(fm_matrix_pq_rs(ispin), fm_struct)
1559 42 : CALL cp_fm_struct_release(fm_struct)
1560 : END DO
1561 :
1562 : ! Copy the active space of the MOs into DBCSR matrices
1563 : END IF
1564 :
1565 68 : CALL auxbas_pw_pool%create_pw(wfn_r)
1566 68 : CALL auxbas_pw_pool%create_pw(rho_g)
1567 : CALL get_qs_env(qs_env, qs_kind_set=qs_kind_set, cell=cell, &
1568 68 : particle_set=particle_set, atomic_kind_set=atomic_kind_set)
1569 :
1570 : ! pre-calculate wavefunctions on reals space grid
1571 68 : nspins = SIZE(mos)
1572 : ! In case of ROHF/ROKS, we assume the orbital coefficients in both spin channels to be the same
1573 : ! and save operations by calculating ERIs from only one spin channel
1574 : IF (restricted) nspins = 1
1575 68 : IF (eri_env%eri_gpw%store_wfn) THEN
1576 : ! pre-calculate wavefunctions on reals space grid
1577 56 : rsize = 0.0_dp
1578 56 : nmo = 0
1579 124 : DO ispin = 1, nspins
1580 68 : CALL get_mo_set(mo_set=mos(ispin), nmo=nx)
1581 68 : nmo = MAX(nmo, nx)
1582 328 : rsize = REAL(SIZE(wfn_r%array), KIND=dp)*nx
1583 : END DO
1584 56 : IF (print1 .AND. iw > 0) THEN
1585 1 : rsize = rsize*8._dp/1000000._dp
1586 1 : WRITE (iw, "(T2,'ERI_GPW|',' Store active orbitals on real space grid ',T66,F12.3,' MB')") rsize
1587 : END IF
1588 530 : ALLOCATE (wfn_a(nmo, nspins))
1589 124 : DO ispin = 1, nspins
1590 68 : CALL get_mo_set(mo_set=mos(ispin), mo_coeff=mo_coeff, nmo=nmo)
1591 308 : DO i1 = 1, SIZE(orbitals, 1)
1592 184 : iwfn = orbitals(i1, ispin)
1593 184 : CALL auxbas_pw_pool%create_pw(wfn_a(iwfn, ispin))
1594 : CALL calculate_wavefunction(mo_coeff, iwfn, wfn_a(iwfn, ispin), rho_g, atomic_kind_set, &
1595 184 : qs_kind_set, cell, dft_control, particle_set, pw_env_sub)
1596 252 : IF (print2 .AND. iw > 0) THEN
1597 0 : WRITE (iw, "(T2,'ERI_GPW|',' Orbital stored ',I4,' Spin ',i1)") iwfn, ispin
1598 : END IF
1599 : END DO
1600 : END DO
1601 : ELSE
1602 : ! Even if we do not store all WFNs, we still need containers for the functions to store
1603 12 : ALLOCATE (wfn1, wfn2)
1604 12 : CALL auxbas_pw_pool%create_pw(wfn1)
1605 12 : CALL auxbas_pw_pool%create_pw(wfn2)
1606 12 : IF (eri_env%method /= eri_method_gpw_ht) THEN
1607 6 : ALLOCATE (wfn3, wfn4)
1608 6 : CALL auxbas_pw_pool%create_pw(wfn3)
1609 6 : CALL auxbas_pw_pool%create_pw(wfn4)
1610 : END IF
1611 : END IF
1612 :
1613 : ! get some of the grids ready
1614 68 : CALL auxbas_pw_pool%create_pw(rho_r)
1615 68 : CALL auxbas_pw_pool%create_pw(pot_g)
1616 :
1617 : ! run the FFT once, to set up buffers and to take into account the memory
1618 68 : CALL pw_zero(rho_r)
1619 68 : CALL pw_transfer(rho_r, rho_g)
1620 68 : dvol = rho_r%pw_grid%dvol
1621 :
1622 68 : IF (iw > 0) THEN
1623 34 : CALL m_flush(iw)
1624 : END IF
1625 : ! calculate the integrals
1626 68 : stored_integrals = 0
1627 148 : DO isp1 = 1, nspins
1628 80 : CALL get_mo_set(mo_set=mos(isp1), nmo=nmo1)
1629 80 : nmm = (nmo1*(nmo1 + 1))/2
1630 80 : irange = get_irange_csr(nmm, eri_env%comm_exchange)
1631 436 : DO i1 = 1, SIZE(orbitals, 1)
1632 208 : iwa1 = orbitals(i1, isp1)
1633 208 : IF (eri_env%eri_gpw%store_wfn) THEN
1634 184 : wfn1 => wfn_a(iwa1, isp1)
1635 : ELSE
1636 : CALL calculate_wavefunction(fm_mo_coeff_as(isp1), iwa1, wfn1, rho_g, atomic_kind_set, &
1637 24 : qs_kind_set, cell, dft_control, particle_set, pw_env_sub)
1638 : END IF
1639 694 : DO i2 = i1, SIZE(orbitals, 1)
1640 406 : iwa2 = orbitals(i2, isp1)
1641 406 : iwa12 = csr_idx_to_combined(iwa1, iwa2, nmo1)
1642 : ! Skip calculation directly if the pair is not part of our subgroup
1643 406 : IF (iwa12 < irange(1) .OR. iwa12 > irange(2)) CYCLE
1644 397 : iwa12 = iwa12 - irange(1) + 1
1645 397 : IF (eri_env%eri_gpw%store_wfn) THEN
1646 367 : wfn2 => wfn_a(iwa2, isp1)
1647 : ELSE
1648 : CALL calculate_wavefunction(fm_mo_coeff_as(isp1), iwa2, wfn2, rho_g, atomic_kind_set, &
1649 30 : qs_kind_set, cell, dft_control, particle_set, pw_env_sub)
1650 : END IF
1651 : ! calculate charge distribution and potential
1652 397 : CALL pw_zero(rho_r)
1653 397 : CALL pw_multiply(rho_r, wfn1, wfn2)
1654 397 : CALL pw_transfer(rho_r, rho_g)
1655 397 : CALL pw_poisson_solve(poisson_env, rho_g, pair_int, pot_g)
1656 :
1657 : ! screening using pair_int
1658 397 : IF (pair_int < eri_env%eps_integral) CYCLE
1659 397 : CALL pw_transfer(pot_g, rho_r)
1660 : !
1661 1002 : IF (eri_env%method == eri_method_gpw_ht) THEN
1662 36 : CALL pw_scale(rho_r, dvol)
1663 72 : DO isp2 = isp1, nspins
1664 36 : CALL get_mo_set(mo_set=mos(isp2), nmo=nmo2)
1665 36 : nx = (nmo2*(nmo2 + 1))/2
1666 180 : ALLOCATE (eri(nx), eri_index(nx))
1667 36 : CALL dbcsr_set(mat_munu%matrix, 0.0_dp)
1668 : CALL integrate_v_rspace(rho_r, hmat=mat_munu, qs_env=qs_env, &
1669 : calculate_forces=.FALSE., compute_tau=.FALSE., gapw=.FALSE., &
1670 36 : pw_env_external=pw_env_sub, task_list_external=task_list_sub)
1671 :
1672 : CALL dbcsr_multiply("N", "N", 1.0_dp, mat_munu%matrix, mo_coeff_as(isp2), &
1673 36 : 0.0_dp, matrix_pq_rnu(isp2), filter_eps=eri_env%eri_gpw%eps_filter)
1674 36 : CALL copy_dbcsr_to_fm(matrix_pq_rnu(isp2), fm_matrix_pq_rnu(isp2))
1675 :
1676 36 : CALL cp_fm_get_info(fm_matrix_pq_rnu(isp2), ncol_global=ncol_global, nrow_global=nrow_global)
1677 :
1678 : CALL parallel_gemm("T", "N", ncol_global, ncol_global, nrow_global, 0.5_dp, &
1679 : fm_matrix_pq_rnu(isp2), fm_mo_coeff_as(isp2), &
1680 36 : 0.0_dp, fm_matrix_pq_rs(isp2))
1681 : CALL parallel_gemm("T", "N", ncol_global, ncol_global, nrow_global, 0.5_dp, &
1682 : fm_mo_coeff_as(isp2), fm_matrix_pq_rnu(isp2), &
1683 36 : 1.0_dp, fm_matrix_pq_rs(isp2))
1684 :
1685 : CALL cp_fm_get_info(fm_matrix_pq_rs(isp2), ncol_local=ncol_local, nrow_local=nrow_local, &
1686 36 : col_indices=col_indices, row_indices=row_indices)
1687 :
1688 36 : icount2 = 0
1689 108 : DO col_local = 1, ncol_local
1690 72 : iwb2 = orbitals(col_indices(col_local), isp2)
1691 192 : DO row_local = 1, nrow_local
1692 84 : iwb1 = orbitals(row_indices(row_local), isp2)
1693 :
1694 156 : IF (iwb1 <= iwb2) THEN
1695 63 : iwb12 = csr_idx_to_combined(iwb1, iwb2, nmo2)
1696 63 : erint = fm_matrix_pq_rs(isp2)%local_data(row_local, col_local)
1697 63 : IF (ABS(erint) > eri_env%eps_integral .AND. (irange(1) - 1 + iwa12 <= iwb12 .OR. isp1 /= isp2)) THEN
1698 35 : icount2 = icount2 + 1
1699 35 : eri(icount2) = erint
1700 35 : eri_index(icount2) = iwb12
1701 : END IF
1702 : END IF
1703 : END DO
1704 : END DO
1705 36 : stored_integrals = stored_integrals + icount2
1706 : !
1707 36 : isp = (isp1 - 1)*isp2 + (isp2 - isp1 + 1)
1708 36 : CALL update_csr_matrix(eri_env%eri(isp)%csr_mat, icount2, eri, eri_index, iwa12)
1709 : !
1710 180 : DEALLOCATE (eri, eri_index)
1711 : END DO
1712 361 : ELSEIF (eri_env%method == eri_method_full_gpw) THEN
1713 776 : DO isp2 = isp1, nspins
1714 415 : CALL get_mo_set(mo_set=mos(isp2), nmo=nmo2)
1715 415 : nx = (nmo2*(nmo2 + 1))/2
1716 2075 : ALLOCATE (eri(nx), eri_index(nx))
1717 415 : icount2 = 0
1718 415 : iwbs = 1
1719 415 : IF (isp1 == isp2) iwbs = i1
1720 415 : isp = (isp1 - 1)*isp2 + (isp2 - isp1 + 1)
1721 1486 : DO i3 = iwbs, SIZE(orbitals, 1)
1722 1071 : iwb1 = orbitals(i3, isp2)
1723 1071 : IF (eri_env%eri_gpw%store_wfn) THEN
1724 1046 : wfn3 => wfn_a(iwb1, isp2)
1725 : ELSE
1726 : CALL calculate_wavefunction(fm_mo_coeff_as(isp1), iwb1, wfn3, rho_g, atomic_kind_set, &
1727 25 : qs_kind_set, cell, dft_control, particle_set, pw_env_sub)
1728 : END IF
1729 1071 : CALL pw_zero(wfn_r)
1730 1071 : CALL pw_multiply(wfn_r, rho_r, wfn3)
1731 1071 : iwbt = i3
1732 1071 : IF (isp1 == isp2 .AND. i1 == i3) iwbt = i2
1733 3386 : DO i4 = iwbt, SIZE(orbitals, 1)
1734 1900 : iwb2 = orbitals(i4, isp2)
1735 1900 : IF (eri_env%eri_gpw%store_wfn) THEN
1736 1870 : wfn4 => wfn_a(iwb2, isp2)
1737 : ELSE
1738 : CALL calculate_wavefunction(fm_mo_coeff_as(isp1), iwb2, wfn4, rho_g, atomic_kind_set, &
1739 30 : qs_kind_set, cell, dft_control, particle_set, pw_env_sub)
1740 : END IF
1741 : ! We reduce the amount of communication by collecting the local sums first and sum globally later
1742 1900 : erint = pw_integral_ab(wfn_r, wfn4, local_only=.TRUE.)
1743 1900 : icount2 = icount2 + 1
1744 1900 : eri(icount2) = erint
1745 2971 : eri_index(icount2) = csr_idx_to_combined(iwb1, iwb2, nmo2)
1746 : END DO
1747 : END DO
1748 : ! Now, we sum the integrals globally
1749 415 : CALL eri_env%para_env_sub%sum(eri)
1750 : ! and we reorder the integrals to prevent storing too small integrals
1751 415 : intcount = 0
1752 415 : icount2 = 0
1753 : iwbs = 1
1754 : IF (isp1 == isp2) iwbs = i1
1755 415 : isp = (isp1 - 1)*isp2 + (isp2 - isp1 + 1)
1756 1486 : DO i3 = iwbs, SIZE(orbitals, 1)
1757 1071 : iwb1 = orbitals(i3, isp2)
1758 1071 : iwbt = i3
1759 1071 : IF (isp1 == isp2 .AND. i1 == i3) iwbt = i2
1760 3386 : DO i4 = iwbt, SIZE(orbitals, 1)
1761 1900 : iwb2 = orbitals(i4, isp2)
1762 1900 : intcount = intcount + 1
1763 1900 : erint = eri(intcount)
1764 2971 : IF (ABS(erint) > eri_env%eps_integral) THEN
1765 1526 : IF (MOD(intcount, eri_env%para_env_sub%num_pe) == eri_env%para_env_sub%mepos) THEN
1766 765 : icount2 = icount2 + 1
1767 765 : eri(icount2) = erint
1768 765 : eri_index(icount2) = eri_index(intcount)
1769 : END IF
1770 : END IF
1771 : END DO
1772 : END DO
1773 415 : stored_integrals = stored_integrals + icount2
1774 : !
1775 415 : CALL update_csr_matrix(eri_env%eri(isp)%csr_mat, icount2, eri, eri_index, iwa12)
1776 : !
1777 1191 : DEALLOCATE (eri, eri_index)
1778 : END DO
1779 : ELSE
1780 0 : CPABORT("Unknown option")
1781 : END IF
1782 : END DO
1783 : END DO
1784 : END DO
1785 :
1786 68 : IF (print1 .AND. iw > 0) THEN
1787 1 : WRITE (iw, "(T2,'ERI_GPW|',' Number of Integrals stored locally',T71,I10)") stored_integrals
1788 : END IF
1789 :
1790 68 : IF (eri_env%eri_gpw%store_wfn) THEN
1791 124 : DO ispin = 1, nspins
1792 308 : DO i1 = 1, SIZE(orbitals, 1)
1793 184 : iwfn = orbitals(i1, ispin)
1794 252 : CALL wfn_a(iwfn, ispin)%release()
1795 : END DO
1796 : END DO
1797 56 : DEALLOCATE (wfn_a)
1798 : ELSE
1799 12 : CALL wfn1%release()
1800 12 : CALL wfn2%release()
1801 12 : DEALLOCATE (wfn1, wfn2)
1802 12 : IF (eri_env%method /= eri_method_gpw_ht) THEN
1803 6 : CALL wfn3%release()
1804 6 : CALL wfn4%release()
1805 6 : DEALLOCATE (wfn3, wfn4)
1806 : END IF
1807 : END IF
1808 68 : CALL auxbas_pw_pool%give_back_pw(wfn_r)
1809 68 : CALL auxbas_pw_pool%give_back_pw(rho_g)
1810 68 : CALL auxbas_pw_pool%give_back_pw(rho_r)
1811 68 : CALL auxbas_pw_pool%give_back_pw(pot_g)
1812 :
1813 68 : IF (eri_env%method == eri_method_gpw_ht) THEN
1814 28 : DO ispin = 1, nspins
1815 14 : CALL dbcsr_release(mo_coeff_as(ispin))
1816 14 : CALL dbcsr_release(matrix_pq_rnu(ispin))
1817 14 : CALL cp_fm_release(fm_matrix_pq_rnu(ispin))
1818 28 : CALL cp_fm_release(fm_matrix_pq_rs(ispin))
1819 : END DO
1820 14 : DEALLOCATE (matrix_pq_rnu, fm_matrix_pq_rnu, fm_matrix_pq_rs)
1821 14 : CALL deallocate_task_list(task_list_sub)
1822 : END IF
1823 148 : DO ispin = 1, nspins
1824 80 : CALL dbcsr_release(mo_coeff_as(ispin))
1825 148 : CALL cp_fm_release(fm_mo_coeff_as(ispin))
1826 : END DO
1827 68 : DEALLOCATE (mo_coeff_as, fm_mo_coeff_as)
1828 68 : CALL release_neighbor_list_sets(sab_orb_sub)
1829 68 : CALL cp_blacs_env_release(blacs_env_sub)
1830 68 : CALL dbcsr_release(mat_munu%matrix)
1831 68 : DEALLOCATE (mat_munu%matrix)
1832 68 : CALL pw_env_release(pw_env_sub)
1833 : ! Return to the old qs_control
1834 68 : dft_control%qs_control => qs_control_old
1835 68 : DEALLOCATE (qs_control%e_cutoff)
1836 68 : DEALLOCATE (qs_control)
1837 :
1838 : ! print out progress
1839 68 : IF (iw > 0) THEN
1840 34 : t2 = m_walltime()
1841 34 : WRITE (iw, '(/,T2,A,T66,F14.2)') "ERI_GPW| ERI calculation took (sec)", t2 - t1
1842 34 : CALL m_flush(iw)
1843 : END IF
1844 :
1845 68 : CALL timestop(handle)
1846 :
1847 136 : END SUBROUTINE calculate_eri_gpw
1848 :
1849 : ! **************************************************************************************************
1850 : !> \brief Sets the Green's function for the ERI calculation. Here we deal with the G=0 case!
1851 : !> \param green ...
1852 : !> \param eri_env ...
1853 : !> \par History
1854 : !> 04.2016 created [JGH]
1855 : !> 08.2025 added support for the LR truncation [SB]
1856 : ! **************************************************************************************************
1857 68 : SUBROUTINE pw_eri_green_create(green, eri_env)
1858 :
1859 : TYPE(greens_fn_type), INTENT(INOUT) :: green
1860 : TYPE(eri_type) :: eri_env
1861 :
1862 : COMPLEX(KIND=dp) :: erf_fac_p, z_p
1863 : INTEGER :: ig
1864 : REAL(KIND=dp) :: cossin_fac, ea, erfcos_fac, exp_prefac, &
1865 : g, G0, g2, g3d, ga, Ginf, omega, &
1866 : omega2, Rc, Rc2
1867 :
1868 : ! initialize influence function
1869 : ASSOCIATE (gf => green%influence_fn, grid => green%influence_fn%pw_grid)
1870 74 : SELECT CASE (green%method)
1871 : CASE (PERIODIC3D)
1872 :
1873 70 : SELECT CASE (eri_env%operator)
1874 : CASE (eri_operator_coulomb)
1875 262145 : DO ig = grid%first_gne0, grid%ngpts_cut_local
1876 262143 : g2 = grid%gsq(ig)
1877 262145 : gf%array(ig) = fourpi/g2
1878 : END DO
1879 2 : IF (grid%have_g0) gf%array(1) = 0.0_dp
1880 :
1881 : CASE (eri_operator_yukawa)
1882 0 : CALL cp_warn(__LOCATION__, "Yukawa operator has not been tested")
1883 0 : omega2 = eri_env%omega**2
1884 0 : DO ig = grid%first_gne0, grid%ngpts_cut_local
1885 0 : g2 = grid%gsq(ig)
1886 0 : gf%array(ig) = fourpi/(omega2 + g2)
1887 : END DO
1888 0 : IF (grid%have_g0) gf%array(1) = fourpi/omega2
1889 :
1890 : CASE (eri_operator_erf)
1891 0 : omega2 = eri_env%omega**2
1892 0 : DO ig = grid%first_gne0, grid%ngpts_cut_local
1893 0 : g2 = grid%gsq(ig)
1894 0 : gf%array(ig) = fourpi/g2*EXP(-0.25_dp*g2/omega2)
1895 : END DO
1896 0 : IF (grid%have_g0) gf%array(1) = 0.0_dp
1897 :
1898 : CASE (eri_operator_erfc)
1899 0 : omega2 = eri_env%omega**2
1900 0 : DO ig = grid%first_gne0, grid%ngpts_cut_local
1901 0 : g2 = grid%gsq(ig)
1902 0 : gf%array(ig) = fourpi/g2*(1.0_dp - EXP(-0.25_dp*g2/omega2))
1903 : END DO
1904 0 : IF (grid%have_g0) gf%array(1) = pi/omega2
1905 :
1906 : CASE (eri_operator_trunc)
1907 0 : Rc = eri_env%cutoff_radius
1908 0 : DO ig = grid%first_gne0, grid%ngpts_cut_local
1909 0 : g2 = grid%gsq(ig)
1910 0 : g = SQRT(g2)
1911 : ! Taylor expansion around zero
1912 0 : IF (g*Rc >= 0.005_dp) THEN
1913 0 : gf%array(ig) = fourpi/g2*(1.0_dp - COS(g*Rc))
1914 : ELSE
1915 0 : gf%array(ig) = fourpi/g2*(g*Rc)**2/2.0_dp*(1.0_dp - (g*Rc)**2/12.0_dp)
1916 : END IF
1917 : END DO
1918 0 : IF (grid%have_g0) gf%array(1) = twopi*Rc**2
1919 :
1920 : CASE (eri_operator_lr_trunc)
1921 4 : omega = eri_env%omega
1922 4 : omega2 = omega**2
1923 4 : Rc = eri_env%cutoff_radius
1924 4 : Rc2 = Rc**2
1925 4 : G0 = 0.001_dp ! threshold for the G=0 case
1926 4 : Ginf = 20.0_dp ! threshold for the Taylor exapnsion arounf G=∞
1927 843752 : DO ig = grid%first_gne0, grid%ngpts_cut_local
1928 843748 : g2 = grid%gsq(ig)
1929 843748 : g = SQRT(g2)
1930 843752 : IF (g <= 2.0_dp*G0) THEN
1931 : gf%array(ig) = -pi/omega2*erf(omega*Rc) &
1932 : + twopi*Rc2*erf(omega*Rc) &
1933 0 : + 2*rootpi*Rc*EXP(-omega2*Rc2)/omega
1934 843748 : ELSE IF (g >= 2.0_dp*Ginf*omega) THEN
1935 : ! exponential prefactor
1936 1488 : exp_prefac = EXP(-omega2*Rc2)/(rootpi*(omega2*Rc2 + 0.25_dp*g2/omega2))
1937 : ! cos sin factor
1938 1488 : cossin_fac = omega*Rc*COS(g*Rc) - 0.5_dp*g/omega*SIN(g*Rc)
1939 : ! real erf term with cosine
1940 1488 : erfcos_fac = ERF(omega*Rc)*COS(g*Rc)
1941 : ! Combine terms
1942 1488 : gf%array(ig) = fourpi/g2*(-exp_prefac*cossin_fac - erfcos_fac)
1943 : ELSE
1944 : ! exponential prefactor
1945 842260 : exp_prefac = twopi/g2*EXP(-0.25_dp*g2/omega2)
1946 : ! Compute complex arguments for erf
1947 842260 : z_p = CMPLX(omega*Rc, 0.5_dp*g/omega, kind=dp)
1948 : ! Evaluate complex error functions
1949 842260 : erf_fac_p = 2.0_dp*REAL(erfz_fast(z_p))
1950 : ! Real erf term with cosine
1951 842260 : erfcos_fac = fourpi/g2*ERF(omega*Rc)*COS(g*Rc)
1952 : ! Combine terms
1953 842260 : gf%array(ig) = exp_prefac*erf_fac_p - erfcos_fac
1954 : END IF
1955 : END DO
1956 4 : IF (grid%have_g0) THEN
1957 : gf%array(1) = -pi/omega2*ERF(omega*Rc) &
1958 : + twopi*Rc2*ERF(omega*Rc) &
1959 2 : + 2*rootpi*Rc*EXP(-omega2*Rc2)/omega
1960 : END IF
1961 :
1962 : CASE DEFAULT
1963 6 : CPABORT("Please specify a valid operator for the periodic Poisson solver")
1964 : END SELECT
1965 :
1966 : ! The analytic Poisson solver simply limits the domain of integration
1967 : ! of the Fourier transform to a sphere of radius Rc, rather than integrating
1968 : ! over all space (-∞,∞)
1969 : CASE (ANALYTIC0D)
1970 :
1971 110 : SELECT CASE (eri_env%operator)
1972 : ! This is identical to the truncated Coulomb operator integrated
1973 : ! over all space, when the truncation radius is equal to the radius of
1974 : ! the Poisson solver
1975 : CASE (eri_operator_coulomb, eri_operator_trunc)
1976 48 : IF (eri_env%operator == eri_operator_coulomb) THEN
1977 48 : Rc = green%radius
1978 : ELSE
1979 0 : Rc = eri_env%cutoff_radius
1980 : END IF
1981 15077199 : DO ig = grid%first_gne0, grid%ngpts_cut_local
1982 15077151 : g2 = grid%gsq(ig)
1983 15077151 : g = SQRT(g2)
1984 : ! Taylor expansion around zero
1985 15077199 : IF (g*Rc >= 0.005_dp) THEN
1986 15077151 : gf%array(ig) = fourpi/g2*(1.0_dp - COS(g*Rc))
1987 : ELSE
1988 0 : gf%array(ig) = fourpi/g2*(g*Rc)**2/2.0_dp*(1.0_dp - (g*Rc)**2/12.0_dp)
1989 : END IF
1990 : END DO
1991 48 : IF (grid%have_g0) gf%array(1) = twopi*Rc**2
1992 :
1993 : ! Not tested
1994 : CASE (eri_operator_yukawa)
1995 0 : CALL cp_warn(__LOCATION__, "Yukawa operator has not been tested")
1996 0 : Rc = green%radius
1997 0 : omega = eri_env%omega
1998 0 : ea = EXP(-omega*Rc)
1999 0 : DO ig = grid%first_gne0, grid%ngpts_cut_local
2000 0 : g2 = grid%gsq(ig)
2001 0 : g = SQRT(g2)
2002 0 : g3d = fourpi/(omega**2 + g2)
2003 0 : gf%array(ig) = g3d*(1.0_dp - ea*(COS(g*Rc) + omega/g*SIN(g*Rc)))
2004 : END DO
2005 0 : IF (grid%have_g0) gf%array(1) = fourpi/(omega**2)*(1.0_dp - ea*(1.0_dp + omega*Rc))
2006 :
2007 : ! Long-range Coulomb
2008 : ! TODO: this should be equivalent to LR truncated Coulomb from above!
2009 : CASE (eri_operator_erf, eri_operator_lr_trunc)
2010 14 : IF (eri_env%operator == eri_operator_erf) THEN
2011 14 : Rc = green%radius
2012 : ELSE
2013 0 : Rc = eri_env%cutoff_radius
2014 : END IF
2015 14 : omega2 = eri_env%omega**2
2016 1512007 : DO ig = grid%first_gne0, grid%ngpts_cut_local
2017 1511993 : g2 = grid%gsq(ig)
2018 1511993 : g = SQRT(g2)
2019 1511993 : ga = -0.25_dp*g2/omega2
2020 1512007 : gf%array(ig) = fourpi/g2*EXP(ga)*(1.0_dp - COS(g*Rc))
2021 : END DO
2022 14 : IF (grid%have_g0) gf%array(1) = twopi*Rc**2
2023 :
2024 : ! Short-range Coulomb
2025 : ! TODO: this should actually be properly derived and see whether it is correct
2026 : CASE (eri_operator_erfc)
2027 : CALL cp_warn(__LOCATION__, &
2028 0 : "Short-range Coulomb operator may be incorrect with ANALYTIC0D Poisson solver")
2029 0 : Rc = green%radius
2030 0 : omega2 = eri_env%omega**2
2031 0 : DO ig = grid%first_gne0, grid%ngpts_cut_local
2032 0 : g2 = grid%gsq(ig)
2033 0 : g = SQRT(g2)
2034 0 : ga = -0.25_dp*g2/omega2
2035 0 : gf%array(ig) = fourpi/g2*(1.0_dp - EXP(ga))*(1.0_dp - COS(g*Rc))
2036 : END DO
2037 0 : IF (grid%have_g0) gf%array(1) = pi/omega2
2038 :
2039 : CASE DEFAULT
2040 62 : CPABORT("Unsupported operator")
2041 : END SELECT
2042 :
2043 : CASE DEFAULT
2044 68 : CPABORT("Unsupported Poisson solver")
2045 : END SELECT
2046 : END ASSOCIATE
2047 :
2048 68 : END SUBROUTINE pw_eri_green_create
2049 :
2050 : ! **************************************************************************************************
2051 : !> \brief Adds data for a new row to the csr matrix
2052 : !> \param csr_mat ...
2053 : !> \param nnz ...
2054 : !> \param rdat ...
2055 : !> \param rind ...
2056 : !> \param irow ...
2057 : !> \par History
2058 : !> 04.2016 created [JGH]
2059 : ! **************************************************************************************************
2060 451 : SUBROUTINE update_csr_matrix(csr_mat, nnz, rdat, rind, irow)
2061 :
2062 : TYPE(dbcsr_csr_type), INTENT(INOUT) :: csr_mat
2063 : INTEGER, INTENT(IN) :: nnz
2064 : REAL(KIND=dp), DIMENSION(:), INTENT(IN) :: rdat
2065 : INTEGER, DIMENSION(:), INTENT(IN) :: rind
2066 : INTEGER, INTENT(IN) :: irow
2067 :
2068 : INTEGER :: k, nrow, nze, nze_new
2069 :
2070 451 : IF (irow /= 0) THEN
2071 451 : nze = csr_mat%nze_local
2072 451 : nze_new = nze + nnz
2073 : ! values
2074 451 : CALL reallocate(csr_mat%nzval_local%r_dp, 1, nze_new)
2075 1251 : csr_mat%nzval_local%r_dp(nze + 1:nze_new) = rdat(1:nnz)
2076 : ! col indices
2077 451 : CALL reallocate(csr_mat%colind_local, 1, nze_new)
2078 1251 : csr_mat%colind_local(nze + 1:nze_new) = rind(1:nnz)
2079 : ! rows
2080 451 : nrow = csr_mat%nrows_local
2081 451 : CALL reallocate(csr_mat%rowptr_local, 1, irow + 1)
2082 1210 : csr_mat%rowptr_local(nrow + 1:irow) = nze + 1
2083 451 : csr_mat%rowptr_local(irow + 1) = nze_new + 1
2084 : ! nzerow
2085 451 : CALL reallocate(csr_mat%nzerow_local, 1, irow)
2086 1210 : DO k = nrow + 1, irow
2087 1210 : csr_mat%nzerow_local(k) = csr_mat%rowptr_local(k + 1) - csr_mat%rowptr_local(k)
2088 : END DO
2089 451 : csr_mat%nrows_local = irow
2090 451 : csr_mat%nze_local = csr_mat%nze_local + nnz
2091 : END IF
2092 451 : csr_mat%nze_total = csr_mat%nze_total + nnz
2093 451 : csr_mat%has_indices = .TRUE.
2094 :
2095 451 : END SUBROUTINE update_csr_matrix
2096 :
2097 : ! **************************************************************************************************
2098 : !> \brief Computes and prints the active orbitals on Cube Files
2099 : !> \param input ...
2100 : !> \param qs_env the qs_env in which the qs_env lives
2101 : !> \param mos ...
2102 : ! **************************************************************************************************
2103 2 : SUBROUTINE print_orbital_cubes(input, qs_env, mos)
2104 : TYPE(section_vals_type), POINTER :: input
2105 : TYPE(qs_environment_type), POINTER :: qs_env
2106 : TYPE(mo_set_type), DIMENSION(:), INTENT(IN) :: mos
2107 :
2108 : CHARACTER(LEN=default_path_length) :: filebody, filename, title
2109 : INTEGER :: i, imo, isp, nmo, str(3), unit_nr
2110 2 : INTEGER, DIMENSION(:), POINTER :: alist, blist, istride
2111 : LOGICAL :: do_mo, explicit_a, explicit_b
2112 2 : TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
2113 : TYPE(cell_type), POINTER :: cell
2114 : TYPE(cp_fm_type), POINTER :: mo_coeff
2115 : TYPE(dft_control_type), POINTER :: dft_control
2116 : TYPE(mp_para_env_type), POINTER :: para_env
2117 : TYPE(particle_list_type), POINTER :: particles
2118 2 : TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
2119 : TYPE(pw_c1d_gs_type) :: wf_g
2120 : TYPE(pw_env_type), POINTER :: pw_env
2121 : TYPE(pw_pool_type), POINTER :: auxbas_pw_pool
2122 : TYPE(pw_r3d_rs_type) :: wf_r
2123 2 : TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
2124 : TYPE(qs_subsys_type), POINTER :: subsys
2125 : TYPE(section_vals_type), POINTER :: dft_section, scf_input
2126 :
2127 2 : CALL section_vals_val_get(input, "FILENAME", c_val=filebody)
2128 2 : CALL section_vals_val_get(input, "STRIDE", i_vals=istride)
2129 2 : IF (SIZE(istride) == 1) THEN
2130 8 : str(1:3) = istride(1)
2131 0 : ELSEIF (SIZE(istride) == 3) THEN
2132 0 : str(1:3) = istride(1:3)
2133 : ELSE
2134 0 : CPABORT("STRIDE arguments inconsistent")
2135 : END IF
2136 2 : CALL section_vals_val_get(input, "ALIST", i_vals=alist, explicit=explicit_a)
2137 2 : CALL section_vals_val_get(input, "BLIST", i_vals=blist, explicit=explicit_b)
2138 :
2139 : CALL get_qs_env(qs_env=qs_env, &
2140 : dft_control=dft_control, &
2141 : para_env=para_env, &
2142 : subsys=subsys, &
2143 : atomic_kind_set=atomic_kind_set, &
2144 : qs_kind_set=qs_kind_set, &
2145 : cell=cell, &
2146 : particle_set=particle_set, &
2147 : pw_env=pw_env, &
2148 2 : input=scf_input)
2149 :
2150 2 : CALL qs_subsys_get(subsys, particles=particles)
2151 : !
2152 2 : CALL pw_env_get(pw_env, auxbas_pw_pool=auxbas_pw_pool)
2153 2 : CALL auxbas_pw_pool%create_pw(wf_r)
2154 2 : CALL auxbas_pw_pool%create_pw(wf_g)
2155 : !
2156 2 : dft_section => section_vals_get_subs_vals(scf_input, "DFT")
2157 : !
2158 4 : DO isp = 1, SIZE(mos)
2159 2 : CALL get_mo_set(mo_set=mos(isp), mo_coeff=mo_coeff, nmo=nmo)
2160 :
2161 2 : IF (SIZE(mos) > 1) THEN
2162 0 : SELECT CASE (isp)
2163 : CASE (1)
2164 : CALL write_mo_set_to_output_unit(mos(isp), qs_kind_set, particle_set, &
2165 0 : dft_section, 4, 0, final_mos=.TRUE., spin="ALPHA")
2166 : CASE (2)
2167 : CALL write_mo_set_to_output_unit(mos(isp), qs_kind_set, particle_set, &
2168 0 : dft_section, 4, 0, final_mos=.TRUE., spin="BETA")
2169 : CASE DEFAULT
2170 0 : CPABORT("Invalid spin")
2171 : END SELECT
2172 : ELSE
2173 : CALL write_mo_set_to_output_unit(mos(isp), qs_kind_set, particle_set, &
2174 2 : dft_section, 4, 0, final_mos=.TRUE.)
2175 : END IF
2176 :
2177 22 : DO imo = 1, nmo
2178 16 : IF (isp == 1 .AND. explicit_a) THEN
2179 16 : IF (alist(1) == -1) THEN
2180 : do_mo = .TRUE.
2181 : ELSE
2182 16 : do_mo = .FALSE.
2183 64 : DO i = 1, SIZE(alist)
2184 64 : IF (imo == alist(i)) do_mo = .TRUE.
2185 : END DO
2186 : END IF
2187 0 : ELSE IF (isp == 2 .AND. explicit_b) THEN
2188 0 : IF (blist(1) == -1) THEN
2189 : do_mo = .TRUE.
2190 : ELSE
2191 0 : do_mo = .FALSE.
2192 0 : DO i = 1, SIZE(blist)
2193 0 : IF (imo == blist(i)) do_mo = .TRUE.
2194 : END DO
2195 : END IF
2196 : ELSE
2197 : do_mo = .TRUE.
2198 : END IF
2199 16 : IF (.NOT. do_mo) CYCLE
2200 : CALL calculate_wavefunction(mo_coeff, imo, wf_r, wf_g, atomic_kind_set, &
2201 6 : qs_kind_set, cell, dft_control, particle_set, pw_env)
2202 6 : IF (para_env%is_source()) THEN
2203 3 : WRITE (filename, '(A,A1,I4.4,A1,I1.1,A)') TRIM(filebody), "_", imo, "_", isp, ".cube"
2204 3 : CALL open_file(filename, unit_number=unit_nr, file_status="UNKNOWN", file_action="WRITE")
2205 3 : WRITE (title, *) "Active Orbital ", imo, " spin ", isp
2206 : ELSE
2207 3 : unit_nr = -1
2208 : END IF
2209 6 : CALL cp_pw_to_cube(wf_r, unit_nr, title, particles=particles, stride=istride)
2210 8 : IF (para_env%is_source()) THEN
2211 13 : CALL close_file(unit_nr)
2212 : END IF
2213 : END DO
2214 : END DO
2215 :
2216 2 : CALL auxbas_pw_pool%give_back_pw(wf_r)
2217 2 : CALL auxbas_pw_pool%give_back_pw(wf_g)
2218 :
2219 2 : END SUBROUTINE print_orbital_cubes
2220 :
2221 : ! **************************************************************************************************
2222 : !> \brief Writes a FCIDUMP file
2223 : !> \param active_space_env ...
2224 : !> \param as_input ...
2225 : !> \param restricted ...
2226 : !> \par History
2227 : !> 04.2016 created [JGH]
2228 : ! **************************************************************************************************
2229 68 : SUBROUTINE fcidump(active_space_env, as_input, restricted)
2230 :
2231 : TYPE(active_space_type), POINTER :: active_space_env
2232 : TYPE(section_vals_type), POINTER :: as_input
2233 : LOGICAL, INTENT(IN) :: restricted
2234 :
2235 : INTEGER :: i, i1, i2, i3, i4, isym, iw, m1, m2, &
2236 : nmo, norb, nspins
2237 : REAL(KIND=dp) :: checksum, esub
2238 68 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: fmat
2239 : TYPE(cp_logger_type), POINTER :: logger
2240 : TYPE(eri_fcidump_checksum) :: eri_checksum
2241 :
2242 68 : checksum = 0.0_dp
2243 :
2244 136 : logger => cp_get_default_logger()
2245 : iw = cp_print_key_unit_nr(logger, as_input, "FCIDUMP", &
2246 68 : extension=".fcidump", file_status="REPLACE", file_action="WRITE", file_form="FORMATTED")
2247 : !
2248 68 : nspins = active_space_env%nspins
2249 68 : norb = SIZE(active_space_env%active_orbitals, 1)
2250 68 : IF (nspins == 1 .OR. restricted) THEN
2251 : ! Closed shell or restricted open-shell
2252 : ASSOCIATE (ms2 => active_space_env%multiplicity, &
2253 : nelec => active_space_env%nelec_active)
2254 :
2255 56 : IF (iw > 0) THEN
2256 28 : WRITE (iw, "(A,A,I4,A,I4,A,I2,A)") "&FCI", " NORB=", norb, ",NELEC=", nelec, ",MS2=", ms2, ","
2257 28 : isym = 1
2258 102 : WRITE (iw, "(A,1000(I1,','))") " ORBSYM=", (isym, i=1, norb)
2259 28 : isym = 0
2260 28 : WRITE (iw, "(A,I1,A)") " ISYM=", isym, ","
2261 28 : IF (restricted) WRITE (iw, "(A,I1,A)") " UHF=", 0, ","
2262 28 : WRITE (iw, "(A)") " /"
2263 : END IF
2264 : !
2265 : ! Print integrals: ERI
2266 : CALL active_space_env%eri%eri_foreach(1, active_space_env%active_orbitals, &
2267 56 : eri_fcidump_print(iw, 1, 1), 1, 1)
2268 56 : CALL eri_checksum%set(1, 1)
2269 56 : CALL active_space_env%eri%eri_foreach(1, active_space_env%active_orbitals, eri_checksum, 1, 1)
2270 :
2271 : ! Print integrals: Fij
2272 : ! replicate Fock matrix
2273 56 : nmo = active_space_env%eri%norb
2274 224 : ALLOCATE (fmat(nmo, nmo))
2275 56 : CALL replicate_and_symmetrize_matrix(nmo, active_space_env%fock_sub(1), fmat)
2276 56 : IF (iw > 0) THEN
2277 28 : i3 = 0; i4 = 0
2278 102 : DO m1 = 1, SIZE(active_space_env%active_orbitals, 1)
2279 74 : i1 = active_space_env%active_orbitals(m1, 1)
2280 251 : DO m2 = m1, SIZE(active_space_env%active_orbitals, 1)
2281 149 : i2 = active_space_env%active_orbitals(m2, 1)
2282 149 : checksum = checksum + ABS(fmat(i1, i2))
2283 223 : WRITE (iw, "(ES23.16,4I4)") fmat(i1, i2), m1, m2, i3, i4
2284 : END DO
2285 : END DO
2286 : END IF
2287 56 : DEALLOCATE (fmat)
2288 : ! Print energy
2289 56 : esub = active_space_env%energy_inactive
2290 56 : i1 = 0; i2 = 0; i3 = 0; i4 = 0
2291 56 : checksum = checksum + ABS(esub)
2292 112 : IF (iw > 0) WRITE (iw, "(ES23.16,4I4)") esub, i1, i2, i3, i4
2293 : END ASSOCIATE
2294 :
2295 : ELSE
2296 : ASSOCIATE (ms2 => active_space_env%multiplicity, &
2297 : nelec => active_space_env%nelec_active)
2298 :
2299 12 : IF (iw > 0) THEN
2300 6 : WRITE (iw, "(A,A,I4,A,I4,A,I2,A)") "&FCI", " NORB=", norb, ",NELEC=", nelec, ",MS2=", ms2, ","
2301 6 : isym = 1
2302 21 : WRITE (iw, "(A,1000(I1,','))") " ORBSYM=", (isym, i=1, norb)
2303 6 : isym = 0
2304 6 : WRITE (iw, "(A,I1,A)") " ISYM=", isym, ","
2305 6 : WRITE (iw, "(A,I1,A)") " UHF=", 1, ","
2306 6 : WRITE (iw, "(A)") " /"
2307 : END IF
2308 : !
2309 : ! Print integrals: ERI
2310 : ! alpha-alpha
2311 : CALL active_space_env%eri%eri_foreach(1, active_space_env%active_orbitals, &
2312 12 : eri_fcidump_print(iw, 1, 1), 1, 1)
2313 12 : CALL eri_checksum%set(1, 1)
2314 12 : CALL active_space_env%eri%eri_foreach(1, active_space_env%active_orbitals, eri_checksum, 1, 1)
2315 : ! alpha-beta
2316 : CALL active_space_env%eri%eri_foreach(2, active_space_env%active_orbitals, &
2317 12 : eri_fcidump_print(iw, 1, norb + 1), 1, 2)
2318 12 : CALL eri_checksum%set(1, norb + 1)
2319 12 : CALL active_space_env%eri%eri_foreach(2, active_space_env%active_orbitals, eri_checksum, 1, 2)
2320 : ! beta-beta
2321 : CALL active_space_env%eri%eri_foreach(3, active_space_env%active_orbitals, &
2322 12 : eri_fcidump_print(iw, norb + 1, norb + 1), 2, 2)
2323 12 : CALL eri_checksum%set(norb + 1, norb + 1)
2324 12 : CALL active_space_env%eri%eri_foreach(3, active_space_env%active_orbitals, eri_checksum, 2, 2)
2325 : ! Print integrals: Fij
2326 : ! alpha
2327 12 : nmo = active_space_env%eri%norb
2328 48 : ALLOCATE (fmat(nmo, nmo))
2329 12 : CALL replicate_and_symmetrize_matrix(nmo, active_space_env%fock_sub(1), fmat)
2330 12 : IF (iw > 0) THEN
2331 6 : i3 = 0; i4 = 0
2332 21 : DO m1 = 1, norb
2333 15 : i1 = active_space_env%active_orbitals(m1, 1)
2334 48 : DO m2 = m1, norb
2335 27 : i2 = active_space_env%active_orbitals(m2, 1)
2336 27 : checksum = checksum + ABS(fmat(i1, i2))
2337 42 : WRITE (iw, "(ES23.16,4I4)") fmat(i1, i2), m1, m2, i3, i4
2338 : END DO
2339 : END DO
2340 : END IF
2341 12 : DEALLOCATE (fmat)
2342 : ! beta
2343 48 : ALLOCATE (fmat(nmo, nmo))
2344 12 : CALL replicate_and_symmetrize_matrix(nmo, active_space_env%fock_sub(2), fmat)
2345 12 : IF (iw > 0) THEN
2346 6 : i3 = 0; i4 = 0
2347 21 : DO m1 = 1, SIZE(active_space_env%active_orbitals, 1)
2348 15 : i1 = active_space_env%active_orbitals(m1, 2)
2349 48 : DO m2 = m1, SIZE(active_space_env%active_orbitals, 1)
2350 27 : i2 = active_space_env%active_orbitals(m2, 2)
2351 27 : checksum = checksum + ABS(fmat(i1, i2))
2352 42 : WRITE (iw, "(ES23.16,4I4)") fmat(i1, i2), m1 + norb, m2 + norb, i3, i4
2353 : END DO
2354 : END DO
2355 : END IF
2356 12 : DEALLOCATE (fmat)
2357 : ! Print energy
2358 12 : esub = active_space_env%energy_inactive
2359 12 : i1 = 0; i2 = 0; i3 = 0; i4 = 0
2360 12 : checksum = checksum + ABS(esub)
2361 24 : IF (iw > 0) WRITE (iw, "(ES23.16,4I4)") esub, i1, i2, i3, i4
2362 : END ASSOCIATE
2363 : END IF
2364 : !
2365 68 : CALL cp_print_key_finished_output(iw, logger, as_input, "FCIDUMP")
2366 :
2367 : !>>
2368 68 : iw = cp_logger_get_default_io_unit(logger)
2369 68 : IF (iw > 0) WRITE (iw, '(T4,A,T66,F12.8)') "FCIDUMP| Checksum:", eri_checksum%checksum + checksum
2370 : !<<
2371 :
2372 136 : END SUBROUTINE fcidump
2373 :
2374 : ! **************************************************************************************************
2375 : !> \brief replicate and symmetrize a matrix
2376 : !> \param norb the number of orbitals
2377 : !> \param distributed_matrix ...
2378 : !> \param replicated_matrix ...
2379 : ! **************************************************************************************************
2380 244 : SUBROUTINE replicate_and_symmetrize_matrix(norb, distributed_matrix, replicated_matrix)
2381 : INTEGER, INTENT(IN) :: norb
2382 : TYPE(cp_fm_type), INTENT(IN) :: distributed_matrix
2383 : REAL(dp), DIMENSION(:, :), INTENT(INOUT) :: replicated_matrix
2384 :
2385 : INTEGER :: i1, i2
2386 : REAL(dp) :: mval
2387 :
2388 4156 : replicated_matrix(:, :) = 0.0_dp
2389 1038 : DO i1 = 1, norb
2390 2994 : DO i2 = i1, norb
2391 1956 : CALL cp_fm_get_element(distributed_matrix, i1, i2, mval)
2392 1956 : replicated_matrix(i1, i2) = mval
2393 2750 : replicated_matrix(i2, i1) = mval
2394 : END DO
2395 : END DO
2396 244 : END SUBROUTINE replicate_and_symmetrize_matrix
2397 :
2398 : ! **************************************************************************************************
2399 : !> \brief Calculates active space Fock matrix and inactive energy
2400 : !> \param active_space_env ...
2401 : !> \param restricted ...
2402 : !> \par History
2403 : !> 06.2016 created [JGH]
2404 : ! **************************************************************************************************
2405 68 : SUBROUTINE subspace_fock_matrix(active_space_env, restricted)
2406 :
2407 : TYPE(active_space_type), POINTER :: active_space_env
2408 : LOGICAL, INTENT(IN) :: restricted
2409 :
2410 : INTEGER :: i1, i2, is, norb, nspins
2411 : REAL(KIND=dp) :: eeri, eref, esub, mval
2412 68 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: ks_a_mat, ks_a_ref, ks_b_mat, ks_b_ref, &
2413 68 : ks_mat, ks_ref, p_a_mat, p_b_mat, p_mat
2414 : TYPE(cp_fm_type), POINTER :: matrix, mo_coef
2415 : TYPE(dbcsr_csr_type), POINTER :: eri, eri_aa, eri_ab, eri_bb
2416 :
2417 68 : eref = active_space_env%energy_ref
2418 68 : nspins = active_space_env%nspins
2419 :
2420 68 : IF (nspins == 1) THEN
2421 54 : CALL get_mo_set(active_space_env%mos_active(1), nmo=norb, mo_coeff=mo_coef)
2422 : !
2423 : ! Loop over ERI, calculate subspace HF energy and Fock matrix
2424 : !
2425 : ! replicate KS, Core, and P matrices
2426 540 : ALLOCATE (ks_mat(norb, norb), ks_ref(norb, norb), p_mat(norb, norb))
2427 810 : ks_ref = 0.0_dp
2428 :
2429 : ! ks_mat contains the KS/Fock matrix (of full density) projected onto the AS MO subspace (f_ref in eq. 19)
2430 54 : CALL replicate_and_symmetrize_matrix(norb, active_space_env%ks_sub(1), ks_mat)
2431 54 : CALL replicate_and_symmetrize_matrix(norb, active_space_env%p_active(1), p_mat)
2432 :
2433 : ! compute ks_ref = V_H[rho^A] + V_HFX[rho^A]
2434 54 : eri => active_space_env%eri%eri(1)%csr_mat
2435 : CALL build_subspace_fock_matrix(active_space_env%active_orbitals, eri, p_mat, ks_ref, &
2436 54 : active_space_env%eri%comm_exchange)
2437 :
2438 : ! compute eeri = E_H[rho^A] + E_HFX[rho^A] as
2439 : ! eeri = 1/2 * (SUM_pq (V_H[rho^A] + V_HFX[rho^A])_pq * D^A_pq)
2440 810 : eeri = 0.5_dp*SUM(ks_ref*p_mat)
2441 :
2442 : ! now calculate the inactive energy acoording to eq. 19, that is
2443 : ! esub = E^I = E_ref - f_ref .* D^A + E_H[rho^A] + E_HFX[rho^A]
2444 : ! where f^ref = ks_mat, which is the KS/Fock matrix in MO basis, transformed previously
2445 : ! and is equal to ks_mat = h^0 + V_core + V_H[rho] + V_HFX[rho]
2446 810 : esub = eref - SUM(ks_mat(1:norb, 1:norb)*p_mat(1:norb, 1:norb)) + eeri
2447 :
2448 : ! reuse ks_mat to store f^I = f^ref - (V_H[rho^A] + V_HFX[rho^A]) according to eq. 20
2449 810 : ks_mat(1:norb, 1:norb) = ks_mat(1:norb, 1:norb) - ks_ref(1:norb, 1:norb)
2450 : ! this is now the embedding potential for the AS calculation!
2451 :
2452 54 : active_space_env%energy_inactive = esub
2453 :
2454 54 : CALL cp_fm_release(active_space_env%fock_sub)
2455 216 : ALLOCATE (active_space_env%fock_sub(nspins))
2456 108 : DO is = 1, nspins
2457 54 : matrix => active_space_env%ks_sub(is)
2458 : CALL cp_fm_create(active_space_env%fock_sub(is), matrix%matrix_struct, &
2459 108 : name="Active Fock operator")
2460 : END DO
2461 54 : matrix => active_space_env%fock_sub(1)
2462 270 : DO i1 = 1, norb
2463 810 : DO i2 = 1, norb
2464 594 : mval = ks_mat(i1, i2)
2465 756 : CALL cp_fm_set_element(matrix, i1, i2, mval)
2466 : END DO
2467 : END DO
2468 : ELSE
2469 :
2470 14 : CALL get_mo_set(active_space_env%mos_active(1), nmo=norb)
2471 : !
2472 : ! Loop over ERI, calculate subspace HF energy and Fock matrix
2473 : !
2474 : ! replicate KS, Core, and P matrices
2475 : ALLOCATE (ks_a_mat(norb, norb), ks_b_mat(norb, norb), &
2476 : & ks_a_ref(norb, norb), ks_b_ref(norb, norb), &
2477 266 : & p_a_mat(norb, norb), p_b_mat(norb, norb))
2478 580 : ks_a_ref(:, :) = 0.0_dp; ks_b_ref(:, :) = 0.0_dp
2479 :
2480 14 : CALL replicate_and_symmetrize_matrix(norb, active_space_env%p_active(1), p_a_mat)
2481 14 : CALL replicate_and_symmetrize_matrix(norb, active_space_env%p_active(2), p_b_mat)
2482 14 : CALL replicate_and_symmetrize_matrix(norb, active_space_env%ks_sub(1), ks_a_mat)
2483 14 : CALL replicate_and_symmetrize_matrix(norb, active_space_env%ks_sub(2), ks_b_mat)
2484 : !
2485 : !
2486 14 : IF (restricted) THEN
2487 : ! In the restricted case, we use the same ERIs for each spin channel
2488 2 : eri_aa => active_space_env%eri%eri(1)%csr_mat
2489 : CALL build_subspace_spin_fock_matrix(active_space_env%active_orbitals, eri_aa, eri_aa, p_a_mat, p_b_mat, ks_a_ref, &
2490 2 : tr_mixed_eri=.FALSE., comm_exchange=active_space_env%eri%comm_exchange)
2491 : CALL build_subspace_spin_fock_matrix(active_space_env%active_orbitals, eri_aa, eri_aa, p_b_mat, p_a_mat, ks_b_ref, &
2492 2 : tr_mixed_eri=.TRUE., comm_exchange=active_space_env%eri%comm_exchange)
2493 : ELSE
2494 12 : eri_aa => active_space_env%eri%eri(1)%csr_mat
2495 12 : eri_ab => active_space_env%eri%eri(2)%csr_mat
2496 12 : eri_bb => active_space_env%eri%eri(3)%csr_mat
2497 : CALL build_subspace_spin_fock_matrix(active_space_env%active_orbitals, eri_aa, eri_ab, p_a_mat, p_b_mat, ks_a_ref, &
2498 12 : tr_mixed_eri=.FALSE., comm_exchange=active_space_env%eri%comm_exchange)
2499 : CALL build_subspace_spin_fock_matrix(active_space_env%active_orbitals, eri_bb, eri_ab, p_b_mat, p_a_mat, ks_b_ref, &
2500 12 : tr_mixed_eri=.TRUE., comm_exchange=active_space_env%eri%comm_exchange)
2501 : END IF
2502 : !
2503 : ! calculate energy
2504 14 : eeri = 0.0_dp
2505 580 : eeri = 0.5_dp*(SUM(ks_a_ref*p_a_mat) + SUM(ks_b_ref*p_b_mat))
2506 580 : esub = eref - SUM(ks_a_mat*p_a_mat) - SUM(ks_b_mat*p_b_mat) + eeri
2507 290 : ks_a_mat(:, :) = ks_a_mat(:, :) - ks_a_ref(:, :)
2508 290 : ks_b_mat(:, :) = ks_b_mat(:, :) - ks_b_ref(:, :)
2509 : !
2510 14 : active_space_env%energy_inactive = esub
2511 : !
2512 14 : CALL cp_fm_release(active_space_env%fock_sub)
2513 70 : ALLOCATE (active_space_env%fock_sub(nspins))
2514 42 : DO is = 1, nspins
2515 28 : matrix => active_space_env%ks_sub(is)
2516 : CALL cp_fm_create(active_space_env%fock_sub(is), matrix%matrix_struct, &
2517 42 : name="Active Fock operator")
2518 : END DO
2519 :
2520 14 : matrix => active_space_env%fock_sub(1)
2521 66 : DO i1 = 1, norb
2522 290 : DO i2 = 1, norb
2523 224 : mval = ks_a_mat(i1, i2)
2524 276 : CALL cp_fm_set_element(matrix, i1, i2, mval)
2525 : END DO
2526 : END DO
2527 14 : matrix => active_space_env%fock_sub(2)
2528 80 : DO i1 = 1, norb
2529 290 : DO i2 = 1, norb
2530 224 : mval = ks_b_mat(i1, i2)
2531 276 : CALL cp_fm_set_element(matrix, i1, i2, mval)
2532 : END DO
2533 : END DO
2534 :
2535 : END IF
2536 :
2537 68 : END SUBROUTINE subspace_fock_matrix
2538 :
2539 : ! **************************************************************************************************
2540 : !> \brief build subspace fockian
2541 : !> \param active_orbitals the active orbital indices
2542 : !> \param eri two electon integrals in MO
2543 : !> \param p_mat density matrix
2544 : !> \param ks_ref fockian matrix
2545 : !> \param comm_exchange ...
2546 : ! **************************************************************************************************
2547 54 : SUBROUTINE build_subspace_fock_matrix(active_orbitals, eri, p_mat, ks_ref, comm_exchange)
2548 : INTEGER, DIMENSION(:, :), INTENT(IN) :: active_orbitals
2549 : TYPE(dbcsr_csr_type), INTENT(IN) :: eri
2550 : REAL(dp), DIMENSION(:, :), INTENT(IN) :: p_mat
2551 : REAL(dp), DIMENSION(:, :), INTENT(INOUT) :: ks_ref
2552 : TYPE(mp_comm_type), INTENT(IN) :: comm_exchange
2553 :
2554 : CHARACTER(LEN=*), PARAMETER :: routineN = 'build_subspace_fock_matrix'
2555 :
2556 : INTEGER :: handle, i1, i12, i12l, i2, i3, i34, &
2557 : i34l, i4, irptr, m1, m2, nindex, &
2558 : nmo_total, norb
2559 : INTEGER, DIMENSION(2) :: irange
2560 : REAL(dp) :: erint
2561 : TYPE(mp_comm_type) :: mp_group
2562 :
2563 54 : CALL timeset(routineN, handle)
2564 :
2565 : ! Nothing to do
2566 54 : norb = SIZE(active_orbitals, 1)
2567 54 : nmo_total = SIZE(p_mat, 1)
2568 54 : nindex = (nmo_total*(nmo_total + 1))/2
2569 54 : CALL mp_group%set_handle(eri%mp_group%get_handle())
2570 54 : irange = get_irange_csr(nindex, comm_exchange)
2571 198 : DO m1 = 1, norb
2572 144 : i1 = active_orbitals(m1, 1)
2573 490 : DO m2 = m1, norb
2574 292 : i2 = active_orbitals(m2, 1)
2575 292 : i12 = csr_idx_to_combined(i1, i2, nmo_total)
2576 436 : IF (i12 >= irange(1) .AND. i12 <= irange(2)) THEN
2577 283 : i12l = i12 - irange(1) + 1
2578 283 : irptr = eri%rowptr_local(i12l) - 1
2579 806 : DO i34l = 1, eri%nzerow_local(i12l)
2580 523 : i34 = eri%colind_local(irptr + i34l)
2581 523 : CALL csr_idx_from_combined(i34, nmo_total, i3, i4)
2582 523 : erint = eri%nzval_local%r_dp(irptr + i34l)
2583 : ! Coulomb
2584 523 : ks_ref(i1, i2) = ks_ref(i1, i2) + erint*p_mat(i3, i4)
2585 523 : IF (i3 /= i4) THEN
2586 278 : ks_ref(i1, i2) = ks_ref(i1, i2) + erint*p_mat(i3, i4)
2587 : END IF
2588 523 : IF (i12 /= i34) THEN
2589 377 : ks_ref(i3, i4) = ks_ref(i3, i4) + erint*p_mat(i1, i2)
2590 377 : IF (i1 /= i2) THEN
2591 244 : ks_ref(i3, i4) = ks_ref(i3, i4) + erint*p_mat(i1, i2)
2592 : END IF
2593 : END IF
2594 : ! Exchange
2595 523 : erint = -0.5_dp*erint
2596 523 : ks_ref(i1, i3) = ks_ref(i1, i3) + erint*p_mat(i2, i4)
2597 523 : IF (i1 /= i2) THEN
2598 318 : ks_ref(i2, i3) = ks_ref(i2, i3) + erint*p_mat(i1, i4)
2599 : END IF
2600 523 : IF (i3 /= i4) THEN
2601 278 : ks_ref(i1, i4) = ks_ref(i1, i4) + erint*p_mat(i2, i3)
2602 : END IF
2603 1329 : IF (i1 /= i2 .AND. i3 /= i4) THEN
2604 219 : ks_ref(i2, i4) = ks_ref(i2, i4) + erint*p_mat(i1, i3)
2605 : END IF
2606 : END DO
2607 : END IF
2608 : END DO
2609 : END DO
2610 : !
2611 198 : DO m1 = 1, norb
2612 144 : i1 = active_orbitals(m1, 1)
2613 490 : DO m2 = m1, norb
2614 292 : i2 = active_orbitals(m2, 1)
2615 436 : ks_ref(i2, i1) = ks_ref(i1, i2)
2616 : END DO
2617 : END DO
2618 1566 : CALL mp_group%sum(ks_ref)
2619 :
2620 54 : CALL timestop(handle)
2621 :
2622 54 : END SUBROUTINE build_subspace_fock_matrix
2623 :
2624 : ! **************************************************************************************************
2625 : !> \brief build subspace fockian for unrestricted spins
2626 : !> \param active_orbitals the active orbital indices
2627 : !> \param eri_aa two electon integrals in MO with parallel spins
2628 : !> \param eri_ab two electon integrals in MO with anti-parallel spins
2629 : !> \param p_a_mat density matrix for up-spin
2630 : !> \param p_b_mat density matrix for down-spin
2631 : !> \param ks_a_ref fockian matrix for up-spin
2632 : !> \param tr_mixed_eri boolean to indicate Coulomb interaction alignment
2633 : !> \param comm_exchange ...
2634 : ! **************************************************************************************************
2635 28 : SUBROUTINE build_subspace_spin_fock_matrix(active_orbitals, eri_aa, eri_ab, p_a_mat, p_b_mat, ks_a_ref, tr_mixed_eri, &
2636 : comm_exchange)
2637 : INTEGER, DIMENSION(:, :), INTENT(IN) :: active_orbitals
2638 : TYPE(dbcsr_csr_type), INTENT(IN) :: eri_aa, eri_ab
2639 : REAL(dp), DIMENSION(:, :), INTENT(IN) :: p_a_mat, p_b_mat
2640 : REAL(dp), DIMENSION(:, :), INTENT(INOUT) :: ks_a_ref
2641 : LOGICAL, INTENT(IN) :: tr_mixed_eri
2642 : TYPE(mp_comm_type), INTENT(IN) :: comm_exchange
2643 :
2644 : CHARACTER(LEN=*), PARAMETER :: routineN = 'build_subspace_spin_fock_matrix'
2645 :
2646 : INTEGER :: handle, i1, i12, i12l, i2, i3, i34, &
2647 : i34l, i4, irptr, m1, m2, nindex, &
2648 : nmo_total, norb, spin1, spin2
2649 : INTEGER, DIMENSION(2) :: irange
2650 : REAL(dp) :: erint
2651 : TYPE(mp_comm_type) :: mp_group
2652 :
2653 28 : CALL timeset(routineN, handle)
2654 :
2655 28 : norb = SIZE(active_orbitals, 1)
2656 28 : nmo_total = SIZE(p_a_mat, 1)
2657 28 : nindex = (nmo_total*(nmo_total + 1))/2
2658 28 : irange = get_irange_csr(nindex, comm_exchange)
2659 28 : IF (tr_mixed_eri) THEN
2660 : spin1 = 2
2661 28 : spin2 = 1
2662 : ELSE
2663 14 : spin1 = 1
2664 14 : spin2 = 2
2665 : END IF
2666 96 : DO m1 = 1, norb
2667 68 : i1 = active_orbitals(m1, spin1)
2668 216 : DO m2 = m1, norb
2669 120 : i2 = active_orbitals(m2, spin1)
2670 120 : i12 = csr_idx_to_combined(i1, i2, nmo_total)
2671 188 : IF (i12 >= irange(1) .AND. i12 <= irange(2)) THEN
2672 120 : i12l = i12 - irange(1) + 1
2673 120 : irptr = eri_aa%rowptr_local(i12l) - 1
2674 281 : DO i34l = 1, eri_aa%nzerow_local(i12l)
2675 161 : i34 = eri_aa%colind_local(irptr + i34l)
2676 161 : CALL csr_idx_from_combined(i34, nmo_total, i3, i4)
2677 161 : erint = eri_aa%nzval_local%r_dp(irptr + i34l)
2678 : ! Coulomb
2679 : !F_ij += (ij|kl)*d_kl
2680 161 : ks_a_ref(i1, i2) = ks_a_ref(i1, i2) + erint*p_a_mat(i3, i4)
2681 161 : IF (i12 /= i34) THEN
2682 : !F_kl += (ij|kl)*d_ij
2683 101 : ks_a_ref(i3, i4) = ks_a_ref(i3, i4) + erint*p_a_mat(i1, i2)
2684 : END IF
2685 : ! Exchange
2686 161 : erint = -1.0_dp*erint
2687 : !F_ik -= (ij|kl)*d_jl
2688 161 : ks_a_ref(i1, i3) = ks_a_ref(i1, i3) + erint*p_a_mat(i2, i4)
2689 161 : IF (i1 /= i2) THEN
2690 : !F_jk -= (ij|kl)*d_il
2691 75 : ks_a_ref(i2, i3) = ks_a_ref(i2, i3) + erint*p_a_mat(i1, i4)
2692 : END IF
2693 161 : IF (i3 /= i4) THEN
2694 : !F_il -= (ij|kl)*d_jk
2695 70 : ks_a_ref(i1, i4) = ks_a_ref(i1, i4) + erint*p_a_mat(i2, i3)
2696 : END IF
2697 442 : IF (i1 /= i2 .AND. i3 /= i4) THEN
2698 : !F_jl -= (ij|kl)*d_ik
2699 44 : ks_a_ref(i2, i4) = ks_a_ref(i2, i4) + erint*p_a_mat(i1, i3)
2700 : END IF
2701 : END DO
2702 : END IF
2703 : END DO
2704 : END DO
2705 : !
2706 :
2707 28 : irange = get_irange_csr(nindex, comm_exchange)
2708 96 : DO m1 = 1, norb
2709 68 : i1 = active_orbitals(m1, 1)
2710 216 : DO m2 = m1, norb
2711 120 : i2 = active_orbitals(m2, 1)
2712 120 : i12 = csr_idx_to_combined(i1, i2, nmo_total)
2713 188 : IF (i12 >= irange(1) .AND. i12 <= irange(2)) THEN
2714 120 : i12l = i12 - irange(1) + 1
2715 120 : irptr = eri_ab%rowptr_local(i12l) - 1
2716 372 : DO i34l = 1, eri_ab%nzerow_local(i12l)
2717 252 : i34 = eri_ab%colind_local(irptr + i34l)
2718 252 : CALL csr_idx_from_combined(i34, nmo_total, i3, i4)
2719 252 : erint = eri_ab%nzval_local%r_dp(irptr + i34l)
2720 : ! Coulomb
2721 372 : IF (tr_mixed_eri) THEN
2722 : !F_kl += (kl beta|ij alpha )*d_alpha_ij
2723 126 : ks_a_ref(i3, i4) = ks_a_ref(i3, i4) + erint*p_b_mat(i1, i2)
2724 : ELSE
2725 : !F_ij += (ij alpha|kl beta )*d_beta_kl
2726 126 : ks_a_ref(i1, i2) = ks_a_ref(i1, i2) + erint*p_b_mat(i3, i4)
2727 : END IF
2728 : END DO
2729 : END IF
2730 : END DO
2731 : END DO
2732 : !
2733 96 : DO m1 = 1, norb
2734 68 : i1 = active_orbitals(m1, spin1)
2735 216 : DO m2 = m1, norb
2736 120 : i2 = active_orbitals(m2, spin1)
2737 188 : ks_a_ref(i2, i1) = ks_a_ref(i1, i2)
2738 : END DO
2739 : END DO
2740 28 : CALL mp_group%set_handle(eri_aa%mp_group%get_handle())
2741 1132 : CALL mp_group%sum(ks_a_ref)
2742 :
2743 28 : CALL timestop(handle)
2744 :
2745 28 : END SUBROUTINE build_subspace_spin_fock_matrix
2746 :
2747 : ! **************************************************************************************************
2748 : !> \brief Creates a local basis
2749 : !> \param pro_basis_set ...
2750 : !> \param zval ...
2751 : !> \param ishell ...
2752 : !> \param nshell ...
2753 : !> \param lnam ...
2754 : !> \par History
2755 : !> 05.2016 created [JGH]
2756 : ! **************************************************************************************************
2757 0 : SUBROUTINE create_pro_basis(pro_basis_set, zval, ishell, nshell, lnam)
2758 : TYPE(gto_basis_set_type), POINTER :: pro_basis_set
2759 : INTEGER, INTENT(IN) :: zval, ishell
2760 : INTEGER, DIMENSION(:), INTENT(IN) :: nshell
2761 : CHARACTER(len=*), DIMENSION(:), INTENT(IN) :: lnam
2762 :
2763 0 : CHARACTER(len=6), DIMENSION(:), POINTER :: sym
2764 : INTEGER :: i, l, nj
2765 : INTEGER, DIMENSION(4, 7) :: ne
2766 0 : INTEGER, DIMENSION(:), POINTER :: lq, nq
2767 0 : REAL(KIND=dp), DIMENSION(:), POINTER :: zet
2768 : TYPE(sto_basis_set_type), POINTER :: sto_basis_set
2769 :
2770 0 : CPASSERT(.NOT. ASSOCIATED(pro_basis_set))
2771 0 : NULLIFY (sto_basis_set)
2772 :
2773 : ! electronic configuration
2774 0 : ne = 0
2775 0 : DO l = 1, 4 !lq(1)+1
2776 0 : nj = 2*(l - 1) + 1
2777 0 : DO i = l, 7 ! nq(1)
2778 0 : ne(l, i) = ptable(zval)%e_conv(l - 1) - 2*nj*(i - l)
2779 0 : ne(l, i) = MAX(ne(l, i), 0)
2780 0 : ne(l, i) = MIN(ne(l, i), 2*nj)
2781 : END DO
2782 : END DO
2783 0 : ALLOCATE (nq(ishell), lq(ishell), zet(ishell), sym(ishell))
2784 0 : DO i = 1, ishell
2785 0 : nq(i) = nshell(i)
2786 0 : SELECT CASE (lnam(i))
2787 : CASE ('S', 's')
2788 0 : lq(i) = 0
2789 : CASE ('P', 'p')
2790 0 : lq(i) = 1
2791 : CASE ('D', 'd')
2792 0 : lq(i) = 2
2793 : CASE ('F', 'f')
2794 0 : lq(i) = 3
2795 : CASE DEFAULT
2796 0 : CPABORT("Wrong l QN")
2797 : END SELECT
2798 0 : sym(i) = lnam(i)
2799 0 : zet(i) = srules(zval, ne, nq(1), lq(1))
2800 : END DO
2801 0 : CALL allocate_sto_basis_set(sto_basis_set)
2802 0 : CALL set_sto_basis_set(sto_basis_set, nshell=1, nq=nq, lq=lq, zet=zet, symbol=sym)
2803 0 : CALL create_gto_from_sto_basis(sto_basis_set, pro_basis_set, 6)
2804 0 : pro_basis_set%norm_type = 2
2805 0 : CALL init_orb_basis_set(pro_basis_set)
2806 0 : CALL deallocate_sto_basis_set(sto_basis_set)
2807 :
2808 0 : END SUBROUTINE create_pro_basis
2809 :
2810 : ! **************************************************************************************************
2811 : !> \brief Update the density matrix in AO basis with the active density contribution
2812 : !> \param active_space_env the active space environment
2813 : !> \param rho_ao the density matrix in AO basis
2814 : ! **************************************************************************************************
2815 0 : SUBROUTINE update_density_ao(active_space_env, rho_ao)
2816 : TYPE(active_space_type), POINTER :: active_space_env
2817 : TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: rho_ao
2818 :
2819 : INTEGER :: ispin, nao, nmo, nspins
2820 : TYPE(cp_fm_type) :: R, U
2821 : TYPE(cp_fm_type), POINTER :: C_active, p_active_mo
2822 : TYPE(dbcsr_type), POINTER :: p_inactive_ao
2823 0 : TYPE(mo_set_type), DIMENSION(:), POINTER :: mos_active
2824 :
2825 : ! Transform the AS density matrix P_MO to the atomic orbital basis,
2826 : ! this is simply C * P_MO * C^T
2827 0 : nspins = active_space_env%nspins
2828 0 : mos_active => active_space_env%mos_active
2829 0 : DO ispin = 1, nspins
2830 : ! size of p_inactive_ao is (nao x nao)
2831 0 : p_inactive_ao => active_space_env%pmat_inactive(ispin)%matrix
2832 :
2833 : ! copy p_inactive_ao to rho_ao
2834 0 : CALL dbcsr_copy(rho_ao(ispin)%matrix, p_inactive_ao)
2835 :
2836 : ! size of p_active_mo is (nmo x nmo)
2837 0 : p_active_mo => active_space_env%p_active(ispin)
2838 :
2839 : ! calculate R = p_mo
2840 0 : CALL cp_fm_create(R, p_active_mo%matrix_struct)
2841 0 : CALL cp_fm_to_fm(p_active_mo, R)
2842 :
2843 : ! calculate U = C * p_mo
2844 0 : CALL get_mo_set(mos_active(ispin), mo_coeff=C_active, nao=nao, nmo=nmo)
2845 0 : CALL cp_fm_create(U, C_active%matrix_struct)
2846 0 : CALL parallel_gemm("N", "N", nao, nmo, nmo, 1.0_dp, C_active, R, 0.0_dp, U)
2847 :
2848 : CALL cp_dbcsr_plus_fm_fm_t(sparse_matrix=rho_ao(ispin)%matrix, &
2849 0 : matrix_v=U, matrix_g=C_active, ncol=nmo, alpha=1.0_dp)
2850 :
2851 0 : CALL cp_fm_release(R)
2852 0 : CALL cp_fm_release(U)
2853 : END DO
2854 :
2855 0 : END SUBROUTINE update_density_ao
2856 :
2857 : ! **************************************************************************************************
2858 : !> \brief Print each value on the master node
2859 : !> \param this object reference
2860 : !> \param i i-index
2861 : !> \param j j-index
2862 : !> \param k k-index
2863 : !> \param l l-index
2864 : !> \param val value of the integral at (i,j,k.l)
2865 : !> \return always true to dump all integrals
2866 : ! **************************************************************************************************
2867 1600 : LOGICAL FUNCTION eri_fcidump_print_func(this, i, j, k, l, val) RESULT(cont)
2868 : CLASS(eri_fcidump_print), INTENT(inout) :: this
2869 : INTEGER, INTENT(in) :: i, j, k, l
2870 : REAL(KIND=dp), INTENT(in) :: val
2871 :
2872 : ! write to the actual file only on the master
2873 1600 : IF (this%unit_nr > 0) THEN
2874 800 : WRITE (this%unit_nr, "(ES23.16,4I4)") val, i + this%bra_start - 1, j + this%bra_start - 1, &
2875 1600 : & k + this%ket_start - 1, l + this%ket_start - 1
2876 : END IF
2877 :
2878 1600 : cont = .TRUE.
2879 1600 : END FUNCTION eri_fcidump_print_func
2880 :
2881 : ! **************************************************************************************************
2882 : !> \brief checksum each value on the master node
2883 : !> \param this object reference
2884 : !> \param i i-index
2885 : !> \param j j-index
2886 : !> \param k k-index
2887 : !> \param l l-index
2888 : !> \param val value of the integral at (i,j,k.l)
2889 : !> \return always true to dump all integrals
2890 : ! **************************************************************************************************
2891 1600 : LOGICAL FUNCTION eri_fcidump_checksum_func(this, i, j, k, l, val) RESULT(cont)
2892 : CLASS(eri_fcidump_checksum), INTENT(inout) :: this
2893 : INTEGER, INTENT(in) :: i, j, k, l
2894 : REAL(KIND=dp), INTENT(in) :: val
2895 : MARK_USED(i)
2896 : MARK_USED(j)
2897 : MARK_USED(k)
2898 : MARK_USED(l)
2899 :
2900 1600 : this%checksum = this%checksum + ABS(val)
2901 :
2902 1600 : cont = .TRUE.
2903 1600 : END FUNCTION eri_fcidump_checksum_func
2904 :
2905 : ! **************************************************************************************************
2906 : !> \brief Update active space density matrix from a fortran array
2907 : !> \param p_act_mo density matrix in active space MO basis
2908 : !> \param active_space_env active space environment
2909 : !> \param ispin spin index
2910 : !> \author Vladimir Rybkin
2911 : ! **************************************************************************************************
2912 0 : SUBROUTINE update_active_density(p_act_mo, active_space_env, ispin)
2913 : REAL(KIND=dp), DIMENSION(:) :: p_act_mo
2914 : TYPE(active_space_type), POINTER :: active_space_env
2915 : INTEGER :: ispin
2916 :
2917 : INTEGER :: i1, i2, m1, m2, nmo_active
2918 : REAL(KIND=dp) :: alpha, pij_new, pij_old
2919 : TYPE(cp_fm_type), POINTER :: p_active
2920 :
2921 0 : p_active => active_space_env%p_active(ispin)
2922 0 : nmo_active = active_space_env%nmo_active
2923 0 : alpha = active_space_env%alpha
2924 :
2925 0 : DO i1 = 1, nmo_active
2926 0 : m1 = active_space_env%active_orbitals(i1, ispin)
2927 0 : DO i2 = 1, nmo_active
2928 0 : m2 = active_space_env%active_orbitals(i2, ispin)
2929 0 : CALL cp_fm_get_element(p_active, m1, m2, pij_old)
2930 0 : pij_new = p_act_mo(i2 + (i1 - 1)*nmo_active)
2931 0 : pij_new = alpha*pij_new + (1.0_dp - alpha)*pij_old
2932 0 : CALL cp_fm_set_element(p_active, m1, m2, pij_new)
2933 : END DO
2934 : END DO
2935 :
2936 0 : END SUBROUTINE update_active_density
2937 :
2938 : ! **************************************************************************************************
2939 : !> \brief Compute and print the AS rdm and the natural orbitals occupation numbers
2940 : !> \param active_space_env active space environment
2941 : !> \param iw output unit
2942 : !> \author Stefano Battaglia
2943 : ! **************************************************************************************************
2944 0 : SUBROUTINE print_pmat_noon(active_space_env, iw)
2945 : TYPE(active_space_type), POINTER :: active_space_env
2946 : INTEGER :: iw
2947 :
2948 : INTEGER :: i1, i2, ii, ispin, jm, m1, m2, &
2949 : nmo_active, nspins
2950 0 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: noon, pmat
2951 : TYPE(cp_fm_type), POINTER :: p_active
2952 :
2953 0 : nspins = active_space_env%nspins
2954 0 : nmo_active = active_space_env%nmo_active
2955 :
2956 0 : ALLOCATE (noon(nmo_active, nspins))
2957 0 : ALLOCATE (pmat(nmo_active, nmo_active))
2958 :
2959 0 : DO ispin = 1, nspins
2960 0 : p_active => active_space_env%p_active(ispin)
2961 0 : noon(:, ispin) = 0.0_dp
2962 0 : pmat = 0.0_dp
2963 :
2964 0 : DO i1 = 1, nmo_active
2965 0 : m1 = active_space_env%active_orbitals(i1, ispin)
2966 0 : DO i2 = 1, nmo_active
2967 0 : m2 = active_space_env%active_orbitals(i2, ispin)
2968 0 : CALL cp_fm_get_element(p_active, m1, m2, pmat(i1, i2))
2969 : END DO
2970 : END DO
2971 :
2972 0 : IF (iw > 0) THEN
2973 0 : WRITE (iw, '(/,T3,A,I2,A)') "Active space density matrix for spin ", ispin
2974 0 : DO i1 = 1, nmo_active
2975 0 : DO ii = 1, nmo_active, 8
2976 0 : jm = MIN(7, nmo_active - ii)
2977 0 : WRITE (iw, '(T3,6(F9.4))') (pmat(i1, ii + i2), i2=0, jm)
2978 : END DO
2979 : END DO
2980 : END IF
2981 :
2982 : ! diagonalize the density matrix
2983 0 : CALL diamat_all(pmat, noon(:, ispin))
2984 :
2985 0 : IF (iw > 0) THEN
2986 0 : WRITE (iw, '(/,T3,A,I2,A)') "Natural orbitals occupation numbers for spin ", ispin
2987 0 : DO i1 = 1, nmo_active, 8
2988 0 : jm = MIN(7, nmo_active - i1)
2989 : ! noons are stored in ascending order, so reverse-print them
2990 0 : WRITE (iw, '(T3,6(F9.4))') (noon(nmo_active - i1 - i2 + 1, ispin), i2=0, jm)
2991 : END DO
2992 : END IF
2993 :
2994 : END DO
2995 :
2996 0 : DEALLOCATE (noon)
2997 0 : DEALLOCATE (pmat)
2998 :
2999 0 : END SUBROUTINE print_pmat_noon
3000 :
3001 : ! **************************************************************************************************
3002 : !> \brief ...
3003 : !> \param qs_env ...
3004 : !> \param active_space_env ...
3005 : !> \param as_input ...
3006 : ! **************************************************************************************************
3007 0 : SUBROUTINE rsdft_embedding(qs_env, active_space_env, as_input)
3008 : TYPE(qs_environment_type), POINTER :: qs_env
3009 : TYPE(active_space_type), POINTER :: active_space_env
3010 : TYPE(section_vals_type), POINTER :: as_input
3011 :
3012 : CHARACTER(len=*), PARAMETER :: routineN = 'rsdft_embedding'
3013 : INTEGER :: handle
3014 :
3015 : #ifdef __NO_SOCKETS
3016 : CALL timeset(routineN, handle)
3017 : CPABORT("CP2K was compiled with the __NO_SOCKETS option!")
3018 : MARK_USED(qs_env)
3019 : MARK_USED(active_space_env)
3020 : MARK_USED(as_input)
3021 : #else
3022 :
3023 : INTEGER :: iw, client_fd, socket_fd, iter, max_iter
3024 : LOGICAL :: converged, do_scf_embedding, ionode
3025 : REAL(KIND=dp) :: delta_E, energy_corr, energy_new, &
3026 : energy_old, energy_scf, eps_iter, t1, t2
3027 : TYPE(cp_logger_type), POINTER :: logger
3028 0 : TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: rho_ao
3029 0 : TYPE(mo_set_type), DIMENSION(:), POINTER :: mos_active
3030 : TYPE(mp_para_env_type), POINTER :: para_env
3031 : TYPE(qs_energy_type), POINTER :: energy
3032 : TYPE(qs_ks_env_type), POINTER :: ks_env
3033 : TYPE(qs_rho_type), POINTER :: rho
3034 : TYPE(dft_control_type), POINTER :: dft_control
3035 :
3036 0 : CALL timeset(routineN, handle)
3037 :
3038 0 : t1 = m_walltime()
3039 :
3040 0 : logger => cp_get_default_logger()
3041 0 : iw = cp_logger_get_default_io_unit(logger)
3042 :
3043 0 : CALL get_qs_env(qs_env, para_env=para_env, dft_control=dft_control)
3044 0 : ionode = para_env%is_source()
3045 :
3046 : ! get info from the input
3047 0 : CALL section_vals_val_get(as_input, "SCF_EMBEDDING", l_val=do_scf_embedding)
3048 0 : active_space_env%do_scf_embedding = do_scf_embedding
3049 0 : CALL section_vals_val_get(as_input, "MAX_ITER", i_val=max_iter)
3050 0 : IF (max_iter < 0) CPABORT("Specify a non-negative number of max iterations.")
3051 0 : CALL section_vals_val_get(as_input, "EPS_ITER", r_val=eps_iter)
3052 0 : IF (eps_iter < 0.0) CPABORT("Specify a non-negative convergence threshold.")
3053 :
3054 : ! create the socket and wait for the client to connect
3055 0 : CALL initialize_socket(socket_fd, client_fd, as_input, ionode)
3056 0 : CALL para_env%sync()
3057 :
3058 : ! send two-electron integrals to the client
3059 0 : CALL send_eri_to_client(client_fd, active_space_env, para_env)
3060 :
3061 : ! get pointer to density in ao basis
3062 0 : CALL get_qs_env(qs_env, rho=rho, energy=energy, ks_env=ks_env)
3063 0 : CALL qs_rho_get(rho, rho_ao=rho_ao)
3064 :
3065 0 : IF (iw > 0) THEN
3066 : WRITE (UNIT=iw, FMT="(/,T2,A,/)") &
3067 0 : "RANGE-SEPARATED DFT EMBEDDING SELF-CONSISTENT OPTIMIZATION"
3068 :
3069 0 : WRITE (iw, '(T3,A,T68,I12)') "Max. iterations", max_iter
3070 0 : WRITE (iw, '(T3,A,T68,E12.4)') "Conv. threshold", eps_iter
3071 0 : WRITE (iw, '(T3,A,T66,F14.2)') "Density damping", active_space_env%alpha
3072 :
3073 : WRITE (UNIT=iw, FMT="(/,T3,A,T11,A,T21,A,T34,A,T55,A,T75,A,/,T3,A)") &
3074 0 : "Iter", "Update", "Time", "Corr. energy", "Total energy", "Change", REPEAT("-", 78)
3075 : END IF
3076 : ! CALL cp_add_iter_level(logger%iter_info, "QS_SCF")
3077 :
3078 0 : iter = 0
3079 0 : converged = .FALSE.
3080 : ! store the scf energy
3081 0 : energy_scf = active_space_env%energy_ref
3082 0 : energy_new = energy_scf
3083 0 : mos_active => active_space_env%mos_active
3084 : ! CALL set_qs_env(qs_env, active_space=active_space_env)
3085 :
3086 : ! start the self-consistent embedding loop
3087 0 : DO WHILE (iter < max_iter)
3088 0 : iter = iter + 1
3089 :
3090 : ! send V_emb and E_ina to the active space solver and update
3091 : ! the active space environment with the new active energy and density
3092 0 : CALL send_fock_to_client(client_fd, active_space_env, para_env)
3093 :
3094 : ! update energies
3095 0 : energy_old = energy_new
3096 0 : energy_new = active_space_env%energy_total
3097 0 : energy_corr = energy_new - energy_scf
3098 0 : delta_E = energy_new - energy_old
3099 :
3100 : ! get timer
3101 0 : t2 = t1
3102 0 : t1 = m_walltime()
3103 : ! print out progress
3104 0 : IF ((iw > 0)) THEN
3105 : WRITE (UNIT=iw, &
3106 : FMT="(T3,I4,T11,A,T19,F6.1,T28,F18.10,T49,F18.10,T70,ES11.2)") &
3107 0 : iter, 'P_Mix', t1 - t2, energy_corr, energy_new, delta_E
3108 0 : CALL m_flush(iw)
3109 : END IF
3110 :
3111 : ! update total density in AO basis with the AS contribution
3112 0 : CALL update_density_ao(active_space_env, rho_ao) ! rho_ao is updated
3113 :
3114 : ! calculate F_ks in AO basis (which contains Vxc) with the new density
3115 0 : CALL qs_rho_update_rho(rho, qs_env=qs_env) ! updates rho_r and rho_g using rho_ao
3116 0 : CALL qs_ks_did_change(qs_env%ks_env, rho_changed=.TRUE.) ! set flags about the change
3117 : ! Re-evaluate the traces between the density matrix and the core Hamiltonians
3118 0 : CALL evaluate_core_matrix_traces(qs_env)
3119 : ! the ks matrix will be rebuilt so this is fine now
3120 : ! CALL set_ks_env(qs_env%ks_env, potential_changed=.FALSE.)
3121 : CALL qs_ks_build_kohn_sham_matrix(qs_env, calculate_forces=.FALSE., &
3122 : just_energy=.FALSE., &
3123 0 : ext_xc_section=active_space_env%xc_section)
3124 :
3125 : ! update the reference energy
3126 0 : active_space_env%energy_ref = energy%total
3127 :
3128 : ! transform KS/Fock, Vxc and Hcore from AO to MO basis
3129 0 : CALL calculate_operators(mos_active, qs_env, active_space_env)
3130 :
3131 : ! calculate the new inactive energy and embedding potential
3132 0 : CALL subspace_fock_matrix(active_space_env, dft_control%roks)
3133 :
3134 : ! check if it is a one-shot correction
3135 0 : IF (.NOT. active_space_env%do_scf_embedding) THEN
3136 0 : IF (iw > 0) THEN
3137 : WRITE (UNIT=iw, FMT="(/,T3,A,I5,A)") &
3138 0 : "*** one-shot embedding correction finished ***"
3139 : END IF
3140 : converged = .TRUE.
3141 : EXIT
3142 : ! check for convergence
3143 0 : ELSEIF (ABS(delta_E) <= eps_iter) THEN
3144 0 : IF (iw > 0) THEN
3145 : WRITE (UNIT=iw, FMT="(/,T3,A,I5,A)") &
3146 0 : "*** rs-DFT embedding run converged in ", iter, " iteration(s) ***"
3147 : END IF
3148 : converged = .TRUE.
3149 : EXIT
3150 : END IF
3151 : END DO
3152 :
3153 : IF (.NOT. converged) THEN
3154 0 : IF (iw > 0) THEN
3155 : WRITE (UNIT=iw, FMT="(/,T3,A,I5,A)") &
3156 0 : "*** rs-DFT embedding did not converged after ", iter, " iteration(s) ***"
3157 : END IF
3158 : END IF
3159 :
3160 : ! update qs total energy to the final rs-DFT energy
3161 0 : energy%total = active_space_env%energy_total
3162 :
3163 : ! print final energy contributions
3164 0 : IF (iw > 0) THEN
3165 : WRITE (UNIT=iw, FMT="(/,T3,A)") &
3166 0 : "Final energy contributions:"
3167 : WRITE (UNIT=iw, FMT="(T6,A,T56,F20.10)") &
3168 0 : "Inactive energy:", active_space_env%energy_inactive
3169 : WRITE (UNIT=iw, FMT="(T6,A,T56,F20.10)") &
3170 0 : "Active energy:", active_space_env%energy_active
3171 : WRITE (UNIT=iw, FMT="(T6,A,T56,F20.10)") &
3172 0 : "Correlation energy:", energy_corr
3173 : WRITE (UNIT=iw, FMT="(T6,A,T56,F20.10)") &
3174 0 : "Total rs-DFT energy:", active_space_env%energy_total
3175 : END IF
3176 :
3177 : ! print the AS rdm and the natural orbital occupation numbers
3178 0 : CALL print_pmat_noon(active_space_env, iw)
3179 :
3180 0 : CALL finalize_socket(socket_fd, client_fd, as_input, ionode)
3181 0 : CALL para_env%sync()
3182 : #endif
3183 :
3184 0 : CALL timestop(handle)
3185 :
3186 0 : END SUBROUTINE rsdft_embedding
3187 :
3188 : #ifndef __NO_SOCKETS
3189 : ! **************************************************************************************************
3190 : !> \brief Creates the socket, spawns the client and connects to it
3191 : !> \param socket_fd the socket file descriptor
3192 : !> \param client_fd the client file descriptor
3193 : !> \param as_input active space inpute section
3194 : !> \param ionode logical flag indicating if the process is the master
3195 : ! **************************************************************************************************
3196 0 : SUBROUTINE initialize_socket(socket_fd, client_fd, as_input, ionode)
3197 : INTEGER, INTENT(OUT) :: socket_fd, client_fd
3198 : TYPE(section_vals_type), INTENT(IN), POINTER :: as_input
3199 : LOGICAL, INTENT(IN) :: ionode
3200 :
3201 : CHARACTER(len=*), PARAMETER :: routineN = 'initialize_socket'
3202 : INTEGER, PARAMETER :: backlog = 10
3203 :
3204 : CHARACTER(len=default_path_length) :: hostname
3205 : INTEGER :: handle, iw, port, protocol
3206 : LOGICAL :: inet
3207 : TYPE(cp_logger_type), POINTER :: logger
3208 :
3209 0 : CALL timeset(routineN, handle)
3210 :
3211 0 : logger => cp_get_default_logger()
3212 0 : iw = cp_logger_get_default_io_unit(logger)
3213 :
3214 : ! protocol == 0 for UNIX, protocol > 0 for INET
3215 0 : CALL section_vals_val_get(as_input, "SOCKET%INET", l_val=inet)
3216 0 : IF (inet) THEN
3217 0 : protocol = 1
3218 : ELSE
3219 0 : protocol = 0
3220 : END IF
3221 0 : CALL section_vals_val_get(as_input, "SOCKET%HOST", c_val=hostname)
3222 0 : CALL section_vals_val_get(as_input, "SOCKET%PORT", i_val=port)
3223 :
3224 0 : IF (ionode) THEN
3225 0 : CALL open_bind_socket(socket_fd, protocol, port, TRIM(hostname)//C_NULL_CHAR)
3226 0 : WRITE (iw, '(/,T2,A,A)') "@SERVER: Created socket with address ", TRIM(hostname)
3227 0 : CALL listen_socket(socket_fd, backlog)
3228 :
3229 : ! wait until a connetion request arrives
3230 0 : WRITE (iw, '(T2,A)') "@SERVER: Waiting for requests..."
3231 0 : CALL accept_socket(socket_fd, client_fd)
3232 0 : WRITE (iw, '(T2,A,I2)') "@SERVER: Accepted socket with fd ", client_fd
3233 : END IF
3234 :
3235 0 : CALL timestop(handle)
3236 :
3237 0 : END SUBROUTINE initialize_socket
3238 :
3239 : ! **************************************************************************************************
3240 : !> \brief Closes the connection to the socket and deletes the file
3241 : !> \param socket_fd the socket file descriptor
3242 : !> \param client_fd the client file descriptor
3243 : !> \param as_input active space inpute section
3244 : !> \param ionode logical flag indicating if the process is the master
3245 : ! **************************************************************************************************
3246 0 : SUBROUTINE finalize_socket(socket_fd, client_fd, as_input, ionode)
3247 : INTEGER, INTENT(IN) :: socket_fd, client_fd
3248 : TYPE(section_vals_type), INTENT(IN), POINTER :: as_input
3249 : LOGICAL, INTENT(IN) :: ionode
3250 :
3251 : CHARACTER(len=*), PARAMETER :: routineN = 'finalize_socket'
3252 : INTEGER, PARAMETER :: header_len = 12
3253 :
3254 : CHARACTER(len=default_path_length) :: hostname
3255 : INTEGER :: handle
3256 :
3257 0 : CALL timeset(routineN, handle)
3258 :
3259 0 : CALL section_vals_val_get(as_input, "SOCKET%HOST", c_val=hostname)
3260 :
3261 0 : IF (ionode) THEN
3262 : ! signal the client to quit
3263 0 : CALL writebuffer(client_fd, "QUIT ", header_len)
3264 : ! close the connection
3265 0 : CALL close_socket(client_fd)
3266 0 : CALL close_socket(socket_fd)
3267 :
3268 : ! delete the socket file
3269 0 : IF (file_exists(TRIM(hostname))) THEN
3270 0 : CALL remove_socket_file(TRIM(hostname)//C_NULL_CHAR)
3271 : END IF
3272 : END IF
3273 :
3274 0 : CALL timestop(handle)
3275 :
3276 0 : END SUBROUTINE finalize_socket
3277 :
3278 : ! **************************************************************************************************
3279 : !> \brief Sends the two-electron integrals to the client vie the socket
3280 : !> \param client_fd the client file descriptor
3281 : !> \param active_space_env active space environment
3282 : !> \param para_env parallel environment
3283 : ! **************************************************************************************************
3284 0 : SUBROUTINE send_eri_to_client(client_fd, active_space_env, para_env)
3285 : INTEGER, INTENT(IN) :: client_fd
3286 : TYPE(active_space_type), INTENT(IN), POINTER :: active_space_env
3287 : TYPE(mp_para_env_type), INTENT(IN), POINTER :: para_env
3288 :
3289 : CHARACTER(len=*), PARAMETER :: routineN = 'send_eri_to_client'
3290 : INTEGER, PARAMETER :: header_len = 12
3291 :
3292 : CHARACTER(len=default_string_length) :: header
3293 : INTEGER :: handle, iw
3294 : LOGICAL :: ionode
3295 0 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: eri_aa, eri_ab, eri_bb, s_ab
3296 : TYPE(cp_logger_type), POINTER :: logger
3297 :
3298 0 : CALL timeset(routineN, handle)
3299 :
3300 0 : logger => cp_get_default_logger()
3301 0 : iw = cp_logger_get_default_io_unit(logger)
3302 0 : ionode = para_env%is_source()
3303 :
3304 0 : ALLOCATE (eri_aa(active_space_env%nmo_active**4))
3305 0 : CALL eri_to_array(active_space_env%eri, eri_aa, active_space_env%active_orbitals, 1, 1)
3306 0 : IF (active_space_env%nspins == 2) THEN
3307 0 : ALLOCATE (eri_ab(active_space_env%nmo_active**4))
3308 0 : CALL eri_to_array(active_space_env%eri, eri_ab, active_space_env%active_orbitals, 1, 2)
3309 0 : ALLOCATE (eri_bb(active_space_env%nmo_active**4))
3310 0 : CALL eri_to_array(active_space_env%eri, eri_bb, active_space_env%active_orbitals, 2, 2)
3311 : ! get the overlap_ab matrix into Fortran array
3312 0 : ALLOCATE (s_ab(active_space_env%nmo_active**2))
3313 : ASSOCIATE (act_indices_a => active_space_env%active_orbitals(:, 1), &
3314 : act_indices_b => active_space_env%active_orbitals(:, 2))
3315 0 : CALL subspace_matrix_to_array(active_space_env%sab_sub(1), s_ab, act_indices_a, act_indices_b)
3316 : END ASSOCIATE
3317 : END IF
3318 :
3319 : ! ask the status of the client
3320 0 : IF (ionode) CALL writebuffer(client_fd, "STATUS ", header_len)
3321 : DO
3322 0 : header = ""
3323 0 : CALL para_env%sync()
3324 0 : IF (ionode) THEN
3325 : ! IF (iw > 0) WRITE(iw, *) "@SERVER: Waiting for messages..."
3326 0 : CALL readbuffer(client_fd, header, header_len)
3327 : END IF
3328 0 : CALL para_env%bcast(header, para_env%source)
3329 :
3330 : ! IF (iw > 0) WRITE(iw, *) "@SERVER: Message from client: ", TRIM(header)
3331 :
3332 0 : IF (TRIM(header) == "READY") THEN
3333 : ! if the client is ready, send the data
3334 0 : CALL para_env%sync()
3335 0 : IF (ionode) THEN
3336 0 : CALL writebuffer(client_fd, "TWOBODY ", header_len)
3337 0 : CALL writebuffer(client_fd, active_space_env%nspins)
3338 0 : CALL writebuffer(client_fd, active_space_env%nmo_active)
3339 0 : CALL writebuffer(client_fd, active_space_env%nelec_active)
3340 0 : CALL writebuffer(client_fd, active_space_env%multiplicity)
3341 : ! send the alpha component
3342 0 : CALL writebuffer(client_fd, eri_aa, SIZE(eri_aa))
3343 : ! send the beta part for unrestricted calculations
3344 0 : IF (active_space_env%nspins == 2) THEN
3345 0 : CALL writebuffer(client_fd, eri_ab, SIZE(eri_ab))
3346 0 : CALL writebuffer(client_fd, eri_bb, SIZE(eri_bb))
3347 0 : CALL writebuffer(client_fd, s_ab, SIZE(s_ab))
3348 : END IF
3349 : END IF
3350 0 : ELSE IF (TRIM(header) == "RECEIVED") THEN
3351 : EXIT
3352 : END IF
3353 : END DO
3354 :
3355 0 : DEALLOCATE (eri_aa)
3356 0 : IF (active_space_env%nspins == 2) THEN
3357 0 : DEALLOCATE (eri_ab)
3358 0 : DEALLOCATE (eri_bb)
3359 0 : DEALLOCATE (s_ab)
3360 : END IF
3361 :
3362 0 : CALL para_env%sync()
3363 :
3364 0 : CALL timestop(handle)
3365 :
3366 0 : END SUBROUTINE send_eri_to_client
3367 :
3368 : ! **************************************************************************************************
3369 : !> \brief Sends the one-electron embedding potential and the inactive energy to the client
3370 : !> \param client_fd the client file descriptor
3371 : !> \param active_space_env active space environment
3372 : !> \param para_env parallel environment
3373 : ! **************************************************************************************************
3374 0 : SUBROUTINE send_fock_to_client(client_fd, active_space_env, para_env)
3375 : INTEGER, INTENT(IN) :: client_fd
3376 : TYPE(active_space_type), INTENT(IN), POINTER :: active_space_env
3377 : TYPE(mp_para_env_type), INTENT(IN), POINTER :: para_env
3378 :
3379 : CHARACTER(len=*), PARAMETER :: routineN = 'send_fock_to_client'
3380 : INTEGER, PARAMETER :: header_len = 12
3381 :
3382 : CHARACTER(len=default_string_length) :: header
3383 : INTEGER :: handle, iw
3384 : LOGICAL :: debug, ionode
3385 0 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: fock_a, fock_b, p_act_mo_a, p_act_mo_b
3386 : TYPE(cp_logger_type), POINTER :: logger
3387 :
3388 0 : CALL timeset(routineN, handle)
3389 :
3390 : ! Set to .TRUE. to activate debug output
3391 0 : debug = .FALSE.
3392 :
3393 0 : logger => cp_get_default_logger()
3394 0 : iw = cp_logger_get_default_io_unit(logger)
3395 0 : ionode = para_env%is_source()
3396 :
3397 0 : ALLOCATE (p_act_mo_a(active_space_env%nmo_active**2))
3398 0 : ALLOCATE (fock_a(active_space_env%nmo_active**2))
3399 0 : IF (active_space_env%nspins == 2) THEN
3400 0 : ALLOCATE (p_act_mo_b(active_space_env%nmo_active**2))
3401 0 : ALLOCATE (fock_b(active_space_env%nmo_active**2))
3402 : END IF
3403 :
3404 : ! get the fock matrix into Fortran arrays
3405 : ASSOCIATE (act_indices => active_space_env%active_orbitals(:, 1))
3406 0 : CALL subspace_matrix_to_array(active_space_env%fock_sub(1), fock_a, act_indices, act_indices)
3407 : END ASSOCIATE
3408 :
3409 0 : IF (active_space_env%nspins == 2) THEN
3410 : ASSOCIATE (act_indices => active_space_env%active_orbitals(:, 2))
3411 0 : CALL subspace_matrix_to_array(active_space_env%fock_sub(2), fock_b, act_indices, act_indices)
3412 : END ASSOCIATE
3413 : END IF
3414 :
3415 : ! ask the status of the client
3416 0 : IF (ionode) CALL writebuffer(client_fd, "STATUS ", header_len)
3417 : DO
3418 0 : header = ""
3419 :
3420 0 : CALL para_env%sync()
3421 0 : IF (ionode) THEN
3422 : IF (debug .AND. iw > 0) WRITE (iw, *) "@SERVER: Waiting for messages..."
3423 0 : CALL readbuffer(client_fd, header, header_len)
3424 : END IF
3425 0 : CALL para_env%bcast(header, para_env%source)
3426 :
3427 : IF (debug .AND. iw > 0) WRITE (iw, *) "@SERVER: Message from client: ", TRIM(header)
3428 :
3429 0 : IF (TRIM(header) == "READY") THEN
3430 : ! if the client is ready, send the data
3431 0 : CALL para_env%sync()
3432 0 : IF (ionode) THEN
3433 0 : CALL writebuffer(client_fd, "ONEBODY ", header_len)
3434 0 : CALL writebuffer(client_fd, active_space_env%energy_inactive)
3435 : ! send the alpha component
3436 0 : CALL writebuffer(client_fd, fock_a, SIZE(fock_a))
3437 : ! send the beta part for unrestricted calculations
3438 0 : IF (active_space_env%nspins == 2) THEN
3439 0 : CALL writebuffer(client_fd, fock_b, SIZE(fock_b))
3440 : END IF
3441 : END IF
3442 :
3443 0 : ELSE IF (TRIM(header) == "HAVEDATA") THEN
3444 : ! qiskit has data to transfer, let them know we want it and wait for it
3445 0 : CALL para_env%sync()
3446 0 : IF (ionode) THEN
3447 : IF (debug .AND. iw > 0) WRITE (iw, *) "@SERVER: Qiskit has data to transfer"
3448 0 : CALL writebuffer(client_fd, "GETDENSITY ", header_len)
3449 :
3450 : ! read the active energy and density
3451 0 : CALL readbuffer(client_fd, active_space_env%energy_active)
3452 0 : CALL readbuffer(client_fd, p_act_mo_a, SIZE(p_act_mo_a))
3453 0 : IF (active_space_env%nspins == 2) THEN
3454 0 : CALL readbuffer(client_fd, p_act_mo_b, SIZE(p_act_mo_b))
3455 : END IF
3456 : END IF
3457 :
3458 : ! broadcast the data to all processors
3459 0 : CALL para_env%bcast(active_space_env%energy_active, para_env%source)
3460 0 : CALL para_env%bcast(p_act_mo_a, para_env%source)
3461 0 : IF (active_space_env%nspins == 2) THEN
3462 0 : CALL para_env%bcast(p_act_mo_b, para_env%source)
3463 : END IF
3464 :
3465 : ! update total and reference energies in active space enviornment
3466 0 : active_space_env%energy_total = active_space_env%energy_inactive + active_space_env%energy_active
3467 :
3468 : ! update the active density matrix in the active space environment
3469 0 : CALL update_active_density(p_act_mo_a, active_space_env, 1)
3470 0 : IF (active_space_env%nspins == 2) THEN
3471 0 : CALL update_active_density(p_act_mo_b, active_space_env, 2)
3472 : END IF
3473 :
3474 : ! the non-iterative part is done, we can continue
3475 : EXIT
3476 : END IF
3477 :
3478 : END DO
3479 :
3480 0 : DEALLOCATE (p_act_mo_a)
3481 0 : DEALLOCATE (fock_a)
3482 0 : IF (active_space_env%nspins == 2) THEN
3483 0 : DEALLOCATE (p_act_mo_b)
3484 0 : DEALLOCATE (fock_b)
3485 : END IF
3486 :
3487 0 : CALL para_env%sync()
3488 :
3489 0 : CALL timestop(handle)
3490 :
3491 0 : END SUBROUTINE send_fock_to_client
3492 : #endif
3493 :
3494 0 : END MODULE qs_active_space_methods
|