(git:27c966b)
Loading...
Searching...
No Matches
kpoint_methods.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 needed for kpoint calculation
10!> \par History
11!> 2014.07 created [JGH]
12!> 2014.11 unified k-point and gamma-point code [Ole Schuett]
13!> \author JGH
14! **************************************************************************************************
17 USE cell_types, ONLY: cell_type,&
25 USE cp_cfm_types, ONLY: cp_cfm_create,&
34 USE cp_dbcsr_api, ONLY: &
40 dbcsr_type, dbcsr_type_antisymmetric, dbcsr_type_no_symmetry, dbcsr_type_symmetric
52 USE cp_fm_types, ONLY: &
57 USE cryssym, ONLY: crys_sym_gen,&
58 csym_type,&
67 smear_mp,&
73 USE kinds, ONLY: dp,&
74 int_8
83 USE kpoint_types, ONLY: &
89 USE mathconstants, ONLY: twopi,&
90 z_one,&
91 z_zero
92 USE mathlib, ONLY: inv_3x3
94 USE message_passing, ONLY: mp_cart_type,&
102 USE qs_mo_types, ONLY: allocate_mo_set,&
103 get_mo_set,&
115 USE smearing_utils, ONLY: smearkp,&
116 smearkp2,&
119 USE util, ONLY: get_limit,&
120 sort
121
122!$ USE OMP_LIB, ONLY: omp_get_max_threads, &
123!$ omp_get_thread_num
124#include "./base/base_uses.f90"
125
126 IMPLICIT NONE
127
128 PRIVATE
129
130 CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'kpoint_methods'
131
133 INTEGER :: nentry = 0, ngroup = 0
134 LOGICAL :: symmetric = .false.
135 INTEGER, ALLOCATABLE, DIMENSION(:) :: col, col_offset, group_start, image, row, row_offset
136 INTEGER, ALLOCATABLE, DIMENSION(:, :) :: cell
137 REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: symmetry_sign
139
140 ! Non-owning view of one symmetry contribution; valid while kpoint metadata is unchanged.
141 TYPE :: kp_symmetry_op_type
142 TYPE(kind_rotmat_type), DIMENSION(:), POINTER :: kind_rot => null()
143 TYPE(kind_rotmat_type), DIMENSION(:), POINTER :: kind_rot_right => null()
144 INTEGER, DIMENSION(:), POINTER :: atom_map => null(), atom_kind => null()
145 INTEGER, DIMENSION(:, :), POINTER :: cell_shift => null()
146 REAL(kind=dp), DIMENSION(3) :: xkp = 0.0_dp
147 LOGICAL :: time_reversal = .false., identity = .false., redistribute = .false.
148 END TYPE kp_symmetry_op_type
149
150 ! Non-owning operation metadata and owned, group-local density contributions.
151 TYPE :: kp_density_group_type
152 TYPE(kp_symmetry_op_type) :: op
153 LOGICAL :: reverse_phase = .false.
154 INTEGER, ALLOCATABLE :: inverse(:), ikp(:)
155 REAL(kind=dp), ALLOCATABLE :: xkp(:, :), weight(:)
156 END TYPE kp_density_group_type
157
167
168! **************************************************************************************************
169
171 COMPLEX(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :) :: values
172 INTEGER, ALLOCATABLE, DIMENSION(:) :: block_col, block_nelem, block_offset, block_row
173 LOGICAL :: ready = .false.
174 END TYPE rskp_grid_type
175
176! **************************************************************************************************
177
178CONTAINS
179
180! **************************************************************************************************
181!> \brief Index full-grid links by irreducible k-point without rescanning the complete mesh.
182!> \param representative irreducible representative of each full-grid link
183!> \param nkp number of irreducible k-points
184!> \param start offsets into order for each irreducible k-point
185!> \param order full-grid link indices grouped by irreducible representative
186! **************************************************************************************************
187 SUBROUTINE kp_link_index_create(representative, nkp, start, order)
188
189 INTEGER, INTENT(IN) :: representative(:), nkp
190 INTEGER, ALLOCATABLE, INTENT(OUT) :: start(:), order(:)
191
192 INTEGER :: i, ik
193 INTEGER, ALLOCATABLE :: count(:), next(:)
194
195 ALLOCATE (count(nkp), next(nkp), start(nkp + 1), order(SIZE(representative)))
196 count(:) = 0
197 DO i = 1, SIZE(representative)
198 ik = representative(i)
199 cpassert(ik >= 1 .AND. ik <= nkp)
200 count(ik) = count(ik) + 1
201 END DO
202 start(1) = 1
203 DO ik = 1, nkp
204 start(ik + 1) = start(ik) + count(ik)
205 next(ik) = start(ik)
206 END DO
207 DO i = 1, SIZE(representative)
208 ik = representative(i)
209 order(next(ik)) = i
210 next(ik) = next(ik) + 1
211 END DO
212
213 END SUBROUTINE kp_link_index_create
214
215! **************************************************************************************************
216!> \brief Precompute lattice representations of crystallographic rotations.
217!> \param crys_sym crystallographic symmetry data
218!> \param cell simulation cell
219!> \param srot scaled-coordinate rotations
220!> \param frot integer direct-lattice rotations
221!> \param krot integer reciprocal-lattice rotations
222! **************************************************************************************************
223 SUBROUTINE kp_rotation_cache_create(crys_sym, cell, srot, frot, krot)
224
225 TYPE(csym_type), INTENT(IN) :: crys_sym
226 TYPE(cell_type), POINTER :: cell
227 REAL(KIND=dp), ALLOCATABLE, INTENT(OUT) :: srot(:, :, :)
228 INTEGER, ALLOCATABLE, INTENT(OUT) :: frot(:, :, :), krot(:, :, :)
229
230 INTEGER :: ic
231
232 ALLOCATE (srot(3, 3, crys_sym%nrtot), frot(3, 3, crys_sym%nrtot), krot(3, 3, crys_sym%nrtot))
233 DO ic = 1, crys_sym%nrtot
234 srot(:, :, ic) = matmul(cell%h_inv, matmul(crys_sym%rt(:, :, ic), cell%hmat))
235 frot(:, :, ic) = nint(srot(:, :, ic))
236 krot(:, :, ic) = nint(transpose(inv_3x3(real(frot(:, :, ic), kind=dp))))
237 END DO
238
239 END SUBROUTINE kp_rotation_cache_create
240
241! **************************************************************************************************
242!> \brief Import symmetry metadata for one irreducible k-point.
243!> \param kpoint k-point environment
244!> \param crys_sym crystallographic symmetry data
245!> \param ik irreducible k-point index
246!> \param natom number of atoms
247!> \param scoord scaled atomic coordinates
248!> \param agauge periodic-image gauge of each atom
249!> \param eps_kpoint reciprocal-space matching tolerance
250!> \param link_start offsets into link_order
251!> \param link_order full-grid links grouped by irreducible representative
252!> \param srot_cache scaled-coordinate rotations
253!> \param frot_cache integer direct-lattice rotations
254!> \param krot_cache integer reciprocal-lattice rotations
255! **************************************************************************************************
256 SUBROUTINE kp_symmetry_import(kpoint, crys_sym, ik, natom, scoord, agauge, eps_kpoint, &
257 link_start, link_order, srot_cache, frot_cache, krot_cache)
258
259 TYPE(kpoint_type), POINTER :: kpoint
260 TYPE(csym_type), INTENT(IN) :: crys_sym
261 INTEGER, INTENT(IN) :: ik, natom
262 REAL(KIND=dp), INTENT(IN) :: scoord(:, :)
263 INTEGER, INTENT(IN) :: agauge(:, :)
264 REAL(KIND=dp), INTENT(IN) :: eps_kpoint
265 INTEGER, ALLOCATABLE, INTENT(IN) :: link_start(:), link_order(:)
266 REAL(KIND=dp), ALLOCATABLE, INTENT(IN) :: srot_cache(:, :, :)
267 INTEGER, ALLOCATABLE, INTENT(IN) :: frot_cache(:, :, :), krot_cache(:, :, :)
268
269 INTEGER :: ic, ilink, ir, ira, is, isign, j, nr, ns
270 INTEGER, DIMENSION(3, 3) :: frot, krot
271 REAL(KIND=dp), DIMENSION(3) :: diff, kgvec, srot
272 REAL(KIND=dp), DIMENSION(3, 3) :: srotmat
273 TYPE(kpoint_sym_type), POINTER :: kpsym
274
275 NULLIFY (kpoint%kp_sym(ik)%kpoint_sym)
276 CALL kpoint_sym_create(kpoint%kp_sym(ik)%kpoint_sym)
277 kpsym => kpoint%kp_sym(ik)%kpoint_sym
278 IF (crys_sym%nrtot <= 0 .OR. crys_sym%fullgrid .OR. &
279 crys_sym%istriz /= 1 .OR. crys_sym%inversion_only) RETURN
280
281 cpassert(ALLOCATED(link_start) .AND. ALLOCATED(link_order))
282 cpassert(ALLOCATED(srot_cache) .AND. ALLOCATED(frot_cache) .AND. ALLOCATED(krot_cache))
283 kpsym%nwght = nint(crys_sym%wkpoint(ik))
284 ns = kpsym%nwght
285 IF (ns <= 1) RETURN
286
287 ! Count mappings not already recorded by kpoint_gen.
288 DO ilink = link_start(ik), link_start(ik + 1) - 1
289 is = link_order(ilink)
290 DO ic = 1, crys_sym%nrtot
291 krot(1:3, 1:3) = krot_cache(1:3, 1:3, ic)
292 DO isign = 1, 2
293 ir = merge(crys_sym%ibrot(ic), -crys_sym%ibrot(ic), isign == 1)
294 IF (ir == crys_sym%kpop(is)) cycle
295 kgvec(1:3) = crys_sym%kpmesh(1:3, is) - &
296 matmul(real(merge(krot(1:3, 1:3), -krot(1:3, 1:3), isign == 1), kind=dp), &
297 kpoint%xkp(1:3, ik))
298 diff(1:3) = kgvec(1:3) - anint(kgvec(1:3))
299 IF (all(abs(diff(1:3)) < eps_kpoint)) ns = ns + 1
300 END DO
301 END DO
302 END DO
303
304 kpsym%apply_symmetry = .true.
305 ALLOCATE (kpsym%rot(3, 3, ns), kpsym%xkp(3, ns), kpsym%rotp(ns))
306 ALLOCATE (kpsym%f0(natom, ns), kpsym%fcell(3, natom, ns), kpsym%fcell_gauge(3, natom, ns))
307 ALLOCATE (kpsym%phase_mode(ns))
308 kpsym%phase_mode(:) = 0
309 kpoint%phases_calibrated = .false.
310
311 nr = 0
312 DO ilink = link_start(ik), link_start(ik + 1) - 1
313 is = link_order(ilink)
314 nr = nr + 1
315 ir = crys_sym%kpop(is)
316 ira = abs(ir)
317 DO ic = 1, crys_sym%nrtot
318 IF (crys_sym%ibrot(ic) /= ira) cycle
319 kpsym%rotp(nr) = ir
320 kpsym%rot(1:3, 1:3, nr) = crys_sym%rt(1:3, 1:3, ic)
321 srotmat(1:3, 1:3) = srot_cache(1:3, 1:3, ic)
322 frot(1:3, 1:3) = frot_cache(1:3, 1:3, ic)
323 kpsym%xkp(1:3, nr) = crys_sym%kpmesh(1:3, is)
324 kpsym%f0(1:natom, nr) = crys_sym%f0(1:natom, ic)
325 DO j = 1, natom
326 srot(1:3) = matmul(srotmat, scoord(1:3, j)) + crys_sym%vt(1:3, ic)
327 kpsym%fcell(1:3, j, nr) = nint(srot(1:3) - scoord(1:3, kpsym%f0(j, nr)))
328 kpsym%fcell_gauge(1:3, j, nr) = kpsym%fcell(1:3, j, nr) + &
329 matmul(frot, agauge(1:3, j)) - &
330 agauge(1:3, kpsym%f0(j, nr))
331 END DO
332 EXIT
333 END DO
334 cpassert(ic <= crys_sym%nrtot)
335 END DO
336
337 ! Complete each star with equivalent operations not selected by kpoint_gen.
338 DO ilink = link_start(ik), link_start(ik + 1) - 1
339 is = link_order(ilink)
340 DO ic = 1, crys_sym%nrtot
341 srotmat(1:3, 1:3) = srot_cache(1:3, 1:3, ic)
342 frot(1:3, 1:3) = frot_cache(1:3, 1:3, ic)
343 krot(1:3, 1:3) = krot_cache(1:3, 1:3, ic)
344 DO isign = 1, 2
345 ir = merge(crys_sym%ibrot(ic), -crys_sym%ibrot(ic), isign == 1)
346 IF (ir == crys_sym%kpop(is)) cycle
347 kgvec(1:3) = crys_sym%kpmesh(1:3, is) - &
348 matmul(real(merge(krot(1:3, 1:3), -krot(1:3, 1:3), isign == 1), kind=dp), &
349 kpoint%xkp(1:3, ik))
350 diff(1:3) = kgvec(1:3) - anint(kgvec(1:3))
351 IF (.NOT. all(abs(diff(1:3)) < eps_kpoint)) cycle
352 nr = nr + 1
353 kpsym%rotp(nr) = ir
354 kpsym%rot(1:3, 1:3, nr) = crys_sym%rt(1:3, 1:3, ic)
355 kpsym%xkp(1:3, nr) = crys_sym%kpmesh(1:3, is)
356 kpsym%f0(1:natom, nr) = crys_sym%f0(1:natom, ic)
357 DO j = 1, natom
358 srot(1:3) = matmul(srotmat, scoord(1:3, j)) + crys_sym%vt(1:3, ic)
359 kpsym%fcell(1:3, j, nr) = nint(srot(1:3) - scoord(1:3, kpsym%f0(j, nr)))
360 kpsym%fcell_gauge(1:3, j, nr) = kpsym%fcell(1:3, j, nr) + &
361 matmul(frot, agauge(1:3, j)) - &
362 agauge(1:3, kpsym%f0(j, nr))
363 END DO
364 END DO
365 END DO
366 END DO
367 cpassert(nr == ns)
368 kpsym%nwred = nr
369
370 END SUBROUTINE kp_symmetry_import
371
372! **************************************************************************************************
373!> \brief Generate the kpoints and initialize the kpoint environment
374!> \param kpoint The kpoint environment
375!> \param particle_set Particle types and coordinates
376!> \param cell Computational cell information
377! **************************************************************************************************
378 SUBROUTINE kpoint_initialize(kpoint, particle_set, cell)
379
380 TYPE(kpoint_type), POINTER :: kpoint
381 TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
382 TYPE(cell_type), POINTER :: cell
383
384 CHARACTER(LEN=*), PARAMETER :: routinen = 'kpoint_initialize'
385
386 INTEGER :: handle, i, ik, iounit, j, natom, nkind, &
387 ns
388 INTEGER, ALLOCATABLE, DIMENSION(:) :: atype, kp_link_order, kp_link_start
389 INTEGER, ALLOCATABLE, DIMENSION(:, :) :: agauge
390 INTEGER, ALLOCATABLE, DIMENSION(:, :, :) :: frot_cache, krot_cache
391 LOGICAL :: spez
392 REAL(kind=dp) :: eps_kpoint, wsum
393 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :) :: coord, scoord
394 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :, :) :: srot_cache
395 REAL(kind=dp), DIMENSION(3) :: r_pbc, scoord_pbc
396 REAL(kind=dp), DIMENSION(:), POINTER :: wkp_full
397 REAL(kind=dp), DIMENSION(:, :), POINTER :: xkp_full
398 TYPE(csym_type) :: crys_sym
399
400 CALL timeset(routinen, handle)
401
402 cpassert(ASSOCIATED(kpoint))
403
404 SELECT CASE (kpoint%kp_scheme)
405 CASE ("NONE")
406 ! do nothing
407 CASE ("GAMMA")
408 kpoint%nkp = 1
409 ALLOCATE (kpoint%xkp(3, 1), kpoint%wkp(1))
410 kpoint%xkp(1:3, 1) = 0.0_dp
411 kpoint%wkp(1) = 1.0_dp
412 ALLOCATE (kpoint%kp_sym(1))
413 NULLIFY (kpoint%kp_sym(1)%kpoint_sym)
414 CALL kpoint_sym_create(kpoint%kp_sym(1)%kpoint_sym)
415 CASE ("MONKHORST-PACK", "MACDONALD")
416
417 IF (.NOT. kpoint%symmetry) THEN
418 ! we set up a random molecule to avoid any possible symmetry
419 natom = 10
420 ALLOCATE (coord(3, natom), scoord(3, natom), atype(natom))
421 DO i = 1, natom
422 atype(i) = i
423 coord(1, i) = sin(i*0.12345_dp)
424 coord(2, i) = cos(i*0.23456_dp)
425 coord(3, i) = sin(i*0.34567_dp)
426 CALL real_to_scaled(scoord(1:3, i), coord(1:3, i), cell)
427 END DO
428 ELSE
429 natom = SIZE(particle_set)
430 ALLOCATE (scoord(3, natom), atype(natom))
431 DO i = 1, natom
432 CALL get_atomic_kind(atomic_kind=particle_set(i)%atomic_kind, kind_number=atype(i))
433 CALL real_to_scaled(scoord(1:3, i), particle_set(i)%r(1:3), cell)
434 END DO
435 END IF
436 IF (kpoint%verbose) THEN
438 ELSE
439 iounit = -1
440 END IF
441 ! kind type list
442 ALLOCATE (kpoint%atype(natom))
443 kpoint%atype = atype
444 ! Match the atom images used by CP2K's periodic neighbor lists.
445 ALLOCATE (agauge(3, natom))
446 agauge = 0
447 IF (kpoint%symmetry) THEN
448 DO i = 1, natom
449 r_pbc(1:3) = pbc_stable(particle_set(i)%r(1:3), cell)
450 CALL real_to_scaled(scoord_pbc, r_pbc, cell)
451 agauge(1:3, i) = nint(scoord_pbc(1:3) - scoord(1:3, i))
452 END DO
453 END IF
454
455 CALL crys_sym_gen(crys_sym, scoord, atype, cell%hmat, delta=kpoint%eps_geo, iounit=iounit, &
456 use_spglib=kpoint%symmetry)
457 CALL kpoint_gen(crys_sym, kpoint%nkp_grid, symm=kpoint%symmetry, shift=kpoint%kp_shift, &
458 full_grid=kpoint%full_grid, gamma_centered=kpoint%gamma_centered, &
459 inversion_symmetry_only=kpoint%inversion_symmetry_only, &
460 use_spglib_reduction= &
461 kpoint%symmetry_reduction_method == use_spglib_kpoint_symmetry, &
462 use_spglib_backend=kpoint%symmetry_backend == use_spglib_kpoint_backend)
463 IF (crys_sym%inversion_only) kpoint%inversion_symmetry_only = .true.
464 kpoint%nkp = crys_sym%nkpoint
465 ALLOCATE (kpoint%xkp(3, kpoint%nkp), kpoint%wkp(kpoint%nkp))
466 wsum = sum(crys_sym%wkpoint)
467 DO ik = 1, kpoint%nkp
468 kpoint%xkp(1:3, ik) = crys_sym%xkpoint(1:3, ik)
469 kpoint%wkp(ik) = crys_sym%wkpoint(ik)/wsum
470 END DO
471 IF (crys_sym%nrtot > 0 .AND. .NOT. crys_sym%fullgrid .AND. &
472 crys_sym%istriz == 1 .AND. .NOT. crys_sym%inversion_only) THEN
473 CALL kp_link_index_create(crys_sym%kplink(2, :), kpoint%nkp, kp_link_start, kp_link_order)
474 CALL kp_rotation_cache_create(crys_sym, cell, srot_cache, frot_cache, krot_cache)
475 END IF
476
477 eps_kpoint = max(1.e-12_dp, 10.0_dp*kpoint%eps_geo)
478 ! print output
479 IF (kpoint%symmetry) CALL print_crys_symmetry(crys_sym)
480 IF (kpoint%symmetry) CALL print_kp_symmetry(crys_sym)
481
482 ! transfer symmetry information
483 ALLOCATE (kpoint%kp_sym(kpoint%nkp))
484 DO ik = 1, kpoint%nkp
485 CALL kp_symmetry_import(kpoint, crys_sym, ik, natom, scoord, agauge, eps_kpoint, &
486 kp_link_start, kp_link_order, srot_cache, frot_cache, krot_cache)
487 END DO
488 IF (kpoint%symmetry) THEN
489 nkind = maxval(atype)
490 ns = crys_sym%nrtot
491 ALLOCATE (kpoint%kind_rotmat(ns, nkind))
492 DO i = 1, ns
493 DO j = 1, nkind
494 NULLIFY (kpoint%kind_rotmat(i, j)%rmat)
495 END DO
496 END DO
497 ALLOCATE (kpoint%kind_rotmat_aux(ns, nkind))
498 DO i = 1, ns
499 DO j = 1, nkind
500 NULLIFY (kpoint%kind_rotmat_aux(i, j)%rmat)
501 END DO
502 END DO
503 ALLOCATE (kpoint%ibrot(ns))
504 kpoint%ibrot(1:ns) = crys_sym%ibrot(1:ns)
505 END IF
506
507 CALL release_csym_type(crys_sym)
508 DEALLOCATE (scoord, atype)
509 DEALLOCATE (agauge)
510
511 CASE ("GENERAL")
512 NULLIFY (xkp_full, wkp_full)
513 IF (ASSOCIATED(kpoint%xkp_input)) THEN
514 xkp_full => kpoint%xkp_input
515 wkp_full => kpoint%wkp_input
516 ELSE
517 xkp_full => kpoint%xkp
518 wkp_full => kpoint%wkp
519 END IF
520 cpassert(ASSOCIATED(xkp_full))
521 cpassert(ASSOCIATED(wkp_full))
522 IF (.NOT. ASSOCIATED(kpoint%xkp_input)) THEN
523 ALLOCATE (kpoint%xkp_input(3, SIZE(wkp_full)), kpoint%wkp_input(SIZE(wkp_full)))
524 kpoint%xkp_input(1:3, 1:SIZE(wkp_full)) = xkp_full(1:3, 1:SIZE(wkp_full))
525 kpoint%wkp_input(1:SIZE(wkp_full)) = wkp_full(1:SIZE(wkp_full))
526 xkp_full => kpoint%xkp_input
527 wkp_full => kpoint%wkp_input
528 END IF
529 IF (.NOT. kpoint%symmetry) THEN
530 IF (.NOT. ASSOCIATED(kpoint%xkp)) THEN
531 kpoint%nkp = SIZE(wkp_full)
532 ALLOCATE (kpoint%xkp(3, kpoint%nkp), kpoint%wkp(kpoint%nkp))
533 kpoint%xkp(1:3, 1:kpoint%nkp) = xkp_full(1:3, 1:kpoint%nkp)
534 kpoint%wkp(1:kpoint%nkp) = wkp_full(1:kpoint%nkp)
535 END IF
536 ! default: no symmetry settings
537 ALLOCATE (kpoint%kp_sym(kpoint%nkp))
538 DO i = 1, kpoint%nkp
539 NULLIFY (kpoint%kp_sym(i)%kpoint_sym)
540 CALL kpoint_sym_create(kpoint%kp_sym(i)%kpoint_sym)
541 END DO
542 ELSE
543 IF (kpoint%verbose) THEN
545 ELSE
546 iounit = -1
547 END IF
548 natom = SIZE(particle_set)
549 ALLOCATE (scoord(3, natom), atype(natom))
550 DO i = 1, natom
551 CALL get_atomic_kind(atomic_kind=particle_set(i)%atomic_kind, kind_number=atype(i))
552 CALL real_to_scaled(scoord(1:3, i), particle_set(i)%r(1:3), cell)
553 END DO
554 ALLOCATE (kpoint%atype(natom))
555 kpoint%atype = atype
556 ALLOCATE (agauge(3, natom))
557 DO i = 1, natom
558 r_pbc(1:3) = pbc_stable(particle_set(i)%r(1:3), cell)
559 CALL real_to_scaled(scoord_pbc, r_pbc, cell)
560 agauge(1:3, i) = nint(scoord_pbc(1:3) - scoord(1:3, i))
561 END DO
562
563 CALL crys_sym_gen(crys_sym, scoord, atype, cell%hmat, delta=kpoint%eps_geo, iounit=iounit, &
564 use_spglib=(kpoint%symmetry_backend == use_spglib_kpoint_backend .OR. &
565 kpoint%symmetry_reduction_method == use_spglib_kpoint_symmetry))
566 CALL kpoint_gen_general(crys_sym, xkp_full, wkp_full, symm=kpoint%symmetry, &
567 full_grid=kpoint%full_grid, &
568 inversion_symmetry_only=kpoint%inversion_symmetry_only, &
569 use_spglib_reduction= &
570 kpoint%symmetry_reduction_method == use_spglib_kpoint_symmetry, &
571 use_spglib_backend=kpoint%symmetry_backend == use_spglib_kpoint_backend)
572 IF (crys_sym%inversion_only) kpoint%inversion_symmetry_only = .true.
573 IF (ASSOCIATED(kpoint%xkp)) THEN
574 DEALLOCATE (kpoint%xkp)
575 NULLIFY (kpoint%xkp)
576 END IF
577 IF (ASSOCIATED(kpoint%wkp)) THEN
578 DEALLOCATE (kpoint%wkp)
579 NULLIFY (kpoint%wkp)
580 END IF
581 kpoint%nkp = crys_sym%nkpoint
582 ALLOCATE (kpoint%xkp(3, kpoint%nkp), kpoint%wkp(kpoint%nkp))
583 wsum = sum(crys_sym%wkpoint)
584 DO ik = 1, kpoint%nkp
585 kpoint%xkp(1:3, ik) = crys_sym%xkpoint(1:3, ik)
586 kpoint%wkp(ik) = crys_sym%wkpoint(ik)/wsum
587 END DO
588 IF (crys_sym%nrtot > 0 .AND. .NOT. crys_sym%fullgrid .AND. &
589 crys_sym%istriz == 1 .AND. .NOT. crys_sym%inversion_only) THEN
590 CALL kp_link_index_create(crys_sym%kplink(2, :), kpoint%nkp, kp_link_start, kp_link_order)
591 CALL kp_rotation_cache_create(crys_sym, cell, srot_cache, frot_cache, krot_cache)
592 END IF
593
594 eps_kpoint = max(1.e-12_dp, 10.0_dp*kpoint%eps_geo)
595 CALL print_crys_symmetry(crys_sym)
596 CALL print_kp_symmetry(crys_sym)
597
598 ALLOCATE (kpoint%kp_sym(kpoint%nkp))
599 DO ik = 1, kpoint%nkp
600 CALL kp_symmetry_import(kpoint, crys_sym, ik, natom, scoord, agauge, eps_kpoint, &
601 kp_link_start, kp_link_order, srot_cache, frot_cache, krot_cache)
602 END DO
603 nkind = maxval(atype)
604 ns = crys_sym%nrtot
605 ALLOCATE (kpoint%kind_rotmat(ns, nkind))
606 DO i = 1, ns
607 DO j = 1, nkind
608 NULLIFY (kpoint%kind_rotmat(i, j)%rmat)
609 END DO
610 END DO
611 ALLOCATE (kpoint%kind_rotmat_aux(ns, nkind))
612 DO i = 1, ns
613 DO j = 1, nkind
614 NULLIFY (kpoint%kind_rotmat_aux(i, j)%rmat)
615 END DO
616 END DO
617 ALLOCATE (kpoint%ibrot(ns))
618 kpoint%ibrot(1:ns) = crys_sym%ibrot(1:ns)
619
620 CALL release_csym_type(crys_sym)
621 DEALLOCATE (scoord, atype)
622 DEALLOCATE (agauge)
623 END IF
624 CASE DEFAULT
625 cpabort("Option invalid or unavailable for kpoint%kp_scheme")
626 END SELECT
627
628 ! check for consistency of options
629 SELECT CASE (kpoint%kp_scheme)
630 CASE ("NONE")
631 ! don't use k-point code
632 CASE ("GAMMA")
633 cpassert(kpoint%nkp == 1)
634 cpassert(sum(abs(kpoint%xkp)) <= 1.e-12_dp)
635 cpassert(kpoint%wkp(1) == 1.0_dp)
636 cpassert(.NOT. kpoint%symmetry)
637 CASE ("GENERAL")
638 cpassert(kpoint%nkp >= 1)
639 CASE ("MONKHORST-PACK", "MACDONALD")
640 cpassert(kpoint%nkp >= 1)
641 END SELECT
642 IF (kpoint%use_real_wfn) THEN
643 ! what about inversion symmetry?
644 ikloop: DO ik = 1, kpoint%nkp
645 DO i = 1, 3
646 spez = (kpoint%xkp(i, ik) == 0.0_dp .OR. kpoint%xkp(i, ik) == 0.5_dp)
647 IF (.NOT. spez) EXIT ikloop
648 END DO
649 END DO ikloop
650 IF (.NOT. spez) THEN
651 ! Warning: real wfn might be wrong for this system
652 CALL cp_warn(__location__, &
653 "A calculation using real wavefunctions is requested. "// &
654 "We could not determine if the symmetry of the system allows real wavefunctions. ")
655 END IF
656 END IF
657
658 CALL timestop(handle)
659
660 END SUBROUTINE kpoint_initialize
661
662! **************************************************************************************************
663!> \brief Initialize the kpoint environment
664!> \param kpoint Kpoint environment
665!> \param para_env ...
666!> \param blacs_env ...
667!> \param with_aux_fit ...
668! **************************************************************************************************
669 SUBROUTINE kpoint_env_initialize(kpoint, para_env, blacs_env, with_aux_fit)
670
671 TYPE(kpoint_type), INTENT(INOUT) :: kpoint
672 TYPE(mp_para_env_type), INTENT(IN), TARGET :: para_env
673 TYPE(cp_blacs_env_type), INTENT(IN), TARGET :: blacs_env
674 LOGICAL, INTENT(IN), OPTIONAL :: with_aux_fit
675
676 CHARACTER(LEN=*), PARAMETER :: routinen = 'kpoint_env_initialize'
677
678 INTEGER :: handle, igr, ngr, niogrp, nkp, &
679 nkp_grp, nkp_loc, npe, unit_nr
680 INTEGER, DIMENSION(2) :: dims, pos
681 LOGICAL :: aux_fit
682 TYPE(mp_cart_type) :: comm_cart
683 TYPE(mp_para_env_type), POINTER :: para_env_inter_kp, para_env_kp
684
685 CALL timeset(routinen, handle)
686
687 IF (PRESENT(with_aux_fit)) THEN
688 aux_fit = with_aux_fit
689 ELSE
690 aux_fit = .false.
691 END IF
692
693 kpoint%para_env => para_env
694 CALL kpoint%para_env%retain()
695 kpoint%blacs_env_all => blacs_env
696 CALL kpoint%blacs_env_all%retain()
697
698 cpassert(.NOT. ASSOCIATED(kpoint%kp_env))
699 IF (aux_fit) THEN
700 cpassert(.NOT. ASSOCIATED(kpoint%kp_aux_env))
701 END IF
702
703 nkp = kpoint%nkp
704 npe = para_env%num_pe
705 IF (npe == 1) THEN
706 ! only one process available -> owns all kpoints
707 ALLOCATE (kpoint%kp_dist(2, 1))
708 kpoint%kp_dist(1, 1) = 1
709 kpoint%kp_dist(2, 1) = nkp
710 kpoint%kp_range(1) = 1
711 kpoint%kp_range(2) = nkp
712
713 ! parallel environments
714 kpoint%para_env_kp => para_env
715 CALL kpoint%para_env_kp%retain()
716 kpoint%para_env_inter_kp => para_env
717 CALL kpoint%para_env_inter_kp%retain()
718 kpoint%iogrp = .true.
719 kpoint%nkp_groups = 1
720 ELSE
721 IF (kpoint%parallel_group_size == -1) THEN
722 ! Maximum parallelization over kpoints with equal-sized MPI groups.
723 ! Each group must own at least one kpoint; their kpoint counts may differ.
724 DO igr = npe, 1, -1
725 IF (mod(npe, igr) /= 0) cycle
726 nkp_grp = npe/igr
727 IF (nkp_grp > nkp) cycle
728 ngr = igr
729 END DO
730 ELSE IF (kpoint%parallel_group_size == 0) THEN
731 ! no parallelization over kpoints
732 ngr = npe
733 ELSE IF (kpoint%parallel_group_size > 0) THEN
734 ngr = min(kpoint%parallel_group_size, npe)
735 ELSE
736 cpabort("kpoint%parallel_group_size cannot be smaller than -1")
737 END IF
738 nkp_grp = npe/ngr
739 ! processor dimensions
740 dims(1) = ngr
741 dims(2) = nkp_grp
742 IF ((dims(1)*dims(2) /= npe)) THEN
743 cpabort("Number of processors is not divisible by the kpoint group size.")
744 END IF
745 IF (nkp_grp > nkp) THEN
746 cpabort("Too many kpoint groups. Increase PARALLEL_GROUP_SIZE.")
747 END IF
748
749 ! Create the subgroups, one for each k-point group and one interconnecting group
750 CALL comm_cart%create(comm_old=para_env, ndims=2, dims=dims)
751 pos = comm_cart%mepos_cart
752 ALLOCATE (para_env_kp)
753 CALL para_env_kp%from_split(comm_cart, pos(2))
754 ALLOCATE (para_env_inter_kp)
755 CALL para_env_inter_kp%from_split(comm_cart, pos(1))
756 CALL comm_cart%free()
757
758 niogrp = 0
759 IF (para_env%is_source()) niogrp = 1
760 CALL para_env_kp%sum(niogrp)
761 kpoint%iogrp = (niogrp == 1)
762
763 ! parallel groups
764 kpoint%para_env_kp => para_env_kp
765 kpoint%para_env_inter_kp => para_env_inter_kp
766
767 ! distribution of kpoints
768 ALLOCATE (kpoint%kp_dist(2, nkp_grp))
769 DO igr = 1, nkp_grp
770 kpoint%kp_dist(1:2, igr) = get_limit(nkp, nkp_grp, igr - 1)
771 END DO
772 ! local kpoints
773 kpoint%kp_range(1:2) = kpoint%kp_dist(1:2, para_env_inter_kp%mepos + 1)
774 nkp_loc = kpoint%kp_range(2) - kpoint%kp_range(1) + 1
775
777
778 IF (unit_nr > 0 .AND. kpoint%verbose) THEN
779 WRITE (unit_nr, *)
780 WRITE (unit_nr, fmt="(T2,A,T71,I10)") "KPOINTS| Total number of kpoints", nkp
781 WRITE (unit_nr, fmt="(T2,A,T71,I10)") "KPOINTS| Number of kpoint groups ", nkp_grp
782 WRITE (unit_nr, fmt="(T2,A,T71,I10)") "KPOINTS| Size of each kpoint group", ngr
783 IF (mod(nkp, nkp_grp) == 0) THEN
784 WRITE (unit_nr, fmt="(T2,A,T71,I10)") "KPOINTS| Number of kpoints per group", nkp_loc
785 ELSE
786 WRITE (unit_nr, fmt="(T2,A,T71,I10)") "KPOINTS| Minimum number of kpoints per group", nkp/nkp_grp
787 WRITE (unit_nr, fmt="(T2,A,T71,I10)") "KPOINTS| Maximum number of kpoints per group", nkp/nkp_grp + 1
788 END IF
789 END IF
790 kpoint%nkp_groups = nkp_grp
791
792 END IF
793
794 nkp_loc = kpoint%kp_range(2) - kpoint%kp_range(1) + 1
795 CALL create_local_environments(kpoint%kp_env)
796 IF (aux_fit) CALL create_local_environments(kpoint%kp_aux_env)
797
798 CALL timestop(handle)
799
800 CONTAINS
801
802! **************************************************************************************************
803!> \brief ...
804!> \param env ...
805! **************************************************************************************************
806 SUBROUTINE create_local_environments(env)
807 TYPE(kpoint_env_p_type), INTENT(OUT), POINTER :: env(:)
808
809 INTEGER :: ik, ikk
810 TYPE(kpoint_env_type), POINTER :: kp
811
812 ALLOCATE (env(nkp_loc))
813 DO ik = 1, nkp_loc
814 ikk = kpoint%kp_range(1) + ik - 1
815 CALL kpoint_env_create(env(ik)%kpoint_env)
816 kp => env(ik)%kpoint_env
817 kp%nkpoint = ikk
818 kp%wkp = kpoint%wkp(ikk)
819 kp%xkp(1:3) = kpoint%xkp(1:3, ikk)
820 kp%is_local = (kpoint%para_env_kp%num_pe == 1)
821 END DO
822 END SUBROUTINE create_local_environments
823
824 END SUBROUTINE kpoint_env_initialize
825
826! **************************************************************************************************
827!> \brief Initialize a set of MOs and density matrix for each kpoint (kpoint group)
828!> \param kpoint Kpoint environment
829!> \param mos Reference MOs (global)
830!> \param added_mos ...
831!> \param for_aux_fit ...
832! **************************************************************************************************
833 SUBROUTINE kpoint_initialize_mos(kpoint, mos, added_mos, for_aux_fit)
834
835 TYPE(kpoint_type), POINTER :: kpoint
836 TYPE(mo_set_type), DIMENSION(:), INTENT(INOUT) :: mos
837 INTEGER, INTENT(IN), OPTIONAL :: added_mos
838 LOGICAL, OPTIONAL :: for_aux_fit
839
840 CHARACTER(LEN=*), PARAMETER :: routinen = 'kpoint_initialize_mos'
841
842 INTEGER :: handle, ic, ik, is, nadd, nao, nc, &
843 nelectron, nkp_loc, nmo, nmorig(2), &
844 nspin
845 LOGICAL :: aux_fit
846 REAL(kind=dp) :: flexible_electron_count, maxocc, n_el_f
847 TYPE(cp_blacs_env_type), POINTER :: blacs_env
848 TYPE(cp_fm_pool_type), POINTER :: ao_ao_fm_pool
849 TYPE(cp_fm_struct_type), POINTER :: matrix_struct
850 TYPE(cp_fm_type), POINTER :: fmlocal
851 TYPE(kpoint_env_type), POINTER :: kp
852 TYPE(qs_matrix_pools_type), POINTER :: mpools
853
854 CALL timeset(routinen, handle)
855
856 IF (PRESENT(for_aux_fit)) THEN
857 aux_fit = for_aux_fit
858 ELSE
859 aux_fit = .false.
860 END IF
861
862 cpassert(ASSOCIATED(kpoint))
863
864 IF (aux_fit) THEN
865 cpassert(ASSOCIATED(kpoint%kp_aux_env))
866 END IF
867
868 IF (PRESENT(added_mos)) THEN
869 nadd = added_mos
870 ELSE
871 nadd = 0
872 END IF
873
874 IF (kpoint%use_real_wfn) THEN
875 nc = 1
876 ELSE
877 nc = 2
878 END IF
879 nspin = SIZE(mos, 1)
880 nkp_loc = kpoint%kp_range(2) - kpoint%kp_range(1) + 1
881 IF (nkp_loc > 0) THEN
882 IF (aux_fit) THEN
883 cpassert(SIZE(kpoint%kp_aux_env) == nkp_loc)
884 ELSE
885 cpassert(SIZE(kpoint%kp_env) == nkp_loc)
886 END IF
887 ! Allocate one MO set per spin; its coefficient matrix owns the scalar type.
888 DO ik = 1, nkp_loc
889 IF (aux_fit) THEN
890 kp => kpoint%kp_aux_env(ik)%kpoint_env
891 ELSE
892 kp => kpoint%kp_env(ik)%kpoint_env
893 END IF
894 ALLOCATE (kp%mos(nspin))
895 DO is = 1, nspin
896 CALL get_mo_set(mos(is), nao=nao, nmo=nmo, nelectron=nelectron, &
897 n_el_f=n_el_f, maxocc=maxocc, flexible_electron_count=flexible_electron_count)
898 ! aux ADMM basis keeps the orbital nmo; do not cap at aux nao
899 IF (.NOT. aux_fit) nmo = min(nao, nmo + nadd)
900 CALL allocate_mo_set(kp%mos(is), nao, nmo, nelectron, n_el_f, maxocc, &
901 flexible_electron_count)
902 END DO
903 ! freshly allocated MOS carry no coefficients: solvers must
904 ! cold-start again until an extrapolation refills them
905 kp%mos_prefilled = .false.
906 END DO
907
908 ! generate the blacs environment for the kpoint group
909 ! we generate a blacs env for each kpoint group in parallel
910 ! we assume here that the group para_env_inter_kp will connect
911 ! equivalent parts of fm matrices, i.e. no reshuffeling of processors
912 NULLIFY (blacs_env)
913 IF (ASSOCIATED(kpoint%blacs_env)) THEN
914 blacs_env => kpoint%blacs_env
915 ELSE
916 CALL cp_blacs_env_create(blacs_env=blacs_env, para_env=kpoint%para_env_kp)
917 kpoint%blacs_env => blacs_env
918 END IF
919
920 ! set possible new number of MOs
921 DO is = 1, nspin
922 CALL get_mo_set(mos(is), nmo=nmorig(is))
923 IF (aux_fit) THEN
924 nmo = nmorig(is)
925 ELSE
926 nmo = min(nao, nmorig(is) + nadd)
927 END IF
928 CALL set_mo_set(mos(is), nmo=nmo)
929 END DO
930 ! matrix pools for the kpoint group, information on MOs is transferred using
931 ! generic mos structure
932 NULLIFY (mpools)
933 CALL mpools_create(mpools=mpools)
934 CALL mpools_rebuild_fm_pools(mpools=mpools, mos=mos, &
935 blacs_env=blacs_env, para_env=kpoint%para_env_kp)
936
937 IF (aux_fit) THEN
938 kpoint%mpools_aux_fit => mpools
939 ELSE
940 kpoint%mpools => mpools
941 END IF
942
943 ! reset old number of MOs
944 DO is = 1, nspin
945 CALL set_mo_set(mos(is), nmo=nmorig(is))
946 END DO
947
948 ! allocate density matrices
949 CALL mpools_get(mpools, ao_ao_fm_pool=ao_ao_fm_pool)
950 ALLOCATE (fmlocal)
951 CALL fm_pool_create_fm(ao_ao_fm_pool, fmlocal)
952 CALL cp_fm_get_info(fmlocal, matrix_struct=matrix_struct)
953 DO ik = 1, nkp_loc
954 IF (aux_fit) THEN
955 kp => kpoint%kp_aux_env(ik)%kpoint_env
956 ELSE
957 kp => kpoint%kp_env(ik)%kpoint_env
958 END IF
959 ! density matrix
960 CALL cp_fm_release(kp%pmat)
961 ALLOCATE (kp%pmat(nc, nspin))
962 DO is = 1, nspin
963 DO ic = 1, nc
964 CALL cp_fm_create(kp%pmat(ic, is), matrix_struct)
965 END DO
966 END DO
967 ! energy weighted density matrix
968 CALL cp_fm_release(kp%wmat)
969 ALLOCATE (kp%wmat(nc, nspin))
970 DO is = 1, nspin
971 DO ic = 1, nc
972 CALL cp_fm_create(kp%wmat(ic, is), matrix_struct)
973 END DO
974 END DO
975 END DO
976 CALL fm_pool_give_back_fm(ao_ao_fm_pool, fmlocal)
977 DEALLOCATE (fmlocal)
978
979 END IF
980
981 CALL timestop(handle)
982
983 END SUBROUTINE kpoint_initialize_mos
984
985! **************************************************************************************************
986!> \brief ...
987!> \param kpoint ...
988! **************************************************************************************************
989 SUBROUTINE kpoint_initialize_mo_set(kpoint)
990 TYPE(kpoint_type), POINTER :: kpoint
991
992 CHARACTER(LEN=*), PARAMETER :: routinen = 'kpoint_initialize_mo_set'
993
994 INTEGER :: handle, ik, ispin
995 TYPE(cp_fm_pool_p_type), DIMENSION(:), POINTER :: ao_mo_fm_pools
996 TYPE(mo_set_type), DIMENSION(:), POINTER :: moskp
997
998 CALL timeset(routinen, handle)
999
1000 DO ik = 1, SIZE(kpoint%kp_env)
1001 CALL mpools_get(kpoint%mpools, ao_mo_fm_pools=ao_mo_fm_pools)
1002 moskp => kpoint%kp_env(ik)%kpoint_env%mos
1003 cpassert(ASSOCIATED(moskp))
1004 DO ispin = 1, SIZE(moskp)
1005 IF (ASSOCIATED(moskp(ispin)%mo_coeff) .OR. &
1006 ASSOCIATED(moskp(ispin)%cmo_coeff)) cycle
1007 CALL init_mo_set(moskp(ispin), fm_pool=ao_mo_fm_pools(ispin)%pool, &
1008 name="kpoints", complex_coeff=.NOT. kpoint%use_real_wfn)
1009 END DO
1010 END DO
1011
1012 CALL timestop(handle)
1013
1014 END SUBROUTINE kpoint_initialize_mo_set
1015
1016! **************************************************************************************************
1017!> \brief Generates the mapping of cell indices and linear RS index
1018!> CELL (0,0,0) is always mapped to index 1
1019!> \param kpoint Kpoint environment
1020!> \param sab_nl Defining neighbour list
1021!> \param para_env Parallel environment
1022!> \param nimages [output]
1023! **************************************************************************************************
1024 SUBROUTINE kpoint_init_cell_index(kpoint, sab_nl, para_env, nimages)
1025
1026 TYPE(kpoint_type), POINTER :: kpoint
1027 TYPE(neighbor_list_set_p_type), DIMENSION(:), &
1028 POINTER :: sab_nl
1029 TYPE(mp_para_env_type), POINTER :: para_env
1030 INTEGER, INTENT(OUT) :: nimages
1031
1032 CHARACTER(LEN=*), PARAMETER :: routinen = 'kpoint_init_cell_index'
1033
1034 INTEGER :: handle, i1, i2, i3, ic, icount, it, &
1035 ncount
1036 INTEGER, DIMENSION(3) :: cell, itm
1037 INTEGER, DIMENSION(:, :), POINTER :: index_to_cell, list
1038 INTEGER, DIMENSION(:, :, :), POINTER :: cell_to_index, cti
1039 LOGICAL :: new
1041 DIMENSION(:), POINTER :: nl_iterator
1042
1043 NULLIFY (cell_to_index, index_to_cell)
1044
1045 CALL timeset(routinen, handle)
1046
1047 cpassert(ASSOCIATED(kpoint))
1048
1049 ALLOCATE (list(3, 125))
1050 list = 0
1051 icount = 1
1052
1053 CALL neighbor_list_iterator_create(nl_iterator, sab_nl)
1054 DO WHILE (neighbor_list_iterate(nl_iterator) == 0)
1055 CALL get_iterator_info(nl_iterator, cell=cell)
1056
1057 new = .true.
1058 DO ic = 1, icount
1059 IF (cell(1) == list(1, ic) .AND. cell(2) == list(2, ic) .AND. &
1060 cell(3) == list(3, ic)) THEN
1061 new = .false.
1062 EXIT
1063 END IF
1064 END DO
1065 IF (new) THEN
1066 icount = icount + 1
1067 IF (icount > SIZE(list, 2)) THEN
1068 CALL reallocate(list, 1, 3, 1, 2*SIZE(list, 2))
1069 END IF
1070 list(1:3, icount) = cell(1:3)
1071 END IF
1072
1073 END DO
1074 CALL neighbor_list_iterator_release(nl_iterator)
1075
1076 itm(1) = maxval(abs(list(1, 1:icount)))
1077 itm(2) = maxval(abs(list(2, 1:icount)))
1078 itm(3) = maxval(abs(list(3, 1:icount)))
1079 CALL para_env%max(itm)
1080 it = maxval(itm(1:3))
1081 IF (ASSOCIATED(kpoint%cell_to_index)) THEN
1082 DEALLOCATE (kpoint%cell_to_index)
1083 END IF
1084 ALLOCATE (kpoint%cell_to_index(-itm(1):itm(1), -itm(2):itm(2), -itm(3):itm(3)))
1085 cell_to_index => kpoint%cell_to_index
1086 cti => cell_to_index
1087 cti(:, :, :) = 0
1088 DO ic = 1, icount
1089 i1 = list(1, ic)
1090 i2 = list(2, ic)
1091 i3 = list(3, ic)
1092 cti(i1, i2, i3) = ic
1093 END DO
1094 CALL para_env%sum(cti)
1095 ncount = 0
1096 DO i1 = -itm(1), itm(1)
1097 DO i2 = -itm(2), itm(2)
1098 DO i3 = -itm(3), itm(3)
1099 IF (cti(i1, i2, i3) == 0) THEN
1100 cti(i1, i2, i3) = 1000000
1101 ELSE
1102 ncount = ncount + 1
1103 cti(i1, i2, i3) = (abs(i1) + abs(i2) + abs(i3))*1000 + abs(i3)*100 + abs(i2)*10 + abs(i1)
1104 cti(i1, i2, i3) = cti(i1, i2, i3) + (i1 + i2 + i3)
1105 END IF
1106 END DO
1107 END DO
1108 END DO
1109
1110 IF (ASSOCIATED(kpoint%index_to_cell)) THEN
1111 DEALLOCATE (kpoint%index_to_cell)
1112 END IF
1113 ALLOCATE (kpoint%index_to_cell(3, ncount))
1114 index_to_cell => kpoint%index_to_cell
1115 DO ic = 1, ncount
1116 cell = minloc(cti)
1117 i1 = cell(1) - 1 - itm(1)
1118 i2 = cell(2) - 1 - itm(2)
1119 i3 = cell(3) - 1 - itm(3)
1120 cti(i1, i2, i3) = 1000000
1121 index_to_cell(1, ic) = i1
1122 index_to_cell(2, ic) = i2
1123 index_to_cell(3, ic) = i3
1124 END DO
1125 cti(:, :, :) = 0
1126 DO ic = 1, ncount
1127 i1 = index_to_cell(1, ic)
1128 i2 = index_to_cell(2, ic)
1129 i3 = index_to_cell(3, ic)
1130 cti(i1, i2, i3) = ic
1131 END DO
1132
1133 ! keep pointer to this neighborlist
1134 kpoint%sab_nl => sab_nl
1135
1136 ! set number of images
1137 nimages = SIZE(index_to_cell, 2)
1138
1139 DEALLOCATE (list)
1140
1141 CALL timestop(handle)
1142
1143 END SUBROUTINE kpoint_init_cell_index
1144
1145! **************************************************************************************************
1146!> \brief Transformation of real space matrices to a kpoint
1147!> \param rmatrix Real part of kpoint matrix
1148!> \param cmatrix Complex part of kpoint matrix (optional)
1149!> \param rsmat Real space matrices
1150!> \param ispin Spin index
1151!> \param xkp Kpoint coordinates
1152!> \param cell_to_index mapping of cell indices to RS index
1153!> \param sab_nl Defining neighbor list
1154!> \param is_complex Matrix to be transformed is imaginary
1155!> \param rs_sign Matrix to be transformed is csaled by rs_sign
1156! **************************************************************************************************
1157 SUBROUTINE rskp_transform(rmatrix, cmatrix, rsmat, ispin, &
1158 xkp, cell_to_index, sab_nl, is_complex, rs_sign)
1159
1160 TYPE(dbcsr_type) :: rmatrix
1161 TYPE(dbcsr_type), OPTIONAL :: cmatrix
1162 TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: rsmat
1163 INTEGER, INTENT(IN) :: ispin
1164 REAL(kind=dp), DIMENSION(3), INTENT(IN) :: xkp
1165 INTEGER, DIMENSION(:, :, :), POINTER :: cell_to_index
1166 TYPE(neighbor_list_set_p_type), DIMENSION(:), &
1167 POINTER :: sab_nl
1168 LOGICAL, INTENT(IN), OPTIONAL :: is_complex
1169 REAL(kind=dp), INTENT(IN), OPTIONAL :: rs_sign
1170
1171 CHARACTER(LEN=*), PARAMETER :: routinen = 'rskp_transform'
1172
1173 INTEGER :: handle, iatom, ic, icol, irow, jatom, &
1174 nimg
1175 INTEGER, DIMENSION(3) :: cell
1176 LOGICAL :: do_symmetric, found, my_complex, &
1177 wfn_real_only
1178 REAL(kind=dp) :: arg, coskl, fsign, fsym, sinkl
1179 REAL(kind=dp), DIMENSION(:, :), POINTER :: cblock, rblock, rsblock
1181 DIMENSION(:), POINTER :: nl_iterator
1182
1183 CALL timeset(routinen, handle)
1184
1185 my_complex = .false.
1186 IF (PRESENT(is_complex)) my_complex = is_complex
1187
1188 fsign = 1.0_dp
1189 IF (PRESENT(rs_sign)) fsign = rs_sign
1190
1191 wfn_real_only = .true.
1192 IF (PRESENT(cmatrix)) wfn_real_only = .false.
1193
1194 nimg = SIZE(rsmat, 2)
1195
1196 CALL get_neighbor_list_set_p(neighbor_list_sets=sab_nl, symmetric=do_symmetric)
1197
1198 CALL neighbor_list_iterator_create(nl_iterator, sab_nl)
1199 DO WHILE (neighbor_list_iterate(nl_iterator) == 0)
1200 CALL get_iterator_info(nl_iterator, iatom=iatom, jatom=jatom, cell=cell)
1201
1202 ! fsym = +- 1 is due to real space matrices being non-symmetric (although in a symmtric type)
1203 ! with the link S_mu^0,nu^b = S_nu^0,mu^-b, and the KP matrices beeing Hermitian
1204 fsym = 1.0_dp
1205 irow = iatom
1206 icol = jatom
1207 IF (do_symmetric .AND. (iatom > jatom)) THEN
1208 irow = jatom
1209 icol = iatom
1210 fsym = -1.0_dp
1211 END IF
1212
1213 ic = cell_to_index(cell(1), cell(2), cell(3))
1214 IF (ic < 1 .OR. ic > nimg) cycle
1215
1216 arg = real(cell(1), dp)*xkp(1) + real(cell(2), dp)*xkp(2) + real(cell(3), dp)*xkp(3)
1217 IF (my_complex) THEN
1218 coskl = fsign*fsym*cos(twopi*arg)
1219 sinkl = fsign*sin(twopi*arg)
1220 ELSE
1221 coskl = fsign*cos(twopi*arg)
1222 sinkl = fsign*fsym*sin(twopi*arg)
1223 END IF
1224
1225 CALL dbcsr_get_block_p(matrix=rsmat(ispin, ic)%matrix, row=irow, col=icol, &
1226 block=rsblock, found=found)
1227 IF (.NOT. found) cycle
1228
1229 IF (wfn_real_only) THEN
1230 CALL dbcsr_get_block_p(matrix=rmatrix, row=irow, col=icol, &
1231 block=rblock, found=found)
1232 IF (.NOT. found) cycle
1233 rblock = rblock + coskl*rsblock
1234 ELSE
1235 CALL dbcsr_get_block_p(matrix=rmatrix, row=irow, col=icol, &
1236 block=rblock, found=found)
1237 IF (.NOT. found) cycle
1238 CALL dbcsr_get_block_p(matrix=cmatrix, row=irow, col=icol, &
1239 block=cblock, found=found)
1240 IF (.NOT. found) cycle
1241 rblock = rblock + coskl*rsblock
1242 cblock = cblock + sinkl*rsblock
1243 END IF
1244
1245 END DO
1246 CALL neighbor_list_iterator_release(nl_iterator)
1247
1248 CALL timestop(handle)
1249
1250 END SUBROUTINE rskp_transform
1251
1252! **************************************************************************************************
1253!> \brief Prepare a batched real-cell transform to a regular grid or subset for local DBCSR blocks.
1254!> The stored blocks remain MPI local. Irregular or too-large grids return used_fft=.FALSE.
1255!> \param grid cached transformed local blocks and their DBCSR coordinates
1256!> \param rmatrix allocated output template defining the local block layout
1257!> \param rsmat real-space matrix set
1258!> \param ispin spin component of rsmat
1259!> \param xkp requested points on a regular reciprocal grid in arbitrary order
1260!> \param nkp_grid reciprocal grid dimensions
1261!> \param cell_to_index real-cell coordinate mapping
1262!> \param sab_nl neighbor list defining the stored block orientation
1263!> \param used_fft whether the cache was prepared
1264!> \param is_complex whether the real-space operator is imaginary
1265!> \param rs_sign optional overall sign
1266!> \param max_storage_bytes optional conservative per-rank memory limit
1267! **************************************************************************************************
1268 SUBROUTINE rskp_transform_grid_prepare(grid, rmatrix, rsmat, ispin, xkp, nkp_grid, &
1269 cell_to_index, sab_nl, used_fft, is_complex, rs_sign, &
1270 max_storage_bytes)
1271
1272 TYPE(rskp_grid_type), INTENT(INOUT) :: grid
1273 TYPE(dbcsr_type), INTENT(INOUT) :: rmatrix
1274 TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: rsmat
1275 INTEGER, INTENT(IN) :: ispin
1276 REAL(kind=dp), DIMENSION(:, :), INTENT(IN) :: xkp
1277 INTEGER, DIMENSION(3), INTENT(IN) :: nkp_grid
1278 INTEGER, DIMENSION(:, :, :), POINTER :: cell_to_index
1279 TYPE(neighbor_list_set_p_type), DIMENSION(:), &
1280 POINTER :: sab_nl
1281 LOGICAL, INTENT(OUT) :: used_fft
1282 LOGICAL, INTENT(IN), OPTIONAL :: is_complex
1283 REAL(kind=dp), INTENT(IN), OPTIONAL :: rs_sign
1284 INTEGER(KIND=int_8), INTENT(IN), OPTIONAL :: max_storage_bytes
1285
1286 CHARACTER(LEN=*), PARAMETER :: routinen = 'rskp_transform_grid_prepare'
1287
1288 INTEGER :: handle, i1, i2, i3, iatom, iblock, ic, &
1289 icol, irow, jatom, nblkcols, nblkrows, &
1290 nblocks, ncell, nelem, nimg, nkp, &
1291 nvalues
1292 INTEGER(KIND=int_8) :: memory_limit, storage_bytes
1293 INTEGER, ALLOCATABLE, DIMENSION(:, :) :: block_map, index_to_cell
1294 INTEGER, ALLOCATABLE, DIMENSION(:, :, :) :: fft_cell_to_index
1295 INTEGER, DIMENSION(3) :: cell, fft_cell, nfft
1296 LOGICAL :: do_symmetric, found, my_complex
1297 REAL(kind=dp) :: fsign, fsym, value_sign
1298 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :, :) :: values_rs
1299 REAL(kind=dp), DIMENSION(:, :), POINTER :: rblock, rsblock
1300 TYPE(dbcsr_iterator_type) :: iter
1302 DIMENSION(:), POINTER :: nl_iterator
1303
1304 CALL timeset(routinen, handle)
1306 used_fft = .false.
1307
1308 nkp = SIZE(xkp, 2)
1309 nimg = SIZE(rsmat, 2)
1310 memory_limit = 512_int_8*1024_int_8**2
1311 IF (PRESENT(max_storage_bytes)) memory_limit = max_storage_bytes
1312 IF (nkp < 1 .OR. any(nkp_grid <= 0)) THEN
1313 CALL timestop(handle)
1314 RETURN
1315 END IF
1316 ! A reduced request still needs scratch for the full FFT mesh.
1317 IF (32_int_8*product(int(nkp_grid, int_8)) > memory_limit) THEN
1318 CALL timestop(handle)
1319 RETURN
1320 END IF
1321 IF (.NOT. lattice_fft_shape(nkp_grid, nfft, allow_arbitrary=.true.)) THEN
1322 CALL timestop(handle)
1323 RETURN
1324 END IF
1325 IF (32_int_8*product(int(nfft, int_8)) > memory_limit) THEN
1326 CALL timestop(handle)
1327 RETURN
1328 END IF
1329 IF (.NOT. regular_kpoint_grid(xkp, nkp_grid, allow_incomplete=.true.)) THEN
1330 CALL timestop(handle)
1331 RETURN
1332 END IF
1333
1334 nblocks = 0
1335 nvalues = 0
1336 CALL dbcsr_iterator_start(iter, rmatrix)
1337 DO WHILE (dbcsr_iterator_blocks_left(iter))
1338 CALL dbcsr_iterator_next_block(iter, irow, icol, rblock)
1339 nblocks = nblocks + 1
1340 nvalues = nvalues + SIZE(rblock)
1341 END DO
1342 CALL dbcsr_iterator_stop(iter)
1343 IF (nblocks == 0 .OR. nvalues == 0) THEN
1344 CALL timestop(handle)
1345 RETURN
1346 END IF
1347
1348 CALL dbcsr_get_info(rmatrix, nblkrows_total=nblkrows, nblkcols_total=nblkcols)
1349 ALLOCATE (block_map(nblkrows, nblkcols), source=0)
1350 ALLOCATE (grid%block_row(nblocks), grid%block_col(nblocks), &
1351 grid%block_offset(nblocks), grid%block_nelem(nblocks))
1352 iblock = 0
1353 nelem = 0
1354 CALL dbcsr_iterator_start(iter, rmatrix)
1355 DO WHILE (dbcsr_iterator_blocks_left(iter))
1356 CALL dbcsr_iterator_next_block(iter, irow, icol, rblock)
1357 iblock = iblock + 1
1358 grid%block_row(iblock) = irow
1359 grid%block_col(iblock) = icol
1360 grid%block_offset(iblock) = nelem + 1
1361 grid%block_nelem(iblock) = SIZE(rblock)
1362 block_map(irow, icol) = iblock
1363 nelem = nelem + SIZE(rblock)
1364 END DO
1365 CALL dbcsr_iterator_stop(iter)
1366
1367 ALLOCATE (fft_cell_to_index(lbound(cell_to_index, 1):ubound(cell_to_index, 1), &
1368 lbound(cell_to_index, 2):ubound(cell_to_index, 2), &
1369 lbound(cell_to_index, 3):ubound(cell_to_index, 3)), source=0)
1370 ncell = 0
1371 DO i3 = lbound(cell_to_index, 3), ubound(cell_to_index, 3)
1372 DO i2 = lbound(cell_to_index, 2), ubound(cell_to_index, 2)
1373 DO i1 = lbound(cell_to_index, 1), ubound(cell_to_index, 1)
1374 IF (cell_to_index(i1, i2, i3) < 1 .OR. cell_to_index(i1, i2, i3) > nimg) cycle
1375 IF (fft_cell_to_index(i1, i2, i3) == 0) THEN
1376 ncell = ncell + 1
1377 fft_cell_to_index(i1, i2, i3) = ncell
1378 END IF
1379 IF (fft_cell_to_index(-i1, -i2, -i3) == 0) THEN
1380 ncell = ncell + 1
1381 fft_cell_to_index(-i1, -i2, -i3) = ncell
1382 END IF
1383 END DO
1384 END DO
1385 END DO
1386 IF (ncell == 0) THEN
1388 CALL timestop(handle)
1389 RETURN
1390 END IF
1391
1392 ! Bound the requested output separately from the full-mesh FFT scratch/maps.
1393 storage_bytes = int(nvalues, int_8)*(8_int_8*int(ncell, int_8) + 16_int_8*int(nkp, int_8)) + &
1394 48_int_8*product(int(nfft, int_8))*lattice_fft_num_threads(nfft, nvalues) + &
1395 4_int_8*product(int(nkp_grid, int_8)) + &
1396 12_int_8*nkp + 28_int_8*ncell
1397 IF (storage_bytes > memory_limit) THEN
1399 CALL timestop(handle)
1400 RETURN
1401 END IF
1402
1403 ALLOCATE (index_to_cell(3, ncell), source=0)
1404 DO i3 = lbound(fft_cell_to_index, 3), ubound(fft_cell_to_index, 3)
1405 DO i2 = lbound(fft_cell_to_index, 2), ubound(fft_cell_to_index, 2)
1406 DO i1 = lbound(fft_cell_to_index, 1), ubound(fft_cell_to_index, 1)
1407 ic = fft_cell_to_index(i1, i2, i3)
1408 IF (ic > 0) index_to_cell(:, ic) = [i1, i2, i3]
1409 END DO
1410 END DO
1411 END DO
1412
1413 ALLOCATE (values_rs(nvalues, 1, ncell), source=0.0_dp)
1414 ALLOCATE (grid%values(nvalues, 1, nkp))
1415 my_complex = .false.
1416 IF (PRESENT(is_complex)) my_complex = is_complex
1417 fsign = 1.0_dp
1418 IF (PRESENT(rs_sign)) fsign = rs_sign
1419
1420 CALL get_neighbor_list_set_p(neighbor_list_sets=sab_nl, symmetric=do_symmetric)
1421 CALL neighbor_list_iterator_create(nl_iterator, sab_nl)
1422 DO WHILE (neighbor_list_iterate(nl_iterator) == 0)
1423 CALL get_iterator_info(nl_iterator, iatom=iatom, jatom=jatom, cell=cell)
1424
1425 fsym = 1.0_dp
1426 irow = iatom
1427 icol = jatom
1428 IF (do_symmetric .AND. iatom > jatom) THEN
1429 irow = jatom
1430 icol = iatom
1431 fsym = -1.0_dp
1432 END IF
1433 IF (irow < 1 .OR. irow > nblkrows .OR. icol < 1 .OR. icol > nblkcols) cycle
1434 iblock = block_map(irow, icol)
1435 IF (iblock == 0) cycle
1436
1437 ic = cell_to_index(cell(1), cell(2), cell(3))
1438 IF (ic < 1 .OR. ic > nimg) cycle
1439 CALL dbcsr_get_block_p(matrix=rsmat(ispin, ic)%matrix, row=irow, col=icol, &
1440 block=rsblock, found=found)
1441 IF (.NOT. found) cycle
1442
1443 fft_cell = cell
1444 value_sign = fsign
1445 IF (fsym < 0.0_dp) fft_cell = -cell
1446 IF (my_complex) value_sign = value_sign*fsym
1447 ic = fft_cell_to_index(fft_cell(1), fft_cell(2), fft_cell(3))
1448 nelem = grid%block_nelem(iblock)
1449 i1 = grid%block_offset(iblock)
1450 values_rs(i1:i1 + nelem - 1, 1, ic) = values_rs(i1:i1 + nelem - 1, 1, ic) + &
1451 value_sign*reshape(rsblock, [nelem])
1452 END DO
1453 CALL neighbor_list_iterator_release(nl_iterator)
1454
1455 CALL cell_to_k_grid_fft(values_rs, index_to_cell, xkp, nkp_grid, grid%values, used_fft)
1456 IF (used_fft) THEN
1457 grid%ready = .true.
1458 ELSE
1460 END IF
1461
1462 CALL timestop(handle)
1463
1464 END SUBROUTINE rskp_transform_grid_prepare
1465
1466! **************************************************************************************************
1467!> \brief Extract one reciprocal-grid matrix from a prepared local DBCSR block cache.
1468!> \param grid ...
1469!> \param ikp ...
1470!> \param rmatrix ...
1471!> \param cmatrix ...
1472! **************************************************************************************************
1473 SUBROUTINE rskp_transform_grid_extract(grid, ikp, rmatrix, cmatrix)
1474
1475 TYPE(rskp_grid_type), INTENT(IN) :: grid
1476 INTEGER, INTENT(IN) :: ikp
1477 TYPE(dbcsr_type), INTENT(INOUT) :: rmatrix
1478 TYPE(dbcsr_type), INTENT(INOUT), OPTIONAL :: cmatrix
1479
1480 INTEGER :: iblock, ioffset, nelem
1481 LOGICAL :: found
1482 REAL(kind=dp), DIMENSION(:, :), POINTER :: cblock, rblock
1483
1484 cpassert(grid%ready)
1485 cpassert(ikp >= 1 .AND. ikp <= SIZE(grid%values, 3))
1486 CALL dbcsr_set(rmatrix, 0.0_dp)
1487 IF (PRESENT(cmatrix)) CALL dbcsr_set(cmatrix, 0.0_dp)
1488
1489 DO iblock = 1, SIZE(grid%block_row)
1490 ioffset = grid%block_offset(iblock)
1491 nelem = grid%block_nelem(iblock)
1492 CALL dbcsr_get_block_p(rmatrix, grid%block_row(iblock), grid%block_col(iblock), &
1493 rblock, found=found)
1494 cpassert(found)
1495 rblock = reshape(real(grid%values(ioffset:ioffset + nelem - 1, 1, ikp), kind=dp), &
1496 shape(rblock))
1497 IF (PRESENT(cmatrix)) THEN
1498 CALL dbcsr_get_block_p(cmatrix, grid%block_row(iblock), grid%block_col(iblock), &
1499 cblock, found=found)
1500 cpassert(found)
1501 cblock = reshape(aimag(grid%values(ioffset:ioffset + nelem - 1, 1, ikp)), shape(cblock))
1502 END IF
1503 END DO
1504
1505 END SUBROUTINE rskp_transform_grid_extract
1506
1507! **************************************************************************************************
1508!> \brief Release a reciprocal-grid transformation cache.
1509!> \param grid ...
1510! **************************************************************************************************
1512
1513 TYPE(rskp_grid_type), INTENT(INOUT) :: grid
1514
1515 IF (ALLOCATED(grid%values)) DEALLOCATE (grid%values)
1516 IF (ALLOCATED(grid%block_row)) DEALLOCATE (grid%block_row)
1517 IF (ALLOCATED(grid%block_col)) DEALLOCATE (grid%block_col)
1518 IF (ALLOCATED(grid%block_offset)) DEALLOCATE (grid%block_offset)
1519 IF (ALLOCATED(grid%block_nelem)) DEALLOCATE (grid%block_nelem)
1520 grid%ready = .false.
1521
1522 END SUBROUTINE rskp_transform_grid_release
1523
1524! **************************************************************************************************
1525!> \brief Given the eigenvalues of all kpoints, calculates the occupation numbers
1526!> \param kpoint Kpoint environment
1527!> \param smear Smearing information
1528!> \param probe ...
1529!> \param added_mos_auto ...
1530!> \param added_mos_auto_grow ...
1531!> \param separate_spin_occupations enforce the electron count of each spin channel
1532! **************************************************************************************************
1534 kpoint, smear, probe, added_mos_auto, added_mos_auto_grow, separate_spin_occupations)
1535
1536 TYPE(kpoint_type), POINTER :: kpoint
1537 TYPE(smear_type) :: smear
1538 TYPE(hairy_probes_type), DIMENSION(:), OPTIONAL, &
1539 POINTER :: probe
1540 LOGICAL, DIMENSION(:), INTENT(IN), OPTIONAL :: added_mos_auto
1541 LOGICAL, INTENT(OUT), OPTIONAL :: added_mos_auto_grow
1542 LOGICAL, INTENT(IN), OPTIONAL :: separate_spin_occupations
1543
1544 CHARACTER(LEN=*), PARAMETER :: routinen = 'kpoint_set_mo_occupation'
1545
1546 COMPLEX(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: csmatrix
1547 INTEGER :: handle, ik, ikpgr, ispin, kplocal, nao, &
1548 nb, ncol_global, ne_a, ne_b, &
1549 nelectron, nkp, nmo, nrow_global, nspin
1550 INTEGER, DIMENSION(2) :: kp_range
1551 LOGICAL :: my_added_mos_auto_grow, &
1552 my_separate_spin_occupations
1553 REAL(kind=dp) :: kts, kts_spin(2), mu, mus(2), nel
1554 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :) :: smatrix
1555 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :, :) :: weig, wocc
1556 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :, :, :) :: icoeff, rcoeff
1557 REAL(kind=dp), DIMENSION(2) :: spin_targets
1558 REAL(kind=dp), DIMENSION(:), POINTER :: eigenvalues, occupation, wkp
1559 TYPE(cp_cfm_type), POINTER :: cmo_coeff
1560 TYPE(cp_fm_type), POINTER :: mo_coeff
1561 TYPE(kpoint_env_type), POINTER :: kp
1562 TYPE(mo_set_type), POINTER :: mo_set
1563 TYPE(mp_para_env_type), POINTER :: para_env_inter_kp
1564
1565 CALL timeset(routinen, handle)
1566
1567 my_added_mos_auto_grow = .false.
1568 IF (PRESENT(added_mos_auto_grow)) added_mos_auto_grow = .false.
1569
1570 ! first collect all the eigenvalues
1571 CALL get_kpoint_info(kpoint, nkp=nkp)
1572 kp => kpoint%kp_env(1)%kpoint_env
1573 nspin = SIZE(kp%mos)
1574 my_separate_spin_occupations = smearing_separate_spin(smear%fixed_mag_mom)
1575 IF (PRESENT(separate_spin_occupations)) THEN
1576 my_separate_spin_occupations = my_separate_spin_occupations .OR. separate_spin_occupations
1577 END IF
1578 mo_set => kp%mos(1)
1579 CALL get_mo_set(mo_set, nmo=nmo, nao=nao, nelectron=nelectron)
1580 ne_a = nelectron
1581 ne_b = 0
1582 IF (nspin == 2) THEN
1583 CALL get_mo_set(kp%mos(2), nmo=nb, nelectron=ne_b)
1584 cpassert(nmo == nb)
1585 END IF
1586 spin_targets(:) = smearing_spin_targets([ne_a, ne_b], smear%fixed_mag_mom)
1587 ALLOCATE (weig(nmo, nkp, nspin), wocc(nmo, nkp, nspin))
1588 weig = 0.0_dp
1589 wocc = 0.0_dp
1590 IF (PRESENT(probe)) THEN
1591 ALLOCATE (rcoeff(nao, nmo, nkp, nspin), icoeff(nao, nmo, nkp, nspin))
1592 rcoeff = 0.0_dp !coeff, real part
1593 icoeff = 0.0_dp !coeff, imaginary part
1594 END IF
1595 CALL get_kpoint_info(kpoint, kp_range=kp_range)
1596 kplocal = kp_range(2) - kp_range(1) + 1
1597 DO ikpgr = 1, kplocal
1598 ik = kp_range(1) + ikpgr - 1
1599 kp => kpoint%kp_env(ikpgr)%kpoint_env
1600 DO ispin = 1, nspin
1601 mo_set => kp%mos(ispin)
1602 CALL get_mo_set(mo_set, eigenvalues=eigenvalues)
1603 weig(1:nmo, ik, ispin) = eigenvalues(1:nmo)
1604 IF (PRESENT(probe)) THEN
1605 IF (kpoint%use_real_wfn) THEN
1606 CALL get_mo_set(mo_set, mo_coeff=mo_coeff)
1607 CALL cp_fm_get_info(mo_coeff, nrow_global=nrow_global, ncol_global=ncol_global)
1608 ALLOCATE (smatrix(nrow_global, ncol_global))
1609 CALL cp_fm_get_submatrix(mo_coeff, smatrix)
1610 rcoeff(1:nao, 1:nmo, ik, ispin) = smatrix(1:nao, 1:nmo)
1611 DEALLOCATE (smatrix)
1612 ELSE
1613 CALL get_mo_set(mo_set, cmo_coeff=cmo_coeff)
1614 CALL cp_cfm_get_info(cmo_coeff, nrow_global=nrow_global, ncol_global=ncol_global)
1615 ALLOCATE (csmatrix(nrow_global, ncol_global))
1616 CALL cp_cfm_get_submatrix(cmo_coeff, csmatrix)
1617 rcoeff(1:nao, 1:nmo, ik, ispin) = real(csmatrix(1:nao, 1:nmo), kind=dp)
1618 icoeff(1:nao, 1:nmo, ik, ispin) = aimag(csmatrix(1:nao, 1:nmo))
1619 DEALLOCATE (csmatrix)
1620 END IF
1621 END IF
1622 END DO
1623 END DO
1624 CALL get_kpoint_info(kpoint, para_env_inter_kp=para_env_inter_kp)
1625 CALL para_env_inter_kp%sum(weig)
1626
1627 IF (PRESENT(probe)) THEN
1628 CALL para_env_inter_kp%sum(rcoeff)
1629 CALL para_env_inter_kp%sum(icoeff)
1630 END IF
1631
1632 CALL get_kpoint_info(kpoint, wkp=wkp)
1633 kts_spin = 0.0_dp
1634
1635!calling of HP module HERE, before smear
1636 IF (PRESENT(probe)) THEN
1637 smear%do_smear = .false. !ensures smearing is switched off
1638
1639 IF (nspin == 1) THEN
1640 nel = real(nelectron, kind=dp)
1641 CALL probe_occupancy_kp(wocc(:, :, :), mus(1), kts, weig(:, :, :), rcoeff(:, :, :, :), icoeff(:, :, :, :), 2.0d0, &
1642 probe, nel, wkp)
1643 ELSE
1644 nel = real(ne_a, kind=dp) + real(ne_b, kind=dp)
1645 CALL probe_occupancy_kp(wocc(:, :, :), mu, kts, weig(:, :, :), rcoeff(:, :, :, :), icoeff(:, :, :, :), 1.0d0, &
1646 probe, nel, wkp)
1647 kts = kts/2._dp
1648 mus(1:2) = mu
1649 END IF
1650
1651 DO ikpgr = 1, kplocal
1652 ik = kp_range(1) + ikpgr - 1
1653 kp => kpoint%kp_env(ikpgr)%kpoint_env
1654 DO ispin = 1, nspin
1655 mo_set => kp%mos(ispin)
1656 CALL get_mo_set(mo_set, eigenvalues=eigenvalues, occupation_numbers=occupation)
1657 eigenvalues(1:nmo) = weig(1:nmo, ik, ispin)
1658 occupation(1:nmo) = wocc(1:nmo, ik, ispin)
1659 mo_set%kTS = kts
1660 mo_set%mu = mus(ispin)
1661
1662 END DO
1663 END DO
1664
1665 DEALLOCATE (weig, wocc, rcoeff, icoeff)
1666
1667 END IF
1668
1669 IF (PRESENT(probe) .EQV. .false.) THEN
1670 IF (smear%do_smear) THEN
1671 SELECT CASE (smear%method)
1672 CASE (smear_fermi_dirac)
1673 ! finite electronic temperature
1674 IF (nspin == 1) THEN
1675 nel = real(nelectron, kind=dp)
1676 CALL smearkp(wocc(:, :, 1), mus(1), kts, weig(:, :, 1), nel, wkp, &
1677 smear%electronic_temperature, 2.0_dp, smear_fermi_dirac)
1678 kts_spin(1) = kts
1679 ELSE IF (my_separate_spin_occupations) THEN
1680 nel = spin_targets(1)
1681 CALL smearkp(wocc(:, :, 1), mus(1), kts, weig(:, :, 1), nel, wkp, &
1682 smear%electronic_temperature, 1.0_dp, smear_fermi_dirac)
1683 kts_spin(1) = kts
1684 nel = spin_targets(2)
1685 CALL smearkp(wocc(:, :, 2), mus(2), kts, weig(:, :, 2), nel, wkp, &
1686 smear%electronic_temperature, 1.0_dp, smear_fermi_dirac)
1687 kts_spin(2) = kts
1688 ELSE
1689 nel = real(ne_a, kind=dp) + real(ne_b, kind=dp)
1690 CALL smearkp2(wocc(:, :, :), mu, kts, weig(:, :, :), nel, wkp, &
1691 smear%electronic_temperature, smear_fermi_dirac)
1692 kts = kts/2._dp
1693 kts_spin(1:2) = kts
1694 mus(1:2) = mu
1695 END IF
1697 IF (nspin == 1) THEN
1698 nel = real(nelectron, kind=dp)
1699 CALL smearkp(wocc(:, :, 1), mus(1), kts, weig(:, :, 1), nel, wkp, &
1700 smear%smearing_width, 2.0_dp, smear%method)
1701 kts_spin(1) = kts
1702 ELSE IF (my_separate_spin_occupations) THEN
1703 nel = spin_targets(1)
1704 CALL smearkp(wocc(:, :, 1), mus(1), kts, weig(:, :, 1), nel, wkp, &
1705 smear%smearing_width, 1.0_dp, smear%method)
1706 kts_spin(1) = kts
1707 nel = spin_targets(2)
1708 CALL smearkp(wocc(:, :, 2), mus(2), kts, weig(:, :, 2), nel, wkp, &
1709 smear%smearing_width, 1.0_dp, smear%method)
1710 kts_spin(2) = kts
1711 ELSE
1712 nel = real(ne_a, kind=dp) + real(ne_b, kind=dp)
1713 CALL smearkp2(wocc(:, :, :), mu, kts, weig(:, :, :), nel, wkp, &
1714 smear%smearing_width, smear%method)
1715 kts = kts/2._dp
1716 kts_spin(1:2) = kts
1717 mus(1:2) = mu
1718 END IF
1719 CASE DEFAULT
1720 cpabort("kpoints: Selected smearing not (yet) supported")
1721 END SELECT
1722 ELSE
1723 ! fixed occupations (2/1)
1724 IF (nspin == 1) THEN
1725 nel = real(nelectron, kind=dp)
1726 CALL smearkp(wocc(:, :, 1), mus(1), kts, weig(:, :, 1), nel, wkp, &
1727 0.0_dp, 2.0_dp, smear_gaussian)
1728 kts_spin(1) = kts
1729 ELSE
1730 nel = real(ne_a, kind=dp)
1731 CALL smearkp(wocc(:, :, 1), mus(1), kts, weig(:, :, 1), nel, wkp, &
1732 0.0_dp, 1.0_dp, smear_gaussian)
1733 kts_spin(1) = kts
1734 nel = real(ne_b, kind=dp)
1735 CALL smearkp(wocc(:, :, 2), mus(2), kts, weig(:, :, 2), nel, wkp, &
1736 0.0_dp, 1.0_dp, smear_gaussian)
1737 kts_spin(2) = kts
1738 END IF
1739 END IF
1740 IF (smear%do_smear .AND. PRESENT(added_mos_auto)) THEN
1741 CALL kpoint_check_added_mos_auto_occupation( &
1742 wocc, wkp, smear, nspin, added_mos_auto, nao, my_added_mos_auto_grow)
1743 END IF
1744 IF (my_added_mos_auto_grow) THEN
1745 IF (PRESENT(added_mos_auto_grow)) THEN
1746 added_mos_auto_grow = .true.
1747 DEALLOCATE (weig, wocc)
1748 CALL timestop(handle)
1749 RETURN
1750 ELSE
1751 CALL cp_abort(__location__, &
1752 "K-point ADDED_MOS AUTO needs a larger virtual-space buffer, "// &
1753 "but this call path cannot grow it.")
1754 END IF
1755 END IF
1756 DO ikpgr = 1, kplocal
1757 ik = kp_range(1) + ikpgr - 1
1758 kp => kpoint%kp_env(ikpgr)%kpoint_env
1759 DO ispin = 1, nspin
1760 mo_set => kp%mos(ispin)
1761 CALL get_mo_set(mo_set, eigenvalues=eigenvalues, occupation_numbers=occupation)
1762 eigenvalues(1:nmo) = weig(1:nmo, ik, ispin)
1763 occupation(1:nmo) = wocc(1:nmo, ik, ispin)
1764 mo_set%kTS = kts_spin(ispin)
1765 mo_set%mu = mus(ispin)
1766 IF (.NOT. smear%do_smear) THEN
1767 ! Zero-width filling can give different occupied ranks at different k points.
1768 kp%mos(ispin)%homo = count(occupation(1:nmo) > 0.0_dp)
1769 END IF
1770 END DO
1771 END DO
1772
1773 DEALLOCATE (weig, wocc)
1774
1775 END IF
1776
1777 CALL timestop(handle)
1778
1779 END SUBROUTINE kpoint_set_mo_occupation
1780
1781! **************************************************************************************************
1782!> \brief summarize whether weighted k-point smearing reaches the available band edges
1783!> \param wocc ...
1784!> \param wkp ...
1785!> \param smear ...
1786!> \param nspin ...
1787!> \param has_weight ...
1788!> \param first_fractional ...
1789!> \param last_occupied ...
1790!> \param last_occupied_spin ...
1791! **************************************************************************************************
1792 SUBROUTINE kpoint_smearing_edge_status(wocc, wkp, smear, nspin, has_weight, &
1793 first_fractional, last_occupied, last_occupied_spin)
1794
1795 REAL(kind=dp), DIMENSION(:, :, :), INTENT(IN) :: wocc
1796 REAL(kind=dp), DIMENSION(:), INTENT(IN) :: wkp
1797 TYPE(smear_type), INTENT(IN) :: smear
1798 INTEGER, INTENT(IN) :: nspin
1799 LOGICAL, INTENT(OUT) :: has_weight, first_fractional, &
1800 last_occupied
1801 LOGICAL, DIMENSION(:), INTENT(OUT), OPTIONAL :: last_occupied_spin
1802
1803 INTEGER :: ik, ispin, nmo
1804 LOGICAL :: band_occupied
1805 REAL(kind=dp) :: eps_occ, maxocc, weight, weight_threshold
1806
1807 has_weight = .false.
1808 first_fractional = .false.
1809 last_occupied = .false.
1810 IF (PRESENT(last_occupied_spin)) last_occupied_spin(:) = .false.
1811 IF (.NOT. smear%do_smear) RETURN
1812
1813 nmo = SIZE(wocc, 1)
1814 IF (nmo < 1) RETURN
1815 eps_occ = max(smear%eps_fermi_dirac, 10.0_dp*epsilon(1.0_dp))
1816 weight_threshold = 10.0_dp*epsilon(1.0_dp)
1817
1818 DO ispin = 1, min(nspin, SIZE(wocc, 3))
1819 maxocc = merge(2.0_dp, 1.0_dp, nspin == 1)
1820 DO ik = 1, min(SIZE(wkp), SIZE(wocc, 2))
1821 weight = abs(wkp(ik))
1822 IF (weight <= weight_threshold) cycle
1823 has_weight = .true.
1824 first_fractional = first_fractional .OR. &
1825 abs(wocc(1, ik, ispin) - maxocc) > eps_occ
1826 band_occupied = abs(wocc(nmo, ik, ispin)) > eps_occ
1827 last_occupied = last_occupied .OR. band_occupied
1828 IF (band_occupied .AND. PRESENT(last_occupied_spin)) THEN
1829 IF (ispin <= SIZE(last_occupied_spin)) last_occupied_spin(ispin) = .true.
1830 END IF
1831 END DO
1832 END DO
1833
1834 END SUBROUTINE kpoint_smearing_edge_status
1835
1836! **************************************************************************************************
1837!> \brief request ADDED_MOS AUTO growth if weighted k-point smearing occupies the last band
1838!> \param wocc ...
1839!> \param wkp ...
1840!> \param smear ...
1841!> \param nspin ...
1842!> \param added_mos_auto ...
1843!> \param nao ...
1844!> \param added_mos_auto_grow ...
1845! **************************************************************************************************
1846 SUBROUTINE kpoint_check_added_mos_auto_occupation(wocc, wkp, smear, nspin, &
1847 added_mos_auto, nao, added_mos_auto_grow)
1848
1849 REAL(kind=dp), DIMENSION(:, :, :), INTENT(IN) :: wocc
1850 REAL(kind=dp), DIMENSION(:), INTENT(IN) :: wkp
1851 TYPE(smear_type), INTENT(IN) :: smear
1852 INTEGER, INTENT(IN) :: nspin
1853 LOGICAL, DIMENSION(:), INTENT(IN) :: added_mos_auto
1854 INTEGER, INTENT(IN) :: nao
1855 LOGICAL, INTENT(OUT) :: added_mos_auto_grow
1856
1857 INTEGER :: ispin, nmo
1858 LOGICAL :: first_fractional, has_weight, &
1859 last_occupied
1860 LOGICAL, DIMENSION(nspin) :: last_occupied_spin
1861
1862 added_mos_auto_grow = .false.
1863 IF (.NOT. any(added_mos_auto) .OR. .NOT. smear%do_smear) RETURN
1864
1865 CALL kpoint_smearing_edge_status(wocc, wkp, smear, nspin, has_weight, &
1866 first_fractional, last_occupied, &
1867 last_occupied_spin=last_occupied_spin)
1868 IF (.NOT. has_weight) RETURN
1869
1870 nmo = SIZE(wocc, 1)
1871 DO ispin = 1, min(nspin, SIZE(added_mos_auto))
1872 IF (.NOT. added_mos_auto(ispin) .OR. .NOT. last_occupied_spin(ispin)) cycle
1873 IF (nmo >= nao) THEN
1874 CALL cp_abort(__location__, &
1875 "K-point ADDED_MOS AUTO exhausted the AO basis but the highest band "// &
1876 "is still occupied. Use a larger basis or reduce the smearing width.")
1877 END IF
1878 added_mos_auto_grow = .true.
1879 RETURN
1880 END DO
1881
1882 END SUBROUTINE kpoint_check_added_mos_auto_occupation
1883
1884! **************************************************************************************************
1885!> \brief Calculate kpoint density matrices (rho(k), owned by kpoint groups)
1886!> \param kpoint kpoint environment
1887!> \param energy_weighted calculate energy weighted density matrix
1888!> \param for_aux_fit ...
1889! **************************************************************************************************
1890 SUBROUTINE kpoint_density_matrices(kpoint, energy_weighted, for_aux_fit)
1891
1892 TYPE(kpoint_type), POINTER :: kpoint
1893 LOGICAL, OPTIONAL :: energy_weighted, for_aux_fit
1894
1895 CHARACTER(LEN=*), PARAMETER :: routinen = 'kpoint_density_matrices'
1896
1897 INTEGER :: handle, ikpgr, ispin, kplocal, nao, nmo, &
1898 nspin, nworkers, thread
1899 LOGICAL :: aux_fit, local, wtype
1900 TYPE(cp_cfm_type), ALLOCATABLE :: cdensity(:), cwork(:)
1901 TYPE(cp_fm_struct_type), POINTER :: matrix_struct
1902 TYPE(cp_fm_type), ALLOCATABLE :: work(:)
1903 TYPE(kpoint_env_type), POINTER :: kp
1904 TYPE(local_gemm_ctxt_type), ALLOCATABLE :: gemm_ctx(:)
1905
1906 CALL timeset(routinen, handle)
1907 wtype = .false.
1908 IF (PRESENT(energy_weighted)) wtype = energy_weighted
1909 aux_fit = .false.
1910 IF (PRESENT(for_aux_fit)) aux_fit = for_aux_fit
1911 IF (aux_fit) THEN
1912 cpassert(ASSOCIATED(kpoint%kp_aux_env))
1913 kp => kpoint%kp_aux_env(1)%kpoint_env
1914 ELSE
1915 kp => kpoint%kp_env(1)%kpoint_env
1916 END IF
1917 CALL get_mo_set(kp%mos(1), nao=nao, nmo=nmo)
1918 IF (kpoint%use_real_wfn) THEN
1919 CALL cp_fm_get_info(kp%mos(1)%mo_coeff, matrix_struct=matrix_struct)
1920 ELSE
1921 CALL cp_cfm_get_info(kp%mos(1)%cmo_coeff, matrix_struct=matrix_struct)
1922 END IF
1923 kplocal = kpoint%kp_range(2) - kpoint%kp_range(1) + 1
1924 nspin = SIZE(kp%mos)
1925 ! Only the group layout matters. Distributed GEMM stays on the calling thread.
1926 local = product(matrix_struct%context%num_pe) == 1
1927 nworkers = 1
1928!$ IF (local) nworkers = MIN(omp_get_max_threads(), kplocal*nspin)
1929 ALLOCATE (gemm_ctx(nworkers))
1930 IF (kpoint%use_real_wfn) THEN
1931 ALLOCATE (work(nworkers))
1932 ELSE
1933 ALLOCATE (cwork(nworkers), cdensity(nworkers))
1934 END IF
1935 DO thread = 1, nworkers
1936 IF (kpoint%use_real_wfn) THEN
1937 CALL cp_fm_create(work(thread), matrix_struct)
1938 ELSE
1939 CALL cp_cfm_create(cwork(thread), matrix_struct)
1940 CALL cp_cfm_create(cdensity(thread), kp%pmat(1, 1)%matrix_struct)
1941 END IF
1942 IF (local) CALL gemm_ctx(thread)%create(local_gemm_pu_gpu, timing=.false.)
1943 END DO
1944
1945!$OMP PARALLEL DO COLLAPSE(2) DEFAULT(NONE) SCHEDULE(DYNAMIC, 1) NUM_THREADS(nworkers) IF(nworkers > 1) &
1946!$OMP SHARED(kpoint,wtype,aux_fit,nao,nmo,kplocal,nspin,nworkers,work,cwork,cdensity,local,gemm_ctx) PRIVATE(ikpgr,ispin,thread)
1947 DO ikpgr = 1, kplocal
1948 DO ispin = 1, nspin
1949 thread = 1
1950!$ thread = omp_get_thread_num() + 1
1951 IF (kpoint%use_real_wfn) THEN
1952 CALL kpoint_density_matrix_job(kpoint, ikpgr, ispin, wtype, aux_fit, nao, nmo, &
1953 local, gemm_ctx(thread), work=work(thread))
1954 ELSE
1955 CALL kpoint_density_matrix_job(kpoint, ikpgr, ispin, wtype, aux_fit, nao, nmo, &
1956 local, gemm_ctx(thread), cwork=cwork(thread), cdensity=cdensity(thread))
1957 END IF
1958 END DO
1959 END DO
1960!$OMP END PARALLEL DO
1961 DO thread = 1, nworkers
1962 CALL gemm_ctx(thread)%destroy()
1963 IF (kpoint%use_real_wfn) THEN
1964 CALL cp_fm_release(work(thread))
1965 ELSE
1966 CALL cp_cfm_release(cwork(thread))
1967 CALL cp_cfm_release(cdensity(thread))
1968 END IF
1969 END DO
1970 IF (.NOT. wtype .AND. .NOT. aux_fit) THEN
1971 kpoint%lowdin_density_ready = .true.
1972 kpoint%lowdin_population_ready = .false.
1973 END IF
1974 CALL timestop(handle)
1975
1976 END SUBROUTINE kpoint_density_matrices
1977
1978! **************************************************************************************************
1979!> \brief Build W(k) for noncanonical complex OT orbitals from H(k) C(k).
1980!> The occupied-space Lagrange multiplier is
1981!> Hermitian[(C^H H C) f], which reduces to the usual eigenvalue-weighted
1982!> expression for canonical orbitals.
1983!> \param coeff C(k)
1984!> \param hc H(k) C(k)
1985!> \param occupation orbital occupations
1986!> \param wmat W(k)
1987! **************************************************************************************************
1988 SUBROUTINE kpoint_ot_energy_weighted_density(coeff, hc, occupation, wmat)
1989
1990 TYPE(cp_cfm_type), INTENT(IN) :: coeff, hc
1991 REAL(kind=dp), DIMENSION(:), INTENT(IN) :: occupation
1992 TYPE(cp_cfm_type), INTENT(INOUT) :: wmat
1993
1994 COMPLEX(KIND=dp), PARAMETER :: zhalf = (0.5_dp, 0.0_dp), &
1995 zone = (1.0_dp, 0.0_dp), &
1996 zzero = (0.0_dp, 0.0_dp)
1997
1998 COMPLEX(KIND=dp), ALLOCATABLE, DIMENSION(:) :: occupation_complex
1999 INTEGER :: nao, nmo
2000 TYPE(cp_cfm_type) :: hblock, hblock_h, weighted
2001
2002 CALL cp_cfm_get_info(coeff, nrow_global=nao, ncol_global=nmo)
2003 cpassert(nmo >= 1)
2004 cpassert(SIZE(occupation) >= nmo)
2005
2006 CALL cp_cfm_create(weighted, coeff%matrix_struct)
2007 CALL cp_cfm_create(hblock, coeff%matrix_struct, nrow=nmo, ncol=nmo)
2008 CALL cp_cfm_create(hblock_h, coeff%matrix_struct, nrow=nmo, ncol=nmo)
2009 CALL parallel_gemm("C", "N", nmo, nmo, nao, zone, coeff, hc, zzero, hblock)
2010
2011 ALLOCATE (occupation_complex(nmo))
2012 occupation_complex(:) = cmplx(occupation(1:nmo), 0.0_dp, kind=dp)
2013 CALL cp_cfm_column_scale(hblock, occupation_complex)
2014 DEALLOCATE (occupation_complex)
2015 CALL cp_cfm_transpose(hblock, "C", hblock_h)
2016 CALL cp_cfm_scale_and_add(zhalf, hblock, zhalf, hblock_h)
2017
2018 CALL parallel_gemm("N", "N", nao, nmo, nmo, zone, coeff, hblock, zzero, weighted)
2019 CALL parallel_gemm("N", "C", nao, nao, nmo, zone, weighted, coeff, zzero, wmat)
2020
2021 CALL cp_cfm_release(hblock_h)
2022 CALL cp_cfm_release(hblock)
2023 CALL cp_cfm_release(weighted)
2024
2026
2027! **************************************************************************************************
2028!> \brief Common C-to-P algebra for local and distributed k-point groups.
2029!> \param kpoint host environment
2030!> \param ikpgr local k-point index
2031!> \param ispin spin index
2032!> \param wtype construct the energy-weighted density
2033!> \param aux_fit use the auxiliary MO set
2034!> \param nao number of atomic orbitals
2035!> \param nmo number of molecular orbitals
2036!> \param local use local GEMM, without communication
2037!> \param gemm_ctx private local GEMM context, unused for distributed groups
2038!> \param work real column-scaled MO scratch
2039!> \param cwork complex column-scaled MO scratch
2040!> \param cdensity complex density scratch
2041! **************************************************************************************************
2042 SUBROUTINE kpoint_density_matrix_job(kpoint, ikpgr, ispin, wtype, aux_fit, nao, nmo, &
2043 local, gemm_ctx, work, cwork, cdensity)
2044
2045 TYPE(kpoint_type), POINTER :: kpoint
2046 INTEGER, INTENT(IN) :: ikpgr, ispin
2047 LOGICAL, INTENT(IN) :: wtype, aux_fit
2048 INTEGER, INTENT(IN) :: nao, nmo
2049 LOGICAL, INTENT(IN) :: local
2050 TYPE(local_gemm_ctxt_type), INTENT(INOUT) :: gemm_ctx
2051 TYPE(cp_fm_type), INTENT(INOUT), OPTIONAL :: work
2052 TYPE(cp_cfm_type), INTENT(INOUT), OPTIONAL :: cwork, cdensity
2053
2054 COMPLEX(KIND=dp) :: weights(nmo)
2055 REAL(kind=dp), POINTER :: eigenvalues(:), occupation(:)
2056 TYPE(cp_cfm_type), POINTER :: ccoeff
2057 TYPE(cp_fm_type), POINTER :: coeff, cpmat, rpmat
2058 TYPE(kpoint_env_type), POINTER :: kp
2059
2060 IF (aux_fit) THEN
2061 kp => kpoint%kp_aux_env(ikpgr)%kpoint_env
2062 ELSE
2063 kp => kpoint%kp_env(ikpgr)%kpoint_env
2064 END IF
2065 CALL get_mo_set(kp%mos(ispin), occupation_numbers=occupation, eigenvalues=eigenvalues)
2066 IF (wtype) THEN
2067 rpmat => kp%wmat(1, ispin)
2068 ELSE
2069 rpmat => kp%pmat(1, ispin)
2070 END IF
2071 IF (kpoint%use_real_wfn) THEN
2072 cpassert(PRESENT(work))
2073 coeff => kp%mos(ispin)%mo_coeff
2074 CALL cp_fm_to_fm(coeff, work)
2075 CALL cp_fm_column_scale(work, occupation)
2076 IF (wtype) CALL cp_fm_column_scale(work, eigenvalues)
2077 IF (local) THEN
2078 cpassert(product(coeff%matrix_struct%context%num_pe) == 1)
2079 cpassert(product(work%matrix_struct%context%num_pe) == 1)
2080 cpassert(product(rpmat%matrix_struct%context%num_pe) == 1)
2081 CALL gemm_ctx%gemm("N", "T", nao, nao, nmo, 1.0_dp, &
2082 coeff%local_data, SIZE(coeff%local_data, 1), &
2083 work%local_data, SIZE(work%local_data, 1), 0.0_dp, &
2084 rpmat%local_data, SIZE(rpmat%local_data, 1))
2085 ELSE
2086 CALL parallel_gemm("N", "T", nao, nao, nmo, 1.0_dp, coeff, work, 0.0_dp, rpmat)
2087 END IF
2088 ELSE
2089 cpassert(PRESENT(cwork) .AND. PRESENT(cdensity))
2090 IF (wtype) THEN
2091 cpmat => kp%wmat(2, ispin)
2092 ELSE
2093 cpmat => kp%pmat(2, ispin)
2094 END IF
2095 ccoeff => kp%mos(ispin)%cmo_coeff
2096 weights = cmplx(occupation(1:nmo), 0.0_dp, kind=dp)
2097 IF (wtype) weights = weights*eigenvalues(1:nmo)
2098 CALL cp_cfm_to_cfm(ccoeff, cwork)
2099 CALL cp_cfm_column_scale(cwork, weights)
2100 IF (local) THEN
2101 cpassert(product(ccoeff%matrix_struct%context%num_pe) == 1)
2102 CALL gemm_ctx%gemm("N", "C", nao, nao, nmo, z_one, &
2103 ccoeff%local_data, SIZE(ccoeff%local_data, 1), &
2104 cwork%local_data, SIZE(cwork%local_data, 1), z_zero, &
2105 cdensity%local_data, SIZE(cdensity%local_data, 1))
2106 ELSE
2107 CALL parallel_gemm("N", "C", nao, nao, nmo, z_one, ccoeff, cwork, z_zero, cdensity)
2108 END IF
2109 CALL cp_cfm_to_fm(cdensity, rpmat, cpmat)
2110 END IF
2111
2112 END SUBROUTINE kpoint_density_matrix_job
2113
2114! **************************************************************************************************
2115!> \brief Calculate Lowdin transformation of density matrix S^1/2 P S^1/2
2116!> Integrate diagonal elements over k-points to get Lowdin charges
2117!> \param kpoint kpoint environment
2118!> \param pmat_diag Sum over kpoints of diagonal elements
2119!> \par History
2120!> 04.2026 created [JGH]
2121! **************************************************************************************************
2122 SUBROUTINE lowdin_kp_trans(kpoint, pmat_diag)
2123
2124 TYPE(kpoint_type), POINTER :: kpoint
2125 REAL(kind=dp), DIMENSION(:, :), INTENT(INOUT) :: pmat_diag
2126
2127 CHARACTER(LEN=*), PARAMETER :: routinen = 'lowdin_kp_trans'
2128 COMPLEX(KIND=dp), PARAMETER :: cone = (1.0_dp, 0.0_dp), &
2129 czero = (0.0_dp, 0.0_dp)
2130
2131 INTEGER :: handle, ikpgr, ispin, kplocal, nao, nspin
2132 INTEGER, DIMENSION(2) :: kp_range
2133 REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: dele
2134 TYPE(cp_cfm_type) :: cf1work, cf2work
2135 TYPE(cp_cfm_type), POINTER :: cshalf
2136 TYPE(cp_fm_struct_type), POINTER :: matrix_struct
2137 TYPE(cp_fm_type) :: f1work, f2work
2138 TYPE(cp_fm_type), POINTER :: cpmat, pmat, rpmat, shalf
2139 TYPE(kpoint_env_type), POINTER :: kp
2140 TYPE(mp_para_env_type), POINTER :: para_env_inter_kp
2141
2142 CALL timeset(routinen, handle)
2143
2144 nspin = SIZE(pmat_diag, 2)
2145 pmat_diag = 0.0_dp
2146
2147 ! work matrix
2148 CALL cp_fm_get_info(kpoint%kp_env(1)%kpoint_env%pmat(1, 1), &
2149 matrix_struct=matrix_struct, nrow_global=nao)
2150 IF (kpoint%use_real_wfn) THEN
2151 CALL cp_fm_create(f1work, matrix_struct, nrow=nao, ncol=nao)
2152 CALL cp_fm_create(f2work, matrix_struct, nrow=nao, ncol=nao)
2153 ELSE
2154 CALL cp_fm_create(f2work, matrix_struct, nrow=nao, ncol=nao)
2155 CALL cp_cfm_create(cf1work, matrix_struct, nrow=nao, ncol=nao)
2156 CALL cp_cfm_create(cf2work, matrix_struct, nrow=nao, ncol=nao)
2157 END IF
2158 ALLOCATE (dele(nao))
2159
2160 CALL get_kpoint_info(kpoint, kp_range=kp_range)
2161 kplocal = kp_range(2) - kp_range(1) + 1
2162 DO ikpgr = 1, kplocal
2163 kp => kpoint%kp_env(ikpgr)%kpoint_env
2164 DO ispin = 1, nspin
2165 IF (kpoint%use_real_wfn) THEN
2166 pmat => kp%pmat(1, ispin)
2167 shalf => kp%shalf
2168 CALL parallel_gemm("N", "N", nao, nao, nao, 1.0_dp, pmat, shalf, 0.0_dp, f1work)
2169 CALL parallel_gemm("N", "N", nao, nao, nao, 1.0_dp, shalf, f1work, 0.0_dp, f2work)
2170 ELSE
2171 rpmat => kp%pmat(1, ispin)
2172 cpmat => kp%pmat(2, ispin)
2173 cshalf => kp%cshalf
2174 CALL cp_fm_to_cfm(rpmat, cpmat, cf1work)
2175 CALL parallel_gemm("N", "N", nao, nao, nao, cone, cf1work, cshalf, czero, cf2work)
2176 CALL parallel_gemm("N", "N", nao, nao, nao, cone, cshalf, cf2work, czero, cf1work)
2177 CALL cp_cfm_to_fm(cf1work, mtargetr=f2work)
2178 END IF
2179 CALL cp_fm_get_diag(f2work, dele)
2180 pmat_diag(1:nao, ispin) = pmat_diag(1:nao, ispin) + kp%wkp*dele(1:nao)
2181 END DO
2182 END DO
2183
2184 CALL get_kpoint_info(kpoint, para_env_inter_kp=para_env_inter_kp)
2185 CALL para_env_inter_kp%sum(pmat_diag)
2186
2187 IF (kpoint%use_real_wfn) THEN
2188 CALL cp_fm_release(f1work)
2189 CALL cp_fm_release(f2work)
2190 ELSE
2191 CALL cp_fm_release(f2work)
2192 CALL cp_cfm_release(cf1work)
2193 CALL cp_cfm_release(cf2work)
2194 END IF
2195 DEALLOCATE (dele)
2196
2197 CALL timestop(handle)
2198
2199 END SUBROUTINE lowdin_kp_trans
2200
2201! **************************************************************************************************
2202!> \brief Calculate S(k)^1/2 C(k) for real or complex k-point wavefunctions
2203!> \param kp K-point environment for one local k point
2204!> \param ispin Spin index
2205!> \param use_real_wfn Use real k-point wavefunctions
2206!> \param shalfc Output matrix containing S(k)^1/2 C(k) for real wavefunctions
2207!> \param cshalfc Output matrix containing S(k)^1/2 C(k) for complex wavefunctions
2208! **************************************************************************************************
2209 SUBROUTINE lowdin_kp_mo_coeff(kp, ispin, use_real_wfn, shalfc, cshalfc)
2210
2211 TYPE(kpoint_env_type), POINTER :: kp
2212 INTEGER, INTENT(IN) :: ispin
2213 LOGICAL, INTENT(IN) :: use_real_wfn
2214 TYPE(cp_fm_type), INTENT(INOUT), OPTIONAL :: shalfc
2215 TYPE(cp_cfm_type), INTENT(INOUT), OPTIONAL :: cshalfc
2216
2217 INTEGER :: nao, nmo
2218 TYPE(mo_set_type), POINTER :: mo_set
2219
2220 IF (use_real_wfn) THEN
2221 cpassert(PRESENT(shalfc))
2222 mo_set => kp%mos(ispin)
2223 CALL get_mo_set(mo_set, nao=nao, nmo=nmo)
2224
2225 CALL parallel_gemm("N", "N", nao, nmo, nao, 1.0_dp, kp%shalf, &
2226 mo_set%mo_coeff, 0.0_dp, shalfc)
2227 ELSE
2228 cpassert(PRESENT(cshalfc))
2229 mo_set => kp%mos(ispin)
2230 CALL get_mo_set(mo_set, nao=nao, nmo=nmo)
2231 CALL parallel_gemm("N", "N", nao, nmo, nao, z_one, kp%cshalf, &
2232 mo_set%cmo_coeff, z_zero, cshalfc)
2233 END IF
2234
2235 END SUBROUTINE lowdin_kp_mo_coeff
2236
2237! **************************************************************************************************
2238!> \brief generate real space density matrices in DBCSR format
2239!> \param kpoint Kpoint environment
2240!> \param denmat Real space (DBCSR) density matrices
2241!> \param wtype True = energy weighted density matrix
2242!> False = normal density matrix
2243!> \param tempmat DBCSR matrix to be used as template
2244!> \param sab_nl ...
2245!> \param fmwork FM work matrices (kpoint group)
2246!> \param for_aux_fit ...
2247!> \param pmat_ext ...
2248!> \param overlap_rs ...
2249!> \param mixed_aux_orb ...
2250! **************************************************************************************************
2251 SUBROUTINE kpoint_density_transform(kpoint, denmat, wtype, tempmat, sab_nl, fmwork, for_aux_fit, &
2252 pmat_ext, overlap_rs, mixed_aux_orb)
2253
2254 TYPE(kpoint_type), POINTER :: kpoint
2255 TYPE(dbcsr_p_type), DIMENSION(:, :) :: denmat
2256 LOGICAL, INTENT(IN) :: wtype
2257 TYPE(dbcsr_type), POINTER :: tempmat
2258 TYPE(neighbor_list_set_p_type), DIMENSION(:), &
2259 POINTER :: sab_nl
2260 TYPE(cp_fm_type), DIMENSION(:), INTENT(IN), TARGET :: fmwork
2261 LOGICAL, OPTIONAL :: for_aux_fit
2262 TYPE(cp_fm_type), DIMENSION(:, :, :), INTENT(IN), &
2263 OPTIONAL, TARGET :: pmat_ext
2264 TYPE(dbcsr_p_type), DIMENSION(:, :), OPTIONAL, &
2265 POINTER :: overlap_rs
2266 LOGICAL, OPTIONAL :: mixed_aux_orb
2267
2268 CHARACTER(LEN=*), PARAMETER :: routinen = 'kpoint_density_transform'
2269
2270 INTEGER :: handle, ic, ik, ikp, ispin, kplocal, nc, &
2271 nimg, nkp, nspin
2272 INTEGER, DIMENSION(:, :, :), POINTER :: cell_to_index
2273 LOGICAL :: aux_fit, do_symmetric, local, &
2274 mixed_aux_orb_local, &
2275 regular_grid_done, &
2276 regular_grid_eligible
2277 TYPE(cp_fm_p_type), ALLOCATABLE, &
2278 DIMENSION(:, :, :) :: source
2279 TYPE(dbcsr_p_type), DIMENSION(2) :: components
2280 TYPE(dbcsr_type), POINTER :: cpmat, rpmat
2281 TYPE(kp_transform_plan_type) :: transform_plan
2282 TYPE(kpoint_env_type), POINTER :: kp
2283 TYPE(mp_para_env_type), POINTER :: para_env
2284
2285 CALL timeset(routinen, handle)
2286
2287 CALL get_neighbor_list_set_p(neighbor_list_sets=sab_nl, symmetric=do_symmetric)
2288
2289 IF (PRESENT(for_aux_fit)) THEN
2290 aux_fit = for_aux_fit
2291 ELSE
2292 aux_fit = .false.
2293 END IF
2294 IF (PRESENT(mixed_aux_orb)) THEN
2295 mixed_aux_orb_local = mixed_aux_orb
2296 ELSE
2297 mixed_aux_orb_local = .false.
2298 END IF
2299
2300 IF (aux_fit) THEN
2301 cpassert(ASSOCIATED(kpoint%kp_aux_env))
2302 END IF
2303
2304 ! work storage
2305 ALLOCATE (rpmat)
2306 CALL dbcsr_create(rpmat, template=tempmat, &
2307 matrix_type=merge(dbcsr_type_symmetric, dbcsr_type_no_symmetry, do_symmetric))
2308 CALL cp_dbcsr_alloc_block_from_nbl(rpmat, sab_nl)
2309 CALL dbcsr_set(rpmat, 0.0_dp)
2310 NULLIFY (cpmat)
2311 components(1)%matrix => rpmat
2312
2313 CALL get_kpoint_info(kpoint, nkp=nkp, cell_to_index=cell_to_index)
2314 IF (PRESENT(overlap_rs)) THEN
2315 CALL calibrate_symmetry_phases(kpoint, overlap_rs, tempmat, sab_nl, cell_to_index, &
2316 for_aux_fit=aux_fit, mixed_aux_orb=mixed_aux_orb_local)
2317 END IF
2318 ! initialize real space density matrices
2319 IF (aux_fit) THEN
2320 kp => kpoint%kp_aux_env(1)%kpoint_env
2321 ELSE
2322 kp => kpoint%kp_env(1)%kpoint_env
2323 END IF
2324 nspin = SIZE(kp%mos)
2325 nc = SIZE(kp%pmat, 1)
2326 nimg = SIZE(denmat, 2)
2327 CALL kp_transform_plan_create(transform_plan, sab_nl, cell_to_index, nimg, &
2328 group_entries=.true.)
2329
2330 ! Borrow one common view for ordinary P, W, auxiliary and external matrices.
2331 ! Pointers remain within this call, including the TARGET lifetime of pmat_ext.
2332 kplocal = kpoint%kp_range(2) - kpoint%kp_range(1) + 1
2333 ALLOCATE (source(kplocal, nc, nspin))
2334 DO ispin = 1, nspin
2335 DO ikp = 1, kplocal
2336 IF (aux_fit) THEN
2337 kp => kpoint%kp_aux_env(ikp)%kpoint_env
2338 ELSE
2339 kp => kpoint%kp_env(ikp)%kpoint_env
2340 END IF
2341 DO ic = 1, nc
2342 IF (PRESENT(pmat_ext)) THEN
2343 source(ikp, ic, ispin)%matrix => pmat_ext(ikp, ic, ispin)
2344 ELSE IF (wtype) THEN
2345 source(ikp, ic, ispin)%matrix => kp%wmat(ic, ispin)
2346 ELSE
2347 source(ikp, ic, ispin)%matrix => kp%pmat(ic, ispin)
2348 END IF
2349 END DO
2350 END DO
2351 END DO
2352
2353 para_env => kpoint%blacs_env_all%para_env
2354 local = para_env%num_pe == 1
2355 IF (local) THEN
2356 cpassert(kpoint%kp_range(1) == 1 .AND. kplocal == nkp)
2357 END IF
2358 ! The regular-grid route is an optional specialization without spatial rotations.
2359 ! The batched block transform handles all remaining cases.
2360 regular_grid_eligible = nimg < nkp
2361 IF (ASSOCIATED(kpoint%index_to_cell)) THEN
2362 regular_grid_eligible = regular_grid_eligible .AND. nimg <= SIZE(kpoint%index_to_cell, 2)
2363 ELSE
2364 regular_grid_eligible = .false.
2365 END IF
2366 IF (regular_grid_eligible) THEN
2367 DO ik = 1, nkp
2368 IF (kpoint%kp_sym(ik)%kpoint_sym%apply_symmetry) THEN
2369 regular_grid_eligible = .false.
2370 EXIT
2371 END IF
2372 END DO
2373 END IF
2374
2375 regular_grid_done = .false.
2376 IF (regular_grid_eligible) THEN
2377 ALLOCATE (cpmat)
2378 CALL dbcsr_create(cpmat, template=rpmat, &
2379 matrix_type=merge(dbcsr_type_antisymmetric, dbcsr_type_no_symmetry, do_symmetric))
2380 CALL cp_dbcsr_alloc_block_from_nbl(cpmat, sab_nl)
2381 CALL dbcsr_set(cpmat, 0.0_dp)
2382 components(2)%matrix => cpmat
2383 CALL kpoint_density_transform_regular_grid(kpoint, source, denmat, components, fmwork, &
2384 transform_plan, regular_grid_done)
2385 END IF
2386 IF (.NOT. regular_grid_done) THEN
2387 CALL kpoint_density_transform_batched(kpoint, source, denmat, rpmat, transform_plan, &
2388 for_aux_fit=aux_fit, mixed_aux_orb=mixed_aux_orb_local)
2389 END IF
2390
2391 CALL dbcsr_deallocate_matrix(rpmat)
2392 IF (ASSOCIATED(cpmat)) CALL dbcsr_deallocate_matrix(cpmat)
2393
2394 CALL timestop(handle)
2395
2396 END SUBROUTINE kpoint_density_transform
2397
2398! **************************************************************************************************
2399!> \brief Contract group-local densities into bounded batches of real-space AO blocks.
2400!> Small tiles of images sharing an AO block are independent OpenMP work items. Rotations follow the
2401!> k-point contraction; only the resulting blocks are reduced to their DBCSR
2402!> owner. This also works when a source AO block spans several MPI ranks.
2403!> \param kpoint ...
2404!> \param source borrowed P, W or external density matrices
2405!> \param denmat real-space output matrices
2406!> \param template union of the source AO sparsities
2407!> \param plan neighbor-list traversal and storage conventions
2408!> \param for_aux_fit ...
2409!> \param mixed_aux_orb ...
2410! **************************************************************************************************
2411 SUBROUTINE kpoint_density_transform_batched(kpoint, source, denmat, template, plan, for_aux_fit, &
2412 mixed_aux_orb)
2413 TYPE(kpoint_type), POINTER :: kpoint
2414 TYPE(cp_fm_p_type), DIMENSION(:, :, :), INTENT(IN) :: source
2415 TYPE(dbcsr_p_type), DIMENSION(:, :) :: denmat
2416 TYPE(dbcsr_type), POINTER :: template
2417 TYPE(kp_transform_plan_type), INTENT(IN) :: plan
2418 LOGICAL, INTENT(IN), OPTIONAL :: for_aux_fit, mixed_aux_orb
2419
2420 CHARACTER(LEN=*), PARAMETER :: routinen = 'kpoint_density_transform_batched'
2421 INTEGER(KIND=int_8), PARAMETER :: max_bytes = 64_int_8*1024*1024
2422 INTEGER, PARAMETER :: images_per_tile = 8, max_batch = 256
2423
2424 INTEGER :: a, b, batch, first, handle, i, ic, igroup, ikp, image, ispin, j, last, nblock, &
2425 ncol, njob, nlocal, nrow, nthreads, ntile, nvalue, orientation, owner, slot, thread, tile
2426 INTEGER(KIND=int_8) :: block_size
2427 INTEGER, ALLOCATABLE :: available(:, :, :), counts(:), &
2428 jobs(:, :), local_jobs(:, :), &
2429 offsets(:), tile_start(:), &
2430 tiles(:, :, :, :, :)
2431 INTEGER, DIMENSION(:), POINTER :: col_offset, col_size, row_offset, &
2432 row_size
2433 LOGICAL :: found, trans
2434 REAL(kind=dp), ALLOCATABLE :: packed(:, :), phases(:, :), &
2435 rotation_work(:, :)
2436 REAL(kind=dp), ALLOCATABLE, TARGET :: sums(:, :), values(:)
2437 REAL(kind=dp), POINTER :: block(:, :)
2438 TYPE(kp_density_group_type), ALLOCATABLE :: groups(:)
2439 TYPE(local_gemm_ctxt_type), ALLOCATABLE :: gemm_ctx(:)
2440 TYPE(mp_para_env_type), POINTER :: world
2441
2442 CALL timeset(routinen, handle)
2443 world => kpoint%blacs_env_all%para_env
2444 CALL dbcsr_get_info(template, row_blk_size=row_size, col_blk_size=col_size, &
2445 row_blk_offset=row_offset, col_blk_offset=col_offset)
2446 CALL kp_density_groups_create(kpoint, SIZE(row_size), groups, for_aux_fit=for_aux_fit, &
2447 mixed_aux_orb=mixed_aux_orb)
2448 cpassert(SIZE(row_size) == SIZE(col_size))
2449 ALLOCATE (tiles(4, SIZE(row_size), SIZE(source, 1), SIZE(source, 2), SIZE(source, 3)))
2450 DO ispin = 1, SIZE(source, 3)
2451 DO ic = 1, SIZE(source, 2)
2452 DO ikp = 1, SIZE(source, 1)
2453 CALL kp_density_layout_create(source(ikp, ic, ispin)%matrix, row_size, col_size, &
2454 row_offset, col_offset, tiles(:, :, ikp, ic, ispin))
2455 END DO
2456 END DO
2457 DO image = 1, SIZE(denmat, 2)
2458 CALL dbcsr_set(denmat(ispin, image)%matrix, 0.0_dp)
2459 END DO
2460 END DO
2461
2462 ! Only owners request blocks. A group contains one (image,row,col), hence
2463 ! all its entries have the same cell; retain both multiplicity and sign.
2464 ALLOCATE (local_jobs(8, plan%ngroup), counts(0:world%num_pe - 1))
2465 nlocal = 0
2466 DO igroup = 1, plan%ngroup
2467 first = plan%group_start(igroup)
2468 last = plan%group_start(igroup + 1) - 1
2469 CALL dbcsr_get_readonly_block_p(denmat(1, plan%image(first))%matrix, &
2470 plan%row(first), plan%col(first), block, found=found)
2471 IF (.NOT. found) cycle
2472 nlocal = nlocal + 1
2473 local_jobs(:, nlocal) = [plan%row(first), plan%col(first), plan%image(first), &
2474 plan%cell(:, first), last - first + 1, &
2475 nint(sum(plan%symmetry_sign(first:last)))]
2476 END DO
2477 CALL world%allgather(nlocal, counts)
2478 IF (maxval(counts) == 0) THEN
2479 CALL timestop(handle)
2480 RETURN
2481 END IF
2482 block_size = int(maxval(row_size), int_8)*maxval(col_size)
2483 batch = max(1, min(max_batch, maxval(counts), int(max_bytes/(8_int_8*block_size))))
2484 ALLOCATE (jobs(8, batch), offsets(batch + 1), tile_start(batch + 1), values(int(block_size)*batch), &
2485 available(2, 0:ubound(groups, 1), batch))
2486 nthreads = 1
2487!$ nthreads = MIN(omp_get_max_threads(), batch)
2488 ALLOCATE (gemm_ctx(nthreads))
2489 DO thread = 1, nthreads
2490 CALL gemm_ctx(thread)%create(local_gemm_pu_host, timing=.false.)
2491 END DO
2492
2493 DO owner = 0, world%num_pe - 1
2494 DO first = 1, counts(owner), batch
2495 njob = min(batch, counts(owner) - first + 1)
2496 IF (world%mepos == owner) jobs(:, 1:njob) = local_jobs(:, first:first + njob - 1)
2497 IF (world%num_pe > 1) CALL world%bcast(jobs(:, 1:njob), owner)
2498 offsets(1) = 1
2499 DO j = 1, njob
2500 offsets(j + 1) = offsets(j) + row_size(jobs(1, j))*col_size(jobs(2, j))
2501 END DO
2502 ! Neighbor-list groups are ordered by AO pair and image. Limit image
2503 ! tiles to retain enough independent work items for small unit cells.
2504 ntile = 0
2505 i = 1
2506 DO WHILE (i <= njob)
2507 ntile = ntile + 1
2508 tile_start(ntile) = i
2509 j = i + 1
2510 DO WHILE (j <= min(njob, i + images_per_tile - 1))
2511 IF (any(jobs(1:2, j) /= jobs(1:2, i))) EXIT
2512 j = j + 1
2513 END DO
2514 i = j
2515 END DO
2516 tile_start(ntile + 1) = njob + 1
2517 available(:, :, :) = 0
2518 DO tile = 1, ntile
2519 j = tile_start(tile)
2520 DO slot = 0, ubound(groups, 1)
2521 IF (.NOT. ALLOCATED(groups(slot)%inverse)) cycle
2522 DO orientation = 1, 2
2523 CALL kp_density_source_block(groups(slot), jobs(1:2, j), plan%symmetric, &
2524 orientation, a, b, trans)
2525 IF (a == 0) cycle
2526 CALL dbcsr_get_readonly_block_p(template, a, b, block, found=found)
2527 available(orientation, slot, tile) = merge(1, 0, found)
2528 END DO
2529 END DO
2530 END DO
2531 ! Source sparsity is distributed independently of the FM layout.
2532 ! Reduce only presence flags, never the k-dependent source matrices.
2533 IF (world%num_pe > 1) CALL world%sum(available(:, :, 1:ntile))
2534 nvalue = offsets(njob + 1) - 1
2535 nthreads = min(SIZE(gemm_ctx), ntile)
2536 DO ispin = 1, SIZE(source, 3)
2537 values(1:nvalue) = 0.0_dp
2538!$OMP PARALLEL DEFAULT(NONE) NUM_THREADS(nthreads) &
2539!$OMP SHARED(source,tiles,groups,jobs,available,plan,row_size,col_size,row_offset,col_offset, &
2540!$OMP values,offsets,tile_start,ntile,ispin,gemm_ctx) &
2541!$OMP PRIVATE(tile,j,last,nrow,ncol,block,packed,phases,sums,rotation_work,thread)
2542 thread = 1
2543!$ thread = omp_get_thread_num() + 1
2544!$OMP DO SCHEDULE(DYNAMIC, 1)
2545 DO tile = 1, ntile
2546 j = tile_start(tile)
2547 last = tile_start(tile + 1) - 1
2548 nrow = row_size(jobs(1, j))
2549 ncol = col_size(jobs(2, j))
2550 block(1:nrow*ncol, 1:last - j + 1) => values(offsets(j):offsets(last + 1) - 1)
2551 CALL kp_density_contract_tile(source(:, :, ispin), tiles(:, :, :, :, ispin), groups, &
2552 jobs(:, j:last), available(:, :, tile), plan%symmetric, &
2553 row_size, col_size, row_offset, col_offset, block, &
2554 packed, phases, sums, rotation_work, gemm_ctx(thread))
2555 END DO
2556!$OMP END DO
2557!$OMP END PARALLEL
2558 IF (world%num_pe > 1) CALL world%sum(values(1:nvalue), root=owner)
2559 IF (world%mepos /= owner) cycle
2560 DO j = 1, njob
2561 CALL dbcsr_get_block_p(denmat(ispin, jobs(3, j))%matrix, jobs(1, j), jobs(2, j), block, found=found)
2562 cpassert(found)
2563 nrow = SIZE(block, 1)
2564 DO i = 1, SIZE(block, 2)
2565 nblock = offsets(j) + (i - 1)*nrow
2566 block(:, i) = values(nblock:nblock + nrow - 1)
2567 END DO
2568 END DO
2569 END DO
2570 END DO
2571 END DO
2572 DO thread = 1, SIZE(gemm_ctx)
2573 CALL gemm_ctx(thread)%destroy()
2574 END DO
2575 CALL timestop(handle)
2576 END SUBROUTINE kpoint_density_transform_batched
2577
2578! **************************************************************************************************
2579!> \brief Group the existing density contributions by operation and phase direction.
2580!> Only nwght entries contribute; additional little-group mappings are not
2581!> extra density terms. Metadata is borrowed, local k-point lists are owned.
2582!> \param kpoint ...
2583!> \param natom number of AO blocks
2584!> \param groups operation views and local contribution lists; zero is the identity
2585!> \param for_aux_fit ...
2586!> \param mixed_aux_orb ...
2587! **************************************************************************************************
2588 SUBROUTINE kp_density_groups_create(kpoint, natom, groups, for_aux_fit, mixed_aux_orb)
2589 TYPE(kpoint_type), POINTER :: kpoint
2590 INTEGER, INTENT(IN) :: natom
2591 TYPE(kp_density_group_type), ALLOCATABLE, &
2592 INTENT(OUT) :: groups(:)
2593 LOGICAL, INTENT(IN), OPTIONAL :: for_aux_fit, mixed_aux_orb
2594
2595 INTEGER :: i, ik, ikp, is, ncontrib, nrot, pass, &
2596 slot
2597 INTEGER, ALLOCATABLE :: counts(:)
2598 TYPE(kpoint_sym_type), POINTER :: kpsym
2599
2600 nrot = 0
2601 IF (ASSOCIATED(kpoint%ibrot)) nrot = SIZE(kpoint%ibrot)
2602 ALLOCATE (groups(0:4*nrot), counts(0:4*nrot))
2603 DO pass = 1, 2
2604 counts(:) = 0
2605 DO ik = 1, kpoint%nkp
2606 ikp = ik - kpoint%kp_range(1) + 1
2607 kpsym => kpoint%kp_sym(ik)%kpoint_sym
2608 ncontrib = 1
2609 IF (kpsym%apply_symmetry) ncontrib = kpsym%nwght
2610 DO is = 1, ncontrib
2611 slot = 0
2612 IF (kpsym%apply_symmetry) THEN
2613 cpassert(kpsym%phase_mode(is) >= 1 .AND. kpsym%phase_mode(is) <= 2)
2614 slot = find_kpoint_rotation_slot(kpoint, kpsym%rotp(is))
2615 cpassert(slot > 0)
2616 slot = slot + nrot*merge(1, 0, kpsym%rotp(is) < 0) + 2*nrot*(kpsym%phase_mode(is) - 1)
2617 END IF
2618 IF (pass == 1) THEN
2619 IF (.NOT. ALLOCATED(groups(slot)%inverse)) THEN
2620 ALLOCATE (groups(slot)%inverse(natom))
2621 groups(slot)%op%identity = .true.
2622 DO i = 1, natom
2623 groups(slot)%inverse(i) = i
2624 END DO
2625 IF (slot > 0) THEN
2626 CALL kp_symmetry_op_init(groups(slot)%op, kpoint, kpsym, is, for_aux_fit, &
2627 mixed_aux_orb)
2628 groups(slot)%reverse_phase = kpsym%phase_mode(is) == 2
2629 DO i = 1, natom
2630 groups(slot)%inverse(groups(slot)%op%atom_map(i)) = i
2631 END DO
2632 END IF
2633 ELSE IF (slot > 0) THEN
2634 ! One crystallographic operation has one atom/gauge mapping.
2635 cpassert(all(groups(slot)%op%atom_map == kpsym%f0(:, is)))
2636 cpassert(all(groups(slot)%op%cell_shift == kpsym%fcell_gauge(:, :, is)))
2637 END IF
2638 END IF
2639 IF (ik < kpoint%kp_range(1) .OR. ik > kpoint%kp_range(2)) cycle
2640 counts(slot) = counts(slot) + 1
2641 IF (pass == 2) THEN
2642 i = counts(slot)
2643 groups(slot)%ikp(i) = ikp
2644 groups(slot)%weight(i) = kpoint%wkp(ik)/real(ncontrib, dp)
2645 IF (slot == 0) THEN
2646 groups(slot)%xkp(:, i) = kpoint%xkp(:, ik)
2647 ELSE
2648 groups(slot)%xkp(:, i) = kpsym%xkp(:, is)
2649 END IF
2650 END IF
2651 END DO
2652 END DO
2653 IF (pass == 1) THEN
2654 DO slot = 0, 4*nrot
2655 ALLOCATE (groups(slot)%ikp(counts(slot)), groups(slot)%weight(counts(slot)), &
2656 groups(slot)%xkp(3, counts(slot)))
2657 END DO
2658 END IF
2659 END DO
2660 END SUBROUTINE kp_density_groups_create
2661
2662! **************************************************************************************************
2663!> \brief Map each AO block to its local FM row and column intervals, excluding padding.
2664!> \param matrix ...
2665!> \param row_size ...
2666!> \param col_size ...
2667!> \param row_offset ...
2668!> \param col_offset ...
2669!> \param tile local first/last row and column indices for each atom
2670! **************************************************************************************************
2671 SUBROUTINE kp_density_layout_create(matrix, row_size, col_size, row_offset, col_offset, tile)
2672 TYPE(cp_fm_type), INTENT(IN) :: matrix
2673 INTEGER, INTENT(IN) :: row_size(:), col_size(:), row_offset(:), &
2674 col_offset(:)
2675 INTEGER, INTENT(OUT) :: tile(:, :)
2676
2677 INTEGER :: a, i, ncol, nrow
2678 INTEGER, ALLOCATABLE :: cols(:), rows(:)
2679 INTEGER, POINTER :: col_indices(:), row_indices(:)
2680
2681 cpassert(matrix%matrix_struct%nrow_global == sum(row_size))
2682 cpassert(matrix%matrix_struct%ncol_global == sum(col_size))
2683 CALL cp_fm_get_info(matrix, nrow_local=nrow, ncol_local=ncol, &
2684 row_indices=row_indices, col_indices=col_indices)
2685 ALLOCATE (rows(0:sum(row_size)), cols(0:sum(col_size)))
2686 rows(:) = 0
2687 cols(:) = 0
2688 DO i = 1, nrow
2689 rows(row_indices(i)) = 1
2690 END DO
2691 DO i = 1, ncol
2692 cols(col_indices(i)) = 1
2693 END DO
2694 DO i = 1, ubound(rows, 1)
2695 rows(i) = rows(i) + rows(i - 1)
2696 END DO
2697 DO i = 1, ubound(cols, 1)
2698 cols(i) = cols(i) + cols(i - 1)
2699 END DO
2700 DO a = 1, SIZE(row_size)
2701 tile(1:2, a) = [rows(row_offset(a) - 1) + 1, rows(row_offset(a) + row_size(a) - 1)]
2702 END DO
2703 DO a = 1, SIZE(col_size)
2704 tile(3:4, a) = [cols(col_offset(a) - 1) + 1, cols(col_offset(a) + col_size(a) - 1)]
2705 END DO
2706 END SUBROUTINE kp_density_layout_create
2707
2708! **************************************************************************************************
2709!> \brief Find the source orientation that maps to a stored output block.
2710!> A nonsymmetric source can contribute both orientations to an upper block,
2711!> as in the DBCSR symmetry adapter. An absent orientation returns a=0.
2712!> \param group ...
2713!> \param TARGET output atom pair
2714!> \param symmetric source storage convention
2715!> \param orientation ...
2716!> \param a ...
2717!> \param b ...
2718!> \param trans whether rotation transposes the source block
2719! **************************************************************************************************
2720 SUBROUTINE kp_density_source_block(group, TARGET, symmetric, orientation, a, b, trans)
2721 TYPE(kp_density_group_type), INTENT(IN) :: group
2722 INTEGER, INTENT(IN) :: target(2)
2723 LOGICAL, INTENT(IN) :: symmetric
2724 INTEGER, INTENT(IN) :: orientation
2725 INTEGER, INTENT(OUT) :: a, b
2726 LOGICAL, INTENT(OUT) :: trans
2727
2728 INTEGER :: i, j
2729
2730 a = 0
2731 b = 0
2732 trans = .false.
2733 IF (group%op%identity) THEN
2734 IF (orientation == 1) THEN
2735 a = target(1)
2736 b = target(2)
2737 END IF
2738 RETURN
2739 END IF
2740 IF (.NOT. symmetric) THEN
2741 IF (orientation == 1) THEN
2742 a = group%inverse(target(1))
2743 b = group%inverse(target(2))
2744 END IF
2745 RETURN
2746 END IF
2747 IF (target(1) > target(2)) RETURN
2748 i = group%inverse(target(1))
2749 j = group%inverse(target(2))
2750 IF (orientation == 2 .AND. (symmetric .OR. i == j)) RETURN
2751 a = min(i, j)
2752 b = max(i, j)
2753 IF (orientation == 2) THEN
2754 a = max(i, j)
2755 b = min(i, j)
2756 END IF
2757 trans = group%op%atom_map(a) > group%op%atom_map(b)
2758 END SUBROUTINE kp_density_source_block
2759
2760! **************************************************************************************************
2761!> \brief Contract a small tile of images sharing an output AO block.
2762!> Pack bounded k-point batches once and reuse them across images in GEMM.
2763!> Gauge/Fourier phases remain inside the contraction; AO rotations follow
2764!> the completed local sum. MPI can then reduce the rotated partial blocks.
2765!> \param source ...
2766!> \param tiles local first/last row and column indices by atom, k-point and component
2767!> \param groups ...
2768!> \param jobs row, col, image, cell, real multiplicity and imaginary sign sum
2769!> \param available global source-block presence by orientation and operation
2770!> \param symmetric ...
2771!> \param row_size ...
2772!> \param col_size ...
2773!> \param row_offset ...
2774!> \param col_offset ...
2775!> \param TARGET flattened output blocks, one column per image
2776!> \param packed reusable thread-local source batch
2777!> \param phases reusable thread-local contraction coefficients
2778!> \param sums reusable thread-local contracted blocks
2779!> \param rotation_work reusable thread-local GEMM buffer
2780!> \param gemm_ctx thread-owned local GEMM context
2781! **************************************************************************************************
2782 SUBROUTINE kp_density_contract_tile(source, tiles, groups, jobs, available, symmetric, &
2783 row_size, col_size, row_offset, col_offset, TARGET, &
2784 packed, phases, sums, rotation_work, gemm_ctx)
2785 TYPE(cp_fm_p_type), INTENT(IN) :: source(:, :)
2786 INTEGER, INTENT(IN) :: tiles(:, :, :, :)
2787 TYPE(kp_density_group_type), INTENT(IN) :: groups(0:)
2788 INTEGER, INTENT(IN) :: jobs(:, :), available(:, 0:)
2789 LOGICAL, INTENT(IN) :: symmetric
2790 INTEGER, INTENT(IN) :: row_size(:), col_size(:), row_offset(:), &
2791 col_offset(:)
2792 REAL(kind=dp), CONTIGUOUS, INTENT(INOUT), TARGET :: target(:, :)
2793 REAL(kind=dp), ALLOCATABLE, INTENT(INOUT) :: packed(:, :), phases(:, :)
2794 REAL(kind=dp), ALLOCATABLE, INTENT(INOUT), TARGET :: sums(:, :)
2795 REAL(kind=dp), ALLOCATABLE, INTENT(INOUT) :: rotation_work(:, :)
2796 TYPE(local_gemm_ctxt_type), INTENT(INOUT) :: gemm_ctx
2797
2798 INTEGER(KIND=int_8), PARAMETER :: max_elements = 65536_int_8
2799
2800 INTEGER :: a, b, batch, column, first, i, ic, &
2801 ifirst, ii, ikp, ilast, image, j, jj, &
2802 k, last, nc, nelement, nimage, nrow, &
2803 orientation, shift(3), slot
2804 LOGICAL :: contributes, real_only, trans
2805 REAL(kind=dp) :: arg, cosine, factors(2, 2), sine
2806 REAL(kind=dp), POINTER :: left_rot(:, :), right_rot(:, :), &
2807 source_block(:, :), target_block(:, :)
2808 TYPE(cp_fm_type), POINTER :: matrix
2809
2810! Up to 512 KiB of packed input per worker, except for a single larger AO block.
2811
2812 nc = SIZE(source, 2)
2813 real_only = nc == 1
2814 nimage = SIZE(jobs, 2)
2815 DO slot = 0, ubound(groups, 1)
2816 IF (SIZE(groups(slot)%ikp) == 0) cycle
2817 DO orientation = 1, 2
2818 IF (available(orientation, slot) == 0) cycle
2819 CALL kp_density_source_block(groups(slot), jobs(1:2, 1), symmetric, orientation, a, b, trans)
2820 cpassert(a > 0)
2821 nelement = row_size(a)*col_size(b)
2822 batch = max(1, min(128, SIZE(groups(slot)%ikp), int(max_elements/(int(nelement, int_8)*nc))))
2823 CALL ensure_work_matrix(packed, nelement, nc*batch)
2824 CALL ensure_work_matrix(phases, nc*batch, nimage)
2825 CALL ensure_work_matrix(sums, nelement, nimage)
2826 sums(:, :) = 0.0_dp
2827 shift(:) = 0
2828 IF (.NOT. groups(slot)%op%identity) THEN
2829 shift(:) = groups(slot)%op%cell_shift(:, b) - groups(slot)%op%cell_shift(:, a)
2830 IF (groups(slot)%reverse_phase) shift(:) = -shift
2831 END IF
2832 DO first = 1, SIZE(groups(slot)%ikp), batch
2833 last = min(first + batch - 1, SIZE(groups(slot)%ikp))
2834 packed(:, :) = 0.0_dp
2835 contributes = .false.
2836 DO k = first, last
2837 ikp = groups(slot)%ikp(k)
2838 CALL kp_symmetry_phase_factors(shift, groups(slot)%xkp(:, k), &
2839 groups(slot)%op%time_reversal, real_only, factors)
2840 DO image = 1, nimage
2841 arg = twopi*dot_product(real(jobs(4:6, image), dp), groups(slot)%xkp(:, k))
2842 cosine = real(jobs(7, image), dp)*cos(arg)
2843 sine = real(jobs(8, image), dp)*sin(arg)*merge(-1.0_dp, 1.0_dp, trans)
2844 IF (real_only) sine = 0.0_dp
2845 DO ic = 1, nc
2846 column = (k - first)*nc + ic
2847 phases(column, image) = groups(slot)%weight(k)*(cosine*factors(1, ic) + sine*factors(2, ic))
2848 END DO
2849 END DO
2850 DO ic = 1, nc
2851 ifirst = tiles(1, a, ikp, ic)
2852 ilast = tiles(2, a, ikp, ic)
2853 IF (ifirst > ilast .OR. tiles(3, b, ikp, ic) > tiles(4, b, ikp, ic)) cycle
2854 contributes = .true.
2855 column = (k - first)*nc + ic
2856 matrix => source(ikp, ic)%matrix
2857 nrow = ilast - ifirst + 1
2858 DO j = tiles(3, b, ikp, ic), tiles(4, b, ikp, ic)
2859 jj = matrix%matrix_struct%col_indices(j) - col_offset(b)
2860 ii = matrix%matrix_struct%row_indices(ifirst) - row_offset(a) + 1 + jj*row_size(a)
2861 IF (matrix%matrix_struct%row_indices(ilast) - &
2862 matrix%matrix_struct%row_indices(ifirst) == nrow - 1) THEN
2863 packed(ii:ii + nrow - 1, column) = matrix%local_data(ifirst:ilast, j)
2864 ELSE
2865 DO i = ifirst, ilast
2866 ii = matrix%matrix_struct%row_indices(i) - row_offset(a) + 1 + jj*row_size(a)
2867 packed(ii, column) = matrix%local_data(i, j)
2868 END DO
2869 END IF
2870 END DO
2871 END DO
2872 END DO
2873 IF (.NOT. contributes) cycle
2874 CALL gemm_ctx%gemm('N', 'N', nelement, nimage, nc*(last - first + 1), 1.0_dp, &
2875 packed, SIZE(packed, 1), phases, SIZE(phases, 1), &
2876 1.0_dp, sums, SIZE(sums, 1))
2877 END DO
2878 IF (groups(slot)%op%identity) THEN
2879 target(:, :) = TARGET + sums
2880 ELSE
2881 left_rot => groups(slot)%op%kind_rot(groups(slot)%op%atom_kind(groups(slot)%inverse(jobs(1, 1))))%rmat
2882 right_rot => groups(slot)%op%kind_rot_right(groups(slot)%op%atom_kind(groups(slot)%inverse(jobs(2, 1))))%rmat
2883 DO image = 1, nimage
2884 source_block(1:row_size(a), 1:col_size(b)) => sums(:, image)
2885 target_block(1:row_size(jobs(1, image)), 1:col_size(jobs(2, image))) => target(:, image)
2886 CALL kp_symmetry_rotate_block(source_block, left_rot, right_rot, trans, &
2887 1.0_dp, target_block, rotation_work)
2888 END DO
2889 END IF
2890 END DO
2891 END DO
2892 END SUBROUTINE kp_density_contract_tile
2893
2894! **************************************************************************************************
2895!> \brief Sum compatible group-local FM tiles before returning each image to DBCSR.
2896!> The single-rank case stores directly. Incompatible layouts and cheaper
2897!> local sparse sums return to the common batched transform in the caller.
2898!> \param kpoint ...
2899!> \param source borrowed density matrices for all local k-points, components and spins
2900!> \param denmat ...
2901!> \param components real/imaginary DBCSR work matrices
2902!> \param fmwork global FM work matrices, unused on one rank
2903!> \param plan neighbor-list traversal and storage conventions
2904!> \param completed whether all spin/image results were produced
2905! **************************************************************************************************
2906 SUBROUTINE kpoint_density_transform_regular_grid(kpoint, source, denmat, components, fmwork, plan, completed)
2907
2908 TYPE(kpoint_type), POINTER :: kpoint
2909 TYPE(cp_fm_p_type), DIMENSION(:, :, :), INTENT(IN) :: source
2910 TYPE(dbcsr_p_type), DIMENSION(:, :) :: denmat
2911 TYPE(dbcsr_p_type), DIMENSION(:), INTENT(IN) :: components
2912 TYPE(cp_fm_type), DIMENSION(:), INTENT(IN), TARGET :: fmwork
2913 TYPE(kp_transform_plan_type), INTENT(IN) :: plan
2914 LOGICAL, INTENT(OUT) :: completed
2915
2916 CHARACTER(LEN=*), PARAMETER :: routinen = 'kpoint_density_transform_regular_grid'
2917
2918 INTEGER :: compatible, element, first, handle, i, &
2919 ic, igroup, ik, ikp, image, ispin, j, &
2920 kplocal, last, nc, ncol, nrow
2921 INTEGER, ALLOCATABLE :: blocks(:, :), factors(:, :)
2922 INTEGER, DIMENSION(14) :: layout, layout_max, layout_min
2923 INTEGER, DIMENSION(2) :: fft_range
2924 LOGICAL :: found, local, same_layout
2925 REAL(kind=dp) :: arg, dense_work, sparse_work
2926 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :) :: phase
2927 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :, :) :: fft_density
2928 REAL(kind=dp), DIMENSION(:, :), POINTER :: block
2929 TYPE(copy_info_type), DIMENSION(2) :: info
2930 TYPE(cp_fm_struct_type), POINTER :: fms
2931 TYPE(cp_fm_type) :: dummy
2932 TYPE(cp_fm_type), DIMENSION(2), TARGET :: partial
2933 TYPE(cp_fm_type), DIMENSION(:), POINTER :: image_source
2934 TYPE(mp_para_env_type), POINTER :: inter, para_env
2935
2936 completed = .false.
2937 para_env => kpoint%blacs_env_all%para_env
2938 inter => kpoint%para_env_inter_kp
2939 local = para_env%num_pe == 1
2940 kplocal = SIZE(source, 1)
2941 nc = SIZE(source, 2)
2942 cpassert(nc >= 1 .AND. nc <= 2)
2943 fms => source(1, 1, 1)%matrix%matrix_struct
2944 same_layout = .true.
2945 DO ispin = 1, SIZE(source, 3)
2946 DO ic = 1, nc
2947 DO ikp = 1, kplocal
2948 same_layout = same_layout .AND. cp_fm_struct_equivalent(fms, source(ikp, ic, ispin)%matrix%matrix_struct)
2949 same_layout = same_layout .AND. &
2950 all(fms%first_p_pos == source(ikp, ic, ispin)%matrix%matrix_struct%first_p_pos)
2951 END DO
2952 END DO
2953 END DO
2954 CALL cp_fm_get_info(source(1, 1, 1)%matrix, nrow_local=nrow, ncol_local=ncol)
2955 layout = [fms%nrow_global, fms%ncol_global, fms%nrow_block, fms%ncol_block, &
2956 fms%first_p_pos, fms%context%num_pe, fms%context%mepos, &
2957 shape(source(1, 1, 1)%matrix%local_data), nrow, ncol]
2958 layout_min = layout
2959 layout_max = layout
2960 IF (inter%num_pe > 1) THEN
2961 CALL inter%min(layout_min)
2962 CALL inter%max(layout_max)
2963 END IF
2964 compatible = merge(1, 0, same_layout .AND. all(layout_min == layout_max))
2965 IF (.NOT. local) CALL para_env%min(compatible)
2966 IF (compatible == 0) RETURN
2967
2968 ! Count the actual stored block work, so a sparse local transform is not
2969 ! replaced by a dense sum merely because the MPI image route is eligible.
2970 sparse_work = -1.0_dp
2971 dense_work = real(nrow, dp)*real(ncol, dp)*SIZE(denmat, 2)
2972 IF (local) THEN
2973 sparse_work = 0.0_dp
2974 DO igroup = 1, plan%ngroup
2975 first = plan%group_start(igroup)
2976 CALL dbcsr_get_readonly_block_p(components(1)%matrix, plan%row(first), plan%col(first), block, found=found)
2977 IF (found) sparse_work = sparse_work + real(SIZE(block), dp)
2978 END DO
2979 END IF
2980
2981 CALL timeset(routinen, handle)
2982 ALLOCATE (phase(kplocal, nc))
2983 DO ispin = 1, SIZE(source, 3)
2984 CALL kp_density_fft(kpoint, source(:, :, ispin), nrow, ncol, SIZE(denmat, 2), &
2985 fft_range, fft_density, local)
2986 IF (ALLOCATED(fft_density) .AND. kpoint%para_env_kp%num_pe == 1) THEN
2987 IF (.NOT. ALLOCATED(blocks)) THEN
2988 ! Keep neighbor-list multiplicity and imaginary signs in the k-point layer.
2989 ALLOCATE (blocks(3, plan%ngroup), factors(nc, plan%ngroup))
2990 DO igroup = 1, plan%ngroup
2991 first = plan%group_start(igroup)
2992 last = plan%group_start(igroup + 1) - 1
2993 blocks(:, igroup) = [plan%row(first), plan%col(first), plan%image(first)]
2994 factors(1, igroup) = last - first + 1
2995 IF (nc == 2) factors(2, igroup) = nint(sum(plan%symmetry_sign(first:last)))
2996 END DO
2997 END IF
2998 CALL cp_array_to_dbcsr_blocks(fft_density, fft_range(1), fft_range(2), para_env, &
2999 denmat(ispin, :), blocks, factors)
3000 DEALLOCATE (fft_density)
3001 completed = .true.
3002 cycle
3003 END IF
3004 IF (ispin == 1) THEN
3005 IF (local .AND. .NOT. ALLOCATED(fft_density) .AND. dense_work > sparse_work) EXIT
3006 DO ic = 1, nc
3007 CALL cp_fm_create(partial(ic), fms)
3008 END DO
3009 image_source => partial
3010 IF (.NOT. local) THEN
3011 cpassert(SIZE(fmwork) >= nc)
3012 image_source => fmwork
3013 END IF
3014 END IF
3015 DO image = 1, SIZE(denmat, 2)
3016 DO ic = 1, nc
3017 partial(ic)%local_data(:, :) = 0.0_dp
3018 END DO
3019 IF (ALLOCATED(fft_density)) THEN
3020!$OMP PARALLEL DO DEFAULT(NONE) SCHEDULE(STATIC) &
3021!$OMP SHARED(partial, fft_density, fft_range, image, nc, nrow) PRIVATE(element, i, j, ic)
3022 DO element = fft_range(1), fft_range(2)
3023 i = modulo(element - 1, nrow) + 1
3024 j = (element - 1)/nrow + 1
3025 DO ic = 1, nc
3026 partial(ic)%local_data(i, j) = fft_density(element - fft_range(1) + 1, ic, image)
3027 END DO
3028 END DO
3029!$OMP END PARALLEL DO
3030 ELSE
3031 DO ikp = 1, kplocal
3032 ik = kpoint%kp_range(1) + ikp - 1
3033 arg = twopi*dot_product(real(kpoint%index_to_cell(:, image), dp), kpoint%xkp(:, ik))
3034 phase(ikp, 1) = kpoint%wkp(ik)*cos(arg)
3035 IF (nc == 2) phase(ikp, 2) = kpoint%wkp(ik)*sin(arg)
3036 END DO
3037 ! Keep real/imaginary sums separate for the neighbor-list signs.
3038!$OMP PARALLEL DO DEFAULT(NONE) SCHEDULE(STATIC) &
3039!$OMP SHARED(partial, source, phase, nc, nrow, ncol, kplocal, ispin) PRIVATE(j, ikp, ic)
3040 DO j = 1, ncol
3041 DO ikp = 1, kplocal
3042 DO ic = 1, nc
3043 partial(ic)%local_data(1:nrow, j) = partial(ic)%local_data(1:nrow, j) + &
3044 phase(ikp, ic)*source(ikp, ic, ispin)%matrix%local_data(1:nrow, j)
3045 END DO
3046 END DO
3047 END DO
3048!$OMP END PARALLEL DO
3049 END IF
3050
3051 IF (.NOT. local) THEN
3052 DO ic = 1, nc
3053 IF (inter%num_pe > 1) CALL inter%sum(partial(ic)%local_data)
3054 IF (kpoint%iogrp) THEN
3055 CALL cp_fm_start_copy_general(partial(ic), fmwork(ic), para_env, info(ic))
3056 ELSE
3057 CALL cp_fm_start_copy_general(dummy, fmwork(ic), para_env, info(ic))
3058 END IF
3059 END DO
3060 DO ic = 1, nc
3061 CALL cp_fm_finish_copy_general(fmwork(ic), info(ic))
3062 IF (kpoint%iogrp) CALL cp_fm_cleanup_copy_general(info(ic))
3063 END DO
3064 END IF
3065 DO ic = 1, nc
3066 CALL kp_copy_fm_to_dbcsr(image_source(ic), components(ic)%matrix, local)
3067 END DO
3068 CALL kp_accumulate_density_image(denmat(ispin, image)%matrix, components(1)%matrix, &
3069 components(2)%matrix, image, nc == 1, plan)
3070 END DO
3071 IF (ALLOCATED(fft_density)) DEALLOCATE (fft_density)
3072 completed = .true.
3073 END DO
3074 DO ic = 1, nc
3075 CALL cp_fm_release(partial(ic))
3076 END DO
3077 CALL timestop(handle)
3078
3079 END SUBROUTINE kpoint_density_transform_regular_grid
3080
3081! **************************************************************************************************
3082!> \brief Exchange k-distributed FM entries into batched FFT pencils.
3083!> Each inter-group rank transforms disjoint FM entries, not a full replicated
3084!> set of matrices. Real F(R) = A(R)+B(R), real F(-R) = A(R)-B(R), where A and B
3085!> are the weighted cosine/real and sine/imaginary sums needed by the DBCSR
3086!> storage convention. Original reduced-grid weights are used without doubling.
3087!> \param kpoint ...
3088!> \param source current group-local real/imaginary density matrices
3089!> \param nrow number of valid local FM rows (excluding padding)
3090!> \param ncol number of valid local FM columns
3091!> \param nimg number of requested real-space images
3092!> \param owned local FM element range assigned to this inter-group rank
3093!> \param density allocated on success; unallocated selects the regular-grid direct sum
3094!> \param local all k-points and matrix entries are on this rank
3095! **************************************************************************************************
3096 SUBROUTINE kp_density_fft(kpoint, source, nrow, ncol, nimg, owned, density, local)
3097
3098 TYPE(kpoint_type), POINTER :: kpoint
3099 TYPE(cp_fm_p_type), DIMENSION(:, :), INTENT(IN) :: source
3100 INTEGER, INTENT(IN) :: nrow, ncol, nimg
3101 INTEGER, DIMENSION(2), INTENT(OUT) :: owned
3102 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :, :), &
3103 INTENT(OUT) :: density
3104 LOGICAL, INTENT(IN) :: local
3105
3106 INTEGER(KIND=int_8), PARAMETER :: max_bytes = 64_int_8*1024*1024, &
3107 min_fft_peer_bytes = 32_int_8*1024
3108 INTEGER, PARAMETER :: max_batch = 32
3109
3110 COMPLEX(KIND=dp), ALLOCATABLE, DIMENSION(:), &
3111 TARGET :: recvbuf
3112 COMPLEX(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :) :: values_rs
3113 COMPLEX(KIND=dp), CONTIGUOUS, DIMENSION(:), &
3114 POINTER :: sendbuf
3115 COMPLEX(KIND=dp), CONTIGUOUS, DIMENSION(:, :, :), &
3116 POINTER :: values_k
3117 INTEGER :: batch, count, element, first, group, handle, i, ikp, j, last, local_k, &
3118 min_local_k, mine, nc, nel, ngroup, nkp, nowned, offset, pos, width
3119 INTEGER(KIND=int_8) :: fixed_bytes, peer_bytes, per_entry, &
3120 tile_size
3121 INTEGER, ALLOCATABLE, DIMENSION(:) :: rcount, rdispl, scount, sdispl
3122 INTEGER, ALLOCATABLE, DIMENSION(:, :) :: cells, ranges
3123 INTEGER, DIMENSION(2) :: choice
3124 INTEGER, DIMENSION(3) :: nfft
3125 LOGICAL :: compatible
3126 TYPE(k_grid_to_cell_work_type) :: fft_work_data
3127 TYPE(mp_para_env_type), POINTER :: inter
3128
3129 owned = [1, 0]
3130 IF (kpoint%lattice_fft == lattice_fft_off) RETURN
3131 nkp = SIZE(kpoint%xkp, 2)
3132 IF (kpoint%lattice_fft == lattice_fft_auto .AND. nkp < 27) RETURN
3133 ! Preserve AUTO's local direct sum until a batched FFT executor can amortize
3134 ! per-transform threading. ON uses FFT on any eligible rank/thread layout.
3135 IF (kpoint%lattice_fft == lattice_fft_auto .AND. local) RETURN
3136 IF (any(kpoint%nkp_grid <= 0)) RETURN
3137 ! Bound map/scratch allocations before attempting a candidate-grid mapping.
3138 IF (32_int_8*product(int(kpoint%nkp_grid, int_8)) > max_bytes) RETURN
3139 compatible = lattice_fft_shape(kpoint%nkp_grid, nfft, allow_arbitrary=.true.)
3140 IF (.NOT. compatible) RETURN
3141 IF (32_int_8*product(int(nfft, int_8)) > max_bytes) RETURN
3142 compatible = regular_kpoint_grid(kpoint%xkp, kpoint%nkp_grid, allow_incomplete=.true.)
3143 IF (.NOT. compatible) RETURN
3144
3145 inter => kpoint%para_env_inter_kp
3146 ngroup = inter%num_pe
3147 mine = inter%mepos + 1
3148 local_k = SIZE(source, 1)
3149 nc = SIZE(source, 2)
3150 tile_size = int(nrow, int_8)*ncol
3151 ! Avoid default-integer overflow, including float-rounded partition limits.
3152 nel = 0
3153 IF (tile_size <= huge(nel)) nel = int(tile_size)
3154 ALLOCATE (ranges(2, ngroup))
3155 DO group = 1, ngroup
3156 ranges(1, group) = int(int(nel, int_8)*(group - 1)/ngroup) + 1
3157 ranges(2, group) = int(int(nel, int_8)*group/ngroup)
3158 END DO
3159 owned = ranges(:, mine)
3160 nowned = max(0, owned(2) - owned(1) + 1)
3161 fixed_bytes = 8_int_8*nowned*nc*nimg + 32_int_8*product(int(nfft, int_8)) + &
3162 4_int_8*product(int(kpoint%nkp_grid, int_8)) + 44_int_8*nkp + 52_int_8*nc*nimg + &
3163 24_int_8*ngroup + 64_int_8
3164 per_entry = 16_int_8*(int(ngroup, int_8)*local_k + nkp + int(nc, int_8)*nimg)
3165 width = int(max(0_int_8, min(int(max_batch, int_8), (max_bytes - fixed_bytes)/per_entry)))
3166 choice = [merge(1, 0, tile_size <= huge(nel)), width]
3167 ! All global ranks must either use FFT pencils or retain the direct path.
3168 IF (kpoint%blacs_env_all%para_env%num_pe > 1) CALL kpoint%blacs_env_all%para_env%min(choice)
3169 IF (choice(1) == 0 .OR. choice(2) == 0) RETURN
3170 width = choice(2)
3171 IF (kpoint%lattice_fft == lattice_fft_auto .AND. ngroup > 1) THEN
3172 ! With several k-point groups, small per-peer payloads do not amortize
3173 ! the all-to-all latency and the regular-grid direct sum is faster.
3174 min_local_k = minval(kpoint%kp_dist(2, :) - kpoint%kp_dist(1, :) + 1)
3175 peer_bytes = 16_int_8*int(width, int_8)*int(min_local_k, int_8)
3176 IF (peer_bytes < min_fft_peer_bytes) RETURN
3177 END IF
3178
3179 CALL timeset("kp_density_fft", handle)
3180 ALLOCATE (density(nowned, nc, nimg))
3181 ALLOCATE (cells(3, nc*nimg))
3182 cells(:, 1:nimg) = kpoint%index_to_cell(:, 1:nimg)
3183 IF (nc == 2) cells(:, nimg + 1:) = -cells(:, 1:nimg)
3184 ALLOCATE (scount(ngroup), sdispl(ngroup), rcount(ngroup), rdispl(ngroup))
3185 ALLOCATE (recvbuf(max(1, width*nkp)))
3186 IF (ngroup == 1) THEN
3187 sendbuf => recvbuf
3188 ELSE
3189 ALLOCATE (sendbuf(max(1, width*ngroup*local_k)))
3190 END IF
3191 ALLOCATE (values_rs(width, 1, nc*nimg))
3192 IF (nowned > 0) THEN
3193 CALL k_grid_to_cell_prepare(fft_work_data, kpoint%xkp, kpoint%nkp_grid, cells, &
3194 weights=kpoint%wkp, allow_incomplete=.true.)
3195 END IF
3196 DO batch = 1, maxval(ranges(2, :) - ranges(1, :) + 1), width
3197 count = max(0, min(width, nowned - batch + 1))
3198 offset = 0
3199 DO group = 1, ngroup
3200 first = ranges(1, group) + batch - 1
3201 last = min(first + width - 1, ranges(2, group))
3202 scount(group) = max(0, last - first + 1)*local_k
3203 sdispl(group) = offset
3204 offset = offset + scount(group)
3205 rcount(group) = count*(kpoint%kp_dist(2, group) - kpoint%kp_dist(1, group) + 1)
3206 rdispl(group) = count*(kpoint%kp_dist(1, group) - 1)
3207 ! Pack elements in k-major order; displacements already encode the
3208 ! unequal k ranges, so neither counts nor indices need an exchange.
3209!$OMP PARALLEL DO DEFAULT(NONE) SCHEDULE(STATIC) &
3210!$OMP SHARED(source, sendbuf, sdispl, group, first, last, nc, nrow, local_k) &
3211!$OMP PRIVATE(ikp, element, i, j, pos)
3212 DO ikp = 1, local_k
3213 DO element = first, last
3214 i = modulo(element - 1, nrow) + 1
3215 j = (element - 1)/nrow + 1
3216 pos = sdispl(group) + (ikp - 1)*(last - first + 1) + element - first + 1
3217 sendbuf(pos) = cmplx(source(ikp, 1)%matrix%local_data(i, j), 0.0_dp, dp)
3218 IF (nc == 2) sendbuf(pos) = cmplx(real(sendbuf(pos), dp), &
3219 source(ikp, 2)%matrix%local_data(i, j), dp)
3220 END DO
3221 END DO
3222!$OMP END PARALLEL DO
3223 END DO
3224 IF (ngroup > 1) CALL inter%alltoall(sendbuf, scount, sdispl, recvbuf, rcount, rdispl)
3225 IF (count == 0) cycle
3226 values_k(1:count, 1:1, 1:nkp) => recvbuf(1:count*nkp)
3227 ! Keep fft_tools pool/planner access outside OpenMP worker regions.
3228 CALL k_grid_to_cell_execute(fft_work_data, values_k, values_rs(1:count, :, :))
3229 DO i = 1, nimg
3230 IF (nc == 1) THEN
3231 density(batch:batch + count - 1, 1, i) = real(values_rs(1:count, 1, i), dp)
3232 ELSE
3233 density(batch:batch + count - 1, 1, i) = 0.5_dp* &
3234 REAL(values_rs(1:count, 1, i) + values_rs(1:count, 1, nimg + i), dp)
3235 density(batch:batch + count - 1, 2, i) = 0.5_dp* &
3236 REAL(values_rs(1:count, 1, i) - values_rs(1:count, 1, nimg + i), dp)
3237 END IF
3238 END DO
3239 END DO
3240 CALL k_grid_to_cell_release(fft_work_data)
3241 IF (ngroup > 1) DEALLOCATE (sendbuf)
3242 NULLIFY (sendbuf)
3243 CALL timestop(handle)
3244
3245 END SUBROUTINE kp_density_fft
3246
3247! **************************************************************************************************
3248!> \brief Apply the neighbor-list storage convention to an already k-summed image.
3249!> Cell phases and k weights have been applied in the FM distribution;
3250!> retain the multiplicity and imaginary sign of each neighbor-list entry.
3251!> \param denmat ...
3252!> \param rpmat ...
3253!> \param cpmat ...
3254!> \param image ...
3255!> \param real_only ...
3256!> \param plan ...
3257! **************************************************************************************************
3258 SUBROUTINE kp_accumulate_density_image(denmat, rpmat, cpmat, image, real_only, plan)
3259
3260 TYPE(dbcsr_type), POINTER :: denmat, rpmat, cpmat
3261 INTEGER, INTENT(IN) :: image
3262 LOGICAL, INTENT(IN) :: real_only
3263 TYPE(kp_transform_plan_type), INTENT(IN) :: plan
3264
3265 INTEGER :: first, igroup, last
3266 LOGICAL :: found
3267 REAL(kind=dp), DIMENSION(:, :), POINTER :: cblock, dblock, rblock
3268
3269 CALL dbcsr_set(denmat, 0.0_dp)
3270!$OMP PARALLEL DO DEFAULT(NONE) SCHEDULE(STATIC) &
3271!$OMP SHARED(denmat, rpmat, cpmat, image, real_only, plan) &
3272!$OMP PRIVATE(igroup, first, last, found, dblock, rblock, cblock)
3273 DO igroup = 1, plan%ngroup
3274 first = plan%group_start(igroup)
3275 IF (plan%image(first) /= image) cycle
3276 last = plan%group_start(igroup + 1) - 1
3277 CALL dbcsr_get_block_p(denmat, plan%row(first), plan%col(first), dblock, found=found)
3278 IF (.NOT. found) cycle
3279 CALL dbcsr_get_readonly_block_p(rpmat, plan%row(first), plan%col(first), rblock, found=found)
3280 IF (.NOT. found) cycle
3281 IF (.NOT. real_only) THEN
3282 CALL dbcsr_get_readonly_block_p(cpmat, plan%row(first), plan%col(first), cblock, found=found)
3283 IF (.NOT. found) cycle
3284 END IF
3285 dblock = real(last - first + 1, dp)*rblock
3286 IF (.NOT. real_only) dblock = dblock + sum(plan%symmetry_sign(first:last))*cblock
3287 END DO
3288!$OMP END PARALLEL DO
3289
3290 END SUBROUTINE kp_accumulate_density_image
3291
3292! **************************************************************************************************
3293!> \brief Copy density FM data to existing DBCSR blocks, with no redistribution on one rank.
3294!> \param fm ...
3295!> \param matrix ...
3296!> \param local all matrix data and DBCSR blocks belong to this rank
3297! **************************************************************************************************
3298 SUBROUTINE kp_copy_fm_to_dbcsr(fm, matrix, local)
3299
3300 TYPE(cp_fm_type), INTENT(IN) :: fm
3301 TYPE(dbcsr_type), POINTER :: matrix
3302 LOGICAL, INTENT(IN) :: local
3303
3304 INTEGER :: col_offset, row_offset
3305 REAL(kind=dp), CONTIGUOUS, DIMENSION(:, :), &
3306 POINTER :: full
3307 REAL(kind=dp), DIMENSION(:, :), POINTER :: block
3308 TYPE(dbcsr_iterator_type) :: iterator
3309
3310 IF (.NOT. local) THEN
3311 CALL copy_fm_to_dbcsr(fm, matrix, keep_sparsity=.true.)
3312 RETURN
3313 END IF
3314 CALL cp_fm_get_info(fm, local_data=full)
3315 CALL dbcsr_iterator_start(iterator, matrix, shared=.false.)
3316 DO WHILE (dbcsr_iterator_blocks_left(iterator))
3317 CALL dbcsr_iterator_next_block(iterator, block=block, row_offset=row_offset, &
3318 col_offset=col_offset)
3319 block(:, :) = full(row_offset:row_offset + SIZE(block, 1) - 1, &
3320 col_offset:col_offset + SIZE(block, 2) - 1)
3321 END DO
3322 CALL dbcsr_iterator_stop(iterator)
3323
3324 END SUBROUTINE kp_copy_fm_to_dbcsr
3325
3326! **************************************************************************************************
3327!> \brief Build the immutable neighbor-list traversal shared by R-to-K and K-to-R transforms.
3328!> \param plan ...
3329!> \param sab_nl ...
3330!> \param cell_to_index ...
3331!> \param nimg ...
3332!> \param block_template ...
3333!> \param group_entries ...
3334! **************************************************************************************************
3335 SUBROUTINE kp_transform_plan_create(plan, sab_nl, cell_to_index, nimg, block_template, group_entries)
3336
3337 TYPE(kp_transform_plan_type), INTENT(OUT) :: plan
3338 TYPE(neighbor_list_set_p_type), DIMENSION(:), &
3339 POINTER :: sab_nl
3340 INTEGER, DIMENSION(:, :, :), POINTER :: cell_to_index
3341 INTEGER, INTENT(IN) :: nimg
3342 TYPE(dbcsr_type), INTENT(IN), OPTIONAL :: block_template
3343 LOGICAL, INTENT(IN), OPTIONAL :: group_entries
3344
3345 INTEGER :: i, iatom, icell, icol, igroup, irow, &
3346 jatom, nblock
3347 INTEGER(KIND=int_8), ALLOCATABLE, DIMENSION(:) :: key
3348 INTEGER, ALLOCATABLE, DIMENSION(:) :: order
3349 INTEGER, DIMENSION(3) :: cell
3350 INTEGER, DIMENSION(:), POINTER :: col_offsets, row_offsets
3351 LOGICAL :: do_grouping, do_symmetric, store_offsets
3353 DIMENSION(:), POINTER :: iterator
3354
3355 CALL get_neighbor_list_set_p(neighbor_list_sets=sab_nl, symmetric=do_symmetric)
3356 plan%symmetric = do_symmetric
3357 do_grouping = .false.
3358 IF (PRESENT(group_entries)) do_grouping = group_entries
3359 store_offsets = PRESENT(block_template)
3360 IF (store_offsets) THEN
3361 CALL dbcsr_get_info(block_template, row_blk_offset=row_offsets, col_blk_offset=col_offsets)
3362 END IF
3363 CALL neighbor_list_iterator_create(iterator, sab_nl)
3364 DO WHILE (neighbor_list_iterate(iterator) == 0)
3365 CALL get_iterator_info(iterator, cell=cell)
3366 icell = cell_to_index(cell(1), cell(2), cell(3))
3367 IF (icell >= 1 .AND. icell <= nimg) plan%nentry = plan%nentry + 1
3368 END DO
3369 CALL neighbor_list_iterator_release(iterator)
3370
3371 ALLOCATE (plan%row(plan%nentry), plan%col(plan%nentry), plan%image(plan%nentry), &
3372 plan%cell(3, plan%nentry), plan%symmetry_sign(plan%nentry))
3373 IF (store_offsets) THEN
3374 ALLOCATE (plan%row_offset(plan%nentry), plan%col_offset(plan%nentry))
3375 END IF
3376
3377 i = 0
3378 CALL neighbor_list_iterator_create(iterator, sab_nl)
3379 DO WHILE (neighbor_list_iterate(iterator) == 0)
3380 CALL get_iterator_info(iterator, iatom=iatom, jatom=jatom, cell=cell)
3381 icell = cell_to_index(cell(1), cell(2), cell(3))
3382 IF (icell < 1 .OR. icell > nimg) cycle
3383 i = i + 1
3384 irow = iatom
3385 icol = jatom
3386 plan%symmetry_sign(i) = 1.0_dp
3387 IF (do_symmetric .AND. iatom > jatom) THEN
3388 irow = jatom
3389 icol = iatom
3390 plan%symmetry_sign(i) = -1.0_dp
3391 END IF
3392 plan%row(i) = irow
3393 plan%col(i) = icol
3394 IF (store_offsets) THEN
3395 plan%row_offset(i) = row_offsets(irow)
3396 plan%col_offset(i) = col_offsets(icol)
3397 END IF
3398 plan%image(i) = icell
3399 plan%cell(:, i) = cell
3400 END DO
3401 CALL neighbor_list_iterator_release(iterator)
3402 cpassert(i == plan%nentry)
3403
3404 IF (do_grouping .AND. plan%nentry > 0) THEN
3405 nblock = max(maxval(plan%row), maxval(plan%col))
3406 ALLOCATE (key(plan%nentry), order(plan%nentry))
3407 DO i = 1, plan%nentry
3408 key(i) = int(plan%image(i), kind=int_8) + int(nimg, kind=int_8)* &
3409 (int(plan%row(i) - 1, kind=int_8) + int(nblock, kind=int_8)* &
3410 int(plan%col(i) - 1, kind=int_8))
3411 END DO
3412 CALL sort(key, plan%nentry, order)
3413 plan%row(:) = plan%row(order)
3414 plan%col(:) = plan%col(order)
3415 IF (store_offsets) THEN
3416 plan%row_offset(:) = plan%row_offset(order)
3417 plan%col_offset(:) = plan%col_offset(order)
3418 END IF
3419 plan%image(:) = plan%image(order)
3420 plan%cell(:, :) = plan%cell(:, order)
3421 plan%symmetry_sign(:) = plan%symmetry_sign(order)
3422
3423 plan%ngroup = 1
3424 DO i = 2, plan%nentry
3425 IF (key(i) /= key(i - 1)) plan%ngroup = plan%ngroup + 1
3426 END DO
3427 ALLOCATE (plan%group_start(plan%ngroup + 1))
3428 igroup = 1
3429 plan%group_start(igroup) = 1
3430 DO i = 2, plan%nentry
3431 IF (key(i) /= key(i - 1)) THEN
3432 igroup = igroup + 1
3433 plan%group_start(igroup) = i
3434 END IF
3435 END DO
3436 plan%group_start(plan%ngroup + 1) = plan%nentry + 1
3437 DEALLOCATE (key, order)
3438 END IF
3439
3440 END SUBROUTINE kp_transform_plan_create
3441
3442! **************************************************************************************************
3443!> \brief Allocate a dense work matrix with the requested shape
3444!> \param work dense work matrix
3445!> \param nrow number of rows
3446!> \param ncol number of columns
3447! **************************************************************************************************
3448 SUBROUTINE ensure_work_matrix(work, nrow, ncol)
3449
3450 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :), &
3451 INTENT(INOUT) :: work
3452 INTEGER, INTENT(IN) :: nrow, ncol
3453
3454 IF (ALLOCATED(work)) THEN
3455 IF (SIZE(work, 1) == nrow .AND. SIZE(work, 2) == ncol) RETURN
3456 DEALLOCATE (work)
3457 END IF
3458 ALLOCATE (work(nrow, ncol))
3459
3460 END SUBROUTINE ensure_work_matrix
3461
3462! **************************************************************************************************
3463!> \brief Select the Bloch-phase convention that preserves overlap covariance.
3464!> \param kpoint ...
3465!> \param overlap_rs ...
3466!> \param tempmat ...
3467!> \param sab_nl ...
3468!> \param cell_to_index ...
3469!> \param for_aux_fit ...
3470!> \param mixed_aux_orb ...
3471! **************************************************************************************************
3472 SUBROUTINE calibrate_symmetry_phases(kpoint, overlap_rs, tempmat, sab_nl, cell_to_index, &
3473 for_aux_fit, mixed_aux_orb)
3474
3475 TYPE(kpoint_type), POINTER :: kpoint
3476 TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: overlap_rs
3477 TYPE(dbcsr_type), POINTER :: tempmat
3478 TYPE(neighbor_list_set_p_type), DIMENSION(:), &
3479 POINTER :: sab_nl
3480 INTEGER, DIMENSION(:, :, :), POINTER :: cell_to_index
3481 LOGICAL, INTENT(IN), OPTIONAL :: for_aux_fit, mixed_aux_orb
3482
3483 CHARACTER(LEN=*), PARAMETER :: routinen = 'calibrate_symmetry_phases'
3484
3485 CHARACTER(LEN=256) :: phase_error
3486 INTEGER :: best_mode, handle, iatom, ik, is, key, &
3487 mode, nrot, rot_slot
3488 INTEGER, ALLOCATABLE :: best_ik(:), best_is(:), mode_by_op(:)
3489 LOGICAL :: aux_fit, mixed_aux_orb_local, &
3490 needs_calibration, reverse
3491 REAL(kind=dp) :: arg, best_residual, candidate_norm, &
3492 direct_norm, overlap_dot, phase_score, &
3493 phase_tolerance, relative_residual
3494 REAL(kind=dp), ALLOCATABLE :: best_score(:)
3495 TYPE(dbcsr_type), POINTER :: direct_c, direct_r, source_c, source_r, &
3496 sym_c, sym_r
3497 TYPE(kp_symmetry_op_type) :: op
3498 TYPE(kpoint_sym_type), POINTER :: kpsym
3499
3500 aux_fit = .false.
3501 IF (PRESENT(for_aux_fit)) aux_fit = for_aux_fit
3502 mixed_aux_orb_local = .false.
3503 IF (PRESENT(mixed_aux_orb)) mixed_aux_orb_local = mixed_aux_orb
3504
3505 IF (kpoint%phases_calibrated) RETURN
3506
3507 needs_calibration = .false.
3508 DO ik = 1, kpoint%nkp
3509 kpsym => kpoint%kp_sym(ik)%kpoint_sym
3510 IF (kpsym%apply_symmetry) THEN
3511 ! Only the first nwght operations enter the density transform.
3512 IF (any(kpsym%phase_mode(1:kpsym%nwght) == 0)) THEN
3513 needs_calibration = .true.
3514 EXIT
3515 END IF
3516 END IF
3517 END DO
3518 IF (.NOT. needs_calibration) THEN
3519 kpoint%phases_calibrated = .true.
3520 RETURN
3521 END IF
3522
3523 CALL timeset(routinen, handle)
3524 cpassert(ASSOCIATED(kpoint%ibrot))
3525 nrot = SIZE(kpoint%ibrot)
3526 ALLOCATE (best_ik(2*nrot), best_is(2*nrot), mode_by_op(2*nrot), best_score(2*nrot))
3527 best_ik(:) = 0
3528 best_is(:) = 0
3529 mode_by_op(:) = 0
3530 best_score(:) = -1.0_dp
3531
3532 ! The Bloch-phase direction is a convention of the signed crystallographic
3533 ! operation, not of an individual member of its k-point star. Select one
3534 ! numerically discriminating representative per signed operation and reuse
3535 ! the result for all equivalent contributions.
3536 DO ik = 1, kpoint%nkp
3537 kpsym => kpoint%kp_sym(ik)%kpoint_sym
3538 IF (.NOT. kpsym%apply_symmetry) cycle
3539 DO is = 1, kpsym%nwght
3540 rot_slot = find_kpoint_rotation_slot(kpoint, kpsym%rotp(is))
3541 cpassert(rot_slot > 0)
3542 key = rot_slot + nrot*merge(1, 0, kpsym%rotp(is) < 0)
3543 IF (kpsym%phase_mode(is) > 0) THEN
3544 IF (mode_by_op(key) == 0) THEN
3545 mode_by_op(key) = kpsym%phase_mode(is)
3546 ELSE
3547 cpassert(mode_by_op(key) == kpsym%phase_mode(is))
3548 END IF
3549 cycle
3550 END IF
3551
3552 phase_score = 0.0_dp
3553 DO iatom = 2, SIZE(kpsym%fcell_gauge, 2)
3554 arg = dot_product(real(kpsym%fcell_gauge(:, iatom, is) - &
3555 kpsym%fcell_gauge(:, 1, is), kind=dp), &
3556 kpsym%xkp(:, is))
3557 phase_score = max(phase_score, abs(sin(twopi*arg)))
3558 END DO
3559 IF (phase_score > best_score(key)) THEN
3560 best_score(key) = phase_score
3561 best_ik(key) = ik
3562 best_is(key) = is
3563 END IF
3564 END DO
3565 END DO
3566
3567 ALLOCATE (source_r, source_c, direct_r, direct_c, sym_r, sym_c)
3568 CALL dbcsr_create(source_r, template=tempmat, matrix_type=dbcsr_type_symmetric)
3569 CALL dbcsr_create(source_c, template=tempmat, matrix_type=dbcsr_type_antisymmetric)
3570 CALL dbcsr_create(direct_r, template=tempmat, matrix_type=dbcsr_type_symmetric)
3571 CALL dbcsr_create(direct_c, template=tempmat, matrix_type=dbcsr_type_antisymmetric)
3572 CALL dbcsr_create(sym_r, template=tempmat, matrix_type=dbcsr_type_symmetric)
3573 CALL dbcsr_create(sym_c, template=tempmat, matrix_type=dbcsr_type_antisymmetric)
3574 CALL cp_dbcsr_alloc_block_from_nbl(source_r, sab_nl)
3575 CALL cp_dbcsr_alloc_block_from_nbl(source_c, sab_nl)
3576 CALL cp_dbcsr_alloc_block_from_nbl(direct_r, sab_nl)
3577 CALL cp_dbcsr_alloc_block_from_nbl(direct_c, sab_nl)
3578 CALL cp_dbcsr_alloc_block_from_nbl(sym_r, sab_nl)
3579 CALL cp_dbcsr_alloc_block_from_nbl(sym_c, sab_nl)
3580
3581 phase_tolerance = max(1.0e-6_dp, 100.0_dp*kpoint%eps_geo)
3582 DO key = 1, 2*nrot
3583 IF (mode_by_op(key) > 0 .OR. best_ik(key) == 0) cycle
3584 ! If both phase directions are algebraically indistinguishable for every
3585 ! occurrence of this operation, avoid an unnecessary overlap transform.
3586 IF (best_score(key) <= 1000.0_dp*epsilon(1.0_dp)) THEN
3587 mode_by_op(key) = 1
3588 cycle
3589 END IF
3590
3591 ik = best_ik(key)
3592 is = best_is(key)
3593 kpsym => kpoint%kp_sym(ik)%kpoint_sym
3594 CALL dbcsr_set(source_r, 0.0_dp)
3595 CALL dbcsr_set(source_c, 0.0_dp)
3596 CALL rskp_transform(source_r, source_c, overlap_rs, 1, kpoint%xkp(1:3, ik), &
3597 cell_to_index, sab_nl)
3598 CALL dbcsr_set(direct_r, 0.0_dp)
3599 CALL dbcsr_set(direct_c, 0.0_dp)
3600 CALL rskp_transform(direct_r, direct_c, overlap_rs, 1, kpsym%xkp(1:3, is), &
3601 cell_to_index, sab_nl)
3602 CALL dbcsr_dot(direct_r, direct_r, direct_norm)
3603 CALL dbcsr_dot(direct_c, direct_c, candidate_norm)
3604 direct_norm = direct_norm + candidate_norm
3605
3606 CALL kp_symmetry_op_init(op, kpoint, kpsym, is, for_aux_fit=aux_fit, &
3607 mixed_aux_orb=mixed_aux_orb_local)
3608 best_mode = 0
3609 best_residual = huge(1.0_dp)
3610 DO mode = 1, 2
3611 reverse = mode == 2
3612 CALL symtrans_phase(sym_r, sym_c, source_r, source_c, .false., op, reverse)
3613 CALL dbcsr_dot(sym_r, sym_r, candidate_norm)
3614 CALL dbcsr_dot(sym_c, sym_c, relative_residual)
3615 candidate_norm = candidate_norm + relative_residual
3616 CALL dbcsr_dot(sym_r, direct_r, overlap_dot)
3617 CALL dbcsr_dot(sym_c, direct_c, relative_residual)
3618 overlap_dot = overlap_dot + relative_residual
3619 relative_residual = sqrt(max(0.0_dp, candidate_norm + direct_norm - &
3620 2.0_dp*overlap_dot)/max(direct_norm, tiny(1.0_dp)))
3621 IF (relative_residual < best_residual) THEN
3622 best_residual = relative_residual
3623 best_mode = mode
3624 END IF
3625 END DO
3626 IF (best_residual > phase_tolerance) THEN
3627 WRITE (phase_error, '(A,ES12.4,A,I0,A,I0)') &
3628 "No Bloch-phase direction preserves overlap covariance; residual=", &
3629 best_residual, ", irreducible k-point=", ik, ", operation=", is
3630 CALL cp_abort(__location__, trim(phase_error))
3631 END IF
3632 mode_by_op(key) = best_mode
3633 END DO
3634
3635 DO ik = 1, kpoint%nkp
3636 kpsym => kpoint%kp_sym(ik)%kpoint_sym
3637 IF (.NOT. kpsym%apply_symmetry) cycle
3638 DO is = 1, kpsym%nwght
3639 IF (kpsym%phase_mode(is) > 0) cycle
3640 rot_slot = find_kpoint_rotation_slot(kpoint, kpsym%rotp(is))
3641 cpassert(rot_slot > 0)
3642 key = rot_slot + nrot*merge(1, 0, kpsym%rotp(is) < 0)
3643 cpassert(mode_by_op(key) > 0)
3644 kpsym%phase_mode(is) = mode_by_op(key)
3645 END DO
3646 END DO
3647
3648 CALL dbcsr_deallocate_matrix(source_r)
3649 CALL dbcsr_deallocate_matrix(source_c)
3650 CALL dbcsr_deallocate_matrix(direct_r)
3651 CALL dbcsr_deallocate_matrix(direct_c)
3652 CALL dbcsr_deallocate_matrix(sym_r)
3653 CALL dbcsr_deallocate_matrix(sym_c)
3654 kpoint%phases_calibrated = .true.
3655 CALL timestop(handle)
3656
3657 END SUBROUTINE calibrate_symmetry_phases
3658
3659! **************************************************************************************************
3660!> \brief Borrow the atom mapping and basis rotations for one symmetry contribution.
3661!> \param op non-owning operation view
3662!> \param kpoint k-point environment
3663!> \param kpsym symmetry data for the representative k-point
3664!> \param is index in the existing symmetry contribution list
3665!> \param for_aux_fit ...
3666!> \param mixed_aux_orb ...
3667! **************************************************************************************************
3668 SUBROUTINE kp_symmetry_op_init(op, kpoint, kpsym, is, for_aux_fit, mixed_aux_orb)
3669 TYPE(kp_symmetry_op_type), INTENT(OUT) :: op
3670 TYPE(kpoint_type), POINTER :: kpoint
3671 TYPE(kpoint_sym_type), POINTER :: kpsym
3672 INTEGER, INTENT(IN) :: is
3673 LOGICAL, INTENT(IN), OPTIONAL :: for_aux_fit, mixed_aux_orb
3674
3675 INTEGER :: iatom, rot_slot
3676 LOGICAL :: aux_fit, has_phase, my_mixed, perm, &
3677 rotates
3678 REAL(kind=dp) :: dr
3679 REAL(kind=dp), DIMENSION(:, :), POINTER :: rot
3680
3681 aux_fit = .false.
3682 IF (PRESENT(for_aux_fit)) aux_fit = for_aux_fit
3683 my_mixed = .false.
3684 IF (PRESENT(mixed_aux_orb)) my_mixed = mixed_aux_orb
3685
3686 rot_slot = find_kpoint_rotation_slot(kpoint, kpsym%rotp(is))
3687 cpassert(rot_slot > 0)
3688 IF (aux_fit .AND. ASSOCIATED(kpoint%kind_rotmat_aux)) THEN
3689 op%kind_rot => kpoint%kind_rotmat_aux(rot_slot, :)
3690 IF (my_mixed) THEN
3691 op%kind_rot_right => kpoint%kind_rotmat(rot_slot, :)
3692 ELSE
3693 op%kind_rot_right => kpoint%kind_rotmat_aux(rot_slot, :)
3694 END IF
3695 ELSE
3696 op%kind_rot => kpoint%kind_rotmat(rot_slot, :)
3697 op%kind_rot_right => kpoint%kind_rotmat(rot_slot, :)
3698 END IF
3699 op%atom_map => kpsym%f0(:, is)
3700 op%atom_kind => kpoint%atype
3701 op%cell_shift => kpsym%fcell_gauge(:, :, is)
3702 op%xkp(:) = kpsym%xkp(:, is)
3703 op%time_reversal = kpsym%rotp(is) < 0
3704
3705 perm = .false.
3706 DO iatom = 1, SIZE(op%atom_map)
3707 IF (op%atom_map(iatom) == iatom) cycle
3708 perm = .true.
3709 EXIT
3710 END DO
3711 rot => kpsym%rot(:, :, is)
3712 dr = abs(rot(1, 1) - 1.0_dp) + abs(rot(2, 2) - 1.0_dp) + abs(rot(3, 3) - 1.0_dp)
3713 rotates = abs(sum(abs(rot)) - 3.0_dp) > 1.e-12_dp .OR. abs(dr) > 1.e-12_dp
3714 has_phase = any(op%cell_shift /= 0) .OR. op%time_reversal
3715 op%identity = .NOT. (rotates .OR. perm .OR. has_phase)
3716 op%redistribute = perm .OR. has_phase
3717
3718 END SUBROUTINE kp_symmetry_op_init
3719
3720! **************************************************************************************************
3721!> \brief DBCSR adapter for a complex k-point symmetry operation.
3722!> \param srpmat real part of transformed matrix
3723!> \param scpmat imaginary part of transformed matrix
3724!> \param rpmat real part of reference matrix
3725!> \param cpmat imaginary part of reference matrix
3726!> \param real_only whether only the real part is present
3727!> \param op symmetry operation and atom mapping
3728!> \param reverse_phase use the reverse Bloch-phase convention
3729! **************************************************************************************************
3730 SUBROUTINE symtrans_phase(srpmat, scpmat, rpmat, cpmat, real_only, op, reverse_phase)
3731 TYPE(dbcsr_type), POINTER :: srpmat, scpmat, rpmat, cpmat
3732 LOGICAL, INTENT(IN) :: real_only
3733 TYPE(kp_symmetry_op_type), INTENT(IN) :: op
3734 LOGICAL, INTENT(IN) :: reverse_phase
3735
3736 CHARACTER(LEN=*), PARAMETER :: routinen = 'symtrans_phase'
3737
3738 INTEGER :: handle, icol, ip, irow, jcol, jp, jrow, &
3739 mynode, nthreads, numnodes, owner
3740 INTEGER, DIMENSION(3) :: shift
3741 LOGICAL :: found, trans
3742 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :) :: cwork, rwork, twork
3743 REAL(kind=dp), DIMENSION(:, :), POINTER :: cblock, left_rot, rblock, right_rot, &
3744 scblock, srblock
3745 TYPE(dbcsr_distribution_type) :: dist
3746 TYPE(dbcsr_iterator_type) :: iter
3747
3748 CALL timeset(routinen, handle)
3749
3750 IF (op%identity) THEN
3751 CALL dbcsr_copy(srpmat, rpmat)
3752 IF (.NOT. real_only) CALL dbcsr_copy(scpmat, cpmat)
3753 CALL timestop(handle)
3754 RETURN
3755 END IF
3756
3757 CALL dbcsr_get_info(rpmat, distribution=dist)
3758 CALL dbcsr_distribution_get(dist, mynode=mynode, numnodes=numnodes)
3759 IF (numnodes /= 1 .AND. op%redistribute) THEN
3760 CALL dbcsr_replicate_all(rpmat)
3761 IF (.NOT. real_only) CALL dbcsr_replicate_all(cpmat)
3762 END IF
3763
3764 CALL dbcsr_set(srpmat, 0.0_dp)
3765 IF (.NOT. real_only) CALL dbcsr_set(scpmat, 0.0_dp)
3766
3767 nthreads = 1
3768!$ nthreads = omp_get_max_threads()
3769 ! Atom permutations preserve row ownership, while block scheduling can race on a target row.
3770!$OMP PARALLEL DEFAULT(NONE) NUM_THREADS(nthreads) &
3771!$OMP SHARED(rpmat,cpmat,srpmat,scpmat,real_only,op,reverse_phase,mynode) &
3772!$OMP PRIVATE(iter,irow,icol,rblock,cblock,rwork,cwork,twork,left_rot,right_rot,shift, &
3773!$OMP found,ip,jp,jrow,jcol,trans,srblock,scblock,owner)
3774 CALL dbcsr_iterator_readonly_start(iter, rpmat, dynamic=.true., dynamic_byrows=.true.)
3775 DO WHILE (dbcsr_iterator_blocks_left(iter))
3776 CALL dbcsr_iterator_next_block(iter, irow, icol, rblock)
3777 shift(:) = op%cell_shift(:, icol) - op%cell_shift(:, irow)
3778 IF (reverse_phase) shift(:) = -shift
3779 IF (real_only) THEN
3780 CALL kp_symmetry_phase_block(rblock, shift, op%xkp, op%time_reversal, rwork)
3781 ELSE
3782 CALL dbcsr_get_readonly_block_p(matrix=cpmat, row=irow, col=icol, block=cblock, found=found)
3783 IF (.NOT. found) NULLIFY (cblock)
3784 CALL kp_symmetry_phase_block(rblock, shift, op%xkp, op%time_reversal, rwork, cwork, cblock)
3785 END IF
3786
3787 ip = op%atom_map(irow)
3788 jp = op%atom_map(icol)
3789 jrow = min(ip, jp)
3790 jcol = max(ip, jp)
3791 trans = ip > jp
3792 IF (trans) THEN
3793 left_rot => op%kind_rot(op%atom_kind(icol))%rmat
3794 right_rot => op%kind_rot_right(op%atom_kind(irow))%rmat
3795 ELSE
3796 left_rot => op%kind_rot(op%atom_kind(irow))%rmat
3797 right_rot => op%kind_rot_right(op%atom_kind(icol))%rmat
3798 END IF
3799
3800 CALL dbcsr_get_block_p(matrix=srpmat, row=jrow, col=jcol, block=srblock, found=found)
3801 IF (.NOT. found) THEN
3802 CALL dbcsr_get_stored_coordinates(srpmat, jrow, jcol, owner)
3803 cpassert(owner /= mynode)
3804 cycle
3805 END IF
3806 CALL kp_symmetry_rotate_block(rwork, left_rot, right_rot, trans, 1.0_dp, srblock, twork)
3807 IF (.NOT. real_only) THEN
3808 CALL dbcsr_get_block_p(matrix=scpmat, row=jrow, col=jcol, block=scblock, found=found)
3809 cpassert(found)
3810 ! Returning to the stored upper triangle conjugates the imaginary part.
3811 CALL kp_symmetry_rotate_block(cwork, left_rot, right_rot, trans, &
3812 merge(-1.0_dp, 1.0_dp, trans), scblock, twork)
3813 END IF
3814 END DO
3815 CALL dbcsr_iterator_stop(iter)
3816!$OMP END PARALLEL
3817 IF (numnodes /= 1 .AND. op%redistribute) THEN
3818 CALL dbcsr_distribute(rpmat)
3819 IF (.NOT. real_only) CALL dbcsr_distribute(cpmat)
3820 END IF
3821
3822 CALL timestop(handle)
3823
3824 END SUBROUTINE symtrans_phase
3825
3826! **************************************************************************************************
3827!> \brief Real two-component representation of the Bloch phase and time reversal.
3828!> \param shift column-minus-row cell shift with the selected phase direction
3829!> \param xkp target k-point coordinates
3830!> \param time_reversal conjugate the source before applying the phase
3831!> \param real_only reject phases not representable by real wavefunctions
3832!> \param phase coefficients mapping source [Re,Im] to transformed [Re,Im]
3833! **************************************************************************************************
3834 SUBROUTINE kp_symmetry_phase_factors(shift, xkp, time_reversal, real_only, phase)
3835 INTEGER, INTENT(IN) :: shift(3)
3836 REAL(kind=dp), INTENT(IN) :: xkp(3)
3837 LOGICAL, INTENT(IN) :: time_reversal, real_only
3838 REAL(kind=dp), INTENT(OUT) :: phase(2, 2)
3839
3840 REAL(kind=dp) :: arg, cosine, sine
3841
3842 arg = real(shift(1), dp)*xkp(1) + real(shift(2), dp)*xkp(2) + real(shift(3), dp)*xkp(3)
3843 cosine = cos(twopi*arg)
3844 sine = sin(twopi*arg)
3845 IF (real_only .AND. abs(sine) > 1.e-12_dp) THEN
3846 CALL cp_abort(__location__, "Real k-point wavefunctions cannot represent symmetry phases")
3847 END IF
3848 phase(:, 1) = [cosine, -sine]
3849 phase(:, 2) = merge(-1.0_dp, 1.0_dp, time_reversal)*[sine, cosine]
3850 END SUBROUTINE kp_symmetry_phase_factors
3851
3852! **************************************************************************************************
3853!> \brief Apply the Bloch phase and optional time reversal to one AO block.
3854!> \param rblock real part of source block
3855!> \param shift column-minus-row cell shift, with the selected phase direction
3856!> \param xkp target k-point coordinates
3857!> \param time_reversal conjugate the source before applying the phase
3858!> \param rwork real part of the phased block; caller-owned reusable workspace
3859!> \param cwork imaginary workspace; absent for real-only wavefunctions
3860!> \param cblock imaginary source block; absent when the sparse block is missing
3861! **************************************************************************************************
3862 SUBROUTINE kp_symmetry_phase_block(rblock, shift, xkp, time_reversal, rwork, cwork, cblock)
3863 REAL(kind=dp), DIMENSION(:, :), INTENT(IN) :: rblock
3864 INTEGER, DIMENSION(3), INTENT(IN) :: shift
3865 REAL(kind=dp), DIMENSION(3), INTENT(IN) :: xkp
3866 LOGICAL, INTENT(IN) :: time_reversal
3867 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :), &
3868 INTENT(INOUT) :: rwork
3869 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :), &
3870 INTENT(INOUT), OPTIONAL :: cwork
3871 REAL(kind=dp), DIMENSION(:, :), INTENT(IN), &
3872 OPTIONAL :: cblock
3873
3874 REAL(kind=dp) :: phase(2, 2)
3875
3876 CALL kp_symmetry_phase_factors(shift, xkp, time_reversal,.NOT. PRESENT(cwork), phase)
3877 CALL ensure_work_matrix(rwork, SIZE(rblock, 1), SIZE(rblock, 2))
3878 rwork(:, :) = phase(1, 1)*rblock
3879 IF (.NOT. PRESENT(cwork)) RETURN
3880 CALL ensure_work_matrix(cwork, SIZE(rblock, 1), SIZE(rblock, 2))
3881 cwork(:, :) = phase(2, 1)*rblock
3882 IF (PRESENT(cblock)) THEN
3883 rwork(:, :) = rwork + phase(1, 2)*cblock
3884 cwork(:, :) = cwork + phase(2, 2)*cblock
3885 END IF
3886
3887 END SUBROUTINE kp_symmetry_phase_block
3888
3889! **************************************************************************************************
3890!> \brief Accumulate left_rot * op(block) * right_rot^T into one AO block.
3891!> \param block phased source block
3892!> \param left_rot rotation for the target row
3893!> \param right_rot rotation for the target column
3894!> \param trans transpose the source when the atom mapping reverses block order
3895!> \param alpha sign for real symmetric or imaginary antisymmetric storage
3896!> \param TARGET destination block, updated in place
3897!> \param work caller-owned reusable GEMM workspace
3898! **************************************************************************************************
3899 SUBROUTINE kp_symmetry_rotate_block(block, left_rot, right_rot, trans, alpha, TARGET, work)
3900 REAL(kind=dp), DIMENSION(:, :), INTENT(IN) :: block, left_rot, right_rot
3901 LOGICAL, INTENT(IN) :: trans
3902 REAL(kind=dp), INTENT(IN) :: alpha
3903 REAL(kind=dp), DIMENSION(:, :), INTENT(INOUT) :: target
3904 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :), &
3905 INTENT(INOUT) :: work
3906
3907 INTEGER :: ncol
3908
3909 ncol = merge(SIZE(block, 1), SIZE(block, 2), trans)
3910 CALL ensure_work_matrix(work, SIZE(left_rot, 1), ncol)
3911 CALL dgemm('N', merge('T', 'N', trans), SIZE(left_rot, 1), ncol, SIZE(left_rot, 2), &
3912 1.0_dp, left_rot, SIZE(left_rot, 1), block, SIZE(block, 1), &
3913 0.0_dp, work, SIZE(work, 1))
3914 CALL dgemm('N', 'T', SIZE(work, 1), SIZE(right_rot, 1), SIZE(work, 2), &
3915 alpha, work, SIZE(work, 1), right_rot, SIZE(right_rot, 1), &
3916 1.0_dp, TARGET, SIZE(TARGET, 1))
3917
3918 END SUBROUTINE kp_symmetry_rotate_block
3919
3920END MODULE kpoint_methods
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(atomic_kind, fist_potential, element_symbol, name, mass, kind_number, natom, atom_list, rcov, rvdw, z, qeff, apol, cpol, mm_radius, shell, shell_active, damping)
Get attributes of an atomic kind.
Handles all functions related to the CELL.
Definition cell_types.F:15
subroutine, public real_to_scaled(s, r, cell)
Transform real to scaled cell coordinates. s=h_inv*r.
Definition cell_types.F:631
real(kind=dp) function, dimension(3), public pbc_stable(r, cell)
Apply a stable periodic-image convention for k-point Bloch gauges.
Definition cell_types.F:458
methods related to the blacs parallel environment
subroutine, public cp_blacs_env_create(blacs_env, para_env, blacs_grid_layout, blacs_repeatable, row_major, grid_2d)
allocates and initializes a type that represent a blacs context
Basic linear algebra operations for complex full matrices.
subroutine, public cp_cfm_scale_and_add(alpha, matrix_a, beta, matrix_b)
Scale and add two BLACS matrices (a = alpha*a + beta*b).
subroutine, public cp_cfm_transpose(matrix, trans, matrixt)
Transposes a BLACS distributed complex matrix.
subroutine, public cp_cfm_column_scale(matrix_a, scaling, first_col, source)
Scales columns of the full matrix by corresponding factors.
Represents a complex full matrix distributed on many processors.
subroutine, public cp_cfm_get_submatrix(fm, target_m, start_row, start_col, n_rows, n_cols, transpose)
Extract a sub-matrix from the full matrix: op(target_m)(1:n_rows,1:n_cols) = fm(start_row:start_row+n...
subroutine, public cp_cfm_release(matrix)
Releases a full matrix.
subroutine, public cp_fm_to_cfm(msourcer, msourcei, mtarget)
Construct a complex full matrix by taking its real and imaginary parts from two separate real-value f...
subroutine, public cp_cfm_create(matrix, matrix_struct, name, nrow, ncol, set_zero)
Creates a new full matrix with the given structure.
subroutine, public cp_cfm_get_info(matrix, name, nrow_global, ncol_global, nrow_block, ncol_block, nrow_local, ncol_local, row_indices, col_indices, local_data, context, matrix_struct, para_env)
Returns information about a full matrix.
subroutine, public cp_cfm_to_fm(msource, mtargetr, mtargeti)
Copy real and imaginary parts of a complex full matrix into separate real-value full matrices.
Defines control structures, which contain the parameters and the settings for the DFT-based calculati...
subroutine, public dbcsr_get_readonly_block_p(matrix, row, col, block, found, row_size, col_size)
Like dbcsr_get_block_p() but with matrix being INTENT(IN). When invoking this routine,...
subroutine, public dbcsr_deallocate_matrix(matrix)
...
logical function, public dbcsr_iterator_blocks_left(iterator)
...
subroutine, public dbcsr_iterator_stop(iterator)
...
subroutine, public dbcsr_copy(matrix_b, matrix_a, name, keep_sparsity, keep_imaginary)
...
subroutine, public dbcsr_get_block_p(matrix, row, col, block, found, row_size, col_size)
...
subroutine, public dbcsr_replicate_all(matrix)
...
subroutine, public dbcsr_get_info(matrix, nblkrows_total, nblkcols_total, nfullrows_total, nfullcols_total, nblkrows_local, nblkcols_local, nfullrows_local, nfullcols_local, my_prow, my_pcol, local_rows, local_cols, proc_row_dist, proc_col_dist, row_blk_size, col_blk_size, row_blk_offset, col_blk_offset, distribution, name, matrix_type, group)
...
subroutine, public dbcsr_get_stored_coordinates(matrix, row, column, processor)
...
subroutine, public dbcsr_distribute(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_iterator_readonly_start(iterator, matrix, shared, dynamic, dynamic_byrows)
Like dbcsr_iterator_start() but with matrix being INTENT(IN). When invoking this routine,...
subroutine, public dbcsr_distribution_get(dist, row_dist, col_dist, nrows, ncols, has_threads, group, mynode, numnodes, nprows, npcols, myprow, mypcol, pgrid, subgroups_defined, prow_group, pcol_group)
...
subroutine, public dbcsr_dot(matrix_a, matrix_b, trace)
Computes the dot product of two matrices, also known as the trace of their matrix product.
DBCSR operations in CP2K.
subroutine, public copy_fm_to_dbcsr(fm, matrix, keep_sparsity)
Copy a BLACS matrix to a dbcsr matrix.
Redistribution of DBCSR blocks and distributed matrix slices.
subroutine, public cp_array_to_dbcsr_blocks(array, first_entry, last_entry, comm, matrices, blocks, factors, row_major)
Fill existing DBCSR blocks directly from distributed flattened array slices. Requests split contiguou...
Basic linear algebra operations for full matrices.
subroutine, public cp_fm_column_scale(matrixa, scaling, first_col, source)
Scales columns of the full matrix by corresponding factors.
pool for for elements that are retained and released
subroutine, public fm_pool_create_fm(pool, element, name)
returns an element, allocating it if none is in the pool
subroutine, public fm_pool_give_back_fm(pool, element)
returns the element to the pool
represent the structure of a full matrix
logical function, public cp_fm_struct_equivalent(fmstruct1, fmstruct2)
returns true if the two matrix structures are equivalent, false otherwise.
represent a full matrix distributed on many processors
Definition cp_fm_types.F:15
subroutine, public cp_fm_start_copy_general(source, destination, para_env, info)
Initiates the copy operation: get distribution data, post MPI isend and irecvs.
subroutine, public cp_fm_cleanup_copy_general(info)
Completes the copy operation: wait for comms clean up MPI state.
subroutine, public cp_fm_get_info(matrix, name, nrow_global, ncol_global, nrow_block, ncol_block, nrow_local, ncol_local, row_indices, col_indices, local_data, context, nrow_locals, ncol_locals, matrix_struct, para_env)
returns all kind of information about the full matrix
subroutine, public cp_fm_get_diag(matrix, diag, n_active)
returns the diagonal elements of a fm
subroutine, public cp_fm_create(matrix, matrix_struct, name, nrow, ncol, set_zero)
creates a new full matrix with the given structure
subroutine, public cp_fm_finish_copy_general(destination, info)
Completes the copy operation: wait for comms, unpack, clean up MPI state.
subroutine, public cp_fm_get_submatrix(fm, target_m, start_row, start_col, n_rows, n_cols, transpose)
gets a submatrix of a full matrix op(target_m)(1:n_rows,1:n_cols) =fm(start_row:start_row+n_rows,...
various routines to log and control the output. The idea is that decisions about where to log should ...
integer function, public cp_logger_get_default_io_unit(logger)
returns the unit nr for the ionode (-1 on all other processors) skips as well checks if the procs cal...
K-points and crystal symmetry routines.
Definition cryssym.F:12
subroutine, public print_crys_symmetry(csym)
...
Definition cryssym.F:1859
subroutine, public kpoint_gen(csym, nk, symm, shift, full_grid, gamma_centered, inversion_symmetry_only, use_spglib_reduction, use_spglib_backend)
...
Definition cryssym.F:252
subroutine, public release_csym_type(csym)
Release the CSYM type.
Definition cryssym.F:89
subroutine, public kpoint_gen_general(csym, xkp_in, wkp_in, symm, full_grid, inversion_symmetry_only, use_spglib_reduction, use_spglib_backend)
Reduce an explicitly supplied GENERAL k-point set.
Definition cryssym.F:459
subroutine, public print_kp_symmetry(csym)
...
Definition cryssym.F:1896
subroutine, public crys_sym_gen(csym, scoor, types, hmat, delta, iounit, use_spglib)
...
Definition cryssym.F:144
subroutine, public probe_occupancy_kp(occ, fermi, kts, energies, rcoeff, icoeff, maxocc, probe, n, wk)
subroutine to calculate occupation number and 'Fermi' level using the
collects all constants needed in input so that they can be used without circular dependencies
integer, parameter, public smear_fermi_dirac
integer, parameter, public smear_gaussian
integer, parameter, public smear_mv
integer, parameter, public smear_mp
function that build the kpoints section of the input
integer, parameter, public use_spglib_kpoint_symmetry
integer, parameter, public lattice_fft_auto
integer, parameter, public lattice_fft_off
integer, parameter, public use_real_wfn
integer, parameter, public use_spglib_kpoint_backend
Defines the basic variable types.
Definition kinds.F:23
integer, parameter, public int_8
Definition kinds.F:54
integer, parameter, public dp
Definition kinds.F:34
Batched lattice Fourier transforms on regular k-point grids.
subroutine, public k_grid_to_cell_prepare(work, xkp, nkp_grid, index_to_cell, weights, allow_incomplete)
Prepare one inverse lattice transform for repeated matrix batches.
logical function, public regular_kpoint_grid(xkp, nkp_grid, grid_index, k_offset, allow_incomplete)
Test and map a uniformly shifted reciprocal grid or an explicitly allowed subset.
subroutine, public k_grid_to_cell_execute(work, values_k, values_rs, used_fft)
Execute a matrix batch using the prepared inverse lattice transform. Call outside OpenMP worker regio...
integer function, public lattice_fft_num_threads(nfft, nentries)
Bound concurrent forward transforms to 64 MiB of scratch, or one grid if larger.
subroutine, public k_grid_to_cell_release(work)
Release call-scoped inverse lattice maps and FFT buffers.
subroutine, public cell_to_k_grid_fft(values_rs, index_to_cell, xkp, nkp_grid, values_k, used_fft, deriv_direction, hmat, selected_kpoints)
Transform real matrices to a regular reciprocal grid or any subset of that grid.
logical function, public lattice_fft_shape(n, nfft, allow_arbitrary)
Find backend-supported FFT dimensions that preserve the original lattice grid. Sample the padded tran...
Routines needed for kpoint calculation.
subroutine, public kpoint_smearing_edge_status(wocc, wkp, smear, nspin, has_weight, first_fractional, last_occupied, last_occupied_spin)
summarize whether weighted k-point smearing reaches the available band edges
subroutine, public kpoint_density_transform(kpoint, denmat, wtype, tempmat, sab_nl, fmwork, for_aux_fit, pmat_ext, overlap_rs, mixed_aux_orb)
generate real space density matrices in DBCSR format
subroutine, public lowdin_kp_mo_coeff(kp, ispin, use_real_wfn, shalfc, cshalfc)
Calculate S(k)^1/2 C(k) for real or complex k-point wavefunctions.
subroutine, public kpoint_initialize_mo_set(kpoint)
...
subroutine, public rskp_transform(rmatrix, cmatrix, rsmat, ispin, xkp, cell_to_index, sab_nl, is_complex, rs_sign)
Transformation of real space matrices to a kpoint.
subroutine, public kpoint_init_cell_index(kpoint, sab_nl, para_env, nimages)
Generates the mapping of cell indices and linear RS index CELL (0,0,0) is always mapped to index 1.
subroutine kp_link_index_create(representative, nkp, start, order)
Index full-grid links by irreducible k-point without rescanning the complete mesh.
subroutine, public kpoint_set_mo_occupation(kpoint, smear, probe, added_mos_auto, added_mos_auto_grow, separate_spin_occupations)
Given the eigenvalues of all kpoints, calculates the occupation numbers.
subroutine, public rskp_transform_grid_extract(grid, ikp, rmatrix, cmatrix)
Extract one reciprocal-grid matrix from a prepared local DBCSR block cache.
subroutine, public rskp_transform_grid_release(grid)
Release a reciprocal-grid transformation cache.
subroutine, public kpoint_initialize_mos(kpoint, mos, added_mos, for_aux_fit)
Initialize a set of MOs and density matrix for each kpoint (kpoint group).
subroutine, public kp_transform_plan_create(plan, sab_nl, cell_to_index, nimg, block_template, group_entries)
Build the immutable neighbor-list traversal shared by R-to-K and K-to-R transforms.
subroutine, public kpoint_initialize(kpoint, particle_set, cell)
Generate the kpoints and initialize the kpoint environment.
subroutine, public kpoint_ot_energy_weighted_density(coeff, hc, occupation, wmat)
Build W(k) for noncanonical complex OT orbitals from H(k) C(k). The occupied-space Lagrange multiplie...
subroutine, public rskp_transform_grid_prepare(grid, rmatrix, rsmat, ispin, xkp, nkp_grid, cell_to_index, sab_nl, used_fft, is_complex, rs_sign, max_storage_bytes)
Prepare a batched real-cell transform to a regular grid or subset for local DBCSR blocks....
subroutine, public kpoint_density_matrices(kpoint, energy_weighted, for_aux_fit)
Calculate kpoint density matrices (rho(k), owned by kpoint groups).
subroutine, public lowdin_kp_trans(kpoint, pmat_diag)
Calculate Lowdin transformation of density matrix S^1/2 P S^1/2 Integrate diagonal elements over k-po...
subroutine, public kpoint_env_initialize(kpoint, para_env, blacs_env, with_aux_fit)
Initialize the kpoint environment.
subroutine, public calibrate_symmetry_phases(kpoint, overlap_rs, tempmat, sab_nl, cell_to_index, for_aux_fit, mixed_aux_orb)
Select the Bloch-phase convention that preserves overlap covariance.
Types and basic routines needed for a kpoint calculation.
subroutine, public kpoint_sym_create(kp_sym)
Create a single kpoint symmetry environment.
integer function, public find_kpoint_rotation_slot(qs_kpoint, rotp)
Locate the basis-rotation slot corresponding to a signed k-point symmetry operation.
subroutine, public kpoint_env_create(kp_env)
Create a single kpoint environment.
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.
Crystal-symmetry routines originating from the K290/ACMI code.
Definition kpsym.F:14
An array-based list which grows on demand. When the internal array is full, a new array of twice the ...
Definition list.F:24
integer, parameter, public local_gemm_pu_gpu
integer, parameter, public local_gemm_pu_host
Definition of mathematical constants and functions.
complex(kind=dp), parameter, public z_one
real(kind=dp), parameter, public twopi
complex(kind=dp), parameter, public z_zero
Collection of simple mathematical functions and subroutines.
Definition mathlib.F:15
pure real(kind=dp) function, dimension(3, 3), public inv_3x3(a)
Returns the inverse of the 3 x 3 matrix a.
Definition mathlib.F:526
Utility routines for the memory handling.
Interface to the message passing library MPI.
basic linear algebra operations for full matrixes
Define the data structure for the particle information.
wrapper for the pools of matrixes
subroutine, public mpools_get(mpools, ao_mo_fm_pools, ao_ao_fm_pool, mo_mo_fm_pools, ao_mosub_fm_pools, mosub_mosub_fm_pools, maxao_maxmo_fm_pool, maxao_maxao_fm_pool, maxmo_maxmo_fm_pool)
returns various attributes of the mpools (notably the pools contained in it)
subroutine, public mpools_create(mpools)
creates a mpools
subroutine, public mpools_rebuild_fm_pools(mpools, mos, blacs_env, para_env, nmosub)
rebuilds the pools of the (ao x mo, ao x ao , mo x mo) full matrixes
Definition and initialisation of the mo data type.
Definition qs_mo_types.F:22
subroutine, public set_mo_set(mo_set, maxocc, homo, lfomo, nao, nelectron, n_el_f, nmo, eigenvalues, occupation_numbers, uniform_occupation, kts, mu, flexible_electron_count)
Set the components of a MO set data structure.
subroutine, public init_mo_set(mo_set, fm_pool, fm_ref, fm_struct, name, counter, complex_coeff)
initializes an allocated mo_set. eigenvalues, mo_coeff, occupation_numbers are valid only after this ...
subroutine, public allocate_mo_set(mo_set, nao, nmo, nelectron, n_el_f, maxocc, flexible_electron_count)
Allocates a mo set and partially initializes it (nao,nmo,nelectron, and flexible_electron_count are v...
subroutine, public get_mo_set(mo_set, maxocc, homo, lfomo, nao, nelectron, n_el_f, nmo, eigenvalues, occupation_numbers, mo_coeff, mo_coeff_b, uniform_occupation, kts, mu, flexible_electron_count, cmo_coeff)
Get the components of a MO set data structure.
Define the neighbor list data types and the corresponding functionality.
subroutine, public neighbor_list_iterator_create(iterator_set, nl, search, nthread)
Neighbor list iterator functions.
subroutine, public neighbor_list_iterator_release(iterator_set)
...
subroutine, public get_neighbor_list_set_p(neighbor_list_sets, nlist, symmetric)
Return the components of the first neighbor list set.
integer function, public neighbor_list_iterate(iterator_set, mepos)
...
subroutine, public get_iterator_info(iterator_set, mepos, ikind, jkind, nkind, ilist, nlist, inode, nnode, iatom, jatom, r, cell)
...
parameters that control an scf iteration
Unified smearing module supporting four methods: smear_fermi_dirac — Fermi-Dirac distribution smear_g...
subroutine, public smearkp(f, mu, kts, e, nel, wk, sigma, maxocc, method)
Bisection search for mu given a target electron count (k-point case, single spin channel or spin-dege...
subroutine, public smearkp2(f, mu, kts, e, nel, wk, sigma, method)
Bisection search for mu (k-point, spin-polarised with a shared chemical potential across both spin ch...
pure logical function, public smearing_separate_spin(fixed_mag_mom)
Whether a smearing constraint fixes the separate spin populations.
pure real(kind=dp) function, dimension(2), public smearing_spin_targets(nelectron, fixed_mag_mom)
Resolve real-valued spin targets without rounding a prescribed moment.
All kind of helpful little routines.
Definition util.F:14
pure integer function, dimension(2), public get_limit(m, n, me)
divide m entries into n parts, return size of part me
Definition util.F:333
Type defining parameters related to the simulation cell.
Definition cell_types.F:60
represent a blacs multidimensional parallel environment (for the mpi corrispective see cp_paratypes/m...
Represent a complex full matrix.
represent a pool of elements with the same structure
keeps the information about the structure of a full matrix
Stores the state of a copy between cp_fm_start_copy_general and cp_fm_finish_copy_general.
just to build arrays of pointers to matrices
represent a full matrix
CSM type.
Definition cryssym.F:43
Rotation matrices for basis sets.
Keeps information about a specific k-point.
Keeps symmetry information about a specific k-point.
Contains information about kpoints.
stores all the informations relevant to an mpi environment
container for the pools of matrixes used by qs
contains the parameters needed by a scf run