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