Line data Source code
1 : !--------------------------------------------------------------------------------------------------!
2 : ! CP2K: A general program to perform molecular dynamics simulations !
3 : ! Copyright 2000-2025 CP2K developers group <https://cp2k.org> !
4 : ! !
5 : ! SPDX-License-Identifier: GPL-2.0-or-later !
6 : !--------------------------------------------------------------------------------------------------!
7 :
8 : ! **************************************************************************************************
9 : !> \brief Routines to calculate EXX within GW
10 : !> \par History
11 : !> 07.2020 separated from mp2.F [F. Stein, code by Jan Wilhelm]
12 : !> 07.2024 determine number of corrected MOs from BSE cutoffs [Maximilian Graml]
13 : !> \author Jan Wilhelm, Frederick Stein
14 : ! **************************************************************************************************
15 : MODULE rpa_gw_sigma_x
16 : USE admm_methods, ONLY: admm_mo_merge_ks_matrix
17 : USE admm_types, ONLY: admm_type,&
18 : get_admm_env
19 : USE bse_util, ONLY: determine_cutoff_indices
20 : USE cp_cfm_basic_linalg, ONLY: cp_cfm_scale_and_add_fm
21 : USE cp_cfm_types, ONLY: cp_cfm_create,&
22 : cp_cfm_get_info,&
23 : cp_cfm_release,&
24 : cp_cfm_type
25 : USE cp_control_types, ONLY: dft_control_type
26 : USE cp_dbcsr_api, ONLY: &
27 : dbcsr_add, dbcsr_copy, dbcsr_create, dbcsr_desymmetrize, dbcsr_multiply, dbcsr_p_type, &
28 : dbcsr_release, dbcsr_release_p, dbcsr_set, dbcsr_type, dbcsr_type_antisymmetric, &
29 : dbcsr_type_symmetric
30 : USE cp_dbcsr_contrib, ONLY: dbcsr_get_diag
31 : USE cp_dbcsr_cp2k_link, ONLY: cp_dbcsr_alloc_block_from_nbl
32 : USE cp_dbcsr_operations, ONLY: copy_dbcsr_to_fm,&
33 : copy_fm_to_dbcsr,&
34 : dbcsr_allocate_matrix_set,&
35 : dbcsr_deallocate_matrix_set
36 : USE cp_files, ONLY: close_file,&
37 : open_file
38 : USE cp_fm_struct, ONLY: cp_fm_struct_type
39 : USE cp_fm_types, ONLY: cp_fm_create,&
40 : cp_fm_get_info,&
41 : cp_fm_release,&
42 : cp_fm_type
43 : USE hfx_energy_potential, ONLY: integrate_four_center
44 : USE hfx_exx, ONLY: calc_exx_admm_xc_contributions,&
45 : exx_post_hfx,&
46 : exx_pre_hfx
47 : USE hfx_ri, ONLY: hfx_ri_update_ks
48 : USE input_constants, ONLY: do_admm_basis_projection,&
49 : do_admm_purify_none,&
50 : gw_print_exx,&
51 : gw_read_exx,&
52 : xc_none
53 : USE input_section_types, ONLY: section_vals_get,&
54 : section_vals_get_subs_vals,&
55 : section_vals_type,&
56 : section_vals_val_get,&
57 : section_vals_val_set
58 : USE kinds, ONLY: dp
59 : USE kpoint_methods, ONLY: rskp_transform
60 : USE kpoint_types, ONLY: get_kpoint_info,&
61 : kpoint_env_type,&
62 : kpoint_type
63 : USE machine, ONLY: m_walltime
64 : USE mathconstants, ONLY: gaussi,&
65 : z_one,&
66 : z_zero
67 : USE message_passing, ONLY: mp_para_env_type
68 : USE mp2_integrals, ONLY: compute_kpoints
69 : USE mp2_ri_2c, ONLY: trunc_coulomb_for_exchange
70 : USE mp2_types, ONLY: mp2_type
71 : USE parallel_gemm_api, ONLY: parallel_gemm
72 : USE physcon, ONLY: evolt
73 : USE qs_energy_types, ONLY: qs_energy_type
74 : USE qs_environment_types, ONLY: get_qs_env,&
75 : qs_environment_type
76 : USE qs_ks_methods, ONLY: qs_ks_build_kohn_sham_matrix
77 : USE qs_ks_types, ONLY: qs_ks_env_type
78 : USE qs_mo_types, ONLY: get_mo_set,&
79 : mo_set_type
80 : USE qs_neighbor_list_types, ONLY: neighbor_list_set_p_type
81 : USE qs_rho_types, ONLY: qs_rho_get,&
82 : qs_rho_type
83 : USE rpa_gw, ONLY: compute_minus_vxc_kpoints,&
84 : trafo_to_mo_and_kpoints
85 : USE rpa_gw_kpoints_util, ONLY: get_bandstruc_and_k_dependent_MOs
86 :
87 : !$ USE OMP_LIB, ONLY: omp_get_max_threads, omp_get_thread_num, omp_get_num_threads
88 :
89 : #include "./base/base_uses.f90"
90 :
91 : IMPLICIT NONE
92 :
93 : PRIVATE
94 :
95 : CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'rpa_gw_sigma_x'
96 :
97 : PUBLIC :: compute_vec_Sigma_x_minus_vxc_gw
98 :
99 : CONTAINS
100 :
101 : ! **************************************************************************************************
102 : !> \brief ...
103 : !> \param qs_env ...
104 : !> \param mp2_env ...
105 : !> \param mos_mp2 ...
106 : !> \param energy_ex ...
107 : !> \param energy_xc_admm ...
108 : !> \param t3 ...
109 : !> \param unit_nr ...
110 : !> \par History
111 : !> 04.2015 created
112 : !> \author Jan Wilhelm
113 : ! **************************************************************************************************
114 104 : SUBROUTINE compute_vec_Sigma_x_minus_vxc_gw(qs_env, mp2_env, mos_mp2, energy_ex, energy_xc_admm, t3, unit_nr)
115 : TYPE(qs_environment_type), POINTER :: qs_env
116 : TYPE(mp2_type) :: mp2_env
117 : TYPE(mo_set_type), DIMENSION(:), INTENT(IN) :: mos_mp2
118 : REAL(KIND=dp), INTENT(OUT) :: energy_ex, energy_xc_admm(2), t3
119 : INTEGER, INTENT(IN) :: unit_nr
120 :
121 : CHARACTER(len=*), PARAMETER :: routineN = 'compute_vec_Sigma_x_minus_vxc_gw'
122 :
123 : CHARACTER(4) :: occ_virt
124 : CHARACTER(LEN=40) :: line
125 : INTEGER :: dimen, gw_corr_lev_occ, gw_corr_lev_tot, gw_corr_lev_virt, handle, homo, &
126 : homo_reduced_bse, homo_startindex_bse, i_img, ikp, irep, ispin, iunit, max_corr_lev_occ, &
127 : max_corr_lev_virt, myfun, myfun_aux, myfun_prim, n_level_gw, n_level_gw_ref, n_rep_hf, &
128 : nkp, nkp_Sigma, nmo, nspins, print_exx, virtual_reduced_bse, virtual_startindex_bse
129 : LOGICAL :: calc_ints, charge_constrain_tmp, do_admm_rpa, do_hfx, do_kpoints_cubic_RPA, &
130 : do_kpoints_from_Gamma, do_ri_Sigma_x, really_read_line
131 : REAL(KIND=dp) :: E_GAP_GW, E_HOMO_GW, E_LUMO_GW, eh1, ehfx, eigval_dft, eigval_hf_at_dft, &
132 : energy_exc, energy_exc1, energy_exc1_aux_fit, energy_exc_aux_fit, energy_total, &
133 : exx_minus_vxc, hfx_fraction, min_direct_HF_at_DFT_gap, t1, t2, tmp
134 104 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: matrix_tmp_2_diag
135 104 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: Eigenval_kp_HF_at_DFT, vec_Sigma_x
136 104 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :) :: Eigenval_kp, vec_Sigma_x_minus_vxc_gw, &
137 104 : vec_Sigma_x_minus_vxc_gw_im
138 104 : REAL(KIND=dp), DIMENSION(:), POINTER :: mo_eigenvalues
139 : TYPE(admm_type), POINTER :: admm_env
140 : TYPE(cp_fm_type), POINTER :: mo_coeff
141 104 : TYPE(dbcsr_p_type), ALLOCATABLE, DIMENSION(:) :: mat_exchange_for_kp_from_gamma
142 104 : TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_ks, matrix_ks_aux_fit, &
143 104 : matrix_ks_aux_fit_hfx, rho_ao, &
144 104 : rho_ao_aux_fit
145 104 : TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_ks_2d, matrix_ks_kp_im, &
146 104 : matrix_ks_kp_re, matrix_ks_transl, matrix_sigma_x_minus_vxc, matrix_sigma_x_minus_vxc_im, &
147 104 : rho_ao_2d
148 : TYPE(dbcsr_type) :: matrix_tmp, matrix_tmp_2, mo_coeff_b
149 : TYPE(dft_control_type), POINTER :: dft_control
150 : TYPE(kpoint_type), POINTER :: kpoints, kpoints_Sigma
151 : TYPE(mp_para_env_type), POINTER :: para_env
152 : TYPE(qs_energy_type), POINTER :: energy
153 : TYPE(qs_ks_env_type), POINTER :: ks_env
154 : TYPE(qs_rho_type), POINTER :: rho, rho_aux_fit
155 : TYPE(section_vals_type), POINTER :: hfx_sections, input, xc_section, &
156 : xc_section_admm_aux, &
157 : xc_section_admm_prim
158 :
159 104 : NULLIFY (admm_env, matrix_ks, matrix_ks_aux_fit, rho_ao, matrix_sigma_x_minus_vxc, input, &
160 104 : xc_section, xc_section_admm_aux, xc_section_admm_prim, hfx_sections, rho, &
161 104 : dft_control, para_env, ks_env, mo_coeff, matrix_sigma_x_minus_vxc_im, matrix_ks_aux_fit_hfx, &
162 104 : rho_aux_fit, rho_ao_aux_fit)
163 :
164 104 : CALL timeset(routineN, handle)
165 :
166 104 : t1 = m_walltime()
167 :
168 104 : do_admm_rpa = mp2_env%ri_rpa%do_admm
169 104 : do_ri_Sigma_x = mp2_env%ri_g0w0%do_ri_Sigma_x
170 104 : do_kpoints_cubic_RPA = qs_env%mp2_env%ri_rpa_im_time%do_im_time_kpoints
171 104 : do_kpoints_from_Gamma = qs_env%mp2_env%ri_rpa_im_time%do_kpoints_from_Gamma
172 104 : print_exx = mp2_env%ri_g0w0%print_exx
173 :
174 104 : IF (do_kpoints_cubic_RPA) THEN
175 0 : CPASSERT(do_ri_Sigma_x)
176 : END IF
177 :
178 : IF (do_kpoints_cubic_RPA) THEN
179 :
180 : CALL get_qs_env(qs_env, &
181 : admm_env=admm_env, &
182 : matrix_ks_kp=matrix_ks_transl, &
183 : rho=rho, &
184 : input=input, &
185 : dft_control=dft_control, &
186 : para_env=para_env, &
187 : kpoints=kpoints, &
188 : ks_env=ks_env, &
189 0 : energy=energy)
190 0 : nkp = kpoints%nkp
191 :
192 : ELSE
193 :
194 : CALL get_qs_env(qs_env, &
195 : admm_env=admm_env, &
196 : matrix_ks=matrix_ks, &
197 : rho=rho, &
198 : input=input, &
199 : dft_control=dft_control, &
200 : para_env=para_env, &
201 : ks_env=ks_env, &
202 104 : energy=energy)
203 104 : nkp = 1
204 104 : CALL qs_rho_get(rho, rho_ao=rho_ao)
205 :
206 104 : IF (do_admm_rpa) THEN
207 : CALL get_admm_env(admm_env, matrix_ks_aux_fit=matrix_ks_aux_fit, rho_aux_fit=rho_aux_fit, &
208 8 : matrix_ks_aux_fit_hfx=matrix_ks_aux_fit_hfx)
209 8 : CALL qs_rho_get(rho_aux_fit, rho_ao=rho_ao_aux_fit)
210 :
211 : ! RPA/GW with ADMM for EXX or the exchange self-energy only implemented for
212 : ! ADMM_PURIFICATION_METHOD NONE
213 : ! METHOD BASIS_PROJECTION
214 : ! in the admm section
215 8 : CPASSERT(admm_env%purification_method == do_admm_purify_none)
216 8 : CPASSERT(dft_control%admm_control%method == do_admm_basis_projection)
217 : END IF
218 : END IF
219 :
220 104 : nspins = dft_control%nspins
221 :
222 : ! safe ks matrix for later: we will transform matrix_ks
223 : ! to T-cell index and then to k-points for band structure calculation
224 104 : IF (do_kpoints_from_Gamma) THEN
225 : ! not yet there: open shell
226 66 : ALLOCATE (qs_env%mp2_env%ri_g0w0%matrix_ks(nspins))
227 34 : DO ispin = 1, nspins
228 18 : NULLIFY (qs_env%mp2_env%ri_g0w0%matrix_ks(ispin)%matrix)
229 18 : ALLOCATE (qs_env%mp2_env%ri_g0w0%matrix_ks(ispin)%matrix)
230 : CALL dbcsr_create(qs_env%mp2_env%ri_g0w0%matrix_ks(ispin)%matrix, &
231 18 : template=matrix_ks(ispin)%matrix)
232 : CALL dbcsr_desymmetrize(matrix_ks(ispin)%matrix, &
233 34 : qs_env%mp2_env%ri_g0w0%matrix_ks(ispin)%matrix)
234 :
235 : END DO
236 : END IF
237 :
238 104 : IF (do_kpoints_cubic_RPA) THEN
239 :
240 0 : CALL allocate_matrix_ks_kp(matrix_ks_transl, matrix_ks_kp_re, matrix_ks_kp_im, kpoints)
241 0 : CALL transform_matrix_ks_to_kp(matrix_ks_transl, matrix_ks_kp_re, matrix_ks_kp_im, kpoints)
242 :
243 0 : DO ispin = 1, nspins
244 0 : DO i_img = 1, SIZE(matrix_ks_transl, 2)
245 0 : CALL dbcsr_set(matrix_ks_transl(ispin, i_img)%matrix, 0.0_dp)
246 : END DO
247 : END DO
248 :
249 : END IF
250 :
251 : ! initialize matrix_sigma_x_minus_vxc
252 104 : NULLIFY (matrix_sigma_x_minus_vxc)
253 104 : CALL dbcsr_allocate_matrix_set(matrix_sigma_x_minus_vxc, nspins, nkp)
254 104 : IF (do_kpoints_cubic_RPA) THEN
255 0 : NULLIFY (matrix_sigma_x_minus_vxc_im)
256 0 : CALL dbcsr_allocate_matrix_set(matrix_sigma_x_minus_vxc_im, nspins, nkp)
257 : END IF
258 :
259 220 : DO ispin = 1, nspins
260 336 : DO ikp = 1, nkp
261 :
262 232 : IF (do_kpoints_cubic_RPA) THEN
263 :
264 0 : ALLOCATE (matrix_sigma_x_minus_vxc(ispin, ikp)%matrix)
265 : CALL dbcsr_create(matrix_sigma_x_minus_vxc(ispin, ikp)%matrix, &
266 : template=matrix_ks_kp_re(1, 1)%matrix, &
267 0 : matrix_type=dbcsr_type_symmetric)
268 :
269 0 : CALL dbcsr_copy(matrix_sigma_x_minus_vxc(ispin, ikp)%matrix, matrix_ks_kp_re(ispin, ikp)%matrix)
270 0 : CALL dbcsr_set(matrix_ks_kp_re(ispin, ikp)%matrix, 0.0_dp)
271 :
272 0 : ALLOCATE (matrix_sigma_x_minus_vxc_im(ispin, ikp)%matrix)
273 : CALL dbcsr_create(matrix_sigma_x_minus_vxc_im(ispin, ikp)%matrix, &
274 : template=matrix_ks_kp_im(1, 1)%matrix, &
275 0 : matrix_type=dbcsr_type_antisymmetric)
276 :
277 0 : CALL dbcsr_copy(matrix_sigma_x_minus_vxc_im(ispin, ikp)%matrix, matrix_ks_kp_im(ispin, ikp)%matrix)
278 0 : CALL dbcsr_set(matrix_ks_kp_im(ispin, ikp)%matrix, 0.0_dp)
279 :
280 : ELSE
281 :
282 116 : ALLOCATE (matrix_sigma_x_minus_vxc(ispin, ikp)%matrix)
283 : CALL dbcsr_create(matrix_sigma_x_minus_vxc(ispin, ikp)%matrix, &
284 116 : template=matrix_ks(1)%matrix)
285 :
286 116 : CALL dbcsr_copy(matrix_sigma_x_minus_vxc(ispin, ikp)%matrix, matrix_ks(ispin)%matrix)
287 116 : CALL dbcsr_set(matrix_ks(ispin)%matrix, 0.0_dp)
288 :
289 : END IF
290 :
291 : END DO
292 : END DO
293 :
294 : ! set DFT functional to none and hfx_fraction to zero
295 104 : hfx_sections => section_vals_get_subs_vals(input, "DFT%XC%HF")
296 104 : CALL section_vals_get(hfx_sections, explicit=do_hfx)
297 :
298 104 : IF (do_hfx) THEN
299 18 : hfx_fraction = qs_env%x_data(1, 1)%general_parameter%fraction
300 54 : qs_env%x_data(:, :)%general_parameter%fraction = 0.0_dp
301 : END IF
302 104 : xc_section => section_vals_get_subs_vals(input, "DFT%XC")
303 : CALL section_vals_val_get(xc_section, "XC_FUNCTIONAL%_SECTION_PARAMETERS_", &
304 104 : i_val=myfun)
305 : CALL section_vals_val_set(xc_section, "XC_FUNCTIONAL%_SECTION_PARAMETERS_", &
306 104 : i_val=xc_none)
307 :
308 : ! in ADMM, also set the XC functional for ADMM correction to none
309 : ! do not do this if we do ADMM for Sigma_x
310 104 : IF (dft_control%do_admm) THEN
311 : xc_section_admm_aux => section_vals_get_subs_vals(admm_env%xc_section_aux, &
312 8 : "XC_FUNCTIONAL")
313 : CALL section_vals_val_get(xc_section_admm_aux, "_SECTION_PARAMETERS_", &
314 8 : i_val=myfun_aux)
315 : CALL section_vals_val_set(xc_section_admm_aux, "_SECTION_PARAMETERS_", &
316 8 : i_val=xc_none)
317 :
318 : ! the same for the primary basis
319 : xc_section_admm_prim => section_vals_get_subs_vals(admm_env%xc_section_primary, &
320 8 : "XC_FUNCTIONAL")
321 : CALL section_vals_val_get(xc_section_admm_prim, "_SECTION_PARAMETERS_", &
322 8 : i_val=myfun_prim)
323 : CALL section_vals_val_set(xc_section_admm_prim, "_SECTION_PARAMETERS_", &
324 8 : i_val=xc_none)
325 :
326 : ! for ADMMQ/S, set the charge_constrain to false (otherwise wrong results)
327 8 : charge_constrain_tmp = .FALSE.
328 8 : IF (admm_env%charge_constrain) THEN
329 0 : admm_env%charge_constrain = .FALSE.
330 0 : charge_constrain_tmp = .TRUE.
331 : END IF
332 :
333 : END IF
334 :
335 : ! if we do ADMM for Sigma_x, we write the ADMM correction into matrix_ks_aux_fit
336 : ! and therefore we should set it to zero
337 104 : IF (do_admm_rpa) THEN
338 18 : DO ispin = 1, nspins
339 18 : CALL dbcsr_set(matrix_ks_aux_fit(ispin)%matrix, 0.0_dp)
340 : END DO
341 : END IF
342 :
343 104 : IF (.NOT. mp2_env%ri_g0w0%update_xc_energy) THEN
344 78 : energy_total = energy%total
345 78 : energy_exc = energy%exc
346 78 : energy_exc1 = energy%exc1
347 78 : energy_exc_aux_fit = energy%ex
348 78 : energy_exc1_aux_fit = energy%exc_aux_fit
349 78 : energy_ex = energy%exc1_aux_fit
350 : END IF
351 :
352 : ! Remove the Exchange-correlation energy contributions from the total energy
353 : energy%total = energy%total - (energy%exc + energy%exc1 + energy%ex + &
354 104 : energy%exc_aux_fit + energy%exc1_aux_fit)
355 :
356 : ! calculate KS-matrix without XC and without HF
357 : CALL qs_ks_build_kohn_sham_matrix(qs_env=qs_env, calculate_forces=.FALSE., &
358 104 : just_energy=.FALSE.)
359 :
360 104 : IF (.NOT. mp2_env%ri_g0w0%update_xc_energy) THEN
361 78 : energy%exc = energy_exc
362 78 : energy%exc1 = energy_exc1
363 78 : energy%exc_aux_fit = energy_ex
364 78 : energy%exc1_aux_fit = energy_exc_aux_fit
365 78 : energy%ex = energy_exc1_aux_fit
366 78 : energy%total = energy_total
367 : END IF
368 :
369 : ! set the DFT functional and HF fraction back
370 : CALL section_vals_val_set(xc_section, "XC_FUNCTIONAL%_SECTION_PARAMETERS_", &
371 104 : i_val=myfun)
372 104 : IF (do_hfx) THEN
373 54 : qs_env%x_data(:, :)%general_parameter%fraction = hfx_fraction
374 : END IF
375 :
376 104 : IF (dft_control%do_admm) THEN
377 : xc_section_admm_aux => section_vals_get_subs_vals(admm_env%xc_section_aux, &
378 8 : "XC_FUNCTIONAL")
379 : xc_section_admm_prim => section_vals_get_subs_vals(admm_env%xc_section_primary, &
380 8 : "XC_FUNCTIONAL")
381 :
382 : CALL section_vals_val_set(xc_section_admm_aux, "_SECTION_PARAMETERS_", &
383 8 : i_val=myfun_aux)
384 : CALL section_vals_val_set(xc_section_admm_prim, "_SECTION_PARAMETERS_", &
385 8 : i_val=myfun_prim)
386 8 : IF (charge_constrain_tmp) THEN
387 0 : admm_env%charge_constrain = .TRUE.
388 : END IF
389 : END IF
390 :
391 104 : IF (do_kpoints_cubic_RPA) THEN
392 0 : CALL transform_matrix_ks_to_kp(matrix_ks_transl, matrix_ks_kp_re, matrix_ks_kp_im, kpoints)
393 : END IF
394 :
395 : ! remove the single-particle part (kin. En + Hartree pot) and change the sign
396 220 : DO ispin = 1, nspins
397 220 : IF (do_kpoints_cubic_RPA) THEN
398 0 : DO ikp = 1, nkp
399 0 : CALL dbcsr_add(matrix_sigma_x_minus_vxc(ispin, ikp)%matrix, matrix_ks_kp_re(ispin, ikp)%matrix, -1.0_dp, 1.0_dp)
400 0 : CALL dbcsr_add(matrix_sigma_x_minus_vxc_im(ispin, ikp)%matrix, matrix_ks_kp_im(ispin, ikp)%matrix, -1.0_dp, 1.0_dp)
401 : END DO
402 : ELSE
403 116 : CALL dbcsr_add(matrix_sigma_x_minus_vxc(ispin, 1)%matrix, matrix_ks(ispin)%matrix, -1.0_dp, 1.0_dp)
404 : END IF
405 : END DO
406 :
407 104 : IF (do_kpoints_cubic_RPA) THEN
408 :
409 : CALL transform_sigma_x_minus_vxc_to_MO_basis(kpoints, matrix_sigma_x_minus_vxc, &
410 : matrix_sigma_x_minus_vxc_im, &
411 : vec_Sigma_x_minus_vxc_gw, &
412 : vec_Sigma_x_minus_vxc_gw_im, &
413 0 : para_env, nmo, mp2_env)
414 :
415 : ELSE
416 :
417 220 : DO ispin = 1, nspins
418 116 : CALL dbcsr_set(matrix_ks(ispin)%matrix, 0.0_dp)
419 220 : IF (do_admm_rpa) THEN
420 10 : CALL dbcsr_set(matrix_ks_aux_fit(ispin)%matrix, 0.0_dp)
421 : END IF
422 : END DO
423 :
424 104 : hfx_sections => section_vals_get_subs_vals(input, "DFT%XC%WF_CORRELATION%RI_RPA%HF")
425 :
426 104 : CALL section_vals_get(hfx_sections, n_repetition=n_rep_hf)
427 :
428 : ! in most cases, we calculate the exchange self-energy here. But if we do only RI for
429 : ! the exchange self-energy, we do not calculate exchange here
430 104 : ehfx = 0.0_dp
431 104 : IF (.NOT. do_ri_Sigma_x) THEN
432 :
433 48 : CALL exx_pre_hfx(hfx_sections, qs_env%mp2_env%ri_rpa%x_data, qs_env%mp2_env%ri_rpa%reuse_hfx)
434 48 : calc_ints = .NOT. qs_env%mp2_env%ri_rpa%reuse_hfx
435 :
436 : ! add here HFX (=Sigma_exchange) to matrix_sigma_x_minus_vxc
437 96 : DO irep = 1, n_rep_hf
438 48 : IF (do_admm_rpa) THEN
439 8 : matrix_ks_2d(1:nspins, 1:1) => matrix_ks_aux_fit(1:nspins)
440 8 : rho_ao_2d(1:nspins, 1:1) => rho_ao_aux_fit(1:nspins)
441 : ELSE
442 40 : matrix_ks_2d(1:nspins, 1:1) => matrix_ks(1:nspins)
443 40 : rho_ao_2d(1:nspins, 1:1) => rho_ao(1:nspins)
444 : END IF
445 :
446 96 : IF (qs_env%mp2_env%ri_rpa%x_data(irep, 1)%do_hfx_ri) THEN
447 : CALL hfx_ri_update_ks(qs_env, qs_env%mp2_env%ri_rpa%x_data(irep, 1)%ri_data, matrix_ks_2d, ehfx, &
448 : rho_ao=rho_ao_2d, geometry_did_change=calc_ints, nspins=nspins, &
449 0 : hf_fraction=qs_env%mp2_env%ri_rpa%x_data(irep, 1)%general_parameter%fraction)
450 :
451 0 : IF (do_admm_rpa) THEN
452 : !for ADMMS, we need the exchange matrix k(d) for both spins
453 0 : DO ispin = 1, nspins
454 : CALL dbcsr_copy(matrix_ks_aux_fit_hfx(ispin)%matrix, matrix_ks_2d(ispin, 1)%matrix, &
455 0 : name="HF exch. part of matrix_ks_aux_fit for ADMMS")
456 : END DO
457 : END IF
458 : ELSE
459 : CALL integrate_four_center(qs_env, qs_env%mp2_env%ri_rpa%x_data, matrix_ks_2d, eh1, &
460 : rho_ao_2d, hfx_sections, &
461 : para_env, calc_ints, irep, .TRUE., &
462 48 : ispin=1)
463 48 : ehfx = ehfx + eh1
464 : END IF
465 : END DO
466 :
467 : !ADMM XC correction
468 48 : IF (do_admm_rpa) THEN
469 : CALL calc_exx_admm_xc_contributions(qs_env=qs_env, &
470 : matrix_prim=matrix_ks, &
471 : matrix_aux=matrix_ks_aux_fit, &
472 : x_data=qs_env%mp2_env%ri_rpa%x_data, &
473 : exc=energy_xc_admm(1), &
474 : exc_aux_fit=energy_xc_admm(2), &
475 : calc_forces=.FALSE., &
476 8 : use_virial=.FALSE.)
477 : END IF
478 :
479 48 : IF (do_kpoints_from_Gamma .AND. print_exx == gw_print_exx) THEN
480 0 : ALLOCATE (mat_exchange_for_kp_from_gamma(1))
481 :
482 0 : DO ispin = 1, 1
483 0 : NULLIFY (mat_exchange_for_kp_from_gamma(ispin)%matrix)
484 0 : ALLOCATE (mat_exchange_for_kp_from_gamma(ispin)%matrix)
485 0 : CALL dbcsr_create(mat_exchange_for_kp_from_gamma(ispin)%matrix, template=matrix_ks(ispin)%matrix)
486 0 : CALL dbcsr_desymmetrize(matrix_ks(ispin)%matrix, mat_exchange_for_kp_from_gamma(ispin)%matrix)
487 : END DO
488 :
489 : END IF
490 :
491 48 : CALL exx_post_hfx(qs_env, qs_env%mp2_env%ri_rpa%x_data, qs_env%mp2_env%ri_rpa%reuse_hfx)
492 : END IF
493 :
494 104 : energy_ex = ehfx
495 :
496 : ! transform Fock-Matrix (calculated in integrate_four_center, written in matrix_ks_aux_fit in case
497 : ! of ADMM) from ADMM basis to primary basis
498 104 : IF (do_admm_rpa) THEN
499 8 : CALL admm_mo_merge_ks_matrix(qs_env)
500 : END IF
501 :
502 220 : DO ispin = 1, nspins
503 220 : CALL dbcsr_add(matrix_sigma_x_minus_vxc(ispin, 1)%matrix, matrix_ks(ispin)%matrix, 1.0_dp, 1.0_dp)
504 : END DO
505 :
506 : ! safe matrix_sigma_x_minus_vxc for later: for example, we will transform matrix_sigma_x_minus_vxc
507 : ! to T-cell index and then to k-points for band structure calculation
508 104 : IF (do_kpoints_from_Gamma) THEN
509 : ! not yet there: open shell
510 66 : ALLOCATE (qs_env%mp2_env%ri_g0w0%matrix_sigma_x_minus_vxc(nspins))
511 34 : DO ispin = 1, nspins
512 18 : NULLIFY (qs_env%mp2_env%ri_g0w0%matrix_sigma_x_minus_vxc(ispin)%matrix)
513 18 : ALLOCATE (qs_env%mp2_env%ri_g0w0%matrix_sigma_x_minus_vxc(ispin)%matrix)
514 : CALL dbcsr_create(qs_env%mp2_env%ri_g0w0%matrix_sigma_x_minus_vxc(ispin)%matrix, &
515 18 : template=matrix_ks(ispin)%matrix)
516 :
517 : CALL dbcsr_desymmetrize(matrix_sigma_x_minus_vxc(ispin, 1)%matrix, &
518 34 : qs_env%mp2_env%ri_g0w0%matrix_sigma_x_minus_vxc(ispin)%matrix)
519 :
520 : END DO
521 : END IF
522 :
523 104 : CALL dbcsr_desymmetrize(matrix_ks(1)%matrix, mo_coeff_b)
524 104 : CALL dbcsr_set(mo_coeff_b, 0.0_dp)
525 :
526 : ! Transform matrix_sigma_x_minus_vxc to MO basis
527 220 : DO ispin = 1, nspins
528 :
529 : CALL get_mo_set(mo_set=mos_mp2(ispin), &
530 : mo_coeff=mo_coeff, &
531 : eigenvalues=mo_eigenvalues, &
532 : nmo=nmo, &
533 : homo=homo, &
534 116 : nao=dimen)
535 :
536 116 : IF (ispin == 1) THEN
537 :
538 520 : ALLOCATE (vec_Sigma_x_minus_vxc_gw(nmo, nspins, nkp))
539 3070 : vec_Sigma_x_minus_vxc_gw = 0.0_dp
540 :
541 312 : ALLOCATE (matrix_tmp_2_diag(dimen))
542 : END IF
543 :
544 116 : CALL dbcsr_set(mo_coeff_b, 0.0_dp)
545 116 : CALL copy_fm_to_dbcsr(mo_coeff, mo_coeff_b, keep_sparsity=.FALSE.)
546 :
547 : ! initialize matrix_tmp and matrix_tmp2
548 116 : IF (ispin == 1) THEN
549 104 : CALL dbcsr_create(matrix_tmp, template=mo_coeff_b)
550 104 : CALL dbcsr_copy(matrix_tmp, mo_coeff_b)
551 104 : CALL dbcsr_set(matrix_tmp, 0.0_dp)
552 :
553 104 : CALL dbcsr_create(matrix_tmp_2, template=mo_coeff_b)
554 104 : CALL dbcsr_copy(matrix_tmp_2, mo_coeff_b)
555 104 : CALL dbcsr_set(matrix_tmp_2, 0.0_dp)
556 : END IF
557 :
558 116 : gw_corr_lev_occ = mp2_env%ri_g0w0%corr_mos_occ
559 116 : gw_corr_lev_virt = mp2_env%ri_g0w0%corr_mos_virt
560 :
561 : ! If SVD is used to invert overlap matrix (for CHOLESKY OFF), some MOs are removed
562 : ! Therefore, setting the number of gw_corr_lev_virt simply to dimen - homo leads to index problems
563 : ! Instead, we take into account the removed MOs
564 116 : max_corr_lev_occ = homo
565 116 : max_corr_lev_virt = nmo - homo
566 :
567 : ! If BSE is invoked, manipulate corrected MO number
568 116 : IF (mp2_env%bse%do_bse) THEN
569 : ! Logic: If cutoff is negative, all MOs are included in BSE, i.e. we need to correct them all
570 : ! If cutoff is positive, we can reduce the number of MOs to be corrected and force gw_corr_lev_...
571 : ! to a sufficiently large number by setting it to -2 and read indices afterwards
572 : ! Handling for occupied levels
573 30 : IF (mp2_env%bse%bse_cutoff_occ < 0) THEN
574 2 : gw_corr_lev_occ = -1
575 : ELSE
576 28 : IF (gw_corr_lev_occ > 0) THEN
577 28 : gw_corr_lev_occ = -2
578 : END IF
579 : END IF
580 : ! Handling for virtual levels
581 30 : IF (mp2_env%bse%bse_cutoff_empty < 0) THEN
582 0 : gw_corr_lev_virt = -1
583 : ELSE
584 30 : IF (gw_corr_lev_virt > 0) THEN
585 30 : gw_corr_lev_virt = -2
586 : END IF
587 : END IF
588 :
589 : ! Obtain indices from DFT if gw_corr... are set to -2
590 : CALL determine_cutoff_indices(mo_eigenvalues, &
591 : homo, max_corr_lev_virt, &
592 : homo_reduced_bse, virtual_reduced_bse, &
593 : homo_startindex_bse, virtual_startindex_bse, &
594 30 : mp2_env)
595 30 : IF (gw_corr_lev_occ == -2) THEN
596 28 : CPWARN("BSE cutoff overwrites user input for CORR_MOS_OCC")
597 28 : gw_corr_lev_occ = homo_reduced_bse
598 : END IF
599 30 : IF (gw_corr_lev_virt == -2) THEN
600 30 : CPWARN("BSE cutoff overwrites user input for CORR_MOS_VIRT")
601 30 : gw_corr_lev_virt = virtual_reduced_bse
602 : END IF
603 : END IF
604 :
605 : ! if requested number of occ/virt levels for correction either exceed the number of
606 : ! occ/virt levels or the requested number is negative, default to correct all
607 : ! occ/virt level energies
608 116 : IF (gw_corr_lev_occ > homo .OR. gw_corr_lev_occ < 0) gw_corr_lev_occ = max_corr_lev_occ
609 116 : IF (gw_corr_lev_virt > max_corr_lev_virt .OR. gw_corr_lev_virt < 0) gw_corr_lev_virt = max_corr_lev_virt
610 116 : IF (ispin == 1) THEN
611 104 : mp2_env%ri_g0w0%corr_mos_occ = gw_corr_lev_occ
612 104 : mp2_env%ri_g0w0%corr_mos_virt = gw_corr_lev_virt
613 12 : ELSE IF (ispin == 2) THEN
614 : ! ensure that the total number of corrected MOs is the same for alpha and beta, important
615 : ! for parallelization
616 12 : IF (mp2_env%ri_g0w0%corr_mos_occ + mp2_env%ri_g0w0%corr_mos_virt /= &
617 : gw_corr_lev_occ + gw_corr_lev_virt) THEN
618 10 : gw_corr_lev_virt = mp2_env%ri_g0w0%corr_mos_occ + mp2_env%ri_g0w0%corr_mos_virt - gw_corr_lev_occ
619 : END IF
620 12 : mp2_env%ri_g0w0%corr_mos_occ_beta = gw_corr_lev_occ
621 12 : mp2_env%ri_g0w0%corr_mos_virt_beta = gw_corr_lev_virt
622 :
623 : END IF
624 :
625 : CALL dbcsr_multiply('N', 'N', 1.0_dp, matrix_sigma_x_minus_vxc(ispin, 1)%matrix, &
626 : mo_coeff_b, 0.0_dp, matrix_tmp, first_column=homo + 1 - gw_corr_lev_occ, &
627 116 : last_column=homo + gw_corr_lev_virt)
628 :
629 : CALL dbcsr_multiply('T', 'N', 1.0_dp, mo_coeff_b, &
630 : matrix_tmp, 0.0_dp, matrix_tmp_2, first_row=homo + 1 - gw_corr_lev_occ, &
631 116 : last_row=homo + gw_corr_lev_virt)
632 :
633 116 : CALL dbcsr_get_diag(matrix_tmp_2, matrix_tmp_2_diag)
634 2862 : vec_Sigma_x_minus_vxc_gw(1:nmo, ispin, 1) = matrix_tmp_2_diag(1:nmo)
635 :
636 116 : CALL dbcsr_set(matrix_tmp, 0.0_dp)
637 336 : CALL dbcsr_set(matrix_tmp_2, 0.0_dp)
638 :
639 : END DO
640 :
641 104 : CALL para_env%sum(vec_Sigma_x_minus_vxc_gw)
642 :
643 : END IF
644 :
645 104 : CALL dbcsr_release(mo_coeff_b)
646 104 : CALL dbcsr_release(matrix_tmp)
647 104 : CALL dbcsr_release(matrix_tmp_2)
648 104 : IF (do_kpoints_cubic_RPA) THEN
649 0 : CALL dbcsr_deallocate_matrix_set(matrix_ks_kp_re)
650 0 : CALL dbcsr_deallocate_matrix_set(matrix_ks_kp_im)
651 : END IF
652 :
653 220 : DO ispin = 1, nspins
654 336 : DO ikp = 1, nkp
655 116 : CALL dbcsr_release_p(matrix_sigma_x_minus_vxc(ispin, ikp)%matrix)
656 232 : IF (do_kpoints_cubic_RPA) THEN
657 0 : CALL dbcsr_release_p(matrix_sigma_x_minus_vxc_im(ispin, ikp)%matrix)
658 : END IF
659 : END DO
660 : END DO
661 :
662 520 : ALLOCATE (mp2_env%ri_g0w0%vec_Sigma_x_minus_vxc_gw(nmo, nspins, nkp))
663 :
664 104 : IF (print_exx == gw_print_exx) THEN
665 :
666 0 : IF (do_kpoints_from_Gamma) THEN
667 :
668 0 : gw_corr_lev_tot = gw_corr_lev_occ + gw_corr_lev_virt
669 :
670 : CALL get_qs_env(qs_env=qs_env, &
671 0 : kpoints=kpoints)
672 :
673 0 : CALL trunc_coulomb_for_exchange(qs_env)
674 :
675 0 : CALL compute_kpoints(qs_env, kpoints, unit_nr)
676 :
677 0 : ALLOCATE (Eigenval_kp(nmo, 1, nspins))
678 :
679 0 : CALL get_bandstruc_and_k_dependent_MOs(qs_env, Eigenval_kp)
680 :
681 0 : CALL compute_minus_vxc_kpoints(qs_env)
682 :
683 0 : nkp_Sigma = SIZE(Eigenval_kp, 2)
684 :
685 0 : ALLOCATE (vec_Sigma_x(nmo, nkp_Sigma))
686 0 : vec_Sigma_x(:, :) = 0.0_dp
687 :
688 : CALL trafo_to_mo_and_kpoints(qs_env, &
689 : mat_exchange_for_kp_from_gamma(1)%matrix, &
690 : vec_Sigma_x(homo - gw_corr_lev_occ + 1:homo + gw_corr_lev_virt, :), &
691 0 : homo, gw_corr_lev_occ, gw_corr_lev_virt, 1)
692 :
693 0 : CALL dbcsr_release(mat_exchange_for_kp_from_gamma(1)%matrix)
694 0 : DEALLOCATE (mat_exchange_for_kp_from_gamma(1)%matrix)
695 0 : DEALLOCATE (mat_exchange_for_kp_from_gamma)
696 :
697 0 : DEALLOCATE (vec_Sigma_x_minus_vxc_gw)
698 :
699 0 : ALLOCATE (vec_Sigma_x_minus_vxc_gw(nmo, nspins, nkp_Sigma))
700 :
701 : vec_Sigma_x_minus_vxc_gw(:, 1, :) = vec_Sigma_x(:, :) + &
702 0 : qs_env%mp2_env%ri_g0w0%vec_Sigma_x_minus_vxc_gw(:, 1, :)
703 :
704 0 : kpoints_Sigma => qs_env%mp2_env%ri_rpa_im_time%kpoints_Sigma
705 :
706 : ELSE
707 :
708 0 : nkp_Sigma = 1
709 :
710 : END IF
711 :
712 0 : IF (unit_nr > 0) THEN
713 :
714 0 : ALLOCATE (Eigenval_kp_HF_at_DFT(nmo, nkp_Sigma))
715 0 : Eigenval_kp_HF_at_DFT(:, :) = Eigenval_kp(:, :, 1) + vec_Sigma_x_minus_vxc_gw(:, 1, :)
716 :
717 0 : min_direct_HF_at_DFT_gap = 100.0_dp
718 :
719 0 : WRITE (unit_nr, '(T3,A)') ''
720 0 : WRITE (unit_nr, '(T3,A)') 'Exchange energies'
721 0 : WRITE (unit_nr, '(T3,A)') '-----------------'
722 0 : WRITE (unit_nr, '(T3,A)') ''
723 0 : WRITE (unit_nr, '(T6,2A)') 'MO e_n^DFT Sigma_x-vxc e_n^HF@DFT'
724 0 : DO ikp = 1, nkp_Sigma
725 0 : IF (nkp_Sigma > 1) THEN
726 0 : WRITE (unit_nr, '(T3,A)') ''
727 0 : WRITE (unit_nr, '(T3,A7,I3,A3,I3,A8,3F7.3,A12,3F7.3)') 'Kpoint ', ikp, ' /', nkp_Sigma, &
728 0 : ' xkp =', kpoints_Sigma%xkp(1, ikp), kpoints_Sigma%xkp(2, ikp), &
729 0 : kpoints_Sigma%xkp(3, ikp), ' and xkp =', -kpoints_Sigma%xkp(1, ikp), &
730 0 : -kpoints_Sigma%xkp(2, ikp), -kpoints_Sigma%xkp(3, ikp)
731 0 : WRITE (unit_nr, '(T3,A)') ''
732 : END IF
733 0 : DO n_level_gw = 1, gw_corr_lev_occ + gw_corr_lev_virt
734 :
735 0 : n_level_gw_ref = n_level_gw + homo - gw_corr_lev_occ
736 0 : IF (n_level_gw <= gw_corr_lev_occ) THEN
737 0 : occ_virt = 'occ'
738 : ELSE
739 0 : occ_virt = 'vir'
740 : END IF
741 :
742 0 : eigval_dft = Eigenval_kp(n_level_gw_ref, ikp, 1)*evolt
743 0 : exx_minus_vxc = REAL(vec_Sigma_x_minus_vxc_gw(n_level_gw_ref, 1, ikp)*evolt, kind=dp)
744 0 : eigval_hf_at_dft = Eigenval_kp_HF_at_DFT(n_level_gw_ref, ikp)*evolt
745 :
746 : WRITE (unit_nr, '(T4,I4,3A,3F21.3,3F21.3,3F21.3)') &
747 0 : n_level_gw_ref, ' ( ', occ_virt, ') ', eigval_dft, exx_minus_vxc, eigval_hf_at_dft
748 :
749 : END DO
750 0 : E_HOMO_GW = MAXVAL(Eigenval_kp_HF_at_DFT(homo - gw_corr_lev_occ + 1:homo, ikp))
751 0 : E_LUMO_GW = MINVAL(Eigenval_kp_HF_at_DFT(homo + 1:homo + gw_corr_lev_virt, ikp))
752 0 : E_GAP_GW = E_LUMO_GW - E_HOMO_GW
753 0 : IF (E_GAP_GW < min_direct_HF_at_DFT_gap) min_direct_HF_at_DFT_gap = E_GAP_GW
754 0 : WRITE (unit_nr, '(T3,A)') ''
755 0 : WRITE (unit_nr, '(T3,A,F53.2)') 'HF@DFT HOMO-LUMO gap (eV)', E_GAP_GW*evolt
756 0 : WRITE (unit_nr, '(T3,A)') ''
757 : END DO
758 :
759 0 : WRITE (unit_nr, '(T3,A)') ''
760 0 : WRITE (unit_nr, '(T3,A)') ''
761 0 : WRITE (unit_nr, '(T3,A,F63.3)') 'HF@DFT direct bandgap (eV)', min_direct_HF_at_DFT_gap*evolt
762 :
763 0 : WRITE (unit_nr, '(T3,A)') ''
764 0 : WRITE (unit_nr, '(T3,A)') 'End of exchange energies'
765 0 : WRITE (unit_nr, '(T3,A)') '------------------------'
766 0 : WRITE (unit_nr, '(T3,A)') ''
767 :
768 0 : CPABORT('Stop after printing exchange energies.')
769 :
770 : ELSE
771 0 : CALL para_env%sync()
772 : END IF
773 :
774 : END IF
775 :
776 104 : IF (print_exx == gw_read_exx) THEN
777 :
778 0 : CALL open_file(unit_number=iunit, file_name="exx.out")
779 :
780 0 : really_read_line = .FALSE.
781 :
782 : DO WHILE (.TRUE.)
783 :
784 0 : READ (iunit, '(A)') line
785 :
786 0 : IF (line == " End of exchange energies ") EXIT
787 :
788 0 : IF (really_read_line) THEN
789 :
790 0 : READ (line(1:7), *) n_level_gw_ref
791 0 : READ (line(17:40), *) tmp
792 :
793 0 : DO ikp = 1, SIZE(vec_Sigma_x_minus_vxc_gw, 3)
794 0 : vec_Sigma_x_minus_vxc_gw(n_level_gw_ref, 1, ikp) = tmp/evolt
795 : END DO
796 :
797 : END IF
798 :
799 0 : IF (line == " MO Sigma_x-vxc ") really_read_line = .TRUE.
800 :
801 : END DO
802 :
803 0 : CALL close_file(iunit)
804 :
805 : END IF
806 :
807 : ! store vec_Sigma_x_minus_vxc_gw in the mp2_environment
808 3070 : mp2_env%ri_g0w0%vec_Sigma_x_minus_vxc_gw(:, :, :) = vec_Sigma_x_minus_vxc_gw(:, :, :)
809 :
810 : ! clean up
811 104 : DEALLOCATE (matrix_sigma_x_minus_vxc, vec_Sigma_x_minus_vxc_gw)
812 104 : IF (do_kpoints_cubic_RPA) THEN
813 0 : DEALLOCATE (matrix_sigma_x_minus_vxc_im)
814 : END IF
815 :
816 104 : t2 = m_walltime()
817 :
818 104 : t3 = t2 - t1
819 :
820 104 : CALL timestop(handle)
821 :
822 416 : END SUBROUTINE compute_vec_Sigma_x_minus_vxc_gw
823 :
824 : ! **************************************************************************************************
825 : !> \brief ...
826 : !> \param kpoints ...
827 : !> \param matrix_sigma_x_minus_vxc ...
828 : !> \param matrix_sigma_x_minus_vxc_im ...
829 : !> \param vec_Sigma_x_minus_vxc_gw ...
830 : !> \param vec_Sigma_x_minus_vxc_gw_im ...
831 : !> \param para_env ...
832 : !> \param nmo ...
833 : !> \param mp2_env ...
834 : ! **************************************************************************************************
835 0 : SUBROUTINE transform_sigma_x_minus_vxc_to_MO_basis(kpoints, matrix_sigma_x_minus_vxc, &
836 : matrix_sigma_x_minus_vxc_im, vec_Sigma_x_minus_vxc_gw, &
837 : vec_Sigma_x_minus_vxc_gw_im, para_env, nmo, mp2_env)
838 :
839 : TYPE(kpoint_type), POINTER :: kpoints
840 : TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_sigma_x_minus_vxc, &
841 : matrix_sigma_x_minus_vxc_im
842 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :) :: vec_Sigma_x_minus_vxc_gw, &
843 : vec_Sigma_x_minus_vxc_gw_im
844 : TYPE(mp_para_env_type), INTENT(IN) :: para_env
845 : INTEGER :: nmo
846 : TYPE(mp2_type) :: mp2_env
847 :
848 : CHARACTER(LEN=*), PARAMETER :: routineN = 'transform_sigma_x_minus_vxc_to_MO_basis'
849 :
850 : INTEGER :: dimen, gw_corr_lev_occ, gw_corr_lev_virt, handle, homo, i_global, iiB, ikp, &
851 : ispin, j_global, jjB, max_corr_lev_occ, max_corr_lev_virt, ncol_local, nkp, nrow_local, &
852 : nspins
853 : INTEGER, DIMENSION(2) :: kp_range
854 0 : INTEGER, DIMENSION(:), POINTER :: col_indices, row_indices
855 : REAL(KIND=dp) :: imval, reval
856 : TYPE(cp_cfm_type) :: cfm_mos, cfm_sigma_x_minus_vxc, &
857 : cfm_sigma_x_minus_vxc_mo_basis, cfm_tmp
858 : TYPE(cp_fm_struct_type), POINTER :: matrix_struct
859 : TYPE(cp_fm_type) :: fwork_im, fwork_re
860 : TYPE(kpoint_env_type), POINTER :: kp
861 : TYPE(mo_set_type), POINTER :: mo_set, mo_set_im, mo_set_re
862 :
863 0 : CALL timeset(routineN, handle)
864 :
865 0 : mo_set => kpoints%kp_env(1)%kpoint_env%mos(1, 1)
866 0 : CALL get_mo_set(mo_set, nmo=nmo)
867 :
868 0 : nspins = SIZE(matrix_sigma_x_minus_vxc, 1)
869 0 : CALL get_kpoint_info(kpoints, nkp=nkp, kp_range=kp_range)
870 :
871 : ! if this CPASSERT is wrong, please make sure that the kpoint group size PARALLEL_GROUP_SIZE
872 : ! in the kpoint environment &DFT &KPOINTS is -1
873 0 : CPASSERT(kp_range(1) == 1 .AND. kp_range(2) == nkp)
874 :
875 0 : ALLOCATE (vec_Sigma_x_minus_vxc_gw(nmo, nspins, nkp))
876 0 : vec_Sigma_x_minus_vxc_gw = 0.0_dp
877 :
878 0 : ALLOCATE (vec_Sigma_x_minus_vxc_gw_im(nmo, nspins, nkp))
879 0 : vec_Sigma_x_minus_vxc_gw_im = 0.0_dp
880 :
881 0 : CALL cp_fm_get_info(mo_set%mo_coeff, matrix_struct=matrix_struct)
882 0 : CALL cp_fm_create(fwork_re, matrix_struct)
883 0 : CALL cp_fm_create(fwork_im, matrix_struct)
884 0 : CALL cp_cfm_create(cfm_mos, matrix_struct)
885 0 : CALL cp_cfm_create(cfm_sigma_x_minus_vxc, matrix_struct)
886 0 : CALL cp_cfm_create(cfm_sigma_x_minus_vxc_mo_basis, matrix_struct)
887 0 : CALL cp_cfm_create(cfm_tmp, matrix_struct)
888 :
889 : CALL cp_cfm_get_info(matrix=cfm_sigma_x_minus_vxc_mo_basis, &
890 : nrow_local=nrow_local, &
891 : ncol_local=ncol_local, &
892 : row_indices=row_indices, &
893 0 : col_indices=col_indices)
894 :
895 : ! Transform matrix_sigma_x_minus_vxc to MO basis
896 0 : DO ikp = 1, nkp
897 :
898 0 : kp => kpoints%kp_env(ikp)%kpoint_env
899 :
900 0 : DO ispin = 1, nspins
901 :
902 : ! v_xc_n to fm matrix
903 0 : CALL copy_dbcsr_to_fm(matrix_sigma_x_minus_vxc(ispin, ikp)%matrix, fwork_re)
904 0 : CALL copy_dbcsr_to_fm(matrix_sigma_x_minus_vxc_im(ispin, ikp)%matrix, fwork_im)
905 :
906 0 : CALL cp_cfm_scale_and_add_fm(z_zero, cfm_sigma_x_minus_vxc, z_one, fwork_re)
907 0 : CALL cp_cfm_scale_and_add_fm(z_one, cfm_sigma_x_minus_vxc, gaussi, fwork_im)
908 :
909 : ! get real part (1) and imag. part (2) of the mo coeffs
910 0 : mo_set_re => kp%mos(1, ispin)
911 0 : mo_set_im => kp%mos(2, ispin)
912 :
913 0 : CALL cp_cfm_scale_and_add_fm(z_zero, cfm_mos, z_one, mo_set_re%mo_coeff)
914 0 : CALL cp_cfm_scale_and_add_fm(z_one, cfm_mos, gaussi, mo_set_im%mo_coeff)
915 :
916 : ! tmp = V(k)*C(k)
917 : CALL parallel_gemm('N', 'N', nmo, nmo, nmo, z_one, cfm_sigma_x_minus_vxc, &
918 0 : cfm_mos, z_zero, cfm_tmp)
919 :
920 : ! V_n(k) = C^H(k)*tmp
921 : CALL parallel_gemm('C', 'N', nmo, nmo, nmo, z_one, cfm_mos, cfm_tmp, &
922 0 : z_zero, cfm_sigma_x_minus_vxc_mo_basis)
923 :
924 0 : DO jjB = 1, ncol_local
925 :
926 0 : j_global = col_indices(jjB)
927 :
928 0 : DO iiB = 1, nrow_local
929 :
930 0 : i_global = row_indices(iiB)
931 :
932 0 : IF (j_global == i_global .AND. i_global <= nmo) THEN
933 :
934 0 : reval = REAL(cfm_sigma_x_minus_vxc_mo_basis%local_data(iiB, jjB), kind=dp)
935 0 : imval = AIMAG(cfm_sigma_x_minus_vxc_mo_basis%local_data(iiB, jjB))
936 :
937 0 : vec_Sigma_x_minus_vxc_gw(i_global, ispin, ikp) = reval
938 0 : vec_Sigma_x_minus_vxc_gw_im(i_global, ispin, ikp) = imval
939 :
940 : END IF
941 :
942 : END DO
943 :
944 : END DO
945 :
946 : END DO
947 :
948 : END DO
949 :
950 0 : CALL para_env%sum(vec_Sigma_x_minus_vxc_gw)
951 0 : CALL para_env%sum(vec_Sigma_x_minus_vxc_gw_im)
952 : ! also adjust in the case of kpoints too big gw_corr_lev_occ and gw_corr_lev_virt
953 0 : DO ispin = 1, nspins
954 : CALL get_mo_set(mo_set=kpoints%kp_env(1)%kpoint_env%mos(ispin, 1), &
955 0 : homo=homo, nao=dimen)
956 : ! If SVD is used to invert overlap matrix (for CHOLESKY OFF), some MOs are removed
957 : ! Therefore, setting the number of gw_corr_lev_virt simply to dimen - homo leads to index problems
958 : ! Instead, we take into account the removed MOs
959 0 : max_corr_lev_occ = homo
960 0 : max_corr_lev_virt = nmo - homo
961 :
962 0 : gw_corr_lev_occ = mp2_env%ri_g0w0%corr_mos_occ
963 0 : gw_corr_lev_virt = mp2_env%ri_g0w0%corr_mos_virt
964 : ! if corrected occ/virt levels exceed the number of occ/virt levels or are negative,
965 : ! correct all occ/virt level energies
966 0 : IF (gw_corr_lev_occ > homo .OR. gw_corr_lev_occ < 0) gw_corr_lev_occ = max_corr_lev_occ
967 0 : IF (gw_corr_lev_virt > max_corr_lev_virt .OR. gw_corr_lev_virt < 0) gw_corr_lev_virt = max_corr_lev_virt
968 0 : IF (ispin == 1) THEN
969 0 : mp2_env%ri_g0w0%corr_mos_occ = gw_corr_lev_occ
970 0 : mp2_env%ri_g0w0%corr_mos_virt = gw_corr_lev_virt
971 0 : ELSE IF (ispin == 2) THEN
972 : ! ensure that the total number of corrected MOs is the same for alpha and beta, important
973 : ! for parallelization
974 0 : IF (mp2_env%ri_g0w0%corr_mos_occ + mp2_env%ri_g0w0%corr_mos_virt /= &
975 : gw_corr_lev_occ + gw_corr_lev_virt) THEN
976 0 : gw_corr_lev_virt = mp2_env%ri_g0w0%corr_mos_occ + mp2_env%ri_g0w0%corr_mos_virt - gw_corr_lev_occ
977 : END IF
978 0 : mp2_env%ri_g0w0%corr_mos_occ_beta = gw_corr_lev_occ
979 0 : mp2_env%ri_g0w0%corr_mos_virt_beta = gw_corr_lev_virt
980 : END IF
981 : END DO
982 :
983 0 : CALL cp_fm_release(fwork_re)
984 0 : CALL cp_fm_release(fwork_im)
985 0 : CALL cp_cfm_release(cfm_mos)
986 0 : CALL cp_cfm_release(cfm_sigma_x_minus_vxc)
987 0 : CALL cp_cfm_release(cfm_sigma_x_minus_vxc_mo_basis)
988 0 : CALL cp_cfm_release(cfm_tmp)
989 :
990 0 : CALL timestop(handle)
991 :
992 0 : END SUBROUTINE
993 :
994 : ! **************************************************************************************************
995 : !> \brief ...
996 : !> \param matrix_ks_transl ...
997 : !> \param matrix_ks_kp_re ...
998 : !> \param matrix_ks_kp_im ...
999 : !> \param kpoints ...
1000 : ! **************************************************************************************************
1001 0 : SUBROUTINE transform_matrix_ks_to_kp(matrix_ks_transl, matrix_ks_kp_re, matrix_ks_kp_im, kpoints)
1002 :
1003 : TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_ks_transl, matrix_ks_kp_re, &
1004 : matrix_ks_kp_im
1005 : TYPE(kpoint_type), POINTER :: kpoints
1006 :
1007 : CHARACTER(len=*), PARAMETER :: routineN = 'transform_matrix_ks_to_kp'
1008 :
1009 : INTEGER :: handle, ikp, ispin, nkp, nspin
1010 0 : INTEGER, DIMENSION(:, :, :), POINTER :: cell_to_index
1011 0 : REAL(KIND=dp), DIMENSION(:, :), POINTER :: xkp
1012 : TYPE(neighbor_list_set_p_type), DIMENSION(:), &
1013 0 : POINTER :: sab_nl
1014 :
1015 0 : CALL timeset(routineN, handle)
1016 :
1017 0 : NULLIFY (sab_nl)
1018 0 : CALL get_kpoint_info(kpoints, nkp=nkp, xkp=xkp, sab_nl=sab_nl, cell_to_index=cell_to_index)
1019 :
1020 0 : CPASSERT(ASSOCIATED(sab_nl))
1021 :
1022 0 : nspin = SIZE(matrix_ks_transl, 1)
1023 :
1024 0 : DO ikp = 1, nkp
1025 0 : DO ispin = 1, nspin
1026 :
1027 0 : CALL dbcsr_set(matrix_ks_kp_re(ispin, ikp)%matrix, 0.0_dp)
1028 0 : CALL dbcsr_set(matrix_ks_kp_im(ispin, ikp)%matrix, 0.0_dp)
1029 : CALL rskp_transform(rmatrix=matrix_ks_kp_re(ispin, ikp)%matrix, &
1030 : cmatrix=matrix_ks_kp_im(ispin, ikp)%matrix, &
1031 : rsmat=matrix_ks_transl, ispin=ispin, &
1032 0 : xkp=xkp(1:3, ikp), cell_to_index=cell_to_index, sab_nl=sab_nl)
1033 :
1034 : END DO
1035 : END DO
1036 :
1037 0 : CALL timestop(handle)
1038 :
1039 0 : END SUBROUTINE
1040 :
1041 : ! **************************************************************************************************
1042 : !> \brief ...
1043 : !> \param matrix_ks_transl ...
1044 : !> \param matrix_ks_kp_re ...
1045 : !> \param matrix_ks_kp_im ...
1046 : !> \param kpoints ...
1047 : ! **************************************************************************************************
1048 0 : SUBROUTINE allocate_matrix_ks_kp(matrix_ks_transl, matrix_ks_kp_re, matrix_ks_kp_im, kpoints)
1049 :
1050 : TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_ks_transl, matrix_ks_kp_re, &
1051 : matrix_ks_kp_im
1052 : TYPE(kpoint_type), POINTER :: kpoints
1053 :
1054 : CHARACTER(len=*), PARAMETER :: routineN = 'allocate_matrix_ks_kp'
1055 :
1056 : INTEGER :: handle, ikp, ispin, nkp, nspin
1057 0 : INTEGER, DIMENSION(:, :, :), POINTER :: cell_to_index
1058 0 : REAL(KIND=dp), DIMENSION(:, :), POINTER :: xkp
1059 : TYPE(neighbor_list_set_p_type), DIMENSION(:), &
1060 0 : POINTER :: sab_nl
1061 :
1062 0 : CALL timeset(routineN, handle)
1063 :
1064 0 : NULLIFY (sab_nl)
1065 0 : CALL get_kpoint_info(kpoints, nkp=nkp, xkp=xkp, sab_nl=sab_nl, cell_to_index=cell_to_index)
1066 :
1067 0 : CPASSERT(ASSOCIATED(sab_nl))
1068 :
1069 0 : nspin = SIZE(matrix_ks_transl, 1)
1070 :
1071 0 : NULLIFY (matrix_ks_kp_re, matrix_ks_kp_im)
1072 0 : CALL dbcsr_allocate_matrix_set(matrix_ks_kp_re, nspin, nkp)
1073 0 : CALL dbcsr_allocate_matrix_set(matrix_ks_kp_im, nspin, nkp)
1074 :
1075 0 : DO ikp = 1, nkp
1076 0 : DO ispin = 1, nspin
1077 :
1078 0 : ALLOCATE (matrix_ks_kp_re(ispin, ikp)%matrix)
1079 0 : ALLOCATE (matrix_ks_kp_im(ispin, ikp)%matrix)
1080 :
1081 : CALL dbcsr_create(matrix_ks_kp_re(ispin, ikp)%matrix, &
1082 : template=matrix_ks_transl(1, 1)%matrix, &
1083 0 : matrix_type=dbcsr_type_symmetric)
1084 : CALL dbcsr_create(matrix_ks_kp_im(ispin, ikp)%matrix, &
1085 : template=matrix_ks_transl(1, 1)%matrix, &
1086 0 : matrix_type=dbcsr_type_antisymmetric)
1087 :
1088 0 : CALL cp_dbcsr_alloc_block_from_nbl(matrix_ks_kp_re(ispin, ikp)%matrix, sab_nl)
1089 0 : CALL cp_dbcsr_alloc_block_from_nbl(matrix_ks_kp_im(ispin, ikp)%matrix, sab_nl)
1090 :
1091 0 : CALL dbcsr_set(matrix_ks_kp_re(ispin, ikp)%matrix, 0.0_dp)
1092 0 : CALL dbcsr_set(matrix_ks_kp_im(ispin, ikp)%matrix, 0.0_dp)
1093 :
1094 : END DO
1095 : END DO
1096 :
1097 0 : CALL timestop(handle)
1098 :
1099 0 : END SUBROUTINE
1100 :
1101 : END MODULE rpa_gw_sigma_x
1102 :
|