Octopus
nonlocal_pseudopotential.F90
Go to the documentation of this file.
1!! Copyright (C) 2009 X. Andrade
2!! Copyright (C) 2026 N. Tancogne-Dejean
3!!
4!! This program is free software; you can redistribute it and/or modify
5!! it under the terms of the GNU General Public License as published by
6!! the Free Software Foundation; either version 2, or (at your option)
7!! any later version.
8!!
9!! This program is distributed in the hope that it will be useful,
10!! but WITHOUT ANY WARRANTY; without even the implied warranty of
11!! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12!! GNU General Public License for more details.
13!!
14!! You should have received a copy of the GNU General Public License
15!! along with this program; if not, write to the Free Software
16!! Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17!! 02110-1301, USA.
18!!
19
20#include "global.h"
21
23 use accel_oct_m
26 use batch_oct_m
28 use blas_oct_m
29 use comm_oct_m
30 use debug_oct_m
33 use epot_oct_m
34 use global_oct_m
35 use grid_oct_m
38 use ions_oct_m
45 use math_oct_m
46 use mesh_oct_m
49 use mpi_oct_m
51 use phase_oct_m
55 use ps_oct_m
58 use space_oct_m
63 use types_oct_m
65
66 implicit none
67
68 private
69
70 public :: &
97
101 private
102 type(projector_matrix_t), allocatable, public :: projector_matrices(:)
103 integer, public :: nprojector_matrices
104 logical, public :: apply_projector_matrices
105 logical, public :: has_non_local_potential
106 integer, public :: full_projection_size
107 integer, public :: max_npoints
108 integer, public :: total_points
109 integer, public :: max_nprojs
110 logical :: projector_mix
111 complex(real64), allocatable, public :: projector_phases(:, :, :, :)
112 integer, allocatable, public :: projector_to_atom(:)
113 integer :: nregions
114 integer, allocatable :: regions(:)
115 integer, public :: nphase
120 type(accel_mem_t) :: buff_offsets
121 type(accel_mem_t) :: buff_matrices
122 type(accel_mem_t) :: buff_maps
123 type(accel_mem_t) :: buff_sphere_weights
124 type(accel_mem_t) :: buff_scals
125 type(accel_mem_t) :: buff_position
126 type(accel_mem_t) :: buff_pos
127 type(accel_mem_t) :: buff_invmap
128 type(accel_mem_t) :: buff_invmap_mat
129 type(accel_mem_t) :: buff_point_to_mat
130 type(accel_mem_t) :: buff_proj_to_mat
131 type(accel_mem_t), public :: buff_projector_phases
132 type(accel_mem_t), pointer, public :: buff_bra_phasepsi => null()
133 type(accel_mem_t), pointer, public :: buff_bra_projection_temp => null()
134 type(accel_mem_t) :: buff_mix
135 logical :: projector_self_overlap
138 type(overlap_matrix_t), public :: ovlp_inv
139 real(real64), pointer, public :: spin(:,:,:) => null()
140 logical, public :: uspp_save_memory = .false.
146 real(real64), allocatable, public :: onsite_density_matrix(:, :, :, :)
149 real(real64), allocatable, public :: onsite_current_matrix(:, :, :, :)
151 real(real64), public :: energy_paw = m_zero
153 type(mpi_grp_t), public :: paw_onsite_grp
155 logical :: ghost_checked = .false.
160 logical, public :: owns_projector_data = .true.
164 real(real64), allocatable :: paw_symm_rot(:, :, :, :)
165 integer, allocatable :: paw_symm_map(:, :)
166 integer :: paw_symm_nops = 0
167 integer :: paw_symm_lmax = -1
168 contains
169
170 procedure :: init => nonlocal_pseudopotential_init
171
173
176 procedure :: has_self_overlap => nonlocal_pseudopotential_self_overlap
178 procedure :: dstart => dnonlocal_pseudopotential_start
180 procedure :: zstart => znonlocal_pseudopotential_start
182 procedure :: dfinish => dnonlocal_pseudopotential_finish
184 procedure :: zfinish => znonlocal_pseudopotential_finish
186 procedure :: dforce => dnonlocal_pseudopotential_force
188 procedure :: zforce => znonlocal_pseudopotential_force
189
190 procedure :: dforce_overlap_cross => dnonlocal_pseudopotential_force_overlap_cross
191
192 procedure :: zforce_overlap_cross => znonlocal_pseudopotential_force_overlap_cross
194 procedure :: dposition_commutator => dnonlocal_pseudopotential_position_commutator
196 procedure :: zposition_commutator => znonlocal_pseudopotential_position_commutator
198 procedure :: dr_vn_local => dnonlocal_pseudopotential_r_vnlocal
200 procedure :: zr_vn_local => znonlocal_pseudopotential_r_vnlocal
208 private
209 real(real64), allocatable, public :: dprojection(:, :)
210 complex(real64), allocatable, public :: zprojection(:, :)
211 type(accel_mem_t) :: buff_projection
212 type(accel_mem_t) :: buff_spin_to_phase
213 integer :: cuda_stream_projection_dth
214 end type projection_t
215
216contains
217
218 ! ---------------------------------------------------------
222 class(nonlocal_pseudopotential_t), intent(inout) :: this
226 this%apply_projector_matrices = .false.
227 this%has_non_local_potential = .false.
228 this%nprojector_matrices = 0
229
230 this%projector_self_overlap = .false.
231
233 end subroutine nonlocal_pseudopotential_init
234
235 ! ---------------------------------------------------------
240 subroutine nonlocal_pseudopotential_rebind_projectors(this, epot)
241 class(nonlocal_pseudopotential_t), target, intent(inout) :: this
242 type(epot_t), target, intent(in) :: epot
243
244 integer :: imat, iatom
245 type(projector_matrix_t), pointer :: pmat
249 ! After the assignment these still point to the buffers of the source: drop them, do not free them
250 nullify(this%buff_bra_phasepsi)
251 nullify(this%buff_bra_projection_temp)
252 safe_allocate(this%buff_bra_phasepsi)
253 safe_allocate(this%buff_bra_projection_temp)
255 if (.not. allocated(this%projector_matrices) .or. this%nprojector_matrices == 0) then
257 return
258 end if
259
260 assert(allocated(this%projector_to_atom))
261 assert(size(this%projector_matrices) >= this%nprojector_matrices)
262 assert(size(this%projector_to_atom) >= this%nprojector_matrices)
263
264 do imat = 1, this%nprojector_matrices
265 pmat => this%projector_matrices(imat)
266 iatom = this%projector_to_atom(imat)
267
268 assert(iatom >= 1 .and. iatom <= epot%natoms)
269 assert(allocated(epot%proj))
270 assert(allocated(epot%proj(iatom)%sphere%map))
271 assert(allocated(epot%proj(iatom)%sphere%rel_x))
273 pmat%map => epot%proj(iatom)%sphere%map
274 pmat%position => epot%proj(iatom)%sphere%rel_x
275 end do
276
280
282 !--------------------------------------------------------
292 class(nonlocal_pseudopotential_t), target, intent(inout) :: this
293 class(nonlocal_pseudopotential_t), target, intent(in) :: donor
294
295 integer :: imat
296 type(projector_matrix_t), pointer :: pmat, dmat
297
299
300 assert(.not. accel_is_enabled())
301
302 if (.not. allocated(this%projector_matrices)) then
304 return
305 end if
306 assert(allocated(donor%projector_matrices))
307 assert(this%nprojector_matrices == donor%nprojector_matrices)
308 assert(this%owns_projector_data)
309 assert(donor%owns_projector_data)
310
311 do imat = 1, this%nprojector_matrices
312 pmat => this%projector_matrices(imat)
313 dmat => donor%projector_matrices(imat)
314 ! The instances must enumerate their matrices in the same atom order for the
315 ! index-wise aliasing to be valid.
316 assert(this%projector_to_atom(imat) == donor%projector_to_atom(imat))
317 assert(pmat%npoints == dmat%npoints .and. pmat%nprojs == dmat%nprojs)
318 assert(pmat%is_cmplx .eqv. dmat%is_cmplx)
319
320 ! aug_ylm is not shared
321 safe_deallocate_p(pmat%dprojectors)
322 safe_deallocate_p(pmat%zprojectors)
323 pmat%dprojectors => dmat%dprojectors
324 pmat%zprojectors => dmat%zprojectors
325 end do
326 this%owns_projector_data = .false.
327
330
331 !--------------------------------------------------------
339 subroutine nonlocal_pseudopotential_detach_copy(this, donor)
340 class(nonlocal_pseudopotential_t), target, intent(inout) :: this
342 class(nonlocal_pseudopotential_t), optional, intent(in) :: donor
343
344 integer :: imat
345 type(projector_matrix_t), pointer :: pmat
346 real(real64), pointer, contiguous :: dsrc(:, :)
347 complex(real64), pointer, contiguous :: zsrc(:, :)
348
350
351 assert(this%owns_projector_data .or. present(donor))
352 if (allocated(this%projector_matrices)) then
353 do imat = 1, this%nprojector_matrices
354 pmat => this%projector_matrices(imat)
355 nullify(pmat%aug_ylm)
356 if (this%owns_projector_data) then
357 dsrc => pmat%dprojectors
358 zsrc => pmat%zprojectors
359 safe_allocate_source_p(pmat%dprojectors, dsrc)
360 safe_allocate_source_p(pmat%zprojectors, zsrc)
361 else
362 pmat%dprojectors => donor%projector_matrices(imat)%dprojectors
363 pmat%zprojectors => donor%projector_matrices(imat)%zprojectors
364 end if
365 end do
366 end if
367
370
371 !--------------------------------------------------------
374 class(nonlocal_pseudopotential_t), intent(inout) :: this
375
376 integer :: iproj
377
379
380 if (allocated(this%projector_matrices)) then
381
382 if (accel_is_enabled()) then
383 call accel_free_buffer(this%buff_offsets)
384 call accel_free_buffer(this%buff_matrices)
385 call accel_free_buffer(this%buff_maps)
386 if (accel_buffer_is_allocated(this%buff_sphere_weights)) call accel_free_buffer(this%buff_sphere_weights)
387 call accel_free_buffer(this%buff_scals)
388 call accel_free_buffer(this%buff_position)
389 call accel_free_buffer(this%buff_pos)
390 call accel_free_buffer(this%buff_invmap)
391 call accel_free_buffer(this%buff_invmap_mat)
392 call accel_free_buffer(this%buff_point_to_mat)
393 call accel_free_buffer(this%buff_proj_to_mat)
394 if (this%projector_mix) call accel_free_buffer(this%buff_mix)
395 if (allocated(this%projector_phases)) call accel_free_buffer(this%buff_projector_phases)
396 end if
397
398 do iproj = 1, this%nprojector_matrices
399 call projector_matrix_deallocate(this%projector_matrices(iproj), owns_data = this%owns_projector_data)
400 end do
401 this%owns_projector_data = .true.
402 safe_deallocate_a(this%regions)
403 safe_deallocate_a(this%projector_matrices)
404 safe_deallocate_a(this%projector_phases)
405 safe_deallocate_a(this%projector_to_atom)
406 end if
407
408 if (associated(this%buff_bra_phasepsi)) then
409 if (accel_buffer_is_allocated(this%buff_bra_phasepsi)) call accel_free_buffer(this%buff_bra_phasepsi)
410 end if
411 if (associated(this%buff_bra_projection_temp)) then
412 if (accel_buffer_is_allocated(this%buff_bra_projection_temp)) call accel_free_buffer(this%buff_bra_projection_temp)
413 end if
414 safe_deallocate_p(this%buff_bra_phasepsi)
415 safe_deallocate_p(this%buff_bra_projection_temp)
416
417 call this%ovlp_inv%end()
418
419 safe_deallocate_a(this%onsite_density_matrix)
420 safe_deallocate_a(this%onsite_current_matrix)
421 safe_deallocate_a(this%paw_symm_rot)
422 safe_deallocate_a(this%paw_symm_map)
423 this%paw_symm_nops = 0
424 this%paw_symm_lmax = -1
425 this%energy_paw = m_zero
426
429
430 !-----------------------------------------------------------------
436 !
437 subroutine nonlocal_pseudopotential_build_proj(this, space, mesh, epot, ispin, overlap_operator, inverse_overlap_operator)
438 class(nonlocal_pseudopotential_t), target, intent(inout) :: this
439 class(space_t), intent(in) :: space
440 class(mesh_t), intent(in) :: mesh
441 type(epot_t), target, intent(in) :: epot
442 integer, intent(in) :: ispin
443 logical, optional, intent(in) :: overlap_operator
444 logical, optional, intent(in) :: inverse_overlap_operator
446 integer :: iatom, iproj, ll, lmax, lloc, mm, ic, jc
447 integer :: nmat, imat, ip, iorder, dmix_channels, dm_nspin
448 integer :: nregion, jatom, katom, iregion
449 integer, allocatable :: order(:), head(:), region_count(:)
450 logical, allocatable :: atom_counted(:)
451 logical :: overlap, overlap_operator_, inverse_operator_, mix_is_uspp, spinors_, uspp_spinor_dmatrix
452 type(projector_matrix_t), pointer :: pmat
453 type(kb_projector_t), pointer :: kb_p
454 type(rkb_projector_t), pointer :: rkb_p
455 type(hgh_projector_t), pointer :: hgh_p
456
458
459 overlap_operator_ = optional_default(overlap_operator, .false.)
460 inverse_operator_ = optional_default(inverse_overlap_operator, .false.)
461 spinors_ = (ispin == spinors)
462 ! The ultrasoft D coupling is a complex 2x2 spin matrix for spinors
463 uspp_spinor_dmatrix = spinors_ .and. .not. (overlap_operator_ .or. inverse_operator_)
464 ! Both the overlap and the inverse-overlap operators act only on the USPP
465 ! augmentation projectors (the identity part is handled by the caller).
466 mix_is_uspp = overlap_operator_ .or. inverse_operator_
467 ! With collinear spin polarization the screened ultrasoft coupling D_ij^sigma =
468 ! D_ij^(0) + int V_eff^sigma Q_ij is spin-dependent, so the Hamiltonian (D) matrix carries
469 ! one channel per spin. The overlap / inverse-overlap charges (q_ij / -T_ij) are not screened
470 ! and stay spin-independent.
471 dmix_channels = 1
472 if (ispin == spin_polarized .and. .not. mix_is_uspp) dmix_channels = 2
473
474 call profiling_in("ATOM_COLORING")
475
476 ! this is most likely a very inefficient algorithm, O(natom**2) or
477 ! O(natom**3), probably it should be replaced by something better.
478
479 safe_allocate(order(1:epot%natoms)) ! order(iregion) = ?
480 safe_allocate(head(1:epot%natoms + 1)) ! head(iregion) points to the first atom in region iregion
481 safe_allocate(region_count(1:epot%natoms)) ! region_count(iregion): number of atoms in that region
482 safe_allocate(atom_counted(1:epot%natoms))
483
484 this%projector_self_overlap = .false.
485 atom_counted = .false.
486 order = -1
487
488 head(1) = 1
489 nregion = 0
490 do
491 nregion = nregion + 1
492 assert(nregion <= epot%natoms)
493
494 region_count(nregion) = 0
495
496 do iatom = 1, epot%natoms
497 if (atom_counted(iatom)) cycle
498
499 overlap = .false.
500
501 if (.not. projector_is(epot%proj(iatom), proj_none)) then
502 assert(associated(epot%proj(iatom)%sphere%mesh))
503 do jatom = 1, region_count(nregion)
504 katom = order(head(nregion) + jatom - 1)
505 if (projector_is(epot%proj(katom), proj_none)) cycle
506 overlap = submesh_overlap(epot%proj(iatom)%sphere, epot%proj(katom)%sphere, space)
507 if (overlap) exit
508 end do
509 end if
510
511 if (.not. overlap) then
512 ! iatom did not overlap with any previously counted atoms:
513 ! iatom will be added to the current region
514 region_count(nregion) = region_count(nregion) + 1
515 order(head(nregion) - 1 + region_count(nregion)) = iatom
516 atom_counted(iatom) = .true.
517 end if
518
519 end do
520
521 head(nregion + 1) = head(nregion) + region_count(nregion)
522
523 if (all(atom_counted)) exit
524 end do
525
526 safe_deallocate_a(atom_counted)
527 safe_deallocate_a(region_count)
528
529 call messages_write('The atoms can be separated in ')
530 call messages_write(nregion)
531 call messages_write(' non-overlapping groups.')
532 call messages_info(debug_only=.true.)
533
534 do iregion = 1, nregion
535 do iatom = head(iregion), head(iregion + 1) - 1
536 if (.not. projector_is(epot%proj(order(iatom)), proj_kb)) cycle
537 do jatom = head(iregion), iatom - 1
538 if (.not. projector_is(epot%proj(order(jatom)), proj_kb)) cycle
539 assert(.not. submesh_overlap(epot%proj(order(iatom))%sphere, epot%proj(order(jatom))%sphere, space))
540 end do
541 end do
542 end do
543
544 call profiling_out("ATOM_COLORING")
545
546 ! deallocate previous projectors
547 call this%end()
548
549 ! The two bra scratch buffers. They are pointers so that
550 ! X(nonlocal_pseudopotential_start), whose `this` is intent(in), can fill them.
551 safe_allocate(this%buff_bra_phasepsi)
552 safe_allocate(this%buff_bra_projection_temp)
553
554 ! count projectors
555 this%nprojector_matrices = 0
556 this%apply_projector_matrices = .false.
557 this%has_non_local_potential = .false.
558 this%nregions = nregion
559
560 !We determine if we have only local potential or not.
561 do iorder = 1, epot%natoms
562 iatom = order(iorder)
563
564 if (projector_counted(epot%proj(iatom))) then
565 this%has_non_local_potential = .true.
566 exit
567 end if
568 end do
569
570 do iorder = 1, epot%natoms
571 iatom = order(iorder)
572
573 if (projector_counted(epot%proj(iatom))) then
574 this%nprojector_matrices = this%nprojector_matrices + 1
575 this%apply_projector_matrices = .true.
576 end if
577 end do
578
579 ! This is currently the only not supported case
580 if (mesh%use_curvilinear) this%apply_projector_matrices = .false.
581
582 if (.not. this%apply_projector_matrices) then
583 safe_deallocate_a(order)
584 safe_deallocate_a(head)
585
587 return
588 end if
589
590
591 safe_allocate(this%projector_matrices(1:this%nprojector_matrices))
592 safe_allocate(this%regions(1:this%nregions + 1))
593 safe_allocate(this%projector_to_atom(1:epot%natoms))
594
595 this%full_projection_size = 0
596 this%regions(this%nregions + 1) = this%nprojector_matrices + 1
597
598 this%projector_mix = .false.
599
600 iproj = 0
601 do iregion = 1, this%nregions
602 this%regions(iregion) = iproj + 1
603 do iorder = head(iregion), head(iregion + 1) - 1
604
605 iatom = order(iorder)
606
607 if (.not. projector_counted(epot%proj(iatom))) cycle
608
609 iproj = iproj + 1
610
611 pmat => this%projector_matrices(iproj)
612
613 this%projector_to_atom(iproj) = iatom
614
615 lmax = epot%proj(iatom)%lmax
616 lloc = epot%proj(iatom)%lloc
617
618 if (projector_is(epot%proj(iatom), proj_kb)) then
619
620 ! count the number of projectors for this matrix
621 nmat = 0
622 do ll = 0, lmax
623 if (ll == lloc) cycle
624 do mm = -ll, ll
625 nmat = nmat + epot%proj(iatom)%kb_p(ll, mm)%n_c
626 end do
627 end do
628
629 call projector_matrix_allocate(pmat, nmat, epot%proj(iatom)%sphere, has_mix_matrix = .false.)
630
631 ! generate the matrix
632 pmat%dprojectors = m_zero
633 imat = 1
634 do ll = 0, lmax
635 if (ll == lloc) cycle
636 do mm = -ll, ll
637 kb_p => epot%proj(iatom)%kb_p(ll, mm)
638 do ic = 1, kb_p%n_c
639 call lalg_copy(pmat%npoints, kb_p%p(:, ic), pmat%dprojectors(:, imat))
640 ! the per-point part of the volume element is applied on the bra
641 ! side (sphere_weights) for meshes with per-point volumes
642 pmat%scal(imat) = kb_p%e(ic)*mesh%volume_element
643 pmat%l(imat) = ll
644 pmat%m(imat) = mm
645 pmat%channel(imat) = ic
646 pmat%beta_index(imat) = projector_beta_index(epot%proj(iatom), ll, ic)
647 imat = imat + 1
648 end do
649 end do
650 end do
651
652 this%projector_self_overlap = this%projector_self_overlap .or. epot%proj(iatom)%sphere%overlap
653
654 else if (projector_is(epot%proj(iatom), proj_uspp) .and. epot%reltype == spin_orbit) then
655
656 this%projector_mix = .true.
657
658 nmat = 0
659 do ll = 0, lmax
660 if (ll == lloc) cycle
661 do mm = -ll, ll
662 nmat = nmat + epot%proj(iatom)%kb_p(ll, mm)%n_c
663 end do
664 end do
665
666 call projector_matrix_allocate(pmat, nmat, epot%proj(iatom)%sphere, has_mix_matrix = .true., &
667 is_cmplx = .true.)
668 pmat%zprojectors = m_z0
669 pmat%zmix = m_z0
670 safe_allocate(pmat%qmix(1:nmat, 1:nmat))
671 pmat%qmix = m_zero
672
673 ! Lay out the complex projectors, ordered (l, m, channel).
674 imat = 1
675 do ll = 0, lmax
676 if (ll == lloc) cycle
677 do mm = -ll, ll
678 kb_p => epot%proj(iatom)%kb_p(ll, mm)
679 do ic = 1, kb_p%n_c
680 if (ll == 0) then
681 ! l = 0 has no spin-orbit splitting: Y_00 is real, use the KB projector.
682 pmat%zprojectors(1:pmat%npoints, imat - 1 + ic) = &
683 cmplx(kb_p%p(1:pmat%npoints, ic), m_zero, real64)
684 else
685 pmat%zprojectors(1:pmat%npoints, imat - 1 + ic) = &
686 epot%proj(iatom)%rkb_p(ll, mm)%ket(1:pmat%npoints, ic, 1, 1)
687 end if
688 pmat%scal(imat - 1 + ic) = mesh%volume_element
689 pmat%l(imat - 1 + ic) = ll
690 pmat%m(imat - 1 + ic) = mm
691 pmat%channel(imat - 1 + ic) = ic
692 pmat%beta_index(imat - 1 + ic) = projector_beta_index(epot%proj(iatom), ll, ic)
693 do jc = 1, kb_p%n_c
694 pmat%qmix(imat - 1 + ic, imat - 1 + jc) = epot%proj(iatom)%aug%qij(ll, ic, jc)
695 end do
696 end do
697 imat = imat + kb_p%n_c
698 end do
699 end do
700
701 ! Build the spin coupling zmix: the overlap operator carries q_ij, the Hamiltonian D_ij.
702 call nonlocal_pseudopotential_soc_build_zmix_cmplx(pmat, epot%proj(iatom), &
703 use_qij = (overlap_operator_ .or. inverse_operator_), so_strength = epot%so_strength)
704 if (inverse_operator_) call build_inverse_overlap_sim_grid(pmat, mesh)
705
706 this%projector_self_overlap = this%projector_self_overlap .or. epot%proj(iatom)%sphere%overlap
707
708 else if (projector_is(epot%proj(iatom), proj_uspp)) then
709
710 this%projector_mix = .true.
711
712 ! count the number of projectors for this matrix
713 nmat = 0
714 do ll = 0, lmax
715 if (ll == lloc) cycle
716 do mm = -ll, ll
717 nmat = nmat + epot%proj(iatom)%kb_p(ll, mm)%n_c
718 end do
719 end do
720
721 call projector_matrix_allocate(pmat, nmat, epot%proj(iatom)%sphere, has_mix_matrix = .true., &
722 complex_mix = uspp_spinor_dmatrix, nspin_channels = dmix_channels)
723
724 ! generate the matrix. Ultrasoft uses the same radial projectors as KB,
725 ! but keeps the full coupling matrix for each angular channel. The
726 ! Hamiltonian path uses D_ij, while the overlap path uses Q_ij.
727 pmat%dprojectors = m_zero
728 if (allocated(pmat%dmix)) pmat%dmix = m_zero
729 if (allocated(pmat%zmix)) pmat%zmix = m_zero
730 ! The overlap charges q_ij are needed to build the inverse overlap on the simulation grid.
731 ! The forces also need them on the Hamiltonian (D) projector matrix to build the
732 ! per-state coupling (D_ij - eps_n q_ij) of the orthonormality force.
733 ! Carry them in qmix regardless of which operator this matrix represents.
734 safe_allocate(pmat%qmix(1:nmat, 1:nmat))
735 pmat%qmix = m_zero
736 imat = 1
737 do ll = 0, lmax
738 if (ll == lloc) cycle
739 do mm = -ll, ll
740 kb_p => epot%proj(iatom)%kb_p(ll, mm)
741 do ic = 1, kb_p%n_c
742 call lalg_copy(pmat%npoints, kb_p%p(:, ic), pmat%dprojectors(:, imat - 1 + ic))
743 pmat%scal(imat - 1 + ic) = mesh%volume_element
744 pmat%l(imat - 1 + ic) = ll
745 pmat%m(imat - 1 + ic) = mm
746 pmat%channel(imat - 1 + ic) = ic
747 pmat%beta_index(imat - 1 + ic) = projector_beta_index(epot%proj(iatom), ll, ic)
748 do jc = 1, kb_p%n_c
749 pmat%qmix(imat - 1 + ic, imat - 1 + jc) = epot%proj(iatom)%aug%qij(ll, ic, jc)
750 if (inverse_operator_) then
751 ! S^{-1} mix matrix (-T_ij) is built below on the simulation grid.
752 pmat%dmix(imat - 1 + ic, imat - 1 + jc, 1) = m_zero
753 else if (overlap_operator_) then
754 pmat%dmix(imat - 1 + ic, imat - 1 + jc, 1) = epot%proj(iatom)%aug%qij(ll, ic, jc)
755 else if (uspp_spinor_dmatrix) then
756 ! Bare D^(0) is spin-diagonal (no SOC): fill the up-up and down-down blocks.
757 pmat%zmix(imat - 1 + ic, imat - 1 + jc, 1) = epot%proj(iatom)%aug%dij(ll, ic, jc)
758 pmat%zmix(imat - 1 + ic, imat - 1 + jc, 2) = epot%proj(iatom)%aug%dij(ll, ic, jc)
759 else
760 ! Bare D^(0) is spin-independent; the spin-dependent screening is added in
761 ! nonlocal_pseudopotential_update_dij once V_eff is available.
762 pmat%dmix(imat - 1 + ic, imat - 1 + jc, :) = epot%proj(iatom)%aug%dij(ll, ic, jc)
763 end if
764 end do
765 end do
766 imat = imat + kb_p%n_c
767 end do
768 end do
769
770 ! Build the inverse-overlap weight T = q (1 + M q)^{-1} with the projector overlap M
771 ! evaluated on the SIMULATION grid, so that S^{-1} = 1 - sum |beta_i> T_ij <beta_j| is the
772 ! exact inverse of the discretized overlap S = 1 + sum |beta_i> q_ij <beta_j| used in the
773 ! eigenproblem. Evaluating M on the pseudopotential radial grid instead would leave a
774 ! grid-dependent mismatch with the discretized S, capping the attainable eigensolver
775 ! residual and stalling the Chebyshev filter.
776 if (inverse_operator_) then
777 call build_inverse_overlap_sim_grid(pmat, mesh)
778 end if
779
780 this%projector_self_overlap = this%projector_self_overlap .or. epot%proj(iatom)%sphere%overlap
781
782 else if (projector_is(epot%proj(iatom), proj_hgh)) then
783
784 this%projector_mix = .true.
785
786 ! count the number of projectors for this matrix
787 nmat = 0
788 do ll = 0, lmax
789 if (ll == lloc) cycle
790 do mm = -ll, ll
791 nmat = nmat + 3
792 end do
793 end do
794
795 call projector_matrix_allocate(pmat, nmat, epot%proj(iatom)%sphere, &
796 has_mix_matrix = .true., is_cmplx = (epot%proj_reltype == spin_orbit))
797
798 ! generate the matrix
799 if (epot%proj_reltype == spin_orbit) then
800 pmat%zprojectors = m_zero
801 pmat%zmix = m_zero
802 else
803 pmat%dprojectors = m_zero
804 pmat%dmix = m_zero
805 end if
806
807 imat = 1
808 do ll = 0, lmax
809 if (ll == lloc) cycle
810 do mm = -ll, ll
811 hgh_p => epot%proj(iatom)%hgh_p(ll, mm)
812
813 ! HGH pseudos mix different components, so we need to
814 ! generate a matrix that mixes the projections
815 if (epot%proj_reltype == spin_orbit) then
816 do ic = 1, 3
817 do jc = 1, 3
818 pmat%zmix(imat - 1 + ic, imat - 1 + jc, 1) = hgh_p%h(ic, jc) + m_half*mm*hgh_p%k(ic, jc)
819 pmat%zmix(imat - 1 + ic, imat - 1 + jc, 2) = hgh_p%h(ic, jc) - m_half*mm*hgh_p%k(ic, jc)
820
821 if (mm < ll) then
822 pmat%zmix(imat - 1 + ic, imat + 3 - 1 + jc, 3) = m_half*hgh_p%k(ic, jc) * &
823 sqrt(real(ll*(ll+1)-mm*(mm+1), real64))
824 end if
825
826 if (-mm < ll) then
827 pmat%zmix(imat - 1 + ic, imat - 3 - 1 + jc, 4) = m_half*hgh_p%k(ic, jc) * &
828 sqrt(real(ll*(ll+1)-mm*(mm-1), real64))
829 end if
830 end do
831 end do
832 else
833 do ic = 1, 3
834 do jc = 1, 3
835 pmat%dmix(imat - 1 + ic, imat - 1 + jc, 1) = hgh_p%h(ic, jc)
836 end do
837 end do
838 end if
839
840 do ic = 1, 3
841 if (epot%proj_reltype == spin_orbit) then
842 call lalg_copy(pmat%npoints, hgh_p%zp(:, ic), pmat%zprojectors(:, imat))
843 else
844 call lalg_copy(pmat%npoints, hgh_p%dp(:, ic), pmat%dprojectors(:, imat))
845 end if
846 pmat%scal(imat) = mesh%volume_element
847 imat = imat + 1
848 end do
849
850 end do
851 end do
852
853 this%projector_self_overlap = this%projector_self_overlap .or. epot%proj(iatom)%sphere%overlap
854
855 else if (projector_is(epot%proj(iatom), proj_rkb)) then
856 assert(epot%proj_reltype == spin_orbit)
857
858 this%projector_mix = .true.
859
860 ! count the number of projectors for this matrix
861 nmat = 0
862 if (lloc /= 0) nmat = nmat + epot%proj(iatom)%kb_p(1, 1)%n_c
863
864 do ll = 1, lmax
865 if (ll == lloc) cycle
866 do mm = -ll, ll
867 nmat = nmat + epot%proj(iatom)%rkb_p(ll, mm)%n_c
868 end do
869 end do
870
871 call projector_matrix_allocate(pmat, nmat, epot%proj(iatom)%sphere, &
872 has_mix_matrix = .true., is_cmplx = .true.)
873
874 pmat%zprojectors = m_zero
875 pmat%zmix = m_zero
876
877 imat = 1
878 if (lloc /= 0) then
879 kb_p => epot%proj(iatom)%kb_p(1, 1)
880
881 do ic = 1, kb_p%n_c
882 pmat%zmix(ic, ic, 1:2) = kb_p%e(ic)
883 pmat%zprojectors(1:pmat%npoints, ic) = kb_p%p(1:pmat%npoints, ic)
884 pmat%scal(ic) = mesh%volume_element
885 end do
886 imat = kb_p%n_c + 1
887 nullify(kb_p)
888 end if
889
890 do ll = 1, lmax
891 if (ll == lloc) cycle
892 do mm = -ll, ll
893 rkb_p => epot%proj(iatom)%rkb_p(ll, mm)
894
895 ! See rkb_projector.F90 for understanding the indices
896 do ic = 0, rkb_p%n_c/2-1
897 pmat%zmix(imat + ic*2, imat + ic*2, 1) = rkb_p%f(ic*2+1, 1, 1)
898 pmat%zmix(imat + ic*2, imat + ic*2, 2) = rkb_p%f(ic*2+1, 2, 2)
899
900 pmat%zmix(imat + ic*2+1, imat + ic*2+1, 1) = rkb_p%f(ic*2+2, 1, 1)
901 pmat%zmix(imat + ic*2+1, imat + ic*2+1, 2) = rkb_p%f(ic*2+2, 2, 2)
902
903 if (mm < ll) then
904 pmat%zmix(imat + ic*2+rkb_p%n_c, imat + ic*2, 4) = rkb_p%f(ic*2+1, 2, 1)
905 pmat%zmix(imat + ic*2+1+rkb_p%n_c, imat + ic*2+1, 4) = rkb_p%f(ic*2+2, 2, 1)
906 end if
907
908 if (-mm < ll) then
909 pmat%zmix(imat + ic*2-rkb_p%n_c, imat + ic*2, 3) = rkb_p%f(ic*2+1, 1, 2)
910 pmat%zmix(imat + ic*2+1-rkb_p%n_c, imat + ic*2+1, 3) = rkb_p%f(ic*2+2, 1, 2)
911 end if
912 end do
913
914 do ic = 1, rkb_p%n_c
915 call lalg_copy(pmat%npoints, rkb_p%ket(:, ic, 1, 1), pmat%zprojectors(:, imat))
916 pmat%scal(imat) = mesh%volume_element
917 imat = imat + 1
918 end do
919 end do
920
921 nullify(rkb_p)
922 end do
923
924 this%projector_self_overlap = this%projector_self_overlap .or. epot%proj(iatom)%sphere%overlap
925
926 else
927 cycle
928 end if
929
930 pmat%map => epot%proj(iatom)%sphere%map
931 pmat%position => epot%proj(iatom)%sphere%rel_x
932
933 pmat%regions = epot%proj(iatom)%sphere%regions
934
935 this%full_projection_size = this%full_projection_size + pmat%nprojs
936
937 ! the per-point volume weights of the sphere, applied to the bra side
938 if (mesh%use_vol_pp) then
939 safe_allocate(pmat%sphere_weights(1:max(pmat%npoints, 1)))
940 do ip = 1, pmat%npoints
941 pmat%sphere_weights(ip) = mesh%vol_pp(pmat%map(ip))
942 end do
943 end if
944
945 end do
946 end do
947
948 if (mesh%parallel_in_domains) then
949 call mesh%mpi_grp%allreduce_inplace(this%projector_self_overlap, 1, mpi_logical, mpi_lor)
950 end if
951
952 ! For the S^{-1} operator, detect projector spheres that share grid points (distinct atoms or periodic self-overlap).
953 ! When they do, the per-atom (block-diagonal) inverse is only
954 ! approximate and we switch to the exact inverse assembled per connected cluster.
955 if (inverse_operator_) then
956 call this%ovlp_inv%detect(this%nprojector_matrices, this%projector_matrices, &
957 this%projector_to_atom, epot, space, this%projector_self_overlap)
958 end if
959
960 safe_deallocate_a(order)
961 safe_deallocate_a(head)
962
963 this%total_points = 0
964 this%max_npoints = 0
965 this%max_nprojs = 0
966 do imat = 1, this%nprojector_matrices
967 pmat => this%projector_matrices(imat)
968
969 this%max_npoints = max(this%max_npoints, pmat%npoints)
970 this%max_nprojs = max(this%max_nprojs, pmat%nprojs)
971 this%total_points = this%total_points + pmat%npoints
972 end do
973
974 ! PAW: allocate the one-center density-matrix accumulator
975 safe_deallocate_a(this%onsite_density_matrix)
976 safe_deallocate_a(this%onsite_current_matrix)
977 if (overlap_operator_ .and. epot%has_paw .and. this%nprojector_matrices > 0) then
978 dm_nspin = 1
979 if (ispin == spin_polarized) dm_nspin = 2
980 if (ispin == spinors) dm_nspin = 4
981 safe_allocate(this%onsite_density_matrix(1:this%max_nprojs, 1:this%max_nprojs, 1:this%nprojector_matrices, 1:dm_nspin))
982 this%onsite_density_matrix = m_zero
983 ! One-center current matrices: Im[rho_ij] per collinear spin, or the four antisymmetric
984 ! spinor slabs (see nonlocal_pseudopotential_spinor_current_slabs)
985 safe_allocate(this%onsite_current_matrix(1:this%max_nprojs, 1:this%max_nprojs, 1:this%nprojector_matrices, 1:dm_nspin))
986 this%onsite_current_matrix = m_zero
987 end if
988
989 if (accel_is_enabled()) then
992 end if
993
995
996 contains
997
998 ! ----------------------------------------------------------------------------------
999 logical function projector_counted(proj) result(counted)
1000 type(projector_t), intent(in) :: proj
1001
1002 if (mix_is_uspp) then
1003 counted = projector_is(proj, proj_uspp)
1004 else
1005 counted = .not. projector_is_null(proj)
1006 end if
1007 end function projector_counted
1008
1009 !--------------------------------------------------------
1018 subroutine build_inverse_overlap_sim_grid(pmat, mesh)
1019 type(projector_matrix_t), intent(inout) :: pmat
1020 class(mesh_t), intent(in) :: mesh
1021
1022 integer :: nn, sdim, n2, s1, s2, ia
1023 integer :: blkmap(2, 2)
1024 real(real64) :: min_eig
1025 complex(real64), allocatable :: mmat(:, :), qmat(:, :), amat(:, :), tmat(:, :)
1026 complex(real64), allocatable :: eamat(:, :), evals(:)
1027
1029
1030 nn = pmat%nprojs
1031 ! A complex mix matrix carries the spin-coupled (fully-relativistic) overlap charges.
1032 sdim = 1
1033 if (allocated(pmat%zmix)) sdim = 2
1034 n2 = sdim*nn
1035 blkmap(1, 1) = 1
1036 blkmap(2, 2) = 2
1037 blkmap(1, 2) = 3
1038 blkmap(2, 1) = 4
1039
1040 ! Gram matrix of the discretized projectors, M_ij = vol * sum_p conjg(beta_i) beta_j
1041 ! (Hermitian; real for real projectors, complex for the fully-relativistic Y_lm ones).
1042 safe_allocate(mmat(1:nn, 1:nn))
1043 if (pmat%npoints > 0) then
1044 if (associated(pmat%zprojectors)) then
1045 mmat = matmul(conjg(transpose(pmat%zprojectors(1:pmat%npoints, 1:nn))), &
1046 pmat%zprojectors(1:pmat%npoints, 1:nn))
1047 else
1048 mmat = matmul(transpose(pmat%dprojectors(1:pmat%npoints, 1:nn)), &
1049 pmat%dprojectors(1:pmat%npoints, 1:nn))
1050 end if
1051 else
1052 mmat = m_z0
1053 end if
1054 ! the augmentation sphere may be split across domains
1055 if (mesh%parallel_in_domains) call mesh%allreduce(mmat, dim = (/nn, nn/))
1056 mmat = mmat * mesh%volume_element
1057
1058 ! Assemble Qtilde (sdim*n x sdim*n); index (spin, projector) as (s-1)*n + i.
1059 safe_allocate(qmat(1:n2, 1:n2))
1060 safe_allocate(amat(1:n2, 1:n2))
1061 safe_allocate(tmat(1:n2, 1:n2))
1062 if (sdim == 1) then
1063 qmat(1:nn, 1:nn) = pmat%qmix(1:nn, 1:nn)
1064 else
1065 safe_allocate(pmat%zqmix(1:nn, 1:nn, 1:4))
1066 pmat%zqmix(1:nn, 1:nn, 1:4) = pmat%zmix(1:nn, 1:nn, 1:4)
1067 do s2 = 1, 2
1068 do s1 = 1, 2
1069 qmat((s1-1)*nn + 1:s1*nn, (s2-1)*nn + 1:s2*nn) = pmat%zmix(1:nn, 1:nn, blkmap(s1, s2))
1070 end do
1071 end do
1072 end if
1073
1074 ! A = 1 + Mtilde Qtilde ; Ttilde = Qtilde A^{-1} ; mix = -Ttilde (SVD pseudoinverse,
1075 ! robust to ill-conditioning). Mtilde = M (x) 1_sdim is block-diagonal, so each spin-row
1076 ! block of A is M times the corresponding block of Qtilde.
1077 do s1 = 1, sdim
1078 amat((s1-1)*nn + 1:s1*nn, :) = matmul(mmat, qmat((s1-1)*nn + 1:s1*nn, :))
1079 end do
1080 do ia = 1, n2
1081 amat(ia, ia) = amat(ia, ia) + m_one
1082 end do
1083
1084 ! Definiteness diagnostic: the nontrivial eigenvalues of the discretized overlap
1085 ! S = 1 + B q (vol B^H) on this sphere are the eigenvalues of A = 1 + M q.
1086 ! If the smallest one is not safely positive, S is not positive definite and
1087 ! every generalized-eigenproblem assumption breaks. Two causes: a too-coarse grid that
1088 ! under-resolves the projectors, or a dataset whose truncated partial-wave basis makes
1089 ! the exact 1 + q <beta_i|beta_j> indefinite already in the continuum
1090 safe_allocate(eamat(1:n2, 1:n2))
1091 safe_allocate(evals(1:n2))
1092 eamat = amat
1093 call lalg_eigensolve_nonh(n2, eamat, evals)
1094 min_eig = minval(real(evals(1:n2), real64))
1095 if (min_eig < 1.0e-3_real64) then
1096 write(message(1), '(a,es12.4)') 'The discretized ultrasoft/PAW overlap operator of an atom has the', min_eig
1097 write(message(2), '(a)') 'smallest eigenvalue printed above, which is not safely positive.'
1098 write(message(3), '(a)') 'Either the grid spacing under-resolves the augmentation projectors, or'
1099 write(message(4), '(a)') 'the dataset itself has an indefinite overlap (truncated partial waves).'
1100 write(message(5), '(a)') 'Generalized-eigenproblem results are unreliable; check the spacing'
1101 write(message(6), '(a)') 'convergence or use a different PAW dataset for this element.'
1102 call messages_warning(6)
1103 end if
1104 safe_deallocate_a(eamat)
1105 safe_deallocate_a(evals)
1106
1107 call lalg_inverse(n2, amat, 'svd')
1108 tmat = matmul(qmat, amat)
1109
1110 if (sdim == 1) then
1111 pmat%dmix(1:nn, 1:nn, 1) = -real(tmat(1:nn, 1:nn), real64)
1112 else
1113 do s2 = 1, 2
1114 do s1 = 1, 2
1115 pmat%zmix(1:nn, 1:nn, blkmap(s1, s2)) = -tmat((s1-1)*nn + 1:s1*nn, (s2-1)*nn + 1:s2*nn)
1116 end do
1117 end do
1118 end if
1119
1120 safe_deallocate_a(mmat)
1121 safe_deallocate_a(qmat)
1122 safe_deallocate_a(amat)
1123 safe_deallocate_a(tmat)
1124
1126 end subroutine build_inverse_overlap_sim_grid
1127
1129
1130 !--------------------------------------------------------
1131 integer function projector_beta_index(proj, ll, ic) result(index)
1132 type(projector_t), intent(in) :: proj
1133 integer, intent(in) :: ll
1134 integer, intent(in) :: ic
1135
1136 integer :: il
1137
1138 push_sub(projector_beta_index)
1139
1140 assert(ll >= 0 .and. ll <= proj%lmax)
1141 assert(ic >= 1)
1142
1143 index = ic
1144 do il = 0, ll - 1
1145 if (il == proj%lloc) cycle
1146 index = index + proj%kb_p(il, 0)%n_c
1147 end do
1148
1149 pop_sub(projector_beta_index)
1150 end function projector_beta_index
1151
1152 ! ----------------------------------------------------------------------------------
1154 subroutine nonlocal_pseudopotential_accel_rebuild(this, space, mesh)
1155 class(nonlocal_pseudopotential_t), target, intent(inout) :: this
1156 class(space_t), intent(in) :: space
1157 class(mesh_t), intent(in) :: mesh
1158
1160
1161 if (.not. accel_is_enabled()) then
1163 return
1164 end if
1165
1167
1168 if (.not. allocated(this%projector_matrices) .or. this%nprojector_matrices <= 0) then
1170 return
1171 end if
1172
1173 call nonlocal_pseudopotential_build_accel_buffers(this, space, mesh)
1175
1178
1179 ! ----------------------------------------------------------------------------------
1182 class(nonlocal_pseudopotential_t), intent(inout) :: this
1183
1185
1186 call accel_detach_buffer(this%buff_offsets)
1187 call accel_detach_buffer(this%buff_matrices)
1188 call accel_detach_buffer(this%buff_maps)
1189 call accel_detach_buffer(this%buff_scals)
1190 call accel_detach_buffer(this%buff_position)
1191 call accel_detach_buffer(this%buff_pos)
1192 call accel_detach_buffer(this%buff_invmap)
1193 call accel_detach_buffer(this%buff_invmap_mat)
1194 call accel_detach_buffer(this%buff_point_to_mat)
1195 call accel_detach_buffer(this%buff_proj_to_mat)
1196 call accel_detach_buffer(this%buff_projector_phases)
1197 call accel_detach_buffer(this%buff_mix)
1198
1201
1202 ! ----------------------------------------------------------------------------------
1204 subroutine nonlocal_pseudopotential_build_accel_buffers(this, space, mesh)
1205 class(nonlocal_pseudopotential_t), target, intent(inout) :: this
1206 class(space_t), intent(in) :: space
1207 class(mesh_t), intent(in) :: mesh
1208
1209 integer, parameter :: OFFSET_SIZE = 6
1210 integer, parameter :: POINTS = 1, projs = 2, matrix = 3, map = 4, scal = 5, mix = 6
1211 integer :: imat, matrix_size, scal_size
1212 integer :: ip, is, ii, ipos, mix_offset
1213 integer, allocatable :: cnt(:), invmap(:, :), invmap2(:), pos(:)
1214 integer, allocatable :: invmap_mat(:, :), invmap_mat2(:)
1215 integer, allocatable :: offsets(:, :), point_to_mat(:), proj_to_mat(:)
1216 type(projector_matrix_t), pointer :: pmat
1217
1219
1220 assert(allocated(this%projector_matrices))
1221 assert(this%nprojector_matrices > 0)
1222
1223 safe_allocate(offsets(1:offset_size, 1:this%nprojector_matrices))
1224 safe_allocate(cnt(1:mesh%np))
1225
1226 cnt = 0
1227
1228 matrix_size = 0
1229 this%total_points = 0
1230 scal_size = 0
1231 this%max_npoints = 0
1232 this%max_nprojs = 0
1233 mix_offset = 0
1234 do imat = 1, this%nprojector_matrices
1235 pmat => this%projector_matrices(imat)
1236
1237 this%max_npoints = max(this%max_npoints, pmat%npoints)
1238 this%max_nprojs = max(this%max_nprojs, pmat%nprojs)
1239
1240 offsets(points, imat) = pmat%npoints
1241 offsets(projs, imat) = pmat%nprojs
1242
1243 offsets(matrix, imat) = matrix_size
1244 matrix_size = matrix_size + pmat%npoints*pmat%nprojs
1245
1246 offsets(map, imat) = this%total_points
1247 this%total_points = this%total_points + pmat%npoints
1248
1249 offsets(scal, imat) = scal_size
1250 scal_size = scal_size + pmat%nprojs
1251
1252 offsets(mix, imat) = mix_offset
1253 if (allocated(pmat%dmix)) then
1254 mix_offset = mix_offset + pmat%nprojs**2
1255 else if (allocated(pmat%zmix)) then
1256 mix_offset = mix_offset + 4*pmat%nprojs**2
1257 else
1258 offsets(mix, imat) = -1
1259 end if
1260
1261 do is = 1, pmat%npoints
1262 ip = pmat%map(is)
1263 cnt(ip) = cnt(ip) + 1
1264 end do
1265 end do
1266
1267 safe_allocate(invmap(1:max(maxval(cnt), 1), 1:mesh%np))
1268 safe_allocate(invmap2(1:max(maxval(cnt)*mesh%np, 1)))
1269 safe_allocate(invmap_mat(1:max(maxval(cnt), 1), 1:mesh%np))
1270 safe_allocate(invmap_mat2(1:max(maxval(cnt)*mesh%np, 1)))
1271 safe_allocate(pos(1:mesh%np + 1))
1272
1273 cnt = 0
1274 ii = 0
1275 do imat = 1, this%nprojector_matrices
1276 pmat => this%projector_matrices(imat)
1277 do is = 1, pmat%npoints
1278 ip = pmat%map(is)
1279 cnt(ip) = cnt(ip) + 1
1280 invmap(cnt(ip), ip) = ii
1281 invmap_mat(cnt(ip), ip) = imat - 1
1282 ii = ii + 1
1283 end do
1284 end do
1285
1286 ipos = 0
1287 pos(1) = 0
1288 do ip = 1, mesh%np
1289 do ii = 1, cnt(ip)
1290 ipos = ipos + 1
1291 invmap2(ipos) = invmap(ii, ip)
1292 invmap_mat2(ipos) = invmap_mat(ii, ip)
1293 end do
1294 pos(ip + 1) = ipos
1295 end do
1296
1297 if (this%projector_matrices(1)%is_cmplx) then
1298 call accel_create_buffer(this%buff_matrices, accel_mem_read_only, type_cmplx, matrix_size)
1299 else
1300 call accel_create_buffer(this%buff_matrices, accel_mem_read_only, type_float, matrix_size)
1301 end if
1302 call accel_create_buffer(this%buff_maps, accel_mem_read_only, type_integer, this%total_points)
1303 if (mesh%use_vol_pp) then
1304 call accel_create_buffer(this%buff_sphere_weights, accel_mem_read_only, type_float, this%total_points)
1305 end if
1306 call accel_create_buffer(this%buff_position, accel_mem_read_only, type_float, 3*this%total_points)
1307 call accel_create_buffer(this%buff_scals, accel_mem_read_only, type_float, scal_size)
1308
1309 if (mix_offset > 0) then
1310 if (allocated(this%projector_matrices(1)%zmix)) then
1311 call accel_create_buffer(this%buff_mix, accel_mem_read_only, type_cmplx, mix_offset)
1312 else
1313 call accel_create_buffer(this%buff_mix, accel_mem_read_only, type_float, mix_offset)
1314 end if
1315 end if
1316
1317 do imat = 1, this%nprojector_matrices
1318 pmat => this%projector_matrices(imat)
1319 if (pmat%npoints > 0) then
1320 if (pmat%is_cmplx) then
1321 call accel_write_buffer(this%buff_matrices, pmat%npoints, pmat%nprojs, pmat%zprojectors, &
1322 offset = offsets(matrix, imat))
1323 else
1324 call accel_write_buffer(this%buff_matrices, pmat%npoints, pmat%nprojs, pmat%dprojectors, &
1325 offset = offsets(matrix, imat))
1326 end if
1327 call accel_write_buffer(this%buff_maps, pmat%npoints, pmat%map, offset = offsets(map, imat))
1328 if (mesh%use_vol_pp) then
1329 call accel_write_buffer(this%buff_sphere_weights, pmat%npoints, pmat%sphere_weights, &
1330 offset = offsets(map, imat))
1331 end if
1332 call accel_write_buffer(this%buff_position, space%dim, pmat%npoints, pmat%position, &
1333 offset = 3*offsets(map, imat))
1334 end if
1335 call accel_write_buffer(this%buff_scals, pmat%nprojs, pmat%scal, offset = offsets(scal, imat))
1336 if (offsets(mix, imat) /= -1) then
1337 if (allocated(pmat%zmix)) then
1338 call accel_write_buffer(this%buff_mix, pmat%nprojs, pmat%nprojs, 4, pmat%zmix, offset = offsets(mix, imat))
1339 else
1340 ! Only one spin channel goes to the device: multi-channel dmix occurs only for
1341 ! spin-polarized ultrasoft, which is fenced from the accelerator path.
1342 assert(size(pmat%dmix, 3) == 1)
1343 call accel_write_buffer(this%buff_mix, pmat%nprojs, pmat%nprojs, pmat%dmix(:, :, 1), offset = offsets(mix, imat))
1344 end if
1345 end if
1346 end do
1347
1348 call accel_create_buffer(this%buff_offsets, accel_mem_read_only, type_integer, offset_size*this%nprojector_matrices)
1349 call accel_write_buffer(this%buff_offsets, offset_size, this%nprojector_matrices, offsets)
1350
1351 call accel_create_buffer(this%buff_pos, accel_mem_read_only, type_integer, mesh%np + 1)
1352 call accel_write_buffer(this%buff_pos, mesh%np + 1, pos)
1353
1354 call accel_create_buffer(this%buff_invmap, accel_mem_read_only, type_integer, ipos)
1355 call accel_write_buffer(this%buff_invmap, ipos, invmap2)
1356
1357 call accel_create_buffer(this%buff_invmap_mat, accel_mem_read_only, type_integer, ipos)
1358 call accel_write_buffer(this%buff_invmap_mat, ipos, invmap_mat2)
1359
1360 ! 0-based matrix index of every packed projector point and every projector
1361 safe_allocate(point_to_mat(1:max(this%total_points, 1)))
1362 safe_allocate(proj_to_mat(1:max(scal_size, 1)))
1363 do imat = 1, this%nprojector_matrices
1364 pmat => this%projector_matrices(imat)
1365 point_to_mat(offsets(map, imat) + 1:offsets(map, imat) + pmat%npoints) = imat - 1
1366 proj_to_mat(offsets(scal, imat) + 1:offsets(scal, imat) + pmat%nprojs) = imat - 1
1367 end do
1368
1369 call accel_create_buffer(this%buff_point_to_mat, accel_mem_read_only, type_integer, this%total_points)
1370 call accel_write_buffer(this%buff_point_to_mat, this%total_points, point_to_mat)
1371 call accel_create_buffer(this%buff_proj_to_mat, accel_mem_read_only, type_integer, scal_size)
1372 call accel_write_buffer(this%buff_proj_to_mat, scal_size, proj_to_mat)
1373
1374 safe_deallocate_a(offsets)
1375 safe_deallocate_a(point_to_mat)
1376 safe_deallocate_a(proj_to_mat)
1377 safe_deallocate_a(cnt)
1378 safe_deallocate_a(invmap)
1379 safe_deallocate_a(invmap2)
1380 safe_deallocate_a(invmap_mat)
1381 safe_deallocate_a(invmap_mat2)
1382 safe_deallocate_a(pos)
1383
1386
1387 ! ----------------------------------------------------------------------------------
1390 class(nonlocal_pseudopotential_t), intent(inout) :: this
1391
1392 integer :: ik, imat, iphase, nphase, offset, npoints
1393
1395
1396 if (.not. allocated(this%projector_phases)) then
1398 return
1399 end if
1400
1401 nphase = size(this%projector_phases, 2)
1402 this%nphase = nphase
1403
1404 call accel_create_buffer(this%buff_projector_phases, accel_mem_read_only, type_cmplx, &
1405 this%total_points*nphase*size(this%projector_phases, 4))
1406
1407 offset = 0
1408 do ik = lbound(this%projector_phases, 4), ubound(this%projector_phases, 4)
1409 do imat = 1, this%nprojector_matrices
1410 npoints = this%projector_matrices(imat)%npoints
1411 do iphase = 1, nphase
1412 if (npoints > 0) then
1413 call accel_write_buffer(this%buff_projector_phases, npoints, this%projector_phases(1:, iphase, imat, ik), &
1414 offset = offset, async=.true.)
1415 end if
1416 offset = offset + npoints
1417 end do
1418 end do
1419 end do
1420 call accel_finish()
1421
1424
1425 ! ----------------------------------------------------------------------------------
1427 !
1428 logical pure function nonlocal_pseudopotential_self_overlap(this) result(projector_self_overlap)
1429 class(nonlocal_pseudopotential_t), intent(in) :: this
1430
1431 projector_self_overlap = this%projector_self_overlap
1433
1434 ! ----------------------------------------------------------------------------------
1447 subroutine nonlocal_pseudopotential_update_dij(this, mesh, epot, veff, add_bare, onsite_density_matrix)
1448 class(nonlocal_pseudopotential_t), target, intent(inout) :: this
1449 class(mesh_t), intent(in) :: mesh
1450 type(epot_t), target, intent(in) :: epot
1451 real(real64), intent(in) :: veff(:, :)
1452 logical, optional, intent(in) :: add_bare
1455 real(real64), optional, contiguous, intent(in) :: onsite_density_matrix(:, :, :, :)
1456
1457 integer :: imat, iatom, iproj, jproj, is, np
1458 real(real64) :: vol
1459 integer :: icomp, ncomp
1460 real(real64), allocatable :: vweight(:, :), pairm(:, :, :)
1461 real(real64), allocatable :: dij_buf(:, :, :, :)
1462 logical :: spinor, is_paw_atom, add_bare_
1463 integer :: n_paw, ipaw
1464 integer, allocatable :: paw_imat(:)
1465 type(projector_matrix_t), pointer :: pmat
1466 type(projector_t), pointer :: proj
1467
1468 push_sub_with_profile(nonlocal_pseudopotential_update_dij)
1469
1470 add_bare_ = optional_default(add_bare, .true.)
1471 this%energy_paw = m_zero
1472
1473 ! Collect the PAW atoms first. Without the bare part (e.g. the delta-kick potential), only the
1474 ! screening integral is wanted, so the one-center Hxc coupling is not built.
1475 n_paw = 0
1476 safe_allocate(paw_imat(1:this%nprojector_matrices))
1477 if (add_bare_) then
1478 do imat = 1, this%nprojector_matrices
1479 pmat => this%projector_matrices(imat)
1480 proj => epot%proj(this%projector_to_atom(imat))
1481 if (.not. projector_has_augmentation(proj)) cycle
1482 if (.not. (allocated(pmat%dmix) .or. allocated(pmat%zmix))) cycle
1483 if (.not. associated(proj%aug%paw)) cycle
1484 n_paw = n_paw + 1
1485 paw_imat(n_paw) = imat
1486 end do
1487 end if
1488 if (n_paw > 0) then
1489 assert(present(onsite_density_matrix))
1490 call nonlocal_pseudopotential_paw_onsite(this, epot, n_paw, paw_imat, onsite_density_matrix, dij_buf)
1491 end if
1492
1493 ipaw = 0
1494 do imat = 1, this%nprojector_matrices
1495 pmat => this%projector_matrices(imat)
1496 iatom = this%projector_to_atom(imat)
1497 proj => epot%proj(iatom)
1498
1499 if (.not. projector_has_augmentation(proj)) cycle
1500 spinor = allocated(pmat%zmix)
1501 ! PAW atoms carry the one-center engine on their augmentation data.
1502 is_paw_atom = associated(proj%aug%paw)
1503 if (.not. (allocated(pmat%dmix) .or. spinor)) cycle
1504 assert(allocated(proj%aug%qflm))
1505 ! Same selection as the collection loop above, so ipaw indexes dij_buf consistently.
1506 if (is_paw_atom .and. add_bare_) then
1507 ipaw = ipaw + 1
1508 assert(ipaw <= n_paw)
1509 assert(paw_imat(ipaw) == imat)
1510 end if
1511
1512 if (spinor .and. epot%reltype == spin_orbit) then
1513 if (add_bare_) then
1514 call nonlocal_pseudopotential_soc_build_zmix_cmplx(pmat, proj, use_qij = .false., &
1515 so_strength = epot%so_strength)
1516 else
1517 pmat%zmix = m_z0
1518 end if
1519 if (is_paw_atom .and. add_bare_) then
1520 call nonlocal_pseudopotential_soc_screen_cmplx(pmat, proj, mesh, veff, &
1521 dij_onsite = dij_buf(:, :, :, ipaw))
1522 else
1523 call nonlocal_pseudopotential_soc_screen_cmplx(pmat, proj, mesh, veff)
1524 end if
1525 if (this%uspp_save_memory) then
1526 safe_deallocate_p(pmat%aug_ylm)
1527 end if
1528 cycle
1529 end if
1530
1531 np = pmat%npoints
1532
1533 ! Screening integral, spin-fused: delta_ij^{c} = int V_eff^{c}(r) Q_ij(r) dr for every
1534 ! potential component c. For PAW this is the Dhat_ij term of [Torrent08] Eq. (24),
1535 ! the compensation-charge coupling to the smooth effective potential on the mesh.
1536 if (spinor) then
1537 ncomp = 4
1538 else
1539 ncomp = size(pmat%dmix, 3)
1540 end if
1541 assert(size(veff, 2) >= ncomp)
1542
1543 ! Gather V_eff at the sphere points into contiguous buffers (changes every call).
1544 safe_allocate(vweight(1:np, 1:ncomp))
1545 do icomp = 1, ncomp
1546 do is = 1, np
1547 vweight(is, icomp) = veff(pmat%map(is), icomp)
1548 end do
1549 end do
1550
1551 ! Screening integral delta_ij^c = int V_eff^c(r) Q_ij(r) dr, from the field
1552 ! gathered at the sphere points.
1553 safe_allocate(pairm(1:pmat%nprojs, 1:pmat%nprojs, 1:ncomp))
1554 call nonlocal_pseudopotential_qij_to_pairs(proj, pmat, np, ncomp, vweight, pairm)
1555
1556 vol = mesh%volume_element
1557 do iproj = 1, pmat%nprojs
1558 do jproj = 1, pmat%nprojs
1559 if (spinor) then
1560 pmat%zmix(iproj, jproj, 1) = cmplx(pairm(iproj, jproj, 1)*vol, m_zero, real64)
1561 pmat%zmix(iproj, jproj, 2) = cmplx(pairm(iproj, jproj, 2)*vol, m_zero, real64)
1562 pmat%zmix(iproj, jproj, 3) = cmplx(pairm(iproj, jproj, 3)*vol, pairm(iproj, jproj, 4)*vol, real64)
1563 pmat%zmix(iproj, jproj, 4) = cmplx(pairm(iproj, jproj, 3)*vol, -pairm(iproj, jproj, 4)*vol, real64)
1564 else
1565 pmat%dmix(iproj, jproj, 1:ncomp) = pairm(iproj, jproj, 1:ncomp)*vol
1566 end if
1567 end do
1568 end do
1569
1570 safe_deallocate_a(vweight)
1571 safe_deallocate_a(pairm)
1572
1573 ! The augmentation sphere may be split across domains, so reduce the screening part.
1574 if (mesh%parallel_in_domains) then
1575 if (spinor) then
1576 call mesh%allreduce(pmat%zmix, dim = (/pmat%nprojs, pmat%nprojs, 4/))
1577 else
1578 call mesh%allreduce(pmat%dmix, dim = (/pmat%nprojs, pmat%nprojs, ncomp/))
1579 end if
1580 end if
1581
1582 if (.not. add_bare_) cycle
1583
1584 ! Second pass: add the bare (descreened) D^(0), block-diagonal in (l, m); for spinors it
1585 ! is spin-diagonal (the spin-flip blocks carry only the noncollinear V_xc screening). For PAW
1586 ! this descreened D^(0) (PP_DIJ) carries the frozen kinetic + reference/nuclear one-center
1587 ! part, exactly as in USPP; the SCF-dependent Hartree/XC one-center part is added afterwards.
1588 do iproj = 1, pmat%nprojs
1589 do jproj = 1, pmat%nprojs
1590 if (pmat%l(iproj) == pmat%l(jproj) .and. pmat%m(iproj) == pmat%m(jproj)) then
1591 if (spinor) then
1592 pmat%zmix(iproj, jproj, 1:2) = pmat%zmix(iproj, jproj, 1:2) + &
1593 proj%aug%dij(pmat%l(iproj), pmat%channel(iproj), pmat%channel(jproj))
1594 else
1595 ! The bare D^(0) is spin-independent; it enters every spin channel.
1596 pmat%dmix(iproj, jproj, :) = pmat%dmix(iproj, jproj, :) + &
1597 proj%aug%dij(pmat%l(iproj), pmat%channel(iproj), pmat%channel(jproj))
1598 end if
1599 end if
1600 end do
1601 end do
1602
1603 ! PAW: the one-center Hartree/XC coupling D^1 - D~^1 (Hxc part only) built
1604 ! from the current one-center density matrix rho_ij is added in a separate pass below.
1605 ! The frozen kinetic/reference part is already in PP_DIJ above.
1606 if (is_paw_atom) then
1607 if (spinor) then
1608 do jproj = 1, pmat%nprojs
1609 do iproj = 1, pmat%nprojs
1610 pmat%zmix(iproj, jproj, 1) = pmat%zmix(iproj, jproj, 1) + dij_buf(iproj, jproj, 1, ipaw)
1611 pmat%zmix(iproj, jproj, 2) = pmat%zmix(iproj, jproj, 2) + dij_buf(iproj, jproj, 2, ipaw)
1612 pmat%zmix(iproj, jproj, 3) = pmat%zmix(iproj, jproj, 3) &
1613 + cmplx(dij_buf(iproj, jproj, 3, ipaw), dij_buf(iproj, jproj, 4, ipaw), real64)
1614 pmat%zmix(iproj, jproj, 4) = pmat%zmix(iproj, jproj, 4) &
1615 + cmplx(dij_buf(iproj, jproj, 3, ipaw), -dij_buf(iproj, jproj, 4, ipaw), real64)
1616 end do
1617 end do
1618 else
1619 do is = 1, size(dij_buf, 3)
1620 pmat%dmix(1:pmat%nprojs, 1:pmat%nprojs, is) = pmat%dmix(1:pmat%nprojs, 1:pmat%nprojs, is) &
1621 + dij_buf(1:pmat%nprojs, 1:pmat%nprojs, is, ipaw)
1622 end do
1623 end if
1624 end if
1625 end do
1626
1627 if (.not. this%ghost_checked) then
1628 this%ghost_checked = .true.
1629 call nonlocal_pseudopotential_ghost_check(this, mesh, epot)
1630 end if
1631
1632 safe_deallocate_a(paw_imat)
1633 safe_deallocate_a(dij_buf)
1634
1635 pop_sub_with_profile(nonlocal_pseudopotential_update_dij)
1637
1638 ! ----------------------------------------------------------------------------------
1649 subroutine nonlocal_pseudopotential_ghost_check(this, mesh, epot)
1650 class(nonlocal_pseudopotential_t), target, intent(in) :: this
1651 class(mesh_t), intent(in) :: mesh
1652 type(epot_t), target, intent(in) :: epot
1653
1654 integer :: imat, iatom, iproj, ll, mm, i1, i2, np
1655 real(real64) :: bb(2, 2), qq(2, 2), dd(2, 2), sd(2, 2), binv(2, 2)
1656 real(real64) :: det, tr, lmin, lmax, vv(2), vnorm, vdv, vol
1657 complex(real64) :: zbb(2, 2)
1658 real(real64), allocatable :: dpp(:, :)
1659 complex(real64), allocatable :: zpp(:, :)
1660 logical :: warned
1661 type(projector_matrix_t), pointer :: pmat
1662 type(projector_t), pointer :: proj
1663
1665
1666 warned = .false.
1667 vol = mesh%volume_element
1668
1669 do imat = 1, this%nprojector_matrices
1670 if (warned) exit
1671 pmat => this%projector_matrices(imat)
1672 iatom = this%projector_to_atom(imat)
1673 proj => epot%proj(iatom)
1674 if (.not. projector_has_augmentation(proj)) cycle
1675 if (.not. (allocated(pmat%dmix) .or. allocated(pmat%zmix))) cycle
1676 np = pmat%npoints
1677
1678 if (np > 0) then
1679 if (associated(pmat%zprojectors)) then
1680 safe_allocate(zpp(1:np, 1:2))
1681 else
1682 safe_allocate(dpp(1:np, 1:2))
1683 end if
1684 end if
1685
1686 do ll = 0, proj%lmax
1687 if (warned) exit
1688 ! For j-dependent (SOC) projectors the channels of an l > 0 block belong to
1689 ! different j and are not coupled by q, so the 2x2 heuristic only applies to
1690 ! l = 0 there (the observed ghost class); collinear channels are always covered.
1691 if (associated(pmat%zprojectors) .and. ll > 0) cycle
1692 mm = -ll
1693 ! the two channels of the (ll, mm) block; blocks with a different channel count
1694 ! are not covered by this 2x2 heuristic
1695 i1 = 0
1696 i2 = 0
1697 do iproj = 1, pmat%nprojs
1698 if (pmat%l(iproj) /= ll .or. pmat%m(iproj) /= mm) cycle
1699 if (pmat%channel(iproj) == 1) i1 = iproj
1700 if (pmat%channel(iproj) == 2) i2 = iproj
1701 end do
1702 if (i1 == 0 .or. i2 == 0) cycle
1703
1704 ! mesh Gram matrix of the two projectors, from a single gemm on the gathered pair
1705 if (np > 0) then
1706 if (associated(pmat%zprojectors)) then
1707 call lalg_copy(np, pmat%zprojectors(:, i1), zpp(:, 1))
1708 call lalg_copy(np, pmat%zprojectors(:, i2), zpp(:, 2))
1709 call lalg_gemm_cn(2, 2, np, cmplx(vol, m_zero, real64), zpp, zpp, m_z0, zbb)
1710 bb = real(zbb, real64)
1711 else
1712 call lalg_copy(np, pmat%dprojectors(:, i1), dpp(:, 1))
1713 call lalg_copy(np, pmat%dprojectors(:, i2), dpp(:, 2))
1714 call lalg_gemm_cn(2, 2, np, vol, dpp, dpp, m_zero, bb)
1715 end if
1716 else
1717 bb = m_zero
1718 end if
1719 if (mesh%parallel_in_domains) call mesh%allreduce(bb, dim = (/2, 2/))
1720
1721 det = bb(1, 1)*bb(2, 2) - bb(1, 2)*bb(2, 1)
1722 if (det <= m_epsilon*bb(1, 1)*bb(2, 2)) cycle
1723 binv(1, 1) = bb(2, 2)/det
1724 binv(2, 2) = bb(1, 1)/det
1725 binv(1, 2) = -bb(1, 2)/det
1726 binv(2, 1) = binv(1, 2)
1727
1728 qq(1, 1) = proj%aug%qij(ll, 1, 1)
1729 qq(1, 2) = proj%aug%qij(ll, 1, 2)
1730 qq(2, 1) = qq(1, 2)
1731 qq(2, 2) = proj%aug%qij(ll, 2, 2)
1732
1733 ! dual overlap metric and its extremal eigenpair (2x2 closed form)
1734 sd = binv + qq
1735 tr = sd(1, 1) + sd(2, 2)
1736 det = sd(1, 1)*sd(2, 2) - sd(1, 2)*sd(2, 1)
1737 lmin = m_half*(tr - sqrt(max(tr*tr - m_four*det, m_zero)))
1738 lmax = m_half*(tr + sqrt(max(tr*tr - m_four*det, m_zero)))
1739 if (lmin <= m_zero .or. lmin > 0.05_real64*lmax) cycle
1740 vv(1) = sd(1, 2)
1741 vv(2) = lmin - sd(1, 1)
1742 vnorm = sqrt(vv(1)**2 + vv(2)**2)
1743 if (vnorm <= m_epsilon) cycle
1744 vv = vv/vnorm
1745
1746 ! screened coupling projected on the near-null direction
1747 if (allocated(pmat%zmix)) then
1748 dd(1, 1) = real(pmat%zmix(i1, i1, 1), real64)
1749 dd(1, 2) = real(pmat%zmix(i1, i2, 1), real64)
1750 dd(2, 2) = real(pmat%zmix(i2, i2, 1), real64)
1751 else
1752 dd(1, 1) = pmat%dmix(i1, i1, 1)
1753 dd(1, 2) = pmat%dmix(i1, i2, 1)
1754 dd(2, 2) = pmat%dmix(i2, i2, 1)
1755 end if
1756 dd(2, 1) = dd(1, 2)
1757 vdv = vv(1)*vv(1)*dd(1, 1) + m_two*vv(1)*vv(2)*dd(1, 2) + vv(2)*vv(2)*dd(2, 2)
1758
1759 if (vdv < -1.0e-3_real64) then
1760 write(message(1), '(a,i2,a)') "The pseudopotential of atom " , iatom, &
1761 " has a near-singular augmentation channel"
1762 write(message(2), '(a,i2,a,es9.2,a,f8.4,a)') "block (l = ", ll, &
1763 "): overlap direction ", lmin/lmax, " with attractive coupling (", vdv, " Ha)."
1764 message(3) = "Spurious (ghost) states are likely on the real-space grid and the SCF"
1765 message(4) = "may diverge. This is a dataset conditioning problem; consider a"
1766 message(5) = "different pseudopotential for this element."
1767 call messages_warning(5)
1768 warned = .true.
1769 end if
1770 end do
1771
1772 safe_deallocate_a(dpp)
1773 safe_deallocate_a(zpp)
1774 end do
1775
1778
1779 ! ----------------------------------------------------------------------------------
1791 subroutine nonlocal_pseudopotential_paw_onsite(this, epot, n_paw, paw_imat, onsite_density_matrix, dij_buf)
1792 class(nonlocal_pseudopotential_t), target, intent(inout) :: this
1793 type(epot_t), target, intent(in) :: epot
1794 integer, intent(in) :: n_paw
1795 integer, intent(in) :: paw_imat(:)
1796 real(real64), contiguous, intent(in) :: onsite_density_matrix(:, :, :, :)
1797 real(real64), allocatable, intent(out) :: dij_buf(:, :, :, :)
1798
1799 integer :: ipaw, imat, iu, nlm, nsp, nprojs, is, iproj, jproj
1800 logical :: ae, distributed
1801 integer, allocatable :: map(:)
1802 real(real64), allocatable :: rho(:, :, :), d_side(:, :, :)
1803 real(real64), allocatable :: e_buf(:, :)
1804 real(real64) :: e_side, ecorr, sgn
1805 real(real64), parameter :: wcomp(4) = [m_one, m_one, m_two, m_two]
1806 type(projector_matrix_t), pointer :: pmat
1807 type(projector_t), pointer :: proj
1808
1809 push_sub_with_profile(nonlocal_pseudopotential_paw_onsite)
1810
1811 distributed = this%paw_onsite_grp%size > 1
1812
1813 proj => epot%proj(this%projector_to_atom(paw_imat(1)))
1814 nsp = paw_onsite_nspin(proj%aug%paw)
1815 assert(size(onsite_density_matrix, 4) >= nsp)
1816
1817 safe_allocate(dij_buf(1:this%max_nprojs, 1:this%max_nprojs, 1:nsp, 1:n_paw))
1818 safe_allocate(e_buf(1:2, 1:n_paw))
1819 dij_buf = m_zero
1820 e_buf = m_zero
1821
1822 do iu = 0, 2*n_paw - 1
1823 if (distributed) then
1824 if (mod(iu, this%paw_onsite_grp%size) /= this%paw_onsite_grp%rank) cycle
1825 end if
1826 ipaw = iu/2 + 1
1827 ae = (mod(iu, 2) == 0)
1828 imat = paw_imat(ipaw)
1829 pmat => this%projector_matrices(imat)
1830 proj => epot%proj(this%projector_to_atom(imat))
1831 nprojs = pmat%nprojs
1832 nlm = paw_onsite_nlm(proj%aug%paw)
1833 assert(nlm == nprojs)
1834 assert(paw_onsite_nspin(proj%aug%paw) == nsp)
1835
1836 ! Reorder the projector (l, m, channel) enumeration into the one-center (radial, m) index.
1837 safe_allocate(map(1:nprojs))
1838 call paw_onsite_pmat_map(proj%aug%paw, nprojs, pmat%l, pmat%m, pmat%channel, map)
1839
1840 safe_allocate(rho(1:nlm, 1:nlm, 1:nsp))
1841 safe_allocate(d_side(1:nlm, 1:nlm, 1:nsp))
1842 do is = 1, nsp
1843 call paw_onsite_reorder_pmat(nprojs, map, onsite_density_matrix(:, :, imat, is), rho(:, :, is))
1844 end do
1845
1846 call paw_onsite_side(proj%aug%paw, rho, ae, e_side, d_side, hxc_only = .true.)
1847
1848 if (ae) then
1849 sgn = m_one
1850 e_buf(1, ipaw) = e_side
1851 else
1852 sgn = -m_one
1853 e_buf(2, ipaw) = e_side
1854 end if
1855 ! Store the signed side result back in projector order, so the finalization below can
1856 ! contract it directly with onsite_density_matrix without the map.
1857 do is = 1, nsp
1858 do jproj = 1, nprojs
1859 do iproj = 1, nprojs
1860 dij_buf(iproj, jproj, is, ipaw) = dij_buf(iproj, jproj, is, ipaw) &
1861 + sgn*d_side(map(iproj), map(jproj), is)
1862 end do
1863 end do
1864 end do
1865
1866 safe_deallocate_a(map)
1867 safe_deallocate_a(rho)
1868 safe_deallocate_a(d_side)
1869 end do
1870
1871 if (distributed) then
1872 call comm_allreduce(this%paw_onsite_grp, dij_buf)
1873 call comm_allreduce(this%paw_onsite_grp, e_buf)
1874 end if
1875
1876 ! Accumulate the double-counting correction
1877 do ipaw = 1, n_paw
1878 imat = paw_imat(ipaw)
1879 pmat => this%projector_matrices(imat)
1880 ecorr = e_buf(1, ipaw) - e_buf(2, ipaw)
1881 do is = 1, nsp
1882 do jproj = 1, pmat%nprojs
1883 do iproj = 1, pmat%nprojs
1884 ecorr = ecorr - wcomp(is)*onsite_density_matrix(iproj, jproj, imat, is)*dij_buf(iproj, jproj, is, ipaw)
1885 end do
1886 end do
1887 end do
1888 this%energy_paw = this%energy_paw + ecorr
1889 end do
1890
1891 safe_deallocate_a(e_buf)
1892
1893 pop_sub_with_profile(nonlocal_pseudopotential_paw_onsite)
1895
1896 ! ----------------------------------------------------------------------------------
1906 subroutine nonlocal_pseudopotential_build_paw_symm(this, epot, space, gr, ions)
1907 class(nonlocal_pseudopotential_t), intent(inout) :: this
1908 type(epot_t), intent(in) :: epot
1909 class(space_t), intent(in) :: space
1910 type(grid_t), intent(in) :: gr
1911 type(ions_t), intent(in) :: ions
1912
1913 integer :: nops, lmax, imat, iatom, iatom_sym, jmat, iop, ll, mm, im, imp, norb, nprojs
1914 real(real64) :: rc, origin(1:space%dim)
1915 type(submesh_t) :: sphere
1916 real(real64), allocatable :: orb(:,:), orb_sym(:), ylm(:), nrm(:)
1917
1919
1920 safe_deallocate_a(this%paw_symm_rot)
1921 safe_deallocate_a(this%paw_symm_map)
1922 this%paw_symm_nops = 0
1923 this%paw_symm_lmax = -1
1924
1925 if (this%nprojector_matrices == 0) then
1927 return
1928 end if
1929
1930 ! Maximum l over the PAW projector matrices.
1931 lmax = -1
1932 do imat = 1, this%nprojector_matrices
1933 iatom = this%projector_to_atom(imat)
1934 if (.not. associated(epot%proj(iatom)%aug%paw)) cycle
1935 nprojs = this%projector_matrices(imat)%nprojs
1936 lmax = max(lmax, maxval(this%projector_matrices(imat)%l(1:nprojs)))
1937 end do
1938 if (lmax < 0) then ! no PAW matrices
1940 return
1941 end if
1942
1943 nops = ions%symm%nops
1944 this%paw_symm_nops = nops
1945 this%paw_symm_lmax = lmax
1946
1947 ! Map each PAW matrix to the equivalent atom matrix under every operation. Non-PAW matrices
1948 ! (norm-conserving / ultrasoft atoms of a mixed system) keep a zero map: they carry no
1949 ! one-center density matrix and nonlocal_pseudopotential_symmetrize_onsite leaves them
1950 ! untouched. This also keeps their l out of paw_symm_rot, which is sized by the PAW lmax only.
1951 safe_allocate(this%paw_symm_map(1:this%nprojector_matrices, 1:nops))
1952 this%paw_symm_map = 0
1953 do imat = 1, this%nprojector_matrices
1954 iatom = this%projector_to_atom(imat)
1955 if (.not. associated(epot%proj(iatom)%aug%paw)) cycle
1956 do iop = 1, nops
1957 iatom_sym = ions%inv_map_symm_atoms(iatom, iop)
1958 do jmat = 1, this%nprojector_matrices
1959 if (this%projector_to_atom(jmat) == iatom_sym) then
1960 this%paw_symm_map(imat, iop) = jmat
1961 exit
1962 end if
1963 end do
1964 ! A symmetry-equivalent atom is of the same (PAW) species, so it has a projector matrix.
1965 assert(this%paw_symm_map(imat, iop) > 0)
1966 end do
1967 end do
1968
1969 ! Real-Y_LM rotation matrices per l. l = 0 is invariant; higher l are built by the grid
1970 ! projection below (partial dot products, reduced over domains afterwards).
1971 safe_allocate(this%paw_symm_rot(1:2*lmax+1, 1:2*lmax+1, 0:lmax, 1:nops))
1972 this%paw_symm_rot = m_zero
1973 if (lmax >= 1) then
1974 ! Small submesh at the origin holding ~50 points, exactly as orbitals_get_symm_weight.
1975 rc = (50.0_real64 * m_three/m_four/m_pi*product(gr%spacing))**m_third
1976 origin = m_zero
1977 call submesh_init(sphere, space, gr, ions%latt, origin, rc)
1978 safe_allocate(ylm(1:sphere%np))
1979 safe_allocate(orb_sym(1:gr%np))
1980 safe_allocate(nrm(1:2*lmax+1))
1981 do ll = 1, lmax
1982 norb = 2*ll + 1
1983 safe_allocate(orb(1:gr%np, 1:norb))
1984 do im = 1, norb
1985 mm = im - 1 - ll
1986 orb(:, im) = m_zero
1987 ! The origin submesh may be empty on this domain (domain parallelism); only tabulate and
1988 ! scatter Y_lm where it holds points. dmf_nrm2 reduces over all domains regardless.
1989 if (sphere%np > 0) then
1990 call loct_ylm(sphere%np, sphere%rel_x(1, 1), sphere%r(1), ll, mm, ylm(1))
1991 call submesh_add_to_mesh(sphere, ylm, orb(:, im))
1992 end if
1993 nrm(im) = dmf_nrm2(gr, orb(:, im))
1994 end do
1995 ! D^l_{m m`} = <Y_m' | S Y_m> / (||Y_m|| ||Y_m'||)
1996 do im = 1, norb
1997 do iop = 1, nops
1998 call dgrid_symmetrize_single(gr, iop, orb(:, im), orb_sym)
1999 do imp = 1, norb
2000 this%paw_symm_rot(im, imp, ll, iop) = &
2001 dmf_dotp(gr, orb(:, imp), orb_sym, reduce = .false.) / (nrm(im)*nrm(imp))
2002 end do
2003 end do
2004 end do
2005 safe_deallocate_a(orb)
2006 end do
2007 safe_deallocate_a(nrm)
2008 call gr%allreduce(this%paw_symm_rot)
2009 safe_deallocate_a(ylm)
2010 safe_deallocate_a(orb_sym)
2011 call submesh_end(sphere)
2012 end if
2013 ! l = 0 rotation is the identity (set after the reduction so it is not summed over domains).
2014 this%paw_symm_rot(1, 1, 0, 1:nops) = m_one
2015
2018
2019 ! ----------------------------------------------------------------------------------
2029 class(nonlocal_pseudopotential_t), target, intent(inout) :: this
2030
2031 integer :: nsym, imat, imat_src, iop, is, nspin, nprojs
2032 integer :: iproj, jproj, ki, kj, li, lj, oi, oj
2033 real(real64) :: wi, wj, s
2034 real(real64), allocatable :: tmp(:, :, :, :)
2035 integer, allocatable :: part(:, :)
2036 type(projector_matrix_t), pointer :: pmat
2037
2038 if (.not. allocated(this%onsite_density_matrix)) return
2039 if (.not. allocated(this%paw_symm_rot)) return
2040 nsym = this%paw_symm_nops
2041 if (nsym <= 1) return
2042
2044
2045 nspin = size(this%onsite_density_matrix, 4)
2046 safe_allocate(tmp(1:this%max_nprojs, 1:this%max_nprojs, 1:this%nprojector_matrices, 1:nspin))
2047 tmp = this%onsite_density_matrix
2048 this%onsite_density_matrix = m_zero
2049
2050 do imat = 1, this%nprojector_matrices
2051 pmat => this%projector_matrices(imat)
2052 nprojs = pmat%nprojs
2053
2054 ! Matrices without a symmetry map (non-PAW atoms, see nonlocal_pseudopotential_build_paw_symm)
2055 ! are left untouched.
2056 if (this%paw_symm_map(imat, 1) == 0) then
2057 this%onsite_density_matrix(1:nprojs, 1:nprojs, imat, 1:nspin) = tmp(1:nprojs, 1:nprojs, imat, 1:nspin)
2058 cycle
2059 end if
2060
2061 ! part(iproj, o) = index of the projector with the same (l, channel) as iproj and m = -l+o-1.
2062 ! Equivalent atoms of one species share this ordering, so it also indexes the source matrix.
2063 safe_allocate(part(1:nprojs, 1:2*this%paw_symm_lmax+1))
2064 part = 0
2065 do iproj = 1, nprojs
2066 do ki = 1, nprojs
2067 if (pmat%l(ki) == pmat%l(iproj) .and. pmat%channel(ki) == pmat%channel(iproj)) then
2068 part(iproj, pmat%m(ki) + pmat%l(iproj) + 1) = ki
2069 end if
2070 end do
2071 end do
2072
2073 do iop = 1, nsym
2074 imat_src = this%paw_symm_map(imat, iop)
2075 assert(this%projector_matrices(imat_src)%nprojs == nprojs)
2076 do is = 1, nspin
2077 do iproj = 1, nprojs
2078 li = pmat%l(iproj)
2079 oi = pmat%m(iproj) + li + 1
2080 do jproj = 1, nprojs
2081 lj = pmat%l(jproj)
2082 oj = pmat%m(jproj) + lj + 1
2083 s = m_zero
2084 do ki = 1, 2*li+1
2085 wi = this%paw_symm_rot(oi, ki, li, iop)
2086 if (abs(wi) < 1.0e-12_real64) cycle
2087 do kj = 1, 2*lj+1
2088 wj = this%paw_symm_rot(oj, kj, lj, iop)
2089 if (abs(wj) < 1.0e-12_real64) cycle
2090 s = s + wi*wj*tmp(part(iproj, ki), part(jproj, kj), imat_src, is)
2091 end do
2092 end do
2093 this%onsite_density_matrix(iproj, jproj, imat, is) = &
2094 this%onsite_density_matrix(iproj, jproj, imat, is) + s/real(nsym, real64)
2095 end do
2096 end do
2097 end do
2098 end do
2099 safe_deallocate_a(part)
2100 end do
2101
2102 safe_deallocate_a(tmp)
2103
2106
2107 ! ----------------------------------------------------------------------------------
2117 subroutine nonlocal_pseudopotential_qij_to_pairs(proj, pmat, np, ncomp, field, pairm)
2118 type(projector_t), intent(in) :: proj
2119 type(projector_matrix_t), intent(in) :: pmat
2120 integer, intent(in) :: np, ncomp
2121 real(real64), contiguous, intent(in) :: field(:, :)
2122 real(real64), intent(out) :: pairm(:, :, :)
2123
2124 real(real64), allocatable :: wq(:, :)
2125
2127
2128 safe_allocate(wq(1:proj%aug%qflm_ncombo, 1:ncomp))
2129 wq = m_zero
2130 ! wq(col, c) = sum_is aug_qflm(is, col) field(is, c), all spin components at once.
2131 if (np > 0) then
2132 call blas_gemm('T', 'N', proj%aug%qflm_ncombo, ncomp, np, m_one, &
2133 proj%aug%qflm(1, 1), np, field(1, 1), np, m_zero, wq(1, 1), proj%aug%qflm_ncombo)
2134 end if
2135 call projector_aug_columns_to_pairs(proj, pmat, ncomp, wq, pairm)
2136 safe_deallocate_a(wq)
2137
2140
2141 ! ----------------------------------------------------------------------------------
2148 subroutine nonlocal_pseudopotential_soc_build_zmix_cmplx(pmat, proj, use_qij, so_strength)
2149 type(projector_matrix_t), target, intent(inout) :: pmat
2150 type(projector_t), target, intent(in) :: proj
2151 logical, intent(in) :: use_qij
2153 real(real64), optional, intent(in) :: so_strength
2154
2155 integer :: ll, nc, ci, cj, ip, ki, kj
2156 real(real64) :: lam, wplus, wminus, avg
2157 integer, allocatable :: pidx(:, :)
2158 logical, allocatable :: jplus(:)
2159 real(real64), allocatable :: mat(:, :)
2160
2162
2163 assert(allocated(pmat%zmix))
2164 pmat%zmix = m_z0
2165
2166 do ll = 0, proj%lmax
2167 if (ll == proj%lloc) cycle
2168 nc = proj%kb_p(ll, 0)%n_c
2169 if (nc == 0) cycle
2170
2171 safe_allocate(jplus(1:nc))
2172 safe_allocate(pidx(1:2*ll+1, 1:nc))
2173 safe_allocate(mat(1:nc, 1:nc))
2174
2175 do ci = 1, nc
2176 jplus(ci) = (ll == 0) .or. (mod(ci, 2) == 1)
2177 end do
2178 pidx = 0
2179 do ip = 1, pmat%nprojs
2180 if (pmat%l(ip) /= ll) cycle
2181 pidx(pmat%m(ip) + ll + 1, pmat%channel(ip)) = ip
2182 end do
2183 do ci = 1, nc
2184 do cj = 1, nc
2185 if (use_qij) then
2186 mat(ci, cj) = proj%aug%qij(ll, ci, cj)
2187 else
2188 mat(ci, cj) = proj%aug%dij(ll, ci, cj)
2189 end if
2190 end do
2191 end do
2192
2193 ! Spin-orbit scaling (SOStrength): the j = l +/- 1/2 splitting of the bare coupling is scaled
2194 ! around its (2j+1)-weighted average, D_j -> avg + lam (D_j - avg).
2195 ! This is not done for PAW: the spin-orbit splitting of a PAW dataset is mostly carried by the
2196 ! j-dependent partial waves (augmentation Q_ij and one-center terms), and the splitting of the
2197 ! descreened D^0 can have the opposite sign, so scaling D^0 alone does not scale the
2198 ! spin-orbit coupling (SOStrength /= 1 is rejected for PAW in epot_init).
2199 lam = optional_default(so_strength, m_one)
2200 if (associated(proj%aug%paw)) lam = m_one
2201 if (abs(lam - m_one) > m_epsilon .and. .not. use_qij .and. ll > 0 .and. mod(nc, 2) == 0) then
2202 wplus = real(2*ll + 2, real64)
2203 wminus = real(2*ll, real64)
2204 do ki = 1, nc/2
2205 do kj = 1, nc/2
2206 avg = (wplus*mat(2*ki - 1, 2*kj - 1) + wminus*mat(2*ki, 2*kj))/(wplus + wminus)
2207 mat(2*ki - 1, 2*kj - 1) = avg + lam*(mat(2*ki - 1, 2*kj - 1) - avg)
2208 mat(2*ki, 2*kj) = avg + lam*(mat(2*ki, 2*kj) - avg)
2209 end do
2210 end do
2211 end if
2212
2213 call soc_uspp_add_channel_coupling_cmplx(ll, nc, jplus, mat, pidx, pmat%zmix)
2214
2215 safe_deallocate_a(jplus)
2216 safe_deallocate_a(pidx)
2217 safe_deallocate_a(mat)
2218 end do
2219
2222
2223 ! ----------------------------------------------------------------------------------
2233 subroutine nonlocal_pseudopotential_soc_screen_cmplx(pmat, proj, mesh, veff, dij_onsite)
2234 type(projector_matrix_t), target, intent(inout) :: pmat
2235 type(projector_t), target, intent(in) :: proj
2236 class(mesh_t), intent(in) :: mesh
2237 real(real64), intent(in) :: veff(:, :)
2238 real(real64), optional, intent(in) :: dij_onsite(:, :, :)
2239
2240 integer :: np, lmaxq, iproj, jproj, is, ltot, mtot, pair_index, lm, icomp
2241 integer :: sa, sb, n2
2242 real(real64) :: vol, coeff, ssum(4), acc_lm
2243 real(real64), allocatable :: vweight(:, :), qyv(:)
2244 complex(real64), allocatable :: dr(:, :, :), gmat(:, :), dfl(:, :)
2245 integer :: blkmap_screen(2, 2)
2246
2248
2249 blkmap_screen(1, 1) = 1
2250 blkmap_screen(2, 2) = 2
2251 blkmap_screen(1, 2) = 3
2252 blkmap_screen(2, 1) = 4
2253
2254 np = pmat%npoints
2255 lmaxq = proj%aug%qfunc_lmax
2256 vol = mesh%volume_element
2257 call projector_matrix_build_aug_ylm(pmat, lmaxq)
2258
2259 ! Gather the 4 components of V_eff at the sphere points into contiguous buffers.
2260 safe_allocate(vweight(1:np, 1:4))
2261 do icomp = 1, 4
2262 do is = 1, np
2263 vweight(is, icomp) = veff(pmat%map(is), icomp)
2264 end do
2265 end do
2266
2267 ! Real-Y_lm screening slabs D^r (1=uu, 2=dd, 3=ud complex; du = conjg(ud)), spin-fused:
2268 ! ssum(c) = int V_eff^c(r) Q_ij(r) dr, exactly as in the collinear part of update_dij.
2269 safe_allocate(qyv(1:np))
2270 safe_allocate(dr(1:pmat%nprojs, 1:pmat%nprojs, 1:3))
2271 dr = m_z0
2272 if (np > 0) then
2273 do iproj = 1, pmat%nprojs
2274 do jproj = 1, pmat%nprojs
2275 pair_index = (pmat%beta_index(iproj) - 1)*proj%aug%qfunc_nbeta + pmat%beta_index(jproj)
2276 ssum = m_zero
2277 do ltot = 0, lmaxq
2278 do mtot = -ltot, ltot
2279 coeff = real_gaunt(pmat%l(iproj), pmat%m(iproj), pmat%l(jproj), pmat%m(jproj), ltot, mtot)
2280 if (abs(coeff) <= 1.0e-12_real64) cycle
2281 lm = ltot*(ltot + 1) + mtot + 1
2282 !$omp simd
2283 do is = 1, np
2284 qyv(is) = pmat%aug_ylm(is, lm)*proj%aug%qfunc(is, pair_index, ltot)
2285 end do
2286 do icomp = 1, 4
2287 acc_lm = blas_dot(np, vweight(1, icomp), 1, qyv(1), 1)
2288 ssum(icomp) = ssum(icomp) + coeff*acc_lm
2289 end do
2290 end do
2291 end do
2292 dr(iproj, jproj, 1) = cmplx(ssum(1)*vol, m_zero, real64)
2293 dr(iproj, jproj, 2) = cmplx(ssum(2)*vol, m_zero, real64)
2294 dr(iproj, jproj, 3) = cmplx(ssum(3)*vol, ssum(4)*vol, real64)
2295 end do
2296 end do
2297 end if
2298 safe_deallocate_a(vweight)
2299 safe_deallocate_a(qyv)
2300 if (mesh%parallel_in_domains) call mesh%allreduce(dr, dim = (/pmat%nprojs, pmat%nprojs, 3/))
2301
2302
2303 ! PAW: the one-center coupling is a real-Y_lm-basis object like the screening; adding it
2304 ! here (before the recoupling) makes Tr[rho^c D^c] = sum_c w_c Tr[rho^r_c D^r_c] hold for
2305 ! the one-center part as well.
2306 if (present(dij_onsite)) then
2307 do jproj = 1, pmat%nprojs
2308 do iproj = 1, pmat%nprojs
2309 dr(iproj, jproj, 1) = dr(iproj, jproj, 1) + dij_onsite(iproj, jproj, 1)
2310 dr(iproj, jproj, 2) = dr(iproj, jproj, 2) + dij_onsite(iproj, jproj, 2)
2311 dr(iproj, jproj, 3) = dr(iproj, jproj, 3) &
2312 + cmplx(dij_onsite(iproj, jproj, 3), dij_onsite(iproj, jproj, 4), real64)
2313 end do
2314 end do
2315 end if
2316
2317 ! Recouple D^r -> D^c: the screening is the energy dual of the density recoupling.
2318 ! On composite (projector, spin) indices, idx(p, s) = p + (s-1)*n,
2319 ! this is one matrix sandwich with the same recoupling matrix G as the density,
2320 ! so that Tr[rho^c D^c] = Tr[onsite_density_matrix D^r]
2321 ! holds by construction:
2322 ! D^c = conjg(G) D^r G^T .
2323 ! The result accumulates into the zmix blocks (1=uu, 2=dd, 3=ud, 4=du) on top of the bare D^(0).
2324 n2 = 2*pmat%nprojs
2325 safe_allocate(gmat(1:n2, 1:n2))
2327
2328 ! Flatten the 2x2 spin blocks of D^r; the du block follows from the Hermiticity of the
2329 ! screened potential, D^r_du(i, j) = conjg(D^r_ud(j, i)).
2330 safe_allocate(dfl(1:n2, 1:n2))
2331 dfl(1:pmat%nprojs, 1:pmat%nprojs) = dr(:, :, 1)
2332 dfl(pmat%nprojs + 1:n2, pmat%nprojs + 1:n2) = dr(:, :, 2)
2333 dfl(1:pmat%nprojs, pmat%nprojs + 1:n2) = dr(:, :, 3)
2334 dfl(pmat%nprojs + 1:n2, 1:pmat%nprojs) = conjg(transpose(dr(:, :, 3)))
2335
2336 dfl = matmul(conjg(gmat), matmul(dfl, transpose(gmat)))
2337
2338 do sb = 1, 2
2339 do sa = 1, 2
2340 pmat%zmix(1:pmat%nprojs, 1:pmat%nprojs, blkmap_screen(sa, sb)) = &
2341 pmat%zmix(1:pmat%nprojs, 1:pmat%nprojs, blkmap_screen(sa, sb)) + &
2342 dfl((sa-1)*pmat%nprojs + 1:sa*pmat%nprojs, (sb-1)*pmat%nprojs + 1:sb*pmat%nprojs)
2343 end do
2344 end do
2345
2346 safe_deallocate_a(gmat)
2347 safe_deallocate_a(dfl)
2348 safe_deallocate_a(dr)
2349
2352
2353 ! ----------------------------------------------------------------------------------
2365 subroutine nonlocal_pseudopotential_density_matrix_spinor(this, mesh, std, spiral_bnd, psib, weight, beta_density, &
2366 beta_current)
2367 class(nonlocal_pseudopotential_t), target, intent(in) :: this
2368 class(mesh_t), intent(in) :: mesh
2369 type(states_elec_dim_t), intent(in) :: std
2370 logical, intent(in) :: spiral_bnd
2371 type(wfs_elec_t), intent(in) :: psib
2372 real(real64), intent(in) :: weight(:)
2373 real(real64), allocatable, intent(inout) :: beta_density(:, :, :, :)
2375 real(real64), allocatable, optional, intent(inout) :: beta_current(:, :, :, :)
2376
2377 integer :: imat, iproj, ist, iprojection, nprojs
2378 type(projection_t) :: projection
2379 type(projector_matrix_t), pointer :: pmat
2380 complex(real64), allocatable :: pbra_u(:, :), pbra_d(:, :), pket_u(:, :), pket_d(:, :)
2381 complex(real64), allocatable :: rho_uu(:, :), rho_dd(:, :), rho_ud(:, :)
2382
2384
2385 assert(.not. spiral_bnd)
2386 assert(this%apply_projector_matrices)
2387 assert(psib%status() /= batch_device_packed)
2388 assert(std%dim == 2)
2389 assert(size(weight) == psib%nst)
2390
2391 if (.not. allocated(beta_density)) then
2392 safe_allocate(beta_density(1:this%max_nprojs, 1:this%max_nprojs, 1:this%nprojector_matrices, 1:4))
2393 beta_density = m_zero
2394 end if
2395 if (present(beta_current)) then
2396 if (.not. allocated(beta_current)) then
2397 safe_allocate(beta_current(1:this%max_nprojs, 1:this%max_nprojs, 1:this%nprojector_matrices, 1:4))
2398 beta_current = m_zero
2399 end if
2400 end if
2401
2402 call znonlocal_pseudopotential_start(this, mesh, std, spiral_bnd, psib, projection)
2403
2404 if (mesh%parallel_in_domains) then
2405 call mesh%allreduce(projection%zprojection)
2406 end if
2407
2408 iprojection = 0
2409 do imat = 1, this%nprojector_matrices
2410 pmat => this%projector_matrices(imat)
2411 nprojs = pmat%nprojs
2412
2413 ! Split the spinor projections (linear index (ist-1)*dim + idim, idim = 1 up, 2 down) into
2414 ! per-spin matrices P_a(iproj, ist), folding the occupation weight into the bra. Each spin
2415 ! block of the projector density matrix is then a weighted rank-nst update,
2416 ! rho^{ab}(i, j) = sum_n w_n conjg(P_a(i, n)) P_b(j, n) = [(w conjg(P_a)) P_b^T](i, j) .
2417 safe_allocate(pbra_u(1:nprojs, 1:psib%nst))
2418 safe_allocate(pbra_d(1:nprojs, 1:psib%nst))
2419 safe_allocate(pket_u(1:nprojs, 1:psib%nst))
2420 safe_allocate(pket_d(1:nprojs, 1:psib%nst))
2421 do ist = 1, psib%nst
2422 do iproj = 1, nprojs
2423 pket_u(iproj, ist) = projection%zprojection((ist - 1)*std%dim + 1, iprojection + iproj)
2424 pket_d(iproj, ist) = projection%zprojection((ist - 1)*std%dim + 2, iprojection + iproj)
2425 pbra_u(iproj, ist) = weight(ist)*conjg(pket_u(iproj, ist))
2426 pbra_d(iproj, ist) = weight(ist)*conjg(pket_d(iproj, ist))
2427 end do
2428 end do
2429
2430 ! Diagonal spin blocks: their real part is the density (Hermitian rho, symmetric Q); the
2431 ! imaginary part only enters the one-center current.
2432 rho_uu = matmul(pbra_u, transpose(pket_u))
2433 call lalg_axpy(nprojs, nprojs, m_one, real(rho_uu, real64), beta_density(1:nprojs, 1:nprojs, imat, 1))
2434 rho_dd = matmul(pbra_d, transpose(pket_d))
2435 call lalg_axpy(nprojs, nprojs, m_one, real(rho_dd, real64), beta_density(1:nprojs, 1:nprojs, imat, 2))
2436 ! Off-diagonal (up-down) block: n^{ud} = sum_ij Q_ij conjg(pd_i) pu_j (slabs Re, Im).
2437 rho_ud = matmul(pbra_d, transpose(pket_u))
2438 call lalg_axpy(nprojs, nprojs, m_one, real(rho_ud, real64), beta_density(1:nprojs, 1:nprojs, imat, 3))
2439 call lalg_axpy(nprojs, nprojs, m_one, aimag(rho_ud), beta_density(1:nprojs, 1:nprojs, imat, 4))
2440
2441 if (present(beta_current)) then
2442 call nonlocal_pseudopotential_spinor_current_slabs(nprojs, rho_uu, rho_dd, rho_ud, &
2443 beta_current(:, :, imat, :))
2444 end if
2445
2446 safe_deallocate_a(pbra_u)
2447 safe_deallocate_a(pbra_d)
2448 safe_deallocate_a(pket_u)
2449 safe_deallocate_a(pket_d)
2450 safe_deallocate_a(rho_uu)
2451 safe_deallocate_a(rho_dd)
2452 safe_deallocate_a(rho_ud)
2453
2454 iprojection = iprojection + nprojs
2455 end do
2456
2457 safe_deallocate_a(projection%zprojection)
2458
2461
2462 ! ----------------------------------------------------------------------------------
2471 subroutine nonlocal_pseudopotential_spinor_current_slabs(nprojs, rho_uu, rho_dd, rho_ud, cur)
2472 integer, intent(in) :: nprojs
2473 complex(real64), intent(in) :: rho_uu(:, :), rho_dd(:, :), rho_ud(:, :)
2474 real(real64), intent(inout) :: cur(:, :, :)
2475
2476 integer :: ip, jp
2477
2478 ! no PUSH_SUB, called per projector matrix
2479
2480 do jp = 1, nprojs
2481 do ip = 1, nprojs
2482 cur(ip, jp, 1) = cur(ip, jp, 1) + aimag(rho_uu(ip, jp))
2483 cur(ip, jp, 2) = cur(ip, jp, 2) + aimag(rho_dd(ip, jp))
2484 cur(ip, jp, 3) = cur(ip, jp, 3) + m_half*(aimag(rho_ud(ip, jp)) - aimag(rho_ud(jp, ip)))
2485 cur(ip, jp, 4) = cur(ip, jp, 4) - m_half*(real(rho_ud(ip, jp), real64) - real(rho_ud(jp, ip), real64))
2486 end do
2487 end do
2488
2490
2491 ! ----------------------------------------------------------------------------------
2500 subroutine nonlocal_pseudopotential_density_matrix_spinor_cmplx(this, mesh, std, spiral_bnd, psib, weight, beta_density)
2501 class(nonlocal_pseudopotential_t), target, intent(in) :: this
2502 class(mesh_t), intent(in) :: mesh
2503 type(states_elec_dim_t), intent(in) :: std
2504 logical, intent(in) :: spiral_bnd
2505 type(wfs_elec_t), intent(in) :: psib
2506 real(real64), intent(in) :: weight(:)
2507 complex(real64), allocatable, intent(inout) :: beta_density(:, :, :, :, :)
2508
2509 integer :: imat, iproj, jproj, ist, iprojection, nprojs, s1, s2
2510 type(projection_t) :: projection
2511 type(projector_matrix_t), pointer :: pmat
2512 complex(real64) :: pi_s(2), pj_s(2)
2513
2515
2516 assert(.not. spiral_bnd)
2517 assert(this%apply_projector_matrices)
2518 assert(psib%status() /= batch_device_packed)
2519 assert(std%dim == 2)
2520 assert(size(weight) == psib%nst)
2521
2522 if (.not. allocated(beta_density)) then
2523 safe_allocate(beta_density(1:this%max_nprojs, 1:this%max_nprojs, 1:this%nprojector_matrices, 1:2, 1:2))
2524 beta_density = m_z0
2525 end if
2526
2527 call znonlocal_pseudopotential_start(this, mesh, std, spiral_bnd, psib, projection)
2528
2529 if (mesh%parallel_in_domains) then
2530 call mesh%allreduce(projection%zprojection)
2531 end if
2532
2533 iprojection = 0
2534 do imat = 1, this%nprojector_matrices
2535 pmat => this%projector_matrices(imat)
2536 nprojs = pmat%nprojs
2537
2538 do ist = 1, psib%nst
2539 if (abs(weight(ist)) <= m_epsilon) cycle
2540 do iproj = 1, nprojs
2541 pi_s(1) = projection%zprojection((ist - 1)*std%dim + 1, iprojection + iproj)
2542 pi_s(2) = projection%zprojection((ist - 1)*std%dim + 2, iprojection + iproj)
2543 do jproj = iproj, nprojs
2544 pj_s(1) = projection%zprojection((ist - 1)*std%dim + 1, iprojection + jproj)
2545 pj_s(2) = projection%zprojection((ist - 1)*std%dim + 2, iprojection + jproj)
2546 do s1 = 1, 2
2547 do s2 = 1, 2
2548 beta_density(iproj, jproj, imat, s1, s2) = beta_density(iproj, jproj, imat, s1, s2) + &
2549 weight(ist)*conjg(pi_s(s1))*pj_s(s2)
2550 end do
2551 end do
2552 end do
2553 end do
2554 end do
2555
2556 do iproj = 2, nprojs
2557 do jproj = 1, iproj - 1
2558 do s1 = 1, 2
2559 do s2 = 1, 2
2560 beta_density(iproj, jproj, imat, s1, s2) = conjg(beta_density(jproj, iproj, imat, s2, s1))
2561 end do
2562 end do
2563 end do
2564 end do
2565
2566 iprojection = iprojection + nprojs
2567 end do
2568
2569 safe_deallocate_a(projection%zprojection)
2570
2573
2574
2575 ! ----------------------------------------------------------------------------------
2584 subroutine nonlocal_pseudopotential_soc_density_to_real(this, epot, rho_cmplx, beta_density, beta_current)
2585 class(nonlocal_pseudopotential_t), target, intent(in) :: this
2586 type(epot_t), target, intent(in) :: epot
2587 complex(real64), allocatable, intent(in) :: rho_cmplx(:, :, :, :, :)
2588 real(real64), allocatable, intent(inout) :: beta_density(:, :, :, :)
2590 real(real64), allocatable, optional, intent(inout) :: beta_current(:, :, :, :)
2591
2592 integer :: imat, iatom, ip, jp, sa, sb, nprojs, n2
2593 type(projector_matrix_t), pointer :: pmat
2594 type(projector_t), pointer :: proj
2595 complex(real64), allocatable :: gmat(:, :), rho_flat(:, :)
2596
2598
2599 if (.not. allocated(rho_cmplx)) then
2601 return
2602 end if
2603 assert(allocated(beta_density))
2604 if (present(beta_current)) then
2605 if (.not. allocated(beta_current)) then
2606 safe_allocate(beta_current(1:this%max_nprojs, 1:this%max_nprojs, 1:this%nprojector_matrices, 1:4))
2607 beta_current = m_zero
2608 end if
2609 end if
2610
2611 do imat = 1, this%nprojector_matrices
2612 pmat => this%projector_matrices(imat)
2613 nprojs = pmat%nprojs
2614 n2 = 2*nprojs
2615 iatom = this%projector_to_atom(imat)
2616 proj => epot%proj(iatom)
2617 if (.not. projector_has_augmentation(proj)) cycle
2618
2619 ! Recouple rho^c (complex-Y_lm (x) spin) into the real-Y_lm onsite_density_matrix consumed by the
2620 ! augmentation scatter. On composite (projector, spin) indices, idx(p, s) = p + (s-1)*n,
2621 ! this is one matrix sandwich with the mixed (l,j)->spin recoupling matrix G
2622 safe_allocate(gmat(1:n2, 1:n2))
2624
2625 safe_allocate(rho_flat(1:n2, 1:n2))
2626 do sb = 1, 2
2627 do sa = 1, 2
2628 rho_flat((sa-1)*nprojs + 1:sa*nprojs, (sb-1)*nprojs + 1:sb*nprojs) = &
2629 rho_cmplx(1:nprojs, 1:nprojs, imat, sa, sb)
2630 end do
2631 end do
2632 rho_flat = matmul(conjg(transpose(gmat)), matmul(rho_flat, gmat))
2633
2634 ! Unpack into the four real slabs of the scatter (uu, dd, Re du, Im du).
2635 do jp = 1, nprojs
2636 do ip = 1, nprojs
2637 beta_density(ip, jp, imat, 1) = real(rho_flat(ip, jp), real64)
2638 beta_density(ip, jp, imat, 2) = real(rho_flat(nprojs + ip, nprojs + jp), real64)
2639 beta_density(ip, jp, imat, 3) = real(rho_flat(nprojs + ip, jp), real64)
2640 beta_density(ip, jp, imat, 4) = aimag(rho_flat(nprojs + ip, jp))
2641 end do
2642 end do
2643 ! The imaginary parts dropped above carry the one-center current, in the same real-Y_lm basis.
2644 if (present(beta_current)) then
2645 call nonlocal_pseudopotential_spinor_current_slabs(nprojs, rho_flat(1:nprojs, 1:nprojs), &
2646 rho_flat(nprojs + 1:n2, nprojs + 1:n2), rho_flat(nprojs + 1:n2, 1:nprojs), beta_current(:, :, imat, :))
2647 end if
2648
2649 safe_deallocate_a(gmat)
2650 safe_deallocate_a(rho_flat)
2651 end do
2652
2655
2656 ! ---------------------------------------------------------
2660 complex(real64) function soc_recouple_a(ll, jplus, m, m_target, s, uu) result(aval)
2661 integer, intent(in) :: ll, m, m_target, s
2662 logical, intent(in) :: jplus
2663 complex(real64), intent(in) :: uu(:, :)
2664
2665 integer :: ml
2666 real(real64) :: coeff
2667
2668 aval = m_z0
2669 ml = m + (s - 1) ! complex-Y_lm m for spin s in |j, m_j = m+1/2>
2670 if (ml < -ll .or. ml > ll) return
2671 coeff = soc_uspp_spinor_coeff(ll, jplus, m, s)
2672 if (abs(coeff) <= m_epsilon) return
2673 ! A = C^{j m_j}_{l ml 1/2 s} * conjg(uu(a, ml)), a = real-Y_lm index of the projector.
2674 aval = coeff * conjg(uu(m_target + ll + 1, ml + ll + 1))
2675 end function soc_recouple_a
2676
2677 ! ----------------------------------------------------------------------------------
2694 subroutine nonlocal_pseudopotential_soc_recoupling_matrix(pmat, proj, gmat)
2695 type(projector_matrix_t), intent(in) :: pmat
2696 type(projector_t), intent(in) :: proj
2697 complex(real64), intent(out) :: gmat(:, :)
2698
2699 integer :: nprojs, ll, ia, ipc, sa, s1, ma
2700 logical :: jplus
2701 complex(real64), allocatable :: uu(:, :), uu_all(:, :, :)
2702
2704
2705 nprojs = pmat%nprojs
2706 assert(size(gmat, 1) == 2*nprojs .and. size(gmat, 2) == 2*nprojs)
2707
2708 ! Complex->real Y_lm transforms per l, needed by the A coefficient.
2709 safe_allocate(uu(1:2*proj%lmax+1, 1:2*proj%lmax+1))
2710 safe_allocate(uu_all(1:2*proj%lmax+1, 1:2*proj%lmax+1, 0:proj%lmax))
2711 uu_all = m_z0
2712 do ll = 0, proj%lmax
2713 if (ll == proj%lloc) cycle
2714 call soc_uspp_ylm_transform(ll, uu(1:2*ll+1, 1:2*ll+1))
2715 uu_all(1:2*ll+1, 1:2*ll+1, ll) = uu(1:2*ll+1, 1:2*ll+1)
2716 end do
2717 safe_deallocate_a(uu)
2718
2719 gmat = m_z0
2720 do ia = 1, nprojs ! real-basis column projector
2721 ll = pmat%l(ia)
2722 jplus = (ll == 0) .or. (mod(pmat%channel(ia), 2) == 1)
2723 do ipc = 1, nprojs ! complex-basis row projector, same (l, channel) block
2724 if (pmat%l(ipc) /= ll .or. pmat%channel(ipc) /= pmat%channel(ia)) cycle
2725 do sa = 1, 2
2726 ma = pmat%m(ipc) - (sa - 1)
2727 do s1 = 1, 2
2728 gmat(ipc + (sa-1)*nprojs, ia + (s1-1)*nprojs) = &
2729 soc_recouple_a(ll, jplus, ma, pmat%m(ia), s1, uu_all(:, :, ll)) * &
2730 soc_uspp_spinor_coeff(ll, jplus, ma, sa)
2731 end do
2732 end do
2733 end do
2734 end do
2735 safe_deallocate_a(uu_all)
2736
2739
2740 ! ----------------------------------------------------------------------------------
2746 real(real64) function nonlocal_pseudopotential_aug_contract(proj, pmat, np, ncomp, vweight, wij, cw, volume_element) &
2747 result(res)
2748 type(projector_t), intent(in) :: proj
2749 type(projector_matrix_t), intent(in) :: pmat
2750 integer, intent(in) :: np, ncomp
2751 real(real64), intent(in) :: vweight(:, :)
2752 real(real64), intent(in) :: wij(:, :, :)
2753 real(real64), intent(in) :: cw(:)
2754 real(real64), intent(in) :: volume_element
2755
2756 integer :: iproj, jproj
2757 real(real64) :: vv(ncomp)
2758 real(real64), allocatable :: pairm(:, :, :)
2759
2760 ! no PUSH_SUB, called per force/stress component
2761
2762 safe_allocate(pairm(1:pmat%nprojs, 1:pmat%nprojs, 1:ncomp))
2763 call nonlocal_pseudopotential_qij_to_pairs(proj, pmat, np, ncomp, vweight, pairm)
2764 res = m_zero
2765 do iproj = 1, pmat%nprojs
2766 do jproj = 1, pmat%nprojs
2767 vv(1:ncomp) = wij(iproj, jproj, 1:ncomp)
2768 if (sum(abs(vv(1:ncomp))) <= m_epsilon) cycle
2769 res = res + sum(cw(1:ncomp)*vv(1:ncomp)*pairm(iproj, jproj, 1:ncomp))
2770 end do
2771 end do
2772 res = res*volume_element
2773 safe_deallocate_a(pairm)
2774
2776
2777 ! ----------------------------------------------------------------------------------
2798
2799 !! The spin contraction is F^aug_a(I) = - sum_ij Tr_spin[ rho_ij . (d_a V_eff) ] Q_ij(r-R_I) ,
2800 !! which for spinors reads rho^{uu} dV^{uu} + rho^{dd} dV^{dd} + 2 rho^{Re ud} dV^{Re ud}
2801 !! + 2 rho^{Im ud} dV^{Im ud}.
2802 !! For PAW this is the F_2 compensation-charge force of Torrent et al., Comput. Mater. Sci.
2803 !! 42, 337 (2008), Eq. (31); the remaining PAW terms (Eqs. (30), (32)-(33)) are carried by
2804 !! the standard local, nonlocal (with the full D including ddd_paw) and NLCC forces.
2805 subroutine nonlocal_pseudopotential_force_uspp(this, mesh, epot, ndim, grad_pot, force_aug_weight, force)
2806 class(nonlocal_pseudopotential_t), target, intent(in) :: this
2807 class(mesh_t), intent(in) :: mesh
2808 type(epot_t), target, intent(in) :: epot
2809 integer, intent(in) :: ndim
2810 real(real64), intent(in) :: grad_pot(:, :, :)
2811 real(real64), intent(in) :: force_aug_weight(:, :, :, :)
2812 real(real64), intent(inout) :: force(:, :)
2813
2814 integer :: imat, iatom, is, np, idir
2815 integer :: ncomp, icomp
2816 real(real64) :: cw(4)
2817 real(real64), allocatable :: vweight(:, :)
2818 real(real64), allocatable :: faug(:, :)
2819 type(projector_matrix_t), pointer :: pmat
2820 type(projector_t), pointer :: proj
2821
2822 push_sub_with_profile(nonlocal_pseudopotential_force_uspp)
2823
2824 ncomp = size(force_aug_weight, dim=4)
2825 assert(ncomp >= 1 .and. ncomp <= 4)
2826 assert(size(grad_pot, dim=3) == ncomp)
2827
2828 ! Spin-trace contraction weights
2829 cw = m_one
2830 cw(3:4) = m_two
2831
2832 safe_allocate(faug(1:ndim, 1:ubound(force, dim=2)))
2833 faug = m_zero
2834
2835 do imat = 1, this%nprojector_matrices
2836 pmat => this%projector_matrices(imat)
2837 iatom = this%projector_to_atom(imat)
2838 proj => epot%proj(iatom)
2839
2840 if (.not. projector_has_augmentation(proj)) cycle
2841 if (.not. allocated(pmat%qmix)) cycle
2842 assert(allocated(proj%aug%qflm))
2843
2844 np = pmat%npoints
2845
2846 safe_allocate(vweight(1:np, 1:ncomp))
2847
2848 do idir = 1, ndim
2849 ! Gather the idir component of grad V_eff at the sphere points, per spin component.
2850 do icomp = 1, ncomp
2851 do is = 1, np
2852 vweight(is, icomp) = grad_pot(pmat%map(is), idir, icomp)
2853 end do
2854 end do
2855
2856 ! - sum_ij Tr_spin[rho_ij . (d_a V_eff)] Q_ij dr
2857 faug(idir, iatom) = faug(idir, iatom) - nonlocal_pseudopotential_aug_contract(proj, pmat, np, ncomp, &
2858 vweight, force_aug_weight(:, :, imat, :), cw, mesh%volume_element)
2859 end do
2860
2861 safe_deallocate_a(vweight)
2862 end do
2863
2864 ! The augmentation sphere may be split across domains, so reduce the integral.
2865 if (mesh%parallel_in_domains) call mesh%allreduce(faug)
2866
2867 force(1:ndim, :) = force(1:ndim, :) + faug(1:ndim, :)
2868
2869 safe_deallocate_a(faug)
2870
2871 pop_sub_with_profile(nonlocal_pseudopotential_force_uspp)
2873
2874 ! ----------------------------------------------------------------------------------
2895 subroutine nonlocal_pseudopotential_stress_uspp(this, mesh, epot, periodic_dim, grad_pot, aug_weight, stress_aug)
2896 class(nonlocal_pseudopotential_t), target, intent(in) :: this
2897 class(mesh_t), intent(in) :: mesh
2898 type(epot_t), target, intent(in) :: epot
2899 integer, intent(in) :: periodic_dim
2900 real(real64), intent(in) :: grad_pot(:, :, :)
2901 real(real64), intent(in) :: aug_weight(:, :, :, :)
2902 real(real64), intent(out) :: stress_aug(3, 3)
2903
2904 integer :: imat, iatom, is, np, adir, bdir
2905 integer :: ncomp, icomp
2906 real(real64) :: cw(4)
2907 real(real64), allocatable :: vweight(:, :)
2908 type(projector_matrix_t), pointer :: pmat
2909 type(projector_t), pointer :: proj
2910
2911 push_sub_with_profile(nonlocal_pseudopotential_stress_uspp)
2912
2913 ncomp = size(aug_weight, dim=4)
2914 assert(ncomp >= 1 .and. ncomp <= 4)
2915 assert(size(grad_pot, dim=3) == ncomp)
2916
2917 ! Spin-trace contraction weights
2918 cw = m_one
2919 cw(3:4) = m_two
2920
2921 stress_aug = m_zero
2922
2923 do imat = 1, this%nprojector_matrices
2924 pmat => this%projector_matrices(imat)
2925 iatom = this%projector_to_atom(imat)
2926 proj => epot%proj(iatom)
2927
2928 if (.not. projector_has_augmentation(proj)) cycle
2929 if (.not. allocated(pmat%qmix)) cycle
2930 assert(allocated(proj%aug%qflm))
2931
2932 np = pmat%npoints
2933
2934 safe_allocate(vweight(1:np, 1:ncomp))
2935
2936 do adir = 1, periodic_dim
2937 do bdir = 1, periodic_dim
2938 ! Gather the adir component of grad V_eff, weighted by the bdir component of the
2939 ! position relative to the atom, at the sphere points. One parallel region for all
2940 ! spin components; the nowait is safe as every (is, icomp) is written exactly once.
2941 !$omp parallel private(icomp)
2942 do icomp = 1, ncomp
2943 !$omp do schedule(static)
2944 do is = 1, np
2945 vweight(is, icomp) = grad_pot(pmat%map(is), adir, icomp)*pmat%position(bdir, is)
2946 end do
2947 !$omp end do nowait
2948 end do
2949 !$omp end parallel
2950
2951 ! T_ab += sum_ij Tr_spin[rho_ij . (d_a V_eff)] (r-R_I)_b Q_ij dr
2952 stress_aug(adir, bdir) = stress_aug(adir, bdir) + nonlocal_pseudopotential_aug_contract(proj, pmat, &
2953 np, ncomp, vweight, aug_weight(:, :, imat, :), cw, mesh%volume_element)
2954 end do
2955 end do
2956
2957 safe_deallocate_a(vweight)
2958 end do
2959
2960 ! The augmentation sphere may be split across domains, so reduce the integral.
2961 if (mesh%parallel_in_domains) call mesh%allreduce(stress_aug)
2962
2963 pop_sub_with_profile(nonlocal_pseudopotential_stress_uspp)
2965
2966 ! ----------------------------------------------------------------------------------
2986 subroutine nonlocal_pseudopotential_aug_moment(this, mesh, epot, op, aug_op)
2987 class(nonlocal_pseudopotential_t), target, intent(in) :: this
2988 class(mesh_t), intent(in) :: mesh
2989 type(epot_t), target, intent(in) :: epot
2990 real(real64), intent(in) :: op(:)
2991 real(real64), intent(inout) :: aug_op(:, :, :)
2992
2993 integer :: imat, iatom, is, np
2994 real(real64), allocatable :: vweight(:, :), pairm(:, :, :)
2995 type(projector_matrix_t), pointer :: pmat
2996 type(projector_t), pointer :: proj
2997
2998 push_sub_with_profile(nonlocal_pseudopotential_aug_moment)
2999
3000 aug_op = m_zero
3001
3002 do imat = 1, this%nprojector_matrices
3003 pmat => this%projector_matrices(imat)
3004 iatom = this%projector_to_atom(imat)
3005 proj => epot%proj(iatom)
3006
3007 if (.not. projector_has_augmentation(proj)) cycle
3008 assert(allocated(proj%aug%qflm))
3009
3010 np = pmat%npoints
3011
3012 ! Gather the operator A(r) at the sphere points (absolute mesh coordinates).
3013 safe_allocate(vweight(1:np, 1:1))
3014 do is = 1, np
3015 vweight(is, 1) = op(pmat%map(is))
3016 end do
3017
3018 ! aug_op(i,j,I) = int A(r) Q_ij(r-R_I) dr: integrate the gathered operator
3019 ! against the augmentation charges (A = V_eff is the update_dij case).
3020 safe_allocate(pairm(1:pmat%nprojs, 1:pmat%nprojs, 1:1))
3021 call nonlocal_pseudopotential_qij_to_pairs(proj, pmat, np, 1, vweight, pairm)
3022 aug_op(1:pmat%nprojs, 1:pmat%nprojs, imat) = pairm(1:pmat%nprojs, 1:pmat%nprojs, 1)*mesh%volume_element
3023
3024 safe_deallocate_a(vweight)
3025 safe_deallocate_a(pairm)
3026
3027 ! The augmentation sphere may be split across domains, so reduce the integral.
3028 if (mesh%parallel_in_domains) then
3029 call mesh%allreduce(aug_op(:, :, imat), dim = (/pmat%nprojs, pmat%nprojs/))
3030 end if
3031 end do
3032
3033 pop_sub_with_profile(nonlocal_pseudopotential_aug_moment)
3035
3036 ! ----------------------------------------------------------------------------------
3044 subroutine nonlocal_pseudopotential_aug_density(this, mesh, epot, force_aug_weight, aug_density)
3045 class(nonlocal_pseudopotential_t), target, intent(in) :: this
3046 class(mesh_t), intent(in) :: mesh
3047 type(epot_t), target, intent(in) :: epot
3048 real(real64), intent(in) :: force_aug_weight(:, :, :)
3049 real(real64), intent(inout) :: aug_density(:)
3050
3051 integer :: imat, iatom, is, ip, np
3052 real(real64), allocatable :: acc(:), wcol(:, :)
3053 type(projector_matrix_t), pointer :: pmat
3054 type(projector_t), pointer :: proj
3055
3056 push_sub_with_profile(nonlocal_pseudopotential_aug_density)
3057
3058 do imat = 1, this%nprojector_matrices
3059 pmat => this%projector_matrices(imat)
3060 iatom = this%projector_to_atom(imat)
3061 proj => epot%proj(iatom)
3062
3063 if (.not. projector_has_augmentation(proj)) cycle
3064 if (.not. allocated(pmat%qmix)) cycle
3065 assert(allocated(proj%aug%qflm))
3066
3067 np = pmat%npoints
3068
3069 ! The Gaunt sums reduce to scalar weights per combined Q Y_LM column,
3070 ! and the grid work to a single matrix-vector product acc = aug_qflm . wcol.
3071 safe_allocate(wcol(1:proj%aug%qflm_ncombo, 1:1))
3072 wcol = m_zero
3073 call projector_aug_pair_to_columns(proj, pmat, 1, force_aug_weight(:, :, imat:imat), wcol)
3074
3075 if (np > 0) then
3076 safe_allocate(acc(1:np))
3077 call blas_gemv('N', np, proj%aug%qflm_ncombo, m_one, proj%aug%qflm(1, 1), np, &
3078 wcol(1, 1), 1, m_zero, acc(1), 1)
3079
3080 do is = 1, np
3081 ip = pmat%map(is)
3082 aug_density(ip) = aug_density(ip) + acc(is)
3083 end do
3084 safe_deallocate_a(acc)
3085 end if
3086
3087 safe_deallocate_a(wcol)
3088 end do
3089
3090 pop_sub_with_profile(nonlocal_pseudopotential_aug_density)
3092
3093
3094#include "undef.F90"
3095#include "real.F90"
3096#include "nonlocal_pseudopotential_inc.F90"
3097
3098#include "undef.F90"
3099#include "complex.F90"
3100#include "nonlocal_pseudopotential_inc.F90"
3101
3103
3104!! Local Variables:
3105!! mode: f90
3106!! coding: utf-8
3107!! End:
Copies a vector x, to a vector y.
Definition: lalg_basic.F90:197
double sqrt(double __x) __attribute__((__nothrow__
subroutine, public accel_free_buffer(this, async)
Definition: accel.F90:967
subroutine, public accel_finish()
Definition: accel.F90:1104
subroutine, public accel_detach_buffer(this)
Clear a buffer handle without freeing device memory.
Definition: accel.F90:1036
logical pure function, public accel_buffer_is_allocated(this)
Definition: accel.F90:1096
pure logical function, public accel_is_enabled()
Definition: accel.F90:391
integer, parameter, public accel_mem_read_only
Definition: accel.F90:162
This module implements batches of mesh functions.
Definition: batch.F90:112
This module implements common operations on batches of mesh functions.
Definition: batch_ops.F90:95
This module contains interfaces for BLAS routines You should not use these routines directly....
Definition: blas.F90:97
The Ono-Hirose double-grid technique [T. Ono and K. Hirose, Phys. Rev. Lett. 82, 5016 (1999); Phys....
integer, parameter, public spinors
integer, parameter, public spin_polarized
integer, parameter, public spin_orbit
Definition: epot.F90:150
real(real64), parameter, public m_zero
Definition: global.F90:173
complex(real64), parameter, public m_z0
Definition: global.F90:183
real(real64), parameter, public m_half
Definition: global.F90:179
real(real64), parameter, public m_one
Definition: global.F90:174
This module implements the underlying real-space grid.
Definition: grid.F90:96
This module is intended to contain "only mathematical" functions and procedures.
Definition: math.F90:94
This module defines various routines, operating on mesh functions.
This module defines the meshes, which are used in Octopus.
Definition: mesh.F90:97
subroutine, public messages_warning(no_lines, all_nodes, namespace)
Definition: messages.F90:501
character(len=256), dimension(max_lines), public message
to be output by fatal, warning
Definition: messages.F90:138
subroutine, public messages_info(no_lines, iunit, debug_only, stress, all_nodes, namespace)
Definition: messages.F90:570
Exact inverse of the ultrasoft/PAW generalized overlap operator for overlapping spheres.
subroutine, public nonlocal_pseudopotential_force_uspp(this, mesh, epot, ndim, grad_pot, force_aug_weight, force)
Ultrasoft augmentation contribution to the forces.
subroutine nonlocal_pseudopotential_destroy_proj(this)
Destroy the data of nonlocal_pseudopotential_t.
subroutine nonlocal_pseudopotential_detach_accel_buffers(this)
Clear copied accelerator handles so they do not alias source buffers.
subroutine dnonlocal_pseudopotential_force(this, mesh, st, spiral_bnd, iqn, ndim, psi1b, psi2b, force)
calculate contribution to forces, from non-local potentials
subroutine znonlocal_pseudopotential_position_commutator(this, mesh, std, spiral_bnd, psib, commpsib, async)
apply the commutator between the non-local potential and the position to the wave functions.
subroutine, public nonlocal_pseudopotential_aug_density(this, mesh, epot, force_aug_weight, aug_density)
Accumulate the ultrasoft augmentation density n_aug(r) = sum_ij rho_ij Q_ij(r-R) (summed over all ult...
subroutine znonlocal_pseudopotential_r_vnlocal(this, mesh, std, spiral_bnd, psib, commpsib, eigenval, only_qmix)
Accumulates to commpsib the result of x V_{nl} | psib >
subroutine, public znonlocal_pseudopotential_start(this, mesh, std, spiral_bnd, psib, projection, async)
Start application of non-local potentials (stored in the Hamiltonian) to the wave functions.
subroutine, public nonlocal_pseudopotential_stress_uspp(this, mesh, epot, periodic_dim, grad_pot, aug_weight, stress_aug)
Ultrasoft augmentation contribution to the stress tensor.
subroutine, public znonlocal_pseudopotential_add_overlap_inplace(this, mesh, std, xb, phase, spiral_bnd, apply_phase)
In-place version of the generalized overlap: xb := S xb (or S^{-1} xb, depending on this)....
subroutine nonlocal_pseudopotential_soc_build_zmix_cmplx(pmat, proj, use_qij, so_strength)
Build the spin-orbit 2x2 spin coupling (zmix) in the COMPLEX-Y_lm projector basis.
subroutine nonlocal_pseudopotential_paw_onsite(this, epot, n_paw, paw_imat, onsite_density_matrix, dij_buf)
Compute the PAW one-center Hamiltonian coupling of every PAW atom and accumulate the energy correctio...
integer function projector_beta_index(proj, ll, ic)
subroutine, public dnonlocal_pseudopotential_density_matrix(this, mesh, std, spiral_bnd, psib, weight, beta_density, weight2, beta_density2, beta_current)
Accumulate occupation-weighted beta-projector density matrices for USPP density.
subroutine znonlocal_pseudopotential_force(this, mesh, st, spiral_bnd, iqn, ndim, psi1b, psi2b, force)
calculate contribution to forces, from non-local potentials
subroutine dnonlocal_pseudopotential_force_overlap_cross(this, mesh, st, iqn, ndim, psib, gpsib, chib, gchib, force)
Ultrasoft DFT+U force: the overlap-derivative (d_R S) term, as a cross density matrix.
real(real64) function nonlocal_pseudopotential_aug_contract(proj, pmat, np, ncomp, vweight, wij, cw, volume_element)
Integrate a gathered per-sphere-point field against the augmentation charges and contract with the co...
subroutine, public dnonlocal_pseudopotential_apply_overlap(this, mesh, std, psib, spsib, phase, spiral_bnd, apply_phase)
Apply the ultrasoft/PAW generalized overlap S = 1 + sum_ij |beta_i> q_ij <beta_j| (or,...
subroutine dnonlocal_pseudopotential_finish(this, mesh, spiral_bnd, std, projection, vpsib)
finish the application of non-local potentials.
subroutine, public nonlocal_pseudopotential_accel_rebuild(this, space, mesh)
Rebuild accelerator buffers after an intrinsic copy.
subroutine dnonlocal_pseudopotential_position_commutator(this, mesh, std, spiral_bnd, psib, commpsib, async)
apply the commutator between the non-local potential and the position to the wave functions.
subroutine, public nonlocal_pseudopotential_share_projector_data(this, donor)
Alias the heavy per-point projector data of a donor instance.
subroutine, public nonlocal_pseudopotential_rebind_projectors(this, epot)
Rebind projector matrix pointers (map, position) to a target epot.
subroutine znonlocal_pseudopotential_force_overlap_cross(this, mesh, st, iqn, ndim, psib, gpsib, chib, gchib, force)
Ultrasoft DFT+U force: the overlap-derivative (d_R S) term, as a cross density matrix.
subroutine, public znonlocal_pseudopotential_reconstruct_paw_wfs(this, epot, mesh, ik, psi)
Add the PAW one-center correction to a single smooth wavefunction, reconstructing the all-electron wa...
subroutine dnonlocal_pseudopotential_r_vnlocal(this, mesh, std, spiral_bnd, psib, commpsib, eigenval, only_qmix)
Accumulates to commpsib the result of x V_{nl} | psib >
subroutine, public dnonlocal_pseudopotential_reconstruct_paw_wfs(this, epot, mesh, ik, psi)
Add the PAW one-center correction to a single smooth wavefunction, reconstructing the all-electron wa...
subroutine nonlocal_pseudopotential_build_accel_buffers(this, space, mesh)
Build accelerator buffers for projectors from host-side projector matrices.
subroutine, public nonlocal_pseudopotential_soc_density_to_real(this, epot, rho_cmplx, beta_density, beta_current)
Rotate the complex-Y_lm projector density matrix to the real-Y_lm basis (SOC USPP).
subroutine, public znonlocal_pseudopotential_density_matrix(this, mesh, std, spiral_bnd, psib, weight, beta_density, weight2, beta_density2, beta_current)
Accumulate occupation-weighted beta-projector density matrices for USPP density.
subroutine, public znonlocal_pseudopotential_apply_overlap(this, mesh, std, psib, spsib, phase, spiral_bnd, apply_phase)
Apply the ultrasoft/PAW generalized overlap S = 1 + sum_ij |beta_i> q_ij <beta_j| (or,...
subroutine nonlocal_pseudopotential_qij_to_pairs(proj, pmat, np, ncomp, field, pairm)
Integrate a mesh field against the augmentation charges Q_ij over one augmentation sphere: pairm_ij^c...
subroutine, public nonlocal_pseudopotential_aug_moment(this, mesh, epot, op, aug_op)
Ultrasoft augmentation moments of a local operator A(r): int A(r) Q_ij(r-R_I) dr.
subroutine, public nonlocal_pseudopotential_density_matrix_spinor(this, mesh, std, spiral_bnd, psib, weight, beta_density, beta_current)
Noncollinear (spinor) projector density matrix for the ultrasoft augmentation.
subroutine nonlocal_pseudopotential_soc_screen_cmplx(pmat, proj, mesh, veff, dij_onsite)
Add the screening int V^{ab}(r) Q_ij(r) to the complex-Y_lm SOC zmix.
subroutine, public dnonlocal_pseudopotential_add_overlap_inplace(this, mesh, std, xb, phase, spiral_bnd, apply_phase)
In-place version of the generalized overlap: xb := S xb (or S^{-1} xb, depending on this)....
subroutine nonlocal_pseudopotential_spinor_current_slabs(nprojs, rho_uu, rho_dd, rho_ud, cur)
Accumulate the four one-center current slabs of a spinor projector density matrix.
subroutine, public nonlocal_pseudopotential_symmetrize_onsite(this)
Symmetrize the PAW one-center density matrix rho_ij over the crystal symmetries.
subroutine, public nonlocal_pseudopotential_density_matrix_spinor_cmplx(this, mesh, std, spiral_bnd, psib, weight, beta_density)
Full COMPLEX spinor projector density matrix (fully-relativistic ultrasoft).
logical pure function nonlocal_pseudopotential_self_overlap(this)
Returns .true. if the Hamiltonian contains projectors, which overlap with themself.
subroutine nonlocal_pseudopotential_ghost_check(this, mesh, epot)
Warn about augmentation ghost channels: a near-singular direction of the channel-block overlap metric...
subroutine nonlocal_pseudopotential_init(this)
initialize the nonlocal_pseudopotential_t object
subroutine, public dnonlocal_pseudopotential_start(this, mesh, std, spiral_bnd, psib, projection, async)
Start application of non-local potentials (stored in the Hamiltonian) to the wave functions.
subroutine nonlocal_pseudopotential_soc_recoupling_matrix(pmat, proj, gmat)
Build the mixed (l,j)->spin recoupling matrix G on composite (projector, spin) indices.
subroutine, public nonlocal_pseudopotential_detach_copy(this, donor)
Detach the projector data of an instance obtained by intrinsic assignment.
complex(real64) function soc_recouple_a(ll, jplus, m, m_target, s, uu)
A(l,jplus,m,m_l_target,s) = spinor_coeff(l,jplus,m,s) * conjg(uu(a, m_l+l+1)), with m_l = m + (s-1) r...
subroutine, public nonlocal_pseudopotential_update_dij(this, mesh, epot, veff, add_bare, onsite_density_matrix)
Screen the ultrasoft nonlocal coupling matrix D_ij with the local potential.
subroutine nonlocal_pseudopotential_build_proj(this, space, mesh, epot, ispin, overlap_operator, inverse_overlap_operator)
build the projectors for the application of pseudo-potentials
subroutine nonlocal_pseudopotential_build_projector_phase_accel_buffer(this)
Rebuild projector phase accelerator buffer from host-side projector phases.
subroutine, public nonlocal_pseudopotential_build_paw_symm(this, epot, space, gr, ions)
Build the crystal-symmetry data used to symmetrize the PAW one-center density matrix.
subroutine znonlocal_pseudopotential_finish(this, mesh, spiral_bnd, std, projection, vpsib)
finish the application of non-local potentials.
PAW radial engine.
Definition: paw_onsite.F90:131
/* To use the profiling module, call profiling_in and profiling_out with the label of the region as a...
Definition: profiling.F90:122
subroutine, public profiling_out(label)
Increment out counter and sum up difference between entry and exit time.
Definition: profiling.F90:672
subroutine, public profiling_in(label, exclude)
Increment in counter and save entry time.
Definition: profiling.F90:604
subroutine, public projector_matrix_deallocate(this, owns_data)
subroutine, public projector_matrix_allocate(this, nprojs, sphere, has_mix_matrix, is_cmplx, complex_mix, nspin_channels)
logical elemental function, public projector_is(p, type)
Definition: projector.F90:204
logical elemental function, public projector_is_null(p)
Definition: projector.F90:197
Definition: ps.F90:93
integer, parameter, public proj_hgh
Definition: ps.F90:151
integer, parameter, public proj_uspp
Definition: ps.F90:151
integer, parameter, public proj_rkb
Definition: ps.F90:151
integer, parameter, public proj_none
Definition: ps.F90:151
integer, parameter, public proj_kb
Definition: ps.F90:151
Spin-orbit coupling for fully-relativistic ultrasoft pseudopotentials.
Definition: soc_uspp.F90:104
This module handles spin dimensions of the states and the k-point distribution.
logical function, public submesh_overlap(sm1, sm2, space)
Definition: submesh.F90:719
type(type_t), parameter, public type_cmplx
Definition: types.F90:114
type(type_t), parameter, public type_integer
Definition: types.F90:115
type(type_t), parameter, public type_float
Definition: types.F90:113
subroutine build_inverse_overlap_sim_grid(pmat, mesh)
Rebuild the inverse-overlap mix matrix (mix = -T, with T = q (1 + M q)^{-1}) using the projector over...
logical function projector_counted(proj)
Describes mesh distribution to nodes.
Definition: mesh.F90:165
Class for projections of wave functions.
A set of projectors defined on a submesh.
The projector data type is intended to hold the local and non-local parts of the pseudopotentials....
Definition: projector.F90:168
The rkb_projector data type holds the KB projectors build with total angular momentum eigenfunctions....
int true(void)