(git:27c966b)
Loading...
Searching...
No Matches
gw_utils.F
Go to the documentation of this file.
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
10!> \par History
11!> 01.2026 Maximilian Graml: add more bounds to exploit sparsity in 3c integrals, fixes
12!> \author Jan Wilhelm
13!> \date 07.2023
14! **************************************************************************************************
23 USE bibliography, ONLY: graml2024,&
24 cite_reference
25 USE cell_types, ONLY: cell_type,&
26 pbc,&
38 USE cp_dbcsr_api, ONLY: dbcsr_create,&
40 dbcsr_set,&
41 dbcsr_type_symmetric
46 USE cp_files, ONLY: close_file,&
52 USE cp_fm_types, ONLY: cp_fm_create,&
61 USE dbt_api, ONLY: &
62 dbt_clear, dbt_create, dbt_destroy, dbt_filter, dbt_iterator_blocks_left, &
63 dbt_iterator_next_block, dbt_iterator_start, dbt_iterator_stop, dbt_iterator_type, &
64 dbt_mp_environ_pgrid, dbt_pgrid_create, dbt_pgrid_destroy, dbt_pgrid_type, dbt_type
70 USE gw_utils_fm, ONLY: cfm_contract_aba,&
71 fm_invert,&
73 USE input_constants, ONLY: &
84 USE kinds, ONLY: default_path_length,&
86 dp,&
87 int_8
89 USE kpoint_types, ONLY: kpoint_create,&
92 USE machine, ONLY: m_walltime
93 USE mathlib, ONLY: erfc_cutoff,&
94 gcd
95 USE message_passing, ONLY: mp_cart_type,&
99 USE mp2_gpw, ONLY: create_mat_munu
100 USE mp2_ri_2c, ONLY: ri_2c_integral_mat,&
104 USE physcon, ONLY: angstrom,&
105 evolt
115 USE qs_kind_types, ONLY: get_qs_kind,&
125 USE qs_rho_types, ONLY: qs_rho_type
126 USE qs_tensors, ONLY: build_3c_integrals,&
135 USE ri_rs_types, ONLY: ri_rs_env,&
137 USE rpa_gw, ONLY: continuation_pade
140 USE util, ONLY: sort
141#include "base/base_uses.f90"
142
143 IMPLICIT NONE
144
145 PRIVATE
146
149 compute_xkp, &
151 add_r, &
155
156 CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'gw_utils'
157
158CONTAINS
159
160! **************************************************************************************************
161!> \brief Initializes the GW environment from the input and electronic-structure data.
162!> \param qs_env ...
163!> \param bs_env Band-structure environment containing GW parameters.
164!> \param bs_sec BAND_STRUCTURE input section
165! **************************************************************************************************
166 SUBROUTINE create_and_init_bs_env_for_gw(qs_env, bs_env, bs_sec)
167 TYPE(qs_environment_type), POINTER :: qs_env
168 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
169 TYPE(section_vals_type), POINTER :: bs_sec
170
171 CHARACTER(LEN=*), PARAMETER :: routinen = 'create_and_init_bs_env_for_gw'
172
173 INTEGER :: handle
174
175 CALL timeset(routinen, handle)
176
177 CALL cite_reference(graml2024)
178
179 CALL get_parameters_from_qs_env(qs_env, bs_env)
180
181 CALL read_gw_input_parameters(bs_env, bs_sec)
182
183 CALL print_header_and_input_parameters(bs_env)
184
185 CALL setup_ao_and_ri_basis_set(qs_env, bs_env)
186
187 CALL set_heuristic_parameters(bs_env, qs_env)
188
189 IF (bs_env%auto_ri%enabled) CALL generate_auto_ri_basis(qs_env, bs_env)
190
191 CALL get_ri_basis(qs_env, bs_env)
192
193 CALL compute_v_xc(qs_env, bs_env)
194
195 CALL init_interaction_radii(bs_env)
196
197 ! The RI-RS drivers build their (µν|P) blocks on the fly through gw_3c_ctx
198 ! RT-BSE with the AO-RI kernel need nl_3c it after GW, so
199 ! keep everything when an RT-BSE run follows.
200 IF (.NOT. bs_env%do_gw_ri_rs .OR. &
201 bs_env%rtp_method == rtp_method_bse .OR. &
202 bs_env%rtp_method == rtp_method_bse_linearized) THEN
203 CALL create_tensors(qs_env, bs_env)
204 END IF
205
206 CALL allocate_gw_eigenvalues(bs_env)
207
208 SELECT CASE (bs_env%gw_implementation)
210
211 CALL check_sparsity_3c(qs_env, bs_env)
212
213 CALL set_sparsity_parallelization_parameters(bs_env)
214
215 CALL check_for_restart_files(qs_env, bs_env)
216
218
219 CALL check_for_restart_files(qs_env, bs_env)
220
222
223 CALL compute_3c_integrals(qs_env, bs_env)
224
225 CALL setup_cells_delta_r(bs_env)
226
227 CALL setup_parallelization_delta_r(bs_env)
228
229 CALL allocate_matrices_small_cell_full_kp_tensor(qs_env, bs_env)
230
231 CALL trafo_v_xc_r_to_kp(qs_env, bs_env)
232
233 CALL heuristic_ri_regularization(qs_env, bs_env)
234
236
237 CALL trafo_v_xc_r_to_kp(qs_env, bs_env)
238
239 CALL prepare_ri_rs_gamma_correction(qs_env, bs_env)
240
241 END SELECT
242
243 CALL setup_time_and_frequency_minimax_grid(bs_env)
244
245 ! Free memory in qs_env. SCF real-space grids, task lists, neighbor
246 ! lists and work arrays on large systems hold GBs per rank for the whole run.
247 ! Not required by GW. LDOS and RT-BSE still needs these information.
248 !
249 ! Recommendation in case of memory issues: first perform GW calculation without calculating
250 ! LDOS (to save memory). Then, use GW restart files
251 ! in a subsequent calculation to calculate the LDOS
252 IF (.NOT. bs_env%do_ldos .AND. &
253 bs_env%rtp_method /= rtp_method_bse .AND. &
254 bs_env%rtp_method /= rtp_method_bse_linearized) THEN
255 CALL qs_env_part_release(qs_env)
256 END IF
257
258 CALL timestop(handle)
259
260 END SUBROUTINE create_and_init_bs_env_for_gw
261
262! **************************************************************************************************
263!> \brief Store the QS environment parameters needed by GW.
264!> \param qs_env ...
265!> \param bs_env ...
266! **************************************************************************************************
267 SUBROUTINE get_parameters_from_qs_env(qs_env, bs_env)
268 TYPE(qs_environment_type), POINTER :: qs_env
269 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
270
271 TYPE(section_vals_type), POINTER :: input, rtbse_sec
272
273 NULLIFY (input, rtbse_sec)
274 CALL get_qs_env(qs_env, atomic_kind_set=bs_env%ri_rs%atomic_kind_set, &
275 cell=bs_env%ri_rs%cell, particle_set=bs_env%ri_rs%particle_set, input=input)
276 rtbse_sec => section_vals_get_subs_vals(input, "DFT%REAL_TIME_PROPAGATION%RTBSE")
277 CALL section_vals_val_get(rtbse_sec, "CUTOFF_RADIUS_W0", r_val=bs_env%ri_rs%cutoff_radius_w0)
278
279 END SUBROUTINE get_parameters_from_qs_env
280
281! **************************************************************************************************
282!> \brief Reads the GW input parameters.
283!> \param bs_env ...
284!> \param bs_sec BAND_STRUCTURE input section
285! **************************************************************************************************
286 SUBROUTINE read_gw_input_parameters(bs_env, bs_sec)
287 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
288 TYPE(section_vals_type), POINTER :: bs_sec
289
290 CHARACTER(LEN=*), PARAMETER :: routinen = 'read_gw_input_parameters'
291
292 INTEGER :: handle
293 LOGICAL :: do_evgw0
294 TYPE(section_vals_type), POINTER :: auto_ri_sec, cohsex_sec, evgw0_sec, &
295 grid_opt_sec, gw_sec, ri_rs_sec
296
297 CALL timeset(routinen, handle)
298
299 NULLIFY (auto_ri_sec, cohsex_sec, evgw0_sec, grid_opt_sec, gw_sec, ri_rs_sec)
300 gw_sec => section_vals_get_subs_vals(bs_sec, "GW")
301 auto_ri_sec => section_vals_get_subs_vals(gw_sec, "AUTO_RI")
302 cohsex_sec => section_vals_get_subs_vals(gw_sec, "COHSEX")
303 evgw0_sec => section_vals_get_subs_vals(gw_sec, "EVGW0")
304 ri_rs_sec => section_vals_get_subs_vals(gw_sec, "RI_RS")
305 grid_opt_sec => section_vals_get_subs_vals(ri_rs_sec, "GRID_OPTIMIZATION")
306
307 CALL section_vals_val_get(gw_sec, "NUM_TIME_FREQ_POINTS", i_val=bs_env%num_time_freq_points)
308 CALL section_vals_val_get(gw_sec, "EPS_FILTER", r_val=bs_env%eps_filter)
309 CALL section_vals_val_get(gw_sec, "REGULARIZATION_RI", r_val=bs_env%input_regularization_RI)
310 CALL section_vals_val_get(gw_sec, "REGULARIZATION_MINIMAX", r_val=bs_env%input_regularization_minimax)
311 CALL section_vals_val_get(gw_sec, "CUTOFF_RADIUS_RI", r_val=bs_env%ri_metric%cutoff_radius)
312 CALL section_vals_val_get(gw_sec, "RI_CUTOFF_ATTENUATION", r_val=bs_env%ri_metric%omega)
313 CALL section_vals_val_get(gw_sec, "MEMORY_PER_PROC", r_val=bs_env%input_memory_per_proc_GB)
314 CALL section_vals_val_get(gw_sec, "APPROX_KP_EXTRAPOL", l_val=bs_env%approx_kp_extrapol)
315 CALL section_vals_val_get(gw_sec, "SIZE_LATTICE_SUM", i_val=bs_env%size_lattice_sum_V)
316 CALL section_vals_val_get(gw_sec, "KPOINTS_W", i_vals=bs_env%nkp_grid_chi_eps_W_input)
317 CALL section_vals_val_get(gw_sec, "HEDIN_SHIFT", l_val=bs_env%do_hedin_shift)
318 CALL section_vals_val_get(gw_sec, "FREQ_MAX_FIT", r_val=bs_env%freq_max_fit)
319 CALL section_vals_val_get(gw_sec, "ANALYTIC_CONTINUATION", i_val=bs_env%ac_method)
320 CALL section_vals_val_get(cohsex_sec, "SCALE_OCC", r_val=bs_env%cohsex_scale_occ)
321 CALL section_vals_val_get(cohsex_sec, "SCALE_VIRT", r_val=bs_env%cohsex_scale_virt)
322 CALL read_rirs_input(bs_env%ri_rs, ri_rs_sec)
323 CALL read_rirs_grid_opt(bs_env%ri_rs, grid_opt_sec)
324 CALL read_evgw0_input(bs_env, evgw0_sec, do_evgw0)
325 CALL read_auto_ri_input(bs_env%auto_ri, auto_ri_sec)
326 CALL check_gw_input(bs_env, do_evgw0)
327
328 CALL timestop(handle)
329
330 END SUBROUTINE read_gw_input_parameters
331
332! **************************************************************************************************
333!> \brief Reads the RI-RS input.
334!> \param ri_rs RI-RS calculation parameters
335!> \param ri_rs_sec RI_RS input section
336! **************************************************************************************************
337 SUBROUTINE read_rirs_input(ri_rs, ri_rs_sec)
338 TYPE(ri_rs_env), INTENT(INOUT) :: ri_rs
339 TYPE(section_vals_type), POINTER :: ri_rs_sec
340
341 CHARACTER(LEN=*), PARAMETER :: routinen = 'read_rirs_input'
342
343 INTEGER :: handle
344
345 CALL timeset(routinen, handle)
346
347 CALL section_vals_val_get(ri_rs_sec, "TIKHONOV", r_val=ri_rs%tikhonov)
348 CALL section_vals_val_get(ri_rs_sec, "GRID_SELECT", i_val=ri_rs%grid_select)
349 CALL section_vals_val_get(ri_rs_sec, "CUTOFF_RADIUS_RL_AO", r_val=ri_rs%cutoff_radius_ri_ao)
350 CALL section_vals_val_get(ri_rs_sec, "CUTOFF_RADIUS_RL_W", r_val=ri_rs%cutoff_radius_v_w)
351
352 CALL timestop(handle)
353
354 END SUBROUTINE read_rirs_input
355
356! **************************************************************************************************
357!> \brief Read the RI-RS grid optimization input.
358!> \param ri_rs RI-RS parameters receiving the optimized-grid settings
359!> \param grid_opt_sec GRID_OPTIMIZATION input section
360! **************************************************************************************************
361 SUBROUTINE read_rirs_grid_opt(ri_rs, grid_opt_sec)
362 TYPE(ri_rs_env), INTENT(INOUT), TARGET :: ri_rs
363 TYPE(section_vals_type), POINTER :: grid_opt_sec
364
365 CHARACTER(LEN=*), PARAMETER :: routinen = 'read_rirs_grid_opt'
366
367 INTEGER :: handle
368 TYPE(ri_rs_grid_opt_type), POINTER :: grid_opt
369
370 CALL timeset(routinen, handle)
371
372 CALL section_vals_get(grid_opt_sec, explicit=ri_rs%grid_opt%enabled)
373 IF (.NOT. ri_rs%grid_opt%enabled) THEN
374 CALL timestop(handle)
375 RETURN
376 END IF
377
378 NULLIFY (grid_opt)
379 grid_opt => ri_rs%grid_opt
380
381 CALL section_vals_val_get(grid_opt_sec, "CUTOFF_ATOMIC_CLUSTER", r_val=grid_opt%cutoff_atomic_cluster)
382 CALL section_vals_val_get(grid_opt_sec, "MAX_ITER", i_val=grid_opt%max_iter)
383 CALL section_vals_val_get(grid_opt_sec, "RS_AO_RATIO", r_val=grid_opt%rs_ao_ratio)
384
385 CALL timestop(handle)
386
387 END SUBROUTINE read_rirs_grid_opt
388
389! **************************************************************************************************
390!> \brief Reads the eigenvalue-self-consistent GW input.
391!> \param bs_env GW environment receiving the evGW0 convergence settings
392!> \param evgw0_sec EVGW0 input section
393!> \param do_evgw0 whether the EVGW0 section is activated
394! **************************************************************************************************
395 SUBROUTINE read_evgw0_input(bs_env, evgw0_sec, do_evgw0)
396 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
397 TYPE(section_vals_type), POINTER :: evgw0_sec
398 LOGICAL, INTENT(OUT) :: do_evgw0
399
400 CHARACTER(LEN=*), PARAMETER :: routinen = 'read_evgw0_input'
401
402 INTEGER :: handle
403
404 CALL timeset(routinen, handle)
405
406 CALL section_vals_val_get(evgw0_sec, "_SECTION_PARAMETERS_", l_val=do_evgw0)
407 CALL section_vals_val_get(evgw0_sec, "MAX_ITER", i_val=bs_env%evgw0_n_iter)
408 CALL section_vals_val_get(evgw0_sec, "EPS_ITER", r_val=bs_env%evgw0_eps_iter)
409
410 CALL timestop(handle)
411
412 END SUBROUTINE read_evgw0_input
413
414! **************************************************************************************************
415!> \brief Reads the automatic RI input.
416!> \param auto_ri automatic RI basis optimization parameters
417!> \param auto_ri_sec AUTO_RI input section
418! **************************************************************************************************
419 SUBROUTINE read_auto_ri_input(auto_ri, auto_ri_sec)
420 TYPE(auto_ri_type), INTENT(INOUT) :: auto_ri
421 TYPE(section_vals_type), POINTER :: auto_ri_sec
422
423 CHARACTER(LEN=*), PARAMETER :: routinen = 'read_auto_ri_input'
424
425 INTEGER :: handle
426
427 CALL timeset(routinen, handle)
428
429 CALL section_vals_get(auto_ri_sec, explicit=auto_ri%enabled)
430 IF (.NOT. auto_ri%enabled) THEN
431 CALL timestop(handle)
432 RETURN
433 END IF
434
435 CALL section_vals_val_get(auto_ri_sec, "RI_AO_RATIO", r_val=auto_ri%ri_ao_ratio)
436 CALL section_vals_val_get(auto_ri_sec, "OCC_EMPTY_FRONTIER_ORBITAL_WINDOW", r_val=auto_ri%occ_energy_window)
437
438 CALL timestop(handle)
439
440 END SUBROUTINE read_auto_ri_input
441
442! **************************************************************************************************
443!> \brief Checks compatibility among the GW input sections and assigns the GW flavour.
444!> \param bs_env ...
445!> \param do_evgw0 whether eigenvalue self-consistency is requested
446! **************************************************************************************************
447 SUBROUTINE check_gw_input(bs_env, do_evgw0)
448 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
449 LOGICAL, INTENT(IN) :: do_evgw0
450
451 CHARACTER(LEN=*), PARAMETER :: routinen = 'check_gw_input'
452
453 INTEGER :: handle
454
455 CALL timeset(routinen, handle)
456
457 ! GRID_OPTIMIZATION is available only with RI-RS.
458 cpassert(.NOT. bs_env%ri_rs%grid_opt%enabled .OR. bs_env%do_gw_ri_rs)
459 IF (bs_env%ri_rs%grid_opt%enabled) THEN
460 IF (bs_env%ri_rs%grid_opt%rs_ao_ratio <= 0.0_dp) cpabort("RS_AO_RATIO must be positive")
461 IF (bs_env%ri_rs%grid_opt%max_iter < 1) THEN
462 cpabort("GRID_OPTIMIZATION%MAX_ITER must be positive")
463 END IF
464 IF (bs_env%ri_rs%grid_opt%cutoff_atomic_cluster == 0.0_dp) THEN
465 cpabort("CUTOFF_ATOMIC_CLUSTER must be nonzero")
466 END IF
467 END IF
468 IF (bs_env%do_gw_ri_rs) THEN
469 IF (bs_env%ri_rs%tikhonov < 0.0_dp) THEN
470 cpabort("RI_RS%TIKHONOV must not be negative")
471 END IF
472 IF (bs_env%gw_implementation == ri_rs_small_cell_full_kp) THEN
473 IF (.NOT. bs_env%ri_rs%grid_opt%enabled) THEN
474 cpabort("Full-k RI-RS GW requires RI_RS%GRID_OPTIMIZATION")
475 END IF
476 IF (bs_env%size_lattice_sum_V < 1) THEN
477 cpabort("GW%SIZE_LATTICE_SUM must be positive")
478 END IF
479 END IF
480 END IF
481 IF (bs_env%gw_implementation == ri_rs_small_cell_full_kp) THEN
482 IF (.NOT. (bs_env%ri_metric%omega > 0.0_dp)) THEN
483 cpabort("RI_CUTOFF_ATTENUATION must be positive for periodic RI-RS GW")
484 END IF
485 IF (.NOT. (all(bs_env%periodic == [1, 1, 0]) .OR. all(bs_env%periodic == 1))) THEN
486 cpabort("Periodic RI-RS GW requires PERIODIC XY or XYZ")
487 END IF
488 IF (bs_env%approx_kp_extrapol) THEN
489 cpabort("APPROX_KP_EXTRAPOL cannot be combined with RI-RS GW")
490 END IF
491 END IF
492 IF (.NOT. do_evgw0) bs_env%evgw0_n_iter = 1
493 IF (do_evgw0 .AND. bs_env%evgw0_eps_iter <= 0.0_dp) THEN
494 cpabort("EVGW0%EPS_ITER must be positive")
495 END IF
496
497 ! evGW0 implemented so far only with RI-RS.
498 cpassert(.NOT. do_evgw0 .OR. bs_env%do_gw_ri_rs)
499 IF (bs_env%auto_ri%enabled) THEN
500 ! AUTO_RI is available only with RI-RS.
501 cpassert(bs_env%do_gw_ri_rs)
502 IF (bs_env%do_periodic .AND. &
503 bs_env%gw_implementation /= ri_rs_small_cell_full_kp) THEN
504 cpabort('Periodic AUTO_RI requires RI_RS_SMALL_CELL_FULL_KP')
505 END IF
506 IF (.NOT. (bs_env%auto_ri%ri_ao_ratio > 0.0_dp)) cpabort("AUTO_RI%RI_AO_RATIO must be positive")
507 IF (.NOT. (bs_env%auto_ri%occ_energy_window >= 0.0_dp)) THEN
508 cpabort("AUTO_RI%OCC_EMPTY_FRONTIER_ORBITAL_WINDOW must be nonnegative")
509 END IF
510 END IF
511
512 bs_env%gw_flavour = g0w0
513 IF (do_evgw0) THEN
514 bs_env%gw_flavour = evgw0
515 CALL cp_warn(__location__, &
516 "evGW0 in the RI-RS GW code is experimental. The quasiparticle energies "// &
517 "of the previous cycle are fed back into the Green's function, so the "// &
518 "error of the analytic continuation propagates and accumulates over the "// &
519 "cycles instead of affecting one state only, as it does in G0W0. In "// &
520 "tests on small molecules the resulting deviation stayed below 100 meV, "// &
521 "but this has not been established in general. Use at your own risk and "// &
522 "check the convergence of the reported states.")
523 END IF
524
525 CALL resolve_memory_per_proc(bs_env)
526
527 CALL timestop(handle)
528
529 END SUBROUTINE check_gw_input
530
531! **************************************************************************************************
532!> \brief Determine memory per processor available for low-sclaing tensor GW routines.
533!>
534!> MEMORY_PER_PROC = -1 (the default) : Automatically detect available memory from the system.
535!> MEMORY_PER_PROC > 0.0 : Override with user-defined value (in GB).
536!>
537!> \param bs_env ...
538! **************************************************************************************************
539 SUBROUTINE resolve_memory_per_proc(bs_env)
540 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
541
542 CHARACTER(LEN=*), PARAMETER :: routinen = 'resolve_memory_per_proc'
543 REAL(kind=dp), PARAMETER :: detected_memory_fraction = 0.8_dp, &
544 fallback_memory_per_proc_gb = 2.0_dp
545
546 INTEGER :: handle
547 REAL(kind=dp) :: mem_free_per_proc_gb, mem_occ_per_proc_gb
548
549 CALL timeset(routinen, handle)
550
551 bs_env%auto_memory_per_proc = bs_env%input_memory_per_proc_GB <= 0.0_dp
552
553 IF (bs_env%auto_memory_per_proc) THEN
554
555 CALL mp_mem_avail_per_rank_gb(bs_env%para_env, mem_free_per_proc_gb)
556 CALL mp_mem_used_per_rank_gb(bs_env%para_env, mem_occ_per_proc_gb)
557
558 ! detected_memory_fraction is a safety net on the detected free memory
559 bs_env%input_memory_per_proc_GB = detected_memory_fraction*mem_free_per_proc_gb &
560 + mem_occ_per_proc_gb
561
562 ! mp_mem_avail_per_rank_GB returns zero if the free memory cannot be detected
563 IF (mem_free_per_proc_gb <= 0.0_dp) THEN
564 bs_env%auto_memory_per_proc = .false.
565 bs_env%input_memory_per_proc_GB = fallback_memory_per_proc_gb
566 CALL cp_warn(__location__, &
567 "Could not detect the available memory per MPI process. Falling back "// &
568 "to MEMORY_PER_PROC = 2 GB. Set MEMORY_PER_PROC in the GW section "// &
569 "explicitly for good performance.")
570 END IF
571
572 ELSE IF (bs_env%do_gw_ri_rs) THEN
573
574 CALL cp_warn(__location__, &
575 "MEMORY_PER_PROC is set, but it is not used by this RI-RS GW code path, "// &
576 "which detects the available memory per MPI process automatically. The "// &
577 "keyword is ignored.")
578
579 END IF
580
581 CALL timestop(handle)
582
583 END SUBROUTINE resolve_memory_per_proc
584
585! **************************************************************************************************
586!> \brief Prints the common GW input parameters and the RI-RS block when active.
587!> \param bs_env ...
588! **************************************************************************************************
589 SUBROUTINE print_header_and_input_parameters(bs_env)
590
591 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
592
593 CHARACTER(LEN=*), PARAMETER :: routinen = 'print_header_and_input_parameters'
594
595 INTEGER :: handle, u
596
597 CALL timeset(routinen, handle)
598
599 u = bs_env%unit_nr
600
601 IF (u > 0) THEN
602 WRITE (u, '(T2,A)') ' '
603 WRITE (u, '(T2,A)') repeat('-', 79)
604 WRITE (u, '(T2,A,A78)') '-', '-'
605 WRITE (u, '(T2,A,A46,A32)') '-', 'GW CALCULATION', '-'
606 WRITE (u, '(T2,A,A78)') '-', '-'
607 WRITE (u, '(T2,A)') repeat('-', 79)
608 WRITE (u, '(T2,A)') ' '
609 WRITE (u, '(T2,A,I45)') 'Input: Number of time/freq. points', bs_env%num_time_freq_points
610 WRITE (u, "(T2,A,F44.1,A)") 'Input: ω_max for fitting Σ(iω) (eV)', bs_env%freq_max_fit*evolt
611 IF (bs_env%ac_method == gw_ac_nevanlinna) THEN
612 WRITE (u, "(T2,A,A51)") 'Input: Analytic continuation', 'NEVANLINNA'
613 ELSE
614 WRITE (u, "(T2,A,A51)") 'Input: Analytic continuation', 'PADE'
615 END IF
616 WRITE (u, '(T2,A,ES27.1)') 'Input: Filter threshold for sparse tensor operations', &
617 bs_env%eps_filter
618 WRITE (u, "(T2,A,L55)") 'Input: Apply Hedin shift', bs_env%do_hedin_shift
619 IF (bs_env%auto_memory_per_proc) THEN
620 WRITE (u, '(T2,A,F34.1,A)') 'Detected: Available memory per MPI process', &
621 bs_env%input_memory_per_proc_GB, ' GB'
622 ELSE
623 WRITE (u, '(T2,A,F37.1,A)') 'Input: Available memory per MPI process', &
624 bs_env%input_memory_per_proc_GB, ' GB'
625 END IF
626 IF (bs_env%do_gw_ri_rs) CALL print_gw_rirs_input(bs_env)
627 IF (bs_env%do_static_cohsex) THEN
628 IF (bs_env%cohsex_flavor == cohsex_qp_update) THEN
629 WRITE (u, fmt="(T2,A,T71,A10)") "static COHSEX approximation (COHSEX)", "QP_UPDATE"
630 ELSE
631 WRITE (u, fmt="(T2,A,T71,A10)") "static COHSEX approximation (COHSEX)", "DIAGONAL"
632 END IF
633 WRITE (u, fmt="(T2,A,T74,F8.6)") "static COHSEX scaling of Σ^c, occ-occ (COHSEX%SCALE_OCC):", &
634 bs_env%cohsex_scale_occ
635 WRITE (u, fmt="(T2,A,T74,F8.6)") "static COHSEX scaling of Σ^c, virt-virt (COHSEX%SCALE_VIRT):", &
636 bs_env%cohsex_scale_virt
637 END IF
638 WRITE (u, '(A)') ' '
639 END IF
640
641 CALL timestop(handle)
642
643 END SUBROUTINE print_header_and_input_parameters
644
645! **************************************************************************************************
646!> \brief Prints the input parameters specific to an RI-RS GW calculation.
647!> \param bs_env ...
648! **************************************************************************************************
649 SUBROUTINE print_gw_rirs_input(bs_env)
650 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
651
652 CHARACTER(LEN=*), PARAMETER :: routinen = 'print_gw_rirs_input'
653
654 INTEGER :: handle, unit_nr
655
656 CALL timeset(routinen, handle)
657
658 unit_nr = bs_env%unit_nr
659
660 WRITE (unit_nr, '(A)') ' '
661 WRITE (unit_nr, '(T2,A,ES43.2)') 'Input: RI-RS Tikhonov regularization', &
662 bs_env%ri_rs%tikhonov
663 CALL print_cutoff_radius_input(unit_nr, 'AO grid hard cutoff radius', &
664 bs_env%ri_rs%cutoff_radius_ri_ao)
665 CALL print_cutoff_radius_input(unit_nr, 'G/W panel truncation radius', &
666 bs_env%ri_rs%cutoff_radius_v_w)
667 WRITE (unit_nr, '(T2,A,A62)') 'Input: GW flavour', trim(gw_flavour_label(bs_env))
668
669 IF (bs_env%ri_rs%grid_opt%enabled) THEN
670 WRITE (unit_nr, '(A)') ' '
671 WRITE (unit_nr, '(T2,A,T80,L1)') &
672 'Input: RI-RS grid optimization activated', .true.
673 WRITE (unit_nr, '(T2,A,T69,F12.1)') &
674 'Input: RS points per AO function', bs_env%ri_rs%grid_opt%rs_ao_ratio
675 IF (bs_env%ri_rs%grid_opt%cutoff_atomic_cluster < 0.0_dp) THEN
676 WRITE (unit_nr, '(T2,A,T52,A29)') &
677 'Input: Atomic cluster radius', 'Automatic: 1.4 x nearest atom'
678 ELSE
679 WRITE (unit_nr, '(T2,A,T73,F6.1,A)') &
680 'Input: Cutoff radius for atomic clusters', &
681 bs_env%ri_rs%grid_opt%cutoff_atomic_cluster*angstrom, ' Å'
682 END IF
683 WRITE (unit_nr, '(T2,A,T75,I6)') &
684 'Input: Maximum grid optimization iterations', &
685 bs_env%ri_rs%grid_opt%max_iter
686 END IF
687
688 IF (bs_env%auto_ri%enabled) THEN
689 WRITE (unit_nr, '(A)') ' '
690 WRITE (unit_nr, '(T2,A,T80,L1)') &
691 'Input: AUTO_RI basis optimization activated:', .true.
692 WRITE (unit_nr, '(T2,A,T69,F12.1)') &
693 'Input: AUTO_RI number of RI functions per AO function:', bs_env%auto_ri%ri_ao_ratio
694 WRITE (unit_nr, '(T2,A,T74,F4.1,A)') &
695 'Input: AUTO_RI frontier orbital window:', bs_env%auto_ri%occ_energy_window*evolt, ' eV'
696 END IF
697
698 IF (bs_env%gw_flavour == evgw0) THEN
699 WRITE (unit_nr, '(T2,A,I42)') 'Input: Maximum number of evGW0 cycles', &
700 bs_env%evgw0_n_iter
701 WRITE (unit_nr, '(T2,A,F42.5,A)') 'Input: evGW0 convergence threshold', &
702 bs_env%evgw0_eps_iter*evolt, ' eV'
703 END IF
704 WRITE (unit_nr, '(A)') ' '
705
706 CALL timestop(handle)
707
708 END SUBROUTINE print_gw_rirs_input
709
710! **************************************************************************************************
711!> \brief Prints a positive RI-RS cutoff radius.
712!> \param unit_nr output unit
713!> \param label description of the cutoff radius
714!> \param cutoff_radius cutoff radius in atomic units
715! **************************************************************************************************
716 SUBROUTINE print_cutoff_radius_input(unit_nr, label, cutoff_radius)
717 INTEGER, INTENT(IN) :: unit_nr
718 CHARACTER(LEN=*), INTENT(IN) :: label
719 REAL(kind=dp), INTENT(IN) :: cutoff_radius
720
721 CHARACTER(LEN=*), PARAMETER :: routinen = 'print_cutoff_radius_input'
722
723 INTEGER :: handle
724
725 CALL timeset(routinen, handle)
726
727 IF (cutoff_radius > 0.0_dp) THEN
728 WRITE (unit_nr, '(T2,2A,T73,F6.2,A)') &
729 'Input: ', trim(label), cutoff_radius*angstrom, ' Å'
730 END IF
731
732 CALL timestop(handle)
733
734 END SUBROUTINE print_cutoff_radius_input
735
736! **************************************************************************************************
737!> \brief Creates the AO and reference RI basis sets for every atomic kind.
738!> \param qs_env ...
739!> \param bs_env ...
740! **************************************************************************************************
741 SUBROUTINE setup_ao_and_ri_basis_set(qs_env, bs_env)
742 TYPE(qs_environment_type), POINTER :: qs_env
743 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
744
745 CHARACTER(LEN=*), PARAMETER :: routinen = 'setup_AO_and_RI_basis_set'
746
747 INTEGER :: handle, ikind, natom, nkind, sort_basis
748 TYPE(dft_control_type), POINTER :: dft_control
749 TYPE(gto_basis_set_type), POINTER :: reference_basis
750 TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
751 TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
752
753 CALL timeset(routinen, handle)
754
755 CALL get_qs_env(qs_env, &
756 qs_kind_set=qs_kind_set, &
757 particle_set=particle_set, &
758 natom=natom, nkind=nkind, dft_control=dft_control)
759
760 ! AUTO_RI needs a reference pool before its atom-local contraction.
761 IF (bs_env%auto_ri%enabled) THEN
762 CALL section_vals_val_get(qs_env%input, "DFT%SORT_BASIS", i_val=sort_basis)
763 DO ikind = 1, nkind
764 NULLIFY (reference_basis)
765 CALL get_qs_kind(qs_kind_set(ikind), basis_set=reference_basis, basis_type="RI_AUX")
766 IF (.NOT. ASSOCIATED(reference_basis)) THEN
767 CALL create_ri_aux_basis_set(reference_basis, qs_kind_set(ikind), &
768 dft_control%auto_basis_ri_aux, basis_sort=sort_basis)
769 IF (reference_basis%norm_type < 0) reference_basis%norm_type = 2
770 CALL init_orb_basis_set(reference_basis)
771 CALL add_basis_set_to_container(qs_kind_set(ikind)%basis_sets, reference_basis, "RI_AUX")
772 END IF
773 END DO
774 END IF
775
776 ! set up basis
777 ALLOCATE (bs_env%sizes_RI(natom), bs_env%sizes_AO(natom))
778 ALLOCATE (bs_env%basis_set_RI(nkind), bs_env%basis_set_AO(nkind))
779
780 CALL basis_set_list_setup(bs_env%basis_set_RI, "RI_AUX", qs_kind_set)
781 CALL basis_set_list_setup(bs_env%basis_set_AO, "ORB", qs_kind_set)
782
783 CALL get_particle_set(particle_set, qs_kind_set, nsgf=bs_env%sizes_RI, &
784 basis=bs_env%basis_set_RI)
785 CALL get_particle_set(particle_set, qs_kind_set, nsgf=bs_env%sizes_AO, &
786 basis=bs_env%basis_set_AO)
787
788 CALL timestop(handle)
789
790 END SUBROUTINE setup_ao_and_ri_basis_set
791
792! **************************************************************************************************
793!> \brief Sets internal GW heuristics that are not exposed as user input.
794!> \param bs_env ...
795!> \param qs_env ...
796! **************************************************************************************************
797 SUBROUTINE set_heuristic_parameters(bs_env, qs_env)
798 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
799 TYPE(qs_environment_type), OPTIONAL, POINTER :: qs_env
800
801 CHARACTER(LEN=*), PARAMETER :: routinen = 'set_heuristic_parameters'
802
803 INTEGER :: exchange_grid(3), handle, u
804 LOGICAL :: do_bvk_cell
805
806 CALL timeset(routinen, handle)
807
808 ! Use a 16-parameter Padé fit; cap it at the available fit frequencies below.
809 bs_env%nparam_pade = 16
810
811 ! for generating numerically stable minimax Fourier integration weights
812 bs_env%num_points_per_magnitude = 200
813
814 IF (bs_env%input_regularization_minimax > -1.0e-12_dp) THEN
815 bs_env%regularization_minimax = bs_env%input_regularization_minimax
816 ELSE
817 ! for periodic systems and for 20 minimax points, we use a regularized minimax mesh
818 ! (from experience: regularized minimax meshes converge faster for periodic systems
819 ! and for 20 pts)
820 IF (bs_env%do_periodic .OR. bs_env%num_time_freq_points >= 20) THEN
821 bs_env%regularization_minimax = 1.0e-6_dp
822 ELSE
823 bs_env%regularization_minimax = 0.0_dp
824 END IF
825 END IF
826
827 bs_env%stabilize_exp = 70.0_dp
828 bs_env%eps_atom_grid_2d_mat = 1.0e-50_dp
829
830 ! Nevanlinna continuation: weights of Σ^c(iτ) -> Σ^c(iω) by a truncated SVD, and the
831 ! quasiparticle solution as median over fit windows with the Newton search and the
832 ! solution of largest weight Z
833 IF (bs_env%ac_method == gw_ac_nevanlinna) THEN
834 bs_env%nevanlinna%rcond_weights = 1.0e-10_dp
835 bs_env%nevanlinna%window_min_points = 6
836 bs_env%nevanlinna%window_freq_min = 10.0_dp/evolt
837 bs_env%nevanlinna%window_freq_max = 100.0_dp/evolt
838 bs_env%nevanlinna%window_spread = 0.05_dp/evolt
839 bs_env%nevanlinna%qp_newton_max_step = 1.0_dp/evolt
840 bs_env%nevanlinna%qp_maxz_radius = 1.5_dp/evolt
841 END IF
842
843 ! Resolution of the identity with the truncated Coulomb metric.
844 bs_env%ri_metric%potential_type = do_potential_truncated
845 bs_env%ri_metric%filename = "t_c_g.dat"
846 IF (bs_env%gw_implementation == ri_rs_small_cell_full_kp) THEN
847 bs_env%ri_metric%potential_type = do_potential_short
848 CALL erfc_cutoff(1.0e-10_dp, bs_env%ri_metric%omega, bs_env%ri_metric%cutoff_radius)
849 bs_env%ri_metric%filename = ""
850 ELSE
851 bs_env%ri_metric%omega = 0.0_dp
852 END IF
853
854 bs_env%eps_eigval_mat_RI = 0.0_dp
855
856 IF (bs_env%input_regularization_RI >= 0.0_dp) THEN
857 bs_env%regularization_RI = bs_env%input_regularization_RI
858 ELSE IF (bs_env%gw_implementation == ri_rs_small_cell_full_kp) THEN
859 ! periodic RI-RS: relative eigenvalue cutoff when inverting the RI metric M(q)
860 bs_env%regularization_RI = 1.0e-8_dp
861 ELSE
862 ! default case:
863
864 ! 1. for periodic systems, we use the regularized resolution of the identity per default
865 bs_env%regularization_RI = 1.0e-2_dp
866
867 ! 2. for molecules, no regularization is necessary
868 IF (.NOT. bs_env%do_periodic) bs_env%regularization_RI = 0.0_dp
869
870 END IF
871
872 ! Coulomb operator for the exchange self-energy. Periodic systems use the truncated
873 ! operator described by Guidon, VandeVondele, and Hutter, JCTC 5, 3010 (2009).
874 IF (bs_env%do_periodic) THEN
875 do_bvk_cell = bs_env%gw_implementation == tensor_small_cell_full_kp .OR. &
876 bs_env%gw_implementation == ri_rs_small_cell_full_kp
877 SELECT CASE (bs_env%gw_implementation)
879 ! The Born-von Karman cell follows the SCF k mesh, not the AO image-list extent.
880 exchange_grid = bs_env%kpoints_scf_desymm%nkp_grid
881 CASE DEFAULT
882 exchange_grid = bs_env%cell_grid_scf_desymm
883 END SELECT
884 CALL trunc_coulomb_for_exchange(qs_env, bs_env%trunc_coulomb, &
885 rel_cutoff_trunc_coulomb_ri_x=0.5_dp, &
886 cell_grid=exchange_grid, &
887 do_bvk_cell=do_bvk_cell)
888 ELSE
889 bs_env%trunc_coulomb%potential_type = do_potential_coulomb
890 bs_env%trunc_coulomb%cutoff_radius = -1.0_dp
891 bs_env%trunc_coulomb%omega = 0.0_dp
892 bs_env%trunc_coulomb%filename = ""
893 END IF
894
895 ! for small-cell GW, we need more cells than normally used by the filter bs_env%eps_filter
896 ! (in particular for computing the self-energy because of higher number of cells needed)
897 bs_env%heuristic_filter_factor = 1.0e-4_dp
898
899 u = bs_env%unit_nr
900 IF (u > 0) THEN
901 IF (bs_env%do_periodic) THEN
902 WRITE (u, fmt="(T2,2A,F21.1,A)") "Cutoff radius for the truncated Coulomb ", &
903 "operator in Σ^x:", bs_env%trunc_coulomb%cutoff_radius*angstrom, " Å"
904 END IF
905 SELECT CASE (bs_env%ri_metric%potential_type)
906 CASE (do_potential_short)
907 WRITE (u, fmt="(T2,A,T61,F12.2,A)") "Input: Attenuation of the RI metric:", &
908 bs_env%ri_metric%omega, " bohr^-1"
910 WRITE (u, fmt="(T2,2A,F15.1,A)") "Cutoff radius for the truncated Coulomb ", &
911 "operator in RI metric:", bs_env%ri_metric%cutoff_radius*angstrom, " Å"
912 END SELECT
913 IF (bs_env%num_time_freq_points < 35) THEN
914 WRITE (u, fmt="(T2,A,ES38.1)") "Regularization parameter of minimax grids", &
915 bs_env%regularization_minimax
916 END IF
917 IF (bs_env%ac_method == gw_ac_nevanlinna) THEN
918 WRITE (u, fmt="(T2,A,ES31.1)") "SVD cutoff of minimax weights for Σ(iτ) -> Σ(iω)", &
919 bs_env%nevanlinna%rcond_weights
920 END IF
921 IF (bs_env%gw_implementation == ri_rs_small_cell_full_kp) THEN
922 WRITE (u, fmt="(T2,A,T72,A9)") "Coulomb matrix V(q) from", "Ewald sum"
923 IF (all(bs_env%periodic == [1, 1, 0])) THEN
924 WRITE (u, fmt="(T2,A,I43)") "Lattice sum size for 2D V(q=0) body:", bs_env%size_lattice_sum_V
925 END IF
926 ELSE IF (bs_env%do_periodic) THEN
927 WRITE (u, fmt="(T2,A,I53)") "Lattice sum size for V(k):", bs_env%size_lattice_sum_V
928 END IF
929 END IF
930
931 CALL timestop(handle)
932
933 END SUBROUTINE set_heuristic_parameters
934
935! **************************************************************************************************
936!> \brief Sets up the RI basis, its matrix distributions, and M^-1(k=0).
937!> \param qs_env ...
938!> \param bs_env ...
939! **************************************************************************************************
940 SUBROUTINE get_ri_basis(qs_env, bs_env)
941 TYPE(qs_environment_type), POINTER :: qs_env
942 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
943
944 CHARACTER(LEN=*), PARAMETER :: routinen = 'get_RI_basis'
945
946 INTEGER :: handle
947
948 CALL timeset(routinen, handle)
949
950 CALL set_ao_ri_basis_function_indices(qs_env, bs_env)
951 CALL setup_kpoints_chi_eps_w(bs_env, bs_env%kpoints_chi_eps_W)
952 IF (bs_env%gw_implementation == tensor_small_cell_full_kp .OR. &
953 bs_env%gw_implementation == ri_rs_small_cell_full_kp) THEN
954 CALL setup_cells_3c(qs_env, bs_env)
955 END IF
956 CALL set_parallelization_parameters(qs_env, bs_env)
957 CALL allocate_matrices(qs_env, bs_env)
958 IF (bs_env%gw_implementation /= tensor_small_cell_full_kp .AND. &
959 bs_env%gw_implementation /= ri_rs_small_cell_full_kp) THEN
960 CALL compute_minv_gamma(qs_env, bs_env)
961 END IF
962
963 CALL timestop(handle)
964
965 END SUBROUTINE get_ri_basis
966
967! **************************************************************************************************
968!> \brief Determines the AO and RI basis-function indices for every atom.
969!> \param qs_env ...
970!> \param bs_env ...
971! **************************************************************************************************
972 SUBROUTINE set_ao_ri_basis_function_indices(qs_env, bs_env)
973 TYPE(qs_environment_type), POINTER :: qs_env
974 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
975
976 CHARACTER(LEN=*), PARAMETER :: routinen = 'set_AO_RI_basis_function_indices'
977
978 INTEGER :: handle, i_ri, iatom, ikind, iset, &
979 max_ao_bf_per_atom, n_ao_test, n_atom, &
980 n_kind, n_ri, nset, u
981 INTEGER, ALLOCATABLE, DIMENSION(:) :: kind_of
982 INTEGER, DIMENSION(:), POINTER :: l_max, l_min, nsgf_set
983 TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
984 TYPE(gto_basis_set_type), POINTER :: basis
985 TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
986
987 CALL timeset(routinen, handle)
988
989 ! determine RI basis set size
990 CALL get_qs_env(qs_env, atomic_kind_set=atomic_kind_set, qs_kind_set=qs_kind_set)
991
992 n_kind = SIZE(qs_kind_set)
993 n_atom = bs_env%n_atom
994
995 CALL get_atomic_kind_set(atomic_kind_set, kind_of=kind_of)
996
997 DO ikind = 1, n_kind
998 CALL get_qs_kind(qs_kind_set(ikind), basis_set=basis, basis_type="RI_AUX")
999 cpassert(ASSOCIATED(basis))
1000 END DO
1001
1002 ALLOCATE (bs_env%i_RI_start_from_atom(n_atom))
1003 ALLOCATE (bs_env%i_RI_end_from_atom(n_atom))
1004 ALLOCATE (bs_env%i_ao_start_from_atom(n_atom))
1005 ALLOCATE (bs_env%i_ao_end_from_atom(n_atom))
1006
1007 n_ri = 0
1008 DO iatom = 1, n_atom
1009 bs_env%i_RI_start_from_atom(iatom) = n_ri + 1
1010 n_ri = n_ri + bs_env%sizes_RI(iatom)
1011 bs_env%i_RI_end_from_atom(iatom) = n_ri
1012 END DO
1013 bs_env%n_RI = n_ri
1014
1015 max_ao_bf_per_atom = 0
1016 n_ao_test = 0
1017 DO iatom = 1, n_atom
1018 bs_env%i_ao_start_from_atom(iatom) = n_ao_test + 1
1019 n_ao_test = n_ao_test + bs_env%sizes_AO(iatom)
1020 bs_env%i_ao_end_from_atom(iatom) = n_ao_test
1021 max_ao_bf_per_atom = max(max_ao_bf_per_atom, bs_env%sizes_AO(iatom))
1022 END DO
1023 cpassert(n_ao_test == bs_env%n_ao)
1024 bs_env%max_AO_bf_per_atom = max_ao_bf_per_atom
1025
1026 u = bs_env%unit_nr
1027 ALLOCATE (bs_env%l_RI(n_ri), source=-1)
1028 IF (bs_env%auto_ri%enabled) THEN
1029 cpassert(bs_env%auto_ri%ready)
1030 ELSE
1031 i_ri = 0
1032 DO iatom = 1, n_atom
1033 ikind = kind_of(iatom)
1034 nset = bs_env%basis_set_RI(ikind)%gto_basis_set%nset
1035 l_max => bs_env%basis_set_RI(ikind)%gto_basis_set%lmax
1036 l_min => bs_env%basis_set_RI(ikind)%gto_basis_set%lmin
1037 nsgf_set => bs_env%basis_set_RI(ikind)%gto_basis_set%nsgf_set
1038 DO iset = 1, nset
1039 cpassert(l_max(iset) == l_min(iset))
1040 bs_env%l_RI(i_ri + 1:i_ri + nsgf_set(iset)) = l_max(iset)
1041 i_ri = i_ri + nsgf_set(iset)
1042 END DO
1043 END DO
1044 cpassert(i_ri == n_ri)
1045 IF (u > 0) THEN
1046 WRITE (u, fmt="(T2,A)") " "
1047 WRITE (u, fmt="(T2,2A,T75,I8)") "Number of auxiliary Gaussian basis functions ", &
1048 "for χ, ε, W", n_ri
1049 END IF
1050 END IF
1051
1052 CALL timestop(handle)
1053
1054 END SUBROUTINE set_ao_ri_basis_function_indices
1055
1056! **************************************************************************************************
1057!> \brief Sets the reciprocal mesh used for χ, ε, and W.
1058!> \param bs_env ...
1059!> \param kpoints_chi_eps_W reciprocal mesh used for χ, ε, and W
1060! **************************************************************************************************
1061 SUBROUTINE setup_kpoints_chi_eps_w(bs_env, kpoints_chi_eps_W)
1062
1063 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
1064 TYPE(kpoint_type), POINTER :: kpoints_chi_eps_w
1065
1066 CHARACTER(LEN=*), PARAMETER :: routinen = 'setup_kpoints_chi_eps_W'
1067
1068 INTEGER :: handle, i_dim, n_dim, nkp, nkp_extra, &
1069 nkp_orig, u
1070 INTEGER, DIMENSION(3) :: nkp_grid, nkp_grid_extra, periodic
1071 REAL(kind=dp) :: exp_s_p, n_dim_inv
1072
1073 CALL timeset(routinen, handle)
1074
1075 ! routine adapted from mp2_integrals.F
1076 NULLIFY (kpoints_chi_eps_w)
1077 CALL kpoint_create(kpoints_chi_eps_w)
1078
1079 kpoints_chi_eps_w%kp_scheme = "GENERAL"
1080
1081 periodic(1:3) = bs_env%periodic(1:3)
1082
1083 cpassert(SIZE(bs_env%nkp_grid_chi_eps_W_input) == 3)
1084
1085 IF (bs_env%nkp_grid_chi_eps_W_input(1) > 0 .AND. &
1086 bs_env%nkp_grid_chi_eps_W_input(2) > 0 .AND. &
1087 bs_env%nkp_grid_chi_eps_W_input(3) > 0) THEN
1088
1089 ! 1. k-point mesh for χ, ε, W from input
1090
1091 DO i_dim = 1, 3
1092 SELECT CASE (periodic(i_dim))
1093 CASE (0)
1094 nkp_grid(i_dim) = 1
1095 nkp_grid_extra(i_dim) = 1
1096 CASE (1)
1097 nkp_grid(i_dim) = bs_env%nkp_grid_chi_eps_W_input(i_dim)
1098 nkp_grid_extra(i_dim) = nkp_grid(i_dim)*2
1099 CASE DEFAULT
1100 cpabort("Error in periodicity.")
1101 END SELECT
1102 END DO
1103
1104 ELSE IF (bs_env%nkp_grid_chi_eps_W_input(1) == -1 .AND. &
1105 bs_env%nkp_grid_chi_eps_W_input(2) == -1 .AND. &
1106 bs_env%nkp_grid_chi_eps_W_input(3) == -1) THEN
1107
1108 ! 2. automatic k-point mesh for χ, ε, W
1109
1110 DO i_dim = 1, 3
1111
1112 cpassert(periodic(i_dim) == 0 .OR. periodic(i_dim) == 1)
1113
1114 SELECT CASE (periodic(i_dim))
1115 CASE (0)
1116 nkp_grid(i_dim) = 1
1117 nkp_grid_extra(i_dim) = 1
1118 CASE (1)
1119 SELECT CASE (bs_env%gw_implementation)
1121 nkp_grid(i_dim) = 4
1122 nkp_grid_extra(i_dim) = 6
1124 nkp_grid(i_dim) = bs_env%kpoints_scf_desymm%nkp_grid(i_dim)
1125 nkp_grid_extra(i_dim) = 1
1127 nkp_grid(i_dim) = bs_env%kpoints_scf_desymm%nkp_grid(i_dim)*4
1128 nkp_grid_extra(i_dim) = bs_env%kpoints_scf_desymm%nkp_grid(i_dim)*8
1129 END SELECT
1130 CASE DEFAULT
1131 cpabort("Error in periodicity.")
1132 END SELECT
1133
1134 END DO
1135
1136 ELSE
1137
1138 cpabort("An error occured when setting up the k-mesh for W.")
1139
1140 END IF
1141
1142 nkp_orig = max(product(nkp_grid)/2, 1)
1143 nkp_extra = product(nkp_grid_extra)/2
1144 IF (bs_env%gw_implementation == ri_rs_small_cell_full_kp) THEN
1145 cpassert(all(nkp_grid == bs_env%kpoints_scf_desymm%nkp_grid))
1146 nkp_orig = product(nkp_grid)
1147 nkp_extra = 0
1148 cpassert(nkp_orig == bs_env%nkp_scf_desymm)
1149 END IF
1150
1151 nkp = nkp_orig + nkp_extra
1152
1153 kpoints_chi_eps_w%nkp_grid(1:3) = nkp_grid(1:3)
1154 kpoints_chi_eps_w%nkp = nkp
1155
1156 bs_env%nkp_grid_chi_eps_W_orig(1:3) = nkp_grid(1:3)
1157 bs_env%nkp_grid_chi_eps_W_extra(1:3) = nkp_grid_extra(1:3)
1158 bs_env%nkp_chi_eps_W_orig = nkp_orig
1159 bs_env%nkp_chi_eps_W_orig_plus_extra = nkp
1160
1161 ALLOCATE (kpoints_chi_eps_w%xkp(3, nkp), kpoints_chi_eps_w%wkp(nkp))
1162 ALLOCATE (bs_env%wkp_no_extra(nkp), bs_env%wkp_s_p(nkp))
1163
1164 IF (bs_env%gw_implementation == ri_rs_small_cell_full_kp) THEN
1165 kpoints_chi_eps_w%xkp = bs_env%kpoints_scf_desymm%xkp(1:3, :)
1166 ELSE
1167 CALL compute_xkp(kpoints_chi_eps_w%xkp, 1, nkp_orig, nkp_grid)
1168 CALL compute_xkp(kpoints_chi_eps_w%xkp, nkp_orig + 1, nkp, nkp_grid_extra)
1169 END IF
1170
1171 n_dim = sum(periodic)
1172 IF (n_dim == 0) THEN
1173 ! molecules
1174 kpoints_chi_eps_w%wkp(1) = 1.0_dp
1175 bs_env%wkp_s_p(1) = 1.0_dp
1176 bs_env%wkp_no_extra(1) = 1.0_dp
1177 ELSE IF (bs_env%gw_implementation == ri_rs_small_cell_full_kp) THEN
1178 kpoints_chi_eps_w%wkp = 1.0_dp/real(nkp, dp)
1179 bs_env%wkp_s_p(:) = kpoints_chi_eps_w%wkp
1180 bs_env%wkp_no_extra(:) = kpoints_chi_eps_w%wkp
1181 ELSE
1182
1183 n_dim_inv = 1.0_dp/real(n_dim, kind=dp)
1184
1185 ! k-point weights are chosen to automatically extrapolate the k-point mesh
1186 CALL compute_wkp(kpoints_chi_eps_w%wkp(1:nkp_orig), nkp_orig, nkp_extra, n_dim_inv)
1187 CALL compute_wkp(kpoints_chi_eps_w%wkp(nkp_orig + 1:nkp), &
1188 nkp_extra, nkp_orig, n_dim_inv)
1189
1190 bs_env%wkp_no_extra(1:nkp_orig) = 0.0_dp
1191 bs_env%wkp_no_extra(nkp_orig + 1:nkp) = 1.0_dp/real(nkp_extra, kind=dp)
1192
1193 IF (n_dim == 3) THEN
1194 ! W_PQ(k) for an s-function P and a p-function Q diverges as 1/k at k=0
1195 ! (instead of 1/k^2 for P and Q both being s-functions).
1196 exp_s_p = 2.0_dp*n_dim_inv
1197 CALL compute_wkp(bs_env%wkp_s_p(1:nkp_orig), nkp_orig, nkp_extra, exp_s_p)
1198 CALL compute_wkp(bs_env%wkp_s_p(nkp_orig + 1:nkp), nkp_extra, nkp_orig, exp_s_p)
1199 ELSE
1200 bs_env%wkp_s_p(1:nkp) = bs_env%wkp_no_extra(1:nkp)
1201 END IF
1202
1203 END IF
1204
1205 IF (bs_env%approx_kp_extrapol) THEN
1206 bs_env%wkp_orig = 1.0_dp/real(nkp_orig, kind=dp)
1207 END IF
1208
1209 ! heuristic parameter: how many k-points for χ, ε, and W are used simultaneously
1210 ! (less simultaneous k-points: less memory, but more computational effort because of
1211 ! recomputation of V(k))
1212 bs_env%nkp_chi_eps_W_batch = 4
1213
1214 bs_env%num_chi_eps_W_batches = (bs_env%nkp_chi_eps_W_orig_plus_extra - 1)/ &
1215 bs_env%nkp_chi_eps_W_batch + 1
1216
1217 u = bs_env%unit_nr
1218
1219 IF (u > 0 .AND. bs_env%do_periodic) THEN
1220 WRITE (u, fmt="(T2,A)") " "
1221 SELECT CASE (bs_env%gw_implementation)
1223 WRITE (u, '(T2,A,T71,3I4)') "K-point mesh for χ, ε, W", nkp_grid
1224 CASE DEFAULT
1225 WRITE (u, fmt="(T2,1A,T71,3I4)") "K-point mesh 1 for χ, ε, W", nkp_grid(1:3)
1226 WRITE (u, fmt="(T2,2A,T71,3I4)") "K-point mesh 2 for χ, ε, W ", &
1227 "(for k-point extrapolation of W)", nkp_grid_extra(1:3)
1228 WRITE (u, fmt="(T2,A,T80,L)") "Approximate the k-point extrapolation", &
1229 bs_env%approx_kp_extrapol
1230 END SELECT
1231 END IF
1232
1233 CALL timestop(handle)
1234
1235 END SUBROUTINE setup_kpoints_chi_eps_w
1236
1237! **************************************************************************************************
1238!> \brief ...
1239!> \param xkp ...
1240!> \param ikp_start ...
1241!> \param ikp_end ...
1242!> \param grid ...
1243! **************************************************************************************************
1244 SUBROUTINE compute_xkp(xkp, ikp_start, ikp_end, grid)
1245
1246 REAL(kind=dp), DIMENSION(:, :), POINTER :: xkp
1247 INTEGER :: ikp_start, ikp_end
1248 INTEGER, DIMENSION(3) :: grid
1249
1250 CHARACTER(LEN=*), PARAMETER :: routinen = 'compute_xkp'
1251
1252 INTEGER :: handle, i, ix, iy, iz
1253
1254 CALL timeset(routinen, handle)
1255
1256 i = ikp_start
1257 DO ix = 1, grid(1)
1258 DO iy = 1, grid(2)
1259 DO iz = 1, grid(3)
1260
1261 IF (i > ikp_end) cycle
1262
1263 xkp(1, i) = real(2*ix - grid(1) - 1, kind=dp)/(2._dp*real(grid(1), kind=dp))
1264 xkp(2, i) = real(2*iy - grid(2) - 1, kind=dp)/(2._dp*real(grid(2), kind=dp))
1265 xkp(3, i) = real(2*iz - grid(3) - 1, kind=dp)/(2._dp*real(grid(3), kind=dp))
1266 i = i + 1
1267
1268 END DO
1269 END DO
1270 END DO
1271
1272 CALL timestop(handle)
1273
1274 END SUBROUTINE compute_xkp
1275
1276! **************************************************************************************************
1277!> \brief ...
1278!> \param wkp ...
1279!> \param nkp_1 ...
1280!> \param nkp_2 ...
1281!> \param exponent ...
1282! **************************************************************************************************
1283 SUBROUTINE compute_wkp(wkp, nkp_1, nkp_2, exponent)
1284 REAL(kind=dp), DIMENSION(:) :: wkp
1285 INTEGER :: nkp_1, nkp_2
1286 REAL(kind=dp) :: exponent
1287
1288 CHARACTER(LEN=*), PARAMETER :: routinen = 'compute_wkp'
1289
1290 INTEGER :: handle
1291 REAL(kind=dp) :: nkp_ratio
1292
1293 CALL timeset(routinen, handle)
1294
1295 nkp_ratio = real(nkp_2, kind=dp)/real(nkp_1, kind=dp)
1296
1297 wkp(:) = 1.0_dp/real(nkp_1, kind=dp)/(1.0_dp - nkp_ratio**exponent)
1298
1299 CALL timestop(handle)
1300
1301 END SUBROUTINE compute_wkp
1302
1303! **************************************************************************************************
1304!> \brief ...
1305!> \param qs_env ...
1306!> \param bs_env ...
1307! **************************************************************************************************
1308 SUBROUTINE setup_cells_3c(qs_env, bs_env)
1309
1310 TYPE(qs_environment_type), POINTER :: qs_env
1311 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
1312
1313 CHARACTER(LEN=*), PARAMETER :: routinen = 'setup_cells_3c'
1314
1315 INTEGER :: atom_i, atom_j, atom_k, block_count, handle, i, i_cell_x, i_cell_x_max, &
1316 i_cell_x_min, i_size, ikind, img, j, j_cell, j_cell_max, j_cell_y, j_cell_y_max, &
1317 j_cell_y_min, j_size, k_cell, k_cell_max, k_cell_z, k_cell_z_max, k_cell_z_min, k_size, &
1318 nimage_pairs_3c, nimages_3c, nimages_3c_max, nkind, u
1319 INTEGER, ALLOCATABLE, DIMENSION(:) :: kind_of, n_other_3c_images_max
1320 INTEGER, ALLOCATABLE, DIMENSION(:, :) :: index_to_cell_3c_max, nblocks_3c_max
1321 INTEGER, DIMENSION(3) :: cell_index, n_max
1322 REAL(kind=dp) :: avail_mem_per_proc_gb, cell_dist, cell_radius_3c, dij, dik, djk, eps, &
1323 exp_min_ao, exp_min_ri, frobenius_norm, mem_3c_gb, mem_occ_per_proc_gb, radius_ao, &
1324 radius_ao_product, radius_ri
1325 REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: exp_ao_kind, exp_ri_kind, &
1326 radius_ao_kind, &
1327 radius_ao_product_kind, radius_ri_kind
1328 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :, :) :: int_3c
1329 REAL(kind=dp), DIMENSION(3) :: rij, rik, rjk, vec_cell_j, vec_cell_k
1330 REAL(kind=dp), DIMENSION(:, :), POINTER :: exp_ao, exp_ri
1331 TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
1332 TYPE(cell_type), POINTER :: cell
1333 TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
1334
1335 CALL timeset(routinen, handle)
1336
1337 CALL get_qs_env(qs_env, nkind=nkind, atomic_kind_set=atomic_kind_set, particle_set=particle_set, cell=cell)
1338
1339 ALLOCATE (exp_ao_kind(nkind), exp_ri_kind(nkind), radius_ao_kind(nkind), &
1340 radius_ao_product_kind(nkind), radius_ri_kind(nkind))
1341
1342 exp_min_ri = 10.0_dp
1343 exp_min_ao = 10.0_dp
1344 exp_ri_kind = 10.0_dp
1345 exp_ao_kind = 10.0_dp
1346
1347 eps = bs_env%eps_filter*bs_env%heuristic_filter_factor
1348
1349 DO ikind = 1, nkind
1350
1351 CALL get_gto_basis_set(bs_env%basis_set_RI(ikind)%gto_basis_set, zet=exp_ri)
1352 CALL get_gto_basis_set(bs_env%basis_set_ao(ikind)%gto_basis_set, zet=exp_ao)
1353
1354 ! we need to remove all exponents lower than a lower bound, e.g. 1E-3, because
1355 ! for contracted basis sets, there might be exponents = 0 in zet
1356 DO i = 1, SIZE(exp_ri, 1)
1357 DO j = 1, SIZE(exp_ri, 2)
1358 IF (exp_ri(i, j) < exp_min_ri .AND. exp_ri(i, j) > 1e-3_dp) exp_min_ri = exp_ri(i, j)
1359 IF (exp_ri(i, j) < exp_ri_kind(ikind) .AND. exp_ri(i, j) > 1e-3_dp) THEN
1360 exp_ri_kind(ikind) = exp_ri(i, j)
1361 END IF
1362 END DO
1363 END DO
1364 DO i = 1, SIZE(exp_ao, 1)
1365 DO j = 1, SIZE(exp_ao, 2)
1366 IF (exp_ao(i, j) < exp_min_ao .AND. exp_ao(i, j) > 1e-3_dp) exp_min_ao = exp_ao(i, j)
1367 IF (exp_ao(i, j) < exp_ao_kind(ikind) .AND. exp_ao(i, j) > 1e-3_dp) THEN
1368 exp_ao_kind(ikind) = exp_ao(i, j)
1369 END IF
1370 END DO
1371 END DO
1372 radius_ao_kind(ikind) = sqrt(-log(eps)/exp_ao_kind(ikind))
1373 radius_ao_product_kind(ikind) = sqrt(-log(eps)/(2.0_dp*exp_ao_kind(ikind)))
1374 radius_ri_kind(ikind) = sqrt(-log(eps)/exp_ri_kind(ikind))
1375 END DO
1376
1377 radius_ao = sqrt(-log(eps)/exp_min_ao)
1378 radius_ao_product = sqrt(-log(eps)/(2.0_dp*exp_min_ao))
1379 radius_ri = sqrt(-log(eps)/exp_min_ri)
1380
1381 CALL get_atomic_kind_set(atomic_kind_set=atomic_kind_set, kind_of=kind_of)
1382
1383 ! For a 3c integral (μR υS | P0) we have that cell R and cell S need to be within radius_3c
1384 cell_radius_3c = radius_ao_product + radius_ri + bs_env%ri_metric%cutoff_radius
1385
1386 n_max(1:3) = bs_env%periodic(1:3)*30
1387
1388 nimages_3c_max = 0
1389
1390 i_cell_x_min = 0
1391 i_cell_x_max = 0
1392 j_cell_y_min = 0
1393 j_cell_y_max = 0
1394 k_cell_z_min = 0
1395 k_cell_z_max = 0
1396
1397 DO i_cell_x = -n_max(1), n_max(1)
1398 DO j_cell_y = -n_max(2), n_max(2)
1399 DO k_cell_z = -n_max(3), n_max(3)
1400
1401 cell_index(1:3) = [i_cell_x, j_cell_y, k_cell_z]
1402
1403 CALL get_cell_dist(cell_index, bs_env%hmat, cell_dist)
1404
1405 IF (cell_dist < cell_radius_3c) THEN
1406 nimages_3c_max = nimages_3c_max + 1
1407 i_cell_x_min = min(i_cell_x_min, i_cell_x)
1408 i_cell_x_max = max(i_cell_x_max, i_cell_x)
1409 j_cell_y_min = min(j_cell_y_min, j_cell_y)
1410 j_cell_y_max = max(j_cell_y_max, j_cell_y)
1411 k_cell_z_min = min(k_cell_z_min, k_cell_z)
1412 k_cell_z_max = max(k_cell_z_max, k_cell_z)
1413 END IF
1414
1415 END DO
1416 END DO
1417 END DO
1418
1419 ! get index_to_cell_3c_max for the maximum possible cell range;
1420 ! compute 3c integrals later in this routine and check really which cell is needed
1421 ALLOCATE (index_to_cell_3c_max(3, nimages_3c_max))
1422
1423 img = 0
1424 DO i_cell_x = -n_max(1), n_max(1)
1425 DO j_cell_y = -n_max(2), n_max(2)
1426 DO k_cell_z = -n_max(3), n_max(3)
1427
1428 cell_index(1:3) = [i_cell_x, j_cell_y, k_cell_z]
1429
1430 CALL get_cell_dist(cell_index, bs_env%hmat, cell_dist)
1431
1432 IF (cell_dist < cell_radius_3c) THEN
1433 img = img + 1
1434 index_to_cell_3c_max(1:3, img) = cell_index(1:3)
1435 END IF
1436
1437 END DO
1438 END DO
1439 END DO
1440
1441 ! get pairs of R and S which have non-zero 3c integral (μR υS | P0)
1442 ALLOCATE (nblocks_3c_max(nimages_3c_max, nimages_3c_max))
1443 nblocks_3c_max(:, :) = 0
1444
1445 block_count = 0
1446 DO j_cell = 1, nimages_3c_max
1447 DO k_cell = 1, nimages_3c_max
1448
1449 DO atom_j = 1, bs_env%n_atom
1450 DO atom_k = 1, bs_env%n_atom
1451 DO atom_i = 1, bs_env%n_atom
1452
1453 block_count = block_count + 1
1454 IF (modulo(block_count, bs_env%para_env%num_pe) /= bs_env%para_env%mepos) cycle
1455
1456 CALL scaled_to_real(vec_cell_j, real(index_to_cell_3c_max(1:3, j_cell), kind=dp), cell)
1457 CALL scaled_to_real(vec_cell_k, real(index_to_cell_3c_max(1:3, k_cell), kind=dp), cell)
1458
1459 rij = pbc(particle_set(atom_j)%r(:), cell) - pbc(particle_set(atom_i)%r(:), cell) + vec_cell_j(:)
1460 rjk = pbc(particle_set(atom_k)%r(:), cell) - pbc(particle_set(atom_j)%r(:), cell) &
1461 + vec_cell_k(:) - vec_cell_j(:)
1462 rik(:) = rij(:) + rjk(:)
1463 dij = norm2(rij)
1464 dik = norm2(rik)
1465 djk = norm2(rjk)
1466 IF (djk > radius_ao_kind(kind_of(atom_j)) + radius_ao_kind(kind_of(atom_k))) cycle
1467 IF (dij > radius_ao_kind(kind_of(atom_j)) + radius_ri_kind(kind_of(atom_i)) &
1468 + bs_env%ri_metric%cutoff_radius) cycle
1469 IF (dik > radius_ri_kind(kind_of(atom_i)) + radius_ao_kind(kind_of(atom_k)) &
1470 + bs_env%ri_metric%cutoff_radius) cycle
1471
1472 IF (bs_env%gw_implementation == ri_rs_small_cell_full_kp) THEN
1473 ! RI-RS forms its integrals later; only geometric cell support is needed here.
1474 nblocks_3c_max(j_cell, k_cell) = nblocks_3c_max(j_cell, k_cell) + 1
1475 cycle
1476 END IF
1477
1478 j_size = bs_env%i_ao_end_from_atom(atom_j) - bs_env%i_ao_start_from_atom(atom_j) + 1
1479 k_size = bs_env%i_ao_end_from_atom(atom_k) - bs_env%i_ao_start_from_atom(atom_k) + 1
1480 i_size = bs_env%i_RI_end_from_atom(atom_i) - bs_env%i_RI_start_from_atom(atom_i) + 1
1481
1482 ALLOCATE (int_3c(j_size, k_size, i_size))
1483
1484 ! compute 3-c int. ( μ(atom j) R , ν (atom k) S | P (atom i) 0 )
1485 ! ("|": truncated Coulomb operator), inside build_3c_integrals: (j k | i)
1486 CALL build_3c_integral_block(int_3c, qs_env, bs_env%ri_metric, &
1487 basis_j=bs_env%basis_set_AO, &
1488 basis_k=bs_env%basis_set_AO, &
1489 basis_i=bs_env%basis_set_RI, &
1490 cell_j=index_to_cell_3c_max(1:3, j_cell), &
1491 cell_k=index_to_cell_3c_max(1:3, k_cell), &
1492 atom_k=atom_k, atom_j=atom_j, atom_i=atom_i)
1493
1494 frobenius_norm = norm2(int_3c)
1495
1496 DEALLOCATE (int_3c)
1497
1498 ! we use a higher threshold here to safe memory when storing the 3c integrals
1499 ! in every tensor group
1500 IF (frobenius_norm > eps) THEN
1501 nblocks_3c_max(j_cell, k_cell) = nblocks_3c_max(j_cell, k_cell) + 1
1502 END IF
1503
1504 END DO
1505 END DO
1506 END DO
1507
1508 END DO
1509 END DO
1510
1511 CALL bs_env%para_env%sum(nblocks_3c_max)
1512
1513 ALLOCATE (n_other_3c_images_max(nimages_3c_max))
1514 n_other_3c_images_max(:) = 0
1515
1516 nimages_3c = 0
1517 nimage_pairs_3c = 0
1518
1519 DO j_cell = 1, nimages_3c_max
1520 DO k_cell = 1, nimages_3c_max
1521 IF (nblocks_3c_max(j_cell, k_cell) > 0) THEN
1522 n_other_3c_images_max(j_cell) = n_other_3c_images_max(j_cell) + 1
1523 nimage_pairs_3c = nimage_pairs_3c + 1
1524 END IF
1525 END DO
1526
1527 IF (n_other_3c_images_max(j_cell) > 0) nimages_3c = nimages_3c + 1
1528
1529 END DO
1530
1531 bs_env%nimages_3c = nimages_3c
1532 ALLOCATE (bs_env%index_to_cell_3c(3, nimages_3c))
1533 ALLOCATE (bs_env%cell_to_index_3c(i_cell_x_min:i_cell_x_max, &
1534 j_cell_y_min:j_cell_y_max, &
1535 k_cell_z_min:k_cell_z_max))
1536 bs_env%cell_to_index_3c(:, :, :) = -1
1537
1538 ALLOCATE (bs_env%nblocks_3c(nimages_3c, nimages_3c))
1539 bs_env%nblocks_3c(nimages_3c, nimages_3c) = 0
1540
1541 j_cell = 0
1542 DO j_cell_max = 1, nimages_3c_max
1543 IF (n_other_3c_images_max(j_cell_max) == 0) cycle
1544 j_cell = j_cell + 1
1545 cell_index(1:3) = index_to_cell_3c_max(1:3, j_cell_max)
1546 bs_env%index_to_cell_3c(1:3, j_cell) = cell_index(1:3)
1547 bs_env%cell_to_index_3c(cell_index(1), cell_index(2), cell_index(3)) = j_cell
1548
1549 k_cell = 0
1550 DO k_cell_max = 1, nimages_3c_max
1551 IF (n_other_3c_images_max(k_cell_max) == 0) cycle
1552 k_cell = k_cell + 1
1553
1554 bs_env%nblocks_3c(j_cell, k_cell) = nblocks_3c_max(j_cell_max, k_cell_max)
1555 END DO
1556
1557 END DO
1558
1559 ! we use: 8*10^-9 GB / double precision number
1560 mem_3c_gb = real(bs_env%n_RI, kind=dp)*real(bs_env%n_ao, kind=dp)**2 &
1561 *real(nimage_pairs_3c, kind=dp)*8e-9_dp
1562
1563 CALL mp_mem_used_per_rank_gb(bs_env%para_env, mem_occ_per_proc_gb)
1564
1565 ! number of processors per group that entirely stores the 3c integrals and does tensor ops
1566 avail_mem_per_proc_gb = bs_env%input_memory_per_proc_GB - mem_occ_per_proc_gb
1567
1568 ! careful: downconvering real to integer, 1.9 -> 1; thus add 1.0 for upconversion, 1.9 -> 2
1569 bs_env%group_size_tensor = max(int(mem_3c_gb/avail_mem_per_proc_gb + 1.0_dp), 1)
1570
1571 u = bs_env%unit_nr
1572
1573 IF (u > 0) THEN
1574 WRITE (u, fmt="(T2,A,F52.1,A)") "Radius of atomic orbitals", radius_ao*angstrom, " Å"
1575 WRITE (u, fmt="(T2,A,F55.1,A)") "Radius of RI functions", radius_ri*angstrom, " Å"
1576 WRITE (u, fmt="(T2,A,I47)") "Number of cells for 3c integrals", nimages_3c
1577 WRITE (u, fmt="(T2,A,I42)") "Number of cell pairs for 3c integrals", nimage_pairs_3c
1578 WRITE (u, '(T2,A)') ''
1579 IF (bs_env%auto_memory_per_proc) THEN
1580 WRITE (u, '(T2,A,F34.1,A)') 'Detected: Available memory per MPI process', &
1581 bs_env%input_memory_per_proc_GB, ' GB'
1582 ELSE
1583 WRITE (u, '(T2,A,F37.1,A)') 'Input: Available memory per MPI process', &
1584 bs_env%input_memory_per_proc_GB, ' GB'
1585 END IF
1586 WRITE (u, '(T2,A,F35.1,A)') 'Used memory per MPI process before GW run', &
1587 mem_occ_per_proc_gb, ' GB'
1588 WRITE (u, '(T2,A,F44.1,A)') 'Memory of three-center integrals', mem_3c_gb, ' GB'
1589 END IF
1590
1591 CALL timestop(handle)
1592
1593 END SUBROUTINE setup_cells_3c
1594
1595! **************************************************************************************************
1596!> \brief ...
1597!> \param cell_index ...
1598!> \param hmat ...
1599!> \param cell_dist ...
1600! **************************************************************************************************
1601 SUBROUTINE get_cell_dist(cell_index, hmat, cell_dist)
1602
1603 INTEGER, DIMENSION(3) :: cell_index
1604 REAL(kind=dp) :: hmat(3, 3), cell_dist
1605
1606 CHARACTER(LEN=*), PARAMETER :: routinen = 'get_cell_dist'
1607
1608 INTEGER :: handle, i_dim
1609 INTEGER, DIMENSION(3) :: cell_index_adj
1610 REAL(kind=dp) :: cell_dist_3(3)
1611
1612 CALL timeset(routinen, handle)
1613
1614 ! the distance of cells needs to be taken to adjacent neighbors, not
1615 ! between the center of the cells. We thus need to rescale the cell index
1616 DO i_dim = 1, 3
1617 IF (cell_index(i_dim) > 0) cell_index_adj(i_dim) = cell_index(i_dim) - 1
1618 IF (cell_index(i_dim) < 0) cell_index_adj(i_dim) = cell_index(i_dim) + 1
1619 IF (cell_index(i_dim) == 0) cell_index_adj(i_dim) = cell_index(i_dim)
1620 END DO
1621
1622 cell_dist_3(1:3) = matmul(hmat, real(cell_index_adj, kind=dp))
1623
1624 cell_dist = norm2(cell_dist_3)
1625
1626 CALL timestop(handle)
1627
1628 END SUBROUTINE get_cell_dist
1629
1630! **************************************************************************************************
1631!> \brief ...
1632!> \param qs_env ...
1633!> \param bs_env ...
1634! **************************************************************************************************
1635 SUBROUTINE set_parallelization_parameters(qs_env, bs_env)
1636 TYPE(qs_environment_type), POINTER :: qs_env
1637 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
1638
1639 CHARACTER(LEN=*), PARAMETER :: routinen = 'set_parallelization_parameters'
1640
1641 INTEGER :: color_sub, dummy_1, dummy_2, handle, &
1642 num_pe, num_t_groups, u
1643 TYPE(mp_para_env_type), POINTER :: para_env
1644
1645 CALL timeset(routinen, handle)
1646
1647 CALL get_qs_env(qs_env, para_env=para_env)
1648
1649 num_pe = para_env%num_pe
1650 ! if not already set, use all processors for the group (for large-cell GW, performance
1651 ! seems to be best for a single group with all MPI processes per group)
1652 IF (bs_env%group_size_tensor < 0 .OR. bs_env%group_size_tensor > num_pe) THEN
1653 bs_env%group_size_tensor = num_pe
1654 END IF
1655
1656 ! group_size_tensor must divide num_pe without rest; otherwise everything will be complicated
1657 IF (modulo(num_pe, bs_env%group_size_tensor) /= 0) THEN
1658 CALL find_good_group_size(num_pe, bs_env%group_size_tensor)
1659 END IF
1660
1661 ! para_env_tensor for tensor subgroups
1662 color_sub = para_env%mepos/bs_env%group_size_tensor
1663 bs_env%tensor_group_color = color_sub
1664
1665 ALLOCATE (bs_env%para_env_tensor)
1666 CALL bs_env%para_env_tensor%from_split(para_env, color_sub)
1667
1668 num_t_groups = para_env%num_pe/bs_env%group_size_tensor
1669 bs_env%num_tensor_groups = num_t_groups
1670
1671 CALL get_i_j_atoms(bs_env%atoms_i, bs_env%atoms_j, bs_env%n_atom_i, bs_env%n_atom_j, &
1672 color_sub, bs_env)
1673
1674 ALLOCATE (bs_env%atoms_i_t_group(2, num_t_groups))
1675 ALLOCATE (bs_env%atoms_j_t_group(2, num_t_groups))
1676 DO color_sub = 0, num_t_groups - 1
1677 CALL get_i_j_atoms(bs_env%atoms_i_t_group(1:2, color_sub + 1), &
1678 bs_env%atoms_j_t_group(1:2, color_sub + 1), &
1679 dummy_1, dummy_2, color_sub, bs_env)
1680 END DO
1681
1682 u = bs_env%unit_nr
1683 IF (u > 0 .AND. .NOT. bs_env%do_gw_ri_rs) THEN
1684 WRITE (u, '(T2,A,I47)') 'Group size for tensor operations', bs_env%group_size_tensor
1685 IF (bs_env%group_size_tensor > 1 .AND. bs_env%n_atom < 5) THEN
1686 WRITE (u, '(T2,A)') 'The requested group size is > 1 which can lead to bad performance.'
1687 WRITE (u, '(T2,A)') 'Using more memory per MPI process might improve performance.'
1688 WRITE (u, '(T2,A)') '(Also increase MEMORY_PER_PROC when using more memory per process.)'
1689 END IF
1690 END IF
1691
1692 CALL timestop(handle)
1693
1694 END SUBROUTINE set_parallelization_parameters
1695
1696! **************************************************************************************************
1697!> \brief ...
1698!> \param num_pe ...
1699!> \param group_size ...
1700! **************************************************************************************************
1701 SUBROUTINE find_good_group_size(num_pe, group_size)
1702
1703 INTEGER :: num_pe, group_size
1704
1705 CHARACTER(LEN=*), PARAMETER :: routinen = 'find_good_group_size'
1706
1707 INTEGER :: group_size_minus, group_size_orig, &
1708 group_size_plus, handle, i_diff
1709
1710 CALL timeset(routinen, handle)
1711
1712 group_size_orig = group_size
1713
1714 DO i_diff = 1, num_pe
1715
1716 group_size_minus = group_size - i_diff
1717
1718 IF (modulo(num_pe, group_size_minus) == 0 .AND. group_size_minus > 0) THEN
1719 group_size = group_size_minus
1720 EXIT
1721 END IF
1722
1723 group_size_plus = group_size + i_diff
1724
1725 IF (modulo(num_pe, group_size_plus) == 0 .AND. group_size_plus <= num_pe) THEN
1726 group_size = group_size_plus
1727 EXIT
1728 END IF
1729
1730 END DO
1731
1732 IF (group_size_orig == group_size) cpabort("Group size error")
1733
1734 CALL timestop(handle)
1735
1736 END SUBROUTINE find_good_group_size
1737
1738! **************************************************************************************************
1739!> \brief ...
1740!> \param atoms_i ...
1741!> \param atoms_j ...
1742!> \param n_atom_i ...
1743!> \param n_atom_j ...
1744!> \param color_sub ...
1745!> \param bs_env ...
1746! **************************************************************************************************
1747 SUBROUTINE get_i_j_atoms(atoms_i, atoms_j, n_atom_i, n_atom_j, color_sub, bs_env)
1748
1749 INTEGER, DIMENSION(2) :: atoms_i, atoms_j
1750 INTEGER :: n_atom_i, n_atom_j, color_sub
1751 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
1752
1753 CHARACTER(LEN=*), PARAMETER :: routinen = 'get_i_j_atoms'
1754
1755 INTEGER :: handle, i_atoms_per_group, i_group, &
1756 ipcol, ipcol_loop, iprow, iprow_loop, &
1757 j_atoms_per_group, npcol, nprow
1758
1759 CALL timeset(routinen, handle)
1760
1761 ! create a square mesh of tensor groups for iatom and jatom; code from blacs_env_create
1762 CALL square_mesh(nprow, npcol, bs_env%num_tensor_groups)
1763
1764 i_group = 0
1765 DO ipcol_loop = 0, npcol - 1
1766 DO iprow_loop = 0, nprow - 1
1767 IF (i_group == color_sub) THEN
1768 iprow = iprow_loop
1769 ipcol = ipcol_loop
1770 END IF
1771 i_group = i_group + 1
1772 END DO
1773 END DO
1774
1775 IF (modulo(bs_env%n_atom, nprow) == 0) THEN
1776 i_atoms_per_group = bs_env%n_atom/nprow
1777 ELSE
1778 i_atoms_per_group = bs_env%n_atom/nprow + 1
1779 END IF
1780
1781 IF (modulo(bs_env%n_atom, npcol) == 0) THEN
1782 j_atoms_per_group = bs_env%n_atom/npcol
1783 ELSE
1784 j_atoms_per_group = bs_env%n_atom/npcol + 1
1785 END IF
1786
1787 atoms_i(1) = iprow*i_atoms_per_group + 1
1788 atoms_i(2) = min((iprow + 1)*i_atoms_per_group, bs_env%n_atom)
1789 n_atom_i = atoms_i(2) - atoms_i(1) + 1
1790
1791 atoms_j(1) = ipcol*j_atoms_per_group + 1
1792 atoms_j(2) = min((ipcol + 1)*j_atoms_per_group, bs_env%n_atom)
1793 n_atom_j = atoms_j(2) - atoms_j(1) + 1
1794
1795 CALL timestop(handle)
1796
1797 END SUBROUTINE get_i_j_atoms
1798
1799! **************************************************************************************************
1800!> \brief ...
1801!> \param nprow ...
1802!> \param npcol ...
1803!> \param nproc ...
1804! **************************************************************************************************
1805 SUBROUTINE square_mesh(nprow, npcol, nproc)
1806 INTEGER :: nprow, npcol, nproc
1807
1808 CHARACTER(LEN=*), PARAMETER :: routinen = 'square_mesh'
1809
1810 INTEGER :: gcd_max, handle, ipe, jpe
1811
1812 CALL timeset(routinen, handle)
1813
1814 gcd_max = -1
1815 DO ipe = 1, ceiling(sqrt(real(nproc, dp)))
1816 jpe = nproc/ipe
1817 IF (ipe*jpe /= nproc) cycle
1818 IF (gcd(ipe, jpe) >= gcd_max) THEN
1819 nprow = ipe
1820 npcol = jpe
1821 gcd_max = gcd(ipe, jpe)
1822 END IF
1823 END DO
1824
1825 CALL timestop(handle)
1826
1827 END SUBROUTINE square_mesh
1828
1829! **************************************************************************************************
1830!> \brief ...
1831!> \param qs_env ...
1832!> \param bs_env ...
1833! **************************************************************************************************
1834 SUBROUTINE allocate_matrices(qs_env, bs_env)
1835 TYPE(qs_environment_type), POINTER :: qs_env
1836 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
1837
1838 CHARACTER(LEN=*), PARAMETER :: routinen = 'allocate_matrices'
1839
1840 INTEGER :: handle, i_t
1841 TYPE(cp_blacs_env_type), POINTER :: blacs_env, blacs_env_tensor
1842 TYPE(cp_fm_struct_type), POINTER :: fm_struct, fm_struct_ri_global
1843 TYPE(mp_para_env_type), POINTER :: para_env
1844
1845 CALL timeset(routinen, handle)
1846
1847 CALL get_qs_env(qs_env, para_env=para_env, blacs_env=blacs_env)
1848
1849 fm_struct => bs_env%fm_ks_Gamma(1)%matrix_struct
1850
1851 CALL cp_fm_create(bs_env%fm_Gocc, fm_struct)
1852 CALL cp_fm_create(bs_env%fm_Gvir, fm_struct)
1853
1854 NULLIFY (fm_struct_ri_global)
1855 CALL cp_fm_struct_create(fm_struct_ri_global, context=blacs_env, nrow_global=bs_env%n_RI, &
1856 ncol_global=bs_env%n_RI, para_env=para_env)
1857 CALL cp_fm_create(bs_env%fm_RI_RI, fm_struct_ri_global)
1858 CALL cp_fm_create(bs_env%fm_chi_Gamma_freq, fm_struct_ri_global)
1859 CALL cp_fm_create(bs_env%fm_W_MIC_freq, fm_struct_ri_global)
1860 IF (bs_env%approx_kp_extrapol) THEN
1861 CALL cp_fm_create(bs_env%fm_W_MIC_freq_1_extra, fm_struct_ri_global)
1862 CALL cp_fm_create(bs_env%fm_W_MIC_freq_1_no_extra, fm_struct_ri_global)
1863 CALL cp_fm_set_all(bs_env%fm_W_MIC_freq_1_extra, 0.0_dp)
1864 CALL cp_fm_set_all(bs_env%fm_W_MIC_freq_1_no_extra, 0.0_dp)
1865 END IF
1866 CALL cp_fm_struct_release(fm_struct_ri_global)
1867
1868 IF (.NOT. bs_env%do_gw_ri_rs) THEN
1869 ! create blacs_env for subgroups of tensor operations
1870 NULLIFY (blacs_env_tensor)
1871 CALL cp_blacs_env_create(blacs_env=blacs_env_tensor, para_env=bs_env%para_env_tensor)
1872
1873 ! allocate dbcsr matrices in the tensor subgroup; actually, one only needs a small
1874 ! subset of blocks in the tensor subgroup, however, all atomic blocks are allocated.
1875 ! One might think of creating a dbcsr matrix with only the blocks that are needed
1876 ! in the tensor subgroup
1877 CALL create_mat_munu(bs_env%mat_ao_ao_tensor, qs_env, bs_env%eps_atom_grid_2d_mat, &
1878 blacs_env_tensor, do_ri_aux_basis=.false.)
1879
1880 CALL create_mat_munu(bs_env%mat_RI_RI_tensor, qs_env, bs_env%eps_atom_grid_2d_mat, &
1881 blacs_env_tensor, do_ri_aux_basis=.true.)
1882
1883 CALL cp_blacs_env_release(blacs_env_tensor)
1884 END IF
1885
1886 CALL create_mat_munu(bs_env%mat_RI_RI, qs_env, bs_env%eps_atom_grid_2d_mat, &
1887 blacs_env, do_ri_aux_basis=.true., &
1888 custom_row_blk_sizes=bs_env%sizes_RI)
1889
1890 NULLIFY (bs_env%mat_chi_Gamma_tau)
1891 CALL dbcsr_allocate_matrix_set(bs_env%mat_chi_Gamma_tau, bs_env%num_time_freq_points)
1892
1893 DO i_t = 1, bs_env%num_time_freq_points
1894 ALLOCATE (bs_env%mat_chi_Gamma_tau(i_t)%matrix)
1895 CALL dbcsr_create(bs_env%mat_chi_Gamma_tau(i_t)%matrix, template=bs_env%mat_RI_RI%matrix)
1896 END DO
1897
1898 CALL timestop(handle)
1899
1900 END SUBROUTINE allocate_matrices
1901
1902! **************************************************************************************************
1903!> \brief Computes and stores the inverse RI metric at the Γ point.
1904!> \param qs_env ...
1905!> \param bs_env ...
1906! **************************************************************************************************
1907 SUBROUTINE compute_minv_gamma(qs_env, bs_env)
1908 TYPE(qs_environment_type), POINTER :: qs_env
1909 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
1910
1911 CHARACTER(LEN=*), PARAMETER :: routinen = 'compute_Minv_Gamma'
1912
1913 INTEGER :: handle
1914 REAL(kind=dp) :: eigenvalue_threshold, &
1915 metric_regularization
1916 TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:, :) :: fm_m
1917
1918 CALL timeset(routinen, handle)
1919
1920 CALL cp_fm_create(bs_env%fm_Minv_Gamma, bs_env%fm_RI_RI%matrix_struct)
1921 IF (bs_env%auto_ri%enabled) THEN
1922 cpassert(bs_env%auto_ri%ready)
1923 CALL cp_fm_to_fm(bs_env%auto_ri%M_pq_inv, bs_env%fm_Minv_Gamma)
1924 ELSE
1925 metric_regularization = 0.0_dp
1926 eigenvalue_threshold = 0.0_dp
1927 IF (bs_env%do_gw_ri_rs) THEN
1928 metric_regularization = bs_env%regularization_RI
1929 eigenvalue_threshold = bs_env%eps_eigval_mat_RI
1930 END IF
1931 CALL ri_2c_integral_mat(qs_env, fm_m, bs_env%fm_RI_RI%matrix_struct, bs_env%n_RI, &
1932 bs_env%ri_metric, &
1933 regularization_ri=metric_regularization)
1934 CALL fm_invert(fm_m(1, 1), eigenvalue_threshold, bs_env%unit_nr)
1935 CALL cp_fm_to_fm(fm_m(1, 1), bs_env%fm_Minv_Gamma)
1936 CALL cp_fm_release(fm_m)
1937 END IF
1938
1939 CALL timestop(handle)
1940
1941 END SUBROUTINE compute_minv_gamma
1942
1943! **************************************************************************************************
1944!> \brief ...
1945!> \param qs_env ...
1946!> \param bs_env ...
1947! **************************************************************************************************
1948 SUBROUTINE compute_v_xc(qs_env, bs_env)
1949 TYPE(qs_environment_type), POINTER :: qs_env
1950 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
1951
1952 CHARACTER(LEN=*), PARAMETER :: routinen = 'compute_V_xc'
1953
1954 INTEGER :: handle, img, ispin, myfun, nimages
1955 LOGICAL :: hf_present
1956 REAL(kind=dp) :: energy_ex, energy_exc, energy_total, &
1957 myfraction
1958 TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: mat_ks_without_v_xc
1959 TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_ks_kp
1960 TYPE(dft_control_type), POINTER :: dft_control
1961 TYPE(qs_energy_type), POINTER :: energy
1962 TYPE(section_vals_type), POINTER :: hf_section, input, xc_section
1963
1964 CALL timeset(routinen, handle)
1965
1966 CALL get_qs_env(qs_env, input=input, energy=energy, dft_control=dft_control)
1967
1968 ! previously, dft_control%nimages set to # neighbor cells, revert for Γ-only KS matrix
1969 nimages = dft_control%nimages
1970 dft_control%nimages = bs_env%nimages_scf
1971
1972 ! we need to reset XC functional, therefore, get XC input
1973 xc_section => section_vals_get_subs_vals(input, "DFT%XC")
1974 CALL section_vals_val_get(xc_section, "XC_FUNCTIONAL%_SECTION_PARAMETERS_", i_val=myfun)
1975 CALL section_vals_val_set(xc_section, "XC_FUNCTIONAL%_SECTION_PARAMETERS_", i_val=xc_none)
1976 hf_section => section_vals_get_subs_vals(input, "DFT%XC%HF", can_return_null=.true.)
1977 hf_present = .false.
1978 IF (ASSOCIATED(hf_section)) THEN
1979 CALL section_vals_get(hf_section, explicit=hf_present)
1980 END IF
1981 IF (hf_present) THEN
1982 ! Special case for handling hfx
1983 CALL section_vals_val_get(xc_section, "HF%FRACTION", r_val=myfraction)
1984 CALL section_vals_val_set(xc_section, "HF%FRACTION", r_val=0.0_dp)
1985 END IF
1986
1987 ! save the energy before the energy gets updated
1988 energy_total = energy%total
1989 energy_exc = energy%exc
1990 energy_ex = energy%ex
1991
1992 SELECT CASE (bs_env%gw_implementation)
1994
1995 NULLIFY (mat_ks_without_v_xc)
1996 CALL dbcsr_allocate_matrix_set(mat_ks_without_v_xc, bs_env%n_spin)
1997
1998 DO ispin = 1, bs_env%n_spin
1999 ALLOCATE (mat_ks_without_v_xc(ispin)%matrix)
2000 IF (hf_present) THEN
2001 CALL dbcsr_create(mat_ks_without_v_xc(ispin)%matrix, template=bs_env%mat_ao_ao%matrix, &
2002 matrix_type=dbcsr_type_symmetric)
2003 ELSE
2004 CALL dbcsr_create(mat_ks_without_v_xc(ispin)%matrix, template=bs_env%mat_ao_ao%matrix)
2005 END IF
2006 END DO
2007
2008 ! calculate KS-matrix without XC
2009 CALL qs_ks_build_kohn_sham_matrix(qs_env, calculate_forces=.false., just_energy=.false., &
2010 ext_ks_matrix=mat_ks_without_v_xc)
2011
2012 DO ispin = 1, bs_env%n_spin
2013 ! transfer dbcsr matrix to fm
2014 CALL cp_fm_create(bs_env%fm_V_xc_Gamma(ispin), bs_env%fm_s_Gamma%matrix_struct)
2015 CALL copy_dbcsr_to_fm(mat_ks_without_v_xc(ispin)%matrix, bs_env%fm_V_xc_Gamma(ispin))
2016
2017 ! v_xc = h_ks - h_ks(v_xc = 0)
2018 CALL cp_fm_scale_and_add(alpha=-1.0_dp, matrix_a=bs_env%fm_V_xc_Gamma(ispin), &
2019 beta=1.0_dp, matrix_b=bs_env%fm_ks_Gamma(ispin))
2020 END DO
2021
2022 CALL dbcsr_deallocate_matrix_set(mat_ks_without_v_xc)
2023
2025
2026 ! calculate KS-matrix without XC
2027 CALL build_periodic_ks_with_gapw_density(qs_env)
2028 CALL get_qs_env(qs_env=qs_env, matrix_ks_kp=matrix_ks_kp)
2029
2030 ALLOCATE (bs_env%fm_V_xc_R(dft_control%nimages, bs_env%n_spin))
2031 DO ispin = 1, bs_env%n_spin
2032 DO img = 1, dft_control%nimages
2033 ! safe fm_V_xc_R in fm_matrix because saving in dbcsr matrix caused trouble...
2034 CALL copy_dbcsr_to_fm(matrix_ks_kp(ispin, img)%matrix, bs_env%fm_work_mo(1))
2035 CALL cp_fm_create(bs_env%fm_V_xc_R(img, ispin), bs_env%fm_work_mo(1)%matrix_struct, &
2036 set_zero=.true.)
2037 ! store h_ks(v_xc = 0) in fm_V_xc_R
2038 CALL cp_fm_scale_and_add(alpha=1.0_dp, matrix_a=bs_env%fm_V_xc_R(img, ispin), &
2039 beta=1.0_dp, matrix_b=bs_env%fm_work_mo(1))
2040 END DO
2041 END DO
2042
2043 END SELECT
2044
2045 ! set back the energy
2046 energy%total = energy_total
2047 energy%exc = energy_exc
2048 energy%ex = energy_ex
2049
2050 ! set back nimages
2051 dft_control%nimages = nimages
2052
2053 ! set the DFT functional and HF fraction back
2054 CALL section_vals_val_set(xc_section, "XC_FUNCTIONAL%_SECTION_PARAMETERS_", &
2055 i_val=myfun)
2056 IF (hf_present) THEN
2057 CALL section_vals_val_set(xc_section, "HF%FRACTION", &
2058 r_val=myfraction)
2059 END IF
2060
2061 IF (bs_env%gw_implementation == tensor_small_cell_full_kp .OR. &
2062 bs_env%gw_implementation == ri_rs_small_cell_full_kp) THEN
2063 ! calculate KS-matrix again with XC
2064 CALL build_periodic_ks_with_gapw_density(qs_env)
2065 DO ispin = 1, bs_env%n_spin
2066 DO img = 1, dft_control%nimages
2067 ! store h_ks in fm_work_mo
2068 CALL copy_dbcsr_to_fm(matrix_ks_kp(ispin, img)%matrix, bs_env%fm_work_mo(1))
2069 ! v_xc = h_ks - h_ks(v_xc = 0)
2070 CALL cp_fm_scale_and_add(alpha=-1.0_dp, matrix_a=bs_env%fm_V_xc_R(img, ispin), &
2071 beta=1.0_dp, matrix_b=bs_env%fm_work_mo(1))
2072 END DO
2073 END DO
2074 END IF
2075
2076 CALL timestop(handle)
2077
2078 END SUBROUTINE compute_v_xc
2079
2080! **************************************************************************************************
2081!> \brief Rebuild a periodic KS matrix with the GAPW density derived from the converged AO density.
2082!> \param qs_env ...
2083! **************************************************************************************************
2084 SUBROUTINE build_periodic_ks_with_gapw_density(qs_env)
2085 TYPE(qs_environment_type), POINTER :: qs_env
2086
2087 TYPE(dft_control_type), POINTER :: dft_control
2088 TYPE(qs_rho_type), POINTER :: rho
2089
2090 CALL get_qs_env(qs_env, dft_control=dft_control)
2091 IF (dft_control%qs_control%gapw .OR. dft_control%qs_control%gapw_xc) THEN
2092 CALL get_qs_env(qs_env, rho=rho)
2093 CALL qs_rho_update_rho(rho, qs_env)
2094 END IF
2095 CALL qs_ks_build_kohn_sham_matrix(qs_env, calculate_forces=.false., just_energy=.false.)
2096
2097 END SUBROUTINE build_periodic_ks_with_gapw_density
2098
2099! **************************************************************************************************
2100!> \brief Sets the interaction radii used by the GW calculation.
2101!> \param bs_env ...
2102! **************************************************************************************************
2103 SUBROUTINE init_interaction_radii(bs_env)
2104 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
2105
2106 CHARACTER(LEN=*), PARAMETER :: routinen = 'init_interaction_radii'
2107
2108 INTEGER :: handle, ibasis
2109 TYPE(gto_basis_set_type), POINTER :: orb_basis, ri_basis
2110
2111 CALL timeset(routinen, handle)
2112
2113 DO ibasis = 1, SIZE(bs_env%basis_set_AO)
2114
2115 orb_basis => bs_env%basis_set_AO(ibasis)%gto_basis_set
2116 CALL init_interaction_radii_orb_basis(orb_basis, bs_env%eps_filter)
2117
2118 ri_basis => bs_env%basis_set_RI(ibasis)%gto_basis_set
2119 CALL init_interaction_radii_orb_basis(ri_basis, bs_env%eps_filter)
2120
2121 END DO
2122
2123 CALL timestop(handle)
2124
2125 END SUBROUTINE init_interaction_radii
2126
2127! **************************************************************************************************
2128!> \brief ...
2129!> \param qs_env ...
2130!> \param bs_env ...
2131! **************************************************************************************************
2132 SUBROUTINE create_tensors(qs_env, bs_env)
2133 TYPE(qs_environment_type), POINTER :: qs_env
2134 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
2135
2136 CHARACTER(LEN=*), PARAMETER :: routinen = 'create_tensors'
2137
2138 INTEGER :: handle
2139
2140 CALL timeset(routinen, handle)
2141
2142 ! split blocks does not improve load balancing/efficienfy for tensor contraction, so we go
2143 ! with the standard atomic blocks
2144 CALL create_3c_t(bs_env%t_RI_AO__AO, bs_env%para_env_tensor, "(RI AO | AO)", [1, 2], [3], &
2145 bs_env%sizes_RI, bs_env%sizes_AO, &
2146 create_nl_3c=.true., nl_3c=bs_env%nl_3c, qs_env=qs_env)
2147 CALL create_3c_t(bs_env%t_RI__AO_AO, bs_env%para_env_tensor, "(RI | AO AO)", [1], [2, 3], &
2148 bs_env%sizes_RI, bs_env%sizes_AO)
2149
2150 CALL create_2c_t(bs_env)
2151
2152 CALL timestop(handle)
2153
2154 END SUBROUTINE create_tensors
2155
2156! **************************************************************************************************
2157!> \brief ...
2158!> \param tensor ...
2159!> \param para_env ...
2160!> \param tensor_name ...
2161!> \param map1 ...
2162!> \param map2 ...
2163!> \param sizes_RI ...
2164!> \param sizes_AO ...
2165!> \param create_nl_3c ...
2166!> \param nl_3c ...
2167!> \param qs_env ...
2168! **************************************************************************************************
2169 SUBROUTINE create_3c_t(tensor, para_env, tensor_name, map1, map2, sizes_RI, sizes_AO, &
2170 create_nl_3c, nl_3c, qs_env)
2171 TYPE(dbt_type) :: tensor
2172 TYPE(mp_para_env_type), POINTER :: para_env
2173 CHARACTER(LEN=12) :: tensor_name
2174 INTEGER, DIMENSION(:) :: map1, map2
2175 INTEGER, ALLOCATABLE, DIMENSION(:) :: sizes_ri, sizes_ao
2176 LOGICAL, OPTIONAL :: create_nl_3c
2177 TYPE(neighbor_list_3c_type), OPTIONAL :: nl_3c
2178 TYPE(qs_environment_type), OPTIONAL, POINTER :: qs_env
2179
2180 CHARACTER(LEN=*), PARAMETER :: routinen = 'create_3c_t'
2181
2182 INTEGER :: handle, nkind
2183 INTEGER, ALLOCATABLE, DIMENSION(:) :: dist_ao_1, dist_ao_2, dist_ri
2184 INTEGER, DIMENSION(3) :: pcoord, pdims, pdims_3d
2185 LOGICAL :: my_create_nl_3c
2186 TYPE(dbt_pgrid_type) :: pgrid_3d
2187 TYPE(distribution_3d_type) :: dist_3d
2188 TYPE(mp_cart_type) :: mp_comm_t3c_2
2189 TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
2190
2191 CALL timeset(routinen, handle)
2192
2193 pdims_3d = 0
2194 CALL dbt_pgrid_create(para_env, pdims_3d, pgrid_3d)
2195 CALL create_3c_tensor(tensor, dist_ri, dist_ao_1, dist_ao_2, &
2196 pgrid_3d, sizes_ri, sizes_ao, sizes_ao, &
2197 map1=map1, map2=map2, name=tensor_name)
2198
2199 IF (PRESENT(create_nl_3c)) THEN
2200 my_create_nl_3c = create_nl_3c
2201 ELSE
2202 my_create_nl_3c = .false.
2203 END IF
2204
2205 IF (my_create_nl_3c) THEN
2206 CALL get_qs_env(qs_env, nkind=nkind, particle_set=particle_set)
2207 CALL dbt_mp_environ_pgrid(pgrid_3d, pdims, pcoord)
2208 CALL mp_comm_t3c_2%create(pgrid_3d%mp_comm_2d, 3, pdims)
2209 CALL distribution_3d_create(dist_3d, dist_ri, dist_ao_1, dist_ao_2, &
2210 nkind, particle_set, mp_comm_t3c_2, own_comm=.true.)
2211
2212 CALL build_3c_neighbor_lists(nl_3c, &
2213 qs_env%bs_env%basis_set_RI, &
2214 qs_env%bs_env%basis_set_AO, &
2215 qs_env%bs_env%basis_set_AO, &
2216 dist_3d, qs_env%bs_env%ri_metric, &
2217 "GW_3c_nl", qs_env, own_dist=.true.)
2218 END IF
2219
2220 DEALLOCATE (dist_ri, dist_ao_1, dist_ao_2)
2221 CALL dbt_pgrid_destroy(pgrid_3d)
2222
2223 CALL timestop(handle)
2224
2225 END SUBROUTINE create_3c_t
2226
2227! **************************************************************************************************
2228!> \brief Creates the AO-AO and RI-RI two-center tensors.
2229!> \param bs_env ...
2230! **************************************************************************************************
2231 SUBROUTINE create_2c_t(bs_env)
2232 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
2233
2234 CHARACTER(LEN=*), PARAMETER :: routinen = 'create_2c_t'
2235
2236 INTEGER :: handle
2237 INTEGER, ALLOCATABLE, DIMENSION(:) :: dist_1, dist_2
2238 INTEGER, DIMENSION(2) :: pdims_2d
2239 TYPE(dbt_pgrid_type) :: pgrid_2d
2240
2241 CALL timeset(routinen, handle)
2242
2243 ! inspired from rpa_im_time.F / hfx_types.F
2244
2245 pdims_2d = 0
2246 CALL dbt_pgrid_create(bs_env%para_env_tensor, pdims_2d, pgrid_2d)
2247
2248 CALL create_2c_tensor(bs_env%t_G, dist_1, dist_2, pgrid_2d, &
2249 bs_env%sizes_AO, bs_env%sizes_AO, &
2250 name="(AO | AO)")
2251 DEALLOCATE (dist_1, dist_2)
2252 CALL create_2c_tensor(bs_env%t_chi, dist_1, dist_2, pgrid_2d, &
2253 bs_env%sizes_RI, bs_env%sizes_RI, &
2254 name="(RI | RI)")
2255 DEALLOCATE (dist_1, dist_2)
2256 CALL create_2c_tensor(bs_env%t_W, dist_1, dist_2, pgrid_2d, &
2257 bs_env%sizes_RI, bs_env%sizes_RI, &
2258 name="(RI | RI)")
2259 DEALLOCATE (dist_1, dist_2)
2260 CALL dbt_pgrid_destroy(pgrid_2d)
2261
2262 CALL timestop(handle)
2263
2264 END SUBROUTINE create_2c_t
2265
2266! **************************************************************************************************
2267!> \brief Allocate the arrays holding the GW quasiparticle energies
2268!> \param bs_env ...
2269! **************************************************************************************************
2270 SUBROUTINE allocate_gw_eigenvalues(bs_env)
2271 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
2272
2273 CHARACTER(LEN=*), PARAMETER :: routinen = 'allocate_GW_eigenvalues'
2274
2275 INTEGER :: handle, n_ao, n_spin, nkp
2276
2277 CALL timeset(routinen, handle)
2278
2279 n_ao = bs_env%n_ao
2280 nkp = bs_env%nkp_bs_and_DOS
2281 n_spin = bs_env%n_spin
2282
2283 IF (.NOT. ALLOCATED(bs_env%eigenval_GW)) THEN
2284 ALLOCATE (bs_env%eigenval_GW(n_ao, nkp, n_spin))
2285 END IF
2286 IF (.NOT. ALLOCATED(bs_env%eigenval_HF)) THEN
2287 ALLOCATE (bs_env%eigenval_HF(n_ao, nkp, n_spin))
2288 END IF
2289
2290 SELECT CASE (bs_env%gw_flavour)
2291 CASE (g0w0, evgw0)
2292 IF (.NOT. ALLOCATED(bs_env%eigenval_G0W0)) THEN
2293 ALLOCATE (bs_env%eigenval_G0W0(n_ao, nkp, n_spin))
2294 END IF
2295 END SELECT
2296
2297 IF (bs_env%gw_flavour == evgw0 .AND. .NOT. ALLOCATED(bs_env%eigenval_evGW0)) THEN
2298 ALLOCATE (bs_env%eigenval_evGW0(n_ao, nkp, n_spin))
2299 END IF
2300
2301 CALL timestop(handle)
2302
2303 END SUBROUTINE allocate_gw_eigenvalues
2304
2305! **************************************************************************************************
2306!> \brief ...
2307!> \param qs_env ...
2308!> \param bs_env ...
2309! **************************************************************************************************
2310 SUBROUTINE check_sparsity_3c(qs_env, bs_env)
2311 TYPE(qs_environment_type), POINTER :: qs_env
2312 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
2313
2314 CHARACTER(LEN=*), PARAMETER :: routinen = 'check_sparsity_3c'
2315
2316 INTEGER :: handle, n_atom_step, ri_atom
2317 INTEGER(int_8) :: non_zero_elements_sum, nze
2318 REAL(dp) :: max_dist_ao_atoms, occ, occupation_sum
2319 REAL(kind=dp) :: t1, t2
2320 TYPE(dbt_type), ALLOCATABLE, DIMENSION(:, :) :: t_3c_global_array
2321
2322 CALL timeset(routinen, handle)
2323
2324 ! check the sparsity of 3c integral tensor (µν|P); calculate maximum distance between
2325 ! AO atoms µ, ν where at least a single integral (µν|P) is larger than the filter threshold
2326
2327 ALLOCATE (t_3c_global_array(1, 1))
2328 CALL dbt_create(bs_env%t_RI_AO__AO, t_3c_global_array(1, 1))
2329
2330 ! Allocate arrays to store min/max indices for overlap with other AO/RI functions on each atom
2331 ! (Filled during loop via get_i_j_atom_ranges)
2332 ALLOCATE (bs_env%min_RI_idx_from_AO_AO_atom(bs_env%n_atom, bs_env%n_atom))
2333 ALLOCATE (bs_env%max_RI_idx_from_AO_AO_atom(bs_env%n_atom, bs_env%n_atom))
2334 ALLOCATE (bs_env%min_AO_idx_from_RI_AO_atom(bs_env%n_atom, bs_env%n_atom))
2335 ALLOCATE (bs_env%max_AO_idx_from_RI_AO_atom(bs_env%n_atom, bs_env%n_atom))
2336 bs_env%min_RI_idx_from_AO_AO_atom(:, :) = bs_env%n_RI
2337 bs_env%max_RI_idx_from_AO_AO_atom(:, :) = 1
2338 bs_env%min_AO_idx_from_RI_AO_atom(:, :) = bs_env%n_AO
2339 bs_env%max_AO_idx_from_RI_AO_atom(:, :) = 1
2340
2341 CALL bs_env%para_env%sync()
2342 t1 = m_walltime()
2343
2344 occupation_sum = 0.0_dp
2345 non_zero_elements_sum = 0
2346 max_dist_ao_atoms = 0.0_dp
2347 n_atom_step = int(sqrt(real(bs_env%n_atom, kind=dp)))
2348 ! do not compute full 3c integrals at once because it may cause out of memory
2349 DO ri_atom = 1, bs_env%n_atom, n_atom_step
2350
2351 CALL build_3c_integrals(t_3c_global_array, &
2352 bs_env%eps_filter, &
2353 qs_env, &
2354 bs_env%nl_3c, &
2355 int_eps=bs_env%eps_filter, &
2356 basis_i=bs_env%basis_set_RI, &
2357 basis_j=bs_env%basis_set_AO, &
2358 basis_k=bs_env%basis_set_AO, &
2359 bounds_i=[ri_atom, min(ri_atom + n_atom_step - 1, bs_env%n_atom)], &
2360 potential_parameter=bs_env%ri_metric, &
2361 desymmetrize=.false.)
2362
2363 CALL dbt_filter(t_3c_global_array(1, 1), bs_env%eps_filter)
2364
2365 CALL bs_env%para_env%sync()
2366
2367 CALL get_tensor_occupancy(t_3c_global_array(1, 1), nze, occ)
2368 non_zero_elements_sum = non_zero_elements_sum + nze
2369 occupation_sum = occupation_sum + occ
2370
2371 CALL get_max_dist_ao_atoms(t_3c_global_array(1, 1), max_dist_ao_atoms, qs_env)
2372
2373 ! Extract indices per block
2374 CALL get_i_j_atom_ranges(t_3c_global_array(1, 1), bs_env)
2375
2376 CALL dbt_clear(t_3c_global_array(1, 1))
2377
2378 END DO
2379
2380 t2 = m_walltime()
2381
2382 ! Sync/max for max_dist_AO_atoms is done inside each get_max_dist_AO_atoms
2383 bs_env%max_dist_AO_atoms = max_dist_ao_atoms
2384 ! occupation_sum is a global quantity, also needs no sync here
2385 bs_env%occupation_3c_int = occupation_sum
2386
2387 CALL bs_env%para_env%min(bs_env%min_RI_idx_from_AO_AO_atom)
2388 CALL bs_env%para_env%max(bs_env%max_RI_idx_from_AO_AO_atom)
2389 CALL bs_env%para_env%min(bs_env%min_AO_idx_from_RI_AO_atom)
2390 CALL bs_env%para_env%max(bs_env%max_AO_idx_from_RI_AO_atom)
2391
2392 CALL dbt_destroy(t_3c_global_array(1, 1))
2393 DEALLOCATE (t_3c_global_array)
2394
2395 IF (bs_env%unit_nr > 0) THEN
2396 WRITE (bs_env%unit_nr, '(T2,A)') ''
2397 WRITE (bs_env%unit_nr, '(T2,A,F27.1,A)') &
2398 'Computed 3-center integrals (µν|P), Exec. time', t2 - t1, ' s'
2399 WRITE (bs_env%unit_nr, '(T2,A,F48.3,A)') 'Percentage of non-zero (µν|P)', &
2400 bs_env%occupation_3c_int*100, ' %'
2401 WRITE (bs_env%unit_nr, '(T2,A,F33.1,A)') 'Max. distance between µ,ν in non-zero (µν|P)', &
2402 bs_env%max_dist_AO_atoms*angstrom, ' A'
2403 WRITE (bs_env%unit_nr, '(T2,2A,I20,A)') 'Required memory if storing all 3-center ', &
2404 'integrals (µν|P)', int(real(non_zero_elements_sum, kind=dp)*8.0e-9_dp), ' GB'
2405 END IF
2406
2407 CALL timestop(handle)
2408
2409 END SUBROUTINE check_sparsity_3c
2410
2411! **************************************************************************************************
2412!> \brief ...
2413!> \param t_3c_int ...
2414!> \param max_dist_AO_atoms ...
2415!> \param qs_env ...
2416! **************************************************************************************************
2417 SUBROUTINE get_max_dist_ao_atoms(t_3c_int, max_dist_AO_atoms, qs_env)
2418 TYPE(dbt_type) :: t_3c_int
2419 REAL(kind=dp), INTENT(INOUT) :: max_dist_ao_atoms
2420 TYPE(qs_environment_type), POINTER :: qs_env
2421
2422 CHARACTER(LEN=*), PARAMETER :: routinen = 'get_max_dist_AO_atoms'
2423
2424 INTEGER :: atom_1, atom_2, handle, num_cells
2425 INTEGER, DIMENSION(3) :: atom_ind
2426 INTEGER, DIMENSION(:, :), POINTER :: index_to_cell
2427 REAL(kind=dp) :: abs_rab
2428 REAL(kind=dp), DIMENSION(3) :: rab
2429 TYPE(cell_type), POINTER :: cell
2430 TYPE(dbt_iterator_type) :: iter
2431 TYPE(mp_para_env_type), POINTER :: para_env
2432 TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
2433
2434 CALL timeset(routinen, handle)
2435
2436 NULLIFY (cell, particle_set, para_env)
2437 CALL get_qs_env(qs_env, cell=cell, particle_set=particle_set, para_env=para_env)
2438
2439 ! max_dist_AO_atoms is compared to earlier steps in the loop with step n_atom_step
2440 ! do not initialize/overwrite here
2441
2442! IMPORTANT: Use thread-local copy for max_dist_AO_atoms via REDUCTION to avoid race conditions
2443!$OMP PARALLEL DEFAULT(NONE) &
2444!$OMP SHARED(t_3c_int, num_cells, index_to_cell, particle_set, cell) &
2445!$OMP PRIVATE(iter, atom_ind, rab, abs_rab, atom_1, atom_2) &
2446!$OMP REDUCTION(MAX:max_dist_AO_atoms)
2447
2448 CALL dbt_iterator_start(iter, t_3c_int)
2449 DO WHILE (dbt_iterator_blocks_left(iter))
2450 CALL dbt_iterator_next_block(iter, atom_ind)
2451
2452 atom_1 = atom_ind(2)
2453 atom_2 = atom_ind(3)
2454 rab = pbc(particle_set(atom_1)%r(1:3), particle_set(atom_2)%r(1:3), cell)
2455 abs_rab = sqrt(rab(1)**2 + rab(2)**2 + rab(3)**2)
2456
2457 ! Reduction takes care of using a thread-local copy
2458 max_dist_ao_atoms = max(max_dist_ao_atoms, abs_rab)
2459
2460 END DO
2461 CALL dbt_iterator_stop(iter)
2462!$OMP END PARALLEL
2463
2464 CALL para_env%max(max_dist_ao_atoms)
2465
2466 CALL timestop(handle)
2467
2468 END SUBROUTINE get_max_dist_ao_atoms
2469
2470! **************************************************************************************************
2471!> \brief ...
2472!> \param t_3c ...
2473!> \param bs_env ...
2474! **************************************************************************************************
2475 SUBROUTINE get_i_j_atom_ranges(t_3c, bs_env)
2476 TYPE(dbt_type) :: t_3c
2477 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
2478
2479 CHARACTER(LEN=*), PARAMETER :: routinen = 'get_i_j_atom_ranges'
2480
2481 INTEGER :: handle, idx_ao_end, idx_ao_start, &
2482 idx_ri_end, idx_ri_start
2483 INTEGER, DIMENSION(3) :: atom_ind
2484 TYPE(dbt_iterator_type) :: iter
2485
2486 CALL timeset(routinen, handle)
2487
2488 ! Loop over blocks in 3c, for given min_atom: RI_min/max index from min_atom
2489!$OMP PARALLEL DEFAULT(NONE) &
2490!$OMP SHARED(t_3c, bs_env) &
2491!$OMP PRIVATE(iter, atom_ind, &
2492!$OMP idx_RI_start, idx_RI_end, idx_AO_start, idx_AO_end)
2493
2494 CALL dbt_iterator_start(iter, t_3c)
2495 DO WHILE (dbt_iterator_blocks_left(iter))
2496 CALL dbt_iterator_next_block(iter, atom_ind)
2497
2498 ! Pre-fetch indices to avoid referencing 'bs_env' twice inside the ATOMIC blocks
2499 idx_ri_start = bs_env%i_RI_start_from_atom(atom_ind(1))
2500 idx_ri_end = bs_env%i_RI_end_from_atom(atom_ind(1))
2501
2502 idx_ao_start = bs_env%i_ao_start_from_atom(atom_ind(2))
2503 idx_ao_end = bs_env%i_ao_end_from_atom(atom_ind(2))
2504
2505 ! Update values safely inside ATOMIC blocks, otherwise race conditions occur
2506!$OMP ATOMIC UPDATE
2507 bs_env%min_RI_idx_from_AO_AO_atom(atom_ind(2), atom_ind(3)) = &
2508 min(bs_env%min_RI_idx_from_AO_AO_atom(atom_ind(2), atom_ind(3)), idx_ri_start)
2509!$OMP ATOMIC UPDATE
2510 bs_env%max_RI_idx_from_AO_AO_atom(atom_ind(2), atom_ind(3)) = &
2511 max(bs_env%max_RI_idx_from_AO_AO_atom(atom_ind(2), atom_ind(3)), idx_ri_end)
2512
2513!$OMP ATOMIC UPDATE
2514 bs_env%min_AO_idx_from_RI_AO_atom(atom_ind(1), atom_ind(3)) = &
2515 min(bs_env%min_AO_idx_from_RI_AO_atom(atom_ind(1), atom_ind(3)), idx_ao_start)
2516!$OMP ATOMIC UPDATE
2517 bs_env%max_AO_idx_from_RI_AO_atom(atom_ind(1), atom_ind(3)) = &
2518 max(bs_env%max_AO_idx_from_RI_AO_atom(atom_ind(1), atom_ind(3)), idx_ao_end)
2519
2520 END DO
2521 CALL dbt_iterator_stop(iter)
2522!$OMP END PARALLEL
2523
2524 CALL timestop(handle)
2525
2526 END SUBROUTINE get_i_j_atom_ranges
2527
2528! **************************************************************************************************
2529!> \brief ...
2530!> \param bs_env ...
2531! **************************************************************************************************
2532 SUBROUTINE set_sparsity_parallelization_parameters(bs_env)
2533 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
2534
2535 CHARACTER(LEN=*), PARAMETER :: routinen = 'set_sparsity_parallelization_parameters'
2536
2537 INTEGER :: handle, i_ivl, il_ivl, j_ivl, n_atom_per_il_ivl, n_atom_per_ivl, n_intervals_i, &
2538 n_intervals_inner_loop_atoms, n_intervals_j, u
2539 INTEGER(KIND=int_8) :: input_memory_per_proc
2540
2541 CALL timeset(routinen, handle)
2542
2543 ! heuristic parameter to prevent out of memory
2544 bs_env%safety_factor_memory = 0.10_dp
2545
2546 input_memory_per_proc = int(bs_env%input_memory_per_proc_GB*1.0e9_dp, kind=int_8)
2547
2548 ! choose atomic range for λ ("i_atom"), ν ("j_atom") in
2549 ! M_λνP(iτ) = sum_µ (µν|P) G^occ_µλ(i|τ|,k=0)
2550 ! N_νλQ(iτ) = sum_σ (σλ|Q) G^vir_σν(i|τ|,k=0)
2551 ! such that M and N fit into the memory
2552 n_atom_per_ivl = int(sqrt(bs_env%safety_factor_memory*input_memory_per_proc &
2553 *bs_env%group_size_tensor/24/bs_env%n_RI &
2554 /sqrt(bs_env%occupation_3c_int)))/bs_env%max_AO_bf_per_atom
2555
2556 n_intervals_i = (bs_env%n_atom_i - 1)/n_atom_per_ivl + 1
2557 n_intervals_j = (bs_env%n_atom_j - 1)/n_atom_per_ivl + 1
2558
2559 bs_env%n_intervals_i = n_intervals_i
2560 bs_env%n_intervals_j = n_intervals_j
2561
2562 ALLOCATE (bs_env%i_atom_intervals(2, n_intervals_i))
2563 ALLOCATE (bs_env%j_atom_intervals(2, n_intervals_j))
2564
2565 DO i_ivl = 1, n_intervals_i
2566 bs_env%i_atom_intervals(1, i_ivl) = (i_ivl - 1)*n_atom_per_ivl + bs_env%atoms_i(1)
2567 bs_env%i_atom_intervals(2, i_ivl) = min(i_ivl*n_atom_per_ivl + bs_env%atoms_i(1) - 1, &
2568 bs_env%atoms_i(2))
2569 END DO
2570
2571 DO j_ivl = 1, n_intervals_j
2572 bs_env%j_atom_intervals(1, j_ivl) = (j_ivl - 1)*n_atom_per_ivl + bs_env%atoms_j(1)
2573 bs_env%j_atom_intervals(2, j_ivl) = min(j_ivl*n_atom_per_ivl + bs_env%atoms_j(1) - 1, &
2574 bs_env%atoms_j(2))
2575 END DO
2576
2577 ALLOCATE (bs_env%skip_Sigma_occ(n_intervals_i, n_intervals_j))
2578 ALLOCATE (bs_env%skip_Sigma_vir(n_intervals_i, n_intervals_j))
2579 bs_env%skip_Sigma_occ(:, :) = .false.
2580 bs_env%skip_Sigma_vir(:, :) = .false.
2581 bs_env%n_skip_chi = 0
2582
2583 ALLOCATE (bs_env%skip_chi(n_intervals_i, n_intervals_j))
2584 bs_env%skip_chi(:, :) = .false.
2585 bs_env%n_skip_sigma = 0
2586
2587 ! choose atomic range for µ and σ ("inner loop (IL) atom") in
2588 ! M_λνP(iτ) = sum_µ (µν|P) G^occ_µλ(i|τ|,k=0)
2589 ! N_νλQ(iτ) = sum_σ (σλ|Q) G^vir_σν(i|τ|,k=0)
2590 n_atom_per_il_ivl = min(int(bs_env%safety_factor_memory*input_memory_per_proc &
2591 *bs_env%group_size_tensor/n_atom_per_ivl &
2592 /bs_env%max_AO_bf_per_atom &
2593 /bs_env%n_RI/8/sqrt(bs_env%occupation_3c_int) &
2594 /bs_env%max_AO_bf_per_atom), bs_env%n_atom)
2595
2596 n_intervals_inner_loop_atoms = (bs_env%n_atom - 1)/n_atom_per_il_ivl + 1
2597
2598 bs_env%n_intervals_inner_loop_atoms = n_intervals_inner_loop_atoms
2599
2600 ALLOCATE (bs_env%inner_loop_atom_intervals(2, n_intervals_inner_loop_atoms))
2601 DO il_ivl = 1, n_intervals_inner_loop_atoms
2602 bs_env%inner_loop_atom_intervals(1, il_ivl) = (il_ivl - 1)*n_atom_per_il_ivl + 1
2603 bs_env%inner_loop_atom_intervals(2, il_ivl) = min(il_ivl*n_atom_per_il_ivl, bs_env%n_atom)
2604 END DO
2605
2606 u = bs_env%unit_nr
2607 IF (u > 0) THEN
2608 WRITE (u, '(T2,A)') ''
2609 WRITE (u, '(T2,A,I33)') 'Number of i and j atoms in M_λνP(τ), N_νλQ(τ):', n_atom_per_ivl
2610 WRITE (u, '(T2,A,I18)') 'Number of inner loop atoms for µ in M_λνP = sum_µ (µν|P) G_µλ', &
2611 n_atom_per_il_ivl
2612 END IF
2613
2614 CALL timestop(handle)
2615
2616 END SUBROUTINE set_sparsity_parallelization_parameters
2617
2618! **************************************************************************************************
2619!> \brief ...
2620!> \param qs_env ...
2621!> \param bs_env ...
2622! **************************************************************************************************
2623 SUBROUTINE check_for_restart_files(qs_env, bs_env)
2624 TYPE(qs_environment_type), POINTER :: qs_env
2625 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
2626
2627 CHARACTER(LEN=*), PARAMETER :: routinen = 'check_for_restart_files'
2628
2629 CHARACTER(LEN=9) :: frmt
2630 CHARACTER(len=default_path_length) :: f_chi, f_s_n, f_s_p, f_s_x, f_w_t, &
2631 prefix, project_name, z_lp_name
2632 INTEGER :: handle, i_spin, i_t_or_w, ind, n_spin, &
2633 num_time_freq_points
2634 LOGICAL :: chi_exists, sigma_neg_time_exists, &
2635 sigma_pos_time_exists, &
2636 sigma_x_spin_exists, w_time_exists, &
2637 z_lp_exists
2638 TYPE(cp_logger_type), POINTER :: logger
2639 TYPE(section_vals_type), POINTER :: input, print_key
2640
2641 CALL timeset(routinen, handle)
2642
2643 num_time_freq_points = bs_env%num_time_freq_points
2644 n_spin = bs_env%n_spin
2645
2646 ALLOCATE (bs_env%read_chi(num_time_freq_points))
2647 ALLOCATE (bs_env%calc_chi(num_time_freq_points))
2648 ALLOCATE (bs_env%Sigma_c_exists(num_time_freq_points, n_spin))
2649
2650 CALL get_qs_env(qs_env, input=input)
2651
2652 logger => cp_get_default_logger()
2653 print_key => section_vals_get_subs_vals(input, 'PROPERTIES%BANDSTRUCTURE%GW%PRINT%RESTART')
2654 project_name = cp_print_key_generate_filename(logger, print_key, extension="", &
2655 my_local=.false.)
2656 WRITE (prefix, '(2A)') trim(project_name), "-RESTART_"
2657 bs_env%prefix = prefix
2658
2659 bs_env%all_W_exist = .true.
2660
2661 DO i_t_or_w = 1, num_time_freq_points
2662
2663 IF (i_t_or_w < 10) THEN
2664 WRITE (frmt, '(A)') '(3A,I1,A)'
2665 WRITE (f_chi, frmt) trim(prefix), bs_env%chi_name, "_0", i_t_or_w, ".matrix"
2666 WRITE (f_w_t, frmt) trim(prefix), bs_env%W_time_name, "_0", i_t_or_w, ".matrix"
2667 ELSE IF (i_t_or_w < 100) THEN
2668 WRITE (frmt, '(A)') '(3A,I2,A)'
2669 WRITE (f_chi, frmt) trim(prefix), bs_env%chi_name, "_", i_t_or_w, ".matrix"
2670 WRITE (f_w_t, frmt) trim(prefix), bs_env%W_time_name, "_", i_t_or_w, ".matrix"
2671 ELSE
2672 cpabort('Please implement more than 99 time/frequency points.')
2673 END IF
2674
2675 INQUIRE (file=trim(f_chi), exist=chi_exists)
2676 INQUIRE (file=trim(f_w_t), exist=w_time_exists)
2677
2678 bs_env%read_chi(i_t_or_w) = chi_exists
2679 bs_env%calc_chi(i_t_or_w) = .NOT. chi_exists
2680
2681 bs_env%all_W_exist = bs_env%all_W_exist .AND. w_time_exists
2682
2683 ! the self-energy is spin-dependent
2684 DO i_spin = 1, n_spin
2685
2686 ind = i_t_or_w + (i_spin - 1)*num_time_freq_points
2687
2688 IF (ind < 10) THEN
2689 WRITE (frmt, '(A)') '(3A,I1,A)'
2690 WRITE (f_s_p, frmt) trim(prefix), bs_env%Sigma_p_name, "_0", ind, ".matrix"
2691 WRITE (f_s_n, frmt) trim(prefix), bs_env%Sigma_n_name, "_0", ind, ".matrix"
2692 ELSE IF (ind < 100) THEN
2693 WRITE (frmt, '(A)') '(3A,I2,A)'
2694 WRITE (f_s_p, frmt) trim(prefix), bs_env%Sigma_p_name, "_", ind, ".matrix"
2695 WRITE (f_s_n, frmt) trim(prefix), bs_env%Sigma_n_name, "_", ind, ".matrix"
2696 ELSE
2697 cpabort('Please implement more than 99 combined spin+freq indices.')
2698 END IF
2699
2700 INQUIRE (file=trim(f_s_p), exist=sigma_pos_time_exists)
2701 INQUIRE (file=trim(f_s_n), exist=sigma_neg_time_exists)
2702
2703 bs_env%Sigma_c_exists(i_t_or_w, i_spin) = sigma_pos_time_exists .AND. &
2704 sigma_neg_time_exists
2705
2706 END DO
2707
2708 END DO
2709
2710 ! Marek : In the RTBSE run, check also for zero frequency W
2711 IF (bs_env%rtp_method == rtp_method_bse .OR. &
2712 bs_env%rtp_method == rtp_method_bse_linearized) THEN
2713 WRITE (f_w_t, '(3A,I1,A)') trim(prefix), "W_freq_rtp", "_0", 0, ".matrix"
2714 INQUIRE (file=trim(f_w_t), exist=w_time_exists)
2715 bs_env%all_W_exist = bs_env%all_W_exist .AND. w_time_exists
2716 END IF
2717
2718 ! Check for Restart Z_lP file
2719 IF (bs_env%do_gw_ri_rs) THEN
2720 WRITE (z_lp_name, '(3A)') trim(prefix), "Z_lP", ".matrix"
2721 INQUIRE (file=trim(z_lp_name), exist=z_lp_exists)
2722 bs_env%ri_rs%Z_lP_exists = z_lp_exists
2723 END IF
2724
2725 IF (bs_env%all_W_exist) THEN
2726 bs_env%read_chi(:) = .false.
2727 bs_env%calc_chi(:) = .false.
2728 END IF
2729
2730 bs_env%Sigma_x_exists = .true.
2731 DO i_spin = 1, n_spin
2732 WRITE (f_s_x, '(3A,I1,A)') trim(prefix), bs_env%Sigma_x_name, "_0", i_spin, ".matrix"
2733 INQUIRE (file=trim(f_s_x), exist=sigma_x_spin_exists)
2734 bs_env%Sigma_x_exists = bs_env%Sigma_x_exists .AND. sigma_x_spin_exists
2735 END DO
2736
2737 ! static COHSEX: Σ^c(iω=0,k=0) restart, one file per spin; if present, χ and W are not needed
2738 bs_env%Sigma_c_freq_zero_exists = bs_env%do_static_cohsex
2739 DO i_spin = 1, n_spin
2740 WRITE (f_s_x, '(3A,I1,A)') trim(prefix), bs_env%Sigma_c_freq_zero_name, "_0", i_spin, ".matrix"
2741 INQUIRE (file=trim(f_s_x), exist=sigma_x_spin_exists)
2742 bs_env%Sigma_c_freq_zero_exists = bs_env%Sigma_c_freq_zero_exists .AND. sigma_x_spin_exists
2743 END DO
2744
2745 ! If any restart files are read, check if the SCF converged in 1 step.
2746 ! This is important because a re-iterated SCF can lead to spurious GW results
2747 IF (any(bs_env%read_chi(:)) &
2748 .OR. any(bs_env%Sigma_c_exists) &
2749 .OR. bs_env%all_W_exist &
2750 .OR. bs_env%Sigma_x_exists &
2751 .OR. bs_env%Sigma_c_freq_zero_exists &
2752 ) THEN
2753
2754 IF (qs_env%scf_env%iter_count /= 1) THEN
2755 CALL cp_warn(__location__, "SCF needed more than 1 step, "// &
2756 "which might lead to spurious GW results when using GW restart files. ")
2757 END IF
2758 END IF
2759
2760 CALL timestop(handle)
2761
2762 END SUBROUTINE check_for_restart_files
2763
2764! **************************************************************************************************
2765!> \brief ...
2766!> \param qs_env ...
2767!> \param bs_env ...
2768! **************************************************************************************************
2769 SUBROUTINE compute_3c_integrals(qs_env, bs_env)
2770
2771 TYPE(qs_environment_type), POINTER :: qs_env
2772 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
2773
2774 CHARACTER(LEN=*), PARAMETER :: routinen = 'compute_3c_integrals'
2775
2776 INTEGER :: handle, j_cell, k_cell, nimages_3c
2777
2778 CALL timeset(routinen, handle)
2779
2780 nimages_3c = bs_env%nimages_3c
2781 ALLOCATE (bs_env%t_3c_int(nimages_3c, nimages_3c))
2782 DO j_cell = 1, nimages_3c
2783 DO k_cell = 1, nimages_3c
2784 CALL dbt_create(bs_env%t_RI_AO__AO, bs_env%t_3c_int(j_cell, k_cell))
2785 END DO
2786 END DO
2787
2788 CALL build_3c_integrals(bs_env%t_3c_int, &
2789 bs_env%eps_filter, &
2790 qs_env, &
2791 bs_env%nl_3c, &
2792 int_eps=bs_env%eps_filter*0.05_dp, &
2793 basis_i=bs_env%basis_set_RI, &
2794 basis_j=bs_env%basis_set_AO, &
2795 basis_k=bs_env%basis_set_AO, &
2796 potential_parameter=bs_env%ri_metric, &
2797 desymmetrize=.false., do_kpoints=.true., cell_sym=.true., &
2798 cell_to_index_ext=bs_env%cell_to_index_3c)
2799
2800 CALL bs_env%para_env%sync()
2801
2802 CALL timestop(handle)
2803
2804 END SUBROUTINE compute_3c_integrals
2805
2806! **************************************************************************************************
2807!> \brief ...
2808!> \param bs_env ...
2809! **************************************************************************************************
2810 SUBROUTINE setup_cells_delta_r(bs_env)
2811
2812 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
2813
2814 CHARACTER(LEN=*), PARAMETER :: routinen = 'setup_cells_Delta_R'
2815
2816 INTEGER :: handle
2817
2818 CALL timeset(routinen, handle)
2819
2820 ! cell sums batch wise for fixed ΔR = S_1 - R_1; for example:
2821 ! Σ_λσ^R = sum_PR1νS1 M^G_λ0,νS1,PR1 M^W_σR,νS1,PR1
2822
2823 CALL sum_two_r_grids(bs_env%index_to_cell_3c, &
2824 bs_env%index_to_cell_3c, &
2825 bs_env%nimages_3c, bs_env%nimages_3c, &
2826 bs_env%index_to_cell_Delta_R, &
2827 bs_env%cell_to_index_Delta_R, &
2828 bs_env%nimages_Delta_R)
2829
2830 IF (bs_env%unit_nr > 0) THEN
2831 WRITE (bs_env%unit_nr, fmt="(T2,A,I61)") "Number of cells ΔR", bs_env%nimages_Delta_R
2832 END IF
2833
2834 CALL timestop(handle)
2835
2836 END SUBROUTINE setup_cells_delta_r
2837
2838! **************************************************************************************************
2839!> \brief ...
2840!> \param index_to_cell_1 ...
2841!> \param index_to_cell_2 ...
2842!> \param nimages_1 ...
2843!> \param nimages_2 ...
2844!> \param index_to_cell ...
2845!> \param cell_to_index ...
2846!> \param nimages ...
2847! **************************************************************************************************
2848 SUBROUTINE sum_two_r_grids(index_to_cell_1, index_to_cell_2, nimages_1, nimages_2, &
2849 index_to_cell, cell_to_index, nimages)
2850
2851 INTEGER, DIMENSION(:, :) :: index_to_cell_1, index_to_cell_2
2852 INTEGER :: nimages_1, nimages_2
2853 INTEGER, ALLOCATABLE, DIMENSION(:, :) :: index_to_cell
2854 INTEGER, DIMENSION(:, :, :), POINTER :: cell_to_index
2855 INTEGER :: nimages
2856
2857 CHARACTER(LEN=*), PARAMETER :: routinen = 'sum_two_R_grids'
2858
2859 INTEGER :: handle, i_dim, img_1, img_2, nimages_max
2860 INTEGER, ALLOCATABLE, DIMENSION(:, :) :: index_to_cell_tmp
2861 INTEGER, DIMENSION(3) :: cell_1, cell_2, r, r_max, r_min
2862
2863 CALL timeset(routinen, handle)
2864
2865 DO i_dim = 1, 3
2866 r_min(i_dim) = minval(index_to_cell_1(i_dim, :)) + minval(index_to_cell_2(i_dim, :))
2867 r_max(i_dim) = maxval(index_to_cell_1(i_dim, :)) + maxval(index_to_cell_2(i_dim, :))
2868 END DO
2869
2870 nimages_max = (r_max(1) - r_min(1) + 1)*(r_max(2) - r_min(2) + 1)*(r_max(3) - r_min(3) + 1)
2871
2872 ALLOCATE (index_to_cell_tmp(3, nimages_max))
2873 index_to_cell_tmp(:, :) = -1
2874
2875 ALLOCATE (cell_to_index(r_min(1):r_max(1), r_min(2):r_max(2), r_min(3):r_max(3)))
2876 cell_to_index(:, :, :) = -1
2877
2878 nimages = 0
2879
2880 DO img_1 = 1, nimages_1
2881
2882 DO img_2 = 1, nimages_2
2883
2884 cell_1(1:3) = index_to_cell_1(1:3, img_1)
2885 cell_2(1:3) = index_to_cell_2(1:3, img_2)
2886
2887 r(1:3) = cell_1(1:3) + cell_2(1:3)
2888
2889 ! check whether we have found a new cell
2890 IF (cell_to_index(r(1), r(2), r(3)) == -1) THEN
2891
2892 nimages = nimages + 1
2893 cell_to_index(r(1), r(2), r(3)) = nimages
2894 index_to_cell_tmp(1:3, nimages) = r(1:3)
2895
2896 END IF
2897
2898 END DO
2899
2900 END DO
2901
2902 ALLOCATE (index_to_cell(3, nimages))
2903 index_to_cell(:, :) = index_to_cell_tmp(1:3, 1:nimages)
2904
2905 CALL timestop(handle)
2906
2907 END SUBROUTINE sum_two_r_grids
2908
2909! **************************************************************************************************
2910!> \brief ...
2911!> \param bs_env ...
2912! **************************************************************************************************
2913 SUBROUTINE setup_parallelization_delta_r(bs_env)
2914
2915 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
2916
2917 CHARACTER(LEN=*), PARAMETER :: routinen = 'setup_parallelization_Delta_R'
2918
2919 INTEGER :: handle, i_cell_delta_r, i_task_local, &
2920 n_tasks_local
2921 INTEGER, ALLOCATABLE, DIMENSION(:) :: i_cell_delta_r_group, &
2922 n_tensor_ops_delta_r
2923
2924 CALL timeset(routinen, handle)
2925
2926 CALL compute_n_tensor_ops_delta_r(bs_env, n_tensor_ops_delta_r)
2927
2928 CALL compute_delta_r_dist(bs_env, n_tensor_ops_delta_r, i_cell_delta_r_group, n_tasks_local)
2929
2930 bs_env%n_tasks_Delta_R_local = n_tasks_local
2931
2932 ALLOCATE (bs_env%task_Delta_R(n_tasks_local))
2933
2934 i_task_local = 0
2935 DO i_cell_delta_r = 1, bs_env%nimages_Delta_R
2936
2937 IF (i_cell_delta_r_group(i_cell_delta_r) /= bs_env%tensor_group_color) cycle
2938
2939 i_task_local = i_task_local + 1
2940
2941 bs_env%task_Delta_R(i_task_local) = i_cell_delta_r
2942
2943 END DO
2944
2945 ALLOCATE (bs_env%skip_DR_chi(n_tasks_local))
2946 bs_env%skip_DR_chi(:) = .false.
2947 ALLOCATE (bs_env%skip_DR_Sigma(n_tasks_local))
2948 bs_env%skip_DR_Sigma(:) = .false.
2949
2950 CALL allocate_skip_3xr(bs_env%skip_DR_R12_S_Goccx3c_chi, bs_env)
2951 CALL allocate_skip_3xr(bs_env%skip_DR_R12_S_Gvirx3c_chi, bs_env)
2952 CALL allocate_skip_3xr(bs_env%skip_DR_R_R2_MxM_chi, bs_env)
2953
2954 CALL allocate_skip_3xr(bs_env%skip_DR_R1_S2_Gx3c_Sigma, bs_env)
2955 CALL allocate_skip_3xr(bs_env%skip_DR_R1_R_MxM_Sigma, bs_env)
2956
2957 CALL timestop(handle)
2958
2959 END SUBROUTINE setup_parallelization_delta_r
2960
2961! **************************************************************************************************
2962!> \brief ...
2963!> \param bs_env ...
2964!> \param n_tensor_ops_Delta_R ...
2965! **************************************************************************************************
2966 SUBROUTINE compute_n_tensor_ops_delta_r(bs_env, n_tensor_ops_Delta_R)
2967 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
2968 INTEGER, ALLOCATABLE, DIMENSION(:) :: n_tensor_ops_delta_r
2969
2970 CHARACTER(LEN=*), PARAMETER :: routinen = 'compute_n_tensor_ops_Delta_R'
2971
2972 INTEGER :: handle, i_cell_delta_r, i_cell_r, i_cell_r1, i_cell_r1_minus_r, i_cell_r2, &
2973 i_cell_r2_m_r1, i_cell_s1, i_cell_s1_m_r1_p_r2, i_cell_s1_minus_r, i_cell_s2, &
2974 nimages_delta_r
2975 INTEGER, DIMENSION(3) :: cell_dr, cell_m_r1, cell_r, cell_r1, cell_r1_minus_r, cell_r2, &
2976 cell_r2_m_r1, cell_s1, cell_s1_m_r2_p_r1, cell_s1_minus_r, cell_s1_p_s2_m_r1, cell_s2
2977 LOGICAL :: cell_found
2978
2979 CALL timeset(routinen, handle)
2980
2981 nimages_delta_r = bs_env%nimages_Delta_R
2982
2983 ALLOCATE (n_tensor_ops_delta_r(nimages_delta_r))
2984 n_tensor_ops_delta_r(:) = 0
2985
2986 ! compute number of tensor operations for specific Delta_R
2987 DO i_cell_delta_r = 1, nimages_delta_r
2988
2989 IF (modulo(i_cell_delta_r, bs_env%num_tensor_groups) /= bs_env%tensor_group_color) cycle
2990
2991 DO i_cell_r1 = 1, bs_env%nimages_3c
2992
2993 cell_r1(1:3) = bs_env%index_to_cell_3c(1:3, i_cell_r1)
2994 cell_dr(1:3) = bs_env%index_to_cell_Delta_R(1:3, i_cell_delta_r)
2995
2996 ! S_1 = R_1 + ΔR (from ΔR = S_1 - R_1)
2997 CALL add_r(cell_r1, cell_dr, bs_env%index_to_cell_3c, cell_s1, &
2998 cell_found, bs_env%cell_to_index_3c, i_cell_s1)
2999 IF (.NOT. cell_found) cycle
3000
3001 DO i_cell_r2 = 1, bs_env%nimages_scf_desymm
3002
3003 cell_r2(1:3) = bs_env%kpoints_scf_desymm%index_to_cell(1:3, i_cell_r2)
3004
3005 ! R_2 - R_1
3006 CALL add_r(cell_r2, -cell_r1, bs_env%index_to_cell_3c, cell_r2_m_r1, &
3007 cell_found, bs_env%cell_to_index_3c, i_cell_r2_m_r1)
3008 IF (.NOT. cell_found) cycle
3009
3010 ! S_1 - R_1 + R_2
3011 CALL add_r(cell_s1, cell_r2_m_r1, bs_env%index_to_cell_3c, cell_s1_m_r2_p_r1, &
3012 cell_found, bs_env%cell_to_index_3c, i_cell_s1_m_r1_p_r2)
3013 IF (.NOT. cell_found) cycle
3014
3015 n_tensor_ops_delta_r(i_cell_delta_r) = n_tensor_ops_delta_r(i_cell_delta_r) + 1
3016
3017 END DO ! i_cell_R2
3018
3019 DO i_cell_s2 = 1, bs_env%nimages_scf_desymm
3020
3021 cell_s2(1:3) = bs_env%kpoints_scf_desymm%index_to_cell(1:3, i_cell_s2)
3022 cell_m_r1(1:3) = -cell_r1(1:3)
3023 cell_s1_p_s2_m_r1(1:3) = cell_s1(1:3) + cell_s2(1:3) - cell_r1(1:3)
3024
3025 CALL is_cell_in_index_to_cell(cell_m_r1, bs_env%index_to_cell_3c, cell_found)
3026 IF (.NOT. cell_found) cycle
3027
3028 CALL is_cell_in_index_to_cell(cell_s1_p_s2_m_r1, bs_env%index_to_cell_3c, cell_found)
3029 IF (.NOT. cell_found) cycle
3030
3031 END DO ! i_cell_S2
3032
3033 DO i_cell_r = 1, bs_env%nimages_scf_desymm
3034
3035 cell_r = bs_env%kpoints_scf_desymm%index_to_cell(1:3, i_cell_r)
3036
3037 ! R_1 - R
3038 CALL add_r(cell_r1, -cell_r, bs_env%index_to_cell_3c, cell_r1_minus_r, &
3039 cell_found, bs_env%cell_to_index_3c, i_cell_r1_minus_r)
3040 IF (.NOT. cell_found) cycle
3041
3042 ! S_1 - R
3043 CALL add_r(cell_s1, -cell_r, bs_env%index_to_cell_3c, cell_s1_minus_r, &
3044 cell_found, bs_env%cell_to_index_3c, i_cell_s1_minus_r)
3045 IF (.NOT. cell_found) cycle
3046
3047 END DO ! i_cell_R
3048
3049 END DO ! i_cell_R1
3050
3051 END DO ! i_cell_Delta_R
3052
3053 CALL bs_env%para_env%sum(n_tensor_ops_delta_r)
3054
3055 CALL timestop(handle)
3056
3057 END SUBROUTINE compute_n_tensor_ops_delta_r
3058
3059! **************************************************************************************************
3060!> \brief ...
3061!> \param cell_1 ...
3062!> \param cell_2 ...
3063!> \param index_to_cell ...
3064!> \param cell_1_plus_2 ...
3065!> \param cell_found ...
3066!> \param cell_to_index ...
3067!> \param i_cell_1_plus_2 ...
3068! **************************************************************************************************
3069 SUBROUTINE add_r(cell_1, cell_2, index_to_cell, cell_1_plus_2, cell_found, &
3070 cell_to_index, i_cell_1_plus_2)
3071
3072 INTEGER, DIMENSION(3) :: cell_1, cell_2
3073 INTEGER, DIMENSION(:, :) :: index_to_cell
3074 INTEGER, DIMENSION(3) :: cell_1_plus_2
3075 LOGICAL :: cell_found
3076 INTEGER, DIMENSION(:, :, :), INTENT(IN), &
3077 OPTIONAL, POINTER :: cell_to_index
3078 INTEGER, INTENT(OUT), OPTIONAL :: i_cell_1_plus_2
3079
3080 CHARACTER(LEN=*), PARAMETER :: routinen = 'add_R'
3081
3082 INTEGER :: handle
3083
3084 CALL timeset(routinen, handle)
3085
3086 cell_1_plus_2(1:3) = cell_1(1:3) + cell_2(1:3)
3087
3088 CALL is_cell_in_index_to_cell(cell_1_plus_2, index_to_cell, cell_found)
3089
3090 IF (PRESENT(i_cell_1_plus_2)) THEN
3091 IF (cell_found) THEN
3092 cpassert(PRESENT(cell_to_index))
3093 i_cell_1_plus_2 = cell_to_index(cell_1_plus_2(1), cell_1_plus_2(2), cell_1_plus_2(3))
3094 ELSE
3095 i_cell_1_plus_2 = -1000
3096 END IF
3097 END IF
3098
3099 CALL timestop(handle)
3100
3101 END SUBROUTINE add_r
3102
3103! **************************************************************************************************
3104!> \brief ...
3105!> \param cell ...
3106!> \param index_to_cell ...
3107!> \param cell_found ...
3108! **************************************************************************************************
3109 SUBROUTINE is_cell_in_index_to_cell(cell, index_to_cell, cell_found)
3110 INTEGER, DIMENSION(3) :: cell
3111 INTEGER, DIMENSION(:, :) :: index_to_cell
3112 LOGICAL :: cell_found
3113
3114 CHARACTER(LEN=*), PARAMETER :: routinen = 'is_cell_in_index_to_cell'
3115
3116 INTEGER :: handle, i_cell, nimg
3117 INTEGER, DIMENSION(3) :: cell_i
3118
3119 CALL timeset(routinen, handle)
3120
3121 nimg = SIZE(index_to_cell, 2)
3122
3123 cell_found = .false.
3124
3125 DO i_cell = 1, nimg
3126
3127 cell_i(1:3) = index_to_cell(1:3, i_cell)
3128
3129 IF (cell_i(1) == cell(1) .AND. cell_i(2) == cell(2) .AND. cell_i(3) == cell(3)) THEN
3130 cell_found = .true.
3131 END IF
3132
3133 END DO
3134
3135 CALL timestop(handle)
3136
3137 END SUBROUTINE is_cell_in_index_to_cell
3138
3139! **************************************************************************************************
3140!> \brief ...
3141!> \param bs_env ...
3142!> \param n_tensor_ops_Delta_R ...
3143!> \param i_cell_Delta_R_group ...
3144!> \param n_tasks_local ...
3145! **************************************************************************************************
3146 SUBROUTINE compute_delta_r_dist(bs_env, n_tensor_ops_Delta_R, i_cell_Delta_R_group, n_tasks_local)
3147 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
3148 INTEGER, ALLOCATABLE, DIMENSION(:) :: n_tensor_ops_delta_r, &
3149 i_cell_delta_r_group
3150 INTEGER :: n_tasks_local
3151
3152 CHARACTER(LEN=*), PARAMETER :: routinen = 'compute_Delta_R_dist'
3153
3154 INTEGER :: handle, i_delta_r_max_op, i_group_min, &
3155 nimages_delta_r, u
3156 INTEGER, ALLOCATABLE, DIMENSION(:) :: n_tensor_ops_delta_r_in_group
3157
3158 CALL timeset(routinen, handle)
3159
3160 nimages_delta_r = bs_env%nimages_Delta_R
3161
3162 u = bs_env%unit_nr
3163
3164 IF (u > 0 .AND. nimages_delta_r < bs_env%num_tensor_groups) THEN
3165 WRITE (u, fmt="(T2,A,I5,A,I5,A)") "There are only ", nimages_delta_r, &
3166 " tasks to work on but there are ", bs_env%num_tensor_groups, " groups."
3167 WRITE (u, fmt="(T2,A)") "Please reduce the number of MPI processes."
3168 WRITE (u, '(T2,A)') ''
3169 END IF
3170
3171 ALLOCATE (n_tensor_ops_delta_r_in_group(bs_env%num_tensor_groups))
3172 n_tensor_ops_delta_r_in_group(:) = 0
3173 ALLOCATE (i_cell_delta_r_group(nimages_delta_r))
3174 i_cell_delta_r_group(:) = -1
3175
3176 n_tasks_local = 0
3177
3178 DO WHILE (any(n_tensor_ops_delta_r(:) /= 0))
3179
3180 ! get largest element of n_tensor_ops_Delta_R
3181 i_delta_r_max_op = maxloc(n_tensor_ops_delta_r, 1)
3182
3183 ! distribute i_Delta_R_max_op to tensor group which has currently the smallest load
3184 i_group_min = minloc(n_tensor_ops_delta_r_in_group, 1)
3185
3186 ! the tensor groups are 0-index based; but i_group_min is 1-index based
3187 i_cell_delta_r_group(i_delta_r_max_op) = i_group_min - 1
3188 n_tensor_ops_delta_r_in_group(i_group_min) = n_tensor_ops_delta_r_in_group(i_group_min) + &
3189 n_tensor_ops_delta_r(i_delta_r_max_op)
3190
3191 ! remove i_Delta_R_max_op from n_tensor_ops_Delta_R
3192 n_tensor_ops_delta_r(i_delta_r_max_op) = 0
3193
3194 IF (bs_env%tensor_group_color == i_group_min - 1) n_tasks_local = n_tasks_local + 1
3195
3196 END DO
3197
3198 CALL timestop(handle)
3199
3200 END SUBROUTINE compute_delta_r_dist
3201
3202! **************************************************************************************************
3203!> \brief ...
3204!> \param skip ...
3205!> \param bs_env ...
3206! **************************************************************************************************
3207 SUBROUTINE allocate_skip_3xr(skip, bs_env)
3208 LOGICAL, ALLOCATABLE, DIMENSION(:, :, :) :: skip
3209 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
3210
3211 CHARACTER(LEN=*), PARAMETER :: routinen = 'allocate_skip_3xR'
3212
3213 INTEGER :: handle
3214
3215 CALL timeset(routinen, handle)
3216
3217 ALLOCATE (skip(bs_env%n_tasks_Delta_R_local, bs_env%nimages_3c, bs_env%nimages_scf_desymm))
3218 skip(:, :, :) = .false.
3219
3220 CALL timestop(handle)
3221
3222 END SUBROUTINE allocate_skip_3xr
3223
3224! **************************************************************************************************
3225!> \brief ...
3226!> \param qs_env ...
3227!> \param bs_env ...
3228! **************************************************************************************************
3229 SUBROUTINE allocate_matrices_small_cell_full_kp_tensor(qs_env, bs_env)
3230 TYPE(qs_environment_type), POINTER :: qs_env
3231 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
3232
3233 CHARACTER(LEN=*), PARAMETER :: routinen = 'allocate_matrices_small_cell_full_kp_tensor'
3234
3235 INTEGER :: handle, i_spin, i_t, img, n_spin, &
3236 nimages_scf, num_time_freq_points
3237 TYPE(cp_blacs_env_type), POINTER :: blacs_env
3238 TYPE(mp_para_env_type), POINTER :: para_env
3239
3240 CALL timeset(routinen, handle)
3241
3242 nimages_scf = bs_env%nimages_scf_desymm
3243 num_time_freq_points = bs_env%num_time_freq_points
3244 n_spin = bs_env%n_spin
3245
3246 CALL get_qs_env(qs_env, para_env=para_env, blacs_env=blacs_env)
3247
3248 ALLOCATE (bs_env%fm_G_S(nimages_scf))
3249 ALLOCATE (bs_env%fm_Sigma_x_R(nimages_scf))
3250 ALLOCATE (bs_env%fm_chi_R_t(nimages_scf, num_time_freq_points))
3251 ALLOCATE (bs_env%fm_MWM_R_t(nimages_scf, num_time_freq_points))
3252 ALLOCATE (bs_env%fm_Sigma_c_R_neg_tau(nimages_scf, num_time_freq_points, n_spin))
3253 ALLOCATE (bs_env%fm_Sigma_c_R_pos_tau(nimages_scf, num_time_freq_points, n_spin))
3254 DO img = 1, nimages_scf
3255 CALL cp_fm_create(bs_env%fm_G_S(img), bs_env%fm_work_mo(1)%matrix_struct)
3256 CALL cp_fm_create(bs_env%fm_Sigma_x_R(img), bs_env%fm_work_mo(1)%matrix_struct)
3257 DO i_t = 1, num_time_freq_points
3258 CALL cp_fm_create(bs_env%fm_chi_R_t(img, i_t), bs_env%fm_RI_RI%matrix_struct)
3259 CALL cp_fm_create(bs_env%fm_MWM_R_t(img, i_t), bs_env%fm_RI_RI%matrix_struct)
3260 CALL cp_fm_set_all(bs_env%fm_MWM_R_t(img, i_t), 0.0_dp)
3261 DO i_spin = 1, n_spin
3262 CALL cp_fm_create(bs_env%fm_Sigma_c_R_neg_tau(img, i_t, i_spin), &
3263 bs_env%fm_work_mo(1)%matrix_struct)
3264 CALL cp_fm_create(bs_env%fm_Sigma_c_R_pos_tau(img, i_t, i_spin), &
3265 bs_env%fm_work_mo(1)%matrix_struct)
3266 CALL cp_fm_set_all(bs_env%fm_Sigma_c_R_neg_tau(img, i_t, i_spin), 0.0_dp)
3267 CALL cp_fm_set_all(bs_env%fm_Sigma_c_R_pos_tau(img, i_t, i_spin), 0.0_dp)
3268 END DO
3269 END DO
3270 END DO
3271
3272 CALL timestop(handle)
3273
3274 END SUBROUTINE allocate_matrices_small_cell_full_kp_tensor
3275
3276! **************************************************************************************************
3277!> \brief ...
3278!> \param qs_env ...
3279!> \param bs_env ...
3280! **************************************************************************************************
3281 SUBROUTINE trafo_v_xc_r_to_kp(qs_env, bs_env)
3282 TYPE(qs_environment_type), POINTER :: qs_env
3283 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
3284
3285 CHARACTER(LEN=*), PARAMETER :: routinen = 'trafo_V_xc_R_to_kp'
3286
3287 INTEGER :: handle, ikp, img, ispin, n_ao
3288 TYPE(cp_cfm_type) :: cfm_kp_group, cfm_mo_coeff, cfm_v_xc
3289 TYPE(cp_fm_type) :: fm_v_xc_re
3290 TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_ks
3291 TYPE(kpoint_operator_context_type) :: operators
3292 TYPE(kpoint_type), POINTER :: kpoints_bs
3293 TYPE(mp_para_env_type), POINTER :: para_env
3294
3295 CALL timeset(routinen, handle)
3296
3297 n_ao = bs_env%n_ao
3298
3299 CALL get_qs_env(qs_env, para_env=para_env, matrix_ks_kp=matrix_ks)
3300
3301 CALL cp_cfm_create(cfm_v_xc, bs_env%cfm_work_mo%matrix_struct)
3302 CALL cp_cfm_create(cfm_mo_coeff, bs_env%cfm_work_mo%matrix_struct)
3303 CALL cp_fm_create(fm_v_xc_re, bs_env%cfm_work_mo%matrix_struct)
3304
3305 DO img = 1, bs_env%nimages_scf
3306 DO ispin = 1, bs_env%n_spin
3307 ! JW kind of hack because the format of matrix_ks remains dubious...
3308 CALL dbcsr_set(matrix_ks(ispin, img)%matrix, 0.0_dp)
3309 CALL copy_fm_to_dbcsr(bs_env%fm_V_xc_R(img, ispin), matrix_ks(ispin, img)%matrix)
3310 END DO
3311 END DO
3312
3313 CALL create_operator_kpoints(qs_env, bs_env, kpoints_bs, cfm_kp_group)
3314 CALL kpoint_operator_context_create(operators, kpoints_bs, bs_env%fm_work_mo(1), bs_env%n_spin)
3315 CALL kpoint_operator_prepare(operators, matrix_ks, kpoint_slot_ks)
3316
3317 ALLOCATE (bs_env%v_xc_n(n_ao, bs_env%nkp_bs_and_DOS, bs_env%n_spin))
3318
3319 DO ispin = 1, bs_env%n_spin
3320 DO ikp = 1, bs_env%nkp_bs_and_DOS
3321
3322 ! v^xc^R -> v^xc(k) (matrix_ks stores v^xc^R, see SUBROUTINE compute_V_xc)
3323 CALL kpoint_operator_get(operators, ikp, ispin, matrix_ks, cmat=cfm_kp_group, &
3324 matrix_row=ispin, slot=kpoint_slot_ks)
3325 CALL cp_cfm_copy_general(cfm_kp_group, cfm_v_xc, para_env)
3326
3327 ! get C_µn(k)
3328 CALL cp_cfm_to_cfm(bs_env%cfm_mo_coeff_kp(ikp, ispin), cfm_mo_coeff)
3329
3330 ! v^xc_nm(k_i) = sum_µν C^*_µn(k_i) v^xc_µν(k_i) C_νn(k_i)
3331 CALL cfm_contract_aba(cfm_mo_coeff, cfm_v_xc)
3332
3333 ! get v^xc_nn(k_i) which is a real quantity as v^xc is Hermitian
3334 CALL cp_cfm_to_fm(cfm_v_xc, fm_v_xc_re)
3335 CALL cp_fm_get_diag(fm_v_xc_re, bs_env%v_xc_n(:, ikp, ispin))
3336
3337 END DO
3338
3339 END DO
3340
3341 CALL kpoint_operator_context_release(operators)
3342 CALL cp_cfm_release(cfm_kp_group)
3343 CALL kpoint_release(kpoints_bs)
3344
3345 ! just rebuild the overwritten KS matrix again
3346 CALL build_periodic_ks_with_gapw_density(qs_env)
3347
3348 CALL cp_cfm_release(cfm_v_xc)
3349 CALL cp_cfm_release(cfm_mo_coeff)
3350 CALL cp_fm_release(fm_v_xc_re)
3351
3352 CALL timestop(handle)
3353
3354 END SUBROUTINE trafo_v_xc_r_to_kp
3355
3356! **************************************************************************************************
3357!> \brief ...
3358!> \param qs_env ...
3359!> \param bs_env ...
3360! **************************************************************************************************
3361 SUBROUTINE heuristic_ri_regularization(qs_env, bs_env)
3362 TYPE(qs_environment_type), POINTER :: qs_env
3363 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
3364
3365 CHARACTER(LEN=*), PARAMETER :: routinen = 'heuristic_RI_regularization'
3366
3367 COMPLEX(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :) :: m
3368 INTEGER :: handle, ikp, ikp_local, n_ri, nkp, &
3369 nkp_local, u
3370 REAL(kind=dp) :: cond_nr, cond_nr_max, max_ev, &
3371 max_ev_ikp, min_ev, min_ev_ikp
3372 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :, :) :: m_r
3373
3374 CALL timeset(routinen, handle)
3375
3376 ! compute M^R_PQ = <phi_P,0|V^tr(rc)|phi_Q,R> for RI metric
3377 CALL get_v_tr_r(m_r, bs_env%ri_metric, 0.0_dp, bs_env, qs_env)
3378
3379 nkp = bs_env%nkp_chi_eps_W_orig_plus_extra
3380 n_ri = bs_env%n_RI
3381
3382 nkp_local = 0
3383 DO ikp = 1, nkp
3384 ! trivial parallelization over k-points
3385 IF (modulo(ikp, bs_env%para_env%num_pe) /= bs_env%para_env%mepos) cycle
3386 nkp_local = nkp_local + 1
3387 END DO
3388
3389 ALLOCATE (m(n_ri, n_ri, nkp_local))
3390
3391 ikp_local = 0
3392 cond_nr_max = 0.0_dp
3393 min_ev = 1000.0_dp
3394 max_ev = -1000.0_dp
3395
3396 DO ikp = 1, nkp
3397
3398 ! trivial parallelization
3399 IF (modulo(ikp, bs_env%para_env%num_pe) /= bs_env%para_env%mepos) cycle
3400
3401 ikp_local = ikp_local + 1
3402
3403 ! M(k) = sum_R e^ikR M^R
3404 CALL rs_to_kp(m_r, m(:, :, ikp_local), &
3405 bs_env%kpoints_scf_desymm%index_to_cell, &
3406 bs_env%kpoints_chi_eps_W%xkp(1:3, ikp))
3407
3408 ! compute condition number of M_PQ(k)
3409 CALL local_complex_power(m(:, :, ikp_local), 1.0_dp, 0.0_dp, cond_nr, min_ev_ikp, max_ev_ikp)
3410
3411 IF (cond_nr > cond_nr_max) cond_nr_max = cond_nr
3412 IF (max_ev_ikp > max_ev) max_ev = max_ev_ikp
3413 IF (min_ev_ikp < min_ev) min_ev = min_ev_ikp
3414
3415 END DO ! ikp
3416
3417 CALL bs_env%para_env%max(cond_nr_max)
3418 CALL bs_env%para_env%min(min_ev)
3419 CALL bs_env%para_env%max(max_ev)
3420
3421 u = bs_env%unit_nr
3422 IF (u > 0) THEN
3423 WRITE (u, fmt="(T2,A,ES34.1)") "Min. abs. eigenvalue of RI metric matrix M(k)", min_ev
3424 WRITE (u, fmt="(T2,A,ES34.1)") "Max. abs. eigenvalue of RI metric matrix M(k)", max_ev
3425 WRITE (u, fmt="(T2,A,ES50.1)") "Max. condition number of M(k)", cond_nr_max
3426 END IF
3427
3428 CALL timestop(handle)
3429
3430 END SUBROUTINE heuristic_ri_regularization
3431
3432! **************************************************************************************************
3433!> \brief ...
3434!> \param bs_env ...
3435! **************************************************************************************************
3436 SUBROUTINE setup_time_and_frequency_minimax_grid(bs_env)
3437 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
3438
3439 CHARACTER(LEN=*), PARAMETER :: routinen = 'setup_time_and_frequency_minimax_grid'
3440
3441 INTEGER :: handle, homo, ispin, n_mo, n_top, &
3442 num_time_freq_points, u
3443 REAL(kind=dp) :: e_max, e_max_ispin, e_min, e_min_ispin, &
3444 e_range, max_error_min, max_error_sigma
3445
3446 CALL timeset(routinen, handle)
3447
3448 n_mo = bs_env%n_ao
3449 num_time_freq_points = bs_env%num_time_freq_points
3450
3451 ! minimum and maximum difference between eigenvalues of unoccupied and an occupied MOs
3452 e_min = 1000.0_dp
3453 e_max = -1000.0_dp
3454 DO ispin = 1, bs_env%n_spin
3455 homo = bs_env%n_occ(ispin)
3456
3457 ! Highest index that is a real state. The canonical orthogonalization removes the
3458 ! linearly dependent basis modes and parks them at the top of the spectrum with a
3459 ! placeholder eigenvalue; n_mo_retained is how many real states it kept.
3460 n_top = max(min(bs_env%n_mo_retained, n_mo), homo + 1)
3461
3462 SELECT CASE (bs_env%gw_implementation)
3464 e_min_ispin = bs_env%eigenval_scf_Gamma(homo + 1, ispin) - &
3465 bs_env%eigenval_scf_Gamma(homo, ispin)
3466 e_max_ispin = bs_env%eigenval_scf_Gamma(n_top, ispin) - &
3467 bs_env%eigenval_scf_Gamma(1, ispin)
3469 e_min_ispin = minval(bs_env%eigenval_scf(homo + 1, :, ispin)) - &
3470 maxval(bs_env%eigenval_scf(homo, :, ispin))
3471 e_max_ispin = maxval(bs_env%eigenval_scf(n_top, :, ispin)) - &
3472 minval(bs_env%eigenval_scf(1, :, ispin))
3473 END SELECT
3474 e_min = min(e_min, e_min_ispin)
3475 e_max = max(e_max, e_max_ispin)
3476 END DO
3477
3478 ! Open-shell uses ONE minimax grid for the combined [min gap, max span] over both spins (the
3479 ! superset covers each channel, so it is accurate; per-spin grids would only be more efficient).
3480 IF (bs_env%n_spin > 1) THEN
3481 CALL cp_hint(__location__, &
3482 "Open-shell GW uses one time-frequency grid spanning [min gap, max span] across both "// &
3483 "spin channels; raise NUM_TIME_FREQ_POINTS if QP convergence is marginal for "// &
3484 "strongly spin-asymmetric systems.")
3485 END IF
3486
3487 e_range = e_max/e_min
3488
3489 CALL build_minimax_time_frequency_grid(num_time_freq_points, e_min, e_max, bs_env%regularization_minimax, &
3490 bs_env%num_points_per_magnitude, bs_env%time_frequency_grid, &
3491 build_frequency=.true., build_time=.true., build_transforms=.true., &
3492 build_sine=.true., time_scaling=2.0_dp, time_weight_scaling=1.0_dp, &
3493 max_fit_error=max_error_min, print_warning=.false., unit_nr=0, &
3494 prefer_external_backend=.false.)
3495
3496 ! Σ^c(iτ) -> Σ^c(iω) has its own weights: for NEVANLINNA, a truncated SVD keeps the samples
3497 ! of Σ^c(iω) causal; χ and W keep the regularized weights and do not depend on the
3498 ! analytic continuation
3499 IF (bs_env%ac_method == gw_ac_nevanlinna) THEN
3500 CALL build_minimax_time_frequency_grid(num_time_freq_points, e_min, e_max, bs_env%regularization_minimax, &
3501 bs_env%num_points_per_magnitude, bs_env%time_frequency_grid_sigma, &
3502 build_frequency=.true., build_time=.true., build_transforms=.true., &
3503 build_sine=.true., time_scaling=2.0_dp, time_weight_scaling=1.0_dp, &
3504 max_fit_error=max_error_sigma, print_warning=.false., unit_nr=0, &
3505 prefer_external_backend=.false., &
3506 rcond=bs_env%nevanlinna%rcond_weights)
3507 ELSE
3508 bs_env%time_frequency_grid_sigma = bs_env%time_frequency_grid
3509 END IF
3510
3511 ! determine number of fit points in the interval [0,ω_max] for virt, or [-ω_max,0] for occ
3512 bs_env%num_freq_points_fit = count(bs_env%time_frequency_grid%frequency < bs_env%freq_max_fit)
3513
3514 ! iω values for the analytic continuation Σ^c_n(iω,k) -> Σ^c_n(ϵ,k)
3515 ALLOCATE (bs_env%imag_freq_points_fit(bs_env%num_freq_points_fit))
3516 bs_env%imag_freq_points_fit(:) = pack(bs_env%time_frequency_grid%frequency, &
3517 bs_env%time_frequency_grid%frequency < bs_env%freq_max_fit)
3518
3519 ! reset the number of Padé parameters if smaller than the number of
3520 ! imaginary-frequency points for the fit
3521 IF (bs_env%num_freq_points_fit < bs_env%nparam_pade) THEN
3522 bs_env%nparam_pade = bs_env%num_freq_points_fit
3523 END IF
3524
3525 ! the Nevanlinna interpolant goes through all imaginary frequencies; its Padé fallback keeps
3526 ! the points below ω_max and the number of Padé parameters of the PADE continuation
3527 bs_env%num_freq_points_pade = bs_env%num_freq_points_fit
3528 IF (bs_env%ac_method == gw_ac_nevanlinna) THEN
3529 bs_env%num_freq_points_fit = num_time_freq_points
3530 DEALLOCATE (bs_env%imag_freq_points_fit)
3531 ALLOCATE (bs_env%imag_freq_points_fit(num_time_freq_points))
3532 bs_env%imag_freq_points_fit(:) = bs_env%time_frequency_grid%frequency
3533 END IF
3534
3535 u = bs_env%unit_nr
3536 IF (u > 0) THEN
3537 WRITE (u, '(T2,A)') ''
3538 WRITE (u, '(T2,A,F50.2)') 'SCF fundamental band gap (eV)', e_min*evolt
3539 WRITE (u, '(T2,A,F53.2)') 'Max. SCF eigval diff. (eV)', e_max*evolt
3540 WRITE (u, '(T2,A,F55.2)') 'E-Range for minimax grid', e_range
3541 WRITE (u, '(T2,A,I27)') 'Number of Padé parameters for analytic continuation:', &
3542 bs_env%nparam_pade
3543 WRITE (u, '(T2,A)') ''
3544 END IF
3545
3546 ! in minimax grids, Fourier transforms t -> w and w -> t are split using
3547 ! e^(iwt) = cos(wt) + i sin(wt); we thus calculate weights for trafos with a cos and
3548 ! sine prefactor; details in Azizi, Wilhelm, Golze, Giantomassi, Panades-Barrueta,
3549 ! Rinke, Draxl, Gonze et al., 2 publications
3550
3551 CALL timestop(handle)
3552
3553 END SUBROUTINE setup_time_and_frequency_minimax_grid
3554
3555! **************************************************************************************************
3556!> \brief Releases the memory-heavy GW intermediates that cannot be freed in bs_env_release,
3557!> retaining the 3c neighbor list only when an AO-RI RT-BSE self-energy still needs it
3558!> \param qs_env ...
3559!> \param bs_env ...
3560! **************************************************************************************************
3561 SUBROUTINE de_init_bs_env(qs_env, bs_env)
3562 TYPE(qs_environment_type), POINTER :: qs_env
3563 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
3564
3565 CHARACTER(LEN=*), PARAMETER :: routinen = 'de_init_bs_env'
3566
3567 INTEGER :: handle
3568 LOGICAL :: retain_nl_3c, rirs_kernel
3569
3570 CALL timeset(routinen, handle)
3571 ! deallocate quantities here which:
3572 ! 1. cannot be deallocated in bs_env_release due to circular dependencies
3573 ! 2. consume a lot of memory and should not be kept until the quantity is
3574 ! deallocated in bs_env_release
3575
3576 ! nl_3c feeds compute_3c_integrals after the GW: the AO-RI RT-BSE SEX self-energy (the RI-RS
3577 ! path never reads it, and AO-RI Hartree builds its own blocks) and PROPERTIES%BSE (keep_3c_nl).
3578 retain_nl_3c = bs_env%keep_3c_nl
3579 IF (ASSOCIATED(bs_env%nl_3c%ij_list) .AND. (bs_env%rtp_method == rtp_method_bse)) THEN
3580 CALL rtbse_resolve_rirs_flag(qs_env, bs_env, rirs_kernel=rirs_kernel)
3581 retain_nl_3c = retain_nl_3c .OR. .NOT. rirs_kernel
3582 END IF
3583
3584 IF (retain_nl_3c) THEN
3585 IF (bs_env%unit_nr > 0) WRITE (bs_env%unit_nr, *) "Keep nl_3c for BSE"
3586 ELSE
3587 CALL neighbor_list_3c_destroy(bs_env%nl_3c)
3588 END IF
3589
3590 CALL timestop(handle)
3591
3592 END SUBROUTINE de_init_bs_env
3593
3594! **************************************************************************************************
3595!> \brief Resolve the linRTBSE RI-RS kernel switch from the KERNEL_RI input and the GW default.
3596!> \param qs_env ...
3597!> \param bs_env ...
3598!> \param rirs_kernel (optional) .TRUE. if the Hartree + SEX kernels use the RI-RS grid backend
3599!> \author Maximilian Graml
3600!> \note Single source of truth shared by create_rtbse_env (sets the flag) and de_init_bs_env
3601!> (decides whether to retain nl_3c). KERNEL_RI=DEFAULT follows bs_env%do_gw_ri_rs;
3602!> RS/AO force; forced .FALSE. for non-linearized (full) RT-BSE (warn on explicit RS).
3603! **************************************************************************************************
3604 SUBROUTINE rtbse_resolve_rirs_flag(qs_env, bs_env, rirs_kernel)
3605 TYPE(qs_environment_type), POINTER :: qs_env
3606 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
3607 LOGICAL, INTENT(OUT), OPTIONAL :: rirs_kernel
3608
3609 INTEGER :: kernel_ri
3610 LOGICAL :: my_rirs_kernel
3611 TYPE(dft_control_type), POINTER :: dft_control
3612 TYPE(section_vals_type), POINTER :: input
3613
3614 NULLIFY (dft_control, input)
3615 CALL get_qs_env(qs_env, dft_control=dft_control, input=input)
3616
3617 CALL section_vals_val_get(input, "DFT%REAL_TIME_PROPAGATION%RTBSE%KERNEL_RI", &
3618 i_val=kernel_ri)
3619 SELECT CASE (kernel_ri)
3621 my_rirs_kernel = .true.
3623 my_rirs_kernel = .false.
3624 CASE DEFAULT ! rtp_bse_kernel_ri_default
3625 my_rirs_kernel = bs_env%do_gw_ri_rs
3626 END SELECT
3627
3628 ! RI-RS kernels are implemented for linearized RT-BSE only; full RT-BSE always uses AO-RI.
3629 IF (dft_control%rtp_control%rtp_method /= rtp_method_bse_linearized) THEN
3630 IF (kernel_ri == rtp_bse_kernel_ri_rs) THEN
3631 cpwarn("RI-RS kernels are implemented for linearized RT-BSE only; forcing AO")
3632 END IF
3633 my_rirs_kernel = .false.
3634 END IF
3635
3636 IF (PRESENT(rirs_kernel)) rirs_kernel = my_rirs_kernel
3637
3638 END SUBROUTINE rtbse_resolve_rirs_flag
3639
3640! **************************************************************************************************
3641!> \brief ...
3642!> \param bs_env ...
3643!> \param Sigma_c_ikp_n_freq Complex diagonal self-energy at the signed fit frequencies.
3644!> \param Sigma_x_ikp_n ...
3645!> \param V_xc_ikp_n ...
3646!> \param eigenval_scf ...
3647!> \param ikp ...
3648!> \param ispin ...
3649!> \param i_iter ...
3650! **************************************************************************************************
3651 SUBROUTINE analyt_conti_and_print(bs_env, Sigma_c_ikp_n_freq, Sigma_x_ikp_n, V_xc_ikp_n, &
3652 eigenval_scf, ikp, ispin, i_iter)
3653
3654 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
3655 COMPLEX(KIND=dp), DIMENSION(:, :), INTENT(IN) :: sigma_c_ikp_n_freq
3656 REAL(kind=dp), DIMENSION(:) :: sigma_x_ikp_n, v_xc_ikp_n, eigenval_scf
3657 INTEGER :: ikp, ispin
3658 INTEGER, INTENT(IN), OPTIONAL :: i_iter
3659
3660 CHARACTER(LEN=*), PARAMETER :: routinen = 'analyt_conti_and_print'
3661 INTEGER, PARAMETER :: max_pade_orders = 7
3662
3663 CHARACTER(len=3) :: occ_vir
3664 CHARACTER(len=default_path_length) :: fname
3665 CHARACTER(len=default_string_length) :: gw_label
3666 INTEGER :: ac_used, handle, i_central, i_mo, i_order, icycle, ikp_for_print, iunit, &
3667 n_ac_nevanlinna, n_ac_pade, n_central, n_mo, n_orders, n_window, nkp
3668 INTEGER, ALLOCATABLE, DIMENSION(:) :: multi_count
3669 INTEGER, DIMENSION(max_pade_orders) :: order_index
3670 LOGICAL :: do_nevanlinna, is_bandstruc_kpoint, &
3671 multi_i, print_dos_kpoints, print_ikp
3672 LOGICAL, ALLOCATABLE, DIMENSION(:) :: multi, unstable
3673 REAL(kind=dp) :: ac_pick, pick_min
3674 REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: eigenval_g, m_value, sigma_c_ikp_n_qp, &
3675 z_value
3676 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :) :: sigma_c_orders
3677 REAL(kind=dp), DIMENSION(max_pade_orders) :: sigma_c_sorted
3678
3679 CALL timeset(routinen, handle)
3680
3681 icycle = 1
3682 IF (PRESENT(i_iter)) icycle = i_iter
3683
3684 ! A single Padé approximant occasionally places a spurious pole next to the
3685 ! quasiparticle energy, where Newton then fails and Σ^c jumps by up to 1 eV from one
3686 ! k-point to the next. Averaging the central values over the orders nparam_pade,
3687 ! nparam_pade - 1, ..., nparam_pade - 6 discards such outliers: the three central
3688 ! values for an odd number of orders, the two central values for an even one.
3689 n_orders = max(1, min(max_pade_orders, bs_env%nparam_pade - 1))
3690 n_central = min(n_orders, 3 - modulo(n_orders + 1, 2))
3691 i_central = (n_orders - n_central)/2 + 1
3692
3693 n_mo = bs_env%n_ao
3694 ALLOCATE (z_value(n_mo), m_value(n_mo), sigma_c_ikp_n_qp(n_mo), eigenval_g(n_mo), &
3695 sigma_c_orders(n_mo, n_orders))
3696 sigma_c_orders(:, :) = 0.0_dp
3697 sigma_c_ikp_n_qp(:) = 0.0_dp
3698
3699 do_nevanlinna = (bs_env%ac_method == gw_ac_nevanlinna)
3700 ALLOCATE (unstable(n_mo), multi(n_mo), multi_count(n_mo))
3701 unstable(:) = .false.
3702 multi(:) = .false.
3703 multi_count(:) = 0
3704 n_window = 0
3705 n_ac_nevanlinna = 0
3706 n_ac_pade = 0
3707 pick_min = 0.0_dp
3708
3709 ! Eigenvalues of the Green's function that produced Σ^c: the DFT ones for G0W0, the
3710 ! previous cycle's quasiparticle energies for evGW0. They set the Newton start value,
3711 ! the Z/m linearization point and the Hedin shift; the QP equation itself stays
3712 ! referenced to the DFT eigenvalues through the Eigenval_scf argument below.
3713 IF (bs_env%gw_flavour == evgw0 .AND. ALLOCATED(bs_env%eigenval_evGW0)) THEN
3714 eigenval_g(:) = bs_env%eigenval_evGW0(:, ikp, ispin)
3715 ELSE
3716 eigenval_g(:) = eigenval_scf(:)
3717 END IF
3718
3719 IF (do_nevanlinna) THEN
3720
3721 ! Nevanlinna continuation through all imaginary frequencies, then the median over fit
3722 ! windows and the marks of unreliable (*) and multi-solution (m) levels
3723 DO i_mo = 1, n_mo
3724 IF (modulo(i_mo, bs_env%para_env%num_pe) /= bs_env%para_env%mepos) cycle
3725 CALL continuation_pade(sigma_c_ikp_n_qp, &
3726 bs_env%imag_freq_points_fit, z_value, m_value, &
3727 sigma_c_ikp_n_freq(:, 1:bs_env%num_freq_points_fit), &
3728 sigma_x_ikp_n(:) - v_xc_ikp_n(:), &
3729 eigenval_g(:), eigenval_scf(:), &
3730 bs_env%do_hedin_shift, &
3731 i_mo, bs_env%n_occ(ispin), bs_env%n_vir(ispin), &
3732 bs_env%nparam_pade, bs_env%num_freq_points_fit, &
3733 ri_rpa_g0w0_crossing_newton, bs_env%n_occ(ispin), &
3734 0.0_dp, .true., .false., 1, e_fermi_ext=bs_env%e_fermi(ispin), &
3735 ac_method=gw_ac_nevanlinna, ac_used=ac_used, ac_pick=ac_pick, &
3736 num_fit_points_pade=bs_env%num_freq_points_pade, &
3737 qp_start_eigenval=.true., &
3738 qp_newton_max_step=bs_env%nevanlinna%qp_newton_max_step, &
3739 qp_maxz_radius=bs_env%nevanlinna%qp_maxz_radius, qp_multi=multi_i)
3740 IF (multi_i) multi_count(i_mo) = 1
3741 IF (ac_used == gw_ac_nevanlinna) THEN
3742 n_ac_nevanlinna = n_ac_nevanlinna + 1
3743 ELSE
3744 n_ac_pade = n_ac_pade + 1
3745 END IF
3746 pick_min = min(pick_min, ac_pick)
3747 END DO
3748
3749 CALL bs_env%para_env%sum(sigma_c_ikp_n_qp)
3750 CALL bs_env%para_env%sum(n_ac_nevanlinna)
3751 CALL bs_env%para_env%sum(n_ac_pade)
3752 CALL bs_env%para_env%min(pick_min)
3753 CALL bs_env%para_env%sum(multi_count)
3754 multi(:) = (multi_count(:) > 0)
3755
3756 CALL qp_median_over_windows(bs_env, sigma_c_ikp_n_freq, sigma_x_ikp_n, v_xc_ikp_n, &
3757 eigenval_g, eigenval_scf, ispin, sigma_c_ikp_n_qp, unstable, &
3758 multi, n_window)
3759 CALL propagate_unstable_marks(unstable, multi, bs_env%n_occ(ispin))
3760 CALL print_qp_marks(bs_env, unstable, multi, n_window, ikp, ispin)
3761
3762 ELSE
3763
3764 DO i_mo = 1, n_mo
3765
3766 ! parallelization
3767 IF (modulo(i_mo, bs_env%para_env%num_pe) /= bs_env%para_env%mepos) cycle
3768
3769 DO i_order = 1, n_orders
3770 CALL continuation_pade(sigma_c_orders(:, i_order), &
3771 bs_env%imag_freq_points_fit, z_value, m_value, &
3772 sigma_c_ikp_n_freq(:, 1:bs_env%num_freq_points_fit), &
3773 sigma_x_ikp_n(:) - v_xc_ikp_n(:), &
3774 eigenval_g(:), eigenval_scf(:), &
3775 bs_env%do_hedin_shift, &
3776 i_mo, bs_env%n_occ(ispin), bs_env%n_vir(ispin), &
3777 bs_env%nparam_pade - i_order + 1, bs_env%num_freq_points_fit, &
3778 ri_rpa_g0w0_crossing_newton, bs_env%n_occ(ispin), &
3779 0.0_dp, .true., .false., 1, e_fermi_ext=bs_env%e_fermi(ispin))
3780 END DO
3781 END DO
3782
3783 CALL bs_env%para_env%sum(sigma_c_orders)
3784
3785 DO i_mo = 1, n_mo
3786 sigma_c_sorted(1:n_orders) = sigma_c_orders(i_mo, 1:n_orders)
3787 CALL sort(sigma_c_sorted(1:n_orders), n_orders, order_index(1:n_orders))
3788 sigma_c_ikp_n_qp(i_mo) = sum(sigma_c_sorted(i_central:i_central + n_central - 1))/ &
3789 REAL(n_central, kind=dp)
3790 END DO
3791
3792 END IF
3793
3794 CALL correct_obvious_fitting_fails(sigma_c_ikp_n_qp, ispin, bs_env)
3795
3796 bs_env%eigenval_GW(:, ikp, ispin) = eigenval_scf(:) + &
3797 sigma_c_ikp_n_qp(:) + &
3798 sigma_x_ikp_n(:) - &
3799 v_xc_ikp_n(:)
3800
3801 IF (ALLOCATED(bs_env%eigenval_G0W0) .AND. icycle == 1) THEN
3802 bs_env%eigenval_G0W0(:, ikp, ispin) = bs_env%eigenval_GW(:, ikp, ispin)
3803 END IF
3804
3805 bs_env%eigenval_HF(:, ikp, ispin) = eigenval_scf(:) + sigma_x_ikp_n(:) - v_xc_ikp_n(:)
3806
3807 ! only print eigenvalues of DOS k-points in case no bandstructure path has been given
3808 print_dos_kpoints = (bs_env%nkp_only_bs <= 0)
3809 ! in kpoints_DOS, the last nkp_only_bs are bandstructure k-points
3810 is_bandstruc_kpoint = (ikp > bs_env%nkp_only_DOS)
3811 print_ikp = print_dos_kpoints .OR. is_bandstruc_kpoint
3812
3813 IF (bs_env%para_env%is_source() .AND. print_ikp) THEN
3814
3815 IF (print_dos_kpoints) THEN
3816 nkp = bs_env%nkp_only_DOS
3817 ikp_for_print = ikp
3818 ELSE
3819 nkp = bs_env%nkp_only_bs
3820 ikp_for_print = ikp - bs_env%nkp_only_DOS
3821 END IF
3822
3823 fname = "bandstructure_SCF_and_G0W0"
3824
3825 ! in an evGW0 run the spectrum of every cycle is appended, so only the very first
3826 ! cycle replaces the file
3827 IF (ikp_for_print == 1 .AND. ispin == 1 .AND. icycle == 1) THEN
3828 CALL open_file(trim(fname), unit_number=iunit, file_status="REPLACE", &
3829 file_action="WRITE")
3830 ELSE
3831 CALL open_file(trim(fname), unit_number=iunit, file_status="OLD", &
3832 file_action="WRITE", file_position="APPEND")
3833 END IF
3834
3835 IF (bs_env%gw_flavour == evgw0 .AND. ikp_for_print == 1 .AND. ispin == 1) THEN
3836 WRITE (iunit, "(A)") " "
3837 WRITE (iunit, "(A,I0)") "evGW0 cycle: ", icycle
3838 IF (icycle == 1) THEN
3839 WRITE (iunit, '(A)') '# G0W0 spectrum (first cycle, without SOC)'
3840 END IF
3841 END IF
3842
3843 WRITE (iunit, "(A)") " "
3844 WRITE (iunit, "(A10,I7,A25,3F10.4,T90,A7,I2)") "kpoint: ", ikp_for_print, "coordinate: ", &
3845 bs_env%kpoints_DOS%xkp(:, ikp), "spin: ", ispin
3846 WRITE (iunit, "(A)") " "
3847 IF (do_nevanlinna) THEN
3848 WRITE (iunit, "(A,I6,A,I6,A,ES10.2)") " analytic continuation: Nevanlinna for", &
3849 n_ac_nevanlinna, " level(s), Padé fallback for", n_ac_pade, &
3850 " level(s); smallest relative Pick eigenvalue", pick_min
3851 WRITE (iunit, "(A)") " "
3852 END IF
3853 gw_label = "ϵ_nk^"//trim(gw_flavour_label(bs_env))//" (eV)"
3854 IF (bs_env%gw_flavour == evgw0 .AND. icycle == 1) THEN
3855 gw_label = "ϵ_nk^G0W0 (eV)"
3856 END IF
3857 WRITE (iunit, "(A5,A12,3A17,A16,A18)") "n", "k", "ϵ_nk^DFT (eV)", "Σ^c_nk (eV)", &
3858 "Σ^x_nk (eV)", "v_nk^xc (eV)", trim(gw_label)
3859 WRITE (iunit, "(A)") " "
3860
3861 DO i_mo = 1, n_mo
3862 IF (i_mo <= bs_env%n_occ(ispin)) occ_vir = 'occ'
3863 IF (i_mo > bs_env%n_occ(ispin)) occ_vir = 'vir'
3864 WRITE (iunit, "(I5,3A,I5,4F16.3,F17.3,A)") i_mo, ' (', occ_vir, ') ', ikp_for_print, &
3865 eigenval_scf(i_mo)*evolt, &
3866 sigma_c_ikp_n_qp(i_mo)*evolt, &
3867 sigma_x_ikp_n(i_mo)*evolt, &
3868 v_xc_ikp_n(i_mo)*evolt, &
3869 bs_env%eigenval_GW(i_mo, ikp, ispin)*evolt, &
3870 trim(merge(' *', ' ', unstable(i_mo)))//trim(merge(' m', ' ', multi(i_mo)))
3871 END DO
3872
3873 WRITE (iunit, "(A)") " "
3874 IF (any(unstable)) WRITE (iunit, "(A)") &
3875 " * not reliable: the fit windows disagree, or an unstable or multi-solution level "// &
3876 "lies between this level and the HOMO (occupied) or the LUMO (virtual)"
3877 IF (any(multi)) WRITE (iunit, "(A)") &
3878 " m multi-solution level: a second quasiparticle solution carries more than a quarter "// &
3879 "of the weight"
3880
3881 CALL close_file(iunit)
3882
3883 END IF
3884
3885 CALL timestop(handle)
3886
3887 END SUBROUTINE analyt_conti_and_print
3888
3889! **************************************************************************************************
3890!> \brief quasiparticle energies as median over Nevanlinna continuations from fit windows; each
3891!> window consists of the lowest k imaginary frequencies, k >= window_min_points, with the
3892!> highest frequency in [window_freq_min, window_freq_max] (bs_env%nevanlinna)
3893!> \param bs_env ...
3894!> \param Sigma_c_ikp_n_freq Sigma^c(iω) of all levels
3895!> \param Sigma_x_ikp_n ...
3896!> \param V_xc_ikp_n ...
3897!> \param eigenval_G eigenvalues of the Green's function that produced Σ^c
3898!> \param eigenval_scf ...
3899!> \param ispin ...
3900!> \param Sigma_c_ikp_n_qp Sigma^c at the quasiparticle energy; replaced by the window median
3901!> \param unstable the window solutions of the level spread more than window_spread
3902!> \param multi more than half of the windows find a second solution of comparable weight
3903!> \param n_window number of fit windows; zero if no window exists, then nothing is changed
3904! **************************************************************************************************
3905 SUBROUTINE qp_median_over_windows(bs_env, Sigma_c_ikp_n_freq, Sigma_x_ikp_n, V_xc_ikp_n, &
3906 eigenval_G, eigenval_scf, ispin, Sigma_c_ikp_n_qp, unstable, multi, &
3907 n_window)
3908
3909 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
3910 COMPLEX(KIND=dp), DIMENSION(:, :), INTENT(IN) :: sigma_c_ikp_n_freq
3911 REAL(kind=dp), DIMENSION(:) :: sigma_x_ikp_n, v_xc_ikp_n, eigenval_g, &
3912 eigenval_scf
3913 INTEGER :: ispin
3914 REAL(kind=dp), DIMENSION(:) :: sigma_c_ikp_n_qp
3915 LOGICAL, DIMENSION(:) :: unstable, multi
3916 INTEGER, INTENT(OUT) :: n_window
3917
3918 CHARACTER(LEN=*), PARAMETER :: routinen = 'qp_median_over_windows'
3919 REAL(kind=dp), PARAMETER :: max_abs_sigma = 1.0e3_dp
3920
3921 COMPLEX(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: sig_sel
3922 INTEGER :: handle, i_mo, i_win, j, k_w, n_mo, &
3923 n_valid, n_w
3924 INTEGER, ALLOCATABLE, DIMENSION(:) :: multi_count, order, window_size
3925 LOGICAL :: multi_i
3926 REAL(kind=dp) :: v
3927 REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: m_dummy, q_sorted, sigma_c_win, w_sel, &
3928 z_dummy
3929 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :) :: q_win
3930
3931 CALL timeset(routinen, handle)
3932
3933 n_mo = bs_env%n_ao
3934 n_w = bs_env%num_time_freq_points
3935
3936 ! imaginary frequencies in ascending order
3937 ALLOCATE (order(n_w), window_size(n_w))
3938 DO j = 1, n_w
3939 order(j) = j
3940 END DO
3941 DO j = 2, n_w
3942 k_w = order(j)
3943 i_win = j - 1
3944 DO WHILE (i_win >= 1)
3945 IF (bs_env%time_frequency_grid%frequency(order(i_win)) <= bs_env%time_frequency_grid%frequency(k_w)) EXIT
3946 order(i_win + 1) = order(i_win)
3947 i_win = i_win - 1
3948 END DO
3949 order(i_win + 1) = k_w
3950 END DO
3951
3952 n_window = 0
3953 DO j = bs_env%nevanlinna%window_min_points, n_w
3954 v = bs_env%time_frequency_grid%frequency(order(j))
3955 IF (v >= bs_env%nevanlinna%window_freq_min .AND. v <= bs_env%nevanlinna%window_freq_max) THEN
3956 n_window = n_window + 1
3957 window_size(n_window) = j
3958 END IF
3959 END DO
3960
3961 ! without at least two fit windows there is no error estimate: all levels are marked
3962 IF (n_window < 2) THEN
3963 CALL cp_warn(__location__, "Fewer than two fit windows for the Nevanlinna continuation, "// &
3964 "so no error estimate; all GW levels are marked as not reliable. "// &
3965 "Increase NUM_TIME_FREQ_POINTS.")
3966 unstable(:) = .true.
3967 END IF
3968 IF (n_window == 0) THEN
3969 DEALLOCATE (order, window_size)
3970 CALL timestop(handle)
3971 RETURN
3972 END IF
3973
3974 ALLOCATE (q_win(n_mo, n_window), sigma_c_win(n_mo), q_sorted(n_window), &
3975 multi_count(n_mo), z_dummy(n_mo), m_dummy(n_mo))
3976 q_win(:, :) = 0.0_dp
3977 multi_count(:) = 0
3978
3979 DO i_win = 1, n_window
3980 k_w = window_size(i_win)
3981 ALLOCATE (w_sel(k_w), sig_sel(n_mo, k_w))
3982 DO j = 1, k_w
3983 w_sel(j) = bs_env%time_frequency_grid%frequency(order(j))
3984 sig_sel(:, j) = sigma_c_ikp_n_freq(:, order(j))
3985 END DO
3986 sigma_c_win(:) = 0.0_dp
3987 DO i_mo = 1, n_mo
3988 IF (modulo(i_mo, bs_env%para_env%num_pe) /= bs_env%para_env%mepos) cycle
3989 CALL continuation_pade(sigma_c_win, w_sel, z_dummy, m_dummy, sig_sel, &
3990 sigma_x_ikp_n(:) - v_xc_ikp_n(:), &
3991 eigenval_g(:), eigenval_scf(:), &
3992 bs_env%do_hedin_shift, &
3993 i_mo, bs_env%n_occ(ispin), bs_env%n_vir(ispin), &
3994 k_w, k_w, &
3995 ri_rpa_g0w0_crossing_newton, bs_env%n_occ(ispin), &
3996 0.0_dp, .true., .false., 1, e_fermi_ext=bs_env%e_fermi(ispin), &
3997 ac_method=gw_ac_nevanlinna, qp_start_eigenval=.true., &
3998 qp_newton_max_step=bs_env%nevanlinna%qp_newton_max_step, &
3999 qp_maxz_radius=bs_env%nevanlinna%qp_maxz_radius, qp_multi=multi_i)
4000 q_win(i_mo, i_win) = sigma_c_win(i_mo)
4001 IF (multi_i) multi_count(i_mo) = multi_count(i_mo) + 1
4002 END DO
4003 DEALLOCATE (w_sel, sig_sel)
4004 END DO
4005
4006 CALL bs_env%para_env%sum(q_win)
4007 CALL bs_env%para_env%sum(multi_count)
4008 multi(:) = (2*multi_count(:) > n_window)
4009
4010 DO i_mo = 1, n_mo
4011 ! sort the valid window solutions
4012 n_valid = 0
4013 DO i_win = 1, n_window
4014 v = q_win(i_mo, i_win)
4015 IF (.NOT. (v == v) .OR. abs(v) > max_abs_sigma) cycle
4016 n_valid = n_valid + 1
4017 j = n_valid
4018 DO WHILE (j > 1)
4019 IF (q_sorted(j - 1) <= v) EXIT
4020 q_sorted(j) = q_sorted(j - 1)
4021 j = j - 1
4022 END DO
4023 q_sorted(j) = v
4024 END DO
4025 ! a level without two valid window solutions has no error estimate
4026 IF (n_valid < 2) unstable(i_mo) = .true.
4027 IF (n_valid == 0) cycle
4028 IF (modulo(n_valid, 2) == 1) THEN
4029 sigma_c_ikp_n_qp(i_mo) = q_sorted((n_valid + 1)/2)
4030 ELSE
4031 sigma_c_ikp_n_qp(i_mo) = 0.5_dp*(q_sorted(n_valid/2) + q_sorted(n_valid/2 + 1))
4032 END IF
4033 IF (n_valid > 1) unstable(i_mo) = (q_sorted(n_valid) - q_sorted(1) > bs_env%nevanlinna%window_spread)
4034 END DO
4035
4036 DEALLOCATE (order, window_size, q_win, sigma_c_win, q_sorted, multi_count, z_dummy, m_dummy)
4037
4038 CALL timestop(handle)
4039
4040 END SUBROUTINE qp_median_over_windows
4041
4042! **************************************************************************************************
4043!> \brief counted from the HOMO downwards and from the LUMO upwards, all levels beyond the first
4044!> unstable or multi-solution level are marked unstable: the quasiparticle solution of a
4045!> deeper level cannot be trusted once a closer level is not reliable
4046!> \param unstable ...
4047!> \param multi ...
4048!> \param homo ...
4049! **************************************************************************************************
4050 SUBROUTINE propagate_unstable_marks(unstable, multi, homo)
4051
4052 LOGICAL, DIMENSION(:), INTENT(INOUT) :: unstable
4053 LOGICAL, DIMENSION(:), INTENT(IN) :: multi
4054 INTEGER, INTENT(IN) :: homo
4055
4056 INTEGER :: i_mo
4057 LOGICAL :: beyond_mark
4058
4059 beyond_mark = .false.
4060 DO i_mo = homo, 1, -1
4061 IF (beyond_mark) unstable(i_mo) = .true.
4062 IF (unstable(i_mo) .OR. multi(i_mo)) beyond_mark = .true.
4063 END DO
4064
4065 beyond_mark = .false.
4066 DO i_mo = homo + 1, SIZE(unstable)
4067 IF (beyond_mark) unstable(i_mo) = .true.
4068 IF (unstable(i_mo) .OR. multi(i_mo)) beyond_mark = .true.
4069 END DO
4070
4071 END SUBROUTINE propagate_unstable_marks
4072
4073! **************************************************************************************************
4074!> \brief print the number of marked levels and warn if the HOMO or the LUMO is marked
4075!> \param bs_env ...
4076!> \param unstable ...
4077!> \param multi ...
4078!> \param n_window ...
4079!> \param ikp ...
4080!> \param ispin ...
4081! **************************************************************************************************
4082 SUBROUTINE print_qp_marks(bs_env, unstable, multi, n_window, ikp, ispin)
4083
4084 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
4085 LOGICAL, DIMENSION(:), INTENT(IN) :: unstable, multi
4086 INTEGER, INTENT(IN) :: n_window, ikp, ispin
4087
4088 INTEGER :: homo, u
4089
4090 homo = bs_env%n_occ(ispin)
4091 u = bs_env%unit_nr
4092
4093 IF (u > 0) THEN
4094 WRITE (u, '(T2,A,I6,A,I2,A,I4,A)') 'GW analytic continuation (k-point', ikp, ', spin', &
4095 ispin, '): Nevanlinna, median over', n_window, ' fit windows'
4096 WRITE (u, '(T2,A,I49)') 'Levels marked * (not reliable)', count(unstable)
4097 WRITE (u, '(T2,A,I47)') 'Levels marked m (multi-solution)', count(multi)
4098 WRITE (u, '(T2,A)') ' '
4099 END IF
4100
4101 IF (homo >= 1) THEN
4102 IF (unstable(homo)) THEN
4103 cpwarn("Analytic continuation unstable for the HOMO; its G0W0 energy is not reliable.")
4104 END IF
4105 IF (multi(homo)) THEN
4106 cpwarn("The HOMO has a second quasiparticle solution of comparable weight.")
4107 END IF
4108 END IF
4109 IF (homo < SIZE(unstable)) THEN
4110 IF (unstable(homo + 1)) THEN
4111 cpwarn("Analytic continuation unstable for the LUMO; its G0W0 energy is not reliable.")
4112 END IF
4113 IF (multi(homo + 1)) THEN
4114 cpwarn("The LUMO has a second quasiparticle solution of comparable weight.")
4115 END IF
4116 END IF
4117
4118 END SUBROUTINE print_qp_marks
4119
4120! **************************************************************************************************
4121!> \brief ...
4122!> \param Sigma_c_ikp_n_qp ...
4123!> \param ispin ...
4124!> \param bs_env ...
4125! **************************************************************************************************
4126 SUBROUTINE correct_obvious_fitting_fails(Sigma_c_ikp_n_qp, ispin, bs_env)
4127 REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: sigma_c_ikp_n_qp
4128 INTEGER :: ispin
4129 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
4130
4131 CHARACTER(LEN=*), PARAMETER :: routinen = 'correct_obvious_fitting_fails'
4132
4133 INTEGER :: handle, homo, i_mo, j_mo, &
4134 n_levels_scissor, n_mo
4135 LOGICAL :: is_occ, is_vir
4136 REAL(kind=dp) :: sum_sigma_c
4137
4138 CALL timeset(routinen, handle)
4139
4140 n_mo = bs_env%n_ao
4141 homo = bs_env%n_occ(ispin)
4142
4143 DO i_mo = 1, n_mo
4144
4145 ! if |𝚺^c| > 13 eV, we use a scissors shift
4146 IF (abs(sigma_c_ikp_n_qp(i_mo)) > 13.0_dp/evolt) THEN
4147
4148 is_occ = (i_mo <= homo)
4149 is_vir = (i_mo > homo)
4150
4151 n_levels_scissor = 0
4152 sum_sigma_c = 0.0_dp
4153
4154 ! compute scissor
4155 DO j_mo = 1, n_mo
4156
4157 ! only compute scissor from other GW levels close in energy
4158 IF (is_occ .AND. j_mo > homo) cycle
4159 IF (is_vir .AND. j_mo <= homo) cycle
4160 IF (abs(i_mo - j_mo) > 10) cycle
4161 IF (i_mo == j_mo) cycle
4162
4163 n_levels_scissor = n_levels_scissor + 1
4164 sum_sigma_c = sum_sigma_c + sigma_c_ikp_n_qp(j_mo)
4165
4166 END DO
4167
4168 ! overwrite the self-energy with scissor shift
4169 sigma_c_ikp_n_qp(i_mo) = sum_sigma_c/real(n_levels_scissor, kind=dp)
4170
4171 END IF
4172
4173 END DO ! i_mo
4174
4175 CALL timestop(handle)
4176
4177 END SUBROUTINE correct_obvious_fitting_fails
4178
4179! **************************************************************************************************
4180!> \brief Build the real Gamma occupied or virtual one-particle propagator.
4181!> \param bs_env MO coefficients, eigenvalues and reusable work matrix
4182!> \param tau imaginary time
4183!> \param fm_G_Gamma output AO propagator
4184!> \param ispin spin channel
4185!> \param occ select the occupied sector
4186!> \param vir select the virtual sector
4187! **************************************************************************************************
4188 SUBROUTINE g_occ_vir(bs_env, tau, fm_G_Gamma, ispin, occ, vir)
4189 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
4190 REAL(kind=dp) :: tau
4191 TYPE(cp_fm_type) :: fm_g_gamma
4192 INTEGER :: ispin
4193 LOGICAL :: occ, vir
4194
4195 CHARACTER(LEN=*), PARAMETER :: routinen = 'G_occ_vir'
4196
4197 INTEGER :: first_mo, handle, homo, i_mo, last_mo
4198 REAL(kind=dp) :: tau_e
4199 REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: column_factors
4200
4201 CALL timeset(routinen, handle)
4202
4203 cpassert(occ .NEQV. vir)
4204
4205 homo = bs_env%n_occ(ispin)
4206 first_mo = 1
4207 ! Canonical orthogonalization removes the modes above n_mo_retained.
4208 last_mo = min(bs_env%n_ao, bs_env%n_mo_retained)
4209 IF (occ) THEN
4210 last_mo = min(last_mo, homo)
4211 ELSE
4212 first_mo = homo + 1
4213 END IF
4214 ALLOCATE (column_factors(first_mo:last_mo))
4215 DO i_mo = first_mo, last_mo
4216 tau_e = abs(tau*0.5_dp*(bs_env%eigenval_scf_Gamma(i_mo, ispin) - bs_env%e_fermi(ispin)))
4217 IF (tau_e < bs_env%stabilize_exp) THEN
4218 column_factors(i_mo) = exp(-tau_e)
4219 ELSE
4220 column_factors(i_mo) = 0.0_dp
4221 END IF
4222 END DO
4223
4224 CALL build_ao_propagator(bs_env%fm_mo_coeff_Gamma(ispin), column_factors, first_mo, &
4225 bs_env%fm_work_mo(1), fm_g_gamma)
4226 DEALLOCATE (column_factors)
4227
4228 CALL timestop(handle)
4229
4230 END SUBROUTINE g_occ_vir
4231
4232END MODULE gw_utils
static GRID_HOST_DEVICE int modulo(int a, int m)
Equivalent of Fortran's MODULO, which always return a positive number. https://gcc....
struct tensor_ tensor
Define the atomic kind types and their sub types.
subroutine, public get_atomic_kind_set(atomic_kind_set, atom_of_kind, kind_of, natom_of_kind, maxatom, natom, nshell, fist_potential_present, shell_present, shell_adiabatic, shell_check_distance, damping_present)
Get attributes of an atomic kind set.
Automatic generation of auxiliary basis sets of different kind.
Definition auto_basis.F:15
subroutine, public create_ri_aux_basis_set(ri_aux_basis_set, qs_kind, basis_cntrl, basis_type, basis_sort)
Create a RI_AUX basis set using some heuristics.
Definition auto_basis.F:57
subroutine, public add_basis_set_to_container(container, basis_set, basis_set_type)
...
subroutine, public get_gto_basis_set(gto_basis_set, name, aliases, norm_type, kind_radius, ncgf, nset, nsgf, cgf_symbol, sgf_symbol, norm_cgf, set_radius, lmax, lmin, lx, ly, lz, m, ncgf_set, npgf, nsgf_set, nshell, cphi, pgf_radius, sphi, scon, zet, first_cgf, first_sgf, l, last_cgf, last_sgf, n, gcc, maxco, maxl, maxpgf, maxsgf_set, maxshell, maxso, nco_sum, npgf_sum, nshell_sum, maxder, short_kind_radius, npgf_seg_sum, ccon)
...
subroutine, public init_orb_basis_set(gto_basis_set)
Initialise a Gaussian-type orbital (GTO) basis set data set.
collects all references to literature in CP2K as new algorithms / method are included from literature...
integer, save, public graml2024
Handles all functions related to the CELL.
Definition cell_types.F:15
subroutine, public scaled_to_real(r, s, cell)
Transform scaled cell coordinates real coordinates. r=h*s.
Definition cell_types.F:661
methods related to the blacs parallel environment
subroutine, public cp_blacs_env_release(blacs_env)
releases the given blacs_env
subroutine, public cp_blacs_env_create(blacs_env, para_env, blacs_grid_layout, blacs_repeatable, row_major, grid_2d)
allocates and initializes a type that represent a blacs context
Represents a complex full matrix distributed on many processors.
subroutine, public cp_cfm_copy_general(source, destination, para_env)
General copy of a cfm matrix to another cfm matrix. Uses non-blocking MPI rather than ScaLAPACK.
subroutine, public cp_cfm_release(matrix)
Releases a full matrix.
subroutine, public cp_cfm_create(matrix, matrix_struct, name, nrow, ncol, set_zero)
Creates a new full matrix with the given structure.
subroutine, public cp_cfm_to_fm(msource, mtargetr, mtargeti)
Copy real and imaginary parts of a complex full matrix into separate real-value full matrices.
Defines control structures, which contain the parameters and the settings for the DFT-based calculati...
subroutine, public dbcsr_set(matrix, alpha)
...
DBCSR operations in CP2K.
subroutine, public copy_dbcsr_to_fm(matrix, fm, plan, check_plan)
Copy a DBCSR matrix to a BLACS matrix.
subroutine, public copy_fm_to_dbcsr(fm, matrix, keep_sparsity)
Copy a BLACS matrix to a dbcsr matrix.
Utility routines to open and close files.
Definition cp_files.F:22
subroutine, public open_file(file_name, unit_number, file_status, file_form, file_action, file_position, file_pad, file_access, debug)
Opens a file and returns its logical unit number.
Definition cp_files.F:197
subroutine, public close_file(unit_number, file_status)
Closes an open file given by its logical unit number.
Definition cp_files.F:64
Basic linear algebra operations for full matrices.
subroutine, public cp_fm_scale_and_add(alpha, matrix_a, beta, matrix_b)
calc A <- alpha*A + beta*B optimized for alpha == 1.0 (just add beta*B) and beta == 0....
represent the structure of a full matrix
subroutine, public cp_fm_struct_create(fmstruct, para_env, context, nrow_global, ncol_global, nrow_block, ncol_block, descriptor, first_p_pos, local_leading_dimension, template_fmstruct, square_blocks, force_block)
allocates and initializes a full matrix structure
subroutine, public cp_fm_struct_release(fmstruct)
releases a full matrix structure
represent a full matrix distributed on many processors
Definition cp_fm_types.F:15
subroutine, public cp_fm_get_diag(matrix, diag, n_active)
returns the diagonal elements of a fm
subroutine, public cp_fm_set_all(matrix, alpha, beta)
set all elements of a matrix to the same value, and optionally the diagonal to a different one
subroutine, public cp_fm_create(matrix, matrix_struct, name, nrow, ncol, set_zero)
creates a new full matrix with the given structure
various routines to log and control the output. The idea is that decisions about where to log should ...
type(cp_logger_type) function, pointer, public cp_get_default_logger()
returns the default logger
routines to handle the output, The idea is to remove the decision of wheter to output and what to out...
character(len=default_path_length) function, public cp_print_key_generate_filename(logger, print_key, middle_name, extension, my_local)
Utility function that returns a unit number to write the print key. Might open a file with a unique f...
This is the start of a dbt_api, all publically needed functions are exported here....
Definition dbt_api.F:17
Automatic RI basis set optimization for molecular and periodic GW.
subroutine, public generate_auto_ri_basis(qs_env, bs_env)
Selects molecular or periodic atom-local AA RI-basis optimization.
Input and persistent data for automatic RI basis optimization.
Coulomb matrices and screened interactions for imaginary-time GW.
subroutine, public get_v_tr_r(v_tr_r, pot_type, regularization_ri, bs_env, qs_env)
...
Gamma-point (q = 0) correction of W and Σ for periodic RI-RS GW.
subroutine, public prepare_ri_rs_gamma_correction(qs_env, bs_env)
Preserve distributed AO position, Hamiltonian and overlap images before QS release....
Utility method to build 3-center integrals for small cell GW.
subroutine, public build_3c_integral_block(int_3c, qs_env, potential_parameter, basis_j, basis_k, basis_i, cell_j, cell_k, cell_i, atom_j, atom_k, atom_i, j_bf_start_from_atom, k_bf_start_from_atom, i_bf_start_from_atom)
...
Full-matrix operations not provided by the CP2K FM packages.
Definition gw_utils_fm.F:13
subroutine, public fm_invert(matrix_a, eigenvalue_threshold, unit_nr)
Inverts a symmetric matrix. First, Cholesky decomposition is tried. If it fails, the matrix is diagon...
subroutine, public cfm_contract_aba(matrix_a, matrix_b, matrix_c, workspace)
Computes A^H B A for complex full matrices.
Definition gw_utils_fm.F:59
subroutine, public local_complex_power(matrix, exponent, eps, cond_nr, min_ev, max_ev, relative_eps)
Compute a spectral power of a local complex Hermitian matrix. Eigenvalues not larger than eps (or rel...
subroutine, public rtbse_resolve_rirs_flag(qs_env, bs_env, rirs_kernel)
Resolve the linRTBSE RI-RS kernel switch from the KERNEL_RI input and the GW default.
Definition gw_utils.F:3605
subroutine, public compute_xkp(xkp, ikp_start, ikp_end, grid)
...
Definition gw_utils.F:1245
subroutine, public create_and_init_bs_env_for_gw(qs_env, bs_env, bs_sec)
Initializes the GW environment from the input and electronic-structure data.
Definition gw_utils.F:167
subroutine, public add_r(cell_1, cell_2, index_to_cell, cell_1_plus_2, cell_found, cell_to_index, i_cell_1_plus_2)
...
Definition gw_utils.F:3071
subroutine, public de_init_bs_env(qs_env, bs_env)
Releases the memory-heavy GW intermediates that cannot be freed in bs_env_release,...
Definition gw_utils.F:3562
subroutine, public analyt_conti_and_print(bs_env, sigma_c_ikp_n_freq, sigma_x_ikp_n, v_xc_ikp_n, eigenval_scf, ikp, ispin, i_iter)
...
Definition gw_utils.F:3653
subroutine, public is_cell_in_index_to_cell(cell, index_to_cell, cell_found)
...
Definition gw_utils.F:3110
subroutine, public g_occ_vir(bs_env, tau, fm_g_gamma, ispin, occ, vir)
Build the real Gamma occupied or virtual one-particle propagator.
Definition gw_utils.F:4189
collects all constants needed in input so that they can be used without circular dependencies
integer, parameter, public rtp_method_bse_linearized
integer, parameter, public tensor_small_cell_full_kp
integer, parameter, public ri_rs_small_cell_full_kp
integer, parameter, public ri_rs_non_periodic
integer, parameter, public cohsex_qp_update
integer, parameter, public rtp_bse_kernel_ri_rs
integer, parameter, public ri_rs_large_cell_gamma
integer, parameter, public tensor_large_cell_gamma
integer, parameter, public rtp_bse_kernel_ri_ao
integer, parameter, public do_potential_truncated
integer, parameter, public rtp_method_bse
integer, parameter, public g0w0
integer, parameter, public evgw0
integer, parameter, public do_potential_coulomb
integer, parameter, public tensor_static_cohsex
integer, parameter, public do_potential_short
integer, parameter, public gw_ac_nevanlinna
integer, parameter, public xc_none
integer, parameter, public ri_rpa_g0w0_crossing_newton
objects that represent the structure of input sections and the data contained in an input section
subroutine, public section_vals_val_set(section_vals, keyword_name, i_rep_section, i_rep_val, val, l_val, i_val, r_val, c_val, l_vals_ptr, i_vals_ptr, r_vals_ptr, c_vals_ptr)
sets the requested value
recursive type(section_vals_type) function, pointer, public section_vals_get_subs_vals(section_vals, subsection_name, i_rep_section, can_return_null)
returns the values of the requested subsection
subroutine, public section_vals_get(section_vals, ref_count, n_repetition, n_subs_vals_rep, section, explicit)
returns various attributes about the section_vals
subroutine, public section_vals_val_get(section_vals, keyword_name, i_rep_section, i_rep_val, n_rep_val, val, l_val, i_val, r_val, c_val, l_vals, i_vals, r_vals, c_vals, explicit)
returns the requested value
Defines the basic variable types.
Definition kinds.F:23
integer, parameter, public int_8
Definition kinds.F:54
integer, parameter, public dp
Definition kinds.F:34
integer, parameter, public default_string_length
Definition kinds.F:57
integer, parameter, public default_path_length
Definition kinds.F:58
Implements simple k-space/R-space transformations and Gamma-point MIC reconstruction.
subroutine, public rs_to_kp(rs_real, ks_complex, index_to_cell, xkp, deriv_direction, hmat)
Integrate RS matrices (stored as Fortran array) into a kpoint matrix at given kp.
Types and basic routines needed for a kpoint calculation.
subroutine, public kpoint_release(kpoint)
Release a kpoint environment, deallocate all data.
subroutine, public kpoint_create(kpoint)
Create a kpoint environment.
Machine interface based on Fortran 2003 and POSIX.
Definition machine.F:17
real(kind=dp) function, public m_walltime()
returns time from a real-time clock, protected against rolling early/easily
Definition machine.F:144
Collection of simple mathematical functions and subroutines.
Definition mathlib.F:15
elemental integer function, public gcd(a, b)
computes the greatest common divisor of two number
Definition mathlib.F:1291
subroutine, public erfc_cutoff(eps, omg, r_cutoff)
compute a truncation radius for the shortrange operator
Definition mathlib.F:1854
Interface to the message passing library MPI.
subroutine, public mp_mem_used_per_rank_gb(comm, mem_used_gb)
Memory that is currently occupied by this process, in GB, maximized over all ranks of comm,...
subroutine, public mp_mem_avail_per_rank_gb(comm, mem_avail_gb)
Memory that is currently free on the node, per MPI rank of that node, in GB.
Calls routines to get RI integrals and calculate total energies.
Definition mp2_gpw.F:14
subroutine, public create_mat_munu(mat_munu, qs_env, eps_grid, blacs_env_sub, do_ri_aux_basis, do_mixed_basis, group_size_prim, do_alloc_blocks_from_nbl, do_kpoints, sab_orb_sub, dbcsr_sym_type, custom_row_blk_sizes)
Encapsulate the building of dbcsr_matrix mat_munu.
Definition mp2_gpw.F:996
Framework for 2c-integrals for RI.
Definition mp2_ri_2c.F:14
subroutine, public ri_2c_integral_mat(qs_env, fm_matrix_minv_l_gamma, fm_matrix_l_struct, dimen_ri, ri_metric, put_mat_ks_env, regularization_ri)
...
Definition mp2_ri_2c.F:576
subroutine, public trunc_coulomb_for_exchange(qs_env, trunc_coulomb, rel_cutoff_trunc_coulomb_ri_x, cell_grid, do_bvk_cell)
...
Definition mp2_ri_2c.F:1510
Define methods related to particle_type.
subroutine, public get_particle_set(particle_set, qs_kind_set, first_sgf, last_sgf, nsgf, nmao, basis, ncgf)
Get the components of a particle set.
Define the data structure for the particle information.
Definition of physical constants:
Definition physcon.F:68
real(kind=dp), parameter, public evolt
Definition physcon.F:183
real(kind=dp), parameter, public angstrom
Definition physcon.F:144
character(len=default_string_length) function, public gw_flavour_label(bs_env)
Name of the GW flavour that was requested, for printing.
subroutine, public create_operator_kpoints(qs_env, bs_env, kpoints, cfm_kp_group)
Set up the bandstructure and DOS k-points as one k-point group spanning all ranks,...
subroutine, public get_qs_env(qs_env, atomic_kind_set, qs_kind_set, cell, super_cell, cell_ref, use_ref_cell, kpoints, dft_control, mos, sab_orb, sab_all, qmmm, qmmm_periodic, mimic, sac_ae, sac_ppl, sac_lri, sap_ppnl, sab_vdw, sab_scp, sap_oce, sab_lrc, sab_se, sab_xtbe, sab_tbe, sab_core, sab_xb, sab_xtb_pp, sab_xtb_nonbond, sab_almo, sab_kp, sab_kp_nosym, sab_cneo, particle_set, energy, force, matrix_h, matrix_h_im, matrix_ks, matrix_ks_im, matrix_vxc, run_rtp, rtp, matrix_h_kp, matrix_h_im_kp, matrix_ks_kp, matrix_ks_im_kp, matrix_vxc_kp, kinetic_kp, matrix_s_kp, matrix_w_kp, matrix_s_ri_aux_kp, matrix_s, matrix_s_ri_aux, matrix_w, matrix_p_mp2, matrix_p_mp2_admm, matrix_vhxc, rho, rho_xc, pw_env, ewald_env, ewald_pw, active_space, mpools, input, para_env, blacs_env, scf_control, rel_control, kinetic, qs_charges, vppl, xcint_weights, rho_core, rho_nlcc, rho_nlcc_g, ks_env, ks_qmmm_env, wf_history, scf_env, local_particles, local_molecules, distribution_2d, dbcsr_dist, molecule_kind_set, molecule_set, subsys, cp_subsys, oce, local_rho_set, rho_atom_set, task_list, task_list_soft, rho0_atom_set, rho0_mpole, rhoz_set, rhoz_cneo_set, ecoul_1c, rho0_s_rs, rho0_s_gs, rhoz_cneo_s_rs, rhoz_cneo_s_gs, do_kpoints, has_unit_metric, requires_mo_derivs, mo_derivs, mo_loc_history, nkind, natom, nelectron_total, nelectron_spin, efield, neighbor_list_id, linres_control, xas_env, virial, cp_ddapc_env, cp_ddapc_ewald, outer_scf_history, outer_scf_ihistory, x_data, et_coupling, dftb_potential, results, se_taper, se_store_int_env, se_nddo_mpole, se_nonbond_env, admm_env, lri_env, lri_density, exstate_env, ec_env, harris_env, dispersion_env, gcp_env, vee, rho_external, external_vxc, mask, mp2_env, bs_env, kg_env, wanniercentres, atprop, ls_scf_env, do_transport, transport_env, v_hartree_rspace, s_mstruct_changed, rho_changed, potential_changed, forces_up_to_date, mscfg_env, almo_scf_env, gradient_history, variable_history, embed_pot, spin_embed_pot, polar_env, mos_last_converged, eeq, rhs, do_rixs, tb_tblite)
Get the QUICKSTEP environment.
subroutine, public qs_env_part_release(qs_env)
releases part of the given qs_env in order to save memory
Some utility functions for the calculation of integrals.
subroutine, public basis_set_list_setup(basis_set_list, basis_type, qs_kind_set)
Set up an easy accessible list of the basis sets for all kinds.
Calculate the interaction radii for the operator matrix calculation.
subroutine, public init_interaction_radii_orb_basis(orb_basis_set, eps_pgf_orb, eps_pgf_short)
...
Define the quickstep kind type and their sub types.
subroutine, public get_qs_kind(qs_kind, basis_set, basis_type, ncgf, nsgf, all_potential, tnadd_potential, gth_potential, sgp_potential, upf_potential, cneo_potential, se_parameter, dftb_parameter, xtb_parameter, dftb3_param, zatom, zeff, elec_conf, mao, lmax_dftb, alpha_core_charge, ccore_charge, core_charge, core_charge_radius, paw_proj_set, paw_atom, hard_radius, hard0_radius, max_rad_local, covalent_radius, vdw_radius, gpw_type_forced, harmonics, max_iso_not0, max_s_harm, grid_atom, ngrid_ang, ngrid_rad, lmax_rho0, dft_plus_u_atom, l_of_dft_plus_u, n_of_dft_plus_u, u_minus_j, hund_j, u_of_dft_plus_u, j_of_dft_plus_u, alpha_of_dft_plus_u, beta_of_dft_plus_u, j0_of_dft_plus_u, occupation_of_dft_plus_u, dispersion, bs_occupation, magnetization, no_optimize, addel, laddel, naddel, orbitals, max_scf, eps_scf, smear, u_ramping, u_minus_j_target, eps_u_ramping, proj_shell_charge, lr_atom, do_mtlr, u_j_loop, ao_coef, init_u_ramping_each_scf, reltmat, ghost, monovalent, floating, name, element_symbol, pao_basis_size, pao_model_file, pao_potentials, pao_descriptors, nelec)
Get attributes of an atomic kind.
Assembly of real and complex k-point operators from real-space DBCSR matrices. Outputs use the distri...
subroutine, public kpoint_operator_context_create(ctx, kpoints, ao_ao_fm, nspin)
Create an empty operator context. The context owns no buffers after this call; they are created on fi...
subroutine, public kpoint_operator_prepare(ctx, rsmat, slot, prepared, matrix_row, target)
Prepare an immutable operator slot for group-local assembly. Refresh after the source values change,...
subroutine, public kpoint_operator_get(ctx, ikp, ispin, rsmat, cmat, fm_re, fm_im, mat_re, mat_im, matrix_row, slot, grid)
Assemble one operator for one global round and spin in one call: run the single-group transfer of the...
integer, parameter, public kpoint_slot_ks
subroutine, public kpoint_operator_context_release(ctx)
Release the context together with every buffer it created. Call once, at the end of the fixed-layout ...
routines that build the Kohn-Sham matrix (i.e calculate the coulomb and xc parts
subroutine, public qs_ks_build_kohn_sham_matrix(qs_env, calculate_forces, just_energy, print_active, ext_ks_matrix, ext_xc_section)
routine where the real calculations are made: the KS matrix is calculated
methods of the rho structure (defined in qs_rho_types)
subroutine, public qs_rho_update_rho(rho_struct, qs_env, rho_xc_external, local_rho_set, task_list_external, task_list_external_soft, pw_env_external, para_env_external)
updates rho_r and rho_g to the rhorho_ao. if use_kinetic_energy_density also computes tau_r and tau_g...
superstucture that hold various representations of the density and keeps track of which ones are vali...
Utility methods to build 3-center integral tensors of various types.
subroutine, public distribution_3d_create(dist_3d, dist1, dist2, dist3, nkind, particle_set, mp_comm_3d, own_comm)
Create a 3d distribution.
subroutine, public create_2c_tensor(t2c, dist_1, dist_2, pgrid, sizes_1, sizes_2, order, name)
...
subroutine, public create_3c_tensor(t3c, dist_1, dist_2, dist_3, pgrid, sizes_1, sizes_2, sizes_3, map1, map2, name)
...
Utility methods to build 3-center integral tensors of various types.
Definition qs_tensors.F:11
subroutine, public build_3c_integrals(t3c, filter_eps, qs_env, nl_3c, basis_i, basis_j, basis_k, potential_parameter, int_eps, op_pos, do_kpoints, do_hfx_kpoints, desymmetrize, cell_sym, bounds_i, bounds_j, bounds_k, ri_range, img_to_ri_cell, cell_to_index_ext)
Build 3-center integral tensor.
subroutine, public neighbor_list_3c_destroy(ijk_list)
Destroy 3c neighborlist.
Definition qs_tensors.F:375
subroutine, public get_tensor_occupancy(tensor, nze, occ)
...
subroutine, public build_3c_neighbor_lists(ijk_list, basis_i, basis_j, basis_k, dist_3d, potential_parameter, name, qs_env, sym_ij, sym_jk, sym_ik, molecular, op_pos, own_dist)
Build a 3-center neighbor list.
Definition qs_tensors.F:274
Shared real-space resolution-of-identity grid and matrix data.
Definition ri_rs_types.F:11
Utility routines for RPA and GW with imaginary time.
Routines for GW, continuous development [Jan Wilhelm].
Definition rpa_gw.F:14
subroutine, public continuation_pade(vec_gw_energ, vec_omega_fit_gw, z_value, m_value, vec_sigma_c_gw, vec_sigma_x_minus_vxc_gw, eigenval, eigenval_scf, do_hedin_shift, n_level_gw, gw_corr_lev_occ, gw_corr_lev_vir, nparam_pade, num_fit_points, crossing_search, homo, fermi_level_offset, do_gw_im_time, print_self_energy, count_ev_sc_gw, vec_gw_dos, dos_lower_bound, dos_precision, ndos, min_level_self_energy, max_level_self_energy, dos_eta, dos_min, dos_max, e_fermi_ext, output_spin, output_kpoint, ac_method, ac_used, ac_pick, num_fit_points_pade, qp_start_eigenval, qp_newton_max_step, qp_maxz_radius, qp_multi)
perform analytic continuation with pade approximation
Definition rpa_gw.F:4246
Definition and construction of time/frequency grids for correlation methods.
subroutine, public build_minimax_time_frequency_grid(num_points, energy_min, energy_max, regularization, num_points_per_magnitude, grid, build_frequency, build_time, build_transforms, build_sine, time_scaling, time_weight_scaling, max_fit_error, print_warning, unit_nr, prefer_external_backend, used_external_backend, rcond)
Build a minimax time/frequency grid through the common backend boundary.
All kind of helpful little routines.
Definition util.F:14
Provides all information about an atomic kind.
Type defining parameters related to the simulation cell.
Definition cell_types.F:60
represent a blacs multidimensional parallel environment (for the mpi corrispective see cp_paratypes/m...
Represent a complex full matrix.
keeps the information about the structure of a full matrix
represent a full matrix
type of a logger, at the moment it contains just a print level starting at which level it should be l...
Contains information about kpoints.
stores all the informations relevant to an mpi environment
Provides all information about a quickstep kind.
Caller-owned assembly context for a fixed geometry and parallel layout. OT retains it for one inner S...
keeps the density in various representations, keeping track of which ones are valid.