40 dbcsr_type, dbcsr_type_antisymmetric, dbcsr_type_no_symmetry, dbcsr_type_symmetric
124#include "./base/base_uses.f90"
130 CHARACTER(len=*),
PARAMETER,
PRIVATE :: moduleN =
'kpoint_methods'
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
141 TYPE :: kp_symmetry_op_type
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
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
171 COMPLEX(KIND=dp),
ALLOCATABLE,
DIMENSION(:, :, :) :: values
172 INTEGER,
ALLOCATABLE,
DIMENSION(:) :: block_col, block_nelem, block_offset, block_row
173 LOGICAL :: ready = .false.
189 INTEGER,
INTENT(IN) :: representative(:), nkp
190 INTEGER,
ALLOCATABLE,
INTENT(OUT) :: start(:), order(:)
193 INTEGER,
ALLOCATABLE :: count(:), next(:)
195 ALLOCATE (count(nkp), next(nkp), start(nkp + 1), order(
SIZE(representative)))
197 DO i = 1,
SIZE(representative)
198 ik = representative(i)
199 cpassert(ik >= 1 .AND. ik <= nkp)
200 count(ik) = count(ik) + 1
204 start(ik + 1) = start(ik) + count(ik)
207 DO i = 1,
SIZE(representative)
208 ik = representative(i)
210 next(ik) = next(ik) + 1
223 SUBROUTINE kp_rotation_cache_create(crys_sym, cell, srot, frot, krot)
227 REAL(KIND=
dp),
ALLOCATABLE,
INTENT(OUT) :: srot(:, :, :)
228 INTEGER,
ALLOCATABLE,
INTENT(OUT) :: frot(:, :, :), krot(:, :, :)
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))))
239 END SUBROUTINE kp_rotation_cache_create
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)
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(:, :, :)
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
275 NULLIFY (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
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))
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)
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), &
298 diff(1:3) = kgvec(1:3) - anint(kgvec(1:3))
299 IF (all(abs(diff(1:3)) < eps_kpoint)) ns = ns + 1
304 kpsym%apply_symmetry = .true.
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.
312 DO ilink = link_start(ik), link_start(ik + 1) - 1
313 is = link_order(ilink)
315 ir = crys_sym%kpop(is)
317 DO ic = 1, crys_sym%nrtot
318 IF (crys_sym%ibrot(ic) /= ira) cycle
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)
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))
334 cpassert(ic <= crys_sym%nrtot)
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)
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), &
350 diff(1:3) = kgvec(1:3) - anint(kgvec(1:3))
351 IF (.NOT. all(abs(diff(1:3)) < eps_kpoint)) cycle
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)
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))
370 END SUBROUTINE kp_symmetry_import
384 CHARACTER(LEN=*),
PARAMETER :: routinen =
'kpoint_initialize'
386 INTEGER :: handle, i, ik, iounit, j, natom, nkind, &
388 INTEGER,
ALLOCATABLE,
DIMENSION(:) :: atype, kp_link_order, kp_link_start
389 INTEGER,
ALLOCATABLE,
DIMENSION(:, :) :: agauge
390 INTEGER,
ALLOCATABLE,
DIMENSION(:, :, :) :: frot_cache, krot_cache
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
400 CALL timeset(routinen, handle)
402 cpassert(
ASSOCIATED(kpoint))
404 SELECT CASE (kpoint%kp_scheme)
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)
415 CASE (
"MONKHORST-PACK",
"MACDONALD")
417 IF (.NOT. kpoint%symmetry)
THEN
420 ALLOCATE (coord(3, natom), scoord(3, natom), atype(natom))
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)
429 natom =
SIZE(particle_set)
430 ALLOCATE (scoord(3, natom), atype(natom))
432 CALL get_atomic_kind(atomic_kind=particle_set(i)%atomic_kind, kind_number=atype(i))
436 IF (kpoint%verbose)
THEN
442 ALLOCATE (kpoint%atype(natom))
445 ALLOCATE (agauge(3, natom))
447 IF (kpoint%symmetry)
THEN
449 r_pbc(1:3) =
pbc_stable(particle_set(i)%r(1:3), cell)
451 agauge(1:3, i) = nint(scoord_pbc(1:3) - scoord(1:3, i))
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= &
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
471 IF (crys_sym%nrtot > 0 .AND. .NOT. crys_sym%fullgrid .AND. &
472 crys_sym%istriz == 1 .AND. .NOT. crys_sym%inversion_only)
THEN
474 CALL kp_rotation_cache_create(crys_sym, cell, srot_cache, frot_cache, krot_cache)
477 eps_kpoint = max(1.e-12_dp, 10.0_dp*kpoint%eps_geo)
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)
488 IF (kpoint%symmetry)
THEN
489 nkind = maxval(atype)
491 ALLOCATE (kpoint%kind_rotmat(ns, nkind))
494 NULLIFY (kpoint%kind_rotmat(i, j)%rmat)
497 ALLOCATE (kpoint%kind_rotmat_aux(ns, nkind))
500 NULLIFY (kpoint%kind_rotmat_aux(i, j)%rmat)
503 ALLOCATE (kpoint%ibrot(ns))
504 kpoint%ibrot(1:ns) = crys_sym%ibrot(1:ns)
508 DEALLOCATE (scoord, atype)
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
517 xkp_full => kpoint%xkp
518 wkp_full => kpoint%wkp
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
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)
537 ALLOCATE (kpoint%kp_sym(kpoint%nkp))
539 NULLIFY (kpoint%kp_sym(i)%kpoint_sym)
543 IF (kpoint%verbose)
THEN
548 natom =
SIZE(particle_set)
549 ALLOCATE (scoord(3, natom), atype(natom))
551 CALL get_atomic_kind(atomic_kind=particle_set(i)%atomic_kind, kind_number=atype(i))
554 ALLOCATE (kpoint%atype(natom))
556 ALLOCATE (agauge(3, natom))
558 r_pbc(1:3) =
pbc_stable(particle_set(i)%r(1:3), cell)
560 agauge(1:3, i) = nint(scoord_pbc(1:3) - scoord(1:3, i))
563 CALL crys_sym_gen(crys_sym, scoord, atype, cell%hmat, delta=kpoint%eps_geo, iounit=iounit, &
567 full_grid=kpoint%full_grid, &
568 inversion_symmetry_only=kpoint%inversion_symmetry_only, &
569 use_spglib_reduction= &
572 IF (crys_sym%inversion_only) kpoint%inversion_symmetry_only = .true.
573 IF (
ASSOCIATED(kpoint%xkp))
THEN
574 DEALLOCATE (kpoint%xkp)
577 IF (
ASSOCIATED(kpoint%wkp))
THEN
578 DEALLOCATE (kpoint%wkp)
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
588 IF (crys_sym%nrtot > 0 .AND. .NOT. crys_sym%fullgrid .AND. &
589 crys_sym%istriz == 1 .AND. .NOT. crys_sym%inversion_only)
THEN
591 CALL kp_rotation_cache_create(crys_sym, cell, srot_cache, frot_cache, krot_cache)
594 eps_kpoint = max(1.e-12_dp, 10.0_dp*kpoint%eps_geo)
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)
603 nkind = maxval(atype)
605 ALLOCATE (kpoint%kind_rotmat(ns, nkind))
608 NULLIFY (kpoint%kind_rotmat(i, j)%rmat)
611 ALLOCATE (kpoint%kind_rotmat_aux(ns, nkind))
614 NULLIFY (kpoint%kind_rotmat_aux(i, j)%rmat)
617 ALLOCATE (kpoint%ibrot(ns))
618 kpoint%ibrot(1:ns) = crys_sym%ibrot(1:ns)
621 DEALLOCATE (scoord, atype)
625 cpabort(
"Option invalid or unavailable for kpoint%kp_scheme")
629 SELECT CASE (kpoint%kp_scheme)
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)
638 cpassert(kpoint%nkp >= 1)
639 CASE (
"MONKHORST-PACK",
"MACDONALD")
640 cpassert(kpoint%nkp >= 1)
642 IF (kpoint%use_real_wfn)
THEN
644 ikloop:
DO ik = 1, kpoint%nkp
646 spez = (kpoint%xkp(i, ik) == 0.0_dp .OR. kpoint%xkp(i, ik) == 0.5_dp)
647 IF (.NOT. spez)
EXIT ikloop
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. ")
658 CALL timestop(handle)
674 LOGICAL,
INTENT(IN),
OPTIONAL :: with_aux_fit
676 CHARACTER(LEN=*),
PARAMETER :: routinen =
'kpoint_env_initialize'
678 INTEGER :: handle, igr, ngr, niogrp, nkp, &
679 nkp_grp, nkp_loc, npe, unit_nr
680 INTEGER,
DIMENSION(2) :: dims, pos
685 CALL timeset(routinen, handle)
687 IF (
PRESENT(with_aux_fit))
THEN
688 aux_fit = with_aux_fit
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()
698 cpassert(.NOT.
ASSOCIATED(kpoint%kp_env))
700 cpassert(.NOT.
ASSOCIATED(kpoint%kp_aux_env))
704 npe = para_env%num_pe
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
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
721 IF (kpoint%parallel_group_size == -1)
THEN
725 IF (mod(npe, igr) /= 0) cycle
727 IF (nkp_grp > nkp) cycle
730 ELSE IF (kpoint%parallel_group_size == 0)
THEN
733 ELSE IF (kpoint%parallel_group_size > 0)
THEN
734 ngr = min(kpoint%parallel_group_size, npe)
736 cpabort(
"kpoint%parallel_group_size cannot be smaller than -1")
742 IF ((dims(1)*dims(2) /= npe))
THEN
743 cpabort(
"Number of processors is not divisible by the kpoint group size.")
745 IF (nkp_grp > nkp)
THEN
746 cpabort(
"Too many kpoint groups. Increase PARALLEL_GROUP_SIZE.")
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()
759 IF (para_env%is_source()) niogrp = 1
760 CALL para_env_kp%sum(niogrp)
761 kpoint%iogrp = (niogrp == 1)
764 kpoint%para_env_kp => para_env_kp
765 kpoint%para_env_inter_kp => para_env_inter_kp
768 ALLOCATE (kpoint%kp_dist(2, nkp_grp))
770 kpoint%kp_dist(1:2, igr) =
get_limit(nkp, nkp_grp, igr - 1)
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
778 IF (unit_nr > 0 .AND. kpoint%verbose)
THEN
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
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
790 kpoint%nkp_groups = nkp_grp
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)
798 CALL timestop(handle)
806 SUBROUTINE create_local_environments(env)
812 ALLOCATE (env(nkp_loc))
814 ikk = kpoint%kp_range(1) + ik - 1
816 kp => env(ik)%kpoint_env
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)
822 END SUBROUTINE create_local_environments
836 TYPE(
mo_set_type),
DIMENSION(:),
INTENT(INOUT) :: mos
837 INTEGER,
INTENT(IN),
OPTIONAL :: added_mos
838 LOGICAL,
OPTIONAL :: for_aux_fit
840 CHARACTER(LEN=*),
PARAMETER :: routinen =
'kpoint_initialize_mos'
842 INTEGER :: handle, ic, ik, is, nadd, nao, nc, &
843 nelectron, nkp_loc, nmo, nmorig(2), &
846 REAL(kind=
dp) :: flexible_electron_count, maxocc, n_el_f
854 CALL timeset(routinen, handle)
856 IF (
PRESENT(for_aux_fit))
THEN
857 aux_fit = for_aux_fit
862 cpassert(
ASSOCIATED(kpoint))
865 cpassert(
ASSOCIATED(kpoint%kp_aux_env))
868 IF (
PRESENT(added_mos))
THEN
874 IF (kpoint%use_real_wfn)
THEN
880 nkp_loc = kpoint%kp_range(2) - kpoint%kp_range(1) + 1
881 IF (nkp_loc > 0)
THEN
883 cpassert(
SIZE(kpoint%kp_aux_env) == nkp_loc)
885 cpassert(
SIZE(kpoint%kp_env) == nkp_loc)
890 kp => kpoint%kp_aux_env(ik)%kpoint_env
892 kp => kpoint%kp_env(ik)%kpoint_env
894 ALLOCATE (kp%mos(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)
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)
905 kp%mos_prefilled = .false.
913 IF (
ASSOCIATED(kpoint%blacs_env))
THEN
914 blacs_env => kpoint%blacs_env
917 kpoint%blacs_env => blacs_env
926 nmo = min(nao, nmorig(is) + nadd)
935 blacs_env=blacs_env, para_env=kpoint%para_env_kp)
938 kpoint%mpools_aux_fit => mpools
940 kpoint%mpools => mpools
949 CALL mpools_get(mpools, ao_ao_fm_pool=ao_ao_fm_pool)
955 kp => kpoint%kp_aux_env(ik)%kpoint_env
957 kp => kpoint%kp_env(ik)%kpoint_env
961 ALLOCATE (kp%pmat(nc, nspin))
969 ALLOCATE (kp%wmat(nc, nspin))
981 CALL timestop(handle)
992 CHARACTER(LEN=*),
PARAMETER :: routinen =
'kpoint_initialize_mo_set'
994 INTEGER :: handle, ik, ispin
998 CALL timeset(routinen, handle)
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)
1012 CALL timestop(handle)
1030 INTEGER,
INTENT(OUT) :: nimages
1032 CHARACTER(LEN=*),
PARAMETER :: routinen =
'kpoint_init_cell_index'
1034 INTEGER :: handle, i1, i2, i3, ic, icount, it, &
1036 INTEGER,
DIMENSION(3) :: cell, itm
1037 INTEGER,
DIMENSION(:, :),
POINTER :: index_to_cell,
list
1038 INTEGER,
DIMENSION(:, :, :),
POINTER :: cell_to_index, cti
1041 DIMENSION(:),
POINTER :: nl_iterator
1043 NULLIFY (cell_to_index, index_to_cell)
1045 CALL timeset(routinen, handle)
1047 cpassert(
ASSOCIATED(kpoint))
1049 ALLOCATE (
list(3, 125))
1059 IF (cell(1) ==
list(1, ic) .AND. cell(2) ==
list(2, ic) .AND. &
1060 cell(3) ==
list(3, ic))
THEN
1067 IF (icount >
SIZE(
list, 2))
THEN
1070 list(1:3, icount) = cell(1:3)
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)
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
1092 cti(i1, i2, i3) = ic
1094 CALL para_env%sum(cti)
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
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)
1110 IF (
ASSOCIATED(kpoint%index_to_cell))
THEN
1111 DEALLOCATE (kpoint%index_to_cell)
1113 ALLOCATE (kpoint%index_to_cell(3, ncount))
1114 index_to_cell => kpoint%index_to_cell
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
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
1134 kpoint%sab_nl => sab_nl
1137 nimages =
SIZE(index_to_cell, 2)
1141 CALL timestop(handle)
1158 xkp, cell_to_index, sab_nl, is_complex, rs_sign)
1163 INTEGER,
INTENT(IN) :: ispin
1164 REAL(kind=
dp),
DIMENSION(3),
INTENT(IN) :: xkp
1165 INTEGER,
DIMENSION(:, :, :),
POINTER :: cell_to_index
1168 LOGICAL,
INTENT(IN),
OPTIONAL :: is_complex
1169 REAL(kind=
dp),
INTENT(IN),
OPTIONAL :: rs_sign
1171 CHARACTER(LEN=*),
PARAMETER :: routinen =
'rskp_transform'
1173 INTEGER :: handle, iatom, ic, icol, irow, jatom, &
1175 INTEGER,
DIMENSION(3) :: cell
1176 LOGICAL :: do_symmetric, found, my_complex, &
1178 REAL(kind=
dp) :: arg, coskl, fsign, fsym, sinkl
1179 REAL(kind=
dp),
DIMENSION(:, :),
POINTER :: cblock, rblock, rsblock
1181 DIMENSION(:),
POINTER :: nl_iterator
1183 CALL timeset(routinen, handle)
1185 my_complex = .false.
1186 IF (
PRESENT(is_complex)) my_complex = is_complex
1189 IF (
PRESENT(rs_sign)) fsign = rs_sign
1191 wfn_real_only = .true.
1192 IF (
PRESENT(cmatrix)) wfn_real_only = .false.
1194 nimg =
SIZE(rsmat, 2)
1207 IF (do_symmetric .AND. (iatom > jatom))
THEN
1213 ic = cell_to_index(cell(1), cell(2), cell(3))
1214 IF (ic < 1 .OR. ic > nimg) cycle
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)
1221 coskl = fsign*cos(
twopi*arg)
1222 sinkl = fsign*fsym*sin(
twopi*arg)
1226 block=rsblock, found=found)
1227 IF (.NOT. found) cycle
1229 IF (wfn_real_only)
THEN
1231 block=rblock, found=found)
1232 IF (.NOT. found) cycle
1233 rblock = rblock + coskl*rsblock
1236 block=rblock, found=found)
1237 IF (.NOT. found) cycle
1239 block=cblock, found=found)
1240 IF (.NOT. found) cycle
1241 rblock = rblock + coskl*rsblock
1242 cblock = cblock + sinkl*rsblock
1248 CALL timestop(handle)
1269 cell_to_index, sab_nl, used_fft, is_complex, rs_sign, &
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
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
1286 CHARACTER(LEN=*),
PARAMETER :: routinen =
'rskp_transform_grid_prepare'
1288 INTEGER :: handle, i1, i2, i3, iatom, iblock, ic, &
1289 icol, irow, jatom, nblkcols, nblkrows, &
1290 nblocks, ncell, nelem, nimg, nkp, &
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
1302 DIMENSION(:),
POINTER :: nl_iterator
1304 CALL timeset(routinen, handle)
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)
1317 IF (32_int_8*product(int(nkp_grid,
int_8)) > memory_limit)
THEN
1318 CALL timestop(handle)
1322 CALL timestop(handle)
1325 IF (32_int_8*product(int(nfft,
int_8)) > memory_limit)
THEN
1326 CALL timestop(handle)
1330 CALL timestop(handle)
1339 nblocks = nblocks + 1
1340 nvalues = nvalues +
SIZE(rblock)
1343 IF (nblocks == 0 .OR. nvalues == 0)
THEN
1344 CALL timestop(handle)
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))
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)
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)
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
1377 fft_cell_to_index(i1, i2, i3) = ncell
1379 IF (fft_cell_to_index(-i1, -i2, -i3) == 0)
THEN
1381 fft_cell_to_index(-i1, -i2, -i3) = ncell
1386 IF (ncell == 0)
THEN
1388 CALL timestop(handle)
1393 storage_bytes = int(nvalues,
int_8)*(8_int_8*int(ncell,
int_8) + 16_int_8*int(nkp,
int_8)) + &
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)
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]
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
1418 IF (
PRESENT(rs_sign)) fsign = rs_sign
1428 IF (do_symmetric .AND. iatom > jatom)
THEN
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
1437 ic = cell_to_index(cell(1), cell(2), cell(3))
1438 IF (ic < 1 .OR. ic > nimg) cycle
1440 block=rsblock, found=found)
1441 IF (.NOT. found) cycle
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])
1455 CALL cell_to_k_grid_fft(values_rs, index_to_cell, xkp, nkp_grid, grid%values, used_fft)
1462 CALL timestop(handle)
1476 INTEGER,
INTENT(IN) :: ikp
1478 TYPE(
dbcsr_type),
INTENT(INOUT),
OPTIONAL :: cmatrix
1480 INTEGER :: iblock, ioffset, nelem
1482 REAL(kind=
dp),
DIMENSION(:, :),
POINTER :: cblock, rblock
1484 cpassert(grid%ready)
1485 cpassert(ikp >= 1 .AND. ikp <=
SIZE(grid%values, 3))
1487 IF (
PRESENT(cmatrix))
CALL dbcsr_set(cmatrix, 0.0_dp)
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)
1495 rblock = reshape(real(grid%values(ioffset:ioffset + nelem - 1, 1, ikp), kind=
dp), &
1497 IF (
PRESENT(cmatrix))
THEN
1498 CALL dbcsr_get_block_p(cmatrix, grid%block_row(iblock), grid%block_col(iblock), &
1499 cblock, found=found)
1501 cblock = reshape(aimag(grid%values(ioffset:ioffset + nelem - 1, 1, ikp)), shape(cblock))
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.
1534 kpoint, smear, probe, added_mos_auto, added_mos_auto_grow, separate_spin_occupations)
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
1544 CHARACTER(LEN=*),
PARAMETER :: routinen =
'kpoint_set_mo_occupation'
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
1565 CALL timeset(routinen, handle)
1567 my_added_mos_auto_grow = .false.
1568 IF (
PRESENT(added_mos_auto_grow)) added_mos_auto_grow = .false.
1572 kp => kpoint%kp_env(1)%kpoint_env
1573 nspin =
SIZE(kp%mos)
1575 IF (
PRESENT(separate_spin_occupations))
THEN
1576 my_separate_spin_occupations = my_separate_spin_occupations .OR. separate_spin_occupations
1579 CALL get_mo_set(mo_set, nmo=nmo, nao=nao, nelectron=nelectron)
1582 IF (nspin == 2)
THEN
1583 CALL get_mo_set(kp%mos(2), nmo=nb, nelectron=ne_b)
1587 ALLOCATE (weig(nmo, nkp, nspin), wocc(nmo, nkp, nspin))
1590 IF (
PRESENT(probe))
THEN
1591 ALLOCATE (rcoeff(nao, nmo, nkp, nspin), icoeff(nao, nmo, nkp, nspin))
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
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
1607 CALL cp_fm_get_info(mo_coeff, nrow_global=nrow_global, ncol_global=ncol_global)
1608 ALLOCATE (smatrix(nrow_global, ncol_global))
1610 rcoeff(1:nao, 1:nmo, ik, ispin) = smatrix(1:nao, 1:nmo)
1611 DEALLOCATE (smatrix)
1614 CALL cp_cfm_get_info(cmo_coeff, nrow_global=nrow_global, ncol_global=ncol_global)
1615 ALLOCATE (csmatrix(nrow_global, ncol_global))
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)
1625 CALL para_env_inter_kp%sum(weig)
1627 IF (
PRESENT(probe))
THEN
1628 CALL para_env_inter_kp%sum(rcoeff)
1629 CALL para_env_inter_kp%sum(icoeff)
1636 IF (
PRESENT(probe))
THEN
1637 smear%do_smear = .false.
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, &
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, &
1651 DO ikpgr = 1, kplocal
1652 ik = kp_range(1) + ikpgr - 1
1653 kp => kpoint%kp_env(ikpgr)%kpoint_env
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)
1660 mo_set%mu = mus(ispin)
1665 DEALLOCATE (weig, wocc, rcoeff, icoeff)
1669 IF (
PRESENT(probe) .EQV. .false.)
THEN
1670 IF (smear%do_smear)
THEN
1671 SELECT CASE (smear%method)
1674 IF (nspin == 1)
THEN
1675 nel = real(nelectron, kind=
dp)
1676 CALL smearkp(wocc(:, :, 1), mus(1), kts, weig(:, :, 1), nel, wkp, &
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, &
1684 nel = spin_targets(2)
1685 CALL smearkp(wocc(:, :, 2), mus(2), kts, weig(:, :, 2), nel, wkp, &
1689 nel = real(ne_a, kind=
dp) + real(ne_b, kind=
dp)
1690 CALL smearkp2(wocc(:, :, :), mu, kts, weig(:, :, :), nel, wkp, &
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)
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)
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)
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)
1720 cpabort(
"kpoints: Selected smearing not (yet) supported")
1724 IF (nspin == 1)
THEN
1725 nel = real(nelectron, kind=
dp)
1726 CALL smearkp(wocc(:, :, 1), mus(1), kts, weig(:, :, 1), nel, wkp, &
1730 nel = real(ne_a, kind=
dp)
1731 CALL smearkp(wocc(:, :, 1), mus(1), kts, weig(:, :, 1), nel, wkp, &
1734 nel = real(ne_b, kind=
dp)
1735 CALL smearkp(wocc(:, :, 2), mus(2), kts, weig(:, :, 2), nel, wkp, &
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)
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)
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.")
1756 DO ikpgr = 1, kplocal
1757 ik = kp_range(1) + ikpgr - 1
1758 kp => kpoint%kp_env(ikpgr)%kpoint_env
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
1768 kp%mos(ispin)%homo = count(occupation(1:nmo) > 0.0_dp)
1773 DEALLOCATE (weig, wocc)
1777 CALL timestop(handle)
1793 first_fractional, last_occupied, last_occupied_spin)
1795 REAL(kind=
dp),
DIMENSION(:, :, :),
INTENT(IN) :: wocc
1796 REAL(kind=
dp),
DIMENSION(:),
INTENT(IN) :: wkp
1798 INTEGER,
INTENT(IN) :: nspin
1799 LOGICAL,
INTENT(OUT) :: has_weight, first_fractional, &
1801 LOGICAL,
DIMENSION(:),
INTENT(OUT),
OPTIONAL :: last_occupied_spin
1803 INTEGER :: ik, ispin, nmo
1804 LOGICAL :: band_occupied
1805 REAL(kind=
dp) :: eps_occ, maxocc, weight, weight_threshold
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
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)
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
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.
1846 SUBROUTINE kpoint_check_added_mos_auto_occupation(wocc, wkp, smear, nspin, &
1847 added_mos_auto, nao, added_mos_auto_grow)
1849 REAL(kind=
dp),
DIMENSION(:, :, :),
INTENT(IN) :: wocc
1850 REAL(kind=
dp),
DIMENSION(:),
INTENT(IN) :: wkp
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
1857 INTEGER :: ispin, nmo
1858 LOGICAL :: first_fractional, has_weight, &
1860 LOGICAL,
DIMENSION(nspin) :: last_occupied_spin
1862 added_mos_auto_grow = .false.
1863 IF (.NOT. any(added_mos_auto) .OR. .NOT. smear%do_smear)
RETURN
1866 first_fractional, last_occupied, &
1867 last_occupied_spin=last_occupied_spin)
1868 IF (.NOT. has_weight)
RETURN
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.")
1878 added_mos_auto_grow = .true.
1882 END SUBROUTINE kpoint_check_added_mos_auto_occupation
1893 LOGICAL,
OPTIONAL :: energy_weighted, for_aux_fit
1895 CHARACTER(LEN=*),
PARAMETER :: routinen =
'kpoint_density_matrices'
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(:)
1906 CALL timeset(routinen, handle)
1908 IF (
PRESENT(energy_weighted)) wtype = energy_weighted
1910 IF (
PRESENT(for_aux_fit)) aux_fit = for_aux_fit
1912 cpassert(
ASSOCIATED(kpoint%kp_aux_env))
1913 kp => kpoint%kp_aux_env(1)%kpoint_env
1915 kp => kpoint%kp_env(1)%kpoint_env
1918 IF (kpoint%use_real_wfn)
THEN
1919 CALL cp_fm_get_info(kp%mos(1)%mo_coeff, matrix_struct=matrix_struct)
1921 CALL cp_cfm_get_info(kp%mos(1)%cmo_coeff, matrix_struct=matrix_struct)
1923 kplocal = kpoint%kp_range(2) - kpoint%kp_range(1) + 1
1924 nspin =
SIZE(kp%mos)
1926 local = product(matrix_struct%context%num_pe) == 1
1929 ALLOCATE (gemm_ctx(nworkers))
1930 IF (kpoint%use_real_wfn)
THEN
1931 ALLOCATE (work(nworkers))
1933 ALLOCATE (cwork(nworkers), cdensity(nworkers))
1935 DO thread = 1, nworkers
1936 IF (kpoint%use_real_wfn)
THEN
1940 CALL cp_cfm_create(cdensity(thread), kp%pmat(1, 1)%matrix_struct)
1947 DO ikpgr = 1, kplocal
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))
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))
1961 DO thread = 1, nworkers
1962 CALL gemm_ctx(thread)%destroy()
1963 IF (kpoint%use_real_wfn)
THEN
1970 IF (.NOT. wtype .AND. .NOT. aux_fit)
THEN
1971 kpoint%lowdin_density_ready = .true.
1972 kpoint%lowdin_population_ready = .false.
1974 CALL timestop(handle)
1991 REAL(kind=
dp),
DIMENSION(:),
INTENT(IN) :: occupation
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)
1998 COMPLEX(KIND=dp),
ALLOCATABLE,
DIMENSION(:) :: occupation_complex
2004 cpassert(
SIZE(occupation) >= nmo)
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)
2011 ALLOCATE (occupation_complex(nmo))
2012 occupation_complex(:) = cmplx(occupation(1:nmo), 0.0_dp, kind=
dp)
2014 DEALLOCATE (occupation_complex)
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)
2042 SUBROUTINE kpoint_density_matrix_job(kpoint, ikpgr, ispin, wtype, aux_fit, nao, nmo, &
2043 local, gemm_ctx, work, cwork, cdensity)
2046 INTEGER,
INTENT(IN) :: ikpgr, ispin
2047 LOGICAL,
INTENT(IN) :: wtype, aux_fit
2048 INTEGER,
INTENT(IN) :: nao, nmo
2049 LOGICAL,
INTENT(IN) :: local
2051 TYPE(
cp_fm_type),
INTENT(INOUT),
OPTIONAL :: work
2052 TYPE(
cp_cfm_type),
INTENT(INOUT),
OPTIONAL :: cwork, cdensity
2054 COMPLEX(KIND=dp) :: weights(nmo)
2055 REAL(kind=
dp),
POINTER :: eigenvalues(:), occupation(:)
2057 TYPE(
cp_fm_type),
POINTER :: coeff, cpmat, rpmat
2061 kp => kpoint%kp_aux_env(ikpgr)%kpoint_env
2063 kp => kpoint%kp_env(ikpgr)%kpoint_env
2065 CALL get_mo_set(kp%mos(ispin), occupation_numbers=occupation, eigenvalues=eigenvalues)
2067 rpmat => kp%wmat(1, ispin)
2069 rpmat => kp%pmat(1, ispin)
2071 IF (kpoint%use_real_wfn)
THEN
2072 cpassert(
PRESENT(work))
2073 coeff => kp%mos(ispin)%mo_coeff
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))
2086 CALL parallel_gemm(
"N",
"T", nao, nao, nmo, 1.0_dp, coeff, work, 0.0_dp, rpmat)
2089 cpassert(
PRESENT(cwork) .AND.
PRESENT(cdensity))
2091 cpmat => kp%wmat(2, ispin)
2093 cpmat => kp%pmat(2, ispin)
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)
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))
2112 END SUBROUTINE kpoint_density_matrix_job
2125 REAL(kind=
dp),
DIMENSION(:, :),
INTENT(INOUT) :: pmat_diag
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)
2131 INTEGER :: handle, ikpgr, ispin, kplocal, nao, nspin
2132 INTEGER,
DIMENSION(2) :: kp_range
2133 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:) :: dele
2138 TYPE(
cp_fm_type),
POINTER :: cpmat, pmat, rpmat, shalf
2142 CALL timeset(routinen, handle)
2144 nspin =
SIZE(pmat_diag, 2)
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)
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)
2158 ALLOCATE (dele(nao))
2161 kplocal = kp_range(2) - kp_range(1) + 1
2162 DO ikpgr = 1, kplocal
2163 kp => kpoint%kp_env(ikpgr)%kpoint_env
2165 IF (kpoint%use_real_wfn)
THEN
2166 pmat => kp%pmat(1, ispin)
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)
2171 rpmat => kp%pmat(1, ispin)
2172 cpmat => kp%pmat(2, ispin)
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)
2180 pmat_diag(1:nao, ispin) = pmat_diag(1:nao, ispin) + kp%wkp*dele(1:nao)
2185 CALL para_env_inter_kp%sum(pmat_diag)
2187 IF (kpoint%use_real_wfn)
THEN
2197 CALL timestop(handle)
2212 INTEGER,
INTENT(IN) :: ispin
2214 TYPE(
cp_fm_type),
INTENT(INOUT),
OPTIONAL :: shalfc
2215 TYPE(
cp_cfm_type),
INTENT(INOUT),
OPTIONAL :: cshalfc
2221 cpassert(
PRESENT(shalfc))
2222 mo_set => kp%mos(ispin)
2225 CALL parallel_gemm(
"N",
"N", nao, nmo, nao, 1.0_dp, kp%shalf, &
2226 mo_set%mo_coeff, 0.0_dp, shalfc)
2228 cpassert(
PRESENT(cshalfc))
2229 mo_set => kp%mos(ispin)
2232 mo_set%cmo_coeff,
z_zero, cshalfc)
2252 pmat_ext, overlap_rs, mixed_aux_orb)
2256 LOGICAL,
INTENT(IN) :: wtype
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
2265 POINTER :: overlap_rs
2266 LOGICAL,
OPTIONAL :: mixed_aux_orb
2268 CHARACTER(LEN=*),
PARAMETER :: routinen =
'kpoint_density_transform'
2270 INTEGER :: handle, ic, ik, ikp, ispin, kplocal, nc, &
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
2278 DIMENSION(:, :, :) :: source
2285 CALL timeset(routinen, handle)
2289 IF (
PRESENT(for_aux_fit))
THEN
2290 aux_fit = for_aux_fit
2294 IF (
PRESENT(mixed_aux_orb))
THEN
2295 mixed_aux_orb_local = mixed_aux_orb
2297 mixed_aux_orb_local = .false.
2301 cpassert(
ASSOCIATED(kpoint%kp_aux_env))
2307 matrix_type=merge(dbcsr_type_symmetric, dbcsr_type_no_symmetry, do_symmetric))
2311 components(1)%matrix => rpmat
2314 IF (
PRESENT(overlap_rs))
THEN
2316 for_aux_fit=aux_fit, mixed_aux_orb=mixed_aux_orb_local)
2320 kp => kpoint%kp_aux_env(1)%kpoint_env
2322 kp => kpoint%kp_env(1)%kpoint_env
2324 nspin =
SIZE(kp%mos)
2325 nc =
SIZE(kp%pmat, 1)
2326 nimg =
SIZE(denmat, 2)
2328 group_entries=.true.)
2332 kplocal = kpoint%kp_range(2) - kpoint%kp_range(1) + 1
2333 ALLOCATE (source(kplocal, nc, nspin))
2337 kp => kpoint%kp_aux_env(ikp)%kpoint_env
2339 kp => kpoint%kp_env(ikp)%kpoint_env
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)
2347 source(ikp, ic, ispin)%matrix => kp%pmat(ic, ispin)
2353 para_env => kpoint%blacs_env_all%para_env
2354 local = para_env%num_pe == 1
2356 cpassert(kpoint%kp_range(1) == 1 .AND. kplocal == nkp)
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)
2364 regular_grid_eligible = .false.
2366 IF (regular_grid_eligible)
THEN
2368 IF (kpoint%kp_sym(ik)%kpoint_sym%apply_symmetry)
THEN
2369 regular_grid_eligible = .false.
2375 regular_grid_done = .false.
2376 IF (regular_grid_eligible)
THEN
2379 matrix_type=merge(dbcsr_type_antisymmetric, dbcsr_type_no_symmetry, do_symmetric))
2382 components(2)%matrix => cpmat
2383 CALL kpoint_density_transform_regular_grid(kpoint, source, denmat, components, fmwork, &
2384 transform_plan, regular_grid_done)
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)
2394 CALL timestop(handle)
2411 SUBROUTINE kpoint_density_transform_batched(kpoint, source, denmat, template, plan, for_aux_fit, &
2414 TYPE(
cp_fm_p_type),
DIMENSION(:, :, :),
INTENT(IN) :: source
2418 LOGICAL,
INTENT(IN),
OPTIONAL :: for_aux_fit, mixed_aux_orb
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
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, &
2433 LOGICAL :: found, trans
2434 REAL(kind=
dp),
ALLOCATABLE :: packed(:, :), phases(:, :), &
2436 REAL(kind=
dp),
ALLOCATABLE,
TARGET :: sums(:, :), values(:)
2437 REAL(kind=
dp),
POINTER :: block(:, :)
2438 TYPE(kp_density_group_type),
ALLOCATABLE :: groups(:)
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))
2457 DO image = 1,
SIZE(denmat, 2)
2458 CALL dbcsr_set(denmat(ispin, image)%matrix, 0.0_dp)
2464 ALLOCATE (local_jobs(8, plan%ngroup), counts(0:world%num_pe - 1))
2466 DO igroup = 1, plan%ngroup
2467 first = plan%group_start(igroup)
2468 last = plan%group_start(igroup + 1) - 1
2470 plan%row(first), plan%col(first), block, found=found)
2471 IF (.NOT. found) cycle
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)))]
2477 CALL world%allgather(nlocal, counts)
2478 IF (maxval(counts) == 0)
THEN
2479 CALL timestop(handle)
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))
2488 ALLOCATE (gemm_ctx(nthreads))
2489 DO thread = 1, nthreads
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)
2500 offsets(j + 1) = offsets(j) + row_size(jobs(1, j))*col_size(jobs(2, j))
2506 DO WHILE (i <= njob)
2508 tile_start(ntile) = i
2510 DO WHILE (j <= min(njob, i + images_per_tile - 1))
2511 IF (any(jobs(1:2, j) /= jobs(1:2, i)))
EXIT
2516 tile_start(ntile + 1) = njob + 1
2517 available(:, :, :) = 0
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)
2527 available(orientation, slot, tile) = merge(1, 0, found)
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
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))
2558 IF (world%num_pe > 1)
CALL world%sum(values(1:nvalue), root=owner)
2559 IF (world%mepos /= owner) cycle
2561 CALL dbcsr_get_block_p(denmat(ispin, jobs(3, j))%matrix, jobs(1, j), jobs(2, j), block, found=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)
2572 DO thread = 1,
SIZE(gemm_ctx)
2573 CALL gemm_ctx(thread)%destroy()
2575 CALL timestop(handle)
2576 END SUBROUTINE kpoint_density_transform_batched
2588 SUBROUTINE kp_density_groups_create(kpoint, natom, groups, for_aux_fit, mixed_aux_orb)
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
2595 INTEGER :: i, ik, ikp, is, ncontrib, nrot, pass, &
2597 INTEGER,
ALLOCATABLE :: counts(:)
2601 IF (
ASSOCIATED(kpoint%ibrot)) nrot =
SIZE(kpoint%ibrot)
2602 ALLOCATE (groups(0:4*nrot), counts(0:4*nrot))
2605 DO ik = 1, kpoint%nkp
2606 ikp = ik - kpoint%kp_range(1) + 1
2607 kpsym => kpoint%kp_sym(ik)%kpoint_sym
2609 IF (
kpsym%apply_symmetry) ncontrib =
kpsym%nwght
2612 IF (
kpsym%apply_symmetry)
THEN
2613 cpassert(
kpsym%phase_mode(is) >= 1 .AND.
kpsym%phase_mode(is) <= 2)
2616 slot = slot + nrot*merge(1, 0,
kpsym%rotp(is) < 0) + 2*nrot*(
kpsym%phase_mode(is) - 1)
2619 IF (.NOT.
ALLOCATED(groups(slot)%inverse))
THEN
2620 ALLOCATE (groups(slot)%inverse(natom))
2621 groups(slot)%op%identity = .true.
2623 groups(slot)%inverse(i) = i
2626 CALL kp_symmetry_op_init(groups(slot)%op, kpoint,
kpsym, is, for_aux_fit, &
2628 groups(slot)%reverse_phase =
kpsym%phase_mode(is) == 2
2630 groups(slot)%inverse(groups(slot)%op%atom_map(i)) = i
2633 ELSE IF (slot > 0)
THEN
2635 cpassert(all(groups(slot)%op%atom_map ==
kpsym%f0(:, is)))
2636 cpassert(all(groups(slot)%op%cell_shift ==
kpsym%fcell_gauge(:, :, is)))
2639 IF (ik < kpoint%kp_range(1) .OR. ik > kpoint%kp_range(2)) cycle
2640 counts(slot) = counts(slot) + 1
2643 groups(slot)%ikp(i) = ikp
2644 groups(slot)%weight(i) = kpoint%wkp(ik)/real(ncontrib,
dp)
2646 groups(slot)%xkp(:, i) = kpoint%xkp(:, ik)
2648 groups(slot)%xkp(:, i) =
kpsym%xkp(:, is)
2655 ALLOCATE (groups(slot)%ikp(counts(slot)), groups(slot)%weight(counts(slot)), &
2656 groups(slot)%xkp(3, counts(slot)))
2660 END SUBROUTINE kp_density_groups_create
2671 SUBROUTINE kp_density_layout_create(matrix, row_size, col_size, row_offset, col_offset, tile)
2673 INTEGER,
INTENT(IN) :: row_size(:), col_size(:), row_offset(:), &
2675 INTEGER,
INTENT(OUT) :: tile(:, :)
2677 INTEGER :: a, i, ncol, nrow
2678 INTEGER,
ALLOCATABLE :: cols(:), rows(:)
2679 INTEGER,
POINTER :: col_indices(:), row_indices(:)
2681 cpassert(matrix%matrix_struct%nrow_global == sum(row_size))
2682 cpassert(matrix%matrix_struct%ncol_global == sum(col_size))
2684 row_indices=row_indices, col_indices=col_indices)
2685 ALLOCATE (rows(0:sum(row_size)), cols(0:sum(col_size)))
2689 rows(row_indices(i)) = 1
2692 cols(col_indices(i)) = 1
2694 DO i = 1, ubound(rows, 1)
2695 rows(i) = rows(i) + rows(i - 1)
2697 DO i = 1, ubound(cols, 1)
2698 cols(i) = cols(i) + cols(i - 1)
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)]
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)]
2706 END SUBROUTINE kp_density_layout_create
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
2733 IF (group%op%identity)
THEN
2734 IF (orientation == 1)
THEN
2740 IF (.NOT. symmetric)
THEN
2741 IF (orientation == 1)
THEN
2742 a = group%inverse(target(1))
2743 b = group%inverse(target(2))
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
2753 IF (orientation == 2)
THEN
2757 trans = group%op%atom_map(a) > group%op%atom_map(b)
2758 END SUBROUTINE kp_density_source_block
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)
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(:), &
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(:, :)
2798 INTEGER(KIND=int_8),
PARAMETER :: max_elements = 65536_int_8
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(:, :)
2812 nc =
SIZE(source, 2)
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)
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)
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
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.
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
2846 column = (k - first)*nc + ic
2847 phases(column, image) = groups(slot)%weight(k)*(cosine*factors(1, ic) + sine*factors(2, ic))
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)
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)
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))
2878 IF (groups(slot)%op%identity)
THEN
2879 target(:, :) =
TARGET + sums
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)
2892 END SUBROUTINE kp_density_contract_tile
2906 SUBROUTINE kpoint_density_transform_regular_grid(kpoint, source, denmat, components, fmwork, plan, completed)
2909 TYPE(
cp_fm_p_type),
DIMENSION(:, :, :),
INTENT(IN) :: source
2911 TYPE(
dbcsr_p_type),
DIMENSION(:),
INTENT(IN) :: components
2912 TYPE(
cp_fm_type),
DIMENSION(:),
INTENT(IN),
TARGET :: fmwork
2914 LOGICAL,
INTENT(OUT) :: completed
2916 CHARACTER(LEN=*),
PARAMETER :: routinen =
'kpoint_density_transform_regular_grid'
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
2932 TYPE(
cp_fm_type),
DIMENSION(2),
TARGET :: partial
2933 TYPE(
cp_fm_type),
DIMENSION(:),
POINTER :: image_source
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)
2949 same_layout = same_layout .AND. &
2950 all(fms%first_p_pos == source(ikp, ic, ispin)%matrix%matrix_struct%first_p_pos)
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]
2960 IF (inter%num_pe > 1)
THEN
2961 CALL inter%min(layout_min)
2962 CALL inter%max(layout_max)
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
2970 sparse_work = -1.0_dp
2971 dense_work = real(nrow,
dp)*real(ncol,
dp)*
SIZE(denmat, 2)
2973 sparse_work = 0.0_dp
2974 DO igroup = 1, plan%ngroup
2975 first = plan%group_start(igroup)
2977 IF (found) sparse_work = sparse_work + real(
SIZE(block),
dp)
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
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)))
2999 denmat(ispin, :), blocks, factors)
3000 DEALLOCATE (fft_density)
3004 IF (ispin == 1)
THEN
3005 IF (local .AND. .NOT.
ALLOCATED(fft_density) .AND. dense_work > sparse_work)
EXIT
3009 image_source => partial
3010 IF (.NOT. local)
THEN
3011 cpassert(
SIZE(fmwork) >= nc)
3012 image_source => fmwork
3015 DO image = 1,
SIZE(denmat, 2)
3017 partial(ic)%local_data(:, :) = 0.0_dp
3019 IF (
ALLOCATED(fft_density))
THEN
3022 DO element = fft_range(1), fft_range(2)
3023 i =
modulo(element - 1, nrow) + 1
3024 j = (element - 1)/nrow + 1
3026 partial(ic)%local_data(i, j) = fft_density(element - fft_range(1) + 1, ic, image)
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)
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)
3051 IF (.NOT. local)
THEN
3053 IF (inter%num_pe > 1)
CALL inter%sum(partial(ic)%local_data)
3054 IF (kpoint%iogrp)
THEN
3066 CALL kp_copy_fm_to_dbcsr(image_source(ic), components(ic)%matrix, local)
3068 CALL kp_accumulate_density_image(denmat(ispin, image)%matrix, components(1)%matrix, &
3069 components(2)%matrix, image, nc == 1, plan)
3071 IF (
ALLOCATED(fft_density))
DEALLOCATE (fft_density)
3077 CALL timestop(handle)
3079 END SUBROUTINE kpoint_density_transform_regular_grid
3096 SUBROUTINE kp_density_fft(kpoint, source, nrow, ncol, nimg, owned, density, local)
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
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
3110 COMPLEX(KIND=dp),
ALLOCATABLE,
DIMENSION(:), &
3112 COMPLEX(KIND=dp),
ALLOCATABLE,
DIMENSION(:, :, :) :: values_rs
3113 COMPLEX(KIND=dp),
CONTIGUOUS,
DIMENSION(:), &
3115 COMPLEX(KIND=dp),
CONTIGUOUS,
DIMENSION(:, :, :), &
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, &
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
3131 nkp =
SIZE(kpoint%xkp, 2)
3136 IF (any(kpoint%nkp_grid <= 0))
RETURN
3138 IF (32_int_8*product(int(kpoint%nkp_grid,
int_8)) > max_bytes)
RETURN
3140 IF (.NOT. compatible)
RETURN
3141 IF (32_int_8*product(int(nfft,
int_8)) > max_bytes)
RETURN
3143 IF (.NOT. compatible)
RETURN
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
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)
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]
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
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
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
3189 ALLOCATE (sendbuf(max(1, width*ngroup*local_k)))
3191 ALLOCATE (values_rs(width, 1, nc*nimg))
3192 IF (nowned > 0)
THEN
3194 weights=kpoint%wkp, allow_incomplete=.true.)
3196 DO batch = 1, maxval(ranges(2, :) - ranges(1, :) + 1), width
3197 count = max(0, min(width, nowned - batch + 1))
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)
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)
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)
3231 density(batch:batch + count - 1, 1, i) = real(values_rs(1:count, 1, i),
dp)
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)
3241 IF (ngroup > 1)
DEALLOCATE (sendbuf)
3243 CALL timestop(handle)
3245 END SUBROUTINE kp_density_fft
3258 SUBROUTINE kp_accumulate_density_image(denmat, rpmat, cpmat, image, real_only, plan)
3260 TYPE(
dbcsr_type),
POINTER :: denmat, rpmat, cpmat
3261 INTEGER,
INTENT(IN) :: image
3262 LOGICAL,
INTENT(IN) :: real_only
3265 INTEGER :: first, igroup, last
3267 REAL(kind=
dp),
DIMENSION(:, :),
POINTER :: cblock, dblock, rblock
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
3280 IF (.NOT. found) cycle
3281 IF (.NOT. real_only)
THEN
3283 IF (.NOT. found) cycle
3285 dblock = real(last - first + 1,
dp)*rblock
3286 IF (.NOT. real_only) dblock = dblock + sum(plan%symmetry_sign(first:last))*cblock
3290 END SUBROUTINE kp_accumulate_density_image
3298 SUBROUTINE kp_copy_fm_to_dbcsr(fm, matrix, local)
3302 LOGICAL,
INTENT(IN) :: local
3304 INTEGER :: col_offset, row_offset
3305 REAL(kind=
dp),
CONTIGUOUS,
DIMENSION(:, :), &
3307 REAL(kind=
dp),
DIMENSION(:, :),
POINTER :: block
3310 IF (.NOT. local)
THEN
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)
3324 END SUBROUTINE kp_copy_fm_to_dbcsr
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
3345 INTEGER :: i, iatom, icell, icol, igroup, irow, &
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
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)
3366 icell = cell_to_index(cell(1), cell(2), cell(3))
3367 IF (icell >= 1 .AND. icell <= nimg) plan%nentry = plan%nentry + 1
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))
3381 icell = cell_to_index(cell(1), cell(2), cell(3))
3382 IF (icell < 1 .OR. icell > nimg) cycle
3386 plan%symmetry_sign(i) = 1.0_dp
3387 IF (do_symmetric .AND. iatom > jatom)
THEN
3390 plan%symmetry_sign(i) = -1.0_dp
3394 IF (store_offsets)
THEN
3395 plan%row_offset(i) = row_offsets(irow)
3396 plan%col_offset(i) = col_offsets(icol)
3398 plan%image(i) = icell
3399 plan%cell(:, i) = cell
3402 cpassert(i == plan%nentry)
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))
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)
3419 plan%image(:) = plan%image(order)
3420 plan%cell(:, :) = plan%cell(:, order)
3421 plan%symmetry_sign(:) = plan%symmetry_sign(order)
3424 DO i = 2, plan%nentry
3425 IF (key(i) /= key(i - 1)) plan%ngroup = plan%ngroup + 1
3427 ALLOCATE (plan%group_start(plan%ngroup + 1))
3429 plan%group_start(igroup) = 1
3430 DO i = 2, plan%nentry
3431 IF (key(i) /= key(i - 1))
THEN
3433 plan%group_start(igroup) = i
3436 plan%group_start(plan%ngroup + 1) = plan%nentry + 1
3437 DEALLOCATE (key, order)
3448 SUBROUTINE ensure_work_matrix(work, nrow, ncol)
3450 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:, :), &
3451 INTENT(INOUT) :: work
3452 INTEGER,
INTENT(IN) :: nrow, ncol
3454 IF (
ALLOCATED(work))
THEN
3455 IF (
SIZE(work, 1) == nrow .AND.
SIZE(work, 2) == ncol)
RETURN
3458 ALLOCATE (work(nrow, ncol))
3460 END SUBROUTINE ensure_work_matrix
3473 for_aux_fit, mixed_aux_orb)
3476 TYPE(
dbcsr_p_type),
DIMENSION(:, :),
POINTER :: overlap_rs
3480 INTEGER,
DIMENSION(:, :, :),
POINTER :: cell_to_index
3481 LOGICAL,
INTENT(IN),
OPTIONAL :: for_aux_fit, mixed_aux_orb
3483 CHARACTER(LEN=*),
PARAMETER :: routinen =
'calibrate_symmetry_phases'
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, &
3497 TYPE(kp_symmetry_op_type) :: op
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
3505 IF (kpoint%phases_calibrated)
RETURN
3507 needs_calibration = .false.
3508 DO ik = 1, kpoint%nkp
3509 kpsym => kpoint%kp_sym(ik)%kpoint_sym
3510 IF (
kpsym%apply_symmetry)
THEN
3512 IF (any(
kpsym%phase_mode(1:
kpsym%nwght) == 0))
THEN
3513 needs_calibration = .true.
3518 IF (.NOT. needs_calibration)
THEN
3519 kpoint%phases_calibrated = .true.
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))
3530 best_score(:) = -1.0_dp
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
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)
3547 cpassert(mode_by_op(key) ==
kpsym%phase_mode(is))
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), &
3557 phase_score = max(phase_score, abs(sin(
twopi*arg)))
3559 IF (phase_score > best_score(key))
THEN
3560 best_score(key) = phase_score
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)
3581 phase_tolerance = max(1.0e-6_dp, 100.0_dp*kpoint%eps_geo)
3583 IF (mode_by_op(key) > 0 .OR. best_ik(key) == 0) cycle
3586 IF (best_score(key) <= 1000.0_dp*epsilon(1.0_dp))
THEN
3593 kpsym => kpoint%kp_sym(ik)%kpoint_sym
3596 CALL rskp_transform(source_r, source_c, overlap_rs, 1, kpoint%xkp(1:3, ik), &
3597 cell_to_index, sab_nl)
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
3606 CALL kp_symmetry_op_init(op, kpoint,
kpsym, is, for_aux_fit=aux_fit, &
3607 mixed_aux_orb=mixed_aux_orb_local)
3609 best_residual = huge(1.0_dp)
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
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))
3632 mode_by_op(key) = best_mode
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
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)
3654 kpoint%phases_calibrated = .true.
3655 CALL timestop(handle)
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
3672 INTEGER,
INTENT(IN) :: is
3673 LOGICAL,
INTENT(IN),
OPTIONAL :: for_aux_fit, mixed_aux_orb
3675 INTEGER :: iatom, rot_slot
3676 LOGICAL :: aux_fit, has_phase, my_mixed, perm, &
3679 REAL(kind=
dp),
DIMENSION(:, :),
POINTER :: rot
3682 IF (
PRESENT(for_aux_fit)) aux_fit = for_aux_fit
3684 IF (
PRESENT(mixed_aux_orb)) my_mixed = mixed_aux_orb
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, :)
3691 op%kind_rot_right => kpoint%kind_rotmat(rot_slot, :)
3693 op%kind_rot_right => kpoint%kind_rotmat_aux(rot_slot, :)
3696 op%kind_rot => kpoint%kind_rotmat(rot_slot, :)
3697 op%kind_rot_right => kpoint%kind_rotmat(rot_slot, :)
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
3706 DO iatom = 1,
SIZE(op%atom_map)
3707 IF (op%atom_map(iatom) == iatom) cycle
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
3718 END SUBROUTINE kp_symmetry_op_init
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
3736 CHARACTER(LEN=*),
PARAMETER :: routinen =
'symtrans_phase'
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, &
3748 CALL timeset(routinen, handle)
3750 IF (op%identity)
THEN
3752 IF (.NOT. real_only)
CALL dbcsr_copy(scpmat, cpmat)
3753 CALL timestop(handle)
3759 IF (numnodes /= 1 .AND. op%redistribute)
THEN
3765 IF (.NOT. real_only)
CALL dbcsr_set(scpmat, 0.0_dp)
3777 shift(:) = op%cell_shift(:, icol) - op%cell_shift(:, irow)
3778 IF (reverse_phase) shift(:) = -shift
3780 CALL kp_symmetry_phase_block(rblock, shift, op%xkp, op%time_reversal, rwork)
3783 IF (.NOT. found)
NULLIFY (cblock)
3784 CALL kp_symmetry_phase_block(rblock, shift, op%xkp, op%time_reversal, rwork, cwork, cblock)
3787 ip = op%atom_map(irow)
3788 jp = op%atom_map(icol)
3793 left_rot => op%kind_rot(op%atom_kind(icol))%rmat
3794 right_rot => op%kind_rot_right(op%atom_kind(irow))%rmat
3796 left_rot => op%kind_rot(op%atom_kind(irow))%rmat
3797 right_rot => op%kind_rot_right(op%atom_kind(icol))%rmat
3800 CALL dbcsr_get_block_p(matrix=srpmat, row=jrow, col=jcol, block=srblock, found=found)
3801 IF (.NOT. found)
THEN
3803 cpassert(owner /= mynode)
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)
3811 CALL kp_symmetry_rotate_block(cwork, left_rot, right_rot, trans, &
3812 merge(-1.0_dp, 1.0_dp, trans), scblock, twork)
3817 IF (numnodes /= 1 .AND. op%redistribute)
THEN
3822 CALL timestop(handle)
3824 END SUBROUTINE symtrans_phase
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)
3840 REAL(kind=
dp) :: arg, cosine, sine
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")
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
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), &
3874 REAL(kind=
dp) :: phase(2, 2)
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
3887 END SUBROUTINE kp_symmetry_phase_block
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
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))
3918 END SUBROUTINE kp_symmetry_rotate_block
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.
subroutine, public real_to_scaled(s, r, cell)
Transform real to scaled cell coordinates. s=h_inv*r.
real(kind=dp) function, dimension(3), public pbc_stable(r, cell)
Apply a stable periodic-image convention for k-point Bloch gauges.
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.
Routines that link DBCSR and CP2K concepts together.
subroutine, public cp_dbcsr_alloc_block_from_nbl(matrix, sab_orb, desymmetrize)
allocate the blocks of a dbcsr based on the neighbor list
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
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.
subroutine, public print_crys_symmetry(csym)
...
subroutine, public kpoint_gen(csym, nk, symm, shift, full_grid, gamma_centered, inversion_symmetry_only, use_spglib_reduction, use_spglib_backend)
...
subroutine, public release_csym_type(csym)
Release the CSYM type.
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.
subroutine, public print_kp_symmetry(csym)
...
subroutine, public crys_sym_gen(csym, scoor, types, hmat, delta, iounit, use_spglib)
...
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
Defines the basic variable types.
integer, parameter, public int_8
integer, parameter, public dp
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.
An array-based list which grows on demand. When the internal array is full, a new array of twice the ...
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.
pure real(kind=dp) function, dimension(3, 3), public inv_3x3(a)
Returns the inverse of the 3 x 3 matrix a.
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.
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.
pure integer function, dimension(2), public get_limit(m, n, me)
divide m entries into n parts, return size of part me
Type defining parameters related to the simulation cell.
represent a blacs multidimensional parallel environment (for the mpi corrispective see cp_paratypes/m...
Represent a complex full matrix.
to create arrays of pools
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
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