39 use,
intrinsic :: iso_fortran_env
85 real(real64),
contiguous,
pointer :: density(:, :)
87 type(states_elec_t),
pointer :: st => null()
88 type(grid_t),
pointer :: gr => null()
89 type(epot_t),
pointer :: ep => null()
90 type(nonlocal_pseudopotential_t),
pointer :: vnl => null()
91 type(phase_t),
pointer :: phase => null()
92 type(accel_mem_t) :: buff_density
105 type(density_calc_t),
intent(out) :: this
106 type(states_elec_t),
target,
intent(in) :: st
107 type(grid_t),
target,
intent(in) :: gr
108 real(real64),
contiguous,
target,
intent(out) :: density(:, :)
109 type(epot_t),
optional,
target,
intent(in) :: ep
110 type(nonlocal_pseudopotential_t),
optional,
target,
intent(in) :: vnl
111 type(phase_t),
optional,
target,
intent(in) :: phase
114 logical :: correct_size
124 if (st%requires_generalized_overlap)
then
125 if (.not.
present(ep) .or. .not.
present(vnl))
then
130 if (
present(phase)) this%phase => phase
133 this%density => density
135 do ispin = 1, st%d%nspin
138 this%density(ip, ispin) =
m_zero
144 this%packed = .false.
146 correct_size = ubound(this%density, dim = 1) == this%gr%np .or. &
147 ubound(this%density, dim = 1) == this%gr%np_part
157 type(density_calc_t),
intent(inout) :: this
158 logical,
optional,
intent(in) :: async
180 integer,
intent(in) :: istin
182 integer :: ist, ip, ispin, istin_, bsize, gsize
183 complex(real64) :: term, psi1, psi2
184 real(real64),
allocatable :: weight(:)
191 ispin = this%st%d%get_spin_index(psib%ik)
195 safe_allocate(weight(1:psib%nst))
197 weight(ist) = this%st%kweights(psib%ik)*this%st%occ(psib%ist(ist), psib%ik)
198 if (psib%ist(ist) == istin) istin_ = ist
203 if (abs(weight(istin_)) <=
m_epsilon)
then
213 select case (psib%status())
215 select case (this%st%d%ispin)
219 do ip = 1, this%gr%np
220 this%density(ip, ispin) = this%density(ip, ispin) + weight(ist)*psib%dff(ip, 1, ist)**2
224 do ip = 1, this%gr%np
225 this%density(ip, ispin) = this%density(ip, ispin) + weight(ist)* &
226 real(conjg(psib%zff(ip, 1, ist))*psib%zff(ip, 1, ist), real64)
231 do ip = 1, this%gr%np
232 psi1 = psib%zff(ip, 1, ist)
233 psi2 = psib%zff(ip, 2, ist)
234 this%density(ip, 1) = this%density(ip, 1) + weight(ist)*real(conjg(psi1)*psi1, real64)
235 this%density(ip, 2) = this%density(ip, 2) + weight(ist)*real(conjg(psi2)*psi2, real64)
236 term = weight(ist)*psi1*conjg(psi2)
237 this%density(ip, 3) = this%density(ip, 3) + real(term, real64)
238 this%density(ip, 4) = this%density(ip, 4) + aimag(term)
244 select case (this%st%d%ispin)
248 do ip = 1, this%gr%np
249 this%density(ip, ispin) = this%density(ip, ispin) + weight(ist)*psib%dff_pack(ist, ip)**2
254 do ip = 1, this%gr%np
255 this%density(ip, ispin) = this%density(ip, ispin) + weight(ist)* &
256 real(conjg(psib%zff_pack(ist, ip))*psib%zff_pack(ist, ip), real64)
260 assert(mod(psib%nst_linear, 2) == 0)
262 do ip = 1, this%gr%np
263 psi1 = psib%zff_pack(2*ist - 1, ip)
264 psi2 = psib%zff_pack(2*ist, ip)
265 term = weight(ist)*psi1*conjg(psi2)
267 this%density(ip, 1) = this%density(ip, 1) + weight(ist)*real(conjg(psi1)*psi1, real64)
268 this%density(ip, 2) = this%density(ip, 2) + weight(ist)*real(conjg(psi2)*psi2, real64)
269 this%density(ip, 3) = this%density(ip, 3) + real(term, real64)
270 this%density(ip, 4) = this%density(ip, 4) + aimag(term)
280 select case (this%st%d%ispin)
318 if (this%st%requires_generalized_overlap)
then
322 safe_deallocate_a(weight)
335 logical,
optional,
intent(in) :: async
337 integer :: ist, ip, ispin, bsize, gsize
338 complex(real64) :: term, psi1, psi2
339 real(real64),
allocatable :: weight(:)
346 ispin = this%st%d%get_spin_index(psib%ik)
348 safe_allocate(weight(1:psib%nst))
350 weight(ist) = this%st%kweights(psib%ik)*this%st%occ(psib%ist(ist), psib%ik)
353 select case (psib%status())
355 select case (this%st%d%ispin)
361 do ip = 1, this%gr%np
362 this%density(ip, ispin) = this%density(ip, ispin) + weight(ist)*psib%dff(ip, 1, ist)**2
369 do ip = 1, this%gr%np
370 this%density(ip, ispin) = this%density(ip, ispin) + weight(ist)* &
371 real(conjg(psib%zff(ip, 1, ist))*psib%zff(ip, 1, ist), real64)
379 do ip = 1, this%gr%np
380 psi1 = psib%zff(ip, 1, ist)
381 psi2 = psib%zff(ip, 2, ist)
382 this%density(ip, 1) = this%density(ip, 1) + weight(ist)*real(conjg(psi1)*psi1, real64)
383 this%density(ip, 2) = this%density(ip, 2) + weight(ist)*real(conjg(psi2)*psi2, real64)
384 term = weight(ist)*psi1*conjg(psi2)
385 this%density(ip, 3) = this%density(ip, 3) + real(term, real64)
386 this%density(ip, 4) = this%density(ip, 4) + aimag(term)
393 select case (this%st%d%ispin)
397 do ip = 1, this%gr%np
399 this%density(ip, ispin) = this%density(ip, ispin) + weight(ist)*psib%dff_pack(ist, ip)**2
404 do ip = 1, this%gr%np
406 this%density(ip, ispin) = this%density(ip, ispin) + weight(ist)* &
407 real(conjg(psib%zff_pack(ist, ip))*psib%zff_pack(ist, ip), real64)
412 assert(mod(psib%nst_linear, 2) == 0)
414 do ip = 1, this%gr%np
416 psi1 = psib%zff_pack(2*ist - 1, ip)
417 psi2 = psib%zff_pack(2*ist, ip)
418 term = weight(ist)*psi1*conjg(psi2)
420 this%density(ip, 1) = this%density(ip, 1) + weight(ist)*real(conjg(psi1)*psi1, real64)
421 this%density(ip, 2) = this%density(ip, 2) + weight(ist)*real(conjg(psi2)*psi2, real64)
422 this%density(ip, 3) = this%density(ip, 3) + real(term, real64)
423 this%density(ip, 4) = this%density(ip, 4) + aimag(term)
434 select case (this%st%d%ispin)
477 if (this%st%requires_generalized_overlap)
then
481 safe_deallocate_a(weight)
484 if (this%st%d%ispin /=
spinors)
then
507 integer,
intent(in) :: ispin
508 real(real64),
intent(in) :: weight(:)
510 integer :: imat, iatom, iproj, jproj, ltot, mtot, is, ip, pair_index, lm, np, lmaxq
511 real(real64) :: coeff, val, cval
512 real(real64),
allocatable :: beta_density(:, :, :)
513 real(real64),
allocatable :: acc(:)
521 assert(
associated(this%ep))
522 assert(
associated(this%vnl))
523 assert(this%st%d%ispin /=
spinors)
524 assert(this%st%d%dim == 1)
525 assert(.not. this%gr%use_curvilinear)
526 assert(this%vnl%apply_projector_matrices)
536 if (
associated(this%phase)) do_phase = this%phase%is_allocated()
540 else if (do_phase .and. .not. psib%has_phase)
then
541 call psib%copy_to(phased_psib)
542 call this%phase%apply_to(this%gr, this%gr%np, .false., phased_psib, src = psib)
543 phased_psib%has_phase = .
true.
545 call phased_psib%end()
550 do imat = 1, this%vnl%nprojector_matrices
551 pmat => this%vnl%projector_matrices(imat)
552 iatom = this%vnl%projector_to_atom(imat)
553 proj => this%ep%proj(iatom)
556 assert(
allocated(proj%uspp_qfunc))
557 assert(proj%uspp_qfunc_nbeta > 0)
558 assert(proj%uspp_qfunc_lmax >= 0)
561 lmaxq = proj%uspp_qfunc_lmax
568 if (.not.
allocated(pmat%uspp_ylm))
then
569 safe_allocate(pmat%uspp_ylm(1:np, 1:(lmaxq + 1)**2))
571 do mtot = -ltot, ltot
572 lm = ltot*(ltot + 1) + mtot + 1
574 call ylmr_real(pmat%position(1:3, is), ltot, mtot, pmat%uspp_ylm(is, lm))
580 safe_allocate(acc(1:np))
582 do iproj = 1, pmat%nprojs
583 assert(pmat%beta_index(iproj) >= 1 .and. pmat%beta_index(iproj) <= proj%uspp_qfunc_nbeta)
584 do jproj = 1, pmat%nprojs
585 assert(pmat%beta_index(jproj) >= 1 .and. pmat%beta_index(jproj) <= proj%uspp_qfunc_nbeta)
586 val = beta_density(iproj, jproj, imat)
589 pair_index = (pmat%beta_index(iproj) - 1)*proj%uspp_qfunc_nbeta + pmat%beta_index(jproj)
595 do mtot = -ltot, ltot
596 coeff =
real_gaunt(pmat%l(iproj), pmat%m(iproj), pmat%l(jproj), pmat%m(jproj), ltot, mtot)
597 if (abs(coeff) <= 1.0e-12_real64) cycle
599 lm = ltot*(ltot + 1) + mtot + 1
602 acc(is) = acc(is) + cval*pmat%uspp_ylm(is, lm)*proj%uspp_qfunc(is, pair_index, ltot)
612 this%density(ip, ispin) = this%density(ip, ispin) + acc(is)
615 safe_deallocate_a(acc)
617 if (this%vnl%uspp_save_memory)
then
618 safe_deallocate_a(pmat%uspp_ylm)
622 safe_deallocate_a(beta_density)
636 logical,
optional,
intent(in) :: allreduce
637 logical,
optional,
intent(in) :: symmetrize
638 type(
accel_mem_t),
optional,
intent(inout) :: buff_density
641 real(real64),
allocatable :: tmpdensity(:)
642 logical :: was_packed
644 logical:: allreduce_local, symmetrize_local
650 allreduce_local = (this%st%parallel_in_states .or. this%st%d%kpt%parallel) .and.
optional_default(allreduce, .
true.)
653 was_packed = this%packed
654 if (this%packed)
then
655 this%packed = .false.
657 safe_allocate(tmpdensity(1:this%gr%np))
660 do ispin = 1, this%st%d%nspin
661 call accel_read_buffer(this%buff_density, int(this%gr%np, int64), tmpdensity, offset = (ispin - 1)*this%pnp)
664 do ip = 1, this%gr%np
665 this%density(ip, ispin) = this%density(ip, ispin) + tmpdensity(ip)
669 safe_deallocate_a(tmpdensity)
672 if (.not.
present(buff_density))
then
678 if (allreduce_local)
then
680 call comm_allreduce(this%st%st_kpt_mpi_grp, this%density, dim = (/this%gr%np, this%st%d%nspin/))
684 if (symmetrize_local)
then
685 do ispin = 1, this%st%d%nspin
690 if (
present(buff_density) .and. was_packed)
then
691 if (allreduce_local .or. symmetrize_local)
then
692 do ispin = 1, this%st%d%nspin
694 this%density(1:this%gr%np, ispin), &
695 offset=int((ispin - 1)*this%pnp, int64))
717 subroutine density_calc(st, gr, density, istin, ep, vnl, phase)
719 type(
grid_t),
intent(in) :: gr
720 real(real64),
contiguous,
intent(out) :: density(:, :)
721 integer,
optional,
intent(in) :: istin
722 type(
epot_t),
optional,
target,
intent(in) :: ep
724 type(
phase_t),
optional,
target,
intent(in) :: phase
731 assert(ubound(density, dim = 1) == gr%np .or. ubound(density, dim = 1) == gr%np_part)
733 if (st%requires_generalized_overlap .and.
present(istin))
then
739 if (.not.
present(istin))
then
741 do ik = st%d%kpt%start, st%d%kpt%end
742 do ib = st%group%block_start, st%group%block_end
749 do ik = st%d%kpt%start, st%d%kpt%end
750 do ib = st%group%block_start, st%group%block_end
751 if (st%group%psib(ib, ik)%ist(1) <= istin .and. st%group%psib(ib, ik)%ist(st%group%psib(ib, ik)%nst) >= istin)
then
758 call density_calc_end(dens_calc, allreduce = .not.
present(istin), buff_density = st%buff_density)
768 class(
space_t),
intent(in) :: space
769 type(
grid_t),
intent(in) :: gr
772 integer,
intent(in) :: n
773 logical,
intent(in) :: family_is_mgga
775 integer :: ist, ik, ib, nblock
776 integer :: nodeto, nodefr
778 complex(real64),
allocatable :: psi(:, :, :), rec_buffer(:,:)
784 if (st%requires_generalized_overlap)
then
788 if (n >= st%nst)
then
789 write(
message(1),
'(a)')
'Attempting to freeze a number of orbitals which is larger or equal to'
790 write(
message(2),
'(a)')
'the total number. The program has to stop.'
796 if (.not.
allocated(st%frozen_rho))
then
797 safe_allocate(st%frozen_rho(1:gr%np, 1:st%d%nspin))
802 do ik = st%d%kpt%start, st%d%kpt%end
803 if (n < st%st_start) cycle
805 do ib = st%group%block_start, st%group%block_end
816 safe_allocate(psi(1:gr%np, 1:st%d%dim, 1:nblock))
825 safe_deallocate_a(psi)
836 if (family_is_mgga)
then
837 if (.not.
allocated(st%frozen_tau))
then
838 safe_allocate(st%frozen_tau(1:gr%np, 1:st%d%nspin))
840 if (.not.
allocated(st%frozen_gdens))
then
841 safe_allocate(st%frozen_gdens(1:gr%np, 1:space%dim, 1:st%d%nspin))
843 if (.not.
allocated(st%frozen_ldens))
then
844 safe_allocate(st%frozen_ldens(1:gr%np, 1:st%d%nspin))
848 density_gradient = st%frozen_gdens, density_laplacian = st%frozen_ldens, st_end = n)
856 safe_allocate(psi(1:gr%np, 1:st%d%dim, 1))
857 safe_allocate(rec_buffer(1:gr%np, 1:st%d%dim))
859 do ik = st%d%kpt%start, st%d%kpt%end
863 nodeto = st%node(ist)
865 nodefr = staux%node(ist+n)
867 if (st%mpi_grp%rank == nodeto .and. st%mpi_grp%rank == nodefr)
then
873 if (st%mpi_grp%rank == nodefr)
then
875 call st%mpi_grp%send(psi(1, 1, 1), gr%np*st%d%dim, mpi_double_complex, nodeto, ist)
877 if (st%mpi_grp%rank == nodeto)
then
878 call st%mpi_grp%recv(rec_buffer(1, 1), gr%np*st%d%dim, mpi_double_complex, nodefr, ist)
885 safe_deallocate_a(psi)
886 safe_deallocate_a(rec_buffer)
890 st%occ(ist, ik) = staux%occ(n+ist, ik)
891 st%eigenval(ist, ik) = staux%eigenval(n+ist, ik)
903 class(
mesh_t),
intent(in) :: mesh
905 integer,
intent(in) :: nn
915 safe_deallocate_a(st%eigenval)
916 safe_allocate(st%eigenval(1:st%nst, 1:st%nik))
917 st%eigenval = huge(st%eigenval)
919 safe_deallocate_a(st%occ)
920 safe_allocate(st%occ (1:st%nst, 1:st%nik))
942 do ik = st%d%kpt%start, st%d%kpt%end
943 do ist = st%st_start, st%st_end
944 st%qtot = st%qtot + st%occ(ist, ik) * st%kweights(ik)
948 if (st%parallel_in_states .or. st%d%kpt%parallel)
then
969 class(
mesh_t),
intent(in) :: mesh
970 real(real64),
contiguous,
intent(out) :: total_rho(:,:)
976 call lalg_copy(mesh%np, st%d%nspin, st%rho, total_rho)
978 if (
allocated(st%rho_core))
then
979 do is = 1, st%d%spin_channels
980 call lalg_axpy(mesh%np,
m_one/st%d%spin_channels, st%rho_core, total_rho(:, is))
985 if (
allocated(st%frozen_rho))
then
986 call lalg_axpy(mesh%np, st%d%nspin,
m_one, st%frozen_rho, total_rho)
997 class(
mesh_t),
intent(in) :: mesh
1000 integer(int64) :: pnp
1006 do is = 1, st%d%nspin
1007 call accel_write_buffer(st%buff_density, int(mesh%np, int64), st%rho(1:mesh%np, is), &
1008 offset = (is - 1)*pnp)
1017#include "density_inc.F90"
1020#include "complex.F90"
1021#include "density_inc.F90"
constant times a vector plus a vector
Copies a vector x, to a vector y.
type(accel_kernel_t), target, save, public kernel_density_real
integer function, public accel_kernel_block_size(kernel)
subroutine, public accel_free_buffer(this, async)
subroutine, public accel_finish()
subroutine, public accel_move_buffer(buffer_from, buffer_to)
Move the buffer memory from the first buffer to the second.
type(accel_kernel_t), target, save, public kernel_density_spinors
integer, parameter, public accel_mem_read_write
pure logical function, public accel_is_enabled()
type(accel_kernel_t), target, save, public kernel_density_complex
integer, parameter, public accel_mem_read_only
This module implements batches of mesh functions.
integer, parameter, public batch_not_packed
functions are stored in CPU memory, unpacked order
integer, parameter, public batch_device_packed
functions are stored in device memory in packed order
integer, parameter, public batch_packed
functions are stored in CPU memory, in transposed (packed) order
This module implements common operations on batches of mesh functions.
This module implements a calculator for the density and defines related functions.
subroutine density_calc_state(this, psib, istin)
Calculate contribution to the density from state istin.
subroutine, public density_calc_accumulate(this, psib, async)
Accumulate weighted orbital densities for a batch psib.
subroutine, public states_elec_freeze_adjust_qtot(st)
subroutine, public states_elec_freeze_redistribute_states(st, namespace, mesh, mc, nn)
subroutine, public density_calc(st, gr, density, istin, ep, vnl, phase)
Computes the density from the orbitals in st.
subroutine density_calc_pack(this, async)
prepare the density calculator for GPU use
subroutine, public states_elec_sync_buff_density(st, mesh)
Synchronize the GPU density buffer with the host density strho.
subroutine, public ddensity_accumulate_grad(space, mesh, st, psib, grad_psib, grad_rho)
Accumulate within a batch.
subroutine, public zdensity_accumulate_grad(space, mesh, st, psib, grad_psib, grad_rho)
Accumulate within a batch.
subroutine density_calc_accumulate_uspp(this, psib, ispin, weight)
Accumulate the real-space USPP augmentation density for one wavefunction batch.
subroutine, public states_elec_total_density(st, mesh, total_rho)
This routine calculates the total electronic density.
subroutine, public density_calc_end(this, allreduce, symmetrize, buff_density)
Finalize the density calculation.
subroutine, public states_elec_freeze_orbitals(st, namespace, space, gr, mc, kpoints, n, family_is_mgga)
Calculate partial density for frozen orbitals.
subroutine, public density_calc_init(this, st, gr, density, ep, vnl, phase)
initialize the density calculator
This module calculates the derivatives (gradients, Laplacians, etc.) of a function.
integer, parameter, public unpolarized
Parameters...
integer, parameter, public spinors
integer, parameter, public spin_polarized
real(real64), parameter, public m_zero
real(real64), parameter, public m_epsilon
real(real64), parameter, public m_one
This module implements the underlying real-space grid.
subroutine, public dgrid_symmetrize_scalar_field(gr, field, suppress_warning)
This module is intended to contain "only mathematical" functions and procedures.
subroutine, public ylmr_real(xx, li, mi, ylm)
This is a Numerical Recipes-based subroutine computes real spherical harmonics ylm at position (x,...
real(real64) function, public real_gaunt(l1, m1, l2, m2, l3, m3)
Real Gaunt coefficient , by quadrature.
This module defines various routines, operating on mesh functions.
This module defines the meshes, which are used in Octopus.
subroutine, public messages_not_implemented(feature, namespace)
character(len=256), dimension(max_lines), public message
to be output by fatal, warning
subroutine, public messages_fatal(no_lines, only_root_writes, namespace)
This module handles the communicators for the various parallelization strategies.
subroutine, public znonlocal_pseudopotential_density_matrix(this, mesh, std, spiral_bnd, psib, weight, beta_density)
Accumulate occupation-weighted beta-projector density matrices for USPP density.
subroutine, public dnonlocal_pseudopotential_density_matrix(this, mesh, std, spiral_bnd, psib, weight, beta_density)
Accumulate occupation-weighted beta-projector density matrices for USPP density.
/* To use the profiling module, call profiling_in and profiling_out with the label of the region as a...
subroutine, public profiling_out(label)
Increment out counter and sum up difference between entry and exit time.
subroutine, public profiling_in(label, exclude)
Increment in counter and save entry time.
logical elemental function, public projector_is(p, type)
integer, parameter, public proj_uspp
integer, parameter, public smear_fixed_occ
pure logical function, public states_are_complex(st)
pure logical function, public states_are_real(st)
This module handles spin dimensions of the states and the k-point distribution.
integer pure function, public states_elec_block_max(st, ib)
return index of last state in block ib
subroutine, public states_elec_distribute_nodes(st, namespace, mc)
Distribute states over the processes for states parallelization.
subroutine, public states_elec_calc_quantities(gr, st, kpoints, nlcc, kinetic_energy_density, paramagnetic_current, density_gradient, density_laplacian, gi_kinetic_energy_density, st_end)
calculated selected quantities
subroutine, public states_elec_end(st)
finalize the states_elec_t object
subroutine, public states_elec_deallocate_wfns(st)
Deallocates the KS wavefunctions defined within a states_elec_t structure.
subroutine, public states_elec_allocate_wfns(st, mesh, wfs_type, skip, packed)
Allocates the KS wavefunctions defined within a states_elec_t structure.
subroutine, public states_elec_copy(stout, stin, exclude_wfns, exclude_eigenval, special, dest_type, copy_data)
make a (selective) copy of a states_elec_t object
integer pure function, public states_elec_block_min(st, ib)
return index of first state in block ib
type(type_t), parameter, public type_cmplx
type(type_t), parameter, public type_float
Description of the grid, containing information on derivatives, stencil, and symmetries.
Describes mesh distribution to nodes.
Stores all communicators and groups.
nonlocal part of the pseudopotential
A container for the phase.
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....
The states_elec_t class contains all electronic wave functions.
batches of electronic states