Line data Source code
1 : !--------------------------------------------------------------------------------------------------!
2 : ! CP2K: A general program to perform molecular dynamics simulations !
3 : ! Copyright 2000-2026 CP2K developers group <https://cp2k.org> !
4 : ! !
5 : ! SPDX-License-Identifier: GPL-2.0-or-later !
6 : !--------------------------------------------------------------------------------------------------!
7 :
8 : ! **************************************************************************************************
9 : !> \brief Routines to calculate HFX energy and potential
10 : !> \par History
11 : !> 11.2006 created [Manuel Guidon]
12 : !> 10.2025 added libGint interface [M Puligheddu]
13 : !> \author Manuel Guidon
14 : ! **************************************************************************************************
15 : MODULE hfx_energy_potential
16 : USE admm_types, ONLY: get_admm_env
17 : USE atomic_kind_types, ONLY: atomic_kind_type, &
18 : get_atomic_kind_set
19 : USE bibliography, ONLY: cite_reference, &
20 : guidon2008, &
21 : guidon2009
22 : USE cell_types, ONLY: cell_type, &
23 : pbc
24 : USE cp_control_types, ONLY: dft_control_type
25 : USE cp_files, ONLY: close_file, &
26 : open_file
27 : USE cp_log_handling, ONLY: cp_get_default_logger, &
28 : cp_logger_type
29 : USE cp_output_handling, ONLY: cp_p_file, &
30 : cp_print_key_finished_output, &
31 : cp_print_key_should_output, &
32 : cp_print_key_unit_nr
33 : USE message_passing, ONLY: mp_para_env_type
34 : USE cp_dbcsr_api, ONLY: dbcsr_copy, &
35 : dbcsr_get_matrix_type, &
36 : dbcsr_p_type, &
37 : dbcsr_type_antisymmetric, &
38 : dbcsr_dot_threadsafe
39 : USE four_center_integral_interface, ONLY: four_center_integral_context_type
40 : USE four_center_integral_methods, ONLY: coulomb4, &
41 : pgf_product_list_size
42 : USE gamma, ONLY: init_md_ftable
43 : USE hfx_communication, ONLY: distribute_ks_matrix, &
44 : get_atomic_block_maps, &
45 : get_full_density
46 : USE hfx_compression_methods, ONLY: hfx_add_mult_cache_elements, &
47 : hfx_add_single_cache_element, &
48 : hfx_decompress_first_cache, &
49 : hfx_flush_last_cache, &
50 : hfx_get_mult_cache_elements, &
51 : hfx_get_single_cache_element, &
52 : hfx_reset_cache_and_container
53 : USE hfx_contract_block, ONLY: contract_block
54 : USE hfx_load_balance_methods, ONLY: collect_load_balance_info, &
55 : hfx_load_balance, &
56 : hfx_update_load_balance
57 : USE hfx_pair_list_methods, ONLY: build_atomic_pair_list, &
58 : build_pair_list
59 : USE hfx_screening_methods, ONLY: calc_pair_dist_radii, &
60 : calc_screening_functions, &
61 : update_pmax_mat
62 : USE hfx_types, ONLY: &
63 : alloc_containers, dealloc_containers, hfx_basis_info_type, hfx_basis_type, hfx_cache_type, &
64 : hfx_cell_type, hfx_container_type, hfx_create_neighbor_cells, hfx_distribution, &
65 : hfx_general_type, hfx_init_container, hfx_load_balance_type, hfx_memory_type, hfx_p_kind, &
66 : hfx_pgf_list, hfx_pgf_product_list, hfx_potential_type, hfx_reset_memory_usage_counter, &
67 : hfx_screen_coeff_type, hfx_screening_type, hfx_task_list_type, hfx_type, init_t_c_g0_lmax, &
68 : log_zero, pair_list_type, pair_set_list_type
69 :
70 : USE input_constants, ONLY: do_potential_mix_cl_trunc, &
71 : do_potential_truncated, &
72 : do_potential_TShPSC, &
73 : do_potential_short, &
74 : do_potential_coulomb, &
75 : hfx_do_eval_energy, &
76 : hfx_library_is_libint, hfx_library_is_libGint, hfx_library_is_both
77 :
78 : USE input_section_types, ONLY: section_vals_type
79 : USE kinds, ONLY: default_string_length, &
80 : dp, &
81 : int_8
82 : USE kpoint_types, ONLY: get_kpoint_info, &
83 : kpoint_type
84 : USE machine, ONLY: m_flush, &
85 : m_memory, &
86 : m_walltime
87 : USE mathconstants, ONLY: fac
88 : USE orbital_pointers, ONLY: nco, &
89 : ncoset, &
90 : nso
91 : USE particle_types, ONLY: particle_type
92 : USE qs_environment_types, ONLY: get_qs_env, &
93 : qs_environment_type
94 : USE qs_ks_types, ONLY: get_ks_env, &
95 : qs_ks_env_type
96 : USE t_c_g0, ONLY: init
97 : USE util, ONLY: sort
98 : use, intrinsic :: iso_c_binding, only: c_ptr
99 :
100 : use cp2k_info, only: cp2k_flags
101 : use offload_api, only: offload_get_chosen_device, offload_set_chosen_device, offload_activate_chosen_device
102 : use libGint_wrapper, only: cp_libGint_init, libGint_update_env, libGint_set_density, libGint_coulomb4, &
103 : libGint_update_fock_matrix, libGint_get_fock_matrix
104 :
105 : !$ USE OMP_LIB, ONLY: omp_get_max_threads, omp_get_thread_num, omp_get_num_threads
106 :
107 : #include "./base/base_uses.f90"
108 :
109 : IMPLICIT NONE
110 : PRIVATE
111 :
112 : PUBLIC :: integrate_four_center
113 :
114 : CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'hfx_energy_potential'
115 :
116 : !***
117 :
118 : CONTAINS
119 :
120 : ! **************************************************************************************************
121 : !> \brief computes four center integrals for a full basis set and updates the
122 : !> Kohn-Sham-Matrix and energy. Uses all 8 eri symmetries
123 : !> \param qs_env ...
124 : !> \param x_data ...
125 : !> \param ks_matrix ...
126 : !> \param ehfx energy calculated with the updated HFX matrix
127 : !> \param rho_ao density matrix in ao basis
128 : !> \param hfx_section input_section HFX
129 : !> \param para_env ...
130 : !> \param geometry_did_change flag that indicates we have to recalc integrals
131 : !> \param irep Index for the HFX replica
132 : !> \param distribute_fock_matrix Flag that indicates whether to communicate the
133 : !> new fock matrix or not
134 : !> \param ispin ...
135 : !> \par History
136 : !> 06.2007 created [Manuel Guidon]
137 : !> 08.2007 optimized load balance [Manuel Guidon]
138 : !> 09.2007 new parallelization [Manuel Guidon]
139 : !> 02.2009 completely rewritten screening part [Manuel Guidon]
140 : !> 12.2017 major bug fix. removed wrong cycle that was caussing segfault.
141 : !> see https://groups.google.com/forum/#!topic/cp2k/pc6B14XOALY
142 : !> [Tobias Binninger + Valery Weber]
143 : !> \author Manuel Guidon
144 : ! **************************************************************************************************
145 43577 : SUBROUTINE integrate_four_center(qs_env, x_data, ks_matrix, ehfx, rho_ao, hfx_section, para_env, &
146 : geometry_did_change, irep, distribute_fock_matrix, &
147 : ispin, nspins)
148 :
149 : TYPE(qs_environment_type), POINTER :: qs_env
150 : TYPE(hfx_type), DIMENSION(:, :), POINTER :: x_data
151 : TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: ks_matrix
152 : REAL(KIND=dp), INTENT(OUT) :: ehfx
153 : TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: rho_ao
154 : TYPE(section_vals_type), POINTER :: hfx_section
155 : TYPE(mp_para_env_type), POINTER :: para_env
156 : LOGICAL :: geometry_did_change
157 : INTEGER :: irep
158 : LOGICAL, INTENT(IN) :: distribute_fock_matrix
159 : INTEGER, INTENT(IN) :: ispin
160 : INTEGER, INTENT(IN), OPTIONAL :: nspins
161 :
162 : CHARACTER(LEN=*), PARAMETER :: routineN = 'integrate_four_center'
163 :
164 : CHARACTER(len=default_string_length) :: eps_scaling_str, eps_schwarz_min_str
165 : INTEGER :: act_atomic_block_offset, act_set_offset, atomic_offset_ac, atomic_offset_ad, &
166 : atomic_offset_bc, atomic_offset_bd, bin, bits_max_val, buffer_left, buffer_size, &
167 : buffer_start, cache_size, current_counter, handle, handle_bin, handle_dist_ks, &
168 : handle_getP, handle_load, handle_main, i, inla, nla, i_list_ij, i_list_kl, i_set_list_ij, &
169 : i_set_list_ij_start, i_set_list_ij_stop, i_set_list_kl, i_set_list_kl_start, &
170 : i_set_list_kl_stop, i_thread, iatom, iatom_block, iatom_end, iatom_start, ikind, img, &
171 : iset, iw, j, jatom, jatom_block, jatom_end, jatom_start, jkind, jset, katom, katom_block, &
172 : katom_end
173 : INTEGER :: katom_start, kind_kind_idx, kkind, kset, l_max, latom, latom_block, latom_end, &
174 : latom_start, lkind, lset, ma, max_am, max_pgf, max_set, mb, my_bin_id, my_bin_size, &
175 : my_thread_id, n_threads, natom, nbits, ncob, ncos_max, nints, nkimages, nkind, &
176 : nneighbors, nseta, nsetb, nsgf_max, my_nspins, pa, sgfb, shm_task_counter, shm_total_bins, &
177 : sphi_a_u1, sphi_a_u2, sphi_a_u3, sphi_b_u1, sphi_b_u2, sphi_b_u3, sphi_c_u1, sphi_c_u2, &
178 : sphi_c_u3, sphi_d_u1, sphi_d_u2, sphi_d_u3, swap_id, tmp_i4, unit_id
179 : INTEGER(int_8) :: atom_block, counter, estimate_to_store_int, max_val_memory, &
180 : mb_size_buffers, mb_size_f, mb_size_p, mem_compression_counter, &
181 : mem_compression_counter_disk, mem_eris, mem_eris_disk, mem_max_val, memsize_after, &
182 : memsize_before, my_current_counter, my_istart, n_processes, nblocks, ncpu, neris_disk, &
183 : neris_incore, neris_onthefly, neris_tmp, neris_total, nprim_ints, &
184 : shm_mem_compression_counter, shm_neris_disk, shm_neris_incore, shm_neris_onthefly, &
185 : shm_neris_total, shm_nprim_ints, shm_stor_count_int_disk, shm_stor_count_max_val, &
186 : shm_storage_counter_integrals, stor_count_int_disk
187 : INTEGER(int_8) :: stor_count_max_val, storage_counter_integrals, subtr_size_mb, tmp_block, &
188 : tmp_i8(8)
189 43577 : INTEGER(int_8), ALLOCATABLE, DIMENSION(:) :: tmp_task_list_cost
190 87154 : INTEGER, ALLOCATABLE, DIMENSION(:) :: kind_of, last_sgf_global, nimages, &
191 87154 : tmp_index, first_set_of_atom
192 43577 : INTEGER, DIMENSION(:), POINTER :: la_max, la_min, lb_max, lb_min, lc_max, lc_min, ld_max, &
193 87154 : ld_min, npgfa, npgfb, npgfc, npgfd, nsgfa, nsgfb, nsgfc, nsgfd, shm_block_offset
194 87154 : INTEGER, DIMENSION(:, :), POINTER :: first_sgfb, nsgfl_a, nsgfl_b, nsgfl_c, nsgfl_d, &
195 43577 : offset_ac_set, offset_ad_set, offset_bc_set, offset_bd_set, shm_atomic_block_offset
196 : INTEGER, DIMENSION(:, :), POINTER, SAVE :: shm_is_assoc_atomic_block
197 43577 : INTEGER, DIMENSION(:, :, :), POINTER :: cell_to_index
198 43577 : INTEGER, DIMENSION(:, :, :, :), POINTER :: shm_set_offset
199 : INTEGER, SAVE :: shm_number_of_p_entries
200 : LOGICAL :: bins_left, buffer_overflow, do_disk_storage, do_dynamic_load_balancing, do_it, &
201 : do_kpoints, do_p_screening, do_periodic, do_print_load_balance_info, is_anti_symmetric, &
202 : ks_fully_occ, my_geo_change, treat_lsd_in_core, use_disk_storage
203 43577 : LOGICAL, DIMENSION(:, :), POINTER :: shm_atomic_pair_list
204 : REAL(dp) :: afac, bintime_start, bintime_stop, cartesian_estimate, compression_factor, &
205 : compression_factor_disk, ene_x_aa, ene_x_aa_diag, ene_x_bb, ene_x_bb_diag, eps_schwarz, &
206 : eps_storage, etmp, fac, hf_fraction, ln_10, log10_eps_schwarz, log10_pmax, &
207 : max_contraction_val, max_val1, max_val2, max_val2_set, pmax_atom, pmax_blocks, &
208 : pmax_entry, ra(3), rab2, rb(3), rc(3), rcd2, rd(3), screen_kind_ij, screen_kind_kl, &
209 : spherical_estimate, symm_fac
210 43577 : REAL(dp), ALLOCATABLE, DIMENSION(:) :: ee_buffer1, ee_buffer2, ee_primitives_tmp, ee_work, &
211 87154 : ee_work2, kac_buf, kad_buf, kbc_buf, kbd_buf, pac_buf, pad_buf, pbc_buf, pbd_buf, &
212 43577 : primitive_integrals
213 :
214 43577 : REAL(dp), DIMENSION(:, :), POINTER :: full_density_alpha, full_density_beta, full_ks_alpha, &
215 87154 : full_ks_beta, max_contraction, ptr_p_1, ptr_p_2, ptr_p_3, ptr_p_4, shm_pmax_atom, &
216 43577 : shm_pmax_block, sphi_b, zeta, zetb, zetc, zetd, &
217 43577 : full_ks_alpha_from_gpu, full_ks_beta_from_gpu
218 43577 : REAL(dp), DIMENSION(:, :, :), POINTER :: sphi_a_ext_set, sphi_b_ext_set, &
219 43577 : sphi_c_ext_set, sphi_d_ext_set
220 43577 : REAL(dp), DIMENSION(:, :, :, :), POINTER :: sphi_a_ext, sphi_b_ext, sphi_c_ext, &
221 43577 : sphi_d_ext
222 43577 : REAL(dp), DIMENSION(:, :, :), POINTER :: gcc
223 : REAL(KIND=dp) :: coeffs_kind_max0
224 43577 : TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
225 : TYPE(cell_type), POINTER :: cell
226 : TYPE(four_center_integral_context_type) :: private_context
227 : TYPE(cp_logger_type), POINTER :: logger
228 43577 : TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_ks_aux_fit_hfx
229 : TYPE(dft_control_type), POINTER :: dft_control
230 : TYPE(hfx_basis_info_type), POINTER :: basis_info
231 43577 : TYPE(hfx_basis_type), DIMENSION(:), POINTER :: basis_parameter
232 43577 : TYPE(hfx_cache_type), DIMENSION(:), POINTER :: integral_caches, integral_caches_disk
233 : TYPE(hfx_cache_type), POINTER :: maxval_cache, maxval_cache_disk
234 43577 : TYPE(hfx_container_type), DIMENSION(:), POINTER :: integral_containers, &
235 43577 : integral_containers_disk
236 : TYPE(hfx_container_type), POINTER :: maxval_container, maxval_container_disk
237 : TYPE(hfx_distribution), POINTER :: distribution_energy
238 : TYPE(hfx_general_type) :: general_parameter
239 : TYPE(hfx_load_balance_type), POINTER :: load_balance_parameter
240 : TYPE(hfx_memory_type), POINTER :: memory_parameter
241 43577 : TYPE(hfx_p_kind), DIMENSION(:), POINTER :: shm_initial_p
242 43577 : TYPE(hfx_pgf_list), ALLOCATABLE, DIMENSION(:) :: pgf_list_ij, pgf_list_kl
243 : TYPE(hfx_pgf_product_list), ALLOCATABLE, &
244 43577 : DIMENSION(:) :: pgf_product_list
245 : TYPE(hfx_potential_type) :: potential_parameter
246 : TYPE(hfx_screen_coeff_type), DIMENSION(:, :), &
247 43577 : POINTER :: screen_coeffs_kind, tmp_R_1, tmp_R_2, &
248 43577 : tmp_screen_pgf1, tmp_screen_pgf2
249 : TYPE(hfx_screen_coeff_type), &
250 43577 : DIMENSION(:, :, :, :), POINTER :: screen_coeffs_set
251 : TYPE(hfx_screen_coeff_type), &
252 43577 : DIMENSION(:, :, :, :, :, :), POINTER :: radii_pgf, screen_coeffs_pgf
253 : TYPE(hfx_screening_type) :: screening_parameter
254 43577 : TYPE(hfx_task_list_type), DIMENSION(:), POINTER :: shm_task_list, tmp_task_list
255 : TYPE(hfx_type), POINTER :: actual_x_data, shm_master_x_data
256 : TYPE(kpoint_type), POINTER :: kpoints
257 : TYPE(pair_list_type) :: list_ij, list_kl
258 : TYPE(pair_set_list_type), ALLOCATABLE, &
259 43577 : DIMENSION(:) :: set_list_ij, set_list_kl
260 43577 : TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
261 : TYPE(qs_ks_env_type), POINTER :: ks_env
262 :
263 : logical :: screened
264 : real(dp) :: max_abs_delta_KS
265 : integer :: iatom_set, jatom_set, katom_set, latom_set, shared_dev
266 : logical :: use_libint, use_libgint, use_only_alpha_spin
267 : logical :: cp2k_was_compiled_with_cuda_offload, cp2k_was_compiled_with_libint
268 : logical :: cp2k_was_compiled_with_libGint
269 :
270 43577 : NULLIFY (dft_control, matrix_ks_aux_fit_hfx)
271 :
272 43577 : CALL timeset(routineN, handle)
273 :
274 43577 : CALL cite_reference(Guidon2008)
275 43577 : CALL cite_reference(Guidon2009)
276 :
277 43577 : ehfx = 0.0_dp
278 :
279 : ! This is not very clean, but effective. ispin can only be 2 if we do the beta spin part in core
280 43577 : my_geo_change = geometry_did_change
281 43577 : IF (ispin == 2) my_geo_change = .FALSE.
282 :
283 43577 : logger => cp_get_default_logger()
284 :
285 43577 : is_anti_symmetric = dbcsr_get_matrix_type(rho_ao(1, 1)%matrix) == dbcsr_type_antisymmetric
286 :
287 43577 : iw = -1
288 :
289 43577 : IF (my_geo_change) THEN
290 2596 : CALL m_memory(memsize_before)
291 2596 : CALL para_env%max(memsize_before)
292 : iw = cp_print_key_unit_nr(logger, hfx_section, "HF_INFO", &
293 2596 : extension=".scfLog")
294 2596 : IF (iw > 0) THEN
295 : WRITE (UNIT=iw, FMT="(/,(T3,A,T60,I21))") &
296 478 : "HFX_MEM_INFO| Est. max. program size before HFX [MiB]:", memsize_before/(1024*1024)
297 478 : CALL m_flush(iw)
298 : END IF
299 : CALL cp_print_key_finished_output(iw, logger, hfx_section, &
300 2596 : "HF_INFO")
301 : END IF
302 :
303 43577 : CALL get_qs_env(qs_env=qs_env, atomic_kind_set=atomic_kind_set, cell=cell)
304 :
305 43577 : NULLIFY (cell_to_index)
306 43577 : CALL get_qs_env(qs_env=qs_env, do_kpoints=do_kpoints)
307 43577 : IF (do_kpoints) THEN
308 0 : CALL get_qs_env(qs_env=qs_env, ks_env=ks_env)
309 0 : CALL get_ks_env(ks_env=ks_env, kpoints=kpoints)
310 0 : CALL get_kpoint_info(kpoint=kpoints, cell_to_index=cell_to_index)
311 : END IF
312 :
313 : !! Calculate l_max used in fgamma , because init_md_ftable is definitely not thread safe
314 43577 : nkind = SIZE(atomic_kind_set, 1)
315 43577 : l_max = 0
316 123531 : DO ikind = 1, nkind
317 361755 : l_max = MAX(l_max, MAXVAL(x_data(1, 1)%basis_parameter(ikind)%lmax))
318 : END DO
319 43577 : l_max = 4*l_max
320 43577 : CALL init_md_ftable(l_max)
321 :
322 43577 : IF (x_data(1, 1)%potential_parameter%potential_type == do_potential_truncated .OR. &
323 : x_data(1, 1)%potential_parameter%potential_type == do_potential_mix_cl_trunc) THEN
324 9996 : IF (l_max > init_t_c_g0_lmax) THEN
325 320 : IF (para_env%is_source()) THEN
326 160 : CALL open_file(unit_number=unit_id, file_name=x_data(1, 1)%potential_parameter%filename)
327 : END IF
328 320 : CALL init(l_max, unit_id, para_env%mepos, para_env)
329 320 : IF (para_env%is_source()) THEN
330 160 : CALL close_file(unit_id)
331 : END IF
332 320 : init_t_c_g0_lmax = l_max
333 : END IF
334 : END IF
335 :
336 43577 : n_threads = 1
337 43577 : !$ n_threads = omp_get_max_threads()
338 :
339 : ! This initialization is needed to prevent a segmentation fault. The correct assigment is done below
340 43577 : my_nspins = 0
341 43577 : IF (PRESENT(nspins)) my_nspins = nspins
342 :
343 43577 : shm_neris_total = 0
344 43577 : shm_nprim_ints = 0
345 43577 : shm_neris_onthefly = 0
346 43577 : shm_storage_counter_integrals = 0
347 43577 : shm_stor_count_int_disk = 0
348 43577 : shm_neris_incore = 0
349 43577 : shm_neris_disk = 0
350 43577 : shm_stor_count_max_val = 0
351 :
352 : !$OMP PARALLEL DEFAULT(NONE) &
353 : !$OMP SHARED(qs_env,&
354 : !$OMP x_data,&
355 : !$OMP ks_matrix,&
356 : !$OMP ehfx,&
357 : !$OMP rho_ao,&
358 : !$OMP matrix_ks_aux_fit_hfx,&
359 : !$OMP hfx_section,&
360 : !$OMP para_env,&
361 : !$OMP my_geo_change,&
362 : !$OMP irep,&
363 : !$OMP distribute_fock_matrix,&
364 : !$OMP cell_to_index,&
365 : !$OMP ncoset,&
366 : !$OMP nso,&
367 : !$OMP nco,&
368 : !$OMP full_ks_alpha,&
369 : !$OMP full_ks_alpha_from_gpu,&
370 : !$OMP full_ks_beta_from_gpu,&
371 : !$OMP full_ks_beta,&
372 : !$OMP n_threads,&
373 : !$OMP full_density_alpha,&
374 : !$OMP full_density_beta,&
375 : !$OMP shm_initial_p,&
376 : !$OMP shm_is_assoc_atomic_block,&
377 : !$OMP shm_number_of_p_entries,&
378 : !$OMP shm_neris_total,&
379 : !$OMP shm_neris_onthefly,&
380 : !$OMP shm_storage_counter_integrals,&
381 : !$OMP shm_stor_count_int_disk,&
382 : !$OMP shm_neris_incore,&
383 : !$OMP shm_neris_disk,&
384 : !$OMP shm_nprim_ints,&
385 : !$OMP shm_stor_count_max_val,&
386 : !$OMP cell,&
387 : !$OMP screen_coeffs_set,&
388 : !$OMP screen_coeffs_kind,&
389 : !$OMP screen_coeffs_pgf,&
390 : !$OMP pgf_product_list_size,&
391 : !$OMP radii_pgf,&
392 : !$OMP nkind,&
393 : !$OMP ispin,&
394 : !$OMP is_anti_symmetric,&
395 : !$OMP shm_atomic_block_offset,&
396 : !$OMP shm_set_offset,&
397 : !$OMP shm_block_offset,&
398 : !$OMP shm_task_counter,&
399 : !$OMP shm_task_list,&
400 : !$OMP shm_total_bins,&
401 : !$OMP shm_master_x_data,&
402 : !$OMP shm_pmax_atom,&
403 : !$OMP shm_pmax_block,&
404 : !$OMP shm_atomic_pair_list,&
405 : !$OMP shm_mem_compression_counter,&
406 : !$OMP shared_dev, &
407 : !$OMP do_print_load_balance_info,use_libgint,use_libint, &
408 : !$OMP cp2k_was_compiled_with_cuda_offload, &
409 : !$OMP cp2k_was_compiled_with_libint, &
410 : !$OMP cp2k_was_compiled_with_libGint, &
411 : !$OMP my_nspins) &
412 : !$OMP PRIVATE(ln_10,i_thread,actual_x_data,do_periodic,screening_parameter,potential_parameter,&
413 : !$OMP general_parameter,load_balance_parameter,memory_parameter,cache_size,bits_max_val,&
414 : !$OMP basis_parameter,basis_info,treat_lsd_in_core,ncpu,n_processes,neris_total,neris_incore,&
415 : !$OMP neris_disk,neris_onthefly,mem_eris,mem_eris_disk,mem_max_val,compression_factor,&
416 : !$OMP compression_factor_disk,nprim_ints,neris_tmp,max_val_memory,max_am,do_p_screening,&
417 : !$OMP max_set,particle_set,atomic_kind_set,natom,kind_of,ncos_max,nsgf_max,ikind,&
418 : !$OMP nseta,npgfa,la_max,nsgfa,primitive_integrals,pbd_buf,pbc_buf,pad_buf,pac_buf,kbd_buf,kbc_buf,&
419 : !$OMP kad_buf,kac_buf,ee_work,ee_work2,ee_buffer1,ee_buffer2,ee_primitives_tmp,max_contraction,&
420 : !$OMP max_pgf,jkind,lb_max,nsetb,npgfb,first_sgfb,sphi_b,nsgfb,ncob,sgfb,nneighbors,pgf_list_ij,pgf_list_kl,&
421 : !$OMP pgf_product_list,nimages,ks_fully_occ,subtr_size_mb,use_disk_storage,counter,do_disk_storage,&
422 : !$OMP maxval_container_disk,maxval_cache_disk,integral_containers_disk,integral_caches_disk,eps_schwarz,&
423 : !$OMP log10_eps_schwarz,eps_storage,hf_fraction,buffer_overflow,logger,private_context,last_sgf_global,handle_getp,&
424 : !$OMP fac,handle_load,do_dynamic_load_balancing,my_bin_size,maxval_container,integral_containers,maxval_cache,&
425 : !$OMP integral_caches,tmp_task_list,tmp_task_list_cost,tmp_index,handle_main,coeffs_kind_max0,set_list_ij,&
426 : !$OMP set_list_kl,iatom_start,iatom_end,jatom_start,jatom_end,nblocks,bins_left,do_it,distribution_energy,&
427 : !$OMP my_thread_id,my_bin_id,handle_bin,bintime_start,my_istart,my_current_counter,latom_block,tmp_block,&
428 : !$OMP katom_block,katom_start,katom_end,latom_start,latom_end,pmax_blocks,list_ij,list_kl,i_set_list_ij_start,&
429 : !$OMP i_set_list_ij_stop,ra,rb,rab2,la_min,zeta,sphi_a_ext,nsgfl_a,sphi_a_u1,sphi_a_u2,sphi_a_u3,&
430 : !$OMP lb_min,zetb,sphi_b_ext,nsgfl_b,sphi_b_u1,sphi_b_u2,sphi_b_u3,katom,latom,i_set_list_kl_start,i_set_list_kl_stop,&
431 : !$OMP kkind,lkind,rc,rd,rcd2,pmax_atom,screen_kind_ij,screen_kind_kl,symm_fac,lc_max,lc_min,npgfc,zetc,nsgfc,sphi_c_ext,&
432 : !$OMP nsgfl_c,sphi_c_u1,sphi_c_u2,sphi_c_u3,ld_max,ld_min,npgfd,zetd,nsgfd,sphi_d_ext,nsgfl_d,sphi_d_u1,sphi_d_u2,&
433 : !$OMP sphi_d_u3,atomic_offset_bd,atomic_offset_bc,atomic_offset_ad,atomic_offset_ac,offset_bd_set,offset_bc_set,&
434 : !$OMP offset_ad_set,offset_ac_set,swap_id,kind_kind_idx,ptr_p_1,ptr_p_2,ptr_p_3,ptr_p_4,mem_compression_counter,&
435 : !$OMP mem_compression_counter_disk,max_val1,sphi_a_ext_set,sphi_b_ext_set,kset,lset,max_val2_set,max_val2,&
436 : !$OMP sphi_c_ext_set,sphi_d_ext_set,pmax_entry,log10_pmax,current_counter,nints,estimate_to_store_int,&
437 : !$OMP spherical_estimate,nbits,buffer_left,buffer_start,buffer_size,max_contraction_val,tmp_r_1,tmp_r_2,&
438 : !$OMP tmp_screen_pgf1,tmp_screen_pgf2,cartesian_estimate,bintime_stop,iw,memsize_after,storage_counter_integrals,&
439 : !$OMP stor_count_int_disk,stor_count_max_val,ene_x_aa,ene_x_bb,mb_size_p,mb_size_f,mb_size_buffers,afac,ene_x_aa_diag,&
440 : !$OMP ene_x_bb_diag,act_atomic_block_offset,act_set_offset,j,handle_dist_ks,tmp_i8,tmp_i4,dft_control,&
441 : !$OMP etmp,nkimages,img,bin,eps_scaling_str,eps_schwarz_min_str, &
442 : !$OMP i,inla,nla,gcc,first_set_of_atom,max_abs_delta_KS, screened, &
443 43577 : !$OMP iatom_set,jatom_set,katom_set,latom_set,use_only_alpha_spin)
444 :
445 : ln_10 = LOG(10.0_dp)
446 : i_thread = 0
447 : !$ i_thread = omp_get_thread_num()
448 :
449 : actual_x_data => x_data(irep, i_thread + 1)
450 : !$OMP MASTER
451 : shm_master_x_data => x_data(irep, 1)
452 : !$OMP END MASTER
453 : !$OMP BARRIER
454 :
455 : do_periodic = actual_x_data%periodic_parameter%do_periodic
456 :
457 : IF (do_periodic) THEN
458 : ! ** Rebuild neighbor lists in case the cell has changed (i.e. NPT MD)
459 : actual_x_data%periodic_parameter%number_of_shells = actual_x_data%periodic_parameter%mode
460 : CALL hfx_create_neighbor_cells(actual_x_data, actual_x_data%periodic_parameter%number_of_shells_from_input, &
461 : cell, i_thread)
462 : END IF
463 :
464 : screening_parameter = actual_x_data%screening_parameter
465 : potential_parameter = actual_x_data%potential_parameter
466 :
467 : general_parameter = actual_x_data%general_parameter
468 : load_balance_parameter => actual_x_data%load_balance_parameter
469 : memory_parameter => actual_x_data%memory_parameter
470 :
471 : cache_size = memory_parameter%cache_size
472 : bits_max_val = memory_parameter%bits_max_val
473 :
474 : basis_parameter => actual_x_data%basis_parameter
475 : basis_info => actual_x_data%basis_info
476 :
477 : treat_lsd_in_core = general_parameter%treat_lsd_in_core
478 :
479 : ncpu = para_env%num_pe
480 : n_processes = ncpu*n_threads
481 :
482 : !! initialize some counters
483 : neris_total = 0_int_8
484 : neris_incore = 0_int_8
485 : neris_disk = 0_int_8
486 : neris_onthefly = 0_int_8
487 : mem_eris = 0_int_8
488 : mem_eris_disk = 0_int_8
489 : mem_max_val = 0_int_8
490 : compression_factor = 0.0_dp
491 : compression_factor_disk = 0.0_dp
492 : nprim_ints = 0_int_8
493 : neris_tmp = 0_int_8
494 : max_val_memory = 1_int_8
495 :
496 : ! Set the integral engine
497 : IF (general_parameter%hfx_library == hfx_library_is_libint) THEN
498 : use_libint = .true.
499 : use_libGint = .false.
500 : END IF
501 : IF (general_parameter%hfx_library == hfx_library_is_libGint) THEN
502 : use_libint = .false.
503 : use_libGint = .true.
504 : END IF
505 : IF (general_parameter%hfx_library == hfx_library_is_both) THEN
506 : use_libint = .true.
507 : use_libGint = .true.
508 : END IF
509 :
510 : max_am = basis_info%max_am
511 :
512 : CALL get_qs_env(qs_env=qs_env, &
513 : atomic_kind_set=atomic_kind_set, &
514 : particle_set=particle_set, &
515 : dft_control=dft_control)
516 : IF (dft_control%do_admm) CALL get_admm_env(qs_env%admm_env, matrix_ks_aux_fit_hfx=matrix_ks_aux_fit_hfx)
517 :
518 : do_p_screening = screening_parameter%do_initial_p_screening
519 : ! Special treatment for MP2 with initial density screening
520 : IF (do_p_screening) THEN
521 : IF (ASSOCIATED(qs_env%mp2_env)) THEN
522 : IF ((qs_env%mp2_env%ri_grad%free_hfx_buffer)) THEN
523 : do_p_screening = ((qs_env%mp2_env%p_screen) .AND. (qs_env%mp2_env%not_last_hfx))
524 : ELSE
525 : do_p_screening = .FALSE.
526 : END IF
527 : END IF
528 : END IF
529 : max_set = basis_info%max_set
530 : natom = SIZE(particle_set, 1)
531 :
532 : ! Number of image matrices in k-point calculations (nkimages==1 -> no kpoints)
533 : nkimages = dft_control%nimages
534 : CPASSERT(nkimages == 1)
535 :
536 : CALL get_atomic_kind_set(atomic_kind_set=atomic_kind_set, kind_of=kind_of)
537 :
538 : !! precompute maximum nco and allocate scratch
539 : ncos_max = 0
540 : nsgf_max = 0
541 : DO iatom = 1, natom
542 : ikind = kind_of(iatom)
543 : nseta = basis_parameter(ikind)%nset
544 : npgfa => basis_parameter(ikind)%npgf
545 : la_max => basis_parameter(ikind)%lmax
546 : nsgfa => basis_parameter(ikind)%nsgf
547 : DO iset = 1, nseta
548 : ncos_max = MAX(ncos_max, ncoset(la_max(iset)))
549 : nsgf_max = MAX(nsgf_max, nsgfa(iset))
550 : END DO
551 : END DO
552 : !! Allocate the arrays for the integrals.
553 : ALLOCATE (primitive_integrals(nsgf_max**4))
554 : primitive_integrals = 0.0_dp
555 :
556 : ALLOCATE (pbd_buf(nsgf_max**2))
557 : ALLOCATE (pbc_buf(nsgf_max**2))
558 : ALLOCATE (pad_buf(nsgf_max**2))
559 : ALLOCATE (pac_buf(nsgf_max**2))
560 : ALLOCATE (kbd_buf(nsgf_max**2))
561 : ALLOCATE (kbc_buf(nsgf_max**2))
562 : ALLOCATE (kad_buf(nsgf_max**2))
563 : ALLOCATE (kac_buf(nsgf_max**2))
564 : ALLOCATE (ee_work(ncos_max**4))
565 : ALLOCATE (ee_work2(ncos_max**4))
566 : ALLOCATE (ee_buffer1(ncos_max**4))
567 : ALLOCATE (ee_buffer2(ncos_max**4))
568 : ALLOCATE (ee_primitives_tmp(nsgf_max**4))
569 :
570 : IF (my_nspins == 0) my_nspins = dft_control%nspins
571 :
572 : ALLOCATE (max_contraction(max_set, natom))
573 :
574 : max_contraction = 0.0_dp
575 : max_pgf = 0
576 : DO jatom = 1, natom
577 : jkind = kind_of(jatom)
578 : lb_max => basis_parameter(jkind)%lmax
579 : nsetb = basis_parameter(jkind)%nset
580 : npgfb => basis_parameter(jkind)%npgf
581 : first_sgfb => basis_parameter(jkind)%first_sgf
582 : sphi_b => basis_parameter(jkind)%sphi
583 : nsgfb => basis_parameter(jkind)%nsgf
584 : DO jset = 1, nsetb
585 : ! takes the primitive to contracted transformation into account
586 : ncob = npgfb(jset)*ncoset(lb_max(jset))
587 : sgfb = first_sgfb(1, jset)
588 : ! if the primitives are assumed to be all of max_val2, max_val2*p2s_b becomes
589 : ! the maximum value after multiplication with sphi_b
590 : max_contraction(jset, jatom) = MAXVAL([(SUM(ABS(sphi_b(1:ncob, i))), i=sgfb, sgfb + nsgfb(jset) - 1)])
591 : max_pgf = MAX(max_pgf, npgfb(jset))
592 : END DO
593 : END DO
594 :
595 : ! ** Allocate buffers for pgf_lists
596 : nneighbors = SIZE(actual_x_data%neighbor_cells)
597 : ALLOCATE (pgf_list_ij(max_pgf**2))
598 : ALLOCATE (pgf_list_kl(max_pgf**2))
599 : ! the size of pgf_product_list is allocated and resized as needed. The initial guess grows as needed
600 : !$OMP ATOMIC READ
601 : tmp_i4 = pgf_product_list_size
602 : ALLOCATE (pgf_product_list(tmp_i4))
603 : ALLOCATE (nimages(max_pgf**2))
604 :
605 : DO i = 1, max_pgf**2
606 : ALLOCATE (pgf_list_ij(i)%image_list(nneighbors))
607 : ALLOCATE (pgf_list_kl(i)%image_list(nneighbors))
608 : END DO
609 : !$OMP BARRIER
610 : !$OMP MASTER
611 : !! Calculate helper array that stores if a certain atomic pair is associated in the KS matrix
612 : IF (my_geo_change) THEN
613 : CALL get_atomic_block_maps(ks_matrix(1, 1)%matrix, basis_parameter, kind_of, &
614 : shm_master_x_data%is_assoc_atomic_block, &
615 : shm_master_x_data%number_of_p_entries, &
616 : para_env, &
617 : shm_master_x_data%atomic_block_offset, &
618 : shm_master_x_data%set_offset, &
619 : shm_master_x_data%block_offset, &
620 : shm_master_x_data%map_atoms_to_cpus, &
621 : nkind)
622 :
623 : shm_is_assoc_atomic_block => shm_master_x_data%is_assoc_atomic_block
624 :
625 : !! Get occupation of KS-matrix
626 : ks_fully_occ = .TRUE.
627 : outer: DO iatom = 1, natom
628 : DO jatom = iatom, natom
629 : IF (shm_is_assoc_atomic_block(jatom, iatom) == 0) THEN
630 : ks_fully_occ = .FALSE.
631 : EXIT outer
632 : END IF
633 : END DO
634 : END DO outer
635 :
636 : IF (.NOT. ks_fully_occ) THEN
637 : CALL cp_warn(__LOCATION__, &
638 : "The Kohn Sham matrix is not 100% occupied. This "// &
639 : "may result in incorrect Hartree-Fock results. Setting "// &
640 : "MIN_PAIR_LIST_RADIUS to -1 in the QS section ensures a "// &
641 : "fully occupied KS matrix. For more information "// &
642 : "see FAQ: https://www.cp2k.org/faq:hfx_eps_warning")
643 : END IF
644 : END IF
645 :
646 : ! ** Set pointers
647 : shm_number_of_p_entries = shm_master_x_data%number_of_p_entries
648 : shm_is_assoc_atomic_block => shm_master_x_data%is_assoc_atomic_block
649 : shm_atomic_block_offset => shm_master_x_data%atomic_block_offset
650 : shm_set_offset => shm_master_x_data%set_offset
651 : shm_block_offset => shm_master_x_data%block_offset
652 : !$OMP END MASTER
653 : !$OMP BARRIER
654 :
655 : ! ** Reset storage counter given by MAX_MEMORY by subtracting all buffers
656 : ! ** Fock and density Matrices (shared)
657 : subtr_size_mb = 2_int_8*shm_block_offset(ncpu + 1)
658 : ! ** if non restricted ==> alpha/beta spin
659 : IF (.NOT. treat_lsd_in_core) THEN
660 : IF (my_nspins == 2) subtr_size_mb = subtr_size_mb*2_int_8
661 : END IF
662 : ! ** Initial P only MAX(alpha,beta) (shared)
663 : IF (do_p_screening .OR. screening_parameter%do_p_screening_forces) THEN
664 : subtr_size_mb = subtr_size_mb + memory_parameter%size_p_screen
665 : END IF
666 : ! ** In core forces require their own initial P
667 : IF (screening_parameter%do_p_screening_forces) THEN
668 : IF (memory_parameter%treat_forces_in_core) THEN
669 : subtr_size_mb = subtr_size_mb + memory_parameter%size_p_screen
670 : END IF
671 : END IF
672 : ! ** primitive buffer (not shared by the threads)
673 : subtr_size_mb = subtr_size_mb + nsgf_max**4*n_threads
674 : ! ** density + fock buffers
675 : subtr_size_mb = subtr_size_mb + 8_int_8*nsgf_max**2*n_threads
676 : ! ** screening functions (shared)
677 : ! ** coeffs_pgf
678 : subtr_size_mb = subtr_size_mb + max_pgf**2*max_set**2*nkind**2
679 : ! ** coeffs_set
680 : subtr_size_mb = subtr_size_mb + max_set**2*nkind**2
681 : ! ** coeffs_kind
682 : subtr_size_mb = subtr_size_mb + nkind**2
683 : ! ** radii_pgf
684 : subtr_size_mb = subtr_size_mb + max_pgf**2*max_set**2*nkind**2
685 :
686 : ! ** is_assoc (shared)
687 : subtr_size_mb = subtr_size_mb + natom**2
688 :
689 : ! ** pmax_atom (shared)
690 : IF (do_p_screening) THEN
691 : subtr_size_mb = subtr_size_mb + natom**2
692 : END IF
693 : IF (screening_parameter%do_p_screening_forces) THEN
694 : IF (memory_parameter%treat_forces_in_core) THEN
695 : subtr_size_mb = subtr_size_mb + natom**2
696 : END IF
697 : END IF
698 :
699 : ! ** Convert into MiB's
700 : subtr_size_mb = subtr_size_mb*8_int_8/1024_int_8/1024_int_8
701 :
702 : ! ** Subtracting all these buffers from MAX_MEMORY yields the amount
703 : ! ** of RAM that is left for the compressed integrals. When using threads
704 : ! ** all the available memory is shared among all n_threads. i.e. the faster
705 : ! ** ones can steal from the slower ones
706 :
707 : CALL hfx_reset_memory_usage_counter(memory_parameter, subtr_size_mb)
708 :
709 : use_disk_storage = .FALSE.
710 : counter = 0_int_8
711 : do_disk_storage = memory_parameter%do_disk_storage
712 : IF (do_disk_storage) THEN
713 : maxval_container_disk => actual_x_data%store_ints%maxval_container_disk
714 : maxval_cache_disk => actual_x_data%store_ints%maxval_cache_disk
715 :
716 : integral_containers_disk => actual_x_data%store_ints%integral_containers_disk
717 : integral_caches_disk => actual_x_data%store_ints%integral_caches_disk
718 : END IF
719 :
720 : IF (my_geo_change) THEN
721 : memory_parameter%ram_counter = HUGE(memory_parameter%ram_counter)
722 : END IF
723 :
724 : IF (my_geo_change) THEN
725 : memory_parameter%recalc_forces = .TRUE.
726 : ELSE
727 : IF (.NOT. memory_parameter%treat_forces_in_core) memory_parameter%recalc_forces = .TRUE.
728 : END IF
729 :
730 : !! Get screening parameter
731 : eps_schwarz = screening_parameter%eps_schwarz
732 : IF (eps_schwarz <= 0.0_dp) THEN
733 : log10_eps_schwarz = log_zero
734 : ELSE
735 : log10_eps_schwarz = LOG10(eps_schwarz)
736 : END IF
737 : !! get storage epsilon
738 : eps_storage = eps_schwarz*memory_parameter%eps_storage_scaling
739 :
740 : !! If we have a hybrid functional, we may need only a fraction of exact exchange
741 : hf_fraction = general_parameter%fraction
742 :
743 : !! The number of integrals that fit into the given MAX_MEMORY
744 :
745 : !! Parameters related to the potential 1/r, erf(wr)/r, erfc(wr/r)
746 : potential_parameter = actual_x_data%potential_parameter
747 :
748 : !! Variable to check if we calculate the integrals in-core or on the fly
749 : !! If TRUE -> on the fly
750 : IF (.NOT. memory_parameter%do_all_on_the_fly) THEN
751 : buffer_overflow = .FALSE.
752 : ELSE
753 : buffer_overflow = .TRUE.
754 : END IF
755 : logger => cp_get_default_logger()
756 :
757 : private_context = actual_x_data%eri4_context
758 :
759 : !! Helper array to map local basis function indices to global ones
760 : ALLOCATE (last_sgf_global(0:natom))
761 : last_sgf_global(0) = 0
762 : DO iatom = 1, natom
763 : ikind = kind_of(iatom)
764 : last_sgf_global(iatom) = last_sgf_global(iatom - 1) + basis_parameter(ikind)%nsgf_total
765 : END DO
766 : !$OMP BARRIER
767 : !$OMP MASTER
768 : !! Let master thread get the density (avoid problems with MPI)
769 : !! Get the full density from all the processors
770 : NULLIFY (full_density_alpha, full_density_beta)
771 : ALLOCATE (full_density_alpha(shm_block_offset(ncpu + 1), nkimages))
772 : IF (.NOT. treat_lsd_in_core .OR. my_nspins == 1) THEN
773 : CALL timeset(routineN//"_getP", handle_getP)
774 : DO img = 1, nkimages
775 : CALL get_full_density(para_env, full_density_alpha(:, img), rho_ao(ispin, img)%matrix, shm_number_of_p_entries, &
776 : shm_master_x_data%block_offset, &
777 : kind_of, basis_parameter, get_max_vals_spin=.FALSE., antisymmetric=is_anti_symmetric)
778 : END DO
779 :
780 : IF (my_nspins == 2) THEN
781 : ALLOCATE (full_density_beta(shm_block_offset(ncpu + 1), nkimages))
782 : DO img = 1, nkimages
783 : CALL get_full_density(para_env, full_density_beta(:, img), rho_ao(2, img)%matrix, shm_number_of_p_entries, &
784 : shm_master_x_data%block_offset, &
785 : kind_of, basis_parameter, get_max_vals_spin=.FALSE., antisymmetric=is_anti_symmetric)
786 : END DO
787 : END IF
788 : CALL timestop(handle_getP)
789 :
790 : !! Calculate the max values of the density matrix actual_pmax stores the data from the actual density matrix
791 : !! and x_data%initial_p stores the same for the initial guess. The initial guess is updated only in the case of
792 : !! a changed geometry
793 : NULLIFY (shm_initial_p)
794 : IF (do_p_screening) THEN
795 : shm_initial_p => shm_master_x_data%initial_p
796 : shm_pmax_atom => shm_master_x_data%pmax_atom
797 : IF (my_geo_change) THEN
798 : CALL update_pmax_mat(shm_master_x_data%initial_p, &
799 : shm_master_x_data%map_atom_to_kind_atom, &
800 : shm_master_x_data%set_offset, &
801 : shm_master_x_data%atomic_block_offset, &
802 : shm_pmax_atom, &
803 : full_density_alpha, full_density_beta, &
804 : natom, kind_of, basis_parameter, &
805 : nkind, shm_master_x_data%is_assoc_atomic_block, my_nspins)
806 : END IF
807 : END IF
808 : ELSE
809 : IF (do_p_screening) THEN
810 : CALL timeset(routineN//"_getP", handle_getP)
811 : DO img = 1, nkimages
812 : CALL get_full_density(para_env, full_density_alpha(:, img), rho_ao(1, img)%matrix, shm_number_of_p_entries, &
813 : shm_master_x_data%block_offset, &
814 : kind_of, basis_parameter, get_max_vals_spin=.TRUE., &
815 : rho_beta=rho_ao(2, img)%matrix, antisymmetric=is_anti_symmetric)
816 : END DO
817 : CALL timestop(handle_getP)
818 :
819 : !! Calculate the max values of the density matrix actual_pmax stores the data from the actual density matrix
820 : !! and x_data%initial_p stores the same for the initial guess. The initial guess is updated only in the case of
821 : !! a changed geometry
822 : NULLIFY (shm_initial_p)
823 : shm_initial_p => actual_x_data%initial_p
824 : shm_pmax_atom => shm_master_x_data%pmax_atom
825 : IF (my_geo_change) THEN
826 : CALL update_pmax_mat(shm_master_x_data%initial_p, &
827 : shm_master_x_data%map_atom_to_kind_atom, &
828 : shm_master_x_data%set_offset, &
829 : shm_master_x_data%atomic_block_offset, &
830 : shm_pmax_atom, &
831 : full_density_alpha, full_density_beta, &
832 : natom, kind_of, basis_parameter, &
833 : nkind, shm_master_x_data%is_assoc_atomic_block, my_nspins)
834 : END IF
835 : END IF
836 : ! ** Now get the density(ispin)
837 : DO img = 1, nkimages
838 : CALL get_full_density(para_env, full_density_alpha(:, img), rho_ao(ispin, img)%matrix, shm_number_of_p_entries, &
839 : shm_master_x_data%block_offset, &
840 : kind_of, basis_parameter, get_max_vals_spin=.FALSE., &
841 : antisymmetric=is_anti_symmetric)
842 : END DO
843 : END IF
844 :
845 : NULLIFY (full_ks_alpha, full_ks_beta)
846 : ALLOCATE (shm_master_x_data%full_ks_alpha(shm_block_offset(ncpu + 1), nkimages))
847 : full_ks_alpha => shm_master_x_data%full_ks_alpha
848 : full_ks_alpha = 0.0_dp
849 :
850 : IF (.NOT. treat_lsd_in_core) THEN
851 : IF (my_nspins == 2) THEN
852 : ALLOCATE (shm_master_x_data%full_ks_beta(shm_block_offset(ncpu + 1), nkimages))
853 : full_ks_beta => shm_master_x_data%full_ks_beta
854 : full_ks_beta = 0.0_dp
855 : END IF
856 : END IF
857 :
858 : !$OMP END MASTER
859 : !$OMP BARRIER
860 :
861 : !! Initialize schwarz screening matrices for near field estimates and boxing screening matrices
862 : !! for far field estimates. The update is only performed if the geomtry of the system changed.
863 : !! If the system is periodic, then the corresponding routines are called and some variables
864 : !! are initialized
865 :
866 : IF (.NOT. shm_master_x_data%screen_funct_is_initialized) THEN
867 : CALL calc_pair_dist_radii(qs_env, basis_parameter, &
868 : shm_master_x_data%pair_dist_radii_pgf, max_set, max_pgf, eps_schwarz, &
869 : n_threads, i_thread)
870 : !$OMP BARRIER
871 : CALL calc_screening_functions(qs_env, basis_parameter, private_context, shm_master_x_data%potential_parameter, &
872 : shm_master_x_data%screen_funct_coeffs_set, &
873 : shm_master_x_data%screen_funct_coeffs_kind, &
874 : shm_master_x_data%screen_funct_coeffs_pgf, &
875 : shm_master_x_data%pair_dist_radii_pgf, &
876 : max_set, max_pgf, n_threads, i_thread)
877 :
878 : !$OMP MASTER
879 : shm_master_x_data%screen_funct_is_initialized = .TRUE.
880 : !$OMP END MASTER
881 : END IF
882 : !$OMP BARRIER
883 :
884 : !$OMP MASTER
885 : screen_coeffs_set => shm_master_x_data%screen_funct_coeffs_set
886 : screen_coeffs_kind => shm_master_x_data%screen_funct_coeffs_kind
887 : screen_coeffs_pgf => shm_master_x_data%screen_funct_coeffs_pgf
888 : radii_pgf => shm_master_x_data%pair_dist_radii_pgf
889 : !$OMP END MASTER
890 : !$OMP BARRIER
891 :
892 : !! Initialize a prefactor depending on the fraction and the number of spins
893 : IF (my_nspins == 1) THEN
894 : fac = 0.5_dp*hf_fraction
895 : ELSE
896 : fac = 1.0_dp*hf_fraction
897 : END IF
898 :
899 : !! Call routines that distribute the load on all processes. If we want to screen on a initial density matrix, there is
900 : !! an optional parameter. Of course, this is only done if the geometry did change
901 : !$OMP BARRIER
902 : !$OMP MASTER
903 : CALL timeset(routineN//"_load", handle_load)
904 : !$OMP END MASTER
905 : !$OMP BARRIER
906 : IF (my_geo_change) THEN
907 : IF (actual_x_data%b_first_load_balance_energy) THEN
908 : CALL hfx_load_balance(actual_x_data, eps_schwarz, particle_set, max_set, para_env, &
909 : screen_coeffs_set, screen_coeffs_kind, &
910 : shm_is_assoc_atomic_block, do_periodic, load_balance_parameter, &
911 : kind_of, basis_parameter, shm_initial_p, shm_pmax_atom, i_thread, n_threads, &
912 : cell, do_p_screening, actual_x_data%map_atom_to_kind_atom, &
913 : nkind, hfx_do_eval_energy, shm_pmax_block, use_virial=.FALSE.)
914 : actual_x_data%b_first_load_balance_energy = .FALSE.
915 : ELSE
916 : CALL hfx_update_load_balance(actual_x_data, para_env, &
917 : load_balance_parameter, &
918 : i_thread, n_threads, hfx_do_eval_energy)
919 : END IF
920 : END IF
921 : !$OMP BARRIER
922 : !$OMP MASTER
923 : CALL timestop(handle_load)
924 : !$OMP END MASTER
925 : !$OMP BARRIER
926 :
927 : use_only_alpha_spin = treat_lsd_in_core .or. .not. my_nspins == 2
928 :
929 : ! Set up the gpu enviroment for libGint
930 : if (use_libGint) then
931 :
932 : call offload_activate_chosen_device()
933 :
934 : ! At least once, and before any other call from all OMP threads
935 : call cp_libGint_init(actual_x_data)
936 :
937 : ! At least at geo change
938 : call libGint_update_env(fac, memory_parameter, do_periodic, cell, actual_x_data, &
939 : nneighbors, max_pgf, natom, kind_of, particle_set, basis_parameter)
940 :
941 : ! Every cycle, comunicate the updated density to libGint
942 : if (use_only_alpha_spin) then
943 : call libGint_set_density(full_density_alpha)
944 : else
945 : call libGint_set_density(full_density_alpha, full_density_beta)
946 : end if
947 : end if
948 : !!!
949 :
950 : !! Start calculating integrals of the form (ab|cd) or (ij|kl)
951 : !! In order to do so, there is a main four-loop structure that takes into account the two symmetries
952 : !!
953 : !! (ab|cd) = (ba|cd) = (ab|dc) = (ba|dc)
954 : !!
955 : !! by iterating in the following way
956 : !!
957 : !! DO iatom=1,natom and DO katom=1,natom
958 : !! DO jatom=iatom,natom DO latom=katom,natom
959 : !!
960 : !! The third symmetry
961 : !!
962 : !! (ab|cd) = (cd|ab)
963 : !!
964 : !! is taken into account by the following criterion:
965 : !!
966 : !! IF(katom+latom<=iatom+jatom) THEN
967 : !! IF( ((iatom+jatom)==(katom+latom) ) .AND.(katom<iatom)) CYCLE
968 : !!
969 : !! Depending on the degeneracy of an integral the exchange contribution is multiplied by a corresponding
970 : !! factor ( symm_fac ).
971 : !!
972 : !! If one quartet does not pass the screening we CYCLE on the outer most possible loop. Thats why we use
973 : !! different hierarchies of short range screening matrices.
974 : !!
975 : !! If we do a parallel run, each process owns a unique array of workloads. Here, a workload is
976 : !! defined as :
977 : !!
978 : !! istart, jstart, kstart, lstart, number_of_atom_quartets, initial_cpu_id
979 : !!
980 : !! This tells the process where to start the main loops and how many bunches of integrals it has to
981 : !! calculate. The original parallelization is a simple modulo distribution that is binned and
982 : !! optimized in the load_balance routines. Since the Monte Carlo routines can swap processors,
983 : !! we need to know which was the initial cpu_id.
984 : !! Furthermore, the indices iatom, jatom, katom, latom have to be set to istart, jstart, kstart and
985 : !! lstart only the first time the loop is executed. All subsequent loops have to start with one or
986 : !! iatom and katom respectively. Therefore, we use flags like first_j_loop etc.
987 :
988 : !! LibGint follows the same patter, distributing integrals over mpi and openmp with a shared-over-mpi
989 : !! density and fock matrix. The main differences are that the fock matrix is not in a valid state
990 : !! until after the libGint_get_fock_matrix has returned -in approximately a thousand lines-,
991 : !! and the integrals are not saved in memory, but recomputed each cycle
992 :
993 : do_dynamic_load_balancing = .TRUE.
994 :
995 : IF (n_threads == 1 .OR. do_disk_storage) do_dynamic_load_balancing = .FALSE.
996 :
997 : IF (do_dynamic_load_balancing) THEN
998 : my_bin_size = SIZE(actual_x_data%distribution_energy)
999 : ELSE
1000 : my_bin_size = 1
1001 : END IF
1002 : !! We do not need the containers if MAX_MEM = 0
1003 : IF (.NOT. memory_parameter%do_all_on_the_fly) THEN
1004 : !! IF new md step -> reinitialize containers
1005 : IF (my_geo_change) THEN
1006 : CALL dealloc_containers(actual_x_data%store_ints, memory_parameter%actual_memory_usage)
1007 : CALL alloc_containers(actual_x_data%store_ints, my_bin_size)
1008 :
1009 : DO bin = 1, my_bin_size
1010 : maxval_container => actual_x_data%store_ints%maxval_container(bin)
1011 : integral_containers => actual_x_data%store_ints%integral_containers(:, bin)
1012 : CALL hfx_init_container(maxval_container, memory_parameter%actual_memory_usage, .FALSE.)
1013 : DO i = 1, 64
1014 : CALL hfx_init_container(integral_containers(i), memory_parameter%actual_memory_usage, .FALSE.)
1015 : END DO
1016 : END DO
1017 : END IF
1018 :
1019 : !! Decompress the first cache for maxvals and integrals
1020 : IF (.NOT. my_geo_change) THEN
1021 : DO bin = 1, my_bin_size
1022 : maxval_cache => actual_x_data%store_ints%maxval_cache(bin)
1023 : maxval_container => actual_x_data%store_ints%maxval_container(bin)
1024 : integral_caches => actual_x_data%store_ints%integral_caches(:, bin)
1025 : integral_containers => actual_x_data%store_ints%integral_containers(:, bin)
1026 : CALL hfx_decompress_first_cache(bits_max_val, maxval_cache, maxval_container, &
1027 : memory_parameter%actual_memory_usage, .FALSE.)
1028 : DO i = 1, 64
1029 : CALL hfx_decompress_first_cache(i, integral_caches(i), integral_containers(i), &
1030 : memory_parameter%actual_memory_usage, .FALSE.)
1031 : END DO
1032 : END DO
1033 : END IF
1034 : END IF
1035 :
1036 : !! Since the I/O routines are no thread-safe, i.e. the procedure to get the unit number, put a lock here
1037 : !$OMP CRITICAL(hfxenergy_io_critical)
1038 : !! If we do disk storage, we have to initialize the containers/caches
1039 : IF (do_disk_storage) THEN
1040 : !! IF new md step -> reinitialize containers
1041 : IF (my_geo_change) THEN
1042 : CALL hfx_init_container(maxval_container_disk, memory_parameter%actual_memory_usage_disk, do_disk_storage)
1043 : DO i = 1, 64
1044 : CALL hfx_init_container(integral_containers_disk(i), memory_parameter%actual_memory_usage_disk, do_disk_storage)
1045 : END DO
1046 : END IF
1047 : !! Decompress the first cache for maxvals and integrals
1048 : IF (.NOT. my_geo_change) THEN
1049 : CALL hfx_decompress_first_cache(bits_max_val, maxval_cache_disk, maxval_container_disk, &
1050 : memory_parameter%actual_memory_usage_disk, .TRUE.)
1051 : DO i = 1, 64
1052 : CALL hfx_decompress_first_cache(i, integral_caches_disk(i), integral_containers_disk(i), &
1053 : memory_parameter%actual_memory_usage_disk, .TRUE.)
1054 : END DO
1055 : END IF
1056 : END IF
1057 : !$OMP END CRITICAL(hfxenergy_io_critical)
1058 :
1059 : !$OMP BARRIER
1060 : !$OMP MASTER
1061 :
1062 : IF (do_dynamic_load_balancing) THEN
1063 : ! ** Lets construct the task list
1064 : shm_total_bins = 0
1065 : DO i = 1, n_threads
1066 : shm_total_bins = shm_total_bins + SIZE(x_data(irep, i)%distribution_energy)
1067 : END DO
1068 : ALLOCATE (tmp_task_list(shm_total_bins))
1069 : shm_task_counter = 0
1070 : DO i = 1, n_threads
1071 : DO bin = 1, SIZE(x_data(irep, i)%distribution_energy)
1072 : shm_task_counter = shm_task_counter + 1
1073 : tmp_task_list(shm_task_counter)%thread_id = i
1074 : tmp_task_list(shm_task_counter)%bin_id = bin
1075 : tmp_task_list(shm_task_counter)%cost = x_data(irep, i)%distribution_energy(bin)%cost
1076 : END DO
1077 : END DO
1078 :
1079 : ! ** Now sort the task list
1080 : ALLOCATE (tmp_task_list_cost(shm_total_bins))
1081 : ALLOCATE (tmp_index(shm_total_bins))
1082 :
1083 : DO i = 1, shm_total_bins
1084 : tmp_task_list_cost(i) = tmp_task_list(i)%cost
1085 : END DO
1086 :
1087 : CALL sort(tmp_task_list_cost, shm_total_bins, tmp_index)
1088 :
1089 : ALLOCATE (shm_master_x_data%task_list(shm_total_bins))
1090 :
1091 : DO i = 1, shm_total_bins
1092 : shm_master_x_data%task_list(i) = tmp_task_list(tmp_index(shm_total_bins - i + 1))
1093 : END DO
1094 :
1095 : shm_task_list => shm_master_x_data%task_list
1096 : shm_task_counter = 0
1097 :
1098 : DEALLOCATE (tmp_task_list_cost, tmp_index, tmp_task_list)
1099 : END IF
1100 : !$OMP END MASTER
1101 : !$OMP BARRIER
1102 :
1103 : IF (my_bin_size > 0) THEN
1104 : maxval_container => actual_x_data%store_ints%maxval_container(1)
1105 : maxval_cache => actual_x_data%store_ints%maxval_cache(1)
1106 :
1107 : integral_containers => actual_x_data%store_ints%integral_containers(:, 1)
1108 : integral_caches => actual_x_data%store_ints%integral_caches(:, 1)
1109 : END IF
1110 :
1111 : !$OMP BARRIER
1112 : !$OMP MASTER
1113 : CALL timeset(routineN//"_main", handle_main)
1114 : !$OMP END MASTER
1115 : !$OMP BARRIER
1116 :
1117 : coeffs_kind_max0 = MAXVAL(screen_coeffs_kind(:, :)%x(2))
1118 : ALLOCATE (set_list_ij((max_set*load_balance_parameter%block_size)**2))
1119 : ALLOCATE (set_list_kl((max_set*load_balance_parameter%block_size)**2))
1120 :
1121 : !$OMP BARRIER
1122 : !$OMP MASTER
1123 :
1124 : !! precalculate maximum density matrix elements in blocks
1125 : actual_x_data%pmax_block = 0.0_dp
1126 : shm_pmax_block => actual_x_data%pmax_block
1127 : IF (do_p_screening) THEN
1128 : DO iatom_block = 1, SIZE(actual_x_data%blocks)
1129 : iatom_start = actual_x_data%blocks(iatom_block)%istart
1130 : iatom_end = actual_x_data%blocks(iatom_block)%iend
1131 : DO jatom_block = 1, SIZE(actual_x_data%blocks)
1132 : jatom_start = actual_x_data%blocks(jatom_block)%istart
1133 : jatom_end = actual_x_data%blocks(jatom_block)%iend
1134 : shm_pmax_block(iatom_block, jatom_block) = MAXVAL(shm_pmax_atom(iatom_start:iatom_end, jatom_start:jatom_end))
1135 : END DO
1136 : END DO
1137 : END IF
1138 : shm_atomic_pair_list => actual_x_data%atomic_pair_list
1139 : IF (my_geo_change) THEN
1140 : CALL build_atomic_pair_list(natom, shm_atomic_pair_list, kind_of, basis_parameter, particle_set, &
1141 : do_periodic, screen_coeffs_kind, coeffs_kind_max0, log10_eps_schwarz, cell, &
1142 : actual_x_data%blocks)
1143 : END IF
1144 :
1145 : my_bin_size = SIZE(actual_x_data%distribution_energy)
1146 : ! reset timings for the new SCF round
1147 : IF (my_geo_change) THEN
1148 : DO bin = 1, my_bin_size
1149 : actual_x_data%distribution_energy(bin)%time_first_scf = 0.0_dp
1150 : actual_x_data%distribution_energy(bin)%time_other_scf = 0.0_dp
1151 : actual_x_data%distribution_energy(bin)%time_forces = 0.0_dp
1152 : END DO
1153 : END IF
1154 : !$OMP END MASTER
1155 : !$OMP BARRIER
1156 :
1157 : my_bin_size = SIZE(actual_x_data%distribution_energy)
1158 : nblocks = load_balance_parameter%nblocks
1159 :
1160 : bins_left = .TRUE.
1161 : do_it = .TRUE.
1162 : bin = 0
1163 : ! Main cycle starts here
1164 : DO WHILE (bins_left)
1165 : ! Check if this thread can run this bin
1166 : IF (.NOT. do_dynamic_load_balancing) THEN
1167 : bin = bin + 1
1168 : IF (bin > my_bin_size) THEN
1169 : do_it = .FALSE.
1170 : bins_left = .FALSE.
1171 : ELSE
1172 : do_it = .TRUE.
1173 : bins_left = .TRUE.
1174 : distribution_energy => actual_x_data%distribution_energy(bin)
1175 : END IF
1176 : ELSE
1177 : !$OMP CRITICAL(hfxenergy_critical)
1178 : shm_task_counter = shm_task_counter + 1
1179 : IF (shm_task_counter <= shm_total_bins) THEN
1180 : my_thread_id = shm_task_list(shm_task_counter)%thread_id
1181 : my_bin_id = shm_task_list(shm_task_counter)%bin_id
1182 : IF (.NOT. memory_parameter%do_all_on_the_fly) THEN
1183 : maxval_cache => x_data(irep, my_thread_id)%store_ints%maxval_cache(my_bin_id)
1184 : maxval_container => x_data(irep, my_thread_id)%store_ints%maxval_container(my_bin_id)
1185 : integral_caches => x_data(irep, my_thread_id)%store_ints%integral_caches(:, my_bin_id)
1186 : integral_containers => x_data(irep, my_thread_id)%store_ints%integral_containers(:, my_bin_id)
1187 : END IF
1188 : distribution_energy => x_data(irep, my_thread_id)%distribution_energy(my_bin_id)
1189 : do_it = .TRUE.
1190 : bins_left = .TRUE.
1191 : IF (my_geo_change) THEN
1192 : distribution_energy%ram_counter = HUGE(distribution_energy%ram_counter)
1193 : END IF
1194 : counter = 0_Int_8
1195 : ELSE
1196 : do_it = .FALSE.
1197 : bins_left = .FALSE.
1198 : END IF
1199 : !$OMP END CRITICAL(hfxenergy_critical)
1200 : END IF
1201 : IF (.NOT. do_it) CYCLE
1202 :
1203 : !$OMP MASTER
1204 : CALL timeset(routineN//"_bin", handle_bin)
1205 : !$OMP END MASTER
1206 :
1207 : bintime_start = m_walltime()
1208 : my_istart = distribution_energy%istart
1209 : my_current_counter = 0
1210 : IF (distribution_energy%number_of_atom_quartets == 0 .OR. &
1211 : my_istart == -1_int_8) my_istart = nblocks**4
1212 : atomic_blocks: DO atom_block = my_istart, nblocks**4 - 1, n_processes
1213 : latom_block = INT(MODULO(atom_block, nblocks)) + 1
1214 : tmp_block = atom_block/nblocks
1215 : katom_block = INT(MODULO(tmp_block, nblocks)) + 1
1216 : IF (latom_block < katom_block) CYCLE atomic_blocks
1217 : tmp_block = tmp_block/nblocks
1218 : jatom_block = INT(MODULO(tmp_block, nblocks)) + 1
1219 : tmp_block = tmp_block/nblocks
1220 : iatom_block = INT(MODULO(tmp_block, nblocks)) + 1
1221 : IF (jatom_block < iatom_block) CYCLE atomic_blocks
1222 : my_current_counter = my_current_counter + 1
1223 : IF (my_current_counter > distribution_energy%number_of_atom_quartets) EXIT atomic_blocks
1224 :
1225 : iatom_start = actual_x_data%blocks(iatom_block)%istart
1226 : iatom_end = actual_x_data%blocks(iatom_block)%iend
1227 : jatom_start = actual_x_data%blocks(jatom_block)%istart
1228 : jatom_end = actual_x_data%blocks(jatom_block)%iend
1229 : katom_start = actual_x_data%blocks(katom_block)%istart
1230 : katom_end = actual_x_data%blocks(katom_block)%iend
1231 : latom_start = actual_x_data%blocks(latom_block)%istart
1232 : latom_end = actual_x_data%blocks(latom_block)%iend
1233 :
1234 : pmax_blocks = MAX(shm_pmax_block(katom_block, iatom_block), &
1235 : shm_pmax_block(latom_block, jatom_block), &
1236 : shm_pmax_block(latom_block, iatom_block), &
1237 : shm_pmax_block(katom_block, jatom_block))
1238 :
1239 : IF (2.0_dp*coeffs_kind_max0 + pmax_blocks < log10_eps_schwarz) CYCLE atomic_blocks
1240 :
1241 : CALL build_pair_list(natom, list_ij, set_list_ij, iatom_start, iatom_end, &
1242 : jatom_start, jatom_end, &
1243 : kind_of, basis_parameter, particle_set, &
1244 : do_periodic, screen_coeffs_set, screen_coeffs_kind, &
1245 : coeffs_kind_max0, log10_eps_schwarz, cell, pmax_blocks, &
1246 : shm_atomic_pair_list)
1247 :
1248 : CALL build_pair_list(natom, list_kl, set_list_kl, katom_start, katom_end, &
1249 : latom_start, latom_end, &
1250 : kind_of, basis_parameter, particle_set, &
1251 : do_periodic, screen_coeffs_set, screen_coeffs_kind, &
1252 : coeffs_kind_max0, log10_eps_schwarz, cell, pmax_blocks, &
1253 : shm_atomic_pair_list)
1254 :
1255 : DO i_list_ij = 1, list_ij%n_element
1256 :
1257 : iatom = list_ij%elements(i_list_ij)%pair(1)
1258 : jatom = list_ij%elements(i_list_ij)%pair(2)
1259 : i_set_list_ij_start = list_ij%elements(i_list_ij)%set_bounds(1)
1260 : i_set_list_ij_stop = list_ij%elements(i_list_ij)%set_bounds(2)
1261 : ikind = list_ij%elements(i_list_ij)%kind_pair(1)
1262 : jkind = list_ij%elements(i_list_ij)%kind_pair(2)
1263 : ra = list_ij%elements(i_list_ij)%r1
1264 : rb = list_ij%elements(i_list_ij)%r2
1265 : rab2 = list_ij%elements(i_list_ij)%dist2
1266 :
1267 : la_max => basis_parameter(ikind)%lmax
1268 : la_min => basis_parameter(ikind)%lmin
1269 : npgfa => basis_parameter(ikind)%npgf
1270 : nseta = basis_parameter(ikind)%nset
1271 : zeta => basis_parameter(ikind)%zet
1272 : nsgfa => basis_parameter(ikind)%nsgf
1273 : sphi_a_ext => basis_parameter(ikind)%sphi_ext(:, :, :, :)
1274 : nsgfl_a => basis_parameter(ikind)%nsgfl
1275 : sphi_a_u1 = UBOUND(sphi_a_ext, 1)
1276 : sphi_a_u2 = UBOUND(sphi_a_ext, 2)
1277 : sphi_a_u3 = UBOUND(sphi_a_ext, 3)
1278 :
1279 : lb_max => basis_parameter(jkind)%lmax
1280 : lb_min => basis_parameter(jkind)%lmin
1281 : npgfb => basis_parameter(jkind)%npgf
1282 : nsetb = basis_parameter(jkind)%nset
1283 : zetb => basis_parameter(jkind)%zet
1284 : nsgfb => basis_parameter(jkind)%nsgf
1285 : sphi_b_ext => basis_parameter(jkind)%sphi_ext(:, :, :, :)
1286 : nsgfl_b => basis_parameter(jkind)%nsgfl
1287 : sphi_b_u1 = UBOUND(sphi_b_ext, 1)
1288 : sphi_b_u2 = UBOUND(sphi_b_ext, 2)
1289 : sphi_b_u3 = UBOUND(sphi_b_ext, 3)
1290 :
1291 : DO i_list_kl = 1, list_kl%n_element
1292 : katom = list_kl%elements(i_list_kl)%pair(1)
1293 : latom = list_kl%elements(i_list_kl)%pair(2)
1294 :
1295 : IF (.NOT. (katom + latom <= iatom + jatom)) CYCLE
1296 : IF (((iatom + jatom) == (katom + latom)) .AND. (katom < iatom)) CYCLE
1297 : i_set_list_kl_start = list_kl%elements(i_list_kl)%set_bounds(1)
1298 : i_set_list_kl_stop = list_kl%elements(i_list_kl)%set_bounds(2)
1299 : kkind = list_kl%elements(i_list_kl)%kind_pair(1)
1300 : lkind = list_kl%elements(i_list_kl)%kind_pair(2)
1301 : rc = list_kl%elements(i_list_kl)%r1
1302 : rd = list_kl%elements(i_list_kl)%r2
1303 : rcd2 = list_kl%elements(i_list_kl)%dist2
1304 :
1305 : IF (do_p_screening) THEN
1306 : pmax_atom = MAX(shm_pmax_atom(katom, iatom), &
1307 : shm_pmax_atom(latom, jatom), &
1308 : shm_pmax_atom(latom, iatom), &
1309 : shm_pmax_atom(katom, jatom))
1310 : ELSE
1311 : pmax_atom = 0.0_dp
1312 : END IF
1313 :
1314 : screen_kind_ij = screen_coeffs_kind(jkind, ikind)%x(1)*rab2 + &
1315 : screen_coeffs_kind(jkind, ikind)%x(2)
1316 : screen_kind_kl = screen_coeffs_kind(lkind, kkind)%x(1)*rcd2 + &
1317 : screen_coeffs_kind(lkind, kkind)%x(2)
1318 :
1319 : IF (screen_kind_ij + screen_kind_kl + pmax_atom < log10_eps_schwarz) CYCLE
1320 :
1321 : !! we want to be consistent with the KS matrix. If none of the atomic indices
1322 : !! is associated cycle
1323 : IF (.NOT. (shm_is_assoc_atomic_block(latom, iatom) >= 1 .AND. &
1324 : shm_is_assoc_atomic_block(katom, iatom) >= 1 .AND. &
1325 : shm_is_assoc_atomic_block(katom, jatom) >= 1 .AND. &
1326 : shm_is_assoc_atomic_block(latom, jatom) >= 1)) CYCLE
1327 :
1328 : !! calculate symmetry_factor according to degeneracy of atomic quartet
1329 : symm_fac = 0.5_dp
1330 : IF (iatom == jatom) symm_fac = symm_fac*2.0_dp
1331 : IF (katom == latom) symm_fac = symm_fac*2.0_dp
1332 : IF (iatom == katom .AND. jatom == latom .AND. iatom /= jatom .AND. katom /= latom) symm_fac = symm_fac*2.0_dp
1333 : IF (iatom == katom .AND. iatom == jatom .AND. katom == latom) symm_fac = symm_fac*2.0_dp
1334 : symm_fac = 1.0_dp/symm_fac
1335 :
1336 : lc_max => basis_parameter(kkind)%lmax
1337 : lc_min => basis_parameter(kkind)%lmin
1338 : npgfc => basis_parameter(kkind)%npgf
1339 : zetc => basis_parameter(kkind)%zet
1340 : nsgfc => basis_parameter(kkind)%nsgf
1341 : sphi_c_ext => basis_parameter(kkind)%sphi_ext(:, :, :, :)
1342 : nsgfl_c => basis_parameter(kkind)%nsgfl
1343 : sphi_c_u1 = UBOUND(sphi_c_ext, 1)
1344 : sphi_c_u2 = UBOUND(sphi_c_ext, 2)
1345 : sphi_c_u3 = UBOUND(sphi_c_ext, 3)
1346 :
1347 : ld_max => basis_parameter(lkind)%lmax
1348 : ld_min => basis_parameter(lkind)%lmin
1349 : npgfd => basis_parameter(lkind)%npgf
1350 : zetd => basis_parameter(lkind)%zet
1351 : nsgfd => basis_parameter(lkind)%nsgf
1352 : sphi_d_ext => basis_parameter(lkind)%sphi_ext(:, :, :, :)
1353 : nsgfl_d => basis_parameter(lkind)%nsgfl
1354 : sphi_d_u1 = UBOUND(sphi_d_ext, 1)
1355 : sphi_d_u2 = UBOUND(sphi_d_ext, 2)
1356 : sphi_d_u3 = UBOUND(sphi_d_ext, 3)
1357 :
1358 : ! Note: shm_atomic_block_offset is already symmetric
1359 : ! see get_atomic_block_maps in hfx_communication.F
1360 : atomic_offset_bd = shm_atomic_block_offset(jatom, latom)
1361 : atomic_offset_bc = shm_atomic_block_offset(jatom, katom)
1362 : atomic_offset_ad = shm_atomic_block_offset(iatom, latom)
1363 : atomic_offset_ac = shm_atomic_block_offset(iatom, katom)
1364 :
1365 : IF (jatom < latom) THEN
1366 : offset_bd_set => shm_set_offset(:, :, lkind, jkind)
1367 : ELSE
1368 : offset_bd_set => shm_set_offset(:, :, jkind, lkind)
1369 : END IF
1370 : IF (jatom < katom) THEN
1371 : offset_bc_set => shm_set_offset(:, :, kkind, jkind)
1372 : ELSE
1373 : offset_bc_set => shm_set_offset(:, :, jkind, kkind)
1374 : END IF
1375 : IF (iatom < latom) THEN
1376 : offset_ad_set => shm_set_offset(:, :, lkind, ikind)
1377 : ELSE
1378 : offset_ad_set => shm_set_offset(:, :, ikind, lkind)
1379 : END IF
1380 : IF (iatom < katom) THEN
1381 : offset_ac_set => shm_set_offset(:, :, kkind, ikind)
1382 : ELSE
1383 : offset_ac_set => shm_set_offset(:, :, ikind, kkind)
1384 : END IF
1385 :
1386 : IF (do_p_screening) THEN
1387 : swap_id = 0
1388 : kind_kind_idx = INT(get_1D_idx(kkind, ikind, INT(nkind, int_8)))
1389 : IF (ikind >= kkind) THEN
1390 : ptr_p_1 => shm_initial_p(kind_kind_idx)%p_kind(:, :, &
1391 : actual_x_data%map_atom_to_kind_atom(katom), &
1392 : actual_x_data%map_atom_to_kind_atom(iatom))
1393 : ELSE
1394 : ptr_p_1 => shm_initial_p(kind_kind_idx)%p_kind(:, :, &
1395 : actual_x_data%map_atom_to_kind_atom(iatom), &
1396 : actual_x_data%map_atom_to_kind_atom(katom))
1397 : swap_id = swap_id + 1
1398 : END IF
1399 : kind_kind_idx = INT(get_1D_idx(lkind, jkind, INT(nkind, int_8)))
1400 : IF (jkind >= lkind) THEN
1401 : ptr_p_2 => shm_initial_p(kind_kind_idx)%p_kind(:, :, &
1402 : actual_x_data%map_atom_to_kind_atom(latom), &
1403 : actual_x_data%map_atom_to_kind_atom(jatom))
1404 : ELSE
1405 : ptr_p_2 => shm_initial_p(kind_kind_idx)%p_kind(:, :, &
1406 : actual_x_data%map_atom_to_kind_atom(jatom), &
1407 : actual_x_data%map_atom_to_kind_atom(latom))
1408 : swap_id = swap_id + 2
1409 : END IF
1410 : kind_kind_idx = INT(get_1D_idx(lkind, ikind, INT(nkind, int_8)))
1411 : IF (ikind >= lkind) THEN
1412 : ptr_p_3 => shm_initial_p(kind_kind_idx)%p_kind(:, :, &
1413 : actual_x_data%map_atom_to_kind_atom(latom), &
1414 : actual_x_data%map_atom_to_kind_atom(iatom))
1415 : ELSE
1416 : ptr_p_3 => shm_initial_p(kind_kind_idx)%p_kind(:, :, &
1417 : actual_x_data%map_atom_to_kind_atom(iatom), &
1418 : actual_x_data%map_atom_to_kind_atom(latom))
1419 : swap_id = swap_id + 4
1420 : END IF
1421 : kind_kind_idx = INT(get_1D_idx(kkind, jkind, INT(nkind, int_8)))
1422 : IF (jkind >= kkind) THEN
1423 : ptr_p_4 => shm_initial_p(kind_kind_idx)%p_kind(:, :, &
1424 : actual_x_data%map_atom_to_kind_atom(katom), &
1425 : actual_x_data%map_atom_to_kind_atom(jatom))
1426 : ELSE
1427 : ptr_p_4 => shm_initial_p(kind_kind_idx)%p_kind(:, :, &
1428 : actual_x_data%map_atom_to_kind_atom(jatom), &
1429 : actual_x_data%map_atom_to_kind_atom(katom))
1430 : swap_id = swap_id + 8
1431 : END IF
1432 : END IF
1433 :
1434 : !! At this stage, check for memory used in compression
1435 : IF (my_geo_change) THEN
1436 : IF (.NOT. memory_parameter%do_all_on_the_fly) THEN
1437 : ! ** We know the maximum amount of integrals that we can store per MPI-process
1438 : ! ** Now we need to sum the current memory usage among all openMP threads
1439 : ! ** We can just read what is currently stored on the corresponding x_data type
1440 : ! ** If this is thread i and it tries to read the data from thread j, that is
1441 : ! ** currently writing that data, we just dont care, because the possible error
1442 : ! ** is of the order of CACHE_SIZE
1443 : mem_compression_counter = 0
1444 : DO i = 1, n_threads
1445 : !$OMP ATOMIC READ
1446 : tmp_i4 = x_data(irep, i)%memory_parameter%actual_memory_usage
1447 : mem_compression_counter = mem_compression_counter + &
1448 : tmp_i4*memory_parameter%cache_size
1449 : END DO
1450 : IF (mem_compression_counter > memory_parameter%max_compression_counter) THEN
1451 : buffer_overflow = .TRUE.
1452 : IF (do_dynamic_load_balancing) THEN
1453 : distribution_energy%ram_counter = counter
1454 : ELSE
1455 : memory_parameter%ram_counter = counter
1456 : END IF
1457 : ELSE
1458 : counter = counter + 1
1459 : buffer_overflow = .FALSE.
1460 : END IF
1461 : END IF
1462 : ELSE
1463 : IF (.NOT. memory_parameter%do_all_on_the_fly) THEN
1464 : IF (do_dynamic_load_balancing) THEN
1465 : IF (distribution_energy%ram_counter == counter) THEN
1466 : buffer_overflow = .TRUE.
1467 : ELSE
1468 : counter = counter + 1
1469 : buffer_overflow = .FALSE.
1470 : END IF
1471 :
1472 : ELSE
1473 : IF (memory_parameter%ram_counter == counter) THEN
1474 : buffer_overflow = .TRUE.
1475 : ELSE
1476 : counter = counter + 1
1477 : buffer_overflow = .FALSE.
1478 : END IF
1479 : END IF
1480 : END IF
1481 : END IF
1482 :
1483 : IF (buffer_overflow .AND. do_disk_storage) THEN
1484 : use_disk_storage = .TRUE.
1485 : buffer_overflow = .FALSE.
1486 : END IF
1487 :
1488 : IF (use_disk_storage) THEN
1489 : !$OMP ATOMIC READ
1490 : tmp_i4 = memory_parameter%actual_memory_usage_disk
1491 : mem_compression_counter_disk = tmp_i4*memory_parameter%cache_size
1492 : IF (mem_compression_counter_disk > memory_parameter%max_compression_counter_disk) THEN
1493 : buffer_overflow = .TRUE.
1494 : use_disk_storage = .FALSE.
1495 : END IF
1496 : END IF
1497 :
1498 : DO i_set_list_ij = i_set_list_ij_start, i_set_list_ij_stop
1499 : iset = set_list_ij(i_set_list_ij)%pair(1)
1500 : jset = set_list_ij(i_set_list_ij)%pair(2)
1501 :
1502 : ncob = npgfb(jset)*ncoset(lb_max(jset))
1503 : max_val1 = screen_coeffs_set(jset, iset, jkind, ikind)%x(1)*rab2 + &
1504 : screen_coeffs_set(jset, iset, jkind, ikind)%x(2)
1505 :
1506 : IF (max_val1 + screen_kind_kl + pmax_atom < log10_eps_schwarz) CYCLE
1507 :
1508 : DO i_set_list_kl = i_set_list_kl_start, i_set_list_kl_stop
1509 : kset = set_list_kl(i_set_list_kl)%pair(1)
1510 : lset = set_list_kl(i_set_list_kl)%pair(2)
1511 :
1512 : max_val2_set = (screen_coeffs_set(lset, kset, lkind, kkind)%x(1)*rcd2 + &
1513 : screen_coeffs_set(lset, kset, lkind, kkind)%x(2))
1514 : max_val2 = max_val1 + max_val2_set
1515 :
1516 : !! Near field screening
1517 : IF (max_val2 + pmax_atom < log10_eps_schwarz) CYCLE
1518 : !! get max_vals if we screen on initial density
1519 : IF (do_p_screening) THEN
1520 : CALL get_pmax_val(ptr_p_1, ptr_p_2, ptr_p_3, ptr_p_4, &
1521 : iset, jset, kset, lset, &
1522 : pmax_entry, swap_id)
1523 : ELSE
1524 : pmax_entry = 0.0_dp
1525 : END IF
1526 : log10_pmax = pmax_entry
1527 : max_val2 = max_val2 + log10_pmax
1528 : IF (max_val2 < log10_eps_schwarz) CYCLE
1529 : pmax_entry = EXP(log10_pmax*ln_10)
1530 :
1531 : ! libGint can only run in direct mode at the moment
1532 : ! so there is no need to check the buffers
1533 : if (use_libGint) then
1534 : tmp_screen_pgf1 => screen_coeffs_pgf(:, :, jset, iset, jkind, ikind)
1535 : tmp_screen_pgf2 => screen_coeffs_pgf(:, :, lset, kset, lkind, kkind)
1536 : ! TODO a more modern approach in which the call is more like
1537 : ! call coulom_4_gpu( HFX_data, iset, jset, kset, lset )
1538 : call libGint_coulomb4( &
1539 : iatom, jatom, katom, latom, iset, jset, kset, lset, &
1540 : ra, rb, rc, rd, &
1541 : npgfa(iset), npgfb(jset), npgfc(kset), npgfd(lset), &
1542 : potential_parameter, &
1543 : screen_coeffs_set(jset, iset, jkind, ikind)%x, &
1544 : screen_coeffs_set(lset, kset, lkind, kkind)%x, &
1545 : log10_pmax, log10_eps_schwarz, &
1546 : tmp_screen_pgf1, tmp_screen_pgf2, &
1547 : actual_x_data%neighbor_cells, cell, do_periodic, screened)
1548 :
1549 : if (.not. screened) then
1550 :
1551 : ! TODO a more modern approach in which the call is more like
1552 : ! call update_fock_matrix_gpu( HFX_data, iset, jset, kset, lset )
1553 : call libGint_update_fock_matrix( &
1554 : symm_fac, &
1555 : iatom, jatom, katom, latom, &
1556 : iset, jset, kset, lset, &
1557 : atomic_offset_ac, atomic_offset_ad, atomic_offset_bc, atomic_offset_bd, &
1558 : offset_ac_set, offset_ad_set, offset_bc_set, offset_bd_set, &
1559 : nsgfa(iset), nsgfb(jset), nsgfc(kset), nsgfd(lset), &
1560 : la_min(iset), la_max(iset), lb_min(jset), lb_max(jset), &
1561 : lc_min(kset), lc_max(kset), ld_min(lset), ld_max(lset), &
1562 : nsgfl_a, nsgfl_b, nsgfl_c, nsgfl_d)
1563 :
1564 : end if
1565 : end if
1566 : ! Run using libint as integral engine
1567 : if_use_libint: if (use_libint) then
1568 : !! store current number of integrals, update total number and number of integrals in buffer
1569 : current_counter = nsgfa(iset)*nsgfb(jset)*nsgfc(kset)*nsgfd(lset)
1570 : IF (buffer_overflow) THEN
1571 : neris_onthefly = neris_onthefly + current_counter
1572 : END IF
1573 :
1574 : !! Get integrals from buffer and update Kohn-Sham matrix
1575 : IF (.NOT. buffer_overflow .AND. .NOT. my_geo_change) THEN
1576 : nints = current_counter
1577 : IF (.NOT. use_disk_storage) THEN
1578 : CALL hfx_get_single_cache_element( &
1579 : estimate_to_store_int, 6, &
1580 : maxval_cache, maxval_container, memory_parameter%actual_memory_usage, &
1581 : use_disk_storage)
1582 : ELSE
1583 : CALL hfx_get_single_cache_element( &
1584 : estimate_to_store_int, 6, &
1585 : maxval_cache_disk, maxval_container_disk, memory_parameter%actual_memory_usage_disk, &
1586 : use_disk_storage)
1587 : END IF
1588 : spherical_estimate = SET_EXPONENT(1.0_dp, estimate_to_store_int + 1)
1589 : IF (spherical_estimate*pmax_entry < eps_schwarz) CYCLE
1590 : nbits = EXPONENT(ANINT(spherical_estimate*pmax_entry/eps_storage)) + 1
1591 : buffer_left = nints
1592 : buffer_start = 1
1593 : IF (.NOT. use_disk_storage) THEN
1594 : neris_incore = neris_incore + INT(nints, int_8)
1595 : ELSE
1596 : neris_disk = neris_disk + INT(nints, int_8)
1597 : END IF
1598 : DO WHILE (buffer_left > 0)
1599 : buffer_size = MIN(buffer_left, cache_size)
1600 : IF (.NOT. use_disk_storage) THEN
1601 : CALL hfx_get_mult_cache_elements(primitive_integrals(buffer_start), &
1602 : buffer_size, nbits, &
1603 : integral_caches(nbits), &
1604 : integral_containers(nbits), &
1605 : eps_storage, pmax_entry, &
1606 : memory_parameter%actual_memory_usage, &
1607 : use_disk_storage)
1608 : ELSE
1609 : CALL hfx_get_mult_cache_elements(primitive_integrals(buffer_start), &
1610 : buffer_size, nbits, &
1611 : integral_caches_disk(nbits), &
1612 : integral_containers_disk(nbits), &
1613 : eps_storage, pmax_entry, &
1614 : memory_parameter%actual_memory_usage_disk, &
1615 : use_disk_storage)
1616 : END IF
1617 : buffer_left = buffer_left - buffer_size
1618 : buffer_start = buffer_start + buffer_size
1619 : END DO
1620 : END IF
1621 : !! Calculate integrals if we run out of buffer or the geometry did change
1622 : IF (my_geo_change .OR. buffer_overflow) THEN
1623 : sphi_a_ext_set => sphi_a_ext(:, :, :, iset)
1624 : sphi_b_ext_set => sphi_b_ext(:, :, :, jset)
1625 : sphi_c_ext_set => sphi_c_ext(:, :, :, kset)
1626 : sphi_d_ext_set => sphi_d_ext(:, :, :, lset)
1627 :
1628 : max_contraction_val = max_contraction(iset, iatom)* &
1629 : max_contraction(jset, jatom)* &
1630 : max_contraction(kset, katom)* &
1631 : max_contraction(lset, latom)*pmax_entry
1632 : tmp_R_1 => radii_pgf(:, :, jset, iset, jkind, ikind)
1633 : tmp_R_2 => radii_pgf(:, :, lset, kset, lkind, kkind)
1634 : tmp_screen_pgf1 => screen_coeffs_pgf(:, :, jset, iset, jkind, ikind)
1635 : tmp_screen_pgf2 => screen_coeffs_pgf(:, :, lset, kset, lkind, kkind)
1636 :
1637 : CALL coulomb4(private_context, ra, rb, rc, rd, npgfa(iset), npgfb(jset), npgfc(kset), npgfd(lset), &
1638 : la_min(iset), la_max(iset), lb_min(jset), lb_max(jset), &
1639 : lc_min(kset), lc_max(kset), ld_min(lset), ld_max(lset), &
1640 : nsgfa(iset), nsgfb(jset), nsgfc(kset), nsgfd(lset), &
1641 : sphi_a_u1, sphi_a_u2, sphi_a_u3, &
1642 : sphi_b_u1, sphi_b_u2, sphi_b_u3, &
1643 : sphi_c_u1, sphi_c_u2, sphi_c_u3, &
1644 : sphi_d_u1, sphi_d_u2, sphi_d_u3, &
1645 : zeta(1:npgfa(iset), iset), zetb(1:npgfb(jset), jset), &
1646 : zetc(1:npgfc(kset), kset), zetd(1:npgfd(lset), lset), &
1647 : primitive_integrals, &
1648 : potential_parameter, &
1649 : actual_x_data%neighbor_cells, screen_coeffs_set(jset, iset, jkind, ikind)%x, &
1650 : screen_coeffs_set(lset, kset, lkind, kkind)%x, eps_schwarz, &
1651 : max_contraction_val, cartesian_estimate, cell, neris_tmp, &
1652 : log10_pmax, log10_eps_schwarz, &
1653 : tmp_R_1, tmp_R_2, tmp_screen_pgf1, tmp_screen_pgf2, &
1654 : pgf_list_ij, pgf_list_kl, pgf_product_list, &
1655 : nsgfl_a(:, iset), nsgfl_b(:, jset), &
1656 : nsgfl_c(:, kset), nsgfl_d(:, lset), &
1657 : sphi_a_ext_set, &
1658 : sphi_b_ext_set, &
1659 : sphi_c_ext_set, &
1660 : sphi_d_ext_set, &
1661 : ee_work, ee_work2, ee_buffer1, ee_buffer2, ee_primitives_tmp, &
1662 : nimages, do_periodic)
1663 :
1664 : nints = nsgfa(iset)*nsgfb(jset)*nsgfc(kset)*nsgfd(lset)
1665 : neris_total = neris_total + nints
1666 : nprim_ints = nprim_ints + neris_tmp
1667 :
1668 : !! Compress the array for storage
1669 : spherical_estimate = 0.0_dp
1670 : IF (cartesian_estimate /= 0.0_dp) THEN
1671 : DO i = 1, nints
1672 : spherical_estimate = MAX(spherical_estimate, ABS(primitive_integrals(i)))
1673 : END DO
1674 : ELSE IF (buffer_overflow) THEN
1675 : CYCLE
1676 : END IF
1677 :
1678 : IF (spherical_estimate == 0.0_dp) spherical_estimate = TINY(spherical_estimate)
1679 : estimate_to_store_int = EXPONENT(spherical_estimate)
1680 : estimate_to_store_int = MAX(estimate_to_store_int, -15_int_8)
1681 :
1682 : IF (.NOT. buffer_overflow .AND. my_geo_change) THEN
1683 : IF (.NOT. use_disk_storage) THEN
1684 : CALL hfx_add_single_cache_element( &
1685 : estimate_to_store_int, 6, &
1686 : maxval_cache, maxval_container, memory_parameter%actual_memory_usage, &
1687 : use_disk_storage, max_val_memory)
1688 : ELSE
1689 : CALL hfx_add_single_cache_element( &
1690 : estimate_to_store_int, 6, &
1691 : maxval_cache_disk, maxval_container_disk, memory_parameter%actual_memory_usage_disk, &
1692 : use_disk_storage)
1693 : END IF
1694 : END IF
1695 : spherical_estimate = SET_EXPONENT(1.0_dp, estimate_to_store_int + 1)
1696 : IF (spherical_estimate*pmax_entry < eps_schwarz) CYCLE
1697 : IF (.NOT. buffer_overflow) THEN
1698 : nbits = EXPONENT(ANINT(spherical_estimate*pmax_entry/eps_storage)) + 1
1699 :
1700 : ! In case of a tight eps_storage threshold the number of significant
1701 : ! bits in the integer number NINT(value*pmax_entry/eps_storage) may
1702 : ! exceed the width of the storage element. As the compression algorithm
1703 : ! is designed for IEEE 754 double precision numbers, a 64-bit signed
1704 : ! integer variable which is used to store the result of this float-to-
1705 : ! integer conversion (we have no wish to use more memory for storing
1706 : ! compressed ERIs than it is needed for uncompressed ERIs) may overflow.
1707 : ! Abort with a meaningful message when it happens.
1708 : !
1709 : ! The magic number 63 stands for the number of magnitude bits
1710 : ! (64 bits minus one sign bit).
1711 : IF (nbits > 63) THEN
1712 : WRITE (eps_schwarz_min_str, '(ES10.3E2)') &
1713 : spherical_estimate*pmax_entry/ &
1714 : (SET_EXPONENT(1.0_dp, 63)*memory_parameter%eps_storage_scaling)
1715 :
1716 : WRITE (eps_scaling_str, '(ES10.3E2)') &
1717 : spherical_estimate*pmax_entry/(SET_EXPONENT(1.0_dp, 63)*eps_schwarz)
1718 :
1719 : CALL cp_abort(__LOCATION__, &
1720 : "Overflow during ERI's compression. Please use a larger "// &
1721 : "EPS_SCHWARZ threshold (above "//TRIM(ADJUSTL(eps_schwarz_min_str))// &
1722 : ") or increase the EPS_STORAGE_SCALING factor above "// &
1723 : TRIM(ADJUSTL(eps_scaling_str))//".")
1724 : END IF
1725 :
1726 : buffer_left = nints
1727 : buffer_start = 1
1728 : IF (.NOT. use_disk_storage) THEN
1729 : neris_incore = neris_incore + INT(nints, int_8)
1730 : ELSE
1731 : neris_disk = neris_disk + INT(nints, int_8)
1732 : END IF
1733 : DO WHILE (buffer_left > 0)
1734 : buffer_size = MIN(buffer_left, CACHE_SIZE)
1735 : IF (.NOT. use_disk_storage) THEN
1736 : CALL hfx_add_mult_cache_elements(primitive_integrals(buffer_start), &
1737 : buffer_size, nbits, &
1738 : integral_caches(nbits), &
1739 : integral_containers(nbits), &
1740 : eps_storage, pmax_entry, &
1741 : memory_parameter%actual_memory_usage, &
1742 : use_disk_storage)
1743 : ELSE
1744 : CALL hfx_add_mult_cache_elements(primitive_integrals(buffer_start), &
1745 : buffer_size, nbits, &
1746 : integral_caches_disk(nbits), &
1747 : integral_containers_disk(nbits), &
1748 : eps_storage, pmax_entry, &
1749 : memory_parameter%actual_memory_usage_disk, &
1750 : use_disk_storage)
1751 : END IF
1752 : buffer_left = buffer_left - buffer_size
1753 : buffer_start = buffer_start + buffer_size
1754 : END DO
1755 : ELSE
1756 : !! In order to be consistent with in-core part, round all the eris wrt. eps_schwarz
1757 : DO i = 1, nints
1758 : primitive_integrals(i) = primitive_integrals(i)*pmax_entry
1759 : IF (ABS(primitive_integrals(i)) > eps_storage) THEN
1760 : primitive_integrals(i) = ANINT(primitive_integrals(i)/eps_storage, dp)*eps_storage/pmax_entry
1761 : ELSE
1762 : primitive_integrals(i) = 0.0_dp
1763 : END IF
1764 : END DO
1765 : END IF
1766 : IF (cartesian_estimate == 0.0_dp) CYCLE
1767 : END IF
1768 : !!! DEBUG, print out primitive integrals and indices. Only works serial no OMP !!!
1769 : IF (.FALSE.) THEN
1770 : CALL print_integrals( &
1771 : iatom, jatom, katom, latom, shm_set_offset, shm_atomic_block_offset, &
1772 : iset, jset, kset, lset, nsgfa(iset), nsgfb(jset), nsgfc(kset), nsgfd(lset), primitive_integrals)
1773 : END IF
1774 : IF (.NOT. is_anti_symmetric) THEN
1775 : !! Update Kohn-Sham matrix
1776 : CALL update_fock_matrix( &
1777 : nsgfa(iset), nsgfb(jset), nsgfc(kset), nsgfd(lset), &
1778 : fac, symm_fac, full_density_alpha(:, 1), full_ks_alpha(:, 1), &
1779 : primitive_integrals, pbd_buf, pbc_buf, pad_buf, pac_buf, kbd_buf, &
1780 : kbc_buf, kad_buf, kac_buf, iatom, jatom, katom, latom, &
1781 : iset, jset, kset, lset, offset_bd_set, offset_bc_set, offset_ad_set, offset_ac_set, &
1782 : atomic_offset_bd, atomic_offset_bc, atomic_offset_ad, atomic_offset_ac)
1783 : IF (.NOT. treat_lsd_in_core) THEN
1784 : IF (my_nspins == 2) THEN
1785 : CALL update_fock_matrix( &
1786 : nsgfa(iset), nsgfb(jset), nsgfc(kset), nsgfd(lset), &
1787 : fac, symm_fac, full_density_beta(:, 1), full_ks_beta(:, 1), &
1788 : primitive_integrals, pbd_buf, pbc_buf, pad_buf, pac_buf, kbd_buf, &
1789 : kbc_buf, kad_buf, kac_buf, iatom, jatom, katom, latom, &
1790 : iset, jset, kset, lset, offset_bd_set, offset_bc_set, offset_ad_set, offset_ac_set, &
1791 : atomic_offset_bd, atomic_offset_bc, atomic_offset_ad, atomic_offset_ac)
1792 : END IF
1793 : END IF
1794 : ELSE
1795 : !! Update Kohn-Sham matrix
1796 : CALL update_fock_matrix_as( &
1797 : nsgfa(iset), nsgfb(jset), nsgfc(kset), nsgfd(lset), &
1798 : fac, symm_fac, full_density_alpha(:, 1), full_ks_alpha(:, 1), &
1799 : primitive_integrals, pbd_buf, pbc_buf, pad_buf, pac_buf, kbd_buf, &
1800 : kbc_buf, kad_buf, kac_buf, iatom, jatom, katom, latom, &
1801 : iset, jset, kset, lset, offset_bd_set, offset_bc_set, offset_ad_set, offset_ac_set, &
1802 : atomic_offset_bd, atomic_offset_bc, atomic_offset_ad, atomic_offset_ac)
1803 : IF (.NOT. treat_lsd_in_core) THEN
1804 : IF (my_nspins == 2) THEN
1805 : CALL update_fock_matrix_as( &
1806 : nsgfa(iset), nsgfb(jset), nsgfc(kset), nsgfd(lset), &
1807 : fac, symm_fac, full_density_beta(:, 1), full_ks_beta(:, 1), &
1808 : primitive_integrals, pbd_buf, pbc_buf, pad_buf, pac_buf, kbd_buf, &
1809 : kbc_buf, kad_buf, kac_buf, iatom, jatom, katom, latom, &
1810 : iset, jset, kset, lset, offset_bd_set, offset_bc_set, offset_ad_set, offset_ac_set, &
1811 : atomic_offset_bd, atomic_offset_bc, atomic_offset_ad, atomic_offset_ac)
1812 : END IF
1813 : END IF
1814 : END IF
1815 : END IF if_use_libint
1816 : END DO ! i_set_list_kl
1817 : END DO ! i_set_list_ij
1818 : IF (do_disk_storage) THEN
1819 : buffer_overflow = .TRUE.
1820 : END IF
1821 : END DO !i_list_ij
1822 : END DO !i_list_kl
1823 : END DO atomic_blocks
1824 : bintime_stop = m_walltime()
1825 : IF (my_geo_change) THEN
1826 : distribution_energy%time_first_scf = bintime_stop - bintime_start
1827 : ELSE
1828 : distribution_energy%time_other_scf = &
1829 : distribution_energy%time_other_scf + bintime_stop - bintime_start
1830 : END IF
1831 : !$OMP MASTER
1832 : CALL timestop(handle_bin)
1833 : !$OMP END MASTER
1834 : END DO !bin
1835 :
1836 : !!!
1837 : ! IF running both libint and libGint, uses the values from libint to check libGint values
1838 : IF (use_libGint .and. use_libint) THEN
1839 : ! barrier needed after allocate full_ks_alpha(beta)_from_gpu before libGint get K
1840 : ! to feed libgint a valid position for full_ks_alpha(beta)_from_gpu
1841 : ALLOCATE (full_ks_alpha_from_gpu, Mold=full_ks_alpha)
1842 : !$OMP BARRIER
1843 : IF (use_only_alpha_spin) THEN
1844 : call libGint_get_fock_matrix(full_ks_alpha_from_gpu)
1845 : ELSE
1846 : ALLOCATE (full_ks_beta_from_gpu, Mold=full_ks_beta)
1847 : !$OMP BARRIER
1848 : CALL libGint_get_fock_matrix(full_ks_alpha_from_gpu, full_ks_beta_from_gpu)
1849 : END IF
1850 :
1851 : !$OMP BARRIER
1852 : !$omp single
1853 : max_abs_delta_KS = 0.0_dp
1854 : DO i = 1, SIZE(full_ks_alpha)
1855 : max_abs_delta_KS = MAX(max_abs_delta_ks, ABS(full_ks_alpha(i, 1) - full_ks_alpha_from_gpu(i, 1)))
1856 : END DO
1857 : IF (iw > 0) THEN
1858 : WRITE (UNIT=iw, FMT=*) " ------------------------------------ "
1859 : WRITE (UNIT=iw, FMT=*) " | rank ", para_env%mepos
1860 : WRITE (UNIT=iw, FMT=*) " | max abs dif: ", max_abs_delta_KS
1861 : WRITE (UNIT=iw, FMT=*) " ------------------------------------ "
1862 : END IF
1863 :
1864 : ! Print all F if delta is too large or (suspiciously) too small
1865 : IF (iw > 0) THEN
1866 : if (max_abs_delta_KS > 100.0_dp*eps_schwarz .or. max_abs_delta_KS < 1.0E-20_dp) THEN
1867 : WRITE (UNIT=iw, FMT=*) " K from CPU | K from GPU | Delta | Ratio "
1868 : DO i = 1, SIZE(full_ks_alpha)
1869 : write (UNIT=iw, FMT=*) i, full_ks_alpha(i, 1), full_ks_alpha_from_gpu(i, 1), &
1870 : full_ks_alpha(i, 1) - full_ks_alpha_from_gpu(i, 1), &
1871 : full_ks_alpha(i, 1)/full_ks_alpha_from_gpu(i, 1)
1872 : END DO
1873 : END IF
1874 : END IF
1875 :
1876 : IF (.NOT. use_only_alpha_spin) THEN
1877 : max_abs_delta_KS = 0.0_dp
1878 : DO i = 1, size(full_ks_beta)
1879 : max_abs_delta_KS = max(max_abs_delta_ks, abs(full_ks_beta(i, 1) - full_ks_beta_from_gpu(i, 1)))
1880 : END DO
1881 : IF (iw > 0) THEN
1882 : WRITE (UNIT=iw, FMT=*) " ------------------------------------ "
1883 : WRITE (UNIT=iw, FMT=*) " | rank ", para_env%mepos
1884 : WRITE (UNIT=iw, FMT=*) " | max abs dif B: ", max_abs_delta_KS
1885 : WRITE (UNIT=iw, FMT=*) " ------------------------------------ "
1886 : END IF
1887 :
1888 : ! Print all F if delta is too large or (suspiciously) too small
1889 : IF (max_abs_delta_KS > 100.0_dp*eps_schwarz .or. max_abs_delta_KS < 1.0E-18_dp) THEN
1890 : IF (iw > 0) THEN
1891 : WRITE (UNIT=iw, FMT=*) " BETA K from CPU | K from GPU | Delta | Ratio "
1892 : DO i = 1, size(full_ks_beta)
1893 : WRITE (UNIT=iw, FMT=*) i, full_ks_beta(i, 1), full_ks_beta_from_gpu(i, 1), &
1894 : full_ks_beta(i, 1) - full_ks_beta_from_gpu(i, 1), &
1895 : full_ks_beta(i, 1)/full_ks_beta_from_gpu(i, 1)
1896 : END DO
1897 : END IF
1898 : END IF
1899 : END IF
1900 : !$omp end single
1901 : END IF
1902 :
1903 : ! Copy the KS matrix from device to host.
1904 : IF (use_libGint .AND. (.NOT. use_libint)) THEN
1905 : IF (use_only_alpha_spin) THEN
1906 : CALL libGint_get_fock_matrix(full_ks_alpha)
1907 : ELSE
1908 : CALL libGint_get_fock_matrix(full_ks_alpha, full_ks_beta)
1909 : END IF
1910 : END IF
1911 :
1912 : !$OMP MASTER
1913 : logger => cp_get_default_logger()
1914 : do_print_load_balance_info = .FALSE.
1915 : do_print_load_balance_info = BTEST(cp_print_key_should_output(logger%iter_info, hfx_section, &
1916 : "LOAD_BALANCE%PRINT/LOAD_BALANCE_INFO"), cp_p_file)
1917 : !$OMP END MASTER
1918 : !$OMP BARRIER
1919 : IF (do_print_load_balance_info) THEN
1920 : iw = -1
1921 : !$OMP MASTER
1922 : iw = cp_print_key_unit_nr(logger, hfx_section, "LOAD_BALANCE%PRINT/LOAD_BALANCE_INFO", &
1923 : extension=".scfLog")
1924 : !$OMP END MASTER
1925 :
1926 : CALL collect_load_balance_info(para_env, actual_x_data, iw, n_threads, i_thread, &
1927 : hfx_do_eval_energy)
1928 :
1929 : !$OMP MASTER
1930 : CALL cp_print_key_finished_output(iw, logger, hfx_section, &
1931 : "LOAD_BALANCE%PRINT/LOAD_BALANCE_INFO")
1932 : !$OMP END MASTER
1933 : END IF
1934 :
1935 : !$OMP BARRIER
1936 : !$OMP MASTER
1937 : CALL m_memory(memsize_after)
1938 : !$OMP END MASTER
1939 : !$OMP BARRIER
1940 :
1941 : DEALLOCATE (primitive_integrals)
1942 : !$OMP BARRIER
1943 : !! Get some number about ERIS
1944 : !$OMP ATOMIC
1945 : shm_neris_total = shm_neris_total + neris_total
1946 : !$OMP ATOMIC
1947 : shm_neris_onthefly = shm_neris_onthefly + neris_onthefly
1948 : !$OMP ATOMIC
1949 : shm_nprim_ints = shm_nprim_ints + nprim_ints
1950 :
1951 : storage_counter_integrals = memory_parameter%actual_memory_usage* &
1952 : memory_parameter%cache_size
1953 : stor_count_int_disk = memory_parameter%actual_memory_usage_disk* &
1954 : memory_parameter%cache_size
1955 : stor_count_max_val = max_val_memory*memory_parameter%cache_size
1956 : !$OMP ATOMIC
1957 : shm_storage_counter_integrals = shm_storage_counter_integrals + storage_counter_integrals
1958 : !$OMP ATOMIC
1959 : shm_stor_count_int_disk = shm_stor_count_int_disk + stor_count_int_disk
1960 : !$OMP ATOMIC
1961 : shm_neris_incore = shm_neris_incore + neris_incore
1962 : !$OMP ATOMIC
1963 : shm_neris_disk = shm_neris_disk + neris_disk
1964 : !$OMP ATOMIC
1965 : shm_stor_count_max_val = shm_stor_count_max_val + stor_count_max_val
1966 : !$OMP BARRIER
1967 :
1968 : ! ** Calculate how much memory has already been used (might be needed for in-core forces
1969 : !$OMP MASTER
1970 : shm_mem_compression_counter = 0
1971 : DO i = 1, n_threads
1972 : !$OMP ATOMIC READ
1973 : tmp_i4 = x_data(irep, i)%memory_parameter%actual_memory_usage
1974 : shm_mem_compression_counter = shm_mem_compression_counter + &
1975 : tmp_i4*memory_parameter%cache_size
1976 : END DO
1977 : !$OMP END MASTER
1978 : !$OMP BARRIER
1979 : actual_x_data%memory_parameter%final_comp_counter_energy = shm_mem_compression_counter
1980 :
1981 : !$OMP MASTER
1982 : !! Calculate the exchange energies from the Kohn-Sham matrix. Before we can go on, we have to symmetrize.
1983 : ene_x_aa = 0.0_dp
1984 : ene_x_bb = 0.0_dp
1985 :
1986 : mb_size_p = shm_block_offset(ncpu + 1)/1024/128
1987 : mb_size_f = shm_block_offset(ncpu + 1)/1024/128
1988 : IF (.NOT. treat_lsd_in_core) THEN
1989 : IF (my_nspins == 2) THEN
1990 : mb_size_f = mb_size_f*2
1991 : mb_size_p = mb_size_p*2
1992 : END IF
1993 : END IF
1994 : !! size of primitive_integrals(not shared)
1995 : mb_size_buffers = INT(nsgf_max, int_8)**4*n_threads
1996 : !! fock density buffers (not shared)
1997 : mb_size_buffers = mb_size_buffers + INT(nsgf_max, int_8)**2*n_threads
1998 : subtr_size_mb = subtr_size_mb + 8_int_8*nsgf_max**2*n_threads
1999 : !! size of screening functions (shared)
2000 : mb_size_buffers = mb_size_buffers + max_pgf**2*max_set**2*nkind**2 &
2001 : + max_set**2*nkind**2 &
2002 : + nkind**2 &
2003 : + max_pgf**2*max_set**2*nkind**2
2004 : !! is_assoc (shared)
2005 : mb_size_buffers = mb_size_buffers + natom**2
2006 : ! ** pmax_atom (shared)
2007 : IF (do_p_screening) THEN
2008 : mb_size_buffers = mb_size_buffers + natom**2
2009 : END IF
2010 : IF (screening_parameter%do_p_screening_forces) THEN
2011 : IF (memory_parameter%treat_forces_in_core) THEN
2012 : mb_size_buffers = mb_size_buffers + natom**2
2013 : END IF
2014 : END IF
2015 : ! ** Initial P only MAX(alpha,beta) (shared)
2016 : IF (do_p_screening .OR. screening_parameter%do_p_screening_forces) THEN
2017 : mb_size_buffers = mb_size_buffers + memory_parameter%size_p_screen
2018 : END IF
2019 : ! ** In core forces require their own initial P
2020 : IF (screening_parameter%do_p_screening_forces) THEN
2021 : IF (memory_parameter%treat_forces_in_core) THEN
2022 : mb_size_buffers = mb_size_buffers + memory_parameter%size_p_screen
2023 : END IF
2024 : END IF
2025 :
2026 : !! mb
2027 : mb_size_buffers = mb_size_buffers/1024/128
2028 :
2029 : afac = 1.0_dp
2030 : IF (is_anti_symmetric) afac = -1.0_dp
2031 : CALL timestop(handle_main)
2032 : ene_x_aa_diag = 0.0_dp
2033 : ene_x_bb_diag = 0.0_dp
2034 : DO iatom = 1, natom
2035 : ikind = kind_of(iatom)
2036 : nseta = basis_parameter(ikind)%nset
2037 : nsgfa => basis_parameter(ikind)%nsgf
2038 : jatom = iatom
2039 : jkind = kind_of(jatom)
2040 : nsetb = basis_parameter(jkind)%nset
2041 : nsgfb => basis_parameter(jkind)%nsgf
2042 : act_atomic_block_offset = shm_atomic_block_offset(jatom, iatom)
2043 : DO img = 1, nkimages
2044 : DO iset = 1, nseta
2045 : DO jset = 1, nsetb
2046 : act_set_offset = shm_set_offset(jset, iset, jkind, ikind)
2047 : i = act_set_offset + act_atomic_block_offset - 1
2048 : DO ma = 1, nsgfa(iset)
2049 : j = shm_set_offset(iset, jset, jkind, ikind) + act_atomic_block_offset - 1 + ma - 1
2050 : DO mb = 1, nsgfb(jset)
2051 : IF (i > j) THEN
2052 : full_ks_alpha(i, img) = (full_ks_alpha(i, img) + full_ks_alpha(j, img)*afac)
2053 : full_ks_alpha(j, img) = full_ks_alpha(i, img)*afac
2054 : IF (.NOT. treat_lsd_in_core .AND. my_nspins == 2) THEN
2055 : full_ks_beta(i, img) = (full_ks_beta(i, img) + full_ks_beta(j, img)*afac)
2056 : full_ks_beta(j, img) = full_ks_beta(i, img)*afac
2057 : END IF
2058 : END IF
2059 : ! ** For adiabatically rescaled functionals we need the energy coming from the diagonal elements
2060 : IF (i == j) THEN
2061 : ene_x_aa_diag = ene_x_aa_diag + full_ks_alpha(i, img)*full_density_alpha(i, img)
2062 : IF (.NOT. treat_lsd_in_core .AND. my_nspins == 2) THEN
2063 : ene_x_bb_diag = ene_x_bb_diag + full_ks_beta(i, img)*full_density_beta(i, img)
2064 : END IF
2065 : END IF
2066 : i = i + 1
2067 : j = j + nsgfa(iset)
2068 : END DO
2069 : END DO
2070 : END DO
2071 : END DO
2072 : END DO
2073 : END DO
2074 :
2075 : CALL para_env%sync()
2076 : afac = 1.0_dp
2077 : IF (is_anti_symmetric) afac = 0._dp
2078 : IF (distribute_fock_matrix) THEN
2079 : !! Distribute the current KS-matrix to all the processes
2080 : CALL timeset(routineN//"_dist_KS", handle_dist_ks)
2081 : DO img = 1, nkimages
2082 : CALL distribute_ks_matrix(para_env, full_ks_alpha(:, img), ks_matrix(ispin, img)%matrix, shm_number_of_p_entries, &
2083 : shm_block_offset, kind_of, basis_parameter, &
2084 : off_diag_fac=0.5_dp, diag_fac=afac)
2085 : END DO
2086 :
2087 : NULLIFY (full_ks_alpha)
2088 : DEALLOCATE (shm_master_x_data%full_ks_alpha)
2089 : IF (.NOT. treat_lsd_in_core) THEN
2090 : IF (my_nspins == 2) THEN
2091 : DO img = 1, nkimages
2092 : CALL distribute_ks_matrix(para_env, full_ks_beta(:, img), ks_matrix(2, img)%matrix, shm_number_of_p_entries, &
2093 : shm_block_offset, kind_of, basis_parameter, &
2094 : off_diag_fac=0.5_dp, diag_fac=afac)
2095 : END DO
2096 : NULLIFY (full_ks_beta)
2097 : DEALLOCATE (shm_master_x_data%full_ks_beta)
2098 : END IF
2099 : END IF
2100 : CALL timestop(handle_dist_ks)
2101 : END IF
2102 :
2103 : IF (distribute_fock_matrix) THEN
2104 : !! ** Calculate the exchange energy
2105 : ene_x_aa = 0.0_dp
2106 : DO img = 1, nkimages
2107 : CALL dbcsr_dot_threadsafe(ks_matrix(ispin, img)%matrix, rho_ao(ispin, img)%matrix, etmp)
2108 : ene_x_aa = ene_x_aa + etmp
2109 : END DO
2110 : !for ADMMS, we need the exchange matrix k(d) for both spins
2111 : IF (dft_control%do_admm) THEN
2112 : CPASSERT(nkimages == 1)
2113 : CALL dbcsr_copy(matrix_ks_aux_fit_hfx(ispin)%matrix, ks_matrix(ispin, 1)%matrix, &
2114 : name="HF exch. part of matrix_ks_aux_fit for ADMMS")
2115 : END IF
2116 :
2117 : ene_x_bb = 0.0_dp
2118 : IF (my_nspins == 2 .AND. .NOT. treat_lsd_in_core) THEN
2119 : DO img = 1, nkimages
2120 : CALL dbcsr_dot_threadsafe(ks_matrix(2, img)%matrix, rho_ao(2, img)%matrix, etmp)
2121 : ene_x_bb = ene_x_bb + etmp
2122 : END DO
2123 : !for ADMMS, we need the exchange matrix k(d) for both spins
2124 : IF (dft_control%do_admm) THEN
2125 : CPASSERT(nkimages == 1)
2126 : CALL dbcsr_copy(matrix_ks_aux_fit_hfx(2)%matrix, ks_matrix(2, 1)%matrix, &
2127 : name="HF exch. part of matrix_ks_aux_fit for ADMMS")
2128 : END IF
2129 : END IF
2130 :
2131 : !! Update energy type
2132 : ehfx = 0.5_dp*(ene_x_aa + ene_x_bb)
2133 : ELSE
2134 : ! ** It is easier to correct the following expression by the diagonal energy contribution,
2135 : ! ** than explicitly going throuhg the diagonal elements
2136 : DO img = 1, nkimages
2137 : DO pa = 1, SIZE(full_ks_alpha, 1)
2138 : ene_x_aa = ene_x_aa + full_ks_alpha(pa, img)*full_density_alpha(pa, img)
2139 : END DO
2140 : END DO
2141 : ! ** Now correct
2142 : ene_x_aa = (ene_x_aa + ene_x_aa_diag)*0.5_dp
2143 : IF (my_nspins == 2) THEN
2144 : DO img = 1, nkimages
2145 : DO pa = 1, SIZE(full_ks_beta, 1)
2146 : ene_x_bb = ene_x_bb + full_ks_beta(pa, img)*full_density_beta(pa, img)
2147 : END DO
2148 : END DO
2149 : ! ** Now correct
2150 : ene_x_bb = (ene_x_bb + ene_x_bb_diag)*0.5_dp
2151 : END IF
2152 : CALL para_env%sum(ene_x_aa)
2153 : IF (my_nspins == 2) CALL para_env%sum(ene_x_bb)
2154 : ehfx = 0.5_dp*(ene_x_aa + ene_x_bb)
2155 : END IF
2156 :
2157 : !! Print some memeory information if this is the first step
2158 : IF (my_geo_change) THEN
2159 : tmp_i8(1:8) = [shm_storage_counter_integrals, shm_neris_onthefly, shm_neris_incore, shm_neris_disk, &
2160 : shm_neris_total, shm_stor_count_int_disk, shm_nprim_ints, shm_stor_count_max_val]
2161 : CALL para_env%sum(tmp_i8)
2162 : shm_storage_counter_integrals = tmp_i8(1)
2163 : shm_neris_onthefly = tmp_i8(2)
2164 : shm_neris_incore = tmp_i8(3)
2165 : shm_neris_disk = tmp_i8(4)
2166 : shm_neris_total = tmp_i8(5)
2167 : shm_stor_count_int_disk = tmp_i8(6)
2168 : shm_nprim_ints = tmp_i8(7)
2169 : shm_stor_count_max_val = tmp_i8(8)
2170 : CALL para_env%max(memsize_after)
2171 : mem_eris = (shm_storage_counter_integrals + 128*1024 - 1)/1024/128
2172 : compression_factor = REAL(shm_neris_incore, dp)/REAL(shm_storage_counter_integrals, dp)
2173 : mem_eris_disk = (shm_stor_count_int_disk + 128*1024 - 1)/1024/128
2174 : compression_factor_disk = REAL(shm_neris_disk, dp)/REAL(shm_stor_count_int_disk, dp)
2175 : mem_max_val = (shm_stor_count_max_val + 128*1024 - 1)/1024/128
2176 :
2177 : IF (shm_neris_incore == 0) THEN
2178 : mem_eris = 0
2179 : compression_factor = 0.0_dp
2180 : END IF
2181 : IF (shm_neris_disk == 0) THEN
2182 : mem_eris_disk = 0
2183 : compression_factor_disk = 0.0_dp
2184 : END IF
2185 :
2186 : iw = cp_print_key_unit_nr(logger, hfx_section, "HF_INFO", &
2187 : extension=".scfLog")
2188 : IF (iw > 0) THEN
2189 : WRITE (UNIT=iw, FMT="((T3,A,T60,I21))") &
2190 : "HFX_MEM_INFO| Number of cart. primitive ERI's calculated: ", shm_nprim_ints
2191 :
2192 : WRITE (UNIT=iw, FMT="((T3,A,T60,I21))") &
2193 : "HFX_MEM_INFO| Number of sph. ERI's calculated: ", shm_neris_total
2194 :
2195 : WRITE (UNIT=iw, FMT="((T3,A,T60,I21))") &
2196 : "HFX_MEM_INFO| Number of sph. ERI's stored in-core: ", shm_neris_incore
2197 :
2198 : WRITE (UNIT=iw, FMT="((T3,A,T60,I21))") &
2199 : "HFX_MEM_INFO| Number of sph. ERI's stored on disk: ", shm_neris_disk
2200 :
2201 : WRITE (UNIT=iw, FMT="((T3,A,T60,I21))") &
2202 : "HFX_MEM_INFO| Number of sph. ERI's calculated on the fly: ", shm_neris_onthefly
2203 :
2204 : WRITE (UNIT=iw, FMT="((T3,A,T60,I21))") &
2205 : "HFX_MEM_INFO| Total memory consumption ERI's RAM [MiB]: ", mem_eris
2206 :
2207 : WRITE (UNIT=iw, FMT="((T3,A,T60,I21))") &
2208 : "HFX_MEM_INFO| Whereof max-vals [MiB]: ", mem_max_val
2209 :
2210 : WRITE (UNIT=iw, FMT="((T3,A,T60,F21.2))") &
2211 : "HFX_MEM_INFO| Total compression factor ERI's RAM: ", compression_factor
2212 :
2213 : WRITE (UNIT=iw, FMT="((T3,A,T60,I21))") &
2214 : "HFX_MEM_INFO| Total memory consumption ERI's disk [MiB]: ", mem_eris_disk
2215 :
2216 : WRITE (UNIT=iw, FMT="((T3,A,T60,F21.2))") &
2217 : "HFX_MEM_INFO| Total compression factor ERI's disk: ", compression_factor_disk
2218 :
2219 : WRITE (UNIT=iw, FMT="((T3,A,T60,I21))") &
2220 : "HFX_MEM_INFO| Size of density/Fock matrix [MiB]: ", 2_int_8*mb_size_p
2221 :
2222 : IF (do_periodic) THEN
2223 : WRITE (UNIT=iw, FMT="((T3,A,T60,I21))") &
2224 : "HFX_MEM_INFO| Size of buffers [MiB]: ", mb_size_buffers
2225 : WRITE (UNIT=iw, FMT="((T3,A,T60,I21))") &
2226 : "HFX_MEM_INFO| Number of periodic image cells considered: ", SIZE(shm_master_x_data%neighbor_cells)
2227 : ELSE
2228 : WRITE (UNIT=iw, FMT="((T3,A,T60,I21))") &
2229 : "HFX_MEM_INFO| Size of buffers [MiB]: ", mb_size_buffers
2230 : END IF
2231 : WRITE (UNIT=iw, FMT="((T3,A,T60,I21),/)") &
2232 : "HFX_MEM_INFO| Est. max. program size after HFX [MiB]:", memsize_after/(1024*1024)
2233 : CALL m_flush(iw)
2234 : END IF
2235 :
2236 : CALL cp_print_key_finished_output(iw, logger, hfx_section, &
2237 : "HF_INFO")
2238 : END IF
2239 : !$OMP END MASTER
2240 :
2241 : !! flush caches if the geometry changed
2242 : IF (do_dynamic_load_balancing) THEN
2243 : my_bin_size = SIZE(actual_x_data%distribution_energy)
2244 : ELSE
2245 : my_bin_size = 1
2246 : END IF
2247 :
2248 : IF (my_geo_change) THEN
2249 : IF (.NOT. memory_parameter%do_all_on_the_fly) THEN
2250 : DO bin = 1, my_bin_size
2251 : maxval_cache => actual_x_data%store_ints%maxval_cache(bin)
2252 : maxval_container => actual_x_data%store_ints%maxval_container(bin)
2253 : integral_caches => actual_x_data%store_ints%integral_caches(:, bin)
2254 : integral_containers => actual_x_data%store_ints%integral_containers(:, bin)
2255 : CALL hfx_flush_last_cache(bits_max_val, maxval_cache, maxval_container, memory_parameter%actual_memory_usage, &
2256 : .FALSE.)
2257 : DO i = 1, 64
2258 : CALL hfx_flush_last_cache(i, integral_caches(i), integral_containers(i), &
2259 : memory_parameter%actual_memory_usage, .FALSE.)
2260 : END DO
2261 : END DO
2262 : END IF
2263 : END IF
2264 : !! reset all caches except we calculate all on the fly
2265 : IF (.NOT. memory_parameter%do_all_on_the_fly) THEN
2266 : DO bin = 1, my_bin_size
2267 : maxval_cache => actual_x_data%store_ints%maxval_cache(bin)
2268 : maxval_container => actual_x_data%store_ints%maxval_container(bin)
2269 : integral_caches => actual_x_data%store_ints%integral_caches(:, bin)
2270 : integral_containers => actual_x_data%store_ints%integral_containers(:, bin)
2271 :
2272 : CALL hfx_reset_cache_and_container(maxval_cache, maxval_container, memory_parameter%actual_memory_usage, .FALSE.)
2273 : DO i = 1, 64
2274 : CALL hfx_reset_cache_and_container(integral_caches(i), integral_containers(i), &
2275 : memory_parameter%actual_memory_usage, &
2276 : .FALSE.)
2277 : END DO
2278 : END DO
2279 : END IF
2280 :
2281 : !! Since the I/O routines are no thread-safe, i.e. the procedure to get the unit number, put a lock here
2282 : !$OMP CRITICAL(hfxenergy_out_critical)
2283 : IF (do_disk_storage) THEN
2284 : !! flush caches if the geometry changed
2285 : IF (my_geo_change) THEN
2286 : CALL hfx_flush_last_cache(bits_max_val, maxval_cache_disk, maxval_container_disk, &
2287 : memory_parameter%actual_memory_usage_disk, .TRUE.)
2288 : DO i = 1, 64
2289 : CALL hfx_flush_last_cache(i, integral_caches_disk(i), integral_containers_disk(i), &
2290 : memory_parameter%actual_memory_usage_disk, .TRUE.)
2291 : END DO
2292 : END IF
2293 : !! reset all caches except we calculate all on the fly
2294 : CALL hfx_reset_cache_and_container(maxval_cache_disk, maxval_container_disk, memory_parameter%actual_memory_usage_disk, &
2295 : do_disk_storage)
2296 : DO i = 1, 64
2297 : CALL hfx_reset_cache_and_container(integral_caches_disk(i), integral_containers_disk(i), &
2298 : memory_parameter%actual_memory_usage_disk, do_disk_storage)
2299 : END DO
2300 : END IF
2301 : !$OMP END CRITICAL(hfxenergy_out_critical)
2302 : !$OMP BARRIER
2303 : !! Clean up
2304 : DEALLOCATE (last_sgf_global)
2305 : !$OMP MASTER
2306 : DEALLOCATE (full_density_alpha)
2307 : IF (.NOT. treat_lsd_in_core) THEN
2308 : IF (my_nspins == 2) THEN
2309 : DEALLOCATE (full_density_beta)
2310 : END IF
2311 : END IF
2312 : IF (do_dynamic_load_balancing) THEN
2313 : DEALLOCATE (shm_master_x_data%task_list)
2314 : END IF
2315 : !$OMP END MASTER
2316 : DEALLOCATE (pbd_buf, pbc_buf, pad_buf, pac_buf)
2317 : DEALLOCATE (kbd_buf, kbc_buf, kad_buf, kac_buf)
2318 : DEALLOCATE (set_list_ij, set_list_kl)
2319 :
2320 : DO i = 1, max_pgf**2
2321 : DEALLOCATE (pgf_list_ij(i)%image_list)
2322 : DEALLOCATE (pgf_list_kl(i)%image_list)
2323 : END DO
2324 :
2325 : DEALLOCATE (pgf_list_ij)
2326 : DEALLOCATE (pgf_list_kl)
2327 : DEALLOCATE (pgf_product_list)
2328 :
2329 : DEALLOCATE (max_contraction, kind_of)
2330 :
2331 : DEALLOCATE (ee_work, ee_work2, ee_buffer1, ee_buffer2, ee_primitives_tmp)
2332 :
2333 : DEALLOCATE (nimages)
2334 :
2335 : !$OMP BARRIER
2336 : !$OMP END PARALLEL
2337 :
2338 43577 : CALL timestop(handle)
2339 90378698 : END SUBROUTINE integrate_four_center
2340 :
2341 : ! **************************************************************************************************
2342 : !> \brief Given a 2d index pair, this function returns a 1d index pair for
2343 : !> a symmetric upper triangle NxN matrix
2344 : !> The compiler should inline this function, therefore it appears in
2345 : !> several modules
2346 : !> \param i 2d index
2347 : !> \param j 2d index
2348 : !> \param N matrix size
2349 : !> \return ...
2350 : !> \par History
2351 : !> 03.2009 created [Manuel Guidon]
2352 : !> \author Manuel Guidon
2353 : ! **************************************************************************************************
2354 28224 : PURE FUNCTION get_1D_idx(i, j, N)
2355 : INTEGER, INTENT(IN) :: i, j
2356 : INTEGER(int_8), INTENT(IN) :: N
2357 : INTEGER(int_8) :: get_1D_idx
2358 :
2359 : INTEGER(int_8) :: min_ij
2360 :
2361 28224 : min_ij = MIN(i, j)
2362 28224 : get_1D_idx = min_ij*N + MAX(i, j) - (min_ij - 1)*min_ij/2 - N
2363 :
2364 28224 : END FUNCTION get_1D_idx
2365 :
2366 : ! **************************************************************************************************
2367 : !> \brief This routine prefetches density/fock matrix elements and stores them
2368 : !> in cache friendly arrays. These buffers are then used to update the
2369 : !> fock matrix
2370 : !> \param ma_max Size of matrix blocks
2371 : !> \param mb_max Size of matrix blocks
2372 : !> \param mc_max Size of matrix blocks
2373 : !> \param md_max Size of matrix blocks
2374 : !> \param fac multiplication factor (spin)
2375 : !> \param symm_fac multiplication factor (symmetry)
2376 : !> \param density upper triangular density matrix
2377 : !> \param ks upper triangular fock matrix
2378 : !> \param prim primitive integrals
2379 : !> \param pbd buffer that will contain P(b,d)
2380 : !> \param pbc buffer that will contain P(b,c)
2381 : !> \param pad buffer that will contain P(a,d)
2382 : !> \param pac buffer that will contain P(a,c)
2383 : !> \param kbd buffer for KS(b,d)
2384 : !> \param kbc buffer for KS(b,c)
2385 : !> \param kad buffer for KS(a,d)
2386 : !> \param kac buffer for KS(a,c)
2387 : !> \param iatom ...
2388 : !> \param jatom ...
2389 : !> \param katom ...
2390 : !> \param latom ...
2391 : !> \param iset ...
2392 : !> \param jset ...
2393 : !> \param kset ...
2394 : !> \param lset ...
2395 : !> \param offset_bd_set ...
2396 : !> \param offset_bc_set ...
2397 : !> \param offset_ad_set ...
2398 : !> \param offset_ac_set ...
2399 : !> \param atomic_offset_bd ...
2400 : !> \param atomic_offset_bc ...
2401 : !> \param atomic_offset_ad ...
2402 : !> \param atomic_offset_ac ...
2403 : !> \par History
2404 : !> 03.2009 created [Manuel Guidon]
2405 : !> \author Manuel Guidon
2406 : ! **************************************************************************************************
2407 :
2408 81528198 : SUBROUTINE update_fock_matrix(ma_max, mb_max, mc_max, md_max, &
2409 81528198 : fac, symm_fac, density, ks, prim, &
2410 : pbd, pbc, pad, pac, kbd, kbc, kad, kac, &
2411 : iatom, jatom, katom, latom, &
2412 : iset, jset, kset, lset, offset_bd_set, offset_bc_set, offset_ad_set, &
2413 : offset_ac_set, atomic_offset_bd, atomic_offset_bc, atomic_offset_ad, &
2414 : atomic_offset_ac)
2415 :
2416 : INTEGER, INTENT(IN) :: ma_max, mb_max, mc_max, md_max
2417 : REAL(dp), INTENT(IN) :: fac, symm_fac
2418 : REAL(dp), DIMENSION(:), INTENT(IN) :: density
2419 : REAL(dp), DIMENSION(:), INTENT(INOUT) :: ks
2420 : REAL(dp), DIMENSION(ma_max*mb_max*mc_max*md_max), &
2421 : INTENT(IN) :: prim
2422 : REAL(dp), DIMENSION(*), INTENT(INOUT) :: pbd, pbc, pad, pac, kbd, kbc, kad, kac
2423 : INTEGER, INTENT(IN) :: iatom, jatom, katom, latom, iset, jset, &
2424 : kset, lset
2425 : INTEGER, DIMENSION(:, :), POINTER, INTENT(IN) :: offset_bd_set, offset_bc_set, &
2426 : offset_ad_set, offset_ac_set
2427 : INTEGER, INTENT(IN) :: atomic_offset_bd, atomic_offset_bc, &
2428 : atomic_offset_ad, atomic_offset_ac
2429 :
2430 : INTEGER :: i, j, ma, mb, mc, md, offset_ac, &
2431 : offset_ad, offset_bc, offset_bd
2432 : REAL(dp) :: ki
2433 :
2434 81528198 : IF (jatom >= latom) THEN
2435 80318181 : i = 1
2436 80318181 : offset_bd = offset_bd_set(jset, lset) + atomic_offset_bd - 1
2437 80318181 : j = offset_bd
2438 241444047 : DO md = 1, md_max
2439 540132493 : DO mb = 1, mb_max
2440 298688446 : pbd(i) = density(j)
2441 298688446 : i = i + 1
2442 459814312 : j = j + 1
2443 : END DO
2444 : END DO
2445 : ELSE
2446 1210017 : i = 1
2447 1210017 : offset_bd = offset_bd_set(lset, jset) + atomic_offset_bd - 1
2448 3554001 : DO md = 1, md_max
2449 2343984 : j = offset_bd + md - 1
2450 8699913 : DO mb = 1, mb_max
2451 5145912 : pbd(i) = density(j)
2452 5145912 : i = i + 1
2453 7489896 : j = j + md_max
2454 : END DO
2455 : END DO
2456 : END IF
2457 81528198 : IF (jatom >= katom) THEN
2458 81528198 : i = 1
2459 81528198 : offset_bc = offset_bc_set(jset, kset) + atomic_offset_bc - 1
2460 81528198 : j = offset_bc
2461 270392169 : DO mc = 1, mc_max
2462 610194140 : DO mb = 1, mb_max
2463 339801971 : pbc(i) = density(j)
2464 339801971 : i = i + 1
2465 528665942 : j = j + 1
2466 : END DO
2467 : END DO
2468 : ELSE
2469 0 : i = 1
2470 0 : offset_bc = offset_bc_set(kset, jset) + atomic_offset_bc - 1
2471 0 : DO mc = 1, mc_max
2472 0 : j = offset_bc + mc - 1
2473 0 : DO mb = 1, mb_max
2474 0 : pbc(i) = density(j)
2475 0 : i = i + 1
2476 0 : j = j + mc_max
2477 : END DO
2478 : END DO
2479 : END IF
2480 81528198 : IF (iatom >= latom) THEN
2481 58694064 : i = 1
2482 58694064 : offset_ad = offset_ad_set(iset, lset) + atomic_offset_ad - 1
2483 58694064 : j = offset_ad
2484 184492750 : DO md = 1, md_max
2485 456966270 : DO ma = 1, ma_max
2486 272473520 : pad(i) = density(j)
2487 272473520 : i = i + 1
2488 398272206 : j = j + 1
2489 : END DO
2490 : END DO
2491 : ELSE
2492 22834134 : i = 1
2493 22834134 : offset_ad = offset_ad_set(lset, iset) + atomic_offset_ad - 1
2494 60505298 : DO md = 1, md_max
2495 37671164 : j = offset_ad + md - 1
2496 150862478 : DO ma = 1, ma_max
2497 90357180 : pad(i) = density(j)
2498 90357180 : i = i + 1
2499 128028344 : j = j + md_max
2500 : END DO
2501 : END DO
2502 : END IF
2503 81528198 : IF (iatom >= katom) THEN
2504 77774454 : i = 1
2505 77774454 : offset_ac = offset_ac_set(iset, kset) + atomic_offset_ac - 1
2506 77774454 : j = offset_ac
2507 259838187 : DO mc = 1, mc_max
2508 659396701 : DO ma = 1, ma_max
2509 399558514 : pac(i) = density(j)
2510 399558514 : i = i + 1
2511 581622247 : j = j + 1
2512 : END DO
2513 : END DO
2514 : ELSE
2515 3753744 : i = 1
2516 3753744 : offset_ac = offset_ac_set(kset, iset) + atomic_offset_ac - 1
2517 10553982 : DO mc = 1, mc_max
2518 6800238 : j = offset_ac + mc - 1
2519 28489150 : DO ma = 1, ma_max
2520 17935168 : pac(i) = density(j)
2521 17935168 : i = i + 1
2522 24735406 : j = j + mc_max
2523 : END DO
2524 : END DO
2525 : END IF
2526 :
2527 81528198 : CALL contract_block(ma_max, mb_max, mc_max, md_max, kbd, kbc, kad, kac, pbd, pbc, pad, pac, prim, fac*symm_fac)
2528 81528198 : IF (jatom >= latom) THEN
2529 : i = 1
2530 : j = offset_bd
2531 241444047 : DO md = 1, md_max
2532 540132493 : DO mb = 1, mb_max
2533 298688446 : ki = kbd(i)
2534 298688446 : !$OMP ATOMIC
2535 : ks(j) = ks(j) + ki
2536 298688446 : i = i + 1
2537 459814312 : j = j + 1
2538 : END DO
2539 : END DO
2540 : ELSE
2541 : i = 1
2542 3554001 : DO md = 1, md_max
2543 2343984 : j = offset_bd + md - 1
2544 8699913 : DO mb = 1, mb_max
2545 5145912 : ki = kbd(i)
2546 5145912 : !$OMP ATOMIC
2547 : ks(j) = ks(j) + ki
2548 5145912 : i = i + 1
2549 7489896 : j = j + md_max
2550 : END DO
2551 : END DO
2552 : END IF
2553 81528198 : IF (jatom >= katom) THEN
2554 : i = 1
2555 : j = offset_bc
2556 270392169 : DO mc = 1, mc_max
2557 610194140 : DO mb = 1, mb_max
2558 339801971 : ki = kbc(i)
2559 339801971 : !$OMP ATOMIC
2560 : ks(j) = ks(j) + ki
2561 339801971 : i = i + 1
2562 528665942 : j = j + 1
2563 : END DO
2564 : END DO
2565 : ELSE
2566 : i = 1
2567 0 : DO mc = 1, mc_max
2568 0 : j = offset_bc + mc - 1
2569 0 : DO mb = 1, mb_max
2570 0 : ki = kbc(i)
2571 0 : !$OMP ATOMIC
2572 : ks(j) = ks(j) + ki
2573 0 : i = i + 1
2574 0 : j = j + mc_max
2575 : END DO
2576 : END DO
2577 : END IF
2578 81528198 : IF (iatom >= latom) THEN
2579 : i = 1
2580 : j = offset_ad
2581 184492750 : DO md = 1, md_max
2582 456966270 : DO ma = 1, ma_max
2583 272473520 : ki = kad(i)
2584 272473520 : !$OMP ATOMIC
2585 : ks(j) = ks(j) + ki
2586 272473520 : i = i + 1
2587 398272206 : j = j + 1
2588 : END DO
2589 : END DO
2590 : ELSE
2591 : i = 1
2592 60505298 : DO md = 1, md_max
2593 37671164 : j = offset_ad + md - 1
2594 150862478 : DO ma = 1, ma_max
2595 90357180 : ki = kad(i)
2596 90357180 : !$OMP ATOMIC
2597 : ks(j) = ks(j) + ki
2598 90357180 : i = i + 1
2599 128028344 : j = j + md_max
2600 : END DO
2601 : END DO
2602 : END IF
2603 81528198 : IF (iatom >= katom) THEN
2604 : i = 1
2605 : j = offset_ac
2606 259838187 : DO mc = 1, mc_max
2607 659396701 : DO ma = 1, ma_max
2608 399558514 : ki = kac(i)
2609 399558514 : !$OMP ATOMIC
2610 : ks(j) = ks(j) + ki
2611 399558514 : i = i + 1
2612 581622247 : j = j + 1
2613 : END DO
2614 : END DO
2615 : ELSE
2616 : i = 1
2617 10553982 : DO mc = 1, mc_max
2618 6800238 : j = offset_ac + mc - 1
2619 28489150 : DO ma = 1, ma_max
2620 17935168 : ki = kac(i)
2621 17935168 : !$OMP ATOMIC
2622 : ks(j) = ks(j) + ki
2623 17935168 : i = i + 1
2624 24735406 : j = j + mc_max
2625 : END DO
2626 : END DO
2627 : END IF
2628 81528198 : END SUBROUTINE update_fock_matrix
2629 :
2630 : ! **************************************************************************************************
2631 : !> \brief ...
2632 : !> \param ma_max ...
2633 : !> \param mb_max ...
2634 : !> \param mc_max ...
2635 : !> \param md_max ...
2636 : !> \param fac ...
2637 : !> \param symm_fac ...
2638 : !> \param density ...
2639 : !> \param ks ...
2640 : !> \param prim ...
2641 : !> \param pbd ...
2642 : !> \param pbc ...
2643 : !> \param pad ...
2644 : !> \param pac ...
2645 : !> \param kbd ...
2646 : !> \param kbc ...
2647 : !> \param kad ...
2648 : !> \param kac ...
2649 : !> \param iatom ...
2650 : !> \param jatom ...
2651 : !> \param katom ...
2652 : !> \param latom ...
2653 : !> \param iset ...
2654 : !> \param jset ...
2655 : !> \param kset ...
2656 : !> \param lset ...
2657 : !> \param offset_bd_set ...
2658 : !> \param offset_bc_set ...
2659 : !> \param offset_ad_set ...
2660 : !> \param offset_ac_set ...
2661 : !> \param atomic_offset_bd ...
2662 : !> \param atomic_offset_bc ...
2663 : !> \param atomic_offset_ad ...
2664 : !> \param atomic_offset_ac ...
2665 : ! **************************************************************************************************
2666 5576527 : SUBROUTINE update_fock_matrix_as(ma_max, mb_max, mc_max, md_max, &
2667 5576527 : fac, symm_fac, density, ks, prim, &
2668 : pbd, pbc, pad, pac, kbd, kbc, kad, kac, &
2669 : iatom, jatom, katom, latom, &
2670 : iset, jset, kset, lset, offset_bd_set, offset_bc_set, offset_ad_set, &
2671 : offset_ac_set, atomic_offset_bd, atomic_offset_bc, atomic_offset_ad, &
2672 : atomic_offset_ac)
2673 :
2674 : INTEGER, INTENT(IN) :: ma_max, mb_max, mc_max, md_max
2675 : REAL(dp), INTENT(IN) :: fac, symm_fac
2676 : REAL(dp), DIMENSION(:), INTENT(IN) :: density
2677 : REAL(dp), DIMENSION(:), INTENT(INOUT) :: ks
2678 : REAL(dp), DIMENSION(ma_max*mb_max*mc_max*md_max), &
2679 : INTENT(IN) :: prim
2680 : REAL(dp), DIMENSION(*), INTENT(INOUT) :: pbd, pbc, pad, pac, kbd, kbc, kad, kac
2681 : INTEGER, INTENT(IN) :: iatom, jatom, katom, latom, iset, jset, &
2682 : kset, lset
2683 : INTEGER, DIMENSION(:, :), POINTER :: offset_bd_set, offset_bc_set, &
2684 : offset_ad_set, offset_ac_set
2685 : INTEGER, INTENT(IN) :: atomic_offset_bd, atomic_offset_bc, &
2686 : atomic_offset_ad, atomic_offset_ac
2687 :
2688 : INTEGER :: i, j, ma, mb, mc, md, offset_ac, &
2689 : offset_ad, offset_bc, offset_bd
2690 :
2691 5576527 : IF (jatom >= latom) THEN
2692 5563015 : i = 1
2693 5563015 : offset_bd = offset_bd_set(jset, lset) + atomic_offset_bd - 1
2694 5563015 : j = offset_bd
2695 15250568 : DO md = 1, md_max
2696 29473841 : DO mb = 1, mb_max
2697 14223273 : pbd(i) = +density(j)
2698 14223273 : i = i + 1
2699 23910826 : j = j + 1
2700 : END DO
2701 : END DO
2702 : ELSE
2703 13512 : i = 1
2704 13512 : offset_bd = offset_bd_set(lset, jset) + atomic_offset_bd - 1
2705 28504 : DO md = 1, md_max
2706 14992 : j = offset_bd + md - 1
2707 47164 : DO mb = 1, mb_max
2708 18660 : pbd(i) = -density(j)
2709 18660 : i = i + 1
2710 33652 : j = j + md_max
2711 : END DO
2712 : END DO
2713 : END IF
2714 5576527 : IF (jatom >= katom) THEN
2715 5576527 : i = 1
2716 5576527 : offset_bc = offset_bc_set(jset, kset) + atomic_offset_bc - 1
2717 5576527 : j = offset_bc
2718 17136944 : DO mc = 1, mc_max
2719 33611205 : DO mb = 1, mb_max
2720 16474261 : pbc(i) = -density(j)
2721 16474261 : i = i + 1
2722 28034678 : j = j + 1
2723 : END DO
2724 : END DO
2725 : ELSE
2726 0 : i = 1
2727 0 : offset_bc = offset_bc_set(kset, jset) + atomic_offset_bc - 1
2728 0 : DO mc = 1, mc_max
2729 0 : j = offset_bc + mc - 1
2730 0 : DO mb = 1, mb_max
2731 0 : pbc(i) = density(j)
2732 0 : i = i + 1
2733 0 : j = j + mc_max
2734 : END DO
2735 : END DO
2736 : END IF
2737 5576527 : IF (iatom >= latom) THEN
2738 4172693 : i = 1
2739 4172693 : offset_ad = offset_ad_set(iset, lset) + atomic_offset_ad - 1
2740 4172693 : j = offset_ad
2741 12187344 : DO md = 1, md_max
2742 27430903 : DO ma = 1, ma_max
2743 15243559 : pad(i) = -density(j)
2744 15243559 : i = i + 1
2745 23258210 : j = j + 1
2746 : END DO
2747 : END DO
2748 : ELSE
2749 1403834 : i = 1
2750 1403834 : offset_ad = offset_ad_set(lset, iset) + atomic_offset_ad - 1
2751 3091728 : DO md = 1, md_max
2752 1687894 : j = offset_ad + md - 1
2753 6870666 : DO ma = 1, ma_max
2754 3778938 : pad(i) = density(j)
2755 3778938 : i = i + 1
2756 5466832 : j = j + md_max
2757 : END DO
2758 : END DO
2759 : END IF
2760 5576527 : IF (iatom >= katom) THEN
2761 5372033 : i = 1
2762 5372033 : offset_ac = offset_ac_set(iset, kset) + atomic_offset_ac - 1
2763 5372033 : j = offset_ac
2764 16639124 : DO mc = 1, mc_max
2765 38530321 : DO ma = 1, ma_max
2766 21891197 : pac(i) = +density(j)
2767 21891197 : i = i + 1
2768 33158288 : j = j + 1
2769 : END DO
2770 : END DO
2771 : ELSE
2772 204494 : i = 1
2773 204494 : offset_ac = offset_ac_set(kset, iset) + atomic_offset_ac - 1
2774 497820 : DO mc = 1, mc_max
2775 293326 : j = offset_ac + mc - 1
2776 1251692 : DO ma = 1, ma_max
2777 753872 : pac(i) = -density(j)
2778 753872 : i = i + 1
2779 1047198 : j = j + mc_max
2780 : END DO
2781 : END DO
2782 : END IF
2783 :
2784 5576527 : CALL contract_block(ma_max, mb_max, mc_max, md_max, kbd, kbc, kad, kac, pbd, pbc, pad, pac, prim, fac*symm_fac)
2785 :
2786 5576527 : IF (jatom >= latom) THEN
2787 : i = 1
2788 : j = offset_bd
2789 15250568 : DO md = 1, md_max
2790 29473841 : DO mb = 1, mb_max
2791 14223273 : !$OMP ATOMIC
2792 : ks(j) = ks(j) + kbd(i)
2793 14223273 : i = i + 1
2794 23910826 : j = j + 1
2795 : END DO
2796 : END DO
2797 : ELSE
2798 : i = 1
2799 28504 : DO md = 1, md_max
2800 14992 : j = offset_bd + md - 1
2801 47164 : DO mb = 1, mb_max
2802 18660 : !$OMP ATOMIC
2803 : ks(j) = ks(j) - kbd(i)
2804 18660 : i = i + 1
2805 33652 : j = j + md_max
2806 : END DO
2807 : END DO
2808 : END IF
2809 5576527 : IF (jatom >= katom) THEN
2810 : i = 1
2811 : j = offset_bc
2812 17136944 : DO mc = 1, mc_max
2813 33611205 : DO mb = 1, mb_max
2814 16474261 : !$OMP ATOMIC
2815 : ks(j) = ks(j) - kbc(i)
2816 16474261 : i = i + 1
2817 28034678 : j = j + 1
2818 : END DO
2819 : END DO
2820 : ELSE
2821 : i = 1
2822 0 : DO mc = 1, mc_max
2823 0 : j = offset_bc + mc - 1
2824 0 : DO mb = 1, mb_max
2825 0 : !$OMP ATOMIC
2826 : ks(j) = ks(j) + kbc(i)
2827 0 : i = i + 1
2828 0 : j = j + mc_max
2829 : END DO
2830 : END DO
2831 : END IF
2832 5576527 : IF (iatom >= latom) THEN
2833 : i = 1
2834 : j = offset_ad
2835 12187344 : DO md = 1, md_max
2836 27430903 : DO ma = 1, ma_max
2837 15243559 : !$OMP ATOMIC
2838 : ks(j) = ks(j) - kad(i)
2839 15243559 : i = i + 1
2840 23258210 : j = j + 1
2841 : END DO
2842 : END DO
2843 : ELSE
2844 : i = 1
2845 3091728 : DO md = 1, md_max
2846 1687894 : j = offset_ad + md - 1
2847 6870666 : DO ma = 1, ma_max
2848 3778938 : !$OMP ATOMIC
2849 : ks(j) = ks(j) + kad(i)
2850 3778938 : i = i + 1
2851 5466832 : j = j + md_max
2852 : END DO
2853 : END DO
2854 : END IF
2855 : ! XXXXXXXXXXXXXXXXXXXXXXXX
2856 5576527 : IF (iatom >= katom) THEN
2857 : i = 1
2858 : j = offset_ac
2859 16639124 : DO mc = 1, mc_max
2860 38530321 : DO ma = 1, ma_max
2861 21891197 : !$OMP ATOMIC
2862 : ks(j) = ks(j) + kac(i)
2863 21891197 : i = i + 1
2864 33158288 : j = j + 1
2865 : END DO
2866 : END DO
2867 : ELSE
2868 : i = 1
2869 497820 : DO mc = 1, mc_max
2870 293326 : j = offset_ac + mc - 1
2871 1251692 : DO ma = 1, ma_max
2872 753872 : !$OMP ATOMIC
2873 : ks(j) = ks(j) - kac(i)
2874 753872 : i = i + 1
2875 1047198 : j = j + mc_max
2876 : END DO
2877 : END DO
2878 : END IF
2879 :
2880 5576527 : END SUBROUTINE update_fock_matrix_as
2881 :
2882 : ! **************************************************************************************************
2883 : !> \brief ...
2884 : !> \param i ...
2885 : !> \param j ...
2886 : !> \param k ...
2887 : !> \param l ...
2888 : !> \param set_offsets ...
2889 : !> \param atom_offsets ...
2890 : !> \param iset ...
2891 : !> \param jset ...
2892 : !> \param kset ...
2893 : !> \param lset ...
2894 : !> \param ma_max ...
2895 : !> \param mb_max ...
2896 : !> \param mc_max ...
2897 : !> \param md_max ...
2898 : !> \param prim ...
2899 : !> \param output_unit ...
2900 : ! **************************************************************************************************
2901 0 : SUBROUTINE print_integrals(i, j, k, l, set_offsets, atom_offsets, iset, jset, kset, lset, &
2902 0 : ma_max, mb_max, mc_max, md_max, prim, output_unit)
2903 : INTEGER :: i, j, k, l
2904 : INTEGER, DIMENSION(:, :, :, :), POINTER :: set_offsets
2905 : INTEGER, DIMENSION(:, :), POINTER :: atom_offsets
2906 : INTEGER :: iset, jset, kset, lset, ma_max, mb_max, &
2907 : mc_max, md_max
2908 : REAL(dp), DIMENSION(ma_max*mb_max*mc_max*md_max), &
2909 : INTENT(IN) :: prim
2910 : INTEGER, OPTIONAL :: output_unit
2911 :
2912 : REAL(KIND=dp), PARAMETER :: eps_int = 1.0E-13_dp
2913 :
2914 : INTEGER :: iint, ma, mb, mc, md, my_output_unit
2915 :
2916 0 : my_output_unit = 99
2917 0 : IF (PRESENT(output_unit)) my_output_unit = output_unit
2918 :
2919 0 : iint = 0
2920 0 : DO md = 1, md_max
2921 0 : DO mc = 1, mc_max
2922 0 : DO mb = 1, mb_max
2923 0 : DO ma = 1, ma_max
2924 0 : iint = iint + 1
2925 0 : IF (ABS(prim(iint)) > eps_int) THEN
2926 0 : IF (my_output_unit > 0) THEN
2927 0 : WRITE (my_output_unit, *) atom_offsets(i, 1) + ma + set_offsets(iset, 1, i, 1) - 1, &
2928 0 : atom_offsets(j, 1) + ma + set_offsets(jset, 1, j, 1) - 1, &
2929 0 : atom_offsets(k, 1) + ma + set_offsets(kset, 1, k, 1) - 1, &
2930 0 : atom_offsets(l, 1) + ma + set_offsets(lset, 1, l, 1) - 1, &
2931 0 : prim(iint)
2932 : END IF
2933 : END IF
2934 : END DO
2935 : END DO
2936 : END DO
2937 : END DO
2938 :
2939 0 : END SUBROUTINE print_integrals
2940 :
2941 : #:include "hfx_get_pmax_val.fypp"
2942 :
2943 : END MODULE hfx_energy_potential
|