Line data Source code
1 : !--------------------------------------------------------------------------------------------------!
2 : ! CP2K: A general program to perform molecular dynamics simulations !
3 : ! Copyright 2000-2026 CP2K developers group <https://cp2k.org> !
4 : ! !
5 : ! SPDX-License-Identifier: GPL-2.0-or-later !
6 : !--------------------------------------------------------------------------------------------------!
7 :
8 : ! **************************************************************************************************
9 : !> \brief Routines to compute the Coulomb integral V_(alpha beta)(k) for a k-point k using lattice
10 : !> summation in real space. These integrals are e.g. needed in periodic RI for RPA, GW
11 : !> \par History
12 : !> 2018.05 created [Jan Wilhelm]
13 : !> \author Jan Wilhelm
14 : ! **************************************************************************************************
15 : MODULE kpoint_coulomb_2c
16 : USE atomic_kind_types, ONLY: atomic_kind_type,&
17 : get_atomic_kind_set
18 : USE basis_set_types, ONLY: gto_basis_set_type
19 : USE cell_types, ONLY: cell_type,&
20 : get_cell,&
21 : pbc
22 : USE constants_operator, ONLY: operator_coulomb
23 : USE cp_dbcsr_api, ONLY: &
24 : dbcsr_create, dbcsr_init_p, dbcsr_iterator_blocks_left, dbcsr_iterator_next_block, &
25 : dbcsr_iterator_start, dbcsr_iterator_stop, dbcsr_iterator_type, dbcsr_p_type, &
26 : dbcsr_release_p, dbcsr_set, dbcsr_type, dbcsr_type_no_symmetry
27 : USE cp_dbcsr_contrib, ONLY: dbcsr_reserve_all_blocks
28 : USE gamma, ONLY: init_md_ftable
29 : USE generic_shg_integrals, ONLY: int_operators_r12_ab_shg
30 : USE generic_shg_integrals_init, ONLY: contraction_matrix_shg
31 : USE kinds, ONLY: dp,&
32 : dp_size
33 : USE kpoint_types, ONLY: get_kpoint_info,&
34 : kpoint_type
35 : USE mathconstants, ONLY: fourpi,&
36 : gaussi,&
37 : pi,&
38 : twopi,&
39 : z_one,&
40 : z_zero
41 : USE mathlib, ONLY: gauss_legendre,&
42 : vector_product
43 : USE message_passing, ONLY: mp_para_env_type
44 : USE orbital_pointers, ONLY: indco,&
45 : ncoset
46 : USE particle_types, ONLY: particle_type
47 : USE qs_environment_types, ONLY: get_qs_env,&
48 : qs_environment_type
49 : USE qs_kind_types, ONLY: get_qs_kind,&
50 : qs_kind_type
51 : #include "./base/base_uses.f90"
52 :
53 : IMPLICIT NONE
54 :
55 : PRIVATE
56 :
57 : CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'kpoint_coulomb_2c'
58 :
59 : PUBLIC :: build_2c_coulomb_matrix_kp, build_2c_coulomb_matrix_kp_dense, &
60 : add_ewald_reciprocal
61 :
62 : ! **************************************************************************************************
63 :
64 : TYPE two_d_util_type
65 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: block
66 : END TYPE two_d_util_type
67 :
68 : ! Conservative Gaussian-tail cutoff heuristics of the reciprocal Ewald sum.
69 : REAL(KIND=dp), PARAMETER :: ewald_tail_exponent = 50.0_dp, ewald_lmax_margin = 4.0_dp
70 :
71 : CONTAINS
72 :
73 : ! **************************************************************************************************
74 : !> \brief ...
75 : !> \param matrix_v_kp ...
76 : !> \param kpoints ...
77 : !> \param basis_type ...
78 : !> \param cell ...
79 : !> \param particle_set ...
80 : !> \param qs_kind_set ...
81 : !> \param atomic_kind_set ...
82 : !> \param size_lattice_sum ...
83 : !> \param operator_type ...
84 : !> \param ikp_start ...
85 : !> \param ikp_end ...
86 : ! **************************************************************************************************
87 278 : SUBROUTINE build_2c_coulomb_matrix_kp(matrix_v_kp, kpoints, basis_type, cell, particle_set, qs_kind_set, &
88 : atomic_kind_set, size_lattice_sum, operator_type, ikp_start, ikp_end)
89 :
90 : TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_v_kp
91 : TYPE(kpoint_type), POINTER :: kpoints
92 : CHARACTER(LEN=*), INTENT(IN) :: basis_type
93 : TYPE(cell_type), POINTER :: cell
94 : TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
95 : TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
96 : TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
97 : INTEGER :: size_lattice_sum, operator_type, &
98 : ikp_start, ikp_end
99 :
100 : CHARACTER(LEN=*), PARAMETER :: routineN = 'build_2c_coulomb_matrix_kp'
101 :
102 : INTEGER :: handle
103 : TYPE(dbcsr_type), POINTER :: matrix_v_L_tmp
104 :
105 278 : CALL timeset(routineN, handle)
106 :
107 278 : CALL allocate_tmp(matrix_v_L_tmp, matrix_v_kp, ikp_start)
108 :
109 : CALL lattice_sum(matrix_v_kp, kpoints, basis_type, cell, particle_set, &
110 : qs_kind_set, atomic_kind_set, size_lattice_sum, matrix_v_L_tmp, &
111 278 : operator_type, ikp_start, ikp_end)
112 :
113 278 : CALL deallocate_tmp(matrix_v_L_tmp)
114 :
115 278 : CALL timestop(handle)
116 :
117 278 : END SUBROUTINE build_2c_coulomb_matrix_kp
118 :
119 : ! **************************************************************************************************
120 : !> \brief ...
121 : !> \param matrix_v_kp ...
122 : !> \param kpoints ...
123 : !> \param basis_type ...
124 : !> \param cell ...
125 : !> \param particle_set ...
126 : !> \param qs_kind_set ...
127 : !> \param atomic_kind_set ...
128 : !> \param size_lattice_sum ...
129 : !> \param matrix_v_L_tmp ...
130 : !> \param operator_type ...
131 : !> \param ikp_start ...
132 : !> \param ikp_end ...
133 : ! **************************************************************************************************
134 278 : SUBROUTINE lattice_sum(matrix_v_kp, kpoints, basis_type, cell, particle_set, &
135 : qs_kind_set, atomic_kind_set, size_lattice_sum, matrix_v_L_tmp, &
136 : operator_type, ikp_start, ikp_end)
137 :
138 : TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_v_kp
139 : TYPE(kpoint_type), POINTER :: kpoints
140 : CHARACTER(LEN=*), INTENT(IN) :: basis_type
141 : TYPE(cell_type), POINTER :: cell
142 : TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
143 : TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
144 : TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
145 : INTEGER :: size_lattice_sum
146 : TYPE(dbcsr_type), POINTER :: matrix_v_L_tmp
147 : INTEGER :: operator_type, ikp_start, ikp_end
148 :
149 : CHARACTER(LEN=*), PARAMETER :: routineN = 'lattice_sum'
150 :
151 : INTEGER :: factor, handle, handle2, i_block, i_x, i_x_inner, i_x_outer, ik, j_y, j_y_inner, &
152 : j_y_outer, k_z, k_z_inner, k_z_outer, x_max, x_min, y_max, y_min, z_max, z_min
153 : INTEGER, DIMENSION(3) :: nkp_grid
154 : REAL(KIND=dp) :: coskl, sinkl
155 : REAL(KIND=dp), DIMENSION(3) :: vec_L, vec_s
156 : REAL(KIND=dp), DIMENSION(3, 3) :: hmat
157 278 : TYPE(two_d_util_type), ALLOCATABLE, DIMENSION(:) :: blocks_v_L, blocks_v_L_store
158 : TYPE(two_d_util_type), ALLOCATABLE, &
159 278 : DIMENSION(:, :, :) :: blocks_v_kp
160 :
161 278 : CALL timeset(routineN, handle)
162 :
163 : CALL get_factor_and_xyz_min_max(cell, kpoints, size_lattice_sum, factor, hmat, &
164 278 : x_min, x_max, y_min, y_max, z_min, z_max, nkp_grid)
165 :
166 278 : CALL allocate_blocks_v_kp(blocks_v_kp, matrix_v_kp, ikp_start, ikp_end)
167 278 : CALL allocate_blocks_v_L(blocks_v_L, matrix_v_L_tmp)
168 278 : CALL allocate_blocks_v_L(blocks_v_L_store, matrix_v_L_tmp)
169 :
170 1494 : DO i_x_inner = 0, 2*nkp_grid(1) - 1
171 12894 : DO j_y_inner = 0, 2*nkp_grid(2) - 1
172 61064 : DO k_z_inner = 0, 2*nkp_grid(3) - 1
173 :
174 147840 : DO i_x_outer = x_min, x_max + nkp_grid(1), 2*nkp_grid(1)
175 475392 : DO j_y_outer = y_min, y_max + nkp_grid(2), 2*nkp_grid(2)
176 1339072 : DO k_z_outer = z_min, z_max + nkp_grid(3), 2*nkp_grid(3)
177 :
178 912128 : i_x = i_x_inner + i_x_outer
179 912128 : j_y = j_y_inner + j_y_outer
180 912128 : k_z = k_z_inner + k_z_outer
181 :
182 : IF (i_x > x_max .OR. i_x < x_min .OR. &
183 : j_y > y_max .OR. j_y < y_min .OR. &
184 912128 : k_z > z_max .OR. k_z < z_min) CYCLE
185 :
186 1263840 : vec_s = [REAL(i_x, dp), REAL(j_y, dp), REAL(k_z, dp)]
187 :
188 4107480 : vec_L = MATMUL(hmat, vec_s)
189 :
190 : ! Compute (P 0 | Q vec_L) and store it in matrix_v_L_tmp
191 : CALL compute_v_transl(matrix_v_L_tmp, blocks_v_L, vec_L, particle_set, &
192 : qs_kind_set, atomic_kind_set, basis_type, cell, &
193 315960 : operator_type)
194 :
195 : !$OMP PARALLEL DO DEFAULT(NONE) &
196 : !$OMP SHARED(blocks_v_L, blocks_v_L_store) &
197 1239680 : !$OMP PRIVATE(i_block)
198 : DO i_block = 1, SIZE(blocks_v_L)
199 : blocks_v_L_store(i_block)%block(:, :) = blocks_v_L_store(i_block)%block(:, :) &
200 : + blocks_v_L(i_block)%block(:, :)
201 : END DO
202 : !$OMP END PARALLEL DO
203 :
204 : END DO
205 : END DO
206 : END DO
207 :
208 48448 : CALL timeset(routineN//"_R_to_k", handle2)
209 :
210 : ! add exp(iq*vec_L) * (P 0 | Q vec_L) to V_PQ(q)
211 304000 : DO ik = ikp_start, ikp_end
212 :
213 : ! coskl and sinkl are identical for all i_x_outer, j_y_outer, k_z_outer
214 1022208 : coskl = COS(twopi*DOT_PRODUCT(vec_s(1:3), kpoints%xkp(1:3, ik)))
215 1022208 : sinkl = SIN(twopi*DOT_PRODUCT(vec_s(1:3), kpoints%xkp(1:3, ik)))
216 :
217 1241264 : DO i_block = 1, SIZE(blocks_v_L)
218 :
219 : blocks_v_kp(ik, 1, i_block)%block(:, :) = blocks_v_kp(ik, 1, i_block)%block(:, :) &
220 288611936 : + coskl*blocks_v_L_store(i_block)%block(:, :)
221 : blocks_v_kp(ik, 2, i_block)%block(:, :) = blocks_v_kp(ik, 2, i_block)%block(:, :) &
222 288867488 : + sinkl*blocks_v_L_store(i_block)%block(:, :)
223 :
224 : END DO
225 :
226 : END DO
227 :
228 215664 : DO i_block = 1, SIZE(blocks_v_L)
229 :
230 21908608 : blocks_v_L_store(i_block)%block(:, :) = 0.0_dp
231 :
232 : END DO
233 :
234 108296 : CALL timestop(handle2)
235 :
236 : END DO
237 : END DO
238 : END DO
239 :
240 278 : CALL set_blocks_to_matrix_v_kp(matrix_v_kp, blocks_v_kp, ikp_start, ikp_end)
241 :
242 278 : CALL deallocate_blocks_v_kp(blocks_v_kp)
243 278 : CALL deallocate_blocks_v_L(blocks_v_L)
244 278 : CALL deallocate_blocks_v_L(blocks_v_L_store)
245 :
246 278 : CALL timestop(handle)
247 :
248 278 : END SUBROUTINE lattice_sum
249 :
250 : ! **************************************************************************************************
251 : !> \brief ...
252 : !> \param matrix_v_kp ...
253 : !> \param blocks_v_kp ...
254 : !> \param ikp_start ...
255 : !> \param ikp_end ...
256 : ! **************************************************************************************************
257 278 : SUBROUTINE set_blocks_to_matrix_v_kp(matrix_v_kp, blocks_v_kp, ikp_start, ikp_end)
258 :
259 : TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_v_kp
260 : TYPE(two_d_util_type), ALLOCATABLE, &
261 : DIMENSION(:, :, :) :: blocks_v_kp
262 : INTEGER :: ikp_start, ikp_end
263 :
264 : CHARACTER(LEN=*), PARAMETER :: routineN = 'set_blocks_to_matrix_v_kp'
265 :
266 : INTEGER :: col, handle, i_block, i_real_im, ik, row
267 278 : REAL(KIND=dp), DIMENSION(:, :), POINTER :: data_block
268 : TYPE(dbcsr_iterator_type) :: iter
269 :
270 278 : CALL timeset(routineN, handle)
271 :
272 1326 : DO ik = ikp_start, ikp_end
273 :
274 3422 : DO i_real_im = 1, 2
275 :
276 2096 : i_block = 1
277 :
278 2096 : CALL dbcsr_iterator_start(iter, matrix_v_kp(ik, i_real_im)%matrix)
279 :
280 9708 : DO WHILE (dbcsr_iterator_blocks_left(iter))
281 :
282 7612 : CALL dbcsr_iterator_next_block(iter, row, col, data_block)
283 :
284 2665308 : data_block(:, :) = blocks_v_kp(ik, i_real_im, i_block)%block(:, :)
285 :
286 7612 : i_block = i_block + 1
287 :
288 : END DO
289 :
290 5240 : CALL dbcsr_iterator_stop(iter)
291 :
292 : END DO
293 :
294 : END DO
295 :
296 278 : CALL timestop(handle)
297 :
298 278 : END SUBROUTINE set_blocks_to_matrix_v_kp
299 :
300 : ! **************************************************************************************************
301 : !> \brief ...
302 : !> \param matrix_v_L_tmp ...
303 : !> \param blocks_v_L ...
304 : !> \param vec_L ...
305 : !> \param particle_set ...
306 : !> \param qs_kind_set ...
307 : !> \param atomic_kind_set ...
308 : !> \param basis_type ...
309 : !> \param cell ...
310 : !> \param operator_type ...
311 : ! **************************************************************************************************
312 315960 : SUBROUTINE compute_v_transl(matrix_v_L_tmp, blocks_v_L, vec_L, particle_set, &
313 : qs_kind_set, atomic_kind_set, basis_type, cell, operator_type)
314 :
315 : TYPE(dbcsr_type), POINTER :: matrix_v_L_tmp
316 : TYPE(two_d_util_type), ALLOCATABLE, DIMENSION(:) :: blocks_v_L
317 : REAL(KIND=dp), DIMENSION(3) :: vec_L
318 : TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
319 : TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
320 : TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
321 : CHARACTER(LEN=*), INTENT(IN) :: basis_type
322 : TYPE(cell_type), POINTER :: cell
323 : INTEGER :: operator_type
324 :
325 : CHARACTER(LEN=*), PARAMETER :: routineN = 'compute_v_transl'
326 :
327 : INTEGER :: col, handle, i_block, kind_a, kind_b, row
328 315960 : INTEGER, ALLOCATABLE, DIMENSION(:) :: kind_of
329 : REAL(dp), DIMENSION(3) :: ra, rab_L, rb
330 315960 : REAL(KIND=dp), DIMENSION(:, :), POINTER :: data_block
331 315960 : REAL(KIND=dp), DIMENSION(:, :, :), POINTER :: contr_a, contr_b
332 : TYPE(dbcsr_iterator_type) :: iter
333 : TYPE(gto_basis_set_type), POINTER :: basis_set_a, basis_set_b
334 :
335 315960 : CALL timeset(routineN, handle)
336 :
337 315960 : NULLIFY (basis_set_a, basis_set_b, data_block)
338 :
339 315960 : CALL get_atomic_kind_set(atomic_kind_set, kind_of=kind_of)
340 :
341 315960 : CALL dbcsr_set(matrix_v_L_tmp, 0.0_dp)
342 :
343 315960 : i_block = 1
344 :
345 315960 : CALL dbcsr_iterator_start(iter, matrix_v_L_tmp)
346 :
347 1553230 : DO WHILE (dbcsr_iterator_blocks_left(iter))
348 :
349 1237270 : CALL dbcsr_iterator_next_block(iter, row, col, data_block)
350 :
351 1237270 : kind_a = kind_of(row)
352 1237270 : kind_b = kind_of(col)
353 :
354 1237270 : CALL get_qs_kind(qs_kind=qs_kind_set(kind_a), basis_set=basis_set_a, basis_type=basis_type)
355 1237270 : CALL get_qs_kind(qs_kind=qs_kind_set(kind_b), basis_set=basis_set_b, basis_type=basis_type)
356 :
357 1237270 : ra(1:3) = pbc(particle_set(row)%r(1:3), cell)
358 1237270 : rb(1:3) = pbc(particle_set(col)%r(1:3), cell)
359 :
360 4949080 : rab_L(1:3) = rb(1:3) - ra(1:3) + vec_L(1:3)
361 :
362 1237270 : CALL contraction_matrix_shg(basis_set_a, contr_a)
363 1237270 : CALL contraction_matrix_shg(basis_set_b, contr_b)
364 :
365 147151376 : blocks_v_L(i_block)%block = 0.0_dp
366 :
367 : CALL int_operators_r12_ab_shg(operator_type, blocks_v_L(i_block)%block, rab=rab_L, &
368 : fba=basis_set_a, fbb=basis_set_b, scona_shg=contr_a, sconb_shg=contr_b, &
369 1237270 : calculate_forces=.FALSE.)
370 :
371 1237270 : i_block = i_block + 1
372 :
373 1237270 : DEALLOCATE (contr_a, contr_b)
374 :
375 : END DO
376 :
377 315960 : CALL dbcsr_iterator_stop(iter)
378 :
379 315960 : DEALLOCATE (kind_of)
380 :
381 315960 : CALL timestop(handle)
382 :
383 315960 : END SUBROUTINE compute_v_transl
384 :
385 : ! **************************************************************************************************
386 : !> \brief ...
387 : !> \param blocks_v_kp ...
388 : ! **************************************************************************************************
389 278 : SUBROUTINE deallocate_blocks_v_kp(blocks_v_kp)
390 : TYPE(two_d_util_type), ALLOCATABLE, &
391 : DIMENSION(:, :, :) :: blocks_v_kp
392 :
393 : CHARACTER(LEN=*), PARAMETER :: routineN = 'deallocate_blocks_v_kp'
394 :
395 : INTEGER :: handle, i_block, i_real_img, ik
396 :
397 278 : CALL timeset(routineN, handle)
398 :
399 1882 : DO ik = LBOUND(blocks_v_kp, 1), UBOUND(blocks_v_kp, 1)
400 3422 : DO i_real_img = 1, SIZE(blocks_v_kp, 2)
401 10756 : DO i_block = 1, SIZE(blocks_v_kp, 3)
402 9708 : DEALLOCATE (blocks_v_kp(ik, i_real_img, i_block)%block)
403 : END DO
404 : END DO
405 : END DO
406 :
407 7890 : DEALLOCATE (blocks_v_kp)
408 :
409 278 : CALL timestop(handle)
410 :
411 278 : END SUBROUTINE deallocate_blocks_v_kp
412 :
413 : ! **************************************************************************************************
414 : !> \brief ...
415 : !> \param blocks_v_L ...
416 : ! **************************************************************************************************
417 556 : SUBROUTINE deallocate_blocks_v_L(blocks_v_L)
418 : TYPE(two_d_util_type), ALLOCATABLE, DIMENSION(:) :: blocks_v_L
419 :
420 : CHARACTER(LEN=*), PARAMETER :: routineN = 'deallocate_blocks_v_L'
421 :
422 : INTEGER :: handle, i_block
423 :
424 556 : CALL timeset(routineN, handle)
425 :
426 2348 : DO i_block = 1, SIZE(blocks_v_L, 1)
427 2348 : DEALLOCATE (blocks_v_L(i_block)%block)
428 : END DO
429 :
430 2348 : DEALLOCATE (blocks_v_L)
431 :
432 556 : CALL timestop(handle)
433 :
434 556 : END SUBROUTINE deallocate_blocks_v_L
435 :
436 : ! **************************************************************************************************
437 : !> \brief ...
438 : !> \param blocks_v_L ...
439 : !> \param matrix_v_L_tmp ...
440 : ! **************************************************************************************************
441 556 : SUBROUTINE allocate_blocks_v_L(blocks_v_L, matrix_v_L_tmp)
442 : TYPE(two_d_util_type), ALLOCATABLE, DIMENSION(:) :: blocks_v_L
443 : TYPE(dbcsr_type), POINTER :: matrix_v_L_tmp
444 :
445 : CHARACTER(LEN=*), PARAMETER :: routineN = 'allocate_blocks_v_L'
446 :
447 : INTEGER :: col, handle, i_block, nblocks, row
448 556 : REAL(KIND=dp), DIMENSION(:, :), POINTER :: data_block
449 : TYPE(dbcsr_iterator_type) :: iter
450 :
451 556 : CALL timeset(routineN, handle)
452 :
453 556 : nblocks = 0
454 :
455 556 : CALL dbcsr_iterator_start(iter, matrix_v_L_tmp)
456 :
457 2348 : DO WHILE (dbcsr_iterator_blocks_left(iter))
458 :
459 1792 : CALL dbcsr_iterator_next_block(iter, row, col, data_block)
460 :
461 1792 : nblocks = nblocks + 1
462 :
463 : END DO
464 :
465 556 : CALL dbcsr_iterator_stop(iter)
466 :
467 3460 : ALLOCATE (blocks_v_L(nblocks))
468 :
469 556 : i_block = 1
470 :
471 556 : CALL dbcsr_iterator_start(iter, matrix_v_L_tmp)
472 :
473 2348 : DO WHILE (dbcsr_iterator_blocks_left(iter))
474 :
475 1792 : CALL dbcsr_iterator_next_block(iter, row, col, data_block)
476 :
477 7168 : ALLOCATE (blocks_v_L(i_block)%block(SIZE(data_block, 1), SIZE(data_block, 2)))
478 425998 : blocks_v_L(i_block)%block = 0.0_dp
479 :
480 4140 : i_block = i_block + 1
481 :
482 : END DO
483 :
484 556 : CALL dbcsr_iterator_stop(iter)
485 :
486 556 : CALL timestop(handle)
487 :
488 1112 : END SUBROUTINE allocate_blocks_v_L
489 :
490 : ! **************************************************************************************************
491 : !> \brief ...
492 : !> \param blocks_v_kp ...
493 : !> \param matrix_v_kp ...
494 : !> \param ikp_start ...
495 : !> \param ikp_end ...
496 : ! **************************************************************************************************
497 278 : SUBROUTINE allocate_blocks_v_kp(blocks_v_kp, matrix_v_kp, ikp_start, ikp_end)
498 : TYPE(two_d_util_type), ALLOCATABLE, &
499 : DIMENSION(:, :, :) :: blocks_v_kp
500 : TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_v_kp
501 : INTEGER :: ikp_start, ikp_end
502 :
503 : CHARACTER(LEN=*), PARAMETER :: routineN = 'allocate_blocks_v_kp'
504 :
505 : INTEGER :: col, handle, i_block, i_real_img, ik, &
506 : nblocks, row
507 278 : REAL(KIND=dp), DIMENSION(:, :), POINTER :: data_block
508 : TYPE(dbcsr_iterator_type) :: iter
509 :
510 278 : CALL timeset(routineN, handle)
511 :
512 278 : nblocks = 0
513 :
514 278 : CALL dbcsr_iterator_start(iter, matrix_v_kp(ikp_start, 1)%matrix)
515 :
516 1174 : DO WHILE (dbcsr_iterator_blocks_left(iter))
517 :
518 896 : CALL dbcsr_iterator_next_block(iter, row, col, data_block)
519 :
520 896 : nblocks = nblocks + 1
521 :
522 : END DO
523 :
524 278 : CALL dbcsr_iterator_stop(iter)
525 :
526 11690 : ALLOCATE (blocks_v_kp(ikp_start:ikp_end, SIZE(matrix_v_kp, 2), nblocks))
527 :
528 1326 : DO ik = ikp_start, ikp_end
529 :
530 3422 : DO i_real_img = 1, SIZE(matrix_v_kp, 2)
531 :
532 2096 : i_block = 1
533 :
534 2096 : CALL dbcsr_iterator_start(iter, matrix_v_kp(ik, i_real_img)%matrix)
535 :
536 9708 : DO WHILE (dbcsr_iterator_blocks_left(iter))
537 :
538 7612 : CALL dbcsr_iterator_next_block(iter, row, col, data_block)
539 :
540 0 : ALLOCATE (blocks_v_kp(ik, i_real_img, i_block)%block(SIZE(data_block, 1), &
541 30448 : SIZE(data_block, 2)))
542 2665308 : blocks_v_kp(ik, i_real_img, i_block)%block = 0.0_dp
543 :
544 17320 : i_block = i_block + 1
545 :
546 : END DO
547 :
548 5240 : CALL dbcsr_iterator_stop(iter)
549 :
550 : END DO
551 :
552 : END DO
553 :
554 278 : CALL timestop(handle)
555 :
556 278 : END SUBROUTINE allocate_blocks_v_kp
557 :
558 : ! **************************************************************************************************
559 : !> \brief ...
560 : !> \param cell ...
561 : !> \param kpoints ...
562 : !> \param size_lattice_sum ...
563 : !> \param factor ...
564 : !> \param hmat ...
565 : !> \param x_min ...
566 : !> \param x_max ...
567 : !> \param y_min ...
568 : !> \param y_max ...
569 : !> \param z_min ...
570 : !> \param z_max ...
571 : !> \param nkp_grid ...
572 : ! **************************************************************************************************
573 316 : SUBROUTINE get_factor_and_xyz_min_max(cell, kpoints, size_lattice_sum, factor, hmat, &
574 : x_min, x_max, y_min, y_max, z_min, z_max, nkp_grid)
575 :
576 : TYPE(cell_type), POINTER :: cell
577 : TYPE(kpoint_type), POINTER :: kpoints
578 : INTEGER :: size_lattice_sum, factor
579 : REAL(KIND=dp), DIMENSION(3, 3) :: hmat
580 : INTEGER :: x_min, x_max, y_min, y_max, z_min, z_max
581 : INTEGER, DIMENSION(3) :: nkp_grid
582 :
583 : CHARACTER(LEN=*), PARAMETER :: routineN = 'get_factor_and_xyz_min_max'
584 :
585 : INTEGER :: handle, nkp
586 : INTEGER, DIMENSION(3) :: periodic
587 :
588 316 : CALL timeset(routineN, handle)
589 :
590 316 : CALL get_kpoint_info(kpoints, nkp_grid=nkp_grid, nkp=nkp)
591 316 : CALL get_cell(cell=cell, h=hmat, periodic=periodic)
592 :
593 316 : IF (periodic(1) == 0) THEN
594 232 : CPASSERT(nkp_grid(1) == 1)
595 : END IF
596 316 : IF (periodic(2) == 0) THEN
597 118 : CPASSERT(nkp_grid(2) == 1)
598 : END IF
599 316 : IF (periodic(3) == 0) THEN
600 162 : CPASSERT(nkp_grid(3) == 1)
601 : END IF
602 :
603 316 : IF (MODULO(nkp_grid(1), 2) == 1) THEN
604 234 : factor = 3**(size_lattice_sum - 1)
605 82 : ELSE IF (MODULO(nkp_grid(1), 2) == 0) THEN
606 82 : factor = 2**(size_lattice_sum - 1)
607 : END IF
608 :
609 316 : IF (MODULO(nkp_grid(1), 2) == 1) THEN
610 234 : x_min = -(factor*nkp_grid(1) - 1)/2
611 234 : x_max = (factor*nkp_grid(1) - 1)/2
612 82 : ELSE IF (MODULO(nkp_grid(1), 2) == 0) THEN
613 82 : x_min = -factor*nkp_grid(1)/2
614 82 : x_max = factor*nkp_grid(1)/2 - 1
615 : END IF
616 316 : IF (periodic(1) == 0) THEN
617 232 : x_min = 0
618 232 : x_max = 0
619 : END IF
620 :
621 316 : IF (MODULO(nkp_grid(2), 2) == 1) THEN
622 120 : y_min = -(factor*nkp_grid(2) - 1)/2
623 120 : y_max = (factor*nkp_grid(2) - 1)/2
624 196 : ELSE IF (MODULO(nkp_grid(2), 2) == 0) THEN
625 196 : y_min = -factor*nkp_grid(2)/2
626 196 : y_max = factor*nkp_grid(2)/2 - 1
627 : END IF
628 316 : IF (periodic(2) == 0) THEN
629 118 : y_min = 0
630 118 : y_max = 0
631 : END IF
632 :
633 316 : IF (MODULO(nkp_grid(3), 2) == 1) THEN
634 162 : z_min = -(factor*nkp_grid(3) - 1)/2
635 162 : z_max = (factor*nkp_grid(3) - 1)/2
636 154 : ELSE IF (MODULO(nkp_grid(3), 2) == 0) THEN
637 154 : z_min = -factor*nkp_grid(3)/2
638 154 : z_max = factor*nkp_grid(3)/2 - 1
639 : END IF
640 316 : IF (periodic(3) == 0) THEN
641 162 : z_min = 0
642 162 : z_max = 0
643 : END IF
644 :
645 316 : CALL timestop(handle)
646 :
647 316 : END SUBROUTINE get_factor_and_xyz_min_max
648 :
649 : ! **************************************************************************************************
650 : !> \brief ...
651 : !> \param matrix_v_L_tmp ...
652 : !> \param matrix_v_kp ...
653 : !> \param ikp_start ...
654 : ! **************************************************************************************************
655 278 : SUBROUTINE allocate_tmp(matrix_v_L_tmp, matrix_v_kp, ikp_start)
656 :
657 : TYPE(dbcsr_type), POINTER :: matrix_v_L_tmp
658 : TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_v_kp
659 : INTEGER :: ikp_start
660 :
661 : CHARACTER(LEN=*), PARAMETER :: routineN = 'allocate_tmp'
662 :
663 : INTEGER :: handle
664 :
665 278 : CALL timeset(routineN, handle)
666 :
667 278 : NULLIFY (matrix_v_L_tmp)
668 278 : CALL dbcsr_init_p(matrix_v_L_tmp)
669 : CALL dbcsr_create(matrix=matrix_v_L_tmp, &
670 : template=matrix_v_kp(ikp_start, 1)%matrix, &
671 278 : matrix_type=dbcsr_type_no_symmetry)
672 278 : CALL dbcsr_reserve_all_blocks(matrix_v_L_tmp)
673 278 : CALL dbcsr_set(matrix_v_L_tmp, 0.0_dp)
674 :
675 278 : CALL timestop(handle)
676 :
677 278 : END SUBROUTINE allocate_tmp
678 :
679 : ! **************************************************************************************************
680 : !> \brief ...
681 : !> \param matrix_v_L_tmp ...
682 : ! **************************************************************************************************
683 278 : SUBROUTINE deallocate_tmp(matrix_v_L_tmp)
684 :
685 : TYPE(dbcsr_type), POINTER :: matrix_v_L_tmp
686 :
687 : CHARACTER(LEN=*), PARAMETER :: routineN = 'deallocate_tmp'
688 :
689 : INTEGER :: handle
690 :
691 278 : CALL timeset(routineN, handle)
692 :
693 278 : CALL dbcsr_release_p(matrix_v_L_tmp)
694 :
695 278 : CALL timestop(handle)
696 :
697 278 : END SUBROUTINE deallocate_tmp
698 :
699 : ! **************************************************************************************************
700 : !> \brief Dense 2-center Coulomb matrix V_PQ(k) from a direct real-space lattice sum
701 : !> \param V_k ...
702 : !> \param qs_env ...
703 : !> \param kpoints ...
704 : !> \param size_lattice_sum ...
705 : !> \param basis_type ...
706 : !> \param ikp_start ...
707 : !> \param ikp_end ...
708 : ! **************************************************************************************************
709 38 : SUBROUTINE build_2c_coulomb_matrix_kp_dense(V_k, qs_env, kpoints, size_lattice_sum, &
710 : basis_type, ikp_start, ikp_end)
711 : COMPLEX(KIND=dp), DIMENSION(:, :, :) :: V_k
712 : TYPE(qs_environment_type), POINTER :: qs_env
713 : TYPE(kpoint_type), POINTER :: kpoints
714 : INTEGER :: size_lattice_sum
715 : CHARACTER(LEN=*), INTENT(IN) :: basis_type
716 : INTEGER :: ikp_start, ikp_end
717 :
718 : CHARACTER(LEN=*), PARAMETER :: routineN = 'build_2c_coulomb_matrix_kp_dense'
719 :
720 : INTEGER :: factor, handle, x_max, x_min, y_max, &
721 : y_min, z_max, z_min
722 : INTEGER, DIMENSION(3) :: nkp_grid
723 : REAL(KIND=dp), DIMENSION(3, 3) :: hmat
724 : TYPE(cell_type), POINTER :: cell
725 :
726 38 : CALL timeset(routineN, handle)
727 :
728 38 : CALL get_qs_env(qs_env=qs_env, cell=cell)
729 :
730 : CALL get_factor_and_xyz_min_max(cell, kpoints, size_lattice_sum, factor, hmat, &
731 38 : x_min, x_max, y_min, y_max, z_min, z_max, nkp_grid)
732 :
733 : CALL lattice_sum_dense(V_k, qs_env, kpoints, basis_type, &
734 38 : x_min, x_max, y_min, y_max, z_min, z_max, ikp_start, ikp_end)
735 :
736 38 : CALL timestop(handle)
737 :
738 38 : END SUBROUTINE build_2c_coulomb_matrix_kp_dense
739 :
740 : ! **************************************************************************************************
741 : !> \brief Adds sum_L exp(ik*L) (P 0 | Q L) to the dense V_PQ(k) for the lattice vectors L within
742 : !> the given bounds
743 : !> \param V_k ...
744 : !> \param qs_env ...
745 : !> \param kpoints ...
746 : !> \param basis_type ...
747 : !> \param x_min ...
748 : !> \param x_max ...
749 : !> \param y_min ...
750 : !> \param y_max ...
751 : !> \param z_min ...
752 : !> \param z_max ...
753 : !> \param ikp_start ...
754 : !> \param ikp_end ...
755 : !> \par On the k mesh, exp(ik*L) depends on L only modulo 2N (N: mesh size). If every k*N is an
756 : !> integer (half-integer, shifted mesh), exp(ik*L) changes by +1 (-1) when L moves by N, so L
757 : !> modulo N suffices. The cells are grouped into such classes, each cell with its sign, and only
758 : !> occupied classes are kept. With more k points than classes, each rank sums the cells of
759 : !> its own classes, a transpose gives each rank its columns Q of all classes, the phase sum is one
760 : !> GEMM per column, and a second transpose brings V_PQ(k) to the owners of k. Communication:
761 : !> n_bf^2 (N_class + 2 N_k)/N_rank per rank. With fewer k points than classes (e.g. a long direct
762 : !> sum at a single k), the cells are distributed over all ranks, each rank adds its cells directly
763 : !> to all k and the N_k matrices are summed.
764 : ! **************************************************************************************************
765 38 : SUBROUTINE lattice_sum_dense(V_k, qs_env, kpoints, basis_type, &
766 : x_min, x_max, y_min, y_max, z_min, z_max, ikp_start, ikp_end)
767 : COMPLEX(KIND=dp), DIMENSION(:, :, :) :: V_k
768 : TYPE(qs_environment_type), POINTER :: qs_env
769 : TYPE(kpoint_type), POINTER :: kpoints
770 : CHARACTER(LEN=*), INTENT(IN) :: basis_type
771 : INTEGER, INTENT(IN) :: x_min, x_max, y_min, y_max, z_min, &
772 : z_max, ikp_start, ikp_end
773 :
774 : CHARACTER(LEN=*), PARAMETER :: routineN = 'lattice_sum_dense'
775 : REAL(KIND=dp), PARAMETER :: batch_bytes = 64.0_dp*1024.0_dp**2, &
776 : chunk_bytes = 256.0_dp*1024.0_dp**2, complex_bytes = 2.0_dp*dp_size, fold_eps = 1.0E-8_dp
777 :
778 : COMPLEX(KIND=dp) :: phase
779 38 : COMPLEX(KIND=dp), ALLOCATABLE, DIMENSION(:) :: recv_complex, send_complex
780 38 : COMPLEX(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :) :: V_part, V_q_cols
781 : INTEGER :: first_k, handle, handle2, i_cell, i_class, i_own, i_rank, i_x, i_x_inner, &
782 : i_x_outer, idim, ik, j_y, j_y_inner, j_y_outer, k_z, k_z_inner, k_z_outer, last_k, &
783 : n_atom, n_batch, n_bf, n_block, n_cells, n_chunk, n_class, n_cols, n_k, num_pe, pos, q_col
784 38 : INTEGER, ALLOCATABLE, DIMENSION(:) :: bf_end_from_atom, bf_start_from_atom, class_first, &
785 38 : col_first, n_own, recv_counts, recv_displs, send_counts, send_displs
786 38 : INTEGER, ALLOCATABLE, DIMENSION(:, :) :: cell_s
787 : INTEGER, DIMENSION(3) :: fold_sign, nkp_grid, period
788 : REAL(KIND=dp) :: arg
789 38 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: cell_sign, k_times_n, recv_real, &
790 38 : send_real
791 38 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: class_ref, cos_phase, im_part, re_part, &
792 38 : sin_phase
793 38 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :) :: V_cols, V_L, V_own
794 : REAL(KIND=dp), DIMENSION(3) :: vec_L
795 38 : TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
796 : TYPE(cell_type), POINTER :: cell
797 : TYPE(mp_para_env_type), POINTER :: para_env
798 38 : TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
799 38 : TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
800 :
801 38 : CALL timeset(routineN, handle)
802 :
803 : CALL get_qs_env(qs_env=qs_env, &
804 : para_env=para_env, &
805 : particle_set=particle_set, &
806 : cell=cell, &
807 : qs_kind_set=qs_kind_set, &
808 38 : atomic_kind_set=atomic_kind_set)
809 :
810 38 : CALL get_kpoint_info(kpoints, nkp_grid=nkp_grid)
811 :
812 38 : CALL get_basis_sizes(qs_env, n_atom, basis_type, bf_start_from_atom, bf_end_from_atom, n_bf)
813 :
814 114 : ALLOCATE (k_times_n(ikp_end - ikp_start + 1))
815 152 : DO idim = 1, 3
816 30852 : k_times_n(:) = kpoints%xkp(idim, ikp_start:ikp_end)*REAL(nkp_grid(idim), dp)
817 114 : period(idim) = 2*nkp_grid(idim)
818 114 : fold_sign(idim) = 1
819 10410 : IF (ALL(ABS(k_times_n - ANINT(k_times_n)) < fold_eps)) THEN
820 50 : period(idim) = nkp_grid(idim)
821 20544 : ELSE IF (ALL(ABS(k_times_n - ANINT(k_times_n - 0.5_dp) - 0.5_dp) < fold_eps)) THEN
822 64 : period(idim) = nkp_grid(idim)
823 64 : fold_sign(idim) = -1
824 : END IF
825 : END DO
826 38 : DEALLOCATE (k_times_n)
827 :
828 : ! Cells within the bounds, ordered by their class L mod period; classes without a cell are skipped.
829 : ! A class has the phase of its reference vector [x_min, y_min, z_min] + (L mod period).
830 38 : n_cells = (x_max - x_min + 1)*(y_max - y_min + 1)*(z_max - z_min + 1)
831 0 : ALLOCATE (cell_s(3, n_cells), cell_sign(n_cells), class_first(n_cells + 1), &
832 342 : class_ref(3, n_cells))
833 38 : n_cells = 0
834 38 : n_class = 0
835 176 : DO i_x_inner = 0, period(1) - 1
836 3442 : DO j_y_inner = 0, period(2) - 1
837 23918 : DO k_z_inner = 0, period(3) - 1
838 20514 : i_cell = n_cells
839 48900 : DO i_x_outer = x_min, x_max, period(1)
840 252854 : DO j_y_outer = y_min, y_max, period(2)
841 1726534 : DO k_z_outer = z_min, z_max, period(3)
842 1494194 : i_x = i_x_inner + i_x_outer
843 1494194 : j_y = j_y_inner + j_y_outer
844 1494194 : k_z = k_z_inner + k_z_outer
845 1494194 : IF (i_x > x_max .OR. j_y > y_max .OR. k_z > z_max) CYCLE
846 1494194 : n_cells = n_cells + 1
847 5976776 : cell_s(:, n_cells) = [i_x, j_y, k_z]
848 : cell_sign(n_cells) = REAL(fold_sign(1)**((i_x_outer - x_min)/period(1))* &
849 : fold_sign(2)**((j_y_outer - y_min)/period(2))* &
850 1698148 : fold_sign(3)**((k_z_outer - z_min)/period(3)), dp)
851 : END DO
852 : END DO
853 : END DO
854 23780 : IF (n_cells > i_cell) THEN
855 20514 : n_class = n_class + 1
856 20514 : class_first(n_class) = i_cell + 1
857 82056 : class_ref(:, n_class) = REAL([i_x_inner + x_min, j_y_inner + y_min, k_z_inner + z_min], dp)
858 : END IF
859 : END DO
860 : END DO
861 : END DO
862 38 : class_first(n_class + 1) = n_cells + 1
863 :
864 : ! batch_bytes bounds the memory of the summed V_L of one batch [bytes].
865 38 : IF (ikp_end - ikp_start + 1 < n_class) THEN
866 : ! Fewer k points than classes: sum each own cell directly into all k, reduce N_k matrices.
867 38 : n_batch = MAX(1, MIN(ikp_end - ikp_start + 1, INT(batch_bytes/(complex_bytes*REAL(n_bf, dp)**2))))
868 304 : ALLOCATE (V_L(n_bf, 1, n_bf), V_part(n_bf, n_bf, n_batch))
869 76 : DO first_k = ikp_start, ikp_end, n_batch
870 38 : last_k = MIN(ikp_end, first_k + n_batch - 1)
871 38 : V_part(:, :, :) = z_zero
872 1494232 : DO i_cell = 1, n_cells
873 1494194 : IF (MODULO(i_cell, para_env%num_pe) /= para_env%mepos) CYCLE
874 747097 : V_L(:, :, :) = 0.0_dp
875 11953552 : vec_L = MATMUL(cell%hmat, REAL(cell_s(:, i_cell), dp))
876 : CALL add_V_L(V_L(:, 1, :), 1.0_dp, vec_L, n_atom, bf_start_from_atom, bf_end_from_atom, &
877 747097 : particle_set, qs_kind_set, atomic_kind_set, basis_type, cell)
878 : !$OMP PARALLEL DO DEFAULT(NONE) SCHEDULE(STATIC) &
879 747135 : !$OMP SHARED(V_part, V_L, kpoints, cell_s, i_cell, first_k, last_k) PRIVATE(ik, arg, phase)
880 : DO ik = first_k, last_k
881 : arg = twopi*DOT_PRODUCT(REAL(cell_s(:, i_cell), dp), kpoints%xkp(1:3, ik))
882 : phase = EXP(CMPLX(0.0_dp, arg, KIND=dp))
883 : V_part(:, :, ik - first_k + 1) = V_part(:, :, ik - first_k + 1) + phase*V_L(:, 1, :)
884 : END DO
885 : !$OMP END PARALLEL DO
886 : END DO
887 38 : CALL para_env%sum(V_part(:, :, 1:last_k - first_k + 1))
888 10322 : DO ik = first_k, last_k
889 10246 : IF (MODULO(ik, para_env%num_pe) /= para_env%mepos) CYCLE
890 : V_k(:, :, (ik - 1)/para_env%num_pe + 1) = V_k(:, :, (ik - 1)/para_env%num_pe + 1) + &
891 215311 : V_part(:, :, ik - first_k + 1)
892 : END DO
893 : END DO
894 38 : DEALLOCATE (V_L, V_part, cell_s, cell_sign, class_first, class_ref)
895 38 : CALL timestop(handle)
896 38 : RETURN
897 : END IF
898 :
899 : ! Rank r owns the classes r + 1, r + 1 + num_pe, ... and the columns col_first(r):col_first(r + 1) - 1.
900 0 : num_pe = para_env%num_pe
901 0 : ALLOCATE (col_first(0:num_pe), n_own(0:num_pe - 1))
902 0 : DO i_rank = 0, num_pe
903 0 : col_first(i_rank) = (i_rank*n_bf)/num_pe + 1
904 : END DO
905 0 : DO i_rank = 0, num_pe - 1
906 0 : n_own(i_rank) = (n_class - i_rank + num_pe - 1)/num_pe
907 : END DO
908 0 : n_cols = col_first(para_env%mepos + 1) - col_first(para_env%mepos)
909 0 : ALLOCATE (V_own(n_bf, n_bf, n_own(para_env%mepos)), source=0.0_dp)
910 0 : DO i_own = 1, n_own(para_env%mepos)
911 0 : i_class = para_env%mepos + 1 + (i_own - 1)*num_pe
912 0 : DO i_cell = class_first(i_class), class_first(i_class + 1) - 1
913 0 : vec_L = MATMUL(cell%hmat, REAL(cell_s(:, i_cell), dp))
914 : CALL add_V_L(V_own(:, :, i_own), cell_sign(i_cell), vec_L, n_atom, &
915 : bf_start_from_atom, bf_end_from_atom, particle_set, qs_kind_set, &
916 0 : atomic_kind_set, basis_type, cell)
917 : END DO
918 : END DO
919 :
920 0 : CALL timeset(routineN//"_R_to_k", handle2)
921 : ! Transpose: each rank receives its columns Q of (P 0 | Q L_class) for all classes.
922 : ALLOCATE (send_counts(0:num_pe - 1), send_displs(0:num_pe - 1), &
923 0 : recv_counts(0:num_pe - 1), recv_displs(0:num_pe - 1))
924 0 : DO i_rank = 0, num_pe - 1
925 0 : send_counts(i_rank) = n_bf*(col_first(i_rank + 1) - col_first(i_rank))*n_own(para_env%mepos)
926 0 : recv_counts(i_rank) = n_bf*n_cols*n_own(i_rank)
927 : END DO
928 0 : send_displs(0) = 0
929 0 : recv_displs(0) = 0
930 0 : DO i_rank = 1, num_pe - 1
931 0 : send_displs(i_rank) = send_displs(i_rank - 1) + send_counts(i_rank - 1)
932 0 : recv_displs(i_rank) = recv_displs(i_rank - 1) + recv_counts(i_rank - 1)
933 : END DO
934 0 : ALLOCATE (send_real(SUM(send_counts)), recv_real(SUM(recv_counts)))
935 0 : pos = 0
936 0 : DO i_rank = 0, num_pe - 1
937 0 : n_block = n_bf*(col_first(i_rank + 1) - col_first(i_rank))
938 0 : DO i_own = 1, n_own(para_env%mepos)
939 : send_real(pos + 1:pos + n_block) = &
940 0 : RESHAPE(V_own(:, col_first(i_rank):col_first(i_rank + 1) - 1, i_own), [n_block])
941 0 : pos = pos + n_block
942 : END DO
943 : END DO
944 0 : DEALLOCATE (V_own)
945 0 : CALL para_env%alltoall(send_real, send_counts, send_displs, recv_real, recv_counts, recv_displs)
946 0 : DEALLOCATE (send_real)
947 : ! V_cols(P, class, Q): the classes of one column Q form the GEMM operand of that column.
948 0 : ALLOCATE (V_cols(n_bf, n_class, n_cols))
949 0 : pos = 0
950 0 : DO i_rank = 0, num_pe - 1
951 0 : DO i_own = 1, n_own(i_rank)
952 0 : V_cols(:, i_rank + 1 + (i_own - 1)*num_pe, :) = RESHAPE(recv_real(pos + 1:pos + n_bf*n_cols), [n_bf, n_cols])
953 0 : pos = pos + n_bf*n_cols
954 : END DO
955 : END DO
956 0 : DEALLOCATE (recv_real)
957 :
958 : ! Phase sum for chunks of k points; the chunk size is the same on all ranks.
959 : n_chunk = INT(MIN(REAL(ikp_end - ikp_start + 1, dp), &
960 : chunk_bytes/(complex_bytes*REAL(n_bf, dp)*REAL((n_bf + num_pe - 1)/num_pe, dp)), &
961 0 : chunk_bytes/(complex_bytes*REAL(n_class, dp))))
962 0 : n_chunk = MAX(1, n_chunk)
963 0 : ALLOCATE (cos_phase(n_class, n_chunk), sin_phase(n_class, n_chunk), V_q_cols(n_bf, n_chunk, n_cols))
964 0 : DO first_k = ikp_start, ikp_end, n_chunk
965 0 : last_k = MIN(ikp_end, first_k + n_chunk - 1)
966 0 : n_k = last_k - first_k + 1
967 0 : DO ik = first_k, last_k
968 0 : DO i_class = 1, n_class
969 0 : phase = EXP(CMPLX(0.0_dp, twopi*DOT_PRODUCT(class_ref(:, i_class), kpoints%xkp(1:3, ik)), KIND=dp))
970 0 : cos_phase(i_class, ik - first_k + 1) = REAL(phase, KIND=dp)
971 0 : sin_phase(i_class, ik - first_k + 1) = AIMAG(phase)
972 : END DO
973 : END DO
974 : !$OMP PARALLEL DEFAULT(NONE) &
975 : !$OMP SHARED(V_cols, cos_phase, sin_phase, V_q_cols, n_bf, n_class, n_cols, n_k) &
976 0 : !$OMP PRIVATE(q_col, re_part, im_part)
977 : ALLOCATE (re_part(n_bf, n_k), im_part(n_bf, n_k))
978 : !$OMP DO SCHEDULE(STATIC)
979 : DO q_col = 1, n_cols
980 : CALL dgemm('N', 'N', n_bf, n_k, n_class, 1.0_dp, V_cols(:, :, q_col), n_bf, &
981 : cos_phase, n_class, 0.0_dp, re_part, n_bf)
982 : CALL dgemm('N', 'N', n_bf, n_k, n_class, 1.0_dp, V_cols(:, :, q_col), n_bf, &
983 : sin_phase, n_class, 0.0_dp, im_part, n_bf)
984 : V_q_cols(:, 1:n_k, q_col) = CMPLX(re_part, im_part, KIND=dp)
985 : END DO
986 : !$OMP END DO
987 : DEALLOCATE (re_part, im_part)
988 : !$OMP END PARALLEL
989 :
990 : ! Second transpose: the owner of k receives V_PQ(k) for the columns of every rank.
991 0 : DO i_rank = 0, num_pe - 1
992 0 : send_counts(i_rank) = n_bf*n_cols*COUNT([(MODULO(ik, num_pe) == i_rank, ik=first_k, last_k)])
993 : recv_counts(i_rank) = n_bf*(col_first(i_rank + 1) - col_first(i_rank))* &
994 0 : COUNT([(MODULO(ik, num_pe) == para_env%mepos, ik=first_k, last_k)])
995 : END DO
996 0 : DO i_rank = 1, num_pe - 1
997 0 : send_displs(i_rank) = send_displs(i_rank - 1) + send_counts(i_rank - 1)
998 0 : recv_displs(i_rank) = recv_displs(i_rank - 1) + recv_counts(i_rank - 1)
999 : END DO
1000 0 : ALLOCATE (send_complex(SUM(send_counts)), recv_complex(SUM(recv_counts)))
1001 0 : pos = 0
1002 0 : DO i_rank = 0, num_pe - 1
1003 0 : DO ik = first_k, last_k
1004 0 : IF (MODULO(ik, num_pe) /= i_rank) CYCLE
1005 0 : send_complex(pos + 1:pos + n_bf*n_cols) = RESHAPE(V_q_cols(:, ik - first_k + 1, :), [n_bf*n_cols])
1006 0 : pos = pos + n_bf*n_cols
1007 : END DO
1008 : END DO
1009 0 : CALL para_env%alltoall(send_complex, send_counts, send_displs, recv_complex, recv_counts, recv_displs)
1010 0 : pos = 0
1011 0 : DO i_rank = 0, num_pe - 1
1012 0 : n_block = n_bf*(col_first(i_rank + 1) - col_first(i_rank))
1013 0 : DO ik = first_k, last_k
1014 0 : IF (MODULO(ik, num_pe) /= para_env%mepos) CYCLE
1015 : V_k(:, col_first(i_rank):col_first(i_rank + 1) - 1, (ik - 1)/num_pe + 1) = &
1016 : V_k(:, col_first(i_rank):col_first(i_rank + 1) - 1, (ik - 1)/num_pe + 1) + &
1017 0 : RESHAPE(recv_complex(pos + 1:pos + n_block), [n_bf, col_first(i_rank + 1) - col_first(i_rank)])
1018 0 : pos = pos + n_block
1019 : END DO
1020 : END DO
1021 0 : DEALLOCATE (send_complex, recv_complex)
1022 : END DO
1023 0 : CALL timestop(handle2)
1024 :
1025 0 : DEALLOCATE (V_cols, V_q_cols, cos_phase, sin_phase, col_first, n_own, send_counts, send_displs, &
1026 0 : recv_counts, recv_displs, cell_s, cell_sign, class_first, class_ref)
1027 :
1028 0 : CALL timestop(handle)
1029 :
1030 76 : END SUBROUTINE lattice_sum_dense
1031 :
1032 : ! **************************************************************************************************
1033 : !> \brief ...
1034 : !> \param qs_env ...
1035 : !> \param n_atom ...
1036 : !> \param basis_type ...
1037 : !> \param bf_start_from_atom ...
1038 : !> \param bf_end_from_atom ...
1039 : !> \param n_bf ...
1040 : ! **************************************************************************************************
1041 56 : SUBROUTINE get_basis_sizes(qs_env, n_atom, basis_type, bf_start_from_atom, bf_end_from_atom, n_bf)
1042 :
1043 : TYPE(qs_environment_type), POINTER :: qs_env
1044 : INTEGER :: n_atom
1045 : CHARACTER(LEN=*), INTENT(IN) :: basis_type
1046 : INTEGER, ALLOCATABLE, DIMENSION(:) :: bf_start_from_atom, bf_end_from_atom
1047 : INTEGER :: n_bf
1048 :
1049 : CHARACTER(LEN=*), PARAMETER :: routineN = 'get_basis_sizes'
1050 :
1051 : INTEGER :: handle, iatom, ikind, n_kind, nsgf
1052 56 : INTEGER, ALLOCATABLE, DIMENSION(:) :: kind_of
1053 56 : TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
1054 : TYPE(gto_basis_set_type), POINTER :: basis_set_a
1055 56 : TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
1056 56 : TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
1057 :
1058 56 : CALL timeset(routineN, handle)
1059 :
1060 : CALL get_qs_env(qs_env=qs_env, particle_set=particle_set, &
1061 56 : qs_kind_set=qs_kind_set, atomic_kind_set=atomic_kind_set)
1062 56 : CALL get_atomic_kind_set(atomic_kind_set, kind_of=kind_of)
1063 :
1064 56 : n_atom = SIZE(particle_set)
1065 56 : n_kind = SIZE(qs_kind_set)
1066 :
1067 144 : DO ikind = 1, n_kind
1068 : CALL get_qs_kind(qs_kind=qs_kind_set(ikind), basis_set=basis_set_a, &
1069 88 : basis_type=basis_type)
1070 144 : CPASSERT(ASSOCIATED(basis_set_a))
1071 : END DO
1072 :
1073 224 : ALLOCATE (bf_start_from_atom(n_atom), bf_end_from_atom(n_atom))
1074 :
1075 56 : n_bf = 0
1076 196 : DO iatom = 1, n_atom
1077 140 : bf_start_from_atom(iatom) = n_bf + 1
1078 140 : ikind = kind_of(iatom)
1079 140 : CALL get_qs_kind(qs_kind=qs_kind_set(ikind), nsgf=nsgf, basis_type=basis_type)
1080 140 : n_bf = n_bf + nsgf
1081 196 : bf_end_from_atom(iatom) = n_bf
1082 : END DO
1083 :
1084 56 : CALL timestop(handle)
1085 :
1086 112 : END SUBROUTINE get_basis_sizes
1087 :
1088 : ! **************************************************************************************************
1089 : !> \brief Adds factor*(P 0 | Q vec_L) to V_L
1090 : !> \param V_L ...
1091 : !> \param factor ...
1092 : !> \param vec_L ...
1093 : !> \param n_atom ...
1094 : !> \param bf_start_from_atom ...
1095 : !> \param bf_end_from_atom ...
1096 : !> \param particle_set ...
1097 : !> \param qs_kind_set ...
1098 : !> \param atomic_kind_set ...
1099 : !> \param basis_type ...
1100 : !> \param cell ...
1101 : ! **************************************************************************************************
1102 1494194 : SUBROUTINE add_V_L(V_L, factor, vec_L, n_atom, bf_start_from_atom, bf_end_from_atom, &
1103 : particle_set, qs_kind_set, atomic_kind_set, basis_type, cell)
1104 :
1105 : REAL(KIND=dp), DIMENSION(:, :) :: V_L
1106 : REAL(KIND=dp), INTENT(IN) :: factor
1107 : REAL(KIND=dp), DIMENSION(3) :: vec_L
1108 : INTEGER :: n_atom
1109 : INTEGER, ALLOCATABLE, DIMENSION(:) :: bf_start_from_atom, bf_end_from_atom
1110 : TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
1111 : TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
1112 : TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
1113 : CHARACTER(LEN=*), INTENT(IN) :: basis_type
1114 : TYPE(cell_type), POINTER :: cell
1115 :
1116 : CHARACTER(LEN=*), PARAMETER :: routineN = 'add_V_L'
1117 :
1118 : INTEGER :: a_1, a_2, atom_a, atom_b, b_1, b_2, &
1119 : handle, kind_a, kind_b
1120 747097 : INTEGER, ALLOCATABLE, DIMENSION(:) :: kind_of
1121 : REAL(dp), DIMENSION(3) :: ra, rab_L, rb
1122 747097 : REAL(KIND=dp), DIMENSION(:, :), POINTER :: V_L_ab
1123 747097 : REAL(KIND=dp), DIMENSION(:, :, :), POINTER :: contr_a, contr_b
1124 : TYPE(gto_basis_set_type), POINTER :: basis_set_a, basis_set_b
1125 :
1126 747097 : CALL timeset(routineN, handle)
1127 :
1128 747097 : CALL get_atomic_kind_set(atomic_kind_set, kind_of=kind_of)
1129 :
1130 : ! fgamma tabulates F_n(t) on first use, which is not thread safe.
1131 747097 : CALL init_md_ftable(2*basis_lmax(qs_kind_set, basis_type))
1132 :
1133 : ! Each atom pair owns its block of V_L.
1134 : !$OMP PARALLEL DO DEFAULT(NONE) COLLAPSE(2) SCHEDULE(DYNAMIC) &
1135 : !$OMP SHARED(V_L, factor, vec_L, n_atom, bf_start_from_atom, bf_end_from_atom, particle_set, &
1136 : !$OMP qs_kind_set, basis_type, cell, kind_of) &
1137 : !$OMP PRIVATE(atom_a, atom_b, kind_a, kind_b, basis_set_a, basis_set_b, ra, rb, rab_L, &
1138 747097 : !$OMP contr_a, contr_b, a_1, a_2, b_1, b_2, V_L_ab)
1139 : DO atom_a = 1, n_atom
1140 :
1141 : DO atom_b = 1, n_atom
1142 :
1143 : kind_a = kind_of(atom_a)
1144 : kind_b = kind_of(atom_b)
1145 :
1146 : CALL get_qs_kind(qs_kind=qs_kind_set(kind_a), basis_set=basis_set_a, &
1147 : basis_type=basis_type)
1148 : CALL get_qs_kind(qs_kind=qs_kind_set(kind_b), basis_set=basis_set_b, &
1149 : basis_type=basis_type)
1150 :
1151 : ra(1:3) = pbc(particle_set(atom_a)%r(1:3), cell)
1152 : rb(1:3) = pbc(particle_set(atom_b)%r(1:3), cell)
1153 :
1154 : rab_L(1:3) = rb(1:3) - ra(1:3) + vec_L(1:3)
1155 :
1156 : CALL contraction_matrix_shg(basis_set_a, contr_a)
1157 : CALL contraction_matrix_shg(basis_set_b, contr_b)
1158 :
1159 : a_1 = bf_start_from_atom(atom_a)
1160 : a_2 = bf_end_from_atom(atom_a)
1161 : b_1 = bf_start_from_atom(atom_b)
1162 : b_2 = bf_end_from_atom(atom_b)
1163 :
1164 : ALLOCATE (V_L_ab(a_2 - a_1 + 1, b_2 - b_1 + 1))
1165 :
1166 : CALL int_operators_r12_ab_shg(operator_coulomb, V_L_ab, rab=rab_L, &
1167 : fba=basis_set_a, fbb=basis_set_b, &
1168 : scona_shg=contr_a, sconb_shg=contr_b, &
1169 : calculate_forces=.FALSE.)
1170 :
1171 : V_L(a_1:a_2, b_1:b_2) = V_L(a_1:a_2, b_1:b_2) + factor*V_L_ab(:, :)
1172 :
1173 : DEALLOCATE (contr_a, contr_b, V_L_ab)
1174 :
1175 : END DO
1176 :
1177 : END DO
1178 : !$OMP END PARALLEL DO
1179 :
1180 747097 : DEALLOCATE (kind_of)
1181 :
1182 747097 : CALL timestop(handle)
1183 :
1184 1494194 : END SUBROUTINE add_V_L
1185 :
1186 : ! **************************************************************************************************
1187 : !> \brief Adds the reciprocal Ewald part sum_L exp(ik*L) (P 0 | erf(omega r)/r | Q L) to the dense
1188 : !> V_PQ(k) of the owned k points. If V_k holds the erfc(omega r)/r lattice sum on entry, it
1189 : !> holds the Coulomb matrix on exit.
1190 : !> \param V_k ...
1191 : !> \param qs_env ...
1192 : !> \param local_q Fractional q coordinates in the local order of V_k.
1193 : !> \param basis_type ...
1194 : !> \param omega range-separation parameter of the Ewald splitting
1195 : ! **************************************************************************************************
1196 22 : SUBROUTINE add_ewald_reciprocal(V_k, qs_env, local_q, basis_type, omega)
1197 : COMPLEX(KIND=dp), INTENT(INOUT) :: V_k(:, :, :)
1198 : TYPE(qs_environment_type), POINTER :: qs_env
1199 : REAL(KIND=dp), INTENT(IN) :: local_q(:, :)
1200 : CHARACTER(LEN=*), INTENT(IN) :: basis_type
1201 : REAL(KIND=dp), INTENT(IN) :: omega
1202 :
1203 : CHARACTER(LEN=*), PARAMETER :: routineN = 'add_ewald_reciprocal'
1204 : INTEGER, PARAMETER :: n_block = 128
1205 :
1206 22 : COMPLEX(KIND=dp), ALLOCATABLE :: fourier_basis(:, :), scaled_basis(:, :)
1207 : INTEGER :: bounds(3), handle, idim, ikp_local, ix, &
1208 : iy, iz, n_atom, n_bf, n_col
1209 22 : INTEGER, ALLOCATABLE, DIMENSION(:) :: bf_end, bf_start
1210 : REAL(KIND=dp) :: cutoff, p(3), p2, prefactor, q(3), &
1211 : reciprocal(3, 3)
1212 : TYPE(cell_type), POINTER :: cell
1213 22 : TYPE(particle_type), POINTER :: particles(:)
1214 22 : TYPE(qs_kind_type), POINTER :: qs_kinds(:)
1215 :
1216 22 : CALL timeset(routineN, handle)
1217 22 : CPASSERT(SIZE(local_q, 1) == 3)
1218 22 : CPASSERT(SIZE(local_q, 2) == SIZE(V_k, 3))
1219 22 : IF (SIZE(local_q, 2) == 0) THEN
1220 4 : CALL timestop(handle)
1221 : RETURN
1222 : END IF
1223 :
1224 18 : CALL get_qs_env(qs_env, cell=cell, particle_set=particles, qs_kind_set=qs_kinds)
1225 18 : CALL get_basis_sizes(qs_env, n_atom, basis_type, bf_start, bf_end, n_bf)
1226 72 : IF (COUNT(cell%perd /= 0) == 2) THEN
1227 6 : CALL add_ewald_reciprocal_2d(V_k, qs_env, local_q, basis_type, omega, bf_start, bf_end)
1228 : ELSE
1229 48 : CPASSERT(COUNT(cell%perd /= 0) == 3)
1230 156 : reciprocal = twopi*TRANSPOSE(cell%h_inv)
1231 12 : cutoff = 2.0_dp*omega*SQRT(ewald_tail_exponent + ewald_lmax_margin*basis_lmax(qs_kinds, basis_type))
1232 : ! Each thread handles its own k points of this rank and adds n_block momenta p per GEMM.
1233 : !$OMP PARALLEL DEFAULT(NONE) &
1234 : !$OMP SHARED(V_k, local_q, cell, reciprocal, cutoff, omega, &
1235 : !$OMP particles, qs_kinds, basis_type, bf_start, bf_end, n_bf) &
1236 : !$OMP PRIVATE(ikp_local, q, idim, bounds, ix, iy, iz, p, p2, prefactor, n_col, &
1237 12 : !$OMP fourier_basis, scaled_basis)
1238 : ALLOCATE (fourier_basis(n_bf, n_block), scaled_basis(n_bf, n_block))
1239 : !$OMP DO SCHEDULE(DYNAMIC)
1240 : DO ikp_local = 1, SIZE(local_q, 2)
1241 : q = local_q(:, ikp_local)
1242 : DO idim = 1, 3
1243 : bounds(idim) = CEILING(cutoff*NORM2(cell%hmat(:, idim))/twopi + ABS(q(idim)))
1244 : END DO
1245 : n_col = 0
1246 : DO ix = -bounds(1), bounds(1)
1247 : DO iy = -bounds(2), bounds(2)
1248 : DO iz = -bounds(3), bounds(3)
1249 : p = MATMUL(reciprocal, q + REAL([ix, iy, iz], dp))
1250 : p2 = SUM(p*p)
1251 : IF (p2 > cutoff**2) CYCLE
1252 : n_col = n_col + 1
1253 : CALL gaussian_basis_fourier(p, particles, qs_kinds, cell, basis_type, &
1254 : bf_start, bf_end, fourier_basis(:, n_col))
1255 : IF (p2 <= 0.0_dp) THEN
1256 : prefactor = -pi/(cell%deth*omega**2)
1257 : ELSE
1258 : prefactor = fourpi*EXP(-p2/(4.0_dp*omega**2))/(cell%deth*p2)
1259 : END IF
1260 : scaled_basis(:, n_col) = prefactor*fourier_basis(:, n_col)
1261 : IF (n_col == n_block) THEN
1262 : CALL zgemm('N', 'C', n_bf, n_bf, n_col, z_one, scaled_basis, n_bf, &
1263 : fourier_basis, n_bf, z_one, V_k(:, :, ikp_local), n_bf)
1264 : n_col = 0
1265 : END IF
1266 : END DO
1267 : END DO
1268 : END DO
1269 : CALL zgemm('N', 'C', n_bf, n_bf, n_col, z_one, scaled_basis, n_bf, &
1270 : fourier_basis, n_bf, z_one, V_k(:, :, ikp_local), n_bf)
1271 : END DO
1272 : !$OMP END DO
1273 : DEALLOCATE (fourier_basis, scaled_basis)
1274 : !$OMP END PARALLEL
1275 : END IF
1276 :
1277 18 : CALL timestop(handle)
1278 :
1279 66 : END SUBROUTINE add_ewald_reciprocal
1280 :
1281 : ! **************************************************************************************************
1282 : !> \brief Reciprocal 2D Ewald Coulomb matrix for any pair of periodic cell axes.
1283 : !> The momentum perpendicular to their plane is integrated continuously.
1284 : !> \param V_k ...
1285 : !> \param qs_env ...
1286 : !> \param local_q Fractional q coordinates in the local order of V_k.
1287 : !> \param basis_type ...
1288 : !> \param omega ...
1289 : !> \param bf_start ...
1290 : !> \param bf_end ...
1291 : ! **************************************************************************************************
1292 6 : SUBROUTINE add_ewald_reciprocal_2d(V_k, qs_env, local_q, basis_type, omega, bf_start, bf_end)
1293 : COMPLEX(KIND=dp), INTENT(INOUT) :: V_k(:, :, :)
1294 : TYPE(qs_environment_type), POINTER :: qs_env
1295 : REAL(KIND=dp), INTENT(IN) :: local_q(:, :)
1296 : CHARACTER(LEN=*), INTENT(IN) :: basis_type
1297 : REAL(KIND=dp), INTENT(IN) :: omega
1298 : INTEGER, INTENT(IN) :: bf_start(:), bf_end(:)
1299 :
1300 : INTEGER, PARAMETER :: n_kz = 192
1301 :
1302 6 : COMPLEX(KIND=dp), ALLOCATABLE :: f_minus(:), f_plus(:), f_zero(:), &
1303 6 : left(:, :), right(:, :)
1304 : INTEGER :: free_axis, i_col, ikp_local, inode, ix, &
1305 : iy, n_bf, nx, ny, periodic_axis(2)
1306 : REAL(KIND=dp) :: a1(3), a2(3), area, b1(3), b2(3), &
1307 : cutoff, factor, kz, nodes(n_kz), &
1308 : normal(3), p(3), pnorm, q(3), weight, &
1309 : weights(n_kz)
1310 : TYPE(cell_type), POINTER :: cell
1311 6 : TYPE(particle_type), POINTER :: particles(:)
1312 6 : TYPE(qs_kind_type), POINTER :: qs_kinds(:)
1313 :
1314 : CALL get_qs_env(qs_env, cell=cell, &
1315 6 : particle_set=particles, qs_kind_set=qs_kinds)
1316 24 : CPASSERT(COUNT(cell%perd /= 0) == 2)
1317 24 : periodic_axis = PACK([1, 2, 3], cell%perd /= 0)
1318 18 : free_axis = FINDLOC(cell%perd, 0, DIM=1)
1319 24 : a1 = cell%hmat(:, periodic_axis(1))
1320 24 : a2 = cell%hmat(:, periodic_axis(2))
1321 6 : normal = vector_product(a1, a2)
1322 24 : area = NORM2(normal)
1323 6 : CPASSERT(area > 0.0_dp)
1324 24 : normal = normal/area
1325 30 : b1 = twopi*vector_product(a2, normal)/area
1326 30 : b2 = twopi*vector_product(normal, a1)/area
1327 6 : n_bf = SIZE(V_k, 1)
1328 6 : cutoff = 2.0_dp*omega*SQRT(ewald_tail_exponent + ewald_lmax_margin*basis_lmax(qs_kinds, basis_type))
1329 : ! Legendre nodes and weights on [0, cutoff] for the free momentum.
1330 6 : CALL gauss_legendre(n_kz, nodes, weights)
1331 1158 : nodes(:) = 0.5_dp*cutoff*(nodes + 1.0_dp)
1332 1158 : weights(:) = 0.5_dp*cutoff*weights
1333 : ! Each thread handles its own k points of this rank and adds all terms of one momentum p
1334 : ! as SUM_i left_i right_i^H in one GEMM.
1335 : !$OMP PARALLEL DEFAULT(NONE) &
1336 : !$OMP SHARED(V_k, local_q, free_axis, periodic_axis, cutoff, &
1337 : !$OMP a1, a2, b1, b2, normal, area, omega, nodes, weights, particles, qs_kinds, cell, basis_type, &
1338 : !$OMP bf_start, bf_end, n_bf) &
1339 : !$OMP PRIVATE(ikp_local, q, nx, ny, ix, iy, p, pnorm, factor, inode, kz, weight, i_col, &
1340 6 : !$OMP f_zero, f_plus, f_minus, left, right)
1341 : ALLOCATE (f_zero(n_bf), f_plus(n_bf), f_minus(n_bf), left(n_bf, 4*n_kz + 1), right(n_bf, 4*n_kz + 1))
1342 : !$OMP DO SCHEDULE(DYNAMIC)
1343 : DO ikp_local = 1, SIZE(local_q, 2)
1344 : q = local_q(:, ikp_local)
1345 : IF (ABS(q(free_axis)) > 0.0_dp) THEN
1346 : CPABORT('2D reciprocal transfer has a nonzero nonperiodic component')
1347 : END IF
1348 : nx = CEILING(cutoff*NORM2(a1)/twopi + ABS(q(periodic_axis(1))))
1349 : ny = CEILING(cutoff*NORM2(a2)/twopi + ABS(q(periodic_axis(2))))
1350 : DO ix = -nx, nx
1351 : DO iy = -ny, ny
1352 : p = b1*(q(periodic_axis(1)) + REAL(ix, dp)) + &
1353 : b2*(q(periodic_axis(2)) + REAL(iy, dp))
1354 : pnorm = NORM2(p)
1355 : IF (pnorm > cutoff) CYCLE
1356 : CALL gaussian_basis_fourier(p, particles, qs_kinds, cell, basis_type, &
1357 : bf_start, bf_end, f_zero)
1358 : IF (pnorm <= 0.0_dp) THEN
1359 : ! As in 3D, the singular 2π/(A p) of p = 0 is left to the Γ correction and
1360 : ! only the regular remainder of 2π erfc(p/(2ω))/(A p) is kept.
1361 : factor = -2.0_dp*SQRT(pi)/(area*omega)
1362 : ELSE
1363 : factor = twopi*ERFC(pnorm/(2.0_dp*omega))/(area*pnorm)
1364 : END IF
1365 : left(:, 1) = factor*f_zero
1366 : right(:, 1) = f_zero
1367 : DO inode = 1, n_kz
1368 : kz = nodes(inode)
1369 : CALL gaussian_basis_fourier(p + kz*normal, particles, qs_kinds, cell, &
1370 : basis_type, bf_start, bf_end, f_plus)
1371 : CALL gaussian_basis_fourier(p - kz*normal, particles, qs_kinds, cell, &
1372 : basis_type, bf_start, bf_end, f_minus)
1373 : weight = 2.0_dp*weights(inode)* &
1374 : EXP(-(pnorm**2 + kz**2)/(4.0_dp*omega**2))/ &
1375 : (area*(pnorm**2 + kz**2))
1376 : ! Expand the even-in-kz residual before the outer products.
1377 : ! Direct subtraction of three large matrices loses precision
1378 : ! near q=0, where their common denominator is small.
1379 : ! With d_+- = f_+- - f_0: f_0 (d_+ + d_-)^H + (d_+ + d_-) f_0^H + d_+ d_+^H + d_- d_-^H.
1380 : i_col = 4*inode - 2
1381 : right(:, i_col + 2) = f_plus - f_zero
1382 : right(:, i_col + 3) = f_minus - f_zero
1383 : right(:, i_col) = right(:, i_col + 2) + right(:, i_col + 3)
1384 : right(:, i_col + 1) = f_zero
1385 : left(:, i_col) = weight*f_zero
1386 : left(:, i_col + 1) = weight*right(:, i_col)
1387 : left(:, i_col + 2) = weight*right(:, i_col + 2)
1388 : left(:, i_col + 3) = weight*right(:, i_col + 3)
1389 : END DO
1390 : CALL zgemm('N', 'C', n_bf, n_bf, 4*n_kz + 1, z_one, left, n_bf, right, n_bf, &
1391 : z_one, V_k(:, :, ikp_local), n_bf)
1392 : END DO
1393 : END DO
1394 : END DO
1395 : !$OMP END DO
1396 : DEALLOCATE (f_zero, f_plus, f_minus, left, right)
1397 : !$OMP END PARALLEL
1398 18 : END SUBROUTINE add_ewald_reciprocal_2d
1399 :
1400 : ! **************************************************************************************************
1401 : !> \brief Largest angular momentum of a basis type over all kinds.
1402 : !> \param qs_kinds ...
1403 : !> \param basis_type ...
1404 : !> \return ...
1405 : ! **************************************************************************************************
1406 747115 : FUNCTION basis_lmax(qs_kinds, basis_type) RESULT(lmax)
1407 : TYPE(qs_kind_type), POINTER :: qs_kinds(:)
1408 : CHARACTER(LEN=*), INTENT(IN) :: basis_type
1409 : INTEGER :: lmax
1410 :
1411 : INTEGER :: ikind
1412 : TYPE(gto_basis_set_type), POINTER :: basis
1413 :
1414 747115 : lmax = 0
1415 2240470 : DO ikind = 1, SIZE(qs_kinds)
1416 1493355 : CALL get_qs_kind(qs_kinds(ikind), basis_set=basis, basis_type=basis_type)
1417 4480928 : lmax = MAX(lmax, MAXVAL(basis%lmax))
1418 : END DO
1419 747115 : END FUNCTION basis_lmax
1420 :
1421 : ! **************************************************************************************************
1422 : !> \brief ...
1423 : !> \param p ...
1424 : !> \param particles ...
1425 : !> \param qs_kinds ...
1426 : !> \param cell ...
1427 : !> \param basis_type ...
1428 : !> \param bf_start ...
1429 : !> \param bf_end ...
1430 : !> \param values ...
1431 : ! **************************************************************************************************
1432 174593 : SUBROUTINE gaussian_basis_fourier(p, particles, qs_kinds, cell, basis_type, &
1433 174593 : bf_start, bf_end, values)
1434 : REAL(KIND=dp), INTENT(IN) :: p(3)
1435 : TYPE(particle_type), POINTER :: particles(:)
1436 : TYPE(qs_kind_type), POINTER :: qs_kinds(:)
1437 : TYPE(cell_type), POINTER :: cell
1438 : CHARACTER(LEN=*), INTENT(IN) :: basis_type
1439 : INTEGER, INTENT(IN) :: bf_start(:), bf_end(:)
1440 : COMPLEX(KIND=dp), INTENT(OUT) :: values(:)
1441 :
1442 : COMPLEX(KIND=dp) :: phase
1443 174593 : COMPLEX(KIND=dp), ALLOCATABLE :: moments(:, :), primitive(:)
1444 : INTEGER :: iatom, ico, idim, ikind, il, ipgf, iset, &
1445 : isgf, lmax, ncart, offset
1446 : REAL(KIND=dp) :: alpha, centre(3)
1447 : TYPE(gto_basis_set_type), POINTER :: basis
1448 :
1449 5166449 : values = z_zero
1450 523779 : DO iatom = 1, SIZE(particles)
1451 349186 : ikind = particles(iatom)%atomic_kind%kind_number
1452 349186 : CALL get_qs_kind(qs_kinds(ikind), basis_set=basis, basis_type=basis_type)
1453 349186 : centre = pbc(particles(iatom)%r, cell)
1454 1396744 : phase = EXP(gaussi*SUM(p*centre))
1455 349186 : CPASSERT(bf_end(iatom) - bf_start(iatom) + 1 == basis%nsgf)
1456 1194825 : DO iset = 1, basis%nset
1457 671046 : lmax = basis%lmax(iset)
1458 671046 : ncart = ncoset(lmax)
1459 4026276 : ALLOCATE (moments(0:lmax, 3), primitive(ncart*basis%npgf(iset)))
1460 671046 : primitive = z_zero
1461 3409882 : DO ipgf = 1, basis%npgf(iset)
1462 2738836 : alpha = basis%zet(ipgf, iset)
1463 10955344 : DO idim = 1, 3
1464 8216508 : moments(0, idim) = SQRT(pi/alpha)*EXP(-p(idim)**2/(4.0_dp*alpha))
1465 8216508 : IF (lmax > 0) moments(1, idim) = gaussi*p(idim)*moments(0, idim)/(2.0_dp*alpha)
1466 11920924 : DO il = 2, lmax
1467 : moments(il, idim) = (gaussi*p(idim)*moments(il - 1, idim) + &
1468 9182088 : REAL(il - 1, dp)*moments(il - 2, idim))/(2.0_dp*alpha)
1469 : END DO
1470 : END DO
1471 8723598 : DO ico = ncoset(basis%lmin(iset) - 1) + 1, ncart
1472 : primitive((ipgf - 1)*ncart + ico) = &
1473 8052552 : moments(indco(1, ico), 1)*moments(indco(2, ico), 2)*moments(indco(3, ico), 3)
1474 : END DO
1475 : END DO
1476 671046 : offset = basis%first_sgf(1, iset)
1477 5662902 : DO isgf = offset, offset + basis%nsgf_set(iset) - 1
1478 : values(bf_start(iatom) + isgf - 1) = &
1479 48166578 : phase*SUM(basis%sphi(1:SIZE(primitive), isgf)*primitive)
1480 : END DO
1481 1020232 : DEALLOCATE (moments, primitive)
1482 : END DO
1483 : END DO
1484 174593 : END SUBROUTINE gaussian_basis_fourier
1485 :
1486 747097 : END MODULE kpoint_coulomb_2c
|