(git:27c966b)
Loading...
Searching...
No Matches
kpoint_coulomb_2c.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 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! **************************************************************************************************
19 USE cell_types, ONLY: cell_type,&
20 get_cell,&
21 pbc
23 USE cp_dbcsr_api, ONLY: &
26 dbcsr_release_p, dbcsr_set, dbcsr_type, dbcsr_type_no_symmetry
28 USE gamma, ONLY: init_md_ftable
31 USE kinds, ONLY: dp,&
33 USE kpoint_types, ONLY: get_kpoint_info,&
35 USE mathconstants, ONLY: fourpi,&
36 gaussi,&
37 pi,&
38 twopi,&
39 z_one,&
40 z_zero
41 USE mathlib, ONLY: gauss_legendre,&
44 USE orbital_pointers, ONLY: indco,&
45 ncoset
49 USE qs_kind_types, ONLY: get_qs_kind,&
51#include "./base/base_uses.f90"
52
53 IMPLICIT NONE
54
55 PRIVATE
56
57 CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'kpoint_coulomb_2c'
58
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
71CONTAINS
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 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 CALL timeset(routinen, handle)
106
107 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 operator_type, ikp_start, ikp_end)
112
113 CALL deallocate_tmp(matrix_v_l_tmp)
114
115 CALL timestop(handle)
116
117 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 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 TYPE(two_d_util_type), ALLOCATABLE, DIMENSION(:) :: blocks_v_l, blocks_v_l_store
158 TYPE(two_d_util_type), ALLOCATABLE, &
159 DIMENSION(:, :, :) :: blocks_v_kp
160
161 CALL timeset(routinen, handle)
162
163 CALL get_factor_and_xyz_min_max(cell, kpoints, size_lattice_sum, factor, hmat, &
164 x_min, x_max, y_min, y_max, z_min, z_max, nkp_grid)
165
166 CALL allocate_blocks_v_kp(blocks_v_kp, matrix_v_kp, ikp_start, ikp_end)
167 CALL allocate_blocks_v_l(blocks_v_l, matrix_v_l_tmp)
168 CALL allocate_blocks_v_l(blocks_v_l_store, matrix_v_l_tmp)
169
170 DO i_x_inner = 0, 2*nkp_grid(1) - 1
171 DO j_y_inner = 0, 2*nkp_grid(2) - 1
172 DO k_z_inner = 0, 2*nkp_grid(3) - 1
173
174 DO i_x_outer = x_min, x_max + nkp_grid(1), 2*nkp_grid(1)
175 DO j_y_outer = y_min, y_max + nkp_grid(2), 2*nkp_grid(2)
176 DO k_z_outer = z_min, z_max + nkp_grid(3), 2*nkp_grid(3)
177
178 i_x = i_x_inner + i_x_outer
179 j_y = j_y_inner + j_y_outer
180 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 k_z > z_max .OR. k_z < z_min) cycle
185
186 vec_s = [real(i_x, dp), real(j_y, dp), real(k_z, dp)]
187
188 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 operator_type)
194
195!$OMP PARALLEL DO DEFAULT(NONE) &
196!$OMP SHARED(blocks_v_L, blocks_v_L_store) &
197!$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 CALL timeset(routinen//"_R_to_k", handle2)
209
210 ! add exp(iq*vec_L) * (P 0 | Q vec_L) to V_PQ(q)
211 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 coskl = cos(twopi*dot_product(vec_s(1:3), kpoints%xkp(1:3, ik)))
215 sinkl = sin(twopi*dot_product(vec_s(1:3), kpoints%xkp(1:3, ik)))
216
217 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 + 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 + sinkl*blocks_v_l_store(i_block)%block(:, :)
223
224 END DO
225
226 END DO
227
228 DO i_block = 1, SIZE(blocks_v_l)
229
230 blocks_v_l_store(i_block)%block(:, :) = 0.0_dp
231
232 END DO
233
234 CALL timestop(handle2)
235
236 END DO
237 END DO
238 END DO
239
240 CALL set_blocks_to_matrix_v_kp(matrix_v_kp, blocks_v_kp, ikp_start, ikp_end)
241
242 CALL deallocate_blocks_v_kp(blocks_v_kp)
243 CALL deallocate_blocks_v_l(blocks_v_l)
244 CALL deallocate_blocks_v_l(blocks_v_l_store)
245
246 CALL timestop(handle)
247
248 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 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 REAL(kind=dp), DIMENSION(:, :), POINTER :: data_block
268 TYPE(dbcsr_iterator_type) :: iter
269
270 CALL timeset(routinen, handle)
271
272 DO ik = ikp_start, ikp_end
273
274 DO i_real_im = 1, 2
275
276 i_block = 1
277
278 CALL dbcsr_iterator_start(iter, matrix_v_kp(ik, i_real_im)%matrix)
279
280 DO WHILE (dbcsr_iterator_blocks_left(iter))
281
282 CALL dbcsr_iterator_next_block(iter, row, col, data_block)
283
284 data_block(:, :) = blocks_v_kp(ik, i_real_im, i_block)%block(:, :)
285
286 i_block = i_block + 1
287
288 END DO
289
290 CALL dbcsr_iterator_stop(iter)
291
292 END DO
293
294 END DO
295
296 CALL timestop(handle)
297
298 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 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 INTEGER, ALLOCATABLE, DIMENSION(:) :: kind_of
329 REAL(dp), DIMENSION(3) :: ra, rab_l, rb
330 REAL(kind=dp), DIMENSION(:, :), POINTER :: data_block
331 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 CALL timeset(routinen, handle)
336
337 NULLIFY (basis_set_a, basis_set_b, data_block)
338
339 CALL get_atomic_kind_set(atomic_kind_set, kind_of=kind_of)
340
341 CALL dbcsr_set(matrix_v_l_tmp, 0.0_dp)
342
343 i_block = 1
344
345 CALL dbcsr_iterator_start(iter, matrix_v_l_tmp)
346
347 DO WHILE (dbcsr_iterator_blocks_left(iter))
348
349 CALL dbcsr_iterator_next_block(iter, row, col, data_block)
350
351 kind_a = kind_of(row)
352 kind_b = kind_of(col)
353
354 CALL get_qs_kind(qs_kind=qs_kind_set(kind_a), basis_set=basis_set_a, basis_type=basis_type)
355 CALL get_qs_kind(qs_kind=qs_kind_set(kind_b), basis_set=basis_set_b, basis_type=basis_type)
356
357 ra(1:3) = pbc(particle_set(row)%r(1:3), cell)
358 rb(1:3) = pbc(particle_set(col)%r(1:3), cell)
359
360 rab_l(1:3) = rb(1:3) - ra(1:3) + vec_l(1:3)
361
362 CALL contraction_matrix_shg(basis_set_a, contr_a)
363 CALL contraction_matrix_shg(basis_set_b, contr_b)
364
365 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 calculate_forces=.false.)
370
371 i_block = i_block + 1
372
373 DEALLOCATE (contr_a, contr_b)
374
375 END DO
376
377 CALL dbcsr_iterator_stop(iter)
378
379 DEALLOCATE (kind_of)
380
381 CALL timestop(handle)
382
383 END SUBROUTINE compute_v_transl
384
385! **************************************************************************************************
386!> \brief ...
387!> \param blocks_v_kp ...
388! **************************************************************************************************
389 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 CALL timeset(routinen, handle)
398
399 DO ik = lbound(blocks_v_kp, 1), ubound(blocks_v_kp, 1)
400 DO i_real_img = 1, SIZE(blocks_v_kp, 2)
401 DO i_block = 1, SIZE(blocks_v_kp, 3)
402 DEALLOCATE (blocks_v_kp(ik, i_real_img, i_block)%block)
403 END DO
404 END DO
405 END DO
406
407 DEALLOCATE (blocks_v_kp)
408
409 CALL timestop(handle)
410
411 END SUBROUTINE deallocate_blocks_v_kp
412
413! **************************************************************************************************
414!> \brief ...
415!> \param blocks_v_L ...
416! **************************************************************************************************
417 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 CALL timeset(routinen, handle)
425
426 DO i_block = 1, SIZE(blocks_v_l, 1)
427 DEALLOCATE (blocks_v_l(i_block)%block)
428 END DO
429
430 DEALLOCATE (blocks_v_l)
431
432 CALL timestop(handle)
433
434 END SUBROUTINE deallocate_blocks_v_l
435
436! **************************************************************************************************
437!> \brief ...
438!> \param blocks_v_L ...
439!> \param matrix_v_L_tmp ...
440! **************************************************************************************************
441 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 REAL(kind=dp), DIMENSION(:, :), POINTER :: data_block
449 TYPE(dbcsr_iterator_type) :: iter
450
451 CALL timeset(routinen, handle)
452
453 nblocks = 0
454
455 CALL dbcsr_iterator_start(iter, matrix_v_l_tmp)
456
457 DO WHILE (dbcsr_iterator_blocks_left(iter))
458
459 CALL dbcsr_iterator_next_block(iter, row, col, data_block)
460
461 nblocks = nblocks + 1
462
463 END DO
464
465 CALL dbcsr_iterator_stop(iter)
466
467 ALLOCATE (blocks_v_l(nblocks))
468
469 i_block = 1
470
471 CALL dbcsr_iterator_start(iter, matrix_v_l_tmp)
472
473 DO WHILE (dbcsr_iterator_blocks_left(iter))
474
475 CALL dbcsr_iterator_next_block(iter, row, col, data_block)
476
477 ALLOCATE (blocks_v_l(i_block)%block(SIZE(data_block, 1), SIZE(data_block, 2)))
478 blocks_v_l(i_block)%block = 0.0_dp
479
480 i_block = i_block + 1
481
482 END DO
483
484 CALL dbcsr_iterator_stop(iter)
485
486 CALL timestop(handle)
487
488 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 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 REAL(kind=dp), DIMENSION(:, :), POINTER :: data_block
508 TYPE(dbcsr_iterator_type) :: iter
509
510 CALL timeset(routinen, handle)
511
512 nblocks = 0
513
514 CALL dbcsr_iterator_start(iter, matrix_v_kp(ikp_start, 1)%matrix)
515
516 DO WHILE (dbcsr_iterator_blocks_left(iter))
517
518 CALL dbcsr_iterator_next_block(iter, row, col, data_block)
519
520 nblocks = nblocks + 1
521
522 END DO
523
524 CALL dbcsr_iterator_stop(iter)
525
526 ALLOCATE (blocks_v_kp(ikp_start:ikp_end, SIZE(matrix_v_kp, 2), nblocks))
527
528 DO ik = ikp_start, ikp_end
529
530 DO i_real_img = 1, SIZE(matrix_v_kp, 2)
531
532 i_block = 1
533
534 CALL dbcsr_iterator_start(iter, matrix_v_kp(ik, i_real_img)%matrix)
535
536 DO WHILE (dbcsr_iterator_blocks_left(iter))
537
538 CALL dbcsr_iterator_next_block(iter, row, col, data_block)
539
540 ALLOCATE (blocks_v_kp(ik, i_real_img, i_block)%block(SIZE(data_block, 1), &
541 SIZE(data_block, 2)))
542 blocks_v_kp(ik, i_real_img, i_block)%block = 0.0_dp
543
544 i_block = i_block + 1
545
546 END DO
547
548 CALL dbcsr_iterator_stop(iter)
549
550 END DO
551
552 END DO
553
554 CALL timestop(handle)
555
556 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 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 CALL timeset(routinen, handle)
589
590 CALL get_kpoint_info(kpoints, nkp_grid=nkp_grid, nkp=nkp)
591 CALL get_cell(cell=cell, h=hmat, periodic=periodic)
592
593 IF (periodic(1) == 0) THEN
594 cpassert(nkp_grid(1) == 1)
595 END IF
596 IF (periodic(2) == 0) THEN
597 cpassert(nkp_grid(2) == 1)
598 END IF
599 IF (periodic(3) == 0) THEN
600 cpassert(nkp_grid(3) == 1)
601 END IF
602
603 IF (modulo(nkp_grid(1), 2) == 1) THEN
604 factor = 3**(size_lattice_sum - 1)
605 ELSE IF (modulo(nkp_grid(1), 2) == 0) THEN
606 factor = 2**(size_lattice_sum - 1)
607 END IF
608
609 IF (modulo(nkp_grid(1), 2) == 1) THEN
610 x_min = -(factor*nkp_grid(1) - 1)/2
611 x_max = (factor*nkp_grid(1) - 1)/2
612 ELSE IF (modulo(nkp_grid(1), 2) == 0) THEN
613 x_min = -factor*nkp_grid(1)/2
614 x_max = factor*nkp_grid(1)/2 - 1
615 END IF
616 IF (periodic(1) == 0) THEN
617 x_min = 0
618 x_max = 0
619 END IF
620
621 IF (modulo(nkp_grid(2), 2) == 1) THEN
622 y_min = -(factor*nkp_grid(2) - 1)/2
623 y_max = (factor*nkp_grid(2) - 1)/2
624 ELSE IF (modulo(nkp_grid(2), 2) == 0) THEN
625 y_min = -factor*nkp_grid(2)/2
626 y_max = factor*nkp_grid(2)/2 - 1
627 END IF
628 IF (periodic(2) == 0) THEN
629 y_min = 0
630 y_max = 0
631 END IF
632
633 IF (modulo(nkp_grid(3), 2) == 1) THEN
634 z_min = -(factor*nkp_grid(3) - 1)/2
635 z_max = (factor*nkp_grid(3) - 1)/2
636 ELSE IF (modulo(nkp_grid(3), 2) == 0) THEN
637 z_min = -factor*nkp_grid(3)/2
638 z_max = factor*nkp_grid(3)/2 - 1
639 END IF
640 IF (periodic(3) == 0) THEN
641 z_min = 0
642 z_max = 0
643 END IF
644
645 CALL timestop(handle)
646
647 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 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 CALL timeset(routinen, handle)
666
667 NULLIFY (matrix_v_l_tmp)
668 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 matrix_type=dbcsr_type_no_symmetry)
672 CALL dbcsr_reserve_all_blocks(matrix_v_l_tmp)
673 CALL dbcsr_set(matrix_v_l_tmp, 0.0_dp)
674
675 CALL timestop(handle)
676
677 END SUBROUTINE allocate_tmp
678
679! **************************************************************************************************
680!> \brief ...
681!> \param matrix_v_L_tmp ...
682! **************************************************************************************************
683 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 CALL timeset(routinen, handle)
692
693 CALL dbcsr_release_p(matrix_v_l_tmp)
694
695 CALL timestop(handle)
696
697 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 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 CALL timeset(routinen, handle)
727
728 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 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 x_min, x_max, y_min, y_max, z_min, z_max, ikp_start, ikp_end)
735
736 CALL timestop(handle)
737
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 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 COMPLEX(KIND=dp), ALLOCATABLE, DIMENSION(:) :: recv_complex, send_complex
780 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 INTEGER, ALLOCATABLE, DIMENSION(:) :: bf_end_from_atom, bf_start_from_atom, class_first, &
785 col_first, n_own, recv_counts, recv_displs, send_counts, send_displs
786 INTEGER, ALLOCATABLE, DIMENSION(:, :) :: cell_s
787 INTEGER, DIMENSION(3) :: fold_sign, nkp_grid, period
788 REAL(kind=dp) :: arg
789 REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: cell_sign, k_times_n, recv_real, &
790 send_real
791 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :) :: class_ref, cos_phase, im_part, re_part, &
792 sin_phase
793 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :, :) :: v_cols, v_l, v_own
794 REAL(kind=dp), DIMENSION(3) :: vec_l
795 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 TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
799 TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
800
801 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 atomic_kind_set=atomic_kind_set)
809
810 CALL get_kpoint_info(kpoints, nkp_grid=nkp_grid)
811
812 CALL get_basis_sizes(qs_env, n_atom, basis_type, bf_start_from_atom, bf_end_from_atom, n_bf)
813
814 ALLOCATE (k_times_n(ikp_end - ikp_start + 1))
815 DO idim = 1, 3
816 k_times_n(:) = kpoints%xkp(idim, ikp_start:ikp_end)*real(nkp_grid(idim), dp)
817 period(idim) = 2*nkp_grid(idim)
818 fold_sign(idim) = 1
819 IF (all(abs(k_times_n - anint(k_times_n)) < fold_eps)) THEN
820 period(idim) = nkp_grid(idim)
821 ELSE IF (all(abs(k_times_n - anint(k_times_n - 0.5_dp) - 0.5_dp) < fold_eps)) THEN
822 period(idim) = nkp_grid(idim)
823 fold_sign(idim) = -1
824 END IF
825 END DO
826 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 n_cells = (x_max - x_min + 1)*(y_max - y_min + 1)*(z_max - z_min + 1)
831 ALLOCATE (cell_s(3, n_cells), cell_sign(n_cells), class_first(n_cells + 1), &
832 class_ref(3, n_cells))
833 n_cells = 0
834 n_class = 0
835 DO i_x_inner = 0, period(1) - 1
836 DO j_y_inner = 0, period(2) - 1
837 DO k_z_inner = 0, period(3) - 1
838 i_cell = n_cells
839 DO i_x_outer = x_min, x_max, period(1)
840 DO j_y_outer = y_min, y_max, period(2)
841 DO k_z_outer = z_min, z_max, period(3)
842 i_x = i_x_inner + i_x_outer
843 j_y = j_y_inner + j_y_outer
844 k_z = k_z_inner + k_z_outer
845 IF (i_x > x_max .OR. j_y > y_max .OR. k_z > z_max) cycle
846 n_cells = n_cells + 1
847 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 fold_sign(3)**((k_z_outer - z_min)/period(3)), dp)
851 END DO
852 END DO
853 END DO
854 IF (n_cells > i_cell) THEN
855 n_class = n_class + 1
856 class_first(n_class) = i_cell + 1
857 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 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 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 n_batch = max(1, min(ikp_end - ikp_start + 1, int(batch_bytes/(complex_bytes*real(n_bf, dp)**2))))
868 ALLOCATE (v_l(n_bf, 1, n_bf), v_part(n_bf, n_bf, n_batch))
869 DO first_k = ikp_start, ikp_end, n_batch
870 last_k = min(ikp_end, first_k + n_batch - 1)
871 v_part(:, :, :) = z_zero
872 DO i_cell = 1, n_cells
873 IF (modulo(i_cell, para_env%num_pe) /= para_env%mepos) cycle
874 v_l(:, :, :) = 0.0_dp
875 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 particle_set, qs_kind_set, atomic_kind_set, basis_type, cell)
878!$OMP PARALLEL DO DEFAULT(NONE) SCHEDULE(STATIC) &
879!$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 CALL para_env%sum(v_part(:, :, 1:last_k - first_k + 1))
888 DO ik = first_k, last_k
889 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 v_part(:, :, ik - first_k + 1)
892 END DO
893 END DO
894 DEALLOCATE (v_l, v_part, cell_s, cell_sign, class_first, class_ref)
895 CALL timestop(handle)
896 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 num_pe = para_env%num_pe
901 ALLOCATE (col_first(0:num_pe), n_own(0:num_pe - 1))
902 DO i_rank = 0, num_pe
903 col_first(i_rank) = (i_rank*n_bf)/num_pe + 1
904 END DO
905 DO i_rank = 0, num_pe - 1
906 n_own(i_rank) = (n_class - i_rank + num_pe - 1)/num_pe
907 END DO
908 n_cols = col_first(para_env%mepos + 1) - col_first(para_env%mepos)
909 ALLOCATE (v_own(n_bf, n_bf, n_own(para_env%mepos)), source=0.0_dp)
910 DO i_own = 1, n_own(para_env%mepos)
911 i_class = para_env%mepos + 1 + (i_own - 1)*num_pe
912 DO i_cell = class_first(i_class), class_first(i_class + 1) - 1
913 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 atomic_kind_set, basis_type, cell)
917 END DO
918 END DO
919
920 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 recv_counts(0:num_pe - 1), recv_displs(0:num_pe - 1))
924 DO i_rank = 0, num_pe - 1
925 send_counts(i_rank) = n_bf*(col_first(i_rank + 1) - col_first(i_rank))*n_own(para_env%mepos)
926 recv_counts(i_rank) = n_bf*n_cols*n_own(i_rank)
927 END DO
928 send_displs(0) = 0
929 recv_displs(0) = 0
930 DO i_rank = 1, num_pe - 1
931 send_displs(i_rank) = send_displs(i_rank - 1) + send_counts(i_rank - 1)
932 recv_displs(i_rank) = recv_displs(i_rank - 1) + recv_counts(i_rank - 1)
933 END DO
934 ALLOCATE (send_real(sum(send_counts)), recv_real(sum(recv_counts)))
935 pos = 0
936 DO i_rank = 0, num_pe - 1
937 n_block = n_bf*(col_first(i_rank + 1) - col_first(i_rank))
938 DO i_own = 1, n_own(para_env%mepos)
939 send_real(pos + 1:pos + n_block) = &
940 reshape(v_own(:, col_first(i_rank):col_first(i_rank + 1) - 1, i_own), [n_block])
941 pos = pos + n_block
942 END DO
943 END DO
944 DEALLOCATE (v_own)
945 CALL para_env%alltoall(send_real, send_counts, send_displs, recv_real, recv_counts, recv_displs)
946 DEALLOCATE (send_real)
947 ! V_cols(P, class, Q): the classes of one column Q form the GEMM operand of that column.
948 ALLOCATE (v_cols(n_bf, n_class, n_cols))
949 pos = 0
950 DO i_rank = 0, num_pe - 1
951 DO i_own = 1, n_own(i_rank)
952 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 pos = pos + n_bf*n_cols
954 END DO
955 END DO
956 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 chunk_bytes/(complex_bytes*real(n_class, dp))))
962 n_chunk = max(1, n_chunk)
963 ALLOCATE (cos_phase(n_class, n_chunk), sin_phase(n_class, n_chunk), v_q_cols(n_bf, n_chunk, n_cols))
964 DO first_k = ikp_start, ikp_end, n_chunk
965 last_k = min(ikp_end, first_k + n_chunk - 1)
966 n_k = last_k - first_k + 1
967 DO ik = first_k, last_k
968 DO i_class = 1, n_class
969 phase = exp(cmplx(0.0_dp, twopi*dot_product(class_ref(:, i_class), kpoints%xkp(1:3, ik)), kind=dp))
970 cos_phase(i_class, ik - first_k + 1) = real(phase, kind=dp)
971 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!$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 DO i_rank = 0, num_pe - 1
992 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 count([(modulo(ik, num_pe) == para_env%mepos, ik=first_k, last_k)])
995 END DO
996 DO i_rank = 1, num_pe - 1
997 send_displs(i_rank) = send_displs(i_rank - 1) + send_counts(i_rank - 1)
998 recv_displs(i_rank) = recv_displs(i_rank - 1) + recv_counts(i_rank - 1)
999 END DO
1000 ALLOCATE (send_complex(sum(send_counts)), recv_complex(sum(recv_counts)))
1001 pos = 0
1002 DO i_rank = 0, num_pe - 1
1003 DO ik = first_k, last_k
1004 IF (modulo(ik, num_pe) /= i_rank) cycle
1005 send_complex(pos + 1:pos + n_bf*n_cols) = reshape(v_q_cols(:, ik - first_k + 1, :), [n_bf*n_cols])
1006 pos = pos + n_bf*n_cols
1007 END DO
1008 END DO
1009 CALL para_env%alltoall(send_complex, send_counts, send_displs, recv_complex, recv_counts, recv_displs)
1010 pos = 0
1011 DO i_rank = 0, num_pe - 1
1012 n_block = n_bf*(col_first(i_rank + 1) - col_first(i_rank))
1013 DO ik = first_k, last_k
1014 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 reshape(recv_complex(pos + 1:pos + n_block), [n_bf, col_first(i_rank + 1) - col_first(i_rank)])
1018 pos = pos + n_block
1019 END DO
1020 END DO
1021 DEALLOCATE (send_complex, recv_complex)
1022 END DO
1023 CALL timestop(handle2)
1024
1025 DEALLOCATE (v_cols, v_q_cols, cos_phase, sin_phase, col_first, n_own, send_counts, send_displs, &
1026 recv_counts, recv_displs, cell_s, cell_sign, class_first, class_ref)
1027
1028 CALL timestop(handle)
1029
1030 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 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 INTEGER, ALLOCATABLE, DIMENSION(:) :: kind_of
1053 TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
1054 TYPE(gto_basis_set_type), POINTER :: basis_set_a
1055 TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
1056 TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
1057
1058 CALL timeset(routinen, handle)
1059
1060 CALL get_qs_env(qs_env=qs_env, particle_set=particle_set, &
1061 qs_kind_set=qs_kind_set, atomic_kind_set=atomic_kind_set)
1062 CALL get_atomic_kind_set(atomic_kind_set, kind_of=kind_of)
1063
1064 n_atom = SIZE(particle_set)
1065 n_kind = SIZE(qs_kind_set)
1066
1067 DO ikind = 1, n_kind
1068 CALL get_qs_kind(qs_kind=qs_kind_set(ikind), basis_set=basis_set_a, &
1069 basis_type=basis_type)
1070 cpassert(ASSOCIATED(basis_set_a))
1071 END DO
1072
1073 ALLOCATE (bf_start_from_atom(n_atom), bf_end_from_atom(n_atom))
1074
1075 n_bf = 0
1076 DO iatom = 1, n_atom
1077 bf_start_from_atom(iatom) = n_bf + 1
1078 ikind = kind_of(iatom)
1079 CALL get_qs_kind(qs_kind=qs_kind_set(ikind), nsgf=nsgf, basis_type=basis_type)
1080 n_bf = n_bf + nsgf
1081 bf_end_from_atom(iatom) = n_bf
1082 END DO
1083
1084 CALL timestop(handle)
1085
1086 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 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 INTEGER, ALLOCATABLE, DIMENSION(:) :: kind_of
1121 REAL(dp), DIMENSION(3) :: ra, rab_l, rb
1122 REAL(kind=dp), DIMENSION(:, :), POINTER :: v_l_ab
1123 REAL(kind=dp), DIMENSION(:, :, :), POINTER :: contr_a, contr_b
1124 TYPE(gto_basis_set_type), POINTER :: basis_set_a, basis_set_b
1125
1126 CALL timeset(routinen, handle)
1127
1128 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 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!$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 DEALLOCATE (kind_of)
1181
1182 CALL timestop(handle)
1183
1184 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 kpoints ...
1193!> \param basis_type ...
1194!> \param omega range-separation parameter of the Ewald splitting
1195!> \param ikp_start ...
1196!> \param ikp_end ...
1197! **************************************************************************************************
1198 SUBROUTINE add_ewald_reciprocal(V_k, qs_env, kpoints, basis_type, omega, ikp_start, ikp_end)
1199 COMPLEX(KIND=dp), INTENT(INOUT) :: v_k(:, :, :)
1200 TYPE(qs_environment_type), POINTER :: qs_env
1201 TYPE(kpoint_type), POINTER :: kpoints
1202 CHARACTER(LEN=*), INTENT(IN) :: basis_type
1203 REAL(kind=dp), INTENT(IN) :: omega
1204 INTEGER, INTENT(IN) :: ikp_start, ikp_end
1205
1206 CHARACTER(LEN=*), PARAMETER :: routinen = 'add_ewald_reciprocal'
1207 INTEGER, PARAMETER :: n_block = 128
1208
1209 COMPLEX(KIND=dp), ALLOCATABLE :: fourier_basis(:, :), scaled_basis(:, :)
1210 INTEGER :: bounds(3), handle, idim, ikp, ikp_local, &
1211 ix, iy, iz, n_atom, n_bf, n_col
1212 INTEGER, ALLOCATABLE, DIMENSION(:) :: bf_end, bf_start
1213 REAL(kind=dp) :: cutoff, p(3), p2, prefactor, q(3), &
1214 reciprocal(3, 3)
1215 TYPE(cell_type), POINTER :: cell
1216 TYPE(mp_para_env_type), POINTER :: para_env
1217 TYPE(particle_type), POINTER :: particles(:)
1218 TYPE(qs_kind_type), POINTER :: qs_kinds(:)
1219
1220 CALL timeset(routinen, handle)
1221
1222 CALL get_qs_env(qs_env, cell=cell, para_env=para_env, particle_set=particles, qs_kind_set=qs_kinds)
1223 CALL get_basis_sizes(qs_env, n_atom, basis_type, bf_start, bf_end, n_bf)
1224 IF (count(cell%perd /= 0) == 2) THEN
1225 CALL add_ewald_reciprocal_2d(v_k, qs_env, kpoints, basis_type, omega, &
1226 bf_start, bf_end, ikp_start, ikp_end)
1227 ELSE
1228 cpassert(count(cell%perd /= 0) == 3)
1229 reciprocal = twopi*transpose(cell%h_inv)
1230 cutoff = 2.0_dp*omega*sqrt(ewald_tail_exponent + ewald_lmax_margin*basis_lmax(qs_kinds, basis_type))
1231 ! Each thread handles its own k points of this rank and adds n_block momenta p per GEMM.
1232!$OMP PARALLEL DEFAULT(NONE) &
1233!$OMP SHARED(V_k, ikp_start, ikp_end, kpoints, para_env, cell, reciprocal, cutoff, omega, &
1234!$OMP particles, qs_kinds, basis_type, bf_start, bf_end, n_bf) &
1235!$OMP PRIVATE(ikp, ikp_local, q, idim, bounds, ix, iy, iz, p, p2, prefactor, n_col, &
1236!$OMP fourier_basis, scaled_basis)
1237 ALLOCATE (fourier_basis(n_bf, n_block), scaled_basis(n_bf, n_block))
1238!$OMP DO SCHEDULE(DYNAMIC)
1239 DO ikp = ikp_start, ikp_end
1240 IF (modulo(ikp, para_env%num_pe) /= para_env%mepos) cycle
1241 ikp_local = (ikp - para_env%mepos)/para_env%num_pe + 1
1242 IF (para_env%mepos == 0) ikp_local = ikp/para_env%num_pe
1243 q = kpoints%xkp(:, ikp)
1244 DO idim = 1, 3
1245 bounds(idim) = ceiling(cutoff*norm2(cell%hmat(:, idim))/twopi + abs(q(idim)))
1246 END DO
1247 n_col = 0
1248 DO ix = -bounds(1), bounds(1)
1249 DO iy = -bounds(2), bounds(2)
1250 DO iz = -bounds(3), bounds(3)
1251 p = matmul(reciprocal, q + real([ix, iy, iz], dp))
1252 p2 = sum(p*p)
1253 IF (p2 > cutoff**2) cycle
1254 n_col = n_col + 1
1255 CALL gaussian_basis_fourier(p, particles, qs_kinds, cell, basis_type, &
1256 bf_start, bf_end, fourier_basis(:, n_col))
1257 IF (p2 <= 0.0_dp) THEN
1258 prefactor = -pi/(cell%deth*omega**2)
1259 ELSE
1260 prefactor = fourpi*exp(-p2/(4.0_dp*omega**2))/(cell%deth*p2)
1261 END IF
1262 scaled_basis(:, n_col) = prefactor*fourier_basis(:, n_col)
1263 IF (n_col == n_block) THEN
1264 CALL zgemm('N', 'C', n_bf, n_bf, n_col, z_one, scaled_basis, n_bf, &
1265 fourier_basis, n_bf, z_one, v_k(:, :, ikp_local), n_bf)
1266 n_col = 0
1267 END IF
1268 END DO
1269 END DO
1270 END DO
1271 CALL zgemm('N', 'C', n_bf, n_bf, n_col, z_one, scaled_basis, n_bf, &
1272 fourier_basis, n_bf, z_one, v_k(:, :, ikp_local), n_bf)
1273 END DO
1274!$OMP END DO
1275 DEALLOCATE (fourier_basis, scaled_basis)
1276!$OMP END PARALLEL
1277 END IF
1278
1279 CALL timestop(handle)
1280
1281 END SUBROUTINE add_ewald_reciprocal
1282
1283! **************************************************************************************************
1284!> \brief Reciprocal 2D Ewald Coulomb matrix for any pair of periodic cell axes.
1285!> The momentum perpendicular to their plane is integrated continuously.
1286!> \param V_k ...
1287!> \param qs_env ...
1288!> \param kpoints ...
1289!> \param basis_type ...
1290!> \param omega ...
1291!> \param bf_start ...
1292!> \param bf_end ...
1293!> \param ikp_start ...
1294!> \param ikp_end ...
1295! **************************************************************************************************
1296 SUBROUTINE add_ewald_reciprocal_2d(V_k, qs_env, kpoints, basis_type, omega, &
1297 bf_start, bf_end, ikp_start, ikp_end)
1298 COMPLEX(KIND=dp), INTENT(INOUT) :: v_k(:, :, :)
1299 TYPE(qs_environment_type), POINTER :: qs_env
1300 TYPE(kpoint_type), POINTER :: kpoints
1301 CHARACTER(LEN=*), INTENT(IN) :: basis_type
1302 REAL(kind=dp), INTENT(IN) :: omega
1303 INTEGER, INTENT(IN) :: bf_start(:), bf_end(:), ikp_start, &
1304 ikp_end
1305
1306 INTEGER, PARAMETER :: n_kz = 192
1307
1308 COMPLEX(KIND=dp), ALLOCATABLE :: f_minus(:), f_plus(:), f_zero(:), &
1309 left(:, :), right(:, :)
1310 INTEGER :: free_axis, i_col, ikp, ikp_local, inode, &
1311 ix, iy, n_bf, nx, ny, periodic_axis(2)
1312 REAL(kind=dp) :: a1(3), a2(3), area, b1(3), b2(3), &
1313 cutoff, factor, kz, nodes(n_kz), &
1314 normal(3), p(3), pnorm, q(3), weight, &
1315 weights(n_kz)
1316 TYPE(cell_type), POINTER :: cell
1317 TYPE(mp_para_env_type), POINTER :: para_env
1318 TYPE(particle_type), POINTER :: particles(:)
1319 TYPE(qs_kind_type), POINTER :: qs_kinds(:)
1320
1321 CALL get_qs_env(qs_env, cell=cell, para_env=para_env, &
1322 particle_set=particles, qs_kind_set=qs_kinds)
1323 cpassert(count(cell%perd /= 0) == 2)
1324 periodic_axis = pack([1, 2, 3], cell%perd /= 0)
1325 free_axis = findloc(cell%perd, 0, dim=1)
1326 a1 = cell%hmat(:, periodic_axis(1))
1327 a2 = cell%hmat(:, periodic_axis(2))
1328 normal = vector_product(a1, a2)
1329 area = norm2(normal)
1330 cpassert(area > 0.0_dp)
1331 normal = normal/area
1332 b1 = twopi*vector_product(a2, normal)/area
1333 b2 = twopi*vector_product(normal, a1)/area
1334 n_bf = SIZE(v_k, 1)
1335 cutoff = 2.0_dp*omega*sqrt(ewald_tail_exponent + ewald_lmax_margin*basis_lmax(qs_kinds, basis_type))
1336 ! Legendre nodes and weights on [0, cutoff] for the free momentum.
1337 CALL gauss_legendre(n_kz, nodes, weights)
1338 nodes(:) = 0.5_dp*cutoff*(nodes + 1.0_dp)
1339 weights(:) = 0.5_dp*cutoff*weights
1340 ! Each thread handles its own k points of this rank and adds all terms of one momentum p
1341 ! as SUM_i left_i right_i^H in one GEMM.
1342!$OMP PARALLEL DEFAULT(NONE) &
1343!$OMP SHARED(V_k, ikp_start, ikp_end, kpoints, para_env, free_axis, periodic_axis, cutoff, &
1344!$OMP a1, a2, b1, b2, normal, area, omega, nodes, weights, particles, qs_kinds, cell, basis_type, &
1345!$OMP bf_start, bf_end, n_bf) &
1346!$OMP PRIVATE(ikp, ikp_local, q, nx, ny, ix, iy, p, pnorm, factor, inode, kz, weight, i_col, &
1347!$OMP f_zero, f_plus, f_minus, left, right)
1348 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))
1349!$OMP DO SCHEDULE(DYNAMIC)
1350 DO ikp = ikp_start, ikp_end
1351 IF (modulo(ikp, para_env%num_pe) /= para_env%mepos) cycle
1352 ikp_local = (ikp - para_env%mepos)/para_env%num_pe + 1
1353 IF (para_env%mepos == 0) ikp_local = ikp/para_env%num_pe
1354 q = kpoints%xkp(:, ikp)
1355 IF (abs(q(free_axis)) > 0.0_dp) THEN
1356 cpabort('2D reciprocal transfer has a nonzero nonperiodic component')
1357 END IF
1358 nx = ceiling(cutoff*norm2(a1)/twopi + abs(q(periodic_axis(1))))
1359 ny = ceiling(cutoff*norm2(a2)/twopi + abs(q(periodic_axis(2))))
1360 DO ix = -nx, nx
1361 DO iy = -ny, ny
1362 p = b1*(q(periodic_axis(1)) + real(ix, dp)) + &
1363 b2*(q(periodic_axis(2)) + real(iy, dp))
1364 pnorm = norm2(p)
1365 IF (pnorm > cutoff) cycle
1366 IF (pnorm <= 0.0_dp) THEN
1367 cpabort('2D Coulomb at Gamma requires charge-channel separation')
1368 END IF
1369 CALL gaussian_basis_fourier(p, particles, qs_kinds, cell, basis_type, &
1370 bf_start, bf_end, f_zero)
1371 factor = twopi*erfc(pnorm/(2.0_dp*omega))/(area*pnorm)
1372 left(:, 1) = factor*f_zero
1373 right(:, 1) = f_zero
1374 DO inode = 1, n_kz
1375 kz = nodes(inode)
1376 CALL gaussian_basis_fourier(p + kz*normal, particles, qs_kinds, cell, &
1377 basis_type, bf_start, bf_end, f_plus)
1378 CALL gaussian_basis_fourier(p - kz*normal, particles, qs_kinds, cell, &
1379 basis_type, bf_start, bf_end, f_minus)
1380 weight = 2.0_dp*weights(inode)* &
1381 exp(-(pnorm**2 + kz**2)/(4.0_dp*omega**2))/ &
1382 (area*(pnorm**2 + kz**2))
1383 ! Expand the even-in-kz residual before the outer products.
1384 ! Direct subtraction of three large matrices loses precision
1385 ! near q=0, where their common denominator is small.
1386 ! With d_+- = f_+- - f_0: f_0 (d_+ + d_-)^H + (d_+ + d_-) f_0^H + d_+ d_+^H + d_- d_-^H.
1387 i_col = 4*inode - 2
1388 right(:, i_col + 2) = f_plus - f_zero
1389 right(:, i_col + 3) = f_minus - f_zero
1390 right(:, i_col) = right(:, i_col + 2) + right(:, i_col + 3)
1391 right(:, i_col + 1) = f_zero
1392 left(:, i_col) = weight*f_zero
1393 left(:, i_col + 1) = weight*right(:, i_col)
1394 left(:, i_col + 2) = weight*right(:, i_col + 2)
1395 left(:, i_col + 3) = weight*right(:, i_col + 3)
1396 END DO
1397 CALL zgemm('N', 'C', n_bf, n_bf, 4*n_kz + 1, z_one, left, n_bf, right, n_bf, &
1398 z_one, v_k(:, :, ikp_local), n_bf)
1399 END DO
1400 END DO
1401 END DO
1402!$OMP END DO
1403 DEALLOCATE (f_zero, f_plus, f_minus, left, right)
1404!$OMP END PARALLEL
1405 END SUBROUTINE add_ewald_reciprocal_2d
1406
1407! **************************************************************************************************
1408!> \brief Largest angular momentum of a basis type over all kinds.
1409!> \param qs_kinds ...
1410!> \param basis_type ...
1411!> \return ...
1412! **************************************************************************************************
1413 FUNCTION basis_lmax(qs_kinds, basis_type) RESULT(lmax)
1414 TYPE(qs_kind_type), POINTER :: qs_kinds(:)
1415 CHARACTER(LEN=*), INTENT(IN) :: basis_type
1416 INTEGER :: lmax
1417
1418 INTEGER :: ikind
1419 TYPE(gto_basis_set_type), POINTER :: basis
1420
1421 lmax = 0
1422 DO ikind = 1, SIZE(qs_kinds)
1423 CALL get_qs_kind(qs_kinds(ikind), basis_set=basis, basis_type=basis_type)
1424 lmax = max(lmax, maxval(basis%lmax))
1425 END DO
1426 END FUNCTION basis_lmax
1427
1428! **************************************************************************************************
1429!> \brief ...
1430!> \param p ...
1431!> \param particles ...
1432!> \param qs_kinds ...
1433!> \param cell ...
1434!> \param basis_type ...
1435!> \param bf_start ...
1436!> \param bf_end ...
1437!> \param values ...
1438! **************************************************************************************************
1439 SUBROUTINE gaussian_basis_fourier(p, particles, qs_kinds, cell, basis_type, &
1440 bf_start, bf_end, values)
1441 REAL(kind=dp), INTENT(IN) :: p(3)
1442 TYPE(particle_type), POINTER :: particles(:)
1443 TYPE(qs_kind_type), POINTER :: qs_kinds(:)
1444 TYPE(cell_type), POINTER :: cell
1445 CHARACTER(LEN=*), INTENT(IN) :: basis_type
1446 INTEGER, INTENT(IN) :: bf_start(:), bf_end(:)
1447 COMPLEX(KIND=dp), INTENT(OUT) :: values(:)
1448
1449 COMPLEX(KIND=dp) :: phase
1450 COMPLEX(KIND=dp), ALLOCATABLE :: moments(:, :), primitive(:)
1451 INTEGER :: iatom, ico, idim, ikind, il, ipgf, iset, &
1452 isgf, lmax, ncart, offset
1453 REAL(kind=dp) :: alpha, centre(3)
1454 TYPE(gto_basis_set_type), POINTER :: basis
1455
1456 values = z_zero
1457 DO iatom = 1, SIZE(particles)
1458 ikind = particles(iatom)%atomic_kind%kind_number
1459 CALL get_qs_kind(qs_kinds(ikind), basis_set=basis, basis_type=basis_type)
1460 centre = pbc(particles(iatom)%r, cell)
1461 phase = exp(gaussi*sum(p*centre))
1462 cpassert(bf_end(iatom) - bf_start(iatom) + 1 == basis%nsgf)
1463 DO iset = 1, basis%nset
1464 lmax = basis%lmax(iset)
1465 ncart = ncoset(lmax)
1466 ALLOCATE (moments(0:lmax, 3), primitive(ncart*basis%npgf(iset)))
1467 primitive = z_zero
1468 DO ipgf = 1, basis%npgf(iset)
1469 alpha = basis%zet(ipgf, iset)
1470 DO idim = 1, 3
1471 moments(0, idim) = sqrt(pi/alpha)*exp(-p(idim)**2/(4.0_dp*alpha))
1472 IF (lmax > 0) moments(1, idim) = gaussi*p(idim)*moments(0, idim)/(2.0_dp*alpha)
1473 DO il = 2, lmax
1474 moments(il, idim) = (gaussi*p(idim)*moments(il - 1, idim) + &
1475 REAL(il - 1, dp)*moments(il - 2, idim))/(2.0_dp*alpha)
1476 END DO
1477 END DO
1478 DO ico = ncoset(basis%lmin(iset) - 1) + 1, ncart
1479 primitive((ipgf - 1)*ncart + ico) = &
1480 moments(indco(1, ico), 1)*moments(indco(2, ico), 2)*moments(indco(3, ico), 3)
1481 END DO
1482 END DO
1483 offset = basis%first_sgf(1, iset)
1484 DO isgf = offset, offset + basis%nsgf_set(iset) - 1
1485 values(bf_start(iatom) + isgf - 1) = &
1486 phase*sum(basis%sphi(1:SIZE(primitive), isgf)*primitive)
1487 END DO
1488 DEALLOCATE (moments, primitive)
1489 END DO
1490 END DO
1491 END SUBROUTINE gaussian_basis_fourier
1492
1493END MODULE kpoint_coulomb_2c
static GRID_HOST_DEVICE int modulo(int a, int m)
Equivalent of Fortran's MODULO, which always return a positive number. https://gcc....
static void dgemm(const char transa, const char transb, const int m, const int n, const int k, const double alpha, const double *a, const int lda, const double *b, const int ldb, const double beta, double *c, const int ldc)
Convenient wrapper to hide Fortran nature of dgemm_, swapping a and b.
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.
Handles all functions related to the CELL.
Definition cell_types.F:15
subroutine, public get_cell(cell, alpha, beta, gamma, deth, orthorhombic, abc, periodic, h, h_inv, symmetry_id, tag)
Get informations about a simulation cell.
Definition cell_types.F:234
constants for the different operators of the 2c-integrals
integer, parameter, public operator_coulomb
subroutine, public dbcsr_release_p(matrix)
...
logical function, public dbcsr_iterator_blocks_left(iterator)
...
subroutine, public dbcsr_iterator_stop(iterator)
...
subroutine, public dbcsr_init_p(matrix)
...
subroutine, public dbcsr_iterator_next_block(iterator, row, column, block, block_number_argument_has_been_removed, row_size, col_size, row_offset, col_offset, transposed)
...
subroutine, public dbcsr_iterator_start(iterator, matrix, shared, dynamic, dynamic_byrows)
...
subroutine, public dbcsr_set(matrix, alpha)
...
subroutine, public dbcsr_reserve_all_blocks(matrix)
Reserves all blocks.
Calculation of the incomplete Gamma function F_n(t) for multi-center integrals over Cartesian Gaussia...
Definition gamma.F:15
subroutine, public init_md_ftable(nmax)
Initialize a table of F_n(t) values in the range 0 <= t <= 12 with a stepsize of 0....
Definition gamma.F:540
Initialization for solid harmonic Gaussian (SHG) integral scheme. Scheme for calculation of contracte...
subroutine, public contraction_matrix_shg(basis, scon_shg)
contraction matrix for SHG integrals
Calculation of contracted, spherical Gaussian integrals using the solid harmonic Gaussian (SHG) integ...
subroutine, public int_operators_r12_ab_shg(r12_operator, vab, dvab, rab, fba, fbb, scona_shg, sconb_shg, omega, calculate_forces)
Calcululates the two-center integrals of the type (a|O(r12)|b) using the SHG scheme.
Defines the basic variable types.
Definition kinds.F:23
integer, parameter, public dp_size
Definition kinds.F:36
integer, parameter, public dp
Definition kinds.F:34
Routines to compute the Coulomb integral V_(alpha beta)(k) for a k-point k using lattice summation in...
subroutine, public build_2c_coulomb_matrix_kp(matrix_v_kp, kpoints, basis_type, cell, particle_set, qs_kind_set, atomic_kind_set, size_lattice_sum, operator_type, ikp_start, ikp_end)
...
real(kind=dp), parameter ewald_tail_exponent
subroutine, public build_2c_coulomb_matrix_kp_dense(v_k, qs_env, kpoints, size_lattice_sum, basis_type, ikp_start, ikp_end)
Dense 2-center Coulomb matrix V_PQ(k) from a direct real-space lattice sum.
subroutine, public add_ewald_reciprocal(v_k, qs_env, kpoints, basis_type, omega, ikp_start, ikp_end)
Adds the reciprocal Ewald part sum_L exp(ik*L) (P 0 | erf(omega r)/r | Q L) to the dense V_PQ(k) of t...
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, lattice_fft)
Retrieve information from a kpoint environment.
Definition of mathematical constants and functions.
real(kind=dp), parameter, public pi
complex(kind=dp), parameter, public z_one
complex(kind=dp), parameter, public gaussi
real(kind=dp), parameter, public fourpi
real(kind=dp), parameter, public twopi
complex(kind=dp), parameter, public z_zero
Collection of simple mathematical functions and subroutines.
Definition mathlib.F:15
subroutine, public gauss_legendre(n, nodes, weights)
Gauss-Legendre nodes and weights on [-1,1], with nodes in ascending order. The Legendre roots are ref...
Definition mathlib.F:2295
pure real(kind=dp) function, dimension(3), public vector_product(a, b)
Calculation of the vector product c = a x b.
Definition mathlib.F:1274
Interface to the message passing library MPI.
Provides Cartesian and spherical orbital pointers and indices.
integer, dimension(:), allocatable, public ncoset
integer, dimension(:, :), allocatable, public indco
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)
Get the QUICKSTEP environment.
Define the quickstep kind type and their sub types.
subroutine, public get_qs_kind(qs_kind, basis_set, basis_type, ncgf, nsgf, all_potential, tnadd_potential, gth_potential, sgp_potential, upf_potential, cneo_potential, se_parameter, dftb_parameter, xtb_parameter, dftb3_param, zatom, zeff, elec_conf, mao, lmax_dftb, alpha_core_charge, ccore_charge, core_charge, core_charge_radius, paw_proj_set, paw_atom, hard_radius, hard0_radius, max_rad_local, covalent_radius, vdw_radius, gpw_type_forced, harmonics, max_iso_not0, max_s_harm, grid_atom, ngrid_ang, ngrid_rad, lmax_rho0, dft_plus_u_atom, l_of_dft_plus_u, n_of_dft_plus_u, u_minus_j, hund_j, u_of_dft_plus_u, j_of_dft_plus_u, alpha_of_dft_plus_u, beta_of_dft_plus_u, j0_of_dft_plus_u, occupation_of_dft_plus_u, dispersion, bs_occupation, magnetization, no_optimize, addel, laddel, naddel, orbitals, max_scf, eps_scf, smear, u_ramping, u_minus_j_target, eps_u_ramping, proj_shell_charge, lr_atom, do_mtlr, u_j_loop, ao_coef, init_u_ramping_each_scf, reltmat, ghost, monovalent, floating, name, element_symbol, pao_basis_size, pao_model_file, pao_potentials, pao_descriptors, nelec)
Get attributes of an atomic kind.
Provides all information about an atomic kind.
Type defining parameters related to the simulation cell.
Definition cell_types.F:60
Contains information about kpoints.
stores all the informations relevant to an mpi environment
Provides all information about a quickstep kind.