Line data Source code
1 : !--------------------------------------------------------------------------------------------------!
2 : ! CP2K: A general program to perform molecular dynamics simulations !
3 : ! Copyright 2000-2026 CP2K developers group <https://cp2k.org> !
4 : ! !
5 : ! SPDX-License-Identifier: GPL-2.0-or-later !
6 : !--------------------------------------------------------------------------------------------------!
7 :
8 : ! **************************************************************************************************
9 : !> \brief Routines to calculate RI-RPA energy
10 : !> \par History
11 : !> 06.2012 created [Mauro Del Ben]
12 : !> 04.2015 GW routines added [Jan Wilhelm]
13 : !> 10.2015 Cubic-scaling RPA routines added [Jan Wilhelm]
14 : !> 10.2018 Cubic-scaling SOS-MP2 added [Frederick Stein]
15 : !> 03.2019 Refactoring [Frederick Stein]
16 : ! **************************************************************************************************
17 : MODULE rpa_main
18 : USE bibliography, ONLY: &
19 : Bates2013, DelBen2013, DelBen2015, Freeman1977, Gruneis2009, Ren2011, Ren2013, &
20 : Wilhelm2016a, Wilhelm2016b, Wilhelm2017, Wilhelm2018, cite_reference
21 : USE bse_main, ONLY: start_bse_calculation
22 : USE cp_blacs_env, ONLY: cp_blacs_env_create,&
23 : cp_blacs_env_release,&
24 : cp_blacs_env_type
25 : USE cp_cfm_types, ONLY: cp_cfm_type
26 : USE cp_dbcsr_api, ONLY: dbcsr_add,&
27 : dbcsr_clear,&
28 : dbcsr_get_info,&
29 : dbcsr_p_type,&
30 : dbcsr_type
31 : USE cp_fm_basic_linalg, ONLY: cp_fm_scale_and_add
32 : USE cp_fm_struct, ONLY: cp_fm_struct_create,&
33 : cp_fm_struct_release,&
34 : cp_fm_struct_type
35 : USE cp_fm_types, ONLY: cp_fm_create,&
36 : cp_fm_release,&
37 : cp_fm_set_all,&
38 : cp_fm_to_fm,&
39 : cp_fm_type
40 : USE dbt_api, ONLY: dbt_type
41 : USE dgemm_counter_types, ONLY: dgemm_counter_init,&
42 : dgemm_counter_type,&
43 : dgemm_counter_write
44 : USE group_dist_types, ONLY: create_group_dist,&
45 : get_group_dist,&
46 : group_dist_d1_type,&
47 : maxsize,&
48 : release_group_dist
49 : USE hfx_types, ONLY: block_ind_type,&
50 : hfx_compression_type
51 : USE input_constants, ONLY: rpa_exchange_axk,&
52 : rpa_exchange_none,&
53 : rpa_exchange_sosex,&
54 : sigma_none,&
55 : wfc_mm_style_gemm
56 : USE kinds, ONLY: dp,&
57 : int_8
58 : USE kpoint_types, ONLY: kpoint_type
59 : USE machine, ONLY: m_flush,&
60 : m_memory
61 : USE mathconstants, ONLY: pi,&
62 : z_zero
63 : USE message_passing, ONLY: mp_comm_type,&
64 : mp_para_env_release,&
65 : mp_para_env_type
66 : USE minimax_exp, ONLY: check_exp_minimax_range
67 : USE mp2_grids, ONLY: get_clenshaw_grid,&
68 : get_minimax_grid
69 : USE mp2_laplace, ONLY: SOS_MP2_postprocessing
70 : USE mp2_ri_grad_util, ONLY: array2fm
71 : USE mp2_types, ONLY: mp2_type,&
72 : three_dim_real_array,&
73 : two_dim_int_array,&
74 : two_dim_real_array
75 : USE qs_environment_types, ONLY: get_qs_env,&
76 : qs_environment_type
77 : USE rpa_exchange, ONLY: rpa_exchange_needed_mem,&
78 : rpa_exchange_work_type
79 : USE rpa_grad, ONLY: rpa_grad_copy_Q,&
80 : rpa_grad_create,&
81 : rpa_grad_finalize,&
82 : rpa_grad_matrix_operations,&
83 : rpa_grad_needed_mem,&
84 : rpa_grad_type
85 : USE rpa_gw, ONLY: allocate_matrices_gw,&
86 : allocate_matrices_gw_im_time,&
87 : compute_GW_self_energy,&
88 : compute_QP_energies,&
89 : compute_W_cubic_GW,&
90 : deallocate_matrices_gw,&
91 : deallocate_matrices_gw_im_time,&
92 : get_fermi_level_offset
93 : USE rpa_gw_ic, ONLY: calculate_ic_correction
94 : USE rpa_gw_kpoints_util, ONLY: get_bandstruc_and_k_dependent_MOs,&
95 : invert_eps_compute_W_and_Erpa_kp
96 : USE rpa_im_time, ONLY: compute_mat_P_omega,&
97 : zero_mat_P_omega
98 : USE rpa_im_time_force_methods, ONLY: calc_laplace_loop_forces,&
99 : calc_post_loop_forces,&
100 : calc_rpa_loop_forces,&
101 : init_im_time_forces,&
102 : keep_initial_quad
103 : USE rpa_im_time_force_types, ONLY: im_time_force_release,&
104 : im_time_force_type
105 : USE rpa_sigma_functional, ONLY: finalize_rpa_sigma,&
106 : rpa_sigma_create,&
107 : rpa_sigma_matrix_spectral,&
108 : rpa_sigma_type
109 : USE rpa_util, ONLY: Q_trace_and_add_unit_matrix,&
110 : alloc_im_time,&
111 : calc_mat_Q,&
112 : compute_Erpa_by_freq_int,&
113 : contract_P_omega_with_mat_L,&
114 : dealloc_im_time,&
115 : remove_scaling_factor_rpa
116 : USE util, ONLY: get_limit
117 : #include "./base/base_uses.f90"
118 :
119 : IMPLICIT NONE
120 :
121 : PRIVATE
122 :
123 : CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'rpa_main'
124 :
125 : PUBLIC :: rpa_ri_compute_en
126 :
127 : CONTAINS
128 :
129 : ! **************************************************************************************************
130 : !> \brief ...
131 : !> \param qs_env ...
132 : !> \param Erpa ...
133 : !> \param mp2_env ...
134 : !> \param BIb_C ...
135 : !> \param BIb_C_gw ...
136 : !> \param BIb_C_bse_ij ...
137 : !> \param BIb_C_bse_ab ...
138 : !> \param para_env ...
139 : !> \param para_env_sub ...
140 : !> \param color_sub ...
141 : !> \param gd_array ...
142 : !> \param gd_B_virtual ...
143 : !> \param gd_B_all ...
144 : !> \param gd_B_occ_bse ...
145 : !> \param gd_B_virt_bse ...
146 : !> \param mo_coeff ...
147 : !> \param fm_matrix_PQ ...
148 : !> \param fm_matrix_L_kpoints ...
149 : !> \param fm_matrix_Minv_L_kpoints ...
150 : !> \param fm_matrix_Minv ...
151 : !> \param fm_matrix_Minv_Vtrunc_Minv ...
152 : !> \param kpoints ...
153 : !> \param Eigenval ...
154 : !> \param nmo ...
155 : !> \param homo ...
156 : !> \param dimen_RI ...
157 : !> \param dimen_RI_red ...
158 : !> \param gw_corr_lev_occ ...
159 : !> \param gw_corr_lev_virt ...
160 : !> \param bse_lev_virt ...
161 : !> \param unit_nr ...
162 : !> \param do_ri_sos_laplace_mp2 ...
163 : !> \param my_do_gw ...
164 : !> \param do_im_time ...
165 : !> \param do_bse ...
166 : !> \param matrix_s ...
167 : !> \param mat_munu ...
168 : !> \param mat_P_global ...
169 : !> \param t_3c_M ...
170 : !> \param t_3c_O ...
171 : !> \param t_3c_O_compressed ...
172 : !> \param t_3c_O_ind ...
173 : !> \param starts_array_mc ...
174 : !> \param ends_array_mc ...
175 : !> \param starts_array_mc_block ...
176 : !> \param ends_array_mc_block ...
177 : !> \param calc_forces ...
178 : ! **************************************************************************************************
179 1530 : SUBROUTINE rpa_ri_compute_en(qs_env, Erpa, mp2_env, BIb_C, BIb_C_gw, BIb_C_bse_ij, BIb_C_bse_ab, &
180 : para_env, para_env_sub, color_sub, &
181 306 : gd_array, gd_B_virtual, gd_B_all, gd_B_occ_bse, gd_B_virt_bse, &
182 306 : mo_coeff, fm_matrix_PQ, fm_matrix_L_kpoints, fm_matrix_Minv_L_kpoints, &
183 : fm_matrix_Minv, fm_matrix_Minv_Vtrunc_Minv, kpoints, &
184 612 : Eigenval, nmo, homo, dimen_RI, dimen_RI_red, gw_corr_lev_occ, gw_corr_lev_virt, &
185 : bse_lev_virt, &
186 : unit_nr, do_ri_sos_laplace_mp2, my_do_gw, do_im_time, do_bse, matrix_s, &
187 : mat_munu, mat_P_global, t_3c_M, t_3c_O, t_3c_O_compressed, t_3c_O_ind, &
188 : starts_array_mc, ends_array_mc, &
189 : starts_array_mc_block, ends_array_mc_block, calc_forces)
190 :
191 : TYPE(qs_environment_type), POINTER :: qs_env
192 : REAL(KIND=dp), INTENT(OUT) :: Erpa
193 : TYPE(mp2_type), INTENT(INOUT) :: mp2_env
194 : TYPE(three_dim_real_array), DIMENSION(:), &
195 : INTENT(INOUT) :: BIb_C, BIb_C_gw
196 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :), &
197 : INTENT(INOUT) :: BIb_C_bse_ij, BIb_C_bse_ab
198 : TYPE(mp_para_env_type), POINTER :: para_env, para_env_sub
199 : INTEGER, INTENT(INOUT) :: color_sub
200 : TYPE(group_dist_d1_type), INTENT(INOUT) :: gd_array
201 : TYPE(group_dist_d1_type), DIMENSION(:), &
202 : INTENT(INOUT) :: gd_B_virtual
203 : TYPE(group_dist_d1_type), INTENT(INOUT) :: gd_B_all, gd_B_occ_bse, gd_B_virt_bse
204 : TYPE(cp_fm_type), DIMENSION(:), INTENT(IN) :: mo_coeff
205 : TYPE(cp_fm_type), INTENT(IN) :: fm_matrix_PQ
206 : TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:, :) :: fm_matrix_L_kpoints, &
207 : fm_matrix_Minv_L_kpoints, &
208 : fm_matrix_Minv, &
209 : fm_matrix_Minv_Vtrunc_Minv
210 : TYPE(kpoint_type), POINTER :: kpoints
211 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :), &
212 : INTENT(INOUT) :: Eigenval
213 : INTEGER, INTENT(IN) :: nmo
214 : INTEGER, DIMENSION(:), INTENT(IN) :: homo
215 : INTEGER, INTENT(IN) :: dimen_RI, dimen_RI_red
216 : INTEGER, DIMENSION(:), INTENT(IN) :: gw_corr_lev_occ, gw_corr_lev_virt
217 : INTEGER, INTENT(IN) :: bse_lev_virt, unit_nr
218 : LOGICAL, INTENT(IN) :: do_ri_sos_laplace_mp2, my_do_gw, &
219 : do_im_time, do_bse
220 : TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_s
221 : TYPE(dbcsr_p_type), INTENT(IN) :: mat_munu
222 : TYPE(dbcsr_p_type), INTENT(INOUT) :: mat_P_global
223 : TYPE(dbt_type) :: t_3c_M
224 : TYPE(dbt_type), ALLOCATABLE, DIMENSION(:, :) :: t_3c_O
225 : TYPE(hfx_compression_type), ALLOCATABLE, &
226 : DIMENSION(:, :, :), INTENT(INOUT) :: t_3c_O_compressed
227 : TYPE(block_ind_type), ALLOCATABLE, &
228 : DIMENSION(:, :, :), INTENT(INOUT) :: t_3c_O_ind
229 : INTEGER, ALLOCATABLE, DIMENSION(:), INTENT(IN) :: starts_array_mc, ends_array_mc, &
230 : starts_array_mc_block, &
231 : ends_array_mc_block
232 : LOGICAL, INTENT(IN) :: calc_forces
233 :
234 : CHARACTER(LEN=*), PARAMETER :: routineN = 'rpa_ri_compute_en'
235 :
236 : INTEGER :: best_integ_group_size, best_num_integ_point, color_rpa_group, dimen_homo_square, &
237 : dimen_nm_gw, dimen_virt_square, handle, handle2, handle3, ierr, iiB, &
238 : input_num_integ_groups, integ_group_size, ispin, jjB, min_integ_group_size, &
239 : my_ab_comb_bse_end, my_ab_comb_bse_size, my_ab_comb_bse_start, my_group_L_end, &
240 : my_group_L_size, my_group_L_start, my_ij_comb_bse_end, my_ij_comb_bse_size, &
241 : my_ij_comb_bse_start, my_nm_gw_end, my_nm_gw_size, my_nm_gw_start, ncol_block_mat, &
242 : ngroup, nrow_block_mat, nspins, num_integ_group, num_integ_points, pos_integ_group
243 : INTEGER(KIND=int_8) :: mem
244 612 : INTEGER, ALLOCATABLE, DIMENSION(:) :: dimen_ia, my_ia_end, my_ia_size, &
245 306 : my_ia_start, virtual
246 : LOGICAL :: do_kpoints_from_Gamma, do_minimax_quad, &
247 : my_open_shell, skip_integ_group_opt
248 : REAL(KIND=dp) :: allowed_memory, avail_mem, E_Range, Emax, Emin, mem_for_iaK, mem_for_QK, &
249 : mem_min, mem_per_group, mem_per_rank, mem_per_repl, mem_real
250 306 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :) :: Eigenval_kp
251 612 : TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:) :: fm_mat_Q, fm_mat_Q_gemm, fm_mat_S, &
252 306 : fm_mat_S_gw
253 918 : TYPE(cp_fm_type), DIMENSION(1) :: fm_mat_R_gw, fm_mat_S_ab_bse, &
254 612 : fm_mat_S_ij_bse
255 : TYPE(mp_para_env_type), POINTER :: para_env_RPA
256 : TYPE(two_dim_real_array), ALLOCATABLE, &
257 612 : DIMENSION(:) :: BIb_C_2D, BIb_C_2D_gw
258 3060 : TYPE(two_dim_real_array), DIMENSION(1) :: BIb_C_2D_bse_ab, BIb_C_2D_bse_ij
259 :
260 306 : CALL timeset(routineN, handle)
261 :
262 306 : CALL cite_reference(DelBen2013)
263 306 : CALL cite_reference(DelBen2015)
264 :
265 306 : IF (mp2_env%ri_rpa%exchange_correction == rpa_exchange_axk) THEN
266 10 : CALL cite_reference(Bates2013)
267 296 : ELSE IF (mp2_env%ri_rpa%exchange_correction == rpa_exchange_sosex) THEN
268 2 : CALL cite_reference(Freeman1977)
269 2 : CALL cite_reference(Gruneis2009)
270 : END IF
271 306 : IF (mp2_env%ri_rpa%do_rse) THEN
272 6 : CALL cite_reference(Ren2011)
273 6 : CALL cite_reference(Ren2013)
274 : END IF
275 :
276 306 : IF (my_do_gw) THEN
277 108 : CALL cite_reference(Wilhelm2016a)
278 108 : CALL cite_reference(Wilhelm2017)
279 108 : CALL cite_reference(Wilhelm2018)
280 : END IF
281 :
282 306 : IF (do_im_time) THEN
283 136 : CALL cite_reference(Wilhelm2016b)
284 : END IF
285 :
286 306 : nspins = SIZE(homo)
287 306 : my_open_shell = (nspins == 2)
288 2142 : ALLOCATE (virtual(nspins), dimen_ia(nspins), my_ia_end(nspins), my_ia_start(nspins), my_ia_size(nspins))
289 670 : virtual(:) = nmo - homo(:)
290 670 : dimen_ia(:) = virtual(:)*homo(:)
291 :
292 1224 : ALLOCATE (Eigenval_kp(nmo, 1, nspins))
293 9122 : Eigenval_kp(:, 1, :) = Eigenval(:, :)
294 :
295 306 : IF (do_im_time) mp2_env%ri_rpa%minimax_quad = .TRUE.
296 306 : do_minimax_quad = mp2_env%ri_rpa%minimax_quad
297 :
298 306 : IF (do_ri_sos_laplace_mp2) THEN
299 58 : num_integ_points = mp2_env%ri_laplace%n_quadrature
300 58 : input_num_integ_groups = mp2_env%ri_laplace%num_integ_groups
301 :
302 : ! check the range for the minimax approximation
303 58 : E_Range = mp2_env%e_range
304 58 : IF (mp2_env%e_range <= 1.0_dp .OR. mp2_env%e_gap <= 0.0_dp) THEN
305 : Emin = HUGE(dp)
306 : Emax = 0.0_dp
307 88 : DO ispin = 1, nspins
308 88 : IF (homo(ispin) > 0) THEN
309 50 : Emin = MIN(Emin, 2.0_dp*(Eigenval(homo(ispin) + 1, ispin) - Eigenval(homo(ispin), ispin)))
310 2640 : Emax = MAX(Emax, 2.0_dp*(MAXVAL(Eigenval(:, ispin)) - MINVAL(Eigenval(:, ispin))))
311 : END IF
312 : END DO
313 38 : E_Range = Emax/Emin
314 : END IF
315 58 : IF (E_Range < 2.0_dp) E_Range = 2.0_dp
316 : ierr = 0
317 58 : CALL check_exp_minimax_range(num_integ_points, E_Range, ierr)
318 58 : IF (ierr /= 0) THEN
319 : jjB = num_integ_points - 1
320 0 : DO iiB = 1, jjB
321 0 : num_integ_points = num_integ_points - 1
322 : ierr = 0
323 0 : CALL check_exp_minimax_range(num_integ_points, E_Range, ierr)
324 0 : IF (ierr == 0) EXIT
325 : END DO
326 : END IF
327 58 : CPASSERT(num_integ_points >= 1)
328 : ELSE
329 248 : num_integ_points = mp2_env%ri_rpa%rpa_num_quad_points
330 248 : input_num_integ_groups = mp2_env%ri_rpa%rpa_num_integ_groups
331 248 : IF (my_do_gw .AND. do_minimax_quad) THEN
332 46 : IF (num_integ_points > 34) THEN
333 0 : IF (unit_nr > 0) THEN
334 : CALL cp_warn(__LOCATION__, &
335 : "The required number of quadrature point exceeds the maximum possible in the "// &
336 0 : "Minimax quadrature scheme. The number of quadrature point has been reset to 30.")
337 : END IF
338 0 : num_integ_points = 30
339 : END IF
340 : ELSE
341 202 : IF (do_minimax_quad .AND. num_integ_points > 20) THEN
342 0 : IF (unit_nr > 0) THEN
343 : CALL cp_warn(__LOCATION__, &
344 : "The required number of quadrature point exceeds the maximum possible in the "// &
345 0 : "Minimax quadrature scheme. The number of quadrature point has been reset to 20.")
346 : END IF
347 0 : num_integ_points = 20
348 : END IF
349 : END IF
350 : END IF
351 306 : allowed_memory = mp2_env%mp2_memory
352 :
353 306 : CALL get_group_dist(gd_array, color_sub, my_group_L_start, my_group_L_end, my_group_L_size)
354 :
355 306 : ngroup = para_env%num_pe/para_env_sub%num_pe
356 :
357 : ! for imaginary time or periodic GW or BSE, we use all processors for a single frequency/time point
358 306 : IF (do_im_time .OR. mp2_env%ri_g0w0%do_periodic .OR. do_bse) THEN
359 :
360 172 : integ_group_size = ngroup
361 172 : best_num_integ_point = num_integ_points
362 :
363 : ELSE
364 :
365 : ! Calculate available memory and create integral group according to that
366 : ! mem_for_iaK is the memory needed for storing the 3 centre integrals
367 298 : mem_for_iaK = REAL(SUM(dimen_ia), KIND=dp)*dimen_RI_red*8.0_dp/(1024_dp**2)
368 134 : mem_for_QK = REAL(dimen_RI_red, KIND=dp)*nspins*dimen_RI_red*8.0_dp/(1024_dp**2)
369 :
370 134 : CALL m_memory(mem)
371 134 : mem_real = (mem + 1024*1024 - 1)/(1024*1024)
372 134 : CALL para_env%min(mem_real)
373 :
374 134 : mem_per_rank = 0.0_dp
375 :
376 : ! B_ia_P
377 : mem_per_repl = mem_for_iaK
378 : ! Q (regular and for dgemm)
379 134 : mem_per_repl = mem_per_repl + 2.0_dp*mem_for_QK
380 :
381 134 : IF (calc_forces) CALL rpa_grad_needed_mem(homo, virtual, dimen_RI_red, mem_per_rank, mem_per_repl, do_ri_sos_laplace_mp2)
382 134 : CALL rpa_exchange_needed_mem(mp2_env, homo, virtual, dimen_RI_red, para_env, mem_per_rank, mem_per_repl)
383 :
384 134 : mem_min = mem_per_repl/para_env%num_pe + mem_per_rank
385 :
386 134 : IF (unit_nr > 0) THEN
387 67 : WRITE (unit_nr, '(T3,A,T68,F9.2,A4)') 'RI_INFO| Minimum required memory per MPI process:', mem_min, ' MiB'
388 67 : WRITE (unit_nr, '(T3,A,T68,F9.2,A4)') 'RI_INFO| Available memory per MPI process:', mem_real, ' MiB'
389 : END IF
390 :
391 : ! Use only the allowed amount of memory
392 134 : mem_real = MIN(mem_real, allowed_memory)
393 : ! For the memory estimate, we require the amount of required memory per replication group and the available memory
394 134 : mem_real = mem_real - mem_per_rank
395 :
396 134 : mem_per_group = mem_real*para_env_sub%num_pe
397 :
398 : ! here we try to find the best rpa/laplace group size
399 134 : skip_integ_group_opt = .FALSE.
400 :
401 : ! Check the input number of integration groups
402 134 : IF (input_num_integ_groups > 0) THEN
403 2 : IF (num_integ_points < input_num_integ_groups) THEN
404 0 : IF (MOD(ngroup, input_num_integ_groups) == 0) THEN
405 0 : best_integ_group_size = ngroup/input_num_integ_groups
406 0 : best_num_integ_point = (num_integ_points + input_num_integ_groups - 1)/input_num_integ_groups
407 : skip_integ_group_opt = .TRUE.
408 : ELSE
409 0 : IF (unit_nr > 0) WRITE (unit_nr, '(T3,A)') 'Total number of groups not multiple of NUM_INTEG_GROUPS'
410 : END IF
411 : ELSE
412 2 : IF (unit_nr > 0) WRITE (unit_nr, '(T3,A)') 'Too many integration groups for the given number of quadrature points'
413 : END IF
414 : END IF
415 :
416 : IF (.NOT. skip_integ_group_opt) THEN
417 134 : best_integ_group_size = ngroup
418 134 : best_num_integ_point = num_integ_points
419 :
420 134 : min_integ_group_size = MAX(1, ngroup/num_integ_points)
421 :
422 134 : integ_group_size = min_integ_group_size - 1
423 134 : DO iiB = min_integ_group_size + 1, ngroup
424 112 : integ_group_size = integ_group_size + 1
425 :
426 : ! check that the ngroup is a multiple of integ_group_size
427 112 : IF (MOD(ngroup, integ_group_size) /= 0) CYCLE
428 :
429 : ! check for memory
430 112 : avail_mem = integ_group_size*mem_per_group
431 112 : IF (avail_mem < mem_per_repl) CYCLE
432 :
433 : ! check that the integration groups have the same size
434 112 : num_integ_group = ngroup/integ_group_size
435 :
436 112 : best_num_integ_point = (num_integ_points + num_integ_group - 1)/num_integ_group
437 112 : best_integ_group_size = integ_group_size
438 :
439 134 : EXIT
440 :
441 : END DO
442 : END IF
443 :
444 134 : integ_group_size = best_integ_group_size
445 :
446 : END IF
447 :
448 306 : IF (unit_nr > 0 .AND. .NOT. do_im_time) THEN
449 85 : IF (do_ri_sos_laplace_mp2) THEN
450 : WRITE (UNIT=unit_nr, FMT="(T3,A,T75,i6)") &
451 14 : "RI_INFO| Group size for laplace numerical integration:", integ_group_size*para_env_sub%num_pe
452 : WRITE (UNIT=unit_nr, FMT="(T3,A)") &
453 14 : "INTEG_INFO| MINIMAX approximation"
454 : WRITE (UNIT=unit_nr, FMT="(T3,A,T75,i6)") &
455 14 : "INTEG_INFO| Number of integration points:", num_integ_points
456 : WRITE (UNIT=unit_nr, FMT="(T3,A,T75,i6)") &
457 14 : "INTEG_INFO| Max. number of integration points per Laplace group:", best_num_integ_point
458 : ELSE
459 : WRITE (UNIT=unit_nr, FMT="(T3,A,T75,i6)") &
460 71 : "RI_INFO| Group size for frequency integration:", integ_group_size*para_env_sub%num_pe
461 71 : IF (do_minimax_quad) THEN
462 : WRITE (UNIT=unit_nr, FMT="(T3,A)") &
463 21 : "INTEG_INFO| MINIMAX quadrature"
464 : ELSE
465 : WRITE (UNIT=unit_nr, FMT="(T3,A)") &
466 50 : "INTEG_INFO| Clenshaw-Curtius quadrature"
467 : END IF
468 : WRITE (UNIT=unit_nr, FMT="(T3,A,T75,i6)") &
469 71 : "INTEG_INFO| Number of integration points:", num_integ_points
470 : WRITE (UNIT=unit_nr, FMT="(T3,A,T75,i6)") &
471 71 : "INTEG_INFO| Max. number of integration points per RPA group:", best_num_integ_point
472 : END IF
473 85 : CALL m_flush(unit_nr)
474 : END IF
475 :
476 306 : num_integ_group = ngroup/integ_group_size
477 :
478 306 : pos_integ_group = MOD(color_sub, integ_group_size)
479 306 : color_rpa_group = color_sub/integ_group_size
480 :
481 306 : CALL timeset(routineN//"_reorder", handle2)
482 :
483 : ! not necessary for imaginary time
484 :
485 1282 : ALLOCATE (BIb_C_2D(nspins))
486 :
487 306 : IF (.NOT. do_im_time) THEN
488 :
489 : ! reorder the local data in such a way to help the next stage of matrix creation
490 : ! now the data inside the group are divided into a ia x K matrix
491 370 : DO ispin = 1, nspins
492 : CALL calculate_BIb_C_2D(BIb_C_2D(ispin)%array, BIb_C(ispin)%array, para_env_sub, dimen_ia(ispin), &
493 : homo(ispin), virtual(ispin), gd_B_virtual(ispin), &
494 200 : my_ia_size(ispin), my_ia_start(ispin), my_ia_end(ispin), my_group_L_size)
495 :
496 200 : DEALLOCATE (BIb_C(ispin)%array)
497 370 : CALL release_group_dist(gd_B_virtual(ispin))
498 :
499 : END DO
500 :
501 : ! in the GW case, BIb_C_2D_gw is an nm x K matrix, with n: number of corr GW levels, m=nmo
502 170 : IF (my_do_gw) THEN
503 190 : ALLOCATE (BIb_C_2D_gw(nspins))
504 :
505 62 : CALL timeset(routineN//"_reorder_gw", handle3)
506 :
507 62 : dimen_nm_gw = nmo*(gw_corr_lev_occ(1) + gw_corr_lev_virt(1))
508 :
509 : ! The same for open shell
510 128 : DO ispin = 1, nspins
511 : CALL calculate_BIb_C_2D(BIb_C_2D_gw(ispin)%array, BIb_C_gw(ispin)%array, para_env_sub, dimen_nm_gw, &
512 : gw_corr_lev_occ(ispin) + gw_corr_lev_virt(ispin), nmo, gd_B_all, &
513 66 : my_nm_gw_size, my_nm_gw_start, my_nm_gw_end, my_group_L_size)
514 128 : DEALLOCATE (BIb_C_gw(ispin)%array)
515 : END DO
516 :
517 62 : CALL release_group_dist(gd_B_all)
518 :
519 124 : CALL timestop(handle3)
520 :
521 : END IF
522 : END IF
523 :
524 306 : IF (do_bse) THEN
525 :
526 34 : CALL timeset(routineN//"_reorder_bse1", handle3)
527 :
528 34 : dimen_homo_square = homo(1)**2
529 : ! We do not implement an explicit bse_lev_occ different to homo here, because the small number of occupied levels
530 : ! does not critically influence the memory
531 : CALL calculate_BIb_C_2D(BIb_C_2D_bse_ij(1)%array, BIb_C_bse_ij, para_env_sub, dimen_homo_square, &
532 : homo(1), homo(1), gd_B_occ_bse, &
533 34 : my_ij_comb_bse_size, my_ij_comb_bse_start, my_ij_comb_bse_end, my_group_L_size)
534 :
535 34 : DEALLOCATE (BIb_C_bse_ij)
536 34 : CALL release_group_dist(gd_B_occ_bse)
537 :
538 34 : CALL timestop(handle3)
539 :
540 34 : CALL timeset(routineN//"_reorder_bse2", handle3)
541 :
542 34 : dimen_virt_square = bse_lev_virt**2
543 :
544 : CALL calculate_BIb_C_2D(BIb_C_2D_bse_ab(1)%array, BIb_C_bse_ab, para_env_sub, dimen_virt_square, &
545 : bse_lev_virt, bse_lev_virt, gd_B_virt_bse, &
546 34 : my_ab_comb_bse_size, my_ab_comb_bse_start, my_ab_comb_bse_end, my_group_L_size)
547 :
548 34 : DEALLOCATE (BIb_C_bse_ab)
549 34 : CALL release_group_dist(gd_B_virt_bse)
550 :
551 34 : CALL timestop(handle3)
552 :
553 : END IF
554 :
555 306 : CALL timestop(handle2)
556 :
557 306 : IF (num_integ_group > 1) THEN
558 112 : ALLOCATE (para_env_RPA)
559 112 : CALL para_env_RPA%from_split(para_env, color_rpa_group)
560 : ELSE
561 194 : para_env_RPA => para_env
562 : END IF
563 :
564 : ! now create the matrices needed for the calculation, Q, S and G
565 : ! Q and G will have omega dependence
566 :
567 306 : IF (do_im_time) THEN
568 844 : ALLOCATE (fm_mat_Q(nspins), fm_mat_Q_gemm(1), fm_mat_S(1))
569 : ELSE
570 1450 : ALLOCATE (fm_mat_Q(nspins), fm_mat_Q_gemm(nspins), fm_mat_S(nspins))
571 : END IF
572 :
573 : CALL create_integ_mat(BIb_C_2D, para_env, para_env_sub, color_sub, ngroup, integ_group_size, &
574 : dimen_RI_red, dimen_ia, color_rpa_group, &
575 : mp2_env%block_size_row, mp2_env%block_size_col, unit_nr, &
576 : my_ia_size, my_ia_start, my_ia_end, &
577 : my_group_L_size, my_group_L_start, my_group_L_end, &
578 : para_env_RPA, fm_mat_S, nrow_block_mat, ncol_block_mat, &
579 : dimen_ia_for_block_size=dimen_ia(1), &
580 306 : do_im_time=do_im_time, fm_mat_Q_gemm=fm_mat_Q_gemm, fm_mat_Q=fm_mat_Q, qs_env=qs_env)
581 :
582 670 : DEALLOCATE (BIb_C_2D, my_ia_end, my_ia_size, my_ia_start)
583 :
584 : ! for GW, we need other matrix fm_mat_S, always allocate the container to prevent crying compilers
585 1282 : ALLOCATE (fm_mat_S_gw(nspins))
586 306 : IF (my_do_gw .AND. .NOT. do_im_time) THEN
587 :
588 : CALL create_integ_mat(BIb_C_2D_gw, para_env, para_env_sub, color_sub, ngroup, integ_group_size, &
589 : dimen_RI_red, [dimen_nm_gw, dimen_nm_gw], color_rpa_group, &
590 : mp2_env%block_size_row, mp2_env%block_size_col, unit_nr, &
591 : [my_nm_gw_size, my_nm_gw_size], [my_nm_gw_start, my_nm_gw_start], [my_nm_gw_end, my_nm_gw_end], &
592 : my_group_L_size, my_group_L_start, my_group_L_end, &
593 : para_env_RPA, fm_mat_S_gw, nrow_block_mat, ncol_block_mat, &
594 : fm_mat_Q(1)%matrix_struct%context, fm_mat_Q(1)%matrix_struct%context, &
595 558 : fm_mat_Q=fm_mat_R_gw)
596 128 : DEALLOCATE (BIb_C_2D_gw)
597 :
598 : END IF
599 :
600 : ! for Bethe-Salpeter, we need other matrix fm_mat_S
601 306 : IF (do_bse) THEN
602 : CALL create_integ_mat(BIb_C_2D_bse_ij, para_env, para_env_sub, color_sub, ngroup, integ_group_size, &
603 : dimen_RI_red, [dimen_homo_square], color_rpa_group, &
604 : mp2_env%block_size_row, mp2_env%block_size_col, unit_nr, &
605 : [my_ij_comb_bse_size], [my_ij_comb_bse_start], [my_ij_comb_bse_end], &
606 : my_group_L_size, my_group_L_start, my_group_L_end, &
607 : para_env_RPA, fm_mat_S_ij_bse, nrow_block_mat, ncol_block_mat, &
608 170 : fm_mat_Q(1)%matrix_struct%context, fm_mat_Q(1)%matrix_struct%context)
609 :
610 : CALL create_integ_mat(BIb_C_2D_bse_ab, para_env, para_env_sub, color_sub, ngroup, integ_group_size, &
611 : dimen_RI_red, [dimen_virt_square], color_rpa_group, &
612 : mp2_env%block_size_row, mp2_env%block_size_col, unit_nr, &
613 : [my_ab_comb_bse_size], [my_ab_comb_bse_start], [my_ab_comb_bse_end], &
614 : my_group_L_size, my_group_L_start, my_group_L_end, &
615 : para_env_RPA, fm_mat_S_ab_bse, nrow_block_mat, ncol_block_mat, &
616 170 : fm_mat_Q(1)%matrix_struct%context, fm_mat_Q(1)%matrix_struct%context)
617 :
618 : END IF
619 :
620 306 : do_kpoints_from_Gamma = qs_env%mp2_env%ri_rpa_im_time%do_kpoints_from_Gamma
621 306 : IF (do_kpoints_from_Gamma) THEN
622 16 : CALL get_bandstruc_and_k_dependent_MOs(qs_env, Eigenval_kp)
623 : END IF
624 :
625 : ! Now start the RPA calculation
626 : ! fm_mo_coeff_occ, fm_mo_coeff_virt will be deallocated here
627 : CALL rpa_num_int(qs_env, Erpa, mp2_env, para_env, para_env_RPA, para_env_sub, unit_nr, &
628 : homo, virtual, dimen_RI, dimen_RI_red, dimen_ia, dimen_nm_gw, &
629 : Eigenval_kp, num_integ_points, num_integ_group, color_rpa_group, &
630 : fm_matrix_PQ, fm_mat_S, fm_mat_Q_gemm, fm_mat_Q, fm_mat_S_gw, fm_mat_R_gw(1), &
631 : fm_mat_S_ij_bse(1), fm_mat_S_ab_bse(1), &
632 : my_do_gw, do_bse, gw_corr_lev_occ, gw_corr_lev_virt, &
633 : bse_lev_virt, &
634 : do_minimax_quad, &
635 : do_im_time, mo_coeff, &
636 : fm_matrix_L_kpoints, fm_matrix_Minv_L_kpoints, &
637 : fm_matrix_Minv, fm_matrix_Minv_Vtrunc_Minv, mat_munu, mat_P_global, &
638 : t_3c_M, t_3c_O, t_3c_O_compressed, t_3c_O_ind, &
639 : starts_array_mc, ends_array_mc, &
640 : starts_array_mc_block, ends_array_mc_block, &
641 : matrix_s, do_kpoints_from_Gamma, kpoints, gd_array, color_sub, &
642 306 : do_ri_sos_laplace_mp2=do_ri_sos_laplace_mp2, calc_forces=calc_forces)
643 :
644 306 : CALL release_group_dist(gd_array)
645 :
646 306 : IF (num_integ_group > 1) CALL mp_para_env_release(para_env_RPA)
647 :
648 306 : IF (.NOT. do_im_time) THEN
649 170 : CALL cp_fm_release(fm_mat_Q_gemm)
650 170 : CALL cp_fm_release(fm_mat_S)
651 : END IF
652 306 : CALL cp_fm_release(fm_mat_Q)
653 :
654 306 : IF (my_do_gw .AND. .NOT. do_im_time) THEN
655 62 : CALL cp_fm_release(fm_mat_S_gw)
656 62 : CALL cp_fm_release(fm_mat_R_gw(1))
657 : END IF
658 :
659 306 : IF (do_bse) THEN
660 34 : CALL cp_fm_release(fm_mat_S_ij_bse(1))
661 34 : CALL cp_fm_release(fm_mat_S_ab_bse(1))
662 : END IF
663 :
664 306 : CALL timestop(handle)
665 :
666 1224 : END SUBROUTINE rpa_ri_compute_en
667 :
668 : ! **************************************************************************************************
669 : !> \brief reorder the local data in such a way to help the next stage of matrix creation;
670 : !> now the data inside the group are divided into a ia x K matrix (BIb_C_2D);
671 : !> Subroutine created to avoid massive double coding
672 : !> \param BIb_C_2D ...
673 : !> \param BIb_C ...
674 : !> \param para_env_sub ...
675 : !> \param dimen_ia ...
676 : !> \param homo ...
677 : !> \param virtual ...
678 : !> \param gd_B_virtual ...
679 : !> \param my_ia_size ...
680 : !> \param my_ia_start ...
681 : !> \param my_ia_end ...
682 : !> \param my_group_L_size ...
683 : !> \author Jan Wilhelm, 03/2015
684 : ! **************************************************************************************************
685 334 : SUBROUTINE calculate_BIb_C_2D(BIb_C_2D, BIb_C, para_env_sub, dimen_ia, homo, virtual, &
686 : gd_B_virtual, &
687 : my_ia_size, my_ia_start, my_ia_end, my_group_L_size)
688 :
689 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :), &
690 : INTENT(OUT) :: BIb_C_2D
691 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :), &
692 : INTENT(IN) :: BIb_C
693 : TYPE(mp_para_env_type), INTENT(IN) :: para_env_sub
694 : INTEGER, INTENT(IN) :: dimen_ia, homo, virtual
695 : TYPE(group_dist_d1_type), INTENT(INOUT) :: gd_B_virtual
696 : INTEGER :: my_ia_size, my_ia_start, my_ia_end, &
697 : my_group_L_size
698 :
699 : INTEGER, PARAMETER :: occ_chunk = 128
700 :
701 : INTEGER :: ia_global, iiB, itmp(2), jjB, my_B_size, my_B_virtual_start, occ_high, occ_low, &
702 : proc_receive, proc_send, proc_shift, rec_B_size, rec_B_virtual_end, rec_B_virtual_start
703 334 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:), TARGET :: BIb_C_rec_1D
704 334 : REAL(KIND=dp), DIMENSION(:, :, :), POINTER :: BIb_C_rec
705 :
706 334 : itmp = get_limit(dimen_ia, para_env_sub%num_pe, para_env_sub%mepos)
707 334 : my_ia_start = itmp(1)
708 334 : my_ia_end = itmp(2)
709 334 : my_ia_size = my_ia_end - my_ia_start + 1
710 :
711 334 : CALL get_group_dist(gd_B_virtual, para_env_sub%mepos, sizes=my_B_size, starts=my_B_virtual_start)
712 :
713 : ! reorder data
714 1330 : ALLOCATE (BIb_C_2D(my_group_L_size, my_ia_size))
715 :
716 : !$OMP PARALLEL DO DEFAULT(NONE) PRIVATE(jjB,ia_global) &
717 : !$OMP SHARED(homo,my_B_size,virtual,my_B_virtual_start,my_ia_start,my_ia_end,BIb_C,BIb_C_2D,&
718 334 : !$OMP my_group_L_size)
719 : DO iiB = 1, homo
720 : DO jjB = 1, my_B_size
721 : ia_global = (iiB - 1)*virtual + my_B_virtual_start + jjB - 1
722 : IF (ia_global >= my_ia_start .AND. ia_global <= my_ia_end) THEN
723 : BIb_C_2D(1:my_group_L_size, ia_global - my_ia_start + 1) = BIb_C(1:my_group_L_size, jjB, iiB)
724 : END IF
725 : END DO
726 : END DO
727 :
728 334 : IF (para_env_sub%num_pe > 1) THEN
729 30 : ALLOCATE (BIb_C_rec_1D(INT(my_group_L_size, int_8)*maxsize(gd_B_virtual)*MIN(homo, occ_chunk)))
730 20 : DO proc_shift = 1, para_env_sub%num_pe - 1
731 10 : proc_send = MODULO(para_env_sub%mepos + proc_shift, para_env_sub%num_pe)
732 10 : proc_receive = MODULO(para_env_sub%mepos - proc_shift, para_env_sub%num_pe)
733 :
734 10 : CALL get_group_dist(gd_B_virtual, proc_receive, rec_B_virtual_start, rec_B_virtual_end, rec_B_size)
735 :
736 : ! do this in chunks to avoid high memory overhead
737 20 : DO occ_low = 1, homo, occ_chunk
738 10 : occ_high = MIN(homo, occ_low + occ_chunk - 1)
739 : BIb_C_rec(1:my_group_L_size, 1:rec_B_size, 1:occ_high - occ_low + 1) => &
740 10 : BIb_C_rec_1D(1:INT(my_group_L_size, int_8)*rec_B_size*(occ_high - occ_low + 1))
741 : CALL para_env_sub%sendrecv(BIb_C(:, :, occ_low:occ_high), proc_send, &
742 31970 : BIb_C_rec(:, :, 1:occ_high - occ_low + 1), proc_receive)
743 : !$OMP PARALLEL DO DEFAULT(NONE) PRIVATE(jjB,ia_global) &
744 : !$OMP SHARED(occ_low,occ_high,rec_B_size,virtual,rec_B_virtual_start,my_ia_start,my_ia_end,BIb_C_rec,BIb_C_2D,&
745 10 : !$OMP my_group_L_size)
746 : DO iiB = occ_low, occ_high
747 : DO jjB = 1, rec_B_size
748 : ia_global = (iiB - 1)*virtual + rec_B_virtual_start + jjB - 1
749 : IF (ia_global >= my_ia_start .AND. ia_global <= my_ia_end) THEN
750 : BIb_C_2D(1:my_group_L_size, ia_global - my_ia_start + 1) = BIb_C_rec(1:my_group_L_size, jjB, iiB - occ_low + 1)
751 : END IF
752 : END DO
753 : END DO
754 : END DO
755 :
756 : END DO
757 10 : DEALLOCATE (BIb_C_rec_1D)
758 : END IF
759 :
760 334 : END SUBROUTINE calculate_BIb_C_2D
761 :
762 : ! **************************************************************************************************
763 : !> \brief ...
764 : !> \param BIb_C_2D ...
765 : !> \param para_env ...
766 : !> \param para_env_sub ...
767 : !> \param color_sub ...
768 : !> \param ngroup ...
769 : !> \param integ_group_size ...
770 : !> \param dimen_RI ...
771 : !> \param dimen_ia ...
772 : !> \param color_rpa_group ...
773 : !> \param ext_row_block_size ...
774 : !> \param ext_col_block_size ...
775 : !> \param unit_nr ...
776 : !> \param my_ia_size ...
777 : !> \param my_ia_start ...
778 : !> \param my_ia_end ...
779 : !> \param my_group_L_size ...
780 : !> \param my_group_L_start ...
781 : !> \param my_group_L_end ...
782 : !> \param para_env_RPA ...
783 : !> \param fm_mat_S ...
784 : !> \param nrow_block_mat ...
785 : !> \param ncol_block_mat ...
786 : !> \param blacs_env_ext ...
787 : !> \param blacs_env_ext_S ...
788 : !> \param dimen_ia_for_block_size ...
789 : !> \param do_im_time ...
790 : !> \param fm_mat_Q_gemm ...
791 : !> \param fm_mat_Q ...
792 : !> \param qs_env ...
793 : ! **************************************************************************************************
794 436 : SUBROUTINE create_integ_mat(BIb_C_2D, para_env, para_env_sub, color_sub, ngroup, integ_group_size, &
795 436 : dimen_RI, dimen_ia, color_rpa_group, &
796 : ext_row_block_size, ext_col_block_size, unit_nr, &
797 436 : my_ia_size, my_ia_start, my_ia_end, &
798 : my_group_L_size, my_group_L_start, my_group_L_end, &
799 436 : para_env_RPA, fm_mat_S, nrow_block_mat, ncol_block_mat, &
800 : blacs_env_ext, blacs_env_ext_S, dimen_ia_for_block_size, &
801 436 : do_im_time, fm_mat_Q_gemm, fm_mat_Q, qs_env)
802 :
803 : TYPE(two_dim_real_array), DIMENSION(:), &
804 : INTENT(INOUT) :: BIb_C_2D
805 : TYPE(mp_para_env_type), INTENT(IN) :: para_env, para_env_sub
806 : INTEGER, INTENT(IN) :: color_sub, ngroup, integ_group_size, &
807 : dimen_RI
808 : INTEGER, DIMENSION(:), INTENT(IN) :: dimen_ia
809 : INTEGER, INTENT(IN) :: color_rpa_group, ext_row_block_size, &
810 : ext_col_block_size, unit_nr
811 : INTEGER, DIMENSION(:), INTENT(IN) :: my_ia_size, my_ia_start, my_ia_end
812 : INTEGER, INTENT(IN) :: my_group_L_size, my_group_L_start, &
813 : my_group_L_end
814 : TYPE(mp_para_env_type), INTENT(IN), POINTER :: para_env_RPA
815 : TYPE(cp_fm_type), DIMENSION(:), INTENT(INOUT) :: fm_mat_S
816 : INTEGER, INTENT(INOUT) :: nrow_block_mat, ncol_block_mat
817 : TYPE(cp_blacs_env_type), OPTIONAL, POINTER :: blacs_env_ext, blacs_env_ext_S
818 : INTEGER, INTENT(IN), OPTIONAL :: dimen_ia_for_block_size
819 : LOGICAL, INTENT(IN), OPTIONAL :: do_im_time
820 : TYPE(cp_fm_type), DIMENSION(:), OPTIONAL :: fm_mat_Q_gemm, fm_mat_Q
821 : TYPE(qs_environment_type), INTENT(IN), OPTIONAL, &
822 : POINTER :: qs_env
823 :
824 : CHARACTER(LEN=*), PARAMETER :: routineN = 'create_integ_mat'
825 :
826 : INTEGER :: col_row_proc_ratio, grid_2D(2), handle, &
827 : iproc, iproc_col, iproc_row, ispin, &
828 : mepos_in_RPA_group
829 436 : INTEGER, ALLOCATABLE, DIMENSION(:, :) :: group_grid_2_mepos
830 : LOGICAL :: my_blacs_ext, my_blacs_S_ext, &
831 : my_do_im_time
832 : TYPE(cp_blacs_env_type), POINTER :: blacs_env, blacs_env_Q
833 : TYPE(cp_fm_struct_type), POINTER :: fm_struct
834 436 : TYPE(group_dist_d1_type) :: gd_ia, gd_L
835 :
836 436 : CALL timeset(routineN, handle)
837 :
838 436 : CPASSERT(PRESENT(blacs_env_ext) .OR. PRESENT(dimen_ia_for_block_size))
839 :
840 436 : my_blacs_ext = .FALSE.
841 436 : IF (PRESENT(blacs_env_ext)) my_blacs_ext = .TRUE.
842 :
843 436 : my_blacs_S_ext = .FALSE.
844 436 : IF (PRESENT(blacs_env_ext_S)) my_blacs_S_ext = .TRUE.
845 :
846 436 : my_do_im_time = .FALSE.
847 436 : IF (PRESENT(do_im_time)) my_do_im_time = do_im_time
848 :
849 436 : NULLIFY (blacs_env)
850 : ! create the RPA blacs env
851 436 : IF (my_blacs_S_ext) THEN
852 130 : blacs_env => blacs_env_ext_S
853 : ELSE
854 306 : IF (para_env_RPA%num_pe > 1) THEN
855 194 : col_row_proc_ratio = MAX(1, dimen_ia_for_block_size/dimen_RI)
856 :
857 194 : iproc_col = MIN(MAX(INT(SQRT(REAL(para_env_RPA%num_pe*col_row_proc_ratio, KIND=dp))), 1), para_env_RPA%num_pe) + 1
858 194 : DO iproc = 1, para_env_RPA%num_pe
859 194 : iproc_col = iproc_col - 1
860 194 : IF (MOD(para_env_RPA%num_pe, iproc_col) == 0) EXIT
861 : END DO
862 :
863 194 : iproc_row = para_env_RPA%num_pe/iproc_col
864 194 : grid_2D(1) = iproc_row
865 194 : grid_2D(2) = iproc_col
866 : ELSE
867 336 : grid_2D = 1
868 : END IF
869 306 : CALL cp_blacs_env_create(blacs_env=blacs_env, para_env=para_env_RPA, grid_2d=grid_2D)
870 :
871 306 : IF (unit_nr > 0 .AND. .NOT. my_do_im_time) THEN
872 : WRITE (UNIT=unit_nr, FMT="(T3,A,T75,i6)") &
873 85 : "MATRIX_INFO| Number row processes:", grid_2D(1)
874 : WRITE (UNIT=unit_nr, FMT="(T3,A,T75,i6)") &
875 85 : "MATRIX_INFO| Number column processes:", grid_2D(2)
876 : END IF
877 :
878 : ! define the block_size for the row
879 306 : IF (ext_row_block_size > 0) THEN
880 0 : nrow_block_mat = ext_row_block_size
881 : ELSE
882 306 : nrow_block_mat = MAX(1, dimen_RI/grid_2D(1)/2)
883 : END IF
884 :
885 : ! define the block_size for the column
886 306 : IF (ext_col_block_size > 0) THEN
887 0 : ncol_block_mat = ext_col_block_size
888 : ELSE
889 306 : ncol_block_mat = MAX(1, dimen_ia_for_block_size/grid_2D(2)/2)
890 : END IF
891 :
892 306 : IF (unit_nr > 0 .AND. .NOT. my_do_im_time) THEN
893 : WRITE (UNIT=unit_nr, FMT="(T3,A,T75,i6)") &
894 85 : "MATRIX_INFO| Row block size:", nrow_block_mat
895 : WRITE (UNIT=unit_nr, FMT="(T3,A,T75,i6)") &
896 85 : "MATRIX_INFO| Column block size:", ncol_block_mat
897 : END IF
898 : END IF
899 :
900 368 : IF (.NOT. my_do_im_time) THEN
901 634 : DO ispin = 1, SIZE(BIb_C_2D)
902 334 : NULLIFY (fm_struct)
903 334 : IF (my_blacs_ext) THEN
904 : CALL cp_fm_struct_create(fm_struct, context=blacs_env, nrow_global=dimen_RI, &
905 134 : ncol_global=dimen_ia(ispin), para_env=para_env_RPA)
906 : ELSE
907 : CALL cp_fm_struct_create(fm_struct, context=blacs_env, nrow_global=dimen_RI, &
908 : ncol_global=dimen_ia(ispin), para_env=para_env_RPA, &
909 200 : nrow_block=nrow_block_mat, ncol_block=ncol_block_mat, force_block=.TRUE.)
910 :
911 : END IF ! external blacs_env
912 :
913 334 : CALL create_group_dist(gd_ia, my_ia_start(ispin), my_ia_end(ispin), my_ia_size(ispin), para_env_RPA)
914 334 : CALL create_group_dist(gd_L, my_group_L_start, my_group_L_end, my_group_L_size, para_env_RPA)
915 :
916 : ! create the info array
917 :
918 334 : mepos_in_RPA_group = MOD(color_sub, integ_group_size)
919 1336 : ALLOCATE (group_grid_2_mepos(0:integ_group_size - 1, 0:para_env_sub%num_pe - 1))
920 334 : group_grid_2_mepos = 0
921 334 : group_grid_2_mepos(mepos_in_RPA_group, para_env_sub%mepos) = para_env_RPA%mepos
922 334 : CALL para_env_RPA%sum(group_grid_2_mepos)
923 :
924 : CALL array2fm(BIb_C_2D(ispin)%array, fm_struct, my_group_L_start, my_group_L_end, &
925 : my_ia_start(ispin), my_ia_end(ispin), gd_L, gd_ia, &
926 : group_grid_2_mepos, ngroup, para_env_sub%num_pe, fm_mat_S(ispin), &
927 334 : integ_group_size, color_rpa_group)
928 :
929 334 : DEALLOCATE (group_grid_2_mepos)
930 334 : CALL cp_fm_struct_release(fm_struct)
931 :
932 : ! deallocate the info array
933 334 : CALL release_group_dist(gd_L)
934 334 : CALL release_group_dist(gd_ia)
935 :
936 : ! sum the local data across processes belonging to different RPA group.
937 634 : IF (para_env_RPA%num_pe /= para_env%num_pe) THEN
938 : BLOCK
939 : TYPE(mp_comm_type) :: comm_exchange
940 170 : comm_exchange = fm_mat_S(ispin)%matrix_struct%context%interconnect(para_env)
941 170 : CALL comm_exchange%sum(fm_mat_S(ispin)%local_data)
942 340 : CALL comm_exchange%free()
943 : END BLOCK
944 : END IF
945 : END DO
946 : END IF
947 :
948 436 : IF (PRESENT(fm_mat_Q_gemm) .AND. .NOT. my_do_im_time) THEN
949 : ! create the Q matrix dimen_RIxdimen_RI where the result of the mat-mat-mult will be stored
950 170 : NULLIFY (fm_struct)
951 : CALL cp_fm_struct_create(fm_struct, context=blacs_env, nrow_global=dimen_RI, &
952 : ncol_global=dimen_RI, para_env=para_env_RPA, &
953 170 : nrow_block=nrow_block_mat, ncol_block=ncol_block_mat, force_block=.TRUE.)
954 370 : DO ispin = 1, SIZE(fm_mat_Q_gemm)
955 370 : CALL cp_fm_create(fm_mat_Q_gemm(ispin), fm_struct, name="fm_mat_Q_gemm")
956 : END DO
957 170 : CALL cp_fm_struct_release(fm_struct)
958 : END IF
959 :
960 436 : IF (PRESENT(fm_mat_Q)) THEN
961 368 : NULLIFY (blacs_env_Q)
962 368 : IF (my_blacs_ext) THEN
963 62 : blacs_env_Q => blacs_env_ext
964 306 : ELSE IF (para_env_RPA%num_pe == para_env%num_pe .AND. PRESENT(qs_env)) THEN
965 194 : CALL get_qs_env(qs_env, blacs_env=blacs_env_Q)
966 : ELSE
967 112 : CALL cp_blacs_env_create(blacs_env=blacs_env_Q, para_env=para_env_RPA)
968 : END IF
969 368 : NULLIFY (fm_struct)
970 : CALL cp_fm_struct_create(fm_struct, context=blacs_env_Q, nrow_global=dimen_RI, &
971 368 : ncol_global=dimen_RI, para_env=para_env_RPA)
972 794 : DO ispin = 1, SIZE(fm_mat_Q)
973 794 : CALL cp_fm_create(fm_mat_Q(ispin), fm_struct, name="fm_mat_Q", set_zero=.TRUE.)
974 : END DO
975 :
976 368 : CALL cp_fm_struct_release(fm_struct)
977 :
978 368 : IF (.NOT. (my_blacs_ext .OR. (para_env_RPA%num_pe == para_env%num_pe .AND. PRESENT(qs_env)))) THEN
979 112 : CALL cp_blacs_env_release(blacs_env_Q)
980 : END IF
981 : END IF
982 :
983 : ! release blacs_env
984 436 : IF (.NOT. my_blacs_S_ext) THEN
985 306 : CALL cp_blacs_env_release(blacs_env)
986 : ELSE
987 130 : NULLIFY (blacs_env)
988 : END IF
989 :
990 436 : CALL timestop(handle)
991 :
992 436 : END SUBROUTINE create_integ_mat
993 :
994 : ! **************************************************************************************************
995 : !> \brief ...
996 : !> \param qs_env ...
997 : !> \param Erpa ...
998 : !> \param mp2_env ...
999 : !> \param para_env ...
1000 : !> \param para_env_RPA ...
1001 : !> \param para_env_sub ...
1002 : !> \param unit_nr ...
1003 : !> \param homo ...
1004 : !> \param virtual ...
1005 : !> \param dimen_RI ...
1006 : !> \param dimen_RI_red ...
1007 : !> \param dimen_ia ...
1008 : !> \param dimen_nm_gw ...
1009 : !> \param Eigenval ...
1010 : !> \param num_integ_points ...
1011 : !> \param num_integ_group ...
1012 : !> \param color_rpa_group ...
1013 : !> \param fm_matrix_PQ ...
1014 : !> \param fm_mat_S ...
1015 : !> \param fm_mat_Q_gemm ...
1016 : !> \param fm_mat_Q ...
1017 : !> \param fm_mat_S_gw ...
1018 : !> \param fm_mat_R_gw ...
1019 : !> \param fm_mat_S_ij_bse ...
1020 : !> \param fm_mat_S_ab_bse ...
1021 : !> \param my_do_gw ...
1022 : !> \param do_bse ...
1023 : !> \param gw_corr_lev_occ ...
1024 : !> \param gw_corr_lev_virt ...
1025 : !> \param bse_lev_virt ...
1026 : !> \param do_minimax_quad ...
1027 : !> \param do_im_time ...
1028 : !> \param mo_coeff ...
1029 : !> \param fm_matrix_L_kpoints ...
1030 : !> \param fm_matrix_Minv_L_kpoints ...
1031 : !> \param fm_matrix_Minv ...
1032 : !> \param fm_matrix_Minv_Vtrunc_Minv ...
1033 : !> \param mat_munu ...
1034 : !> \param mat_P_global ...
1035 : !> \param t_3c_M ...
1036 : !> \param t_3c_O ...
1037 : !> \param t_3c_O_compressed ...
1038 : !> \param t_3c_O_ind ...
1039 : !> \param starts_array_mc ...
1040 : !> \param ends_array_mc ...
1041 : !> \param starts_array_mc_block ...
1042 : !> \param ends_array_mc_block ...
1043 : !> \param matrix_s ...
1044 : !> \param do_kpoints_from_Gamma ...
1045 : !> \param kpoints ...
1046 : !> \param gd_array ...
1047 : !> \param color_sub ...
1048 : !> \param do_ri_sos_laplace_mp2 ...
1049 : !> \param calc_forces ...
1050 : ! **************************************************************************************************
1051 306 : SUBROUTINE rpa_num_int(qs_env, Erpa, mp2_env, para_env, para_env_RPA, para_env_sub, unit_nr, &
1052 306 : homo, virtual, dimen_RI, dimen_RI_red, dimen_ia, dimen_nm_gw, &
1053 : Eigenval, num_integ_points, num_integ_group, color_rpa_group, &
1054 612 : fm_matrix_PQ, fm_mat_S, fm_mat_Q_gemm, fm_mat_Q, fm_mat_S_gw, fm_mat_R_gw, &
1055 : fm_mat_S_ij_bse, fm_mat_S_ab_bse, &
1056 306 : my_do_gw, do_bse, gw_corr_lev_occ, gw_corr_lev_virt, &
1057 : bse_lev_virt, &
1058 306 : do_minimax_quad, do_im_time, mo_coeff, &
1059 : fm_matrix_L_kpoints, fm_matrix_Minv_L_kpoints, &
1060 : fm_matrix_Minv, fm_matrix_Minv_Vtrunc_Minv, mat_munu, mat_P_global, &
1061 : t_3c_M, t_3c_O, t_3c_O_compressed, t_3c_O_ind, &
1062 : starts_array_mc, ends_array_mc, &
1063 : starts_array_mc_block, ends_array_mc_block, &
1064 : matrix_s, do_kpoints_from_Gamma, kpoints, gd_array, color_sub, &
1065 : do_ri_sos_laplace_mp2, calc_forces)
1066 :
1067 : TYPE(qs_environment_type), POINTER :: qs_env
1068 : REAL(KIND=dp), INTENT(OUT) :: Erpa
1069 : TYPE(mp2_type) :: mp2_env
1070 : TYPE(mp_para_env_type), POINTER :: para_env, para_env_RPA, para_env_sub
1071 : INTEGER, INTENT(IN) :: unit_nr
1072 : INTEGER, DIMENSION(:), INTENT(IN) :: homo, virtual
1073 : INTEGER, INTENT(IN) :: dimen_RI, dimen_RI_red
1074 : INTEGER, DIMENSION(:), INTENT(IN) :: dimen_ia
1075 : INTEGER, INTENT(IN) :: dimen_nm_gw
1076 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :), &
1077 : INTENT(INOUT) :: Eigenval
1078 : INTEGER, INTENT(IN) :: num_integ_points, num_integ_group, &
1079 : color_rpa_group
1080 : TYPE(cp_fm_type), INTENT(IN) :: fm_matrix_PQ
1081 : TYPE(cp_fm_type), DIMENSION(:), INTENT(INOUT) :: fm_mat_S
1082 : TYPE(cp_fm_type), DIMENSION(:), INTENT(IN) :: fm_mat_Q_gemm, fm_mat_Q, fm_mat_S_gw
1083 : TYPE(cp_fm_type), INTENT(IN) :: fm_mat_R_gw, fm_mat_S_ij_bse, &
1084 : fm_mat_S_ab_bse
1085 : LOGICAL, INTENT(IN) :: my_do_gw, do_bse
1086 : INTEGER, DIMENSION(:), INTENT(IN) :: gw_corr_lev_occ, gw_corr_lev_virt
1087 : INTEGER, INTENT(IN) :: bse_lev_virt
1088 : LOGICAL, INTENT(IN) :: do_minimax_quad, do_im_time
1089 : TYPE(cp_fm_type), DIMENSION(:), INTENT(IN) :: mo_coeff
1090 : TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:, :) :: fm_matrix_L_kpoints, &
1091 : fm_matrix_Minv_L_kpoints, &
1092 : fm_matrix_Minv, &
1093 : fm_matrix_Minv_Vtrunc_Minv
1094 : TYPE(dbcsr_p_type), INTENT(IN) :: mat_munu
1095 : TYPE(dbcsr_p_type), INTENT(INOUT) :: mat_P_global
1096 : TYPE(dbt_type), INTENT(INOUT) :: t_3c_M
1097 : TYPE(dbt_type), ALLOCATABLE, DIMENSION(:, :), &
1098 : INTENT(INOUT) :: t_3c_O
1099 : TYPE(hfx_compression_type), ALLOCATABLE, &
1100 : DIMENSION(:, :, :), INTENT(INOUT) :: t_3c_O_compressed
1101 : TYPE(block_ind_type), ALLOCATABLE, &
1102 : DIMENSION(:, :, :), INTENT(INOUT) :: t_3c_O_ind
1103 : INTEGER, ALLOCATABLE, DIMENSION(:), INTENT(IN) :: starts_array_mc, ends_array_mc, &
1104 : starts_array_mc_block, &
1105 : ends_array_mc_block
1106 : TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_s
1107 : LOGICAL :: do_kpoints_from_Gamma
1108 : TYPE(kpoint_type), POINTER :: kpoints
1109 : TYPE(group_dist_d1_type), INTENT(IN) :: gd_array
1110 : INTEGER, INTENT(IN) :: color_sub
1111 : LOGICAL, INTENT(IN) :: do_ri_sos_laplace_mp2, calc_forces
1112 :
1113 : CHARACTER(LEN=*), PARAMETER :: routineN = 'rpa_num_int'
1114 :
1115 : COMPLEX(KIND=dp), ALLOCATABLE, &
1116 306 : DIMENSION(:, :, :, :) :: vec_Sigma_c_gw
1117 : INTEGER :: count_ev_sc_GW, cut_memory, group_size_P, gw_corr_lev_tot, handle, handle3, i, &
1118 : ikp_local, ispin, iter_evGW, iter_sc_GW0, j, jquad, min_bsize, mm_style, nkp, &
1119 : nkp_self_energy, nmo, nspins, num_3c_repl, num_cells_dm, num_fit_points, Pspin, Qspin, &
1120 : size_P
1121 : INTEGER(int_8) :: dbcsr_nflop
1122 306 : INTEGER, ALLOCATABLE, DIMENSION(:, :) :: index_to_cell_3c
1123 306 : INTEGER, ALLOCATABLE, DIMENSION(:, :, :) :: cell_to_index_3c
1124 612 : INTEGER, DIMENSION(:), POINTER :: col_blk_size, prim_blk_sizes, &
1125 306 : RI_blk_sizes
1126 : LOGICAL :: do_apply_ic_corr_to_gw, do_gw_im_time, do_ic_model, do_kpoints_cubic_RPA, &
1127 : do_periodic, do_print, do_ri_Sigma_x, exit_ev_gw, first_cycle, &
1128 : first_cycle_periodic_correction, my_open_shell, print_ic_values
1129 306 : LOGICAL, ALLOCATABLE, DIMENSION(:, :, :, :, :) :: has_mat_P_blocks
1130 : REAL(KIND=dp) :: a_scaling, alpha, dbcsr_time, e_exchange, e_exchange_corr, eps_filter, &
1131 : eps_filter_im_time, ext_scaling, fermi_level_offset, fermi_level_offset_input, &
1132 : my_flop_rate, omega, omega_max_fit, omega_old, tau, tau_old
1133 612 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: delta_corr, e_fermi, tau_tj, tau_wj, tj, &
1134 306 : trace_Qomega, vec_omega_fit_gw, wj, &
1135 306 : wkp_W
1136 306 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: vec_W_gw, weights_cos_tf_t_to_w, &
1137 306 : weights_cos_tf_w_to_t, &
1138 306 : weights_sin_tf_t_to_w
1139 306 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :) :: Eigenval_last, Eigenval_scf, &
1140 306 : vec_Sigma_x_gw
1141 : TYPE(cp_cfm_type) :: cfm_mat_Q
1142 : TYPE(cp_fm_type) :: fm_mat_Q_static_bse_gemm, fm_mat_RI_global_work, fm_mat_S_ia_bse, &
1143 : fm_mat_work, fm_mo_coeff_occ_scaled, fm_mo_coeff_virt_scaled, fm_scaled_dm_occ_tau, &
1144 : fm_scaled_dm_virt_tau
1145 306 : TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:) :: fm_mat_S_gw_work, fm_mat_W, &
1146 306 : fm_mo_coeff_occ, fm_mo_coeff_virt
1147 306 : TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:, :) :: fm_mat_L_kpoints, fm_mat_Minv_L_kpoints
1148 : TYPE(dbcsr_p_type) :: mat_dm, mat_L, mat_M_P_munu_occ, &
1149 : mat_M_P_munu_virt, mat_MinvVMinv
1150 : TYPE(dbcsr_p_type), ALLOCATABLE, &
1151 306 : DIMENSION(:, :, :) :: mat_P_omega
1152 306 : TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_berry_im_mo_mo, &
1153 306 : matrix_berry_re_mo_mo
1154 306 : TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: mat_P_omega_kp
1155 : TYPE(dbcsr_type), POINTER :: mat_W, mat_work
1156 2142 : TYPE(dbt_type) :: t_3c_overl_int_ao_mo
1157 306 : TYPE(dbt_type), ALLOCATABLE, DIMENSION(:) :: t_3c_overl_int_gw_AO, &
1158 306 : t_3c_overl_int_gw_RI, &
1159 306 : t_3c_overl_nnP_ic, &
1160 306 : t_3c_overl_nnP_ic_reflected
1161 : TYPE(dgemm_counter_type) :: dgemm_counter
1162 : TYPE(hfx_compression_type), ALLOCATABLE, &
1163 306 : DIMENSION(:) :: t_3c_O_mo_compressed
1164 22644 : TYPE(im_time_force_type) :: force_data
1165 306 : TYPE(rpa_exchange_work_type) :: exchange_work
1166 1530 : TYPE(rpa_grad_type) :: rpa_grad
1167 306 : TYPE(rpa_sigma_type) :: rpa_sigma
1168 306 : TYPE(two_dim_int_array), ALLOCATABLE, DIMENSION(:) :: t_3c_O_mo_ind
1169 :
1170 306 : CALL timeset(routineN, handle)
1171 :
1172 306 : nspins = SIZE(homo)
1173 306 : nmo = homo(1) + virtual(1)
1174 :
1175 306 : my_open_shell = (nspins == 2)
1176 :
1177 306 : do_gw_im_time = my_do_gw .AND. do_im_time
1178 306 : do_ri_Sigma_x = mp2_env%ri_g0w0%do_ri_Sigma_x
1179 306 : do_ic_model = mp2_env%ri_g0w0%do_ic_model
1180 306 : print_ic_values = mp2_env%ri_g0w0%print_ic_values
1181 306 : do_periodic = mp2_env%ri_g0w0%do_periodic
1182 306 : do_kpoints_cubic_RPA = mp2_env%ri_rpa_im_time%do_im_time_kpoints
1183 :
1184 : ! For SOS-MP2 only gemm is implemented
1185 306 : mm_style = wfc_mm_style_gemm
1186 306 : IF (.NOT. do_ri_sos_laplace_mp2) mm_style = mp2_env%ri_rpa%mm_style
1187 :
1188 306 : IF (my_do_gw) THEN
1189 108 : ext_scaling = 0.2_dp
1190 108 : omega_max_fit = mp2_env%ri_g0w0%omega_max_fit
1191 108 : fermi_level_offset_input = mp2_env%ri_g0w0%fermi_level_offset
1192 108 : iter_evGW = mp2_env%ri_g0w0%iter_evGW
1193 108 : iter_sc_GW0 = mp2_env%ri_g0w0%iter_sc_GW0
1194 108 : IF ((.NOT. do_im_time)) THEN
1195 62 : IF (iter_sc_GW0 /= 1 .AND. iter_evGW /= 1) CPABORT("Mixed scGW0/evGW not implemented.")
1196 : ! in case of scGW0 with the N^4 algorithm, we use the evGW code but use the DFT eigenvalues for W
1197 62 : IF (iter_sc_GW0 /= 1) iter_evGW = iter_sc_GW0
1198 : END IF
1199 : ELSE
1200 198 : ext_scaling = 0.0_dp
1201 198 : iter_evGW = 1
1202 198 : iter_sc_GW0 = 1
1203 : END IF
1204 :
1205 306 : IF (do_kpoints_cubic_RPA .AND. do_ri_sos_laplace_mp2) THEN
1206 0 : CPABORT("RI-SOS-Laplace-MP2 with k-point-sampling is not implemented.")
1207 : END IF
1208 :
1209 306 : do_apply_ic_corr_to_gw = .FALSE.
1210 306 : IF (mp2_env%ri_g0w0%ic_corr_list(1)%array(1) > 0.0_dp) do_apply_ic_corr_to_gw = .TRUE.
1211 :
1212 306 : IF (do_im_time) THEN
1213 136 : CPASSERT(do_minimax_quad .OR. do_ri_sos_laplace_mp2)
1214 :
1215 136 : group_size_P = mp2_env%ri_rpa_im_time%group_size_P
1216 136 : cut_memory = mp2_env%ri_rpa_im_time%cut_memory
1217 136 : eps_filter = mp2_env%ri_rpa_im_time%eps_filter
1218 : eps_filter_im_time = mp2_env%ri_rpa_im_time%eps_filter* &
1219 136 : mp2_env%ri_rpa_im_time%eps_filter_factor
1220 :
1221 136 : min_bsize = mp2_env%ri_rpa_im_time%min_bsize
1222 :
1223 : CALL alloc_im_time(qs_env, para_env, dimen_RI, dimen_RI_red, &
1224 : num_integ_points, nspins, fm_mat_Q(1), fm_mo_coeff_occ, fm_mo_coeff_virt, &
1225 : fm_matrix_Minv_L_kpoints, fm_matrix_L_kpoints, mat_P_global, &
1226 : t_3c_O, matrix_s, kpoints, eps_filter_im_time, &
1227 : cut_memory, nkp, num_cells_dm, num_3c_repl, &
1228 : size_P, ikp_local, &
1229 : index_to_cell_3c, &
1230 : cell_to_index_3c, &
1231 : col_blk_size, &
1232 : do_ic_model, do_kpoints_cubic_RPA, &
1233 : do_kpoints_from_Gamma, do_ri_Sigma_x, my_open_shell, &
1234 : has_mat_P_blocks, wkp_W, &
1235 : cfm_mat_Q, fm_mat_Minv_L_kpoints, fm_mat_L_kpoints, &
1236 : fm_mat_RI_global_work, fm_mat_work, fm_mo_coeff_occ_scaled, &
1237 : fm_mo_coeff_virt_scaled, mat_dm, mat_L, mat_M_P_munu_occ, mat_M_P_munu_virt, &
1238 : mat_MinvVMinv, mat_P_omega, mat_P_omega_kp, mat_work, mo_coeff, &
1239 136 : fm_scaled_dm_occ_tau, fm_scaled_dm_virt_tau, homo, nmo)
1240 :
1241 136 : IF (calc_forces) CALL init_im_time_forces(force_data, fm_matrix_PQ, t_3c_M, unit_nr, mp2_env, qs_env)
1242 :
1243 136 : IF (my_do_gw) THEN
1244 :
1245 : CALL dbcsr_get_info(mat_P_global%matrix, &
1246 46 : row_blk_size=RI_blk_sizes)
1247 :
1248 : CALL dbcsr_get_info(matrix_s(1)%matrix, &
1249 46 : row_blk_size=prim_blk_sizes)
1250 :
1251 46 : gw_corr_lev_tot = gw_corr_lev_occ(1) + gw_corr_lev_virt(1)
1252 :
1253 46 : IF (.NOT. do_kpoints_cubic_RPA) THEN
1254 : CALL allocate_matrices_gw_im_time(gw_corr_lev_occ, gw_corr_lev_virt, homo, nmo, &
1255 : num_integ_points, unit_nr, &
1256 : RI_blk_sizes, do_ic_model, &
1257 : para_env, fm_mat_W, fm_mat_Q(1), &
1258 : mo_coeff, &
1259 : t_3c_overl_int_ao_mo, t_3c_O_mo_compressed, t_3c_O_mo_ind, &
1260 : t_3c_overl_int_gw_RI, t_3c_overl_int_gw_AO, &
1261 : starts_array_mc, ends_array_mc, &
1262 : t_3c_overl_nnP_ic, t_3c_overl_nnP_ic_reflected, &
1263 : matrix_s, mat_W, t_3c_O, &
1264 : t_3c_O_compressed, t_3c_O_ind, &
1265 46 : qs_env)
1266 :
1267 : END IF
1268 : END IF
1269 :
1270 : END IF
1271 306 : IF (do_ic_model) THEN
1272 : ! image charge model only implemented for cubic scaling GW
1273 2 : CPASSERT(do_gw_im_time)
1274 2 : CPASSERT(.NOT. do_periodic)
1275 2 : IF (cut_memory /= 1) CPABORT("For IC, use MEMORY_CUT 1 in the LOW_SCALING section.")
1276 : END IF
1277 :
1278 918 : ALLOCATE (e_fermi(nspins))
1279 306 : IF (do_minimax_quad .OR. do_ri_sos_laplace_mp2) THEN
1280 206 : do_print = .NOT. do_ic_model
1281 : CALL get_minimax_grid(para_env, unit_nr, homo, Eigenval, num_integ_points, do_im_time, &
1282 : do_ri_sos_laplace_mp2, do_print, &
1283 : tau_tj, tau_wj, qs_env, do_gw_im_time, &
1284 : do_kpoints_cubic_RPA, e_fermi(1), tj, wj, &
1285 : weights_cos_tf_t_to_w, weights_cos_tf_w_to_t, weights_sin_tf_t_to_w, &
1286 206 : qs_env%mp2_env%ri_g0w0%regularization_minimax)
1287 :
1288 : !For sos_laplace_mp2 and low-scaling RPA, potentially need to store/retrieve the initial weights
1289 206 : IF (qs_env%mp2_env%ri_rpa_im_time%keep_quad) THEN
1290 : CALL keep_initial_quad(tj, wj, tau_tj, tau_wj, weights_cos_tf_t_to_w, &
1291 : weights_cos_tf_w_to_t, do_ri_sos_laplace_mp2, do_im_time, &
1292 206 : num_integ_points, unit_nr, qs_env)
1293 : END IF
1294 : ELSE
1295 100 : IF (calc_forces) CPABORT("Forces with Clenshaw-Curtis grid not implemented.")
1296 : CALL get_clenshaw_grid(para_env, para_env_RPA, unit_nr, homo, virtual, Eigenval, num_integ_points, &
1297 : num_integ_group, color_rpa_group, fm_mat_S, my_do_gw, &
1298 100 : ext_scaling, a_scaling, tj, wj)
1299 : END IF
1300 :
1301 : ! This array is needed for RPA
1302 306 : IF (.NOT. do_ri_sos_laplace_mp2) THEN
1303 744 : ALLOCATE (trace_Qomega(dimen_RI_red))
1304 : END IF
1305 :
1306 306 : IF (do_ri_sos_laplace_mp2 .AND. .NOT. do_im_time) THEN
1307 28 : alpha = 1.0_dp
1308 278 : ELSE IF (my_open_shell .OR. do_ri_sos_laplace_mp2) THEN
1309 72 : alpha = 2.0_dp
1310 : ELSE
1311 206 : alpha = 4.0_dp
1312 : END IF
1313 306 : IF (my_do_gw) THEN
1314 : CALL allocate_matrices_gw(vec_Sigma_c_gw, color_rpa_group, dimen_nm_gw, &
1315 : gw_corr_lev_occ, gw_corr_lev_virt, homo, &
1316 : nmo, num_integ_group, num_integ_points, unit_nr, &
1317 : gw_corr_lev_tot, num_fit_points, omega_max_fit, &
1318 : do_minimax_quad, do_periodic, do_ri_Sigma_x,.NOT. do_im_time, &
1319 : first_cycle_periodic_correction, &
1320 : a_scaling, Eigenval, tj, vec_omega_fit_gw, vec_Sigma_x_gw, &
1321 : delta_corr, Eigenval_last, Eigenval_scf, vec_W_gw, &
1322 : fm_mat_S_gw, fm_mat_S_gw_work, &
1323 : para_env, mp2_env, kpoints, nkp, nkp_self_energy, &
1324 108 : do_kpoints_cubic_RPA, do_kpoints_from_Gamma)
1325 :
1326 108 : IF (do_bse) THEN
1327 :
1328 34 : CALL cp_fm_create(fm_mat_Q_static_bse_gemm, fm_mat_Q_gemm(1)%matrix_struct)
1329 34 : CALL cp_fm_to_fm(fm_mat_Q_gemm(1), fm_mat_Q_static_bse_gemm)
1330 34 : CALL cp_fm_set_all(fm_mat_Q_static_bse_gemm, 0.0_dp)
1331 :
1332 : END IF
1333 :
1334 : END IF
1335 :
1336 306 : IF (calc_forces .AND. .NOT. do_im_time) CALL rpa_grad_create(rpa_grad, fm_mat_Q(1), &
1337 : fm_mat_S, homo, virtual, mp2_env, Eigenval(:, 1, :), &
1338 44 : unit_nr, do_ri_sos_laplace_mp2)
1339 306 : IF (.NOT. do_im_time .AND. .NOT. do_ri_sos_laplace_mp2) THEN
1340 : CALL exchange_work%create(qs_env, para_env_sub, mat_munu, dimen_RI_red, &
1341 142 : fm_mat_S, fm_mat_Q(1), fm_mat_Q_gemm(1), homo, virtual)
1342 : END IF
1343 306 : Erpa = 0.0_dp
1344 306 : IF (mp2_env%ri_rpa%exchange_correction /= rpa_exchange_none) e_exchange = 0.0_dp
1345 306 : first_cycle = .TRUE.
1346 306 : omega_old = 0.0_dp
1347 306 : CALL dgemm_counter_init(dgemm_counter, unit_nr, mp2_env%ri_rpa%print_dgemm_info)
1348 :
1349 722 : DO count_ev_sc_GW = 1, iter_evGW
1350 436 : dbcsr_time = 0.0_dp
1351 436 : dbcsr_nflop = 0
1352 :
1353 436 : IF (do_ic_model) CYCLE
1354 :
1355 : ! reset some values, important when doing eigenvalue self-consistent GW
1356 434 : IF (my_do_gw) THEN
1357 236 : Erpa = 0.0_dp
1358 236 : vec_Sigma_c_gw = z_zero
1359 236 : first_cycle = .TRUE.
1360 : END IF
1361 :
1362 : ! calculate Q_PQ(it)
1363 434 : IF (do_im_time) THEN ! not using Imaginary time
1364 :
1365 148 : IF (.NOT. do_kpoints_cubic_RPA) THEN
1366 312 : DO ispin = 1, nspins
1367 312 : e_fermi(ispin) = (Eigenval(homo(ispin), 1, ispin) + Eigenval(homo(ispin) + 1, 1, ispin))*0.5_dp
1368 : END DO
1369 : END IF
1370 :
1371 148 : tau = 0.0_dp
1372 148 : tau_old = 0.0_dp
1373 :
1374 148 : IF (unit_nr > 0) WRITE (UNIT=unit_nr, FMT="(/T3,A,T66,i15)") &
1375 74 : "MEMORY_INFO| Memory cut:", cut_memory
1376 148 : IF (unit_nr > 0) WRITE (UNIT=unit_nr, FMT="(T3,A,T66,ES15.2)") &
1377 74 : "SPARSITY_INFO| Eps filter for M virt/occ tensors:", eps_filter
1378 148 : IF (unit_nr > 0) WRITE (UNIT=unit_nr, FMT="(T3,A,T66,ES15.2)") &
1379 74 : "SPARSITY_INFO| Eps filter for P matrix:", eps_filter_im_time
1380 148 : IF (unit_nr > 0) WRITE (UNIT=unit_nr, FMT="(T3,A,T66,i15)") &
1381 74 : "SPARSITY_INFO| Minimum tensor block size:", min_bsize
1382 :
1383 : ! for evGW, we have to ensure that mat_P_omega is zero
1384 148 : CALL zero_mat_P_omega(mat_P_omega(:, :, 1))
1385 :
1386 : ! compute the matrix Q(it) and Fourier transform it directly to mat_P_omega(iw)
1387 : CALL compute_mat_P_omega(mat_P_omega(:, :, 1), fm_scaled_dm_occ_tau, &
1388 : fm_scaled_dm_virt_tau, fm_mo_coeff_occ(1), fm_mo_coeff_virt(1), &
1389 : fm_mo_coeff_occ_scaled, fm_mo_coeff_virt_scaled, &
1390 : mat_P_global, matrix_s, 1, &
1391 : t_3c_M, t_3c_O, t_3c_O_compressed, t_3c_O_ind, &
1392 : starts_array_mc, ends_array_mc, &
1393 : starts_array_mc_block, ends_array_mc_block, &
1394 : weights_cos_tf_t_to_w, tj, tau_tj, e_fermi(1), eps_filter, alpha, &
1395 : eps_filter_im_time, Eigenval(:, 1, 1), nmo, &
1396 : num_integ_points, cut_memory, &
1397 : unit_nr, mp2_env, para_env, &
1398 : qs_env, do_kpoints_from_Gamma, &
1399 : index_to_cell_3c, cell_to_index_3c, &
1400 : has_mat_P_blocks, do_ri_sos_laplace_mp2, &
1401 148 : dbcsr_time, dbcsr_nflop)
1402 :
1403 : ! the same for open shell, use fm_mo_coeff_occ_beta and fm_mo_coeff_virt_beta
1404 148 : IF (my_open_shell) THEN
1405 28 : CALL zero_mat_P_omega(mat_P_omega(:, :, 2))
1406 : CALL compute_mat_P_omega(mat_P_omega(:, :, 2), fm_scaled_dm_occ_tau, &
1407 : fm_scaled_dm_virt_tau, fm_mo_coeff_occ(2), &
1408 : fm_mo_coeff_virt(2), &
1409 : fm_mo_coeff_occ_scaled, fm_mo_coeff_virt_scaled, &
1410 : mat_P_global, matrix_s, 2, &
1411 : t_3c_M, t_3c_O, t_3c_O_compressed, t_3c_O_ind, &
1412 : starts_array_mc, ends_array_mc, &
1413 : starts_array_mc_block, ends_array_mc_block, &
1414 : weights_cos_tf_t_to_w, tj, tau_tj, e_fermi(2), eps_filter, alpha, &
1415 : eps_filter_im_time, Eigenval(:, 1, 2), nmo, &
1416 : num_integ_points, cut_memory, &
1417 : unit_nr, mp2_env, para_env, &
1418 : qs_env, do_kpoints_from_Gamma, &
1419 : index_to_cell_3c, cell_to_index_3c, &
1420 : has_mat_P_blocks, do_ri_sos_laplace_mp2, &
1421 28 : dbcsr_time, dbcsr_nflop)
1422 :
1423 : !For RPA, we sum up the P matrices. If no force needed, can clean-up the beta spin one
1424 28 : IF (.NOT. do_ri_sos_laplace_mp2) THEN
1425 90 : DO j = 1, SIZE(mat_P_omega, 2)
1426 598 : DO i = 1, SIZE(mat_P_omega, 1)
1427 508 : CALL dbcsr_add(mat_P_omega(i, j, 1)%matrix, mat_P_omega(i, j, 2)%matrix, 1.0_dp, 1.0_dp)
1428 578 : IF (.NOT. calc_forces) CALL dbcsr_clear(mat_P_omega(i, j, 2)%matrix)
1429 : END DO
1430 : END DO
1431 : END IF
1432 : END IF ! my_open_shell
1433 :
1434 : END IF ! do im time
1435 :
1436 434 : IF (mp2_env%ri_rpa%sigma_param /= sigma_none) THEN
1437 10 : CALL rpa_sigma_create(rpa_sigma, mp2_env%ri_rpa%sigma_param, fm_mat_Q(1), unit_nr, para_env)
1438 : END IF
1439 :
1440 13064 : DO jquad = 1, num_integ_points
1441 12630 : IF (MODULO(jquad, num_integ_group) /= color_rpa_group) CYCLE
1442 :
1443 11897 : CALL timeset(routineN//"_RPA_matrix_operations", handle3)
1444 :
1445 11897 : IF (do_ri_sos_laplace_mp2) THEN
1446 176 : omega = tau_tj(jquad)
1447 : ELSE
1448 11721 : IF (do_minimax_quad) THEN
1449 1191 : omega = tj(jquad)
1450 : ELSE
1451 10530 : omega = a_scaling/TAN(tj(jquad))
1452 : END IF
1453 : END IF ! do_ri_sos_laplace_mp2
1454 :
1455 11897 : IF (do_im_time) THEN
1456 : ! in case we do imag time, we already calculated fm_mat_Q by a Fourier transform from im. time
1457 :
1458 1180 : IF (.NOT. (do_kpoints_cubic_RPA .OR. do_kpoints_from_Gamma)) THEN
1459 :
1460 2324 : DO ispin = 1, SIZE(mat_P_omega, 3)
1461 : CALL contract_P_omega_with_mat_L(mat_P_omega(jquad, 1, ispin)%matrix, mat_L%matrix, mat_work, &
1462 : eps_filter_im_time, fm_mat_work, dimen_RI, dimen_RI_red, &
1463 2324 : fm_mat_Minv_L_kpoints(1, 1), fm_mat_Q(ispin))
1464 : END DO
1465 : END IF
1466 :
1467 : ELSE
1468 10717 : IF (unit_nr > 0) WRITE (UNIT=unit_nr, FMT="(T3, A, 1X, I3, 1X, A, 1X, I3)") &
1469 5355 : "INTEG_INFO| Started with Integration point", jquad, "of", num_integ_points
1470 :
1471 10717 : IF (first_cycle .AND. count_ev_sc_gw > 1) THEN
1472 116 : IF (iter_sc_gw0 == 1) THEN
1473 124 : DO ispin = 1, nspins
1474 : CALL remove_scaling_factor_rpa(fm_mat_S(ispin), virtual(ispin), &
1475 124 : Eigenval_last(:, 1, ispin), homo(ispin), omega_old)
1476 : END DO
1477 : ELSE
1478 116 : DO ispin = 1, nspins
1479 : CALL remove_scaling_factor_rpa(fm_mat_S(ispin), virtual(ispin), &
1480 116 : Eigenval_scf(:, 1, ispin), homo(ispin), omega_old)
1481 : END DO
1482 : END IF
1483 : END IF
1484 :
1485 10717 : IF (iter_sc_GW0 > 1) THEN
1486 12140 : DO ispin = 1, nspins
1487 : CALL calc_mat_Q(fm_mat_S(ispin), do_ri_sos_laplace_mp2, first_cycle, virtual(ispin), &
1488 : Eigenval_scf(:, 1, ispin), homo(ispin), omega, omega_old, jquad, mm_style, &
1489 : dimen_RI_red, dimen_ia(ispin), alpha, fm_mat_Q(ispin), &
1490 : fm_mat_Q_gemm(ispin), do_bse, fm_mat_Q_static_bse_gemm, dgemm_counter, &
1491 12140 : num_integ_points, count_ev_sc_GW)
1492 : END DO
1493 :
1494 : ! For SOS-MP2 we need both matrices separately
1495 6070 : IF (.NOT. do_ri_sos_laplace_mp2) THEN
1496 6070 : DO ispin = 2, nspins
1497 6070 : CALL cp_fm_scale_and_add(alpha=1.0_dp, matrix_a=fm_mat_Q(1), beta=1.0_dp, matrix_b=fm_mat_Q(ispin))
1498 : END DO
1499 : END IF
1500 : ELSE
1501 9488 : DO ispin = 1, nspins
1502 : CALL calc_mat_Q(fm_mat_S(ispin), do_ri_sos_laplace_mp2, first_cycle, virtual(ispin), &
1503 : Eigenval(:, 1, ispin), homo(ispin), omega, omega_old, jquad, mm_style, &
1504 : dimen_RI_red, dimen_ia(ispin), alpha, fm_mat_Q(ispin), &
1505 : fm_mat_Q_gemm(ispin), do_bse, fm_mat_Q_static_bse_gemm, dgemm_counter, &
1506 9488 : num_integ_points, count_ev_sc_GW)
1507 : END DO
1508 :
1509 : ! For SOS-MP2 we need both matrices separately
1510 4647 : IF (.NOT. do_ri_sos_laplace_mp2) THEN
1511 4753 : DO ispin = 2, nspins
1512 4753 : CALL cp_fm_scale_and_add(alpha=1.0_dp, matrix_a=fm_mat_Q(1), beta=1.0_dp, matrix_b=fm_mat_Q(ispin))
1513 : END DO
1514 : END IF
1515 :
1516 : END IF
1517 :
1518 : END IF ! im time
1519 :
1520 : ! Calculate RPA exchange energy correction
1521 11897 : IF (mp2_env%ri_rpa%exchange_correction /= rpa_exchange_none) THEN
1522 12 : e_exchange_corr = 0.0_dp
1523 12 : CALL exchange_work%compute(fm_mat_Q(1), Eigenval(:, 1, :), fm_mat_S, omega, e_exchange_corr, mp2_env)
1524 :
1525 : ! Evaluate the final exchange energy correction
1526 12 : e_exchange = e_exchange + e_exchange_corr*wj(jquad)
1527 : END IF
1528 :
1529 : ! for developing Sigma functional closed and open shell are taken cared for
1530 11897 : IF (mp2_env%ri_rpa%sigma_param /= sigma_none) THEN
1531 30 : CALL rpa_sigma_matrix_spectral(rpa_sigma, fm_mat_Q(1), wj(jquad), para_env_RPA)
1532 : END IF
1533 :
1534 11897 : IF (do_ri_sos_laplace_mp2) THEN
1535 :
1536 176 : CALL SOS_MP2_postprocessing(fm_mat_Q, Erpa, tau_wj(jquad))
1537 :
1538 176 : IF (calc_forces .AND. .NOT. do_im_time) CALL rpa_grad_matrix_operations(mp2_env, rpa_grad, do_ri_sos_laplace_mp2, &
1539 : fm_mat_Q, fm_mat_Q_gemm, dgemm_counter, fm_mat_S, omega, homo, virtual, &
1540 50 : Eigenval(:, 1, :), tau_wj(jquad), unit_nr)
1541 : ELSE
1542 11721 : IF (calc_forces .AND. .NOT. do_im_time) CALL rpa_grad_copy_Q(fm_mat_Q(1), rpa_grad)
1543 :
1544 11721 : CALL Q_trace_and_add_unit_matrix(dimen_RI_red, trace_Qomega, fm_mat_Q(1))
1545 :
1546 11721 : IF (do_kpoints_cubic_RPA .OR. do_kpoints_from_Gamma) THEN
1547 : CALL invert_eps_compute_W_and_Erpa_kp(dimen_RI, num_integ_points, jquad, nkp, count_ev_sc_GW, para_env, &
1548 : Erpa, tau_tj, tj, wj, weights_cos_tf_w_to_t, &
1549 : wkp_W, do_gw_im_time, do_ri_Sigma_x, do_kpoints_from_Gamma, &
1550 : cfm_mat_Q, ikp_local, &
1551 : mat_P_omega(:, :, 1), mat_P_omega_kp, qs_env, eps_filter_im_time, unit_nr, &
1552 : kpoints, fm_mat_Minv_L_kpoints, fm_mat_L_kpoints, &
1553 : fm_mat_W, fm_mat_RI_global_work, mat_MinvVMinv, &
1554 132 : fm_matrix_Minv, fm_matrix_Minv_Vtrunc_Minv)
1555 : ELSE
1556 11589 : CALL compute_Erpa_by_freq_int(dimen_RI_red, trace_Qomega, fm_mat_Q(1), para_env_RPA, Erpa, wj(jquad))
1557 : END IF
1558 :
1559 11721 : IF (calc_forces .AND. .NOT. do_im_time) CALL rpa_grad_matrix_operations(mp2_env, rpa_grad, do_ri_sos_laplace_mp2, &
1560 : fm_mat_Q, fm_mat_Q_gemm, dgemm_counter, fm_mat_S, omega, homo, virtual, &
1561 56 : Eigenval(:, 1, :), wj(jquad), unit_nr)
1562 : END IF ! do_ri_sos_laplace_mp2
1563 :
1564 : ! save omega and reset the first_cycle flag
1565 11897 : first_cycle = .FALSE.
1566 11897 : omega_old = omega
1567 :
1568 11897 : CALL timestop(handle3)
1569 :
1570 11897 : IF (my_do_gw) THEN
1571 :
1572 11108 : CALL get_fermi_level_offset(fermi_level_offset, fermi_level_offset_input, Eigenval(:, 1, :), homo)
1573 :
1574 : ! do_im_time = TRUE means low-scaling calculation
1575 11108 : IF (do_im_time) THEN
1576 : ! only for molecules
1577 818 : IF (.NOT. (do_kpoints_cubic_RPA .OR. do_kpoints_from_Gamma)) THEN
1578 : CALL compute_W_cubic_GW(fm_mat_W, fm_mat_Q(1), fm_mat_work, dimen_RI, fm_mat_Minv_L_kpoints, num_integ_points, &
1579 722 : tj, tau_tj, weights_cos_tf_w_to_t, jquad, omega)
1580 : END IF
1581 : ELSE
1582 : CALL compute_GW_self_energy(vec_Sigma_c_gw, dimen_nm_gw, dimen_RI_red, gw_corr_lev_occ, &
1583 : gw_corr_lev_virt, homo, jquad, nmo, num_fit_points, &
1584 : do_im_time, do_periodic, first_cycle_periodic_correction, &
1585 : fermi_level_offset, &
1586 : omega, Eigenval(:, 1, :), delta_corr, vec_omega_fit_gw, vec_W_gw, wj, &
1587 : fm_mat_Q(1), fm_mat_R_gw, fm_mat_S_gw, &
1588 : fm_mat_S_gw_work, mo_coeff(1), para_env, &
1589 : para_env_RPA, matrix_berry_im_mo_mo, matrix_berry_re_mo_mo, &
1590 10290 : kpoints, qs_env, mp2_env)
1591 : END IF
1592 : END IF
1593 :
1594 11897 : IF (unit_nr > 0) CALL m_flush(unit_nr)
1595 24961 : CALL para_env_RPA%sync() ! sync to see output
1596 :
1597 : END DO ! jquad
1598 :
1599 434 : IF (mp2_env%ri_rpa%sigma_param /= sigma_none) THEN
1600 10 : CALL finalize_rpa_sigma(rpa_sigma, unit_nr, mp2_env%ri_rpa%e_sigma_corr, para_env, do_minimax_quad)
1601 10 : IF (do_minimax_quad) mp2_env%ri_rpa%e_sigma_corr = mp2_env%ri_rpa%e_sigma_corr/2.0_dp
1602 10 : CALL para_env%sum(mp2_env%ri_rpa%e_sigma_corr)
1603 : END IF
1604 :
1605 434 : CALL para_env%sum(Erpa)
1606 :
1607 434 : IF (.NOT. (do_ri_sos_laplace_mp2)) THEN
1608 376 : Erpa = Erpa/(pi*2.0_dp)
1609 376 : IF (do_minimax_quad) Erpa = Erpa/2.0_dp
1610 : END IF
1611 :
1612 434 : IF (mp2_env%ri_rpa%exchange_correction /= rpa_exchange_none) THEN
1613 12 : CALL para_env%sum(E_exchange)
1614 12 : E_exchange = E_exchange/(pi*2.0_dp)
1615 12 : IF (do_minimax_quad) E_exchange = E_exchange/2.0_dp
1616 12 : mp2_env%ri_rpa%ener_exchange = E_exchange
1617 : END IF
1618 :
1619 434 : IF (calc_forces .AND. do_ri_sos_laplace_mp2 .AND. do_im_time) THEN
1620 22 : IF (my_open_shell) THEN
1621 4 : Pspin = 1
1622 4 : Qspin = 2
1623 : CALL calc_laplace_loop_forces(force_data, mat_P_omega(:, 1, :), t_3c_M, t_3c_O(1, 1), &
1624 : t_3c_O_compressed(1, 1, :), t_3c_O_ind(1, 1, :), fm_scaled_dm_occ_tau, &
1625 : fm_scaled_dm_virt_tau, fm_mo_coeff_occ, fm_mo_coeff_virt, &
1626 : fm_mo_coeff_occ_scaled, fm_mo_coeff_virt_scaled, &
1627 : starts_array_mc, ends_array_mc, starts_array_mc_block, &
1628 : ends_array_mc_block, num_integ_points, nmo, Eigenval(:, 1, :), &
1629 : tau_tj, tau_wj, cut_memory, Pspin, Qspin, my_open_shell, &
1630 4 : unit_nr, dbcsr_time, dbcsr_nflop, mp2_env, qs_env)
1631 4 : Pspin = 2
1632 4 : Qspin = 1
1633 : CALL calc_laplace_loop_forces(force_data, mat_P_omega(:, 1, :), t_3c_M, t_3c_O(1, 1), &
1634 : t_3c_O_compressed(1, 1, :), t_3c_O_ind(1, 1, :), fm_scaled_dm_occ_tau, &
1635 : fm_scaled_dm_virt_tau, fm_mo_coeff_occ, fm_mo_coeff_virt, &
1636 : fm_mo_coeff_occ_scaled, fm_mo_coeff_virt_scaled, &
1637 : starts_array_mc, ends_array_mc, starts_array_mc_block, &
1638 : ends_array_mc_block, num_integ_points, nmo, Eigenval(:, 1, :), &
1639 : tau_tj, tau_wj, cut_memory, Pspin, Qspin, my_open_shell, &
1640 4 : unit_nr, dbcsr_time, dbcsr_nflop, mp2_env, qs_env)
1641 :
1642 : ELSE
1643 18 : Pspin = 1
1644 18 : Qspin = 1
1645 : CALL calc_laplace_loop_forces(force_data, mat_P_omega(:, 1, :), t_3c_M, t_3c_O(1, 1), &
1646 : t_3c_O_compressed(1, 1, :), t_3c_O_ind(1, 1, :), fm_scaled_dm_occ_tau, &
1647 : fm_scaled_dm_virt_tau, fm_mo_coeff_occ, fm_mo_coeff_virt, &
1648 : fm_mo_coeff_occ_scaled, fm_mo_coeff_virt_scaled, &
1649 : starts_array_mc, ends_array_mc, starts_array_mc_block, &
1650 : ends_array_mc_block, num_integ_points, nmo, Eigenval(:, 1, :), &
1651 : tau_tj, tau_wj, cut_memory, Pspin, Qspin, my_open_shell, &
1652 18 : unit_nr, dbcsr_time, dbcsr_nflop, mp2_env, qs_env)
1653 : END IF
1654 22 : CALL calc_post_loop_forces(force_data, unit_nr, qs_env)
1655 : END IF !laplace SOS-MP2
1656 :
1657 434 : IF (calc_forces .AND. do_im_time .AND. .NOT. do_ri_sos_laplace_mp2) THEN
1658 64 : DO ispin = 1, nspins
1659 : CALL calc_rpa_loop_forces(force_data, mat_P_omega(:, 1, :), t_3c_M, t_3c_O(1, 1), &
1660 : t_3c_O_compressed(1, 1, :), t_3c_O_ind(1, 1, :), fm_scaled_dm_occ_tau, &
1661 : fm_scaled_dm_virt_tau, fm_mo_coeff_occ, fm_mo_coeff_virt, &
1662 : fm_mo_coeff_occ_scaled, fm_mo_coeff_virt_scaled, &
1663 : starts_array_mc, ends_array_mc, starts_array_mc_block, &
1664 : ends_array_mc_block, num_integ_points, nmo, Eigenval(:, 1, :), &
1665 : e_fermi(ispin), weights_cos_tf_t_to_w, weights_cos_tf_w_to_t, tj, &
1666 : wj, tau_tj, cut_memory, ispin, my_open_shell, unit_nr, dbcsr_time, &
1667 64 : dbcsr_nflop, mp2_env, qs_env)
1668 : END DO
1669 28 : CALL calc_post_loop_forces(force_data, unit_nr, qs_env)
1670 : END IF
1671 :
1672 434 : IF (do_im_time) THEN
1673 :
1674 148 : my_flop_rate = REAL(dbcsr_nflop, dp)/(1.0E09_dp*dbcsr_time)
1675 148 : IF (unit_nr > 0) WRITE (UNIT=unit_nr, FMT="(/T3,A,T73,ES8.2)") &
1676 74 : "PERFORMANCE| DBCSR total number of flops:", REAL(dbcsr_nflop*para_env%num_pe, dp)
1677 148 : IF (unit_nr > 0) WRITE (UNIT=unit_nr, FMT="(T3,A,T66,F15.2)") &
1678 74 : "PERFORMANCE| DBCSR total execution time:", dbcsr_time
1679 148 : IF (unit_nr > 0) WRITE (UNIT=unit_nr, FMT="(T3,A,T66,F15.2)") &
1680 74 : "PERFORMANCE| DBCSR flop rate (Gflops / MPI rank):", my_flop_rate
1681 :
1682 : ELSE
1683 :
1684 286 : CALL dgemm_counter_write(dgemm_counter, para_env)
1685 :
1686 : END IF
1687 :
1688 : ! GW: for low-scaling calculation: Compute self-energy Sigma(i*tau), Sigma(i*omega)
1689 : ! for low-scaling and ordinary-scaling: analytic continuation from Sigma(iw) -> Sigma(w)
1690 : ! and correction of quasiparticle energies e_n^GW
1691 740 : IF (my_do_gw) THEN
1692 :
1693 : CALL compute_QP_energies(vec_Sigma_c_gw, count_ev_sc_GW, gw_corr_lev_occ, &
1694 : gw_corr_lev_tot, gw_corr_lev_virt, homo, &
1695 : nmo, num_fit_points, num_integ_points, &
1696 : unit_nr, do_apply_ic_corr_to_gw, do_im_time, &
1697 : do_periodic, do_ri_Sigma_x, first_cycle_periodic_correction, &
1698 : e_fermi, eps_filter, fermi_level_offset, &
1699 : delta_corr, Eigenval, &
1700 : Eigenval_last, Eigenval_scf, iter_sc_GW0, exit_ev_gw, tau_tj, tj, &
1701 : vec_omega_fit_gw, vec_Sigma_x_gw, mp2_env%ri_g0w0%ic_corr_list, &
1702 : weights_cos_tf_t_to_w, weights_sin_tf_t_to_w, &
1703 : fm_mo_coeff_occ_scaled, fm_mo_coeff_virt_scaled, fm_mo_coeff_occ, &
1704 : fm_mo_coeff_virt, fm_scaled_dm_occ_tau, fm_scaled_dm_virt_tau, &
1705 : mo_coeff(1), fm_mat_W, para_env, para_env_RPA, mat_dm, mat_MinvVMinv, &
1706 : t_3c_O, t_3c_M, t_3c_overl_int_ao_mo, t_3c_O_compressed, t_3c_O_mo_compressed, &
1707 : t_3c_O_ind, t_3c_O_mo_ind, &
1708 : t_3c_overl_int_gw_RI, t_3c_overl_int_gw_AO, &
1709 : matrix_berry_im_mo_mo, matrix_berry_re_mo_mo, mat_W, matrix_s, &
1710 : kpoints, mp2_env, qs_env, nkp_self_energy, do_kpoints_cubic_RPA, &
1711 236 : starts_array_mc, ends_array_mc)
1712 :
1713 : ! if HOMO-LUMO gap differs by less than mp2_env%ri_g0w0%eps_ev_sc_iter, exit ev sc GW loop
1714 236 : IF (exit_ev_gw) EXIT
1715 :
1716 : END IF ! my_do_gw if
1717 :
1718 : END DO ! evGW loop
1719 :
1720 306 : IF (do_ic_model) THEN
1721 :
1722 2 : IF (my_open_shell) THEN
1723 :
1724 : CALL calculate_ic_correction(Eigenval(:, 1, 1), mat_MinvVMinv%matrix, &
1725 : t_3c_overl_nnP_ic(1), t_3c_overl_nnP_ic_reflected(1), &
1726 : gw_corr_lev_tot, &
1727 : gw_corr_lev_occ(1), gw_corr_lev_virt(1), homo(1), unit_nr, &
1728 0 : print_ic_values, para_env, do_alpha=.TRUE.)
1729 :
1730 : CALL calculate_ic_correction(Eigenval(:, 1, 2), mat_MinvVMinv%matrix, &
1731 : t_3c_overl_nnP_ic(2), t_3c_overl_nnP_ic_reflected(2), &
1732 : gw_corr_lev_tot, &
1733 : gw_corr_lev_occ(2), gw_corr_lev_virt(2), homo(2), unit_nr, &
1734 0 : print_ic_values, para_env, do_beta=.TRUE.)
1735 :
1736 : ELSE
1737 :
1738 : CALL calculate_ic_correction(Eigenval(:, 1, 1), mat_MinvVMinv%matrix, &
1739 : t_3c_overl_nnP_ic(1), t_3c_overl_nnP_ic_reflected(1), &
1740 : gw_corr_lev_tot, &
1741 : gw_corr_lev_occ(1), gw_corr_lev_virt(1), homo(1), unit_nr, &
1742 2 : print_ic_values, para_env)
1743 :
1744 : END IF
1745 :
1746 : END IF
1747 :
1748 : ! postprocessing after GW for Bethe-Salpeter
1749 306 : IF (do_bse) THEN
1750 : ! Check used GW flavor; in Case of evGW we use W0 for BSE
1751 : ! Use environment variable, since local iter_evGW is overwritten if evGW0 is invoked
1752 34 : IF (mp2_env%ri_g0w0%iter_evGW > 1) THEN
1753 4 : IF (unit_nr > 0) THEN
1754 : CALL cp_warn(__LOCATION__, &
1755 2 : "BSE@evGW applies W0, i.e. screening with DFT energies to the BSE!")
1756 : END IF
1757 : END IF
1758 : ! Create a copy of fm_mat_S for usage in BSE
1759 34 : CALL cp_fm_create(fm_mat_S_ia_bse, fm_mat_S(1)%matrix_struct)
1760 34 : CALL cp_fm_to_fm(fm_mat_S(1), fm_mat_S_ia_bse)
1761 : ! Remove energy/frequency factor from 3c-Integral for BSE
1762 34 : IF (iter_sc_gw0 == 1) THEN
1763 : CALL remove_scaling_factor_rpa(fm_mat_S_ia_bse, virtual(1), &
1764 22 : Eigenval_last(:, 1, 1), homo(1), omega)
1765 : ELSE
1766 : CALL remove_scaling_factor_rpa(fm_mat_S_ia_bse, virtual(1), &
1767 12 : Eigenval_scf(:, 1, 1), homo(1), omega)
1768 : END IF
1769 : ! Main routine for all BSE postprocessing
1770 : CALL start_bse_calculation(fm_mat_S_ia_bse, fm_mat_S_ij_bse, fm_mat_S_ab_bse, &
1771 : fm_mat_Q_static_bse_gemm, &
1772 : Eigenval, Eigenval_scf, &
1773 : homo, virtual, dimen_RI, dimen_RI_red, bse_lev_virt, &
1774 34 : gd_array, color_sub, mp2_env, qs_env, mo_coeff, unit_nr)
1775 : ! Release BSE-copy of fm_mat_S
1776 34 : CALL cp_fm_release(fm_mat_S_ia_bse)
1777 : END IF
1778 :
1779 306 : IF (my_do_gw) THEN
1780 : CALL deallocate_matrices_gw(fm_mat_S_gw_work, vec_W_gw, vec_Sigma_c_gw, vec_omega_fit_gw, &
1781 : mp2_env%ri_g0w0%vec_Sigma_x_minus_vxc_gw, &
1782 : Eigenval_last, Eigenval_scf, do_periodic, matrix_berry_re_mo_mo, matrix_berry_im_mo_mo, &
1783 108 : kpoints, vec_Sigma_x_gw,.NOT. do_im_time)
1784 : END IF
1785 :
1786 306 : IF (do_im_time) THEN
1787 :
1788 : CALL dealloc_im_time(fm_mo_coeff_occ, fm_mo_coeff_virt, &
1789 : fm_scaled_dm_occ_tau, fm_scaled_dm_virt_tau, index_to_cell_3c, &
1790 : cell_to_index_3c, do_ic_model, &
1791 : do_kpoints_cubic_RPA, do_kpoints_from_Gamma, do_ri_Sigma_x, &
1792 : has_mat_P_blocks, &
1793 : wkp_W, cfm_mat_Q, fm_mat_Minv_L_kpoints, fm_mat_L_kpoints, &
1794 : fm_matrix_Minv, fm_matrix_Minv_Vtrunc_Minv, fm_mat_RI_global_work, fm_mat_work, &
1795 : fm_mo_coeff_occ_scaled, fm_mo_coeff_virt_scaled, mat_dm, mat_L, &
1796 : mat_MinvVMinv, mat_P_omega, mat_P_omega_kp, &
1797 136 : t_3c_M, t_3c_O, t_3c_O_compressed, t_3c_O_ind, mat_work, qs_env)
1798 :
1799 136 : IF (my_do_gw) THEN
1800 : CALL deallocate_matrices_gw_im_time(weights_cos_tf_w_to_t, weights_sin_tf_t_to_w, do_ic_model, &
1801 : do_kpoints_cubic_RPA, fm_mat_W, &
1802 : t_3c_overl_int_ao_mo, t_3c_O_mo_compressed, t_3c_O_mo_ind, &
1803 : t_3c_overl_int_gw_RI, t_3c_overl_int_gw_AO, &
1804 : t_3c_overl_nnP_ic, t_3c_overl_nnP_ic_reflected, &
1805 46 : mat_W, qs_env)
1806 : END IF
1807 :
1808 : END IF
1809 :
1810 306 : IF (.NOT. do_im_time .AND. .NOT. do_ri_sos_laplace_mp2) CALL exchange_work%release()
1811 :
1812 306 : IF (.NOT. do_ri_sos_laplace_mp2) THEN
1813 248 : DEALLOCATE (tj)
1814 248 : DEALLOCATE (wj)
1815 248 : DEALLOCATE (trace_Qomega)
1816 : END IF
1817 :
1818 306 : IF (do_im_time .OR. do_ri_sos_laplace_mp2) THEN
1819 164 : DEALLOCATE (tau_tj)
1820 164 : DEALLOCATE (tau_wj)
1821 : END IF
1822 :
1823 306 : IF (do_im_time .AND. calc_forces) THEN
1824 50 : CALL im_time_force_release(force_data)
1825 : END IF
1826 :
1827 306 : IF (calc_forces .AND. .NOT. do_im_time) CALL rpa_grad_finalize(rpa_grad, mp2_env, para_env_sub, para_env, &
1828 : qs_env, gd_array, color_sub, do_ri_sos_laplace_mp2, &
1829 44 : homo, virtual)
1830 :
1831 306 : CALL timestop(handle)
1832 :
1833 6242 : END SUBROUTINE rpa_num_int
1834 :
1835 : END MODULE rpa_main
|