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