38 use,
intrinsic :: iso_fortran_env
79 real(real64),
contiguous,
pointer :: density(:, :)
81 type(states_elec_t),
pointer :: st
82 type(grid_t),
pointer :: gr
83 type(accel_mem_t) :: buff_density
96 type(density_calc_t),
intent(out) :: this
97 type(states_elec_t),
target,
intent(in) :: st
98 type(grid_t),
target,
intent(in) :: gr
99 real(real64),
contiguous,
target,
intent(out) :: density(:, :)
102 logical :: correct_size
109 this%density => density
111 do ispin = 1, st%d%nspin
114 this%density(ip, ispin) =
m_zero
120 this%packed = .false.
122 correct_size = ubound(this%density, dim = 1) == this%gr%np .or. &
123 ubound(this%density, dim = 1) == this%gr%np_part
133 type(density_calc_t),
intent(inout) :: this
134 logical,
optional,
intent(in) :: async
154 type(density_calc_t),
intent(inout) :: this
155 type(wfs_elec_t),
intent(in) :: psib
156 integer,
intent(in) :: istin
158 integer :: ist, ip, ispin, istin_, bsize, gsize
159 complex(real64) :: term, psi1, psi2
160 real(real64),
allocatable :: weight(:)
161 type(accel_mem_t) :: buff_weight
162 type(accel_kernel_t),
pointer :: kernel
167 ispin = this%st%d%get_spin_index(psib%ik)
171 safe_allocate(weight(1:psib%nst))
173 weight(ist) = this%st%kweights(psib%ik)*this%st%occ(psib%ist(ist), psib%ik)
174 if (psib%ist(ist) == istin) istin_ = ist
189 select case (psib%status())
191 select case (this%st%d%ispin)
195 do ip = 1, this%gr%np
196 this%density(ip, ispin) = this%density(ip, ispin) + weight(ist)*psib%dff(ip, 1, ist)**2
200 do ip = 1, this%gr%np
201 this%density(ip, ispin) = this%density(ip, ispin) + weight(ist)* &
202 real(conjg(psib%zff(ip, 1, ist))*psib%zff(ip, 1, ist), real64)
207 do ip = 1, this%gr%np
208 psi1 = psib%zff(ip, 1, ist)
209 psi2 = psib%zff(ip, 2, ist)
210 this%density(ip, 1) = this%density(ip, 1) + weight(ist)*real(conjg(psi1)*psi1, real64)
211 this%density(ip, 2) = this%density(ip, 2) + weight(ist)*real(conjg(psi2)*psi2, real64)
212 term = weight(ist)*psi1*conjg(psi2)
213 this%density(ip, 3) = this%density(ip, 3) + real(term, real64)
214 this%density(ip, 4) = this%density(ip, 4) + aimag(term)
220 select case (this%st%d%ispin)
224 do ip = 1, this%gr%np
225 this%density(ip, ispin) = this%density(ip, ispin) + weight(ist)*psib%dff_pack(ist, ip)**2
230 do ip = 1, this%gr%np
231 this%density(ip, ispin) = this%density(ip, ispin) + weight(ist)* &
232 real(conjg(psib%zff_pack(ist, ip))*psib%zff_pack(ist, ip), real64)
236 assert(mod(psib%nst_linear, 2) == 0)
238 do ip = 1, this%gr%np
239 psi1 = psib%zff_pack(2*ist - 1, ip)
240 psi2 = psib%zff_pack(2*ist, ip)
241 term = weight(ist)*psi1*conjg(psi2)
243 this%density(ip, 1) = this%density(ip, 1) + weight(ist)*real(conjg(psi1)*psi1, real64)
244 this%density(ip, 2) = this%density(ip, 2) + weight(ist)*real(conjg(psi2)*psi2, real64)
245 this%density(ip, 3) = this%density(ip, 3) + real(term, real64)
246 this%density(ip, 4) = this%density(ip, 4) + aimag(term)
256 select case (this%st%d%ispin)
294 safe_deallocate_a(weight)
307 logical,
optional,
intent(in) :: async
309 integer :: ist, ip, ispin, bsize, gsize
310 complex(real64) :: term, psi1, psi2
311 real(real64),
allocatable :: weight(:)
318 ispin = this%st%d%get_spin_index(psib%ik)
320 safe_allocate(weight(1:psib%nst))
322 weight(ist) = this%st%kweights(psib%ik)*this%st%occ(psib%ist(ist), psib%ik)
325 select case (psib%status())
327 select case (this%st%d%ispin)
333 do ip = 1, this%gr%np
334 this%density(ip, ispin) = this%density(ip, ispin) + weight(ist)*psib%dff(ip, 1, ist)**2
341 do ip = 1, this%gr%np
342 this%density(ip, ispin) = this%density(ip, ispin) + weight(ist)* &
343 real(conjg(psib%zff(ip, 1, ist))*psib%zff(ip, 1, ist), real64)
351 do ip = 1, this%gr%np
352 psi1 = psib%zff(ip, 1, ist)
353 psi2 = psib%zff(ip, 2, ist)
354 this%density(ip, 1) = this%density(ip, 1) + weight(ist)*real(conjg(psi1)*psi1, real64)
355 this%density(ip, 2) = this%density(ip, 2) + weight(ist)*real(conjg(psi2)*psi2, real64)
356 term = weight(ist)*psi1*conjg(psi2)
357 this%density(ip, 3) = this%density(ip, 3) + real(term, real64)
358 this%density(ip, 4) = this%density(ip, 4) + aimag(term)
365 select case (this%st%d%ispin)
369 do ip = 1, this%gr%np
371 this%density(ip, ispin) = this%density(ip, ispin) + weight(ist)*psib%dff_pack(ist, ip)**2
376 do ip = 1, this%gr%np
378 this%density(ip, ispin) = this%density(ip, ispin) + weight(ist)* &
379 real(conjg(psib%zff_pack(ist, ip))*psib%zff_pack(ist, ip), real64)
384 assert(mod(psib%nst_linear, 2) == 0)
386 do ip = 1, this%gr%np
388 psi1 = psib%zff_pack(2*ist - 1, ip)
389 psi2 = psib%zff_pack(2*ist, ip)
390 term = weight(ist)*psi1*conjg(psi2)
392 this%density(ip, 1) = this%density(ip, 1) + weight(ist)*real(conjg(psi1)*psi1, real64)
393 this%density(ip, 2) = this%density(ip, 2) + weight(ist)*real(conjg(psi2)*psi2, real64)
394 this%density(ip, 3) = this%density(ip, 3) + real(term, real64)
395 this%density(ip, 4) = this%density(ip, 4) + aimag(term)
406 select case (this%st%d%ispin)
446 safe_deallocate_a(weight)
449 if (this%st%d%ispin /=
spinors)
then
476 logical,
optional,
intent(in) :: allreduce
477 logical,
optional,
intent(in) :: symmetrize
478 type(
accel_mem_t),
optional,
intent(inout) :: buff_density
481 real(real64),
allocatable :: tmpdensity(:)
482 logical :: was_packed
484 logical:: allreduce_local, symmetrize_local
490 allreduce_local = (this%st%parallel_in_states .or. this%st%d%kpt%parallel) .and.
optional_default(allreduce, .
true.)
493 was_packed = this%packed
494 if (this%packed)
then
495 this%packed = .false.
497 safe_allocate(tmpdensity(1:this%gr%np))
500 do ispin = 1, this%st%d%nspin
501 call accel_read_buffer(this%buff_density, int(this%gr%np, int64), tmpdensity, offset = (ispin - 1)*this%pnp)
504 do ip = 1, this%gr%np
505 this%density(ip, ispin) = this%density(ip, ispin) + tmpdensity(ip)
509 safe_deallocate_a(tmpdensity)
512 if (.not.
present(buff_density))
then
518 if (allreduce_local)
then
520 call comm_allreduce(this%st%st_kpt_mpi_grp, this%density, dim = (/this%gr%np, this%st%d%nspin/))
524 if (symmetrize_local)
then
525 do ispin = 1, this%st%d%nspin
530 if (
present(buff_density) .and. was_packed)
then
531 if (allreduce_local .or. symmetrize_local)
then
532 do ispin = 1, this%st%d%nspin
534 this%density(1:this%gr%np, ispin), &
535 offset=int((ispin - 1)*this%pnp, int64))
559 type(
grid_t),
intent(in) :: gr
560 real(real64),
contiguous,
intent(out) :: density(:, :)
561 integer,
optional,
intent(in) :: istin
568 assert(ubound(density, dim = 1) == gr%np .or. ubound(density, dim = 1) == gr%np_part)
572 if (.not.
present(istin))
then
574 do ik = st%d%kpt%start, st%d%kpt%end
575 do ib = st%group%block_start, st%group%block_end
582 do ik = st%d%kpt%start, st%d%kpt%end
583 do ib = st%group%block_start, st%group%block_end
584 if (st%group%psib(ib, ik)%ist(1) <= istin .and. st%group%psib(ib, ik)%ist(st%group%psib(ib, ik)%nst) >= istin)
then
591 call density_calc_end(dens_calc, allreduce = .not.
present(istin), buff_density = st%buff_density)
601 class(
space_t),
intent(in) :: space
602 type(
grid_t),
intent(in) :: gr
605 integer,
intent(in) :: n
606 logical,
intent(in) :: family_is_mgga
608 integer :: ist, ik, ib, nblock
609 integer :: nodeto, nodefr
611 complex(real64),
allocatable :: psi(:, :, :), rec_buffer(:,:)
617 if (n >= st%nst)
then
618 write(
message(1),
'(a)')
'Attempting to freeze a number of orbitals which is larger or equal to'
619 write(
message(2),
'(a)')
'the total number. The program has to stop.'
625 if (.not.
allocated(st%frozen_rho))
then
626 safe_allocate(st%frozen_rho(1:gr%np, 1:st%d%nspin))
631 do ik = st%d%kpt%start, st%d%kpt%end
632 if (n < st%st_start) cycle
634 do ib = st%group%block_start, st%group%block_end
645 safe_allocate(psi(1:gr%np, 1:st%d%dim, 1:nblock))
654 safe_deallocate_a(psi)
665 if (family_is_mgga)
then
666 if (.not.
allocated(st%frozen_tau))
then
667 safe_allocate(st%frozen_tau(1:gr%np, 1:st%d%nspin))
669 if (.not.
allocated(st%frozen_gdens))
then
670 safe_allocate(st%frozen_gdens(1:gr%np, 1:space%dim, 1:st%d%nspin))
672 if (.not.
allocated(st%frozen_ldens))
then
673 safe_allocate(st%frozen_ldens(1:gr%np, 1:st%d%nspin))
677 density_gradient = st%frozen_gdens, density_laplacian = st%frozen_ldens, st_end = n)
685 safe_allocate(psi(1:gr%np, 1:st%d%dim, 1))
686 safe_allocate(rec_buffer(1:gr%np, 1:st%d%dim))
688 do ik = st%d%kpt%start, st%d%kpt%end
692 nodeto = st%node(ist)
694 nodefr = staux%node(ist+n)
696 if (st%mpi_grp%rank == nodeto .and. st%mpi_grp%rank == nodefr)
then
702 if (st%mpi_grp%rank == nodefr)
then
704 call st%mpi_grp%send(psi(1, 1, 1), gr%np*st%d%dim, mpi_double_complex, nodeto, ist)
706 if (st%mpi_grp%rank == nodeto)
then
707 call st%mpi_grp%recv(rec_buffer(1, 1), gr%np*st%d%dim, mpi_double_complex, nodefr, ist)
714 safe_deallocate_a(psi)
715 safe_deallocate_a(rec_buffer)
719 st%occ(ist, ik) = staux%occ(n+ist, ik)
720 st%eigenval(ist, ik) = staux%eigenval(n+ist, ik)
732 class(
mesh_t),
intent(in) :: mesh
734 integer,
intent(in) :: nn
744 safe_deallocate_a(st%eigenval)
745 safe_allocate(st%eigenval(1:st%nst, 1:st%nik))
746 st%eigenval = huge(st%eigenval)
748 safe_deallocate_a(st%occ)
749 safe_allocate(st%occ (1:st%nst, 1:st%nik))
771 do ik = st%d%kpt%start, st%d%kpt%end
772 do ist = st%st_start, st%st_end
773 st%qtot = st%qtot + st%occ(ist, ik) * st%kweights(ik)
777 if (st%parallel_in_states .or. st%d%kpt%parallel)
then
798 class(
mesh_t),
intent(in) :: mesh
799 real(real64),
contiguous,
intent(out) :: total_rho(:,:)
805 call lalg_copy(mesh%np, st%d%nspin, st%rho, total_rho)
807 if (
allocated(st%rho_core))
then
808 do is = 1, st%d%spin_channels
809 call lalg_axpy(mesh%np,
m_one/st%d%spin_channels, st%rho_core, total_rho(:, is))
814 if (
allocated(st%frozen_rho))
then
815 call lalg_axpy(mesh%np, st%d%nspin,
m_one, st%frozen_rho, total_rho)
826 class(
mesh_t),
intent(in) :: mesh
829 integer(int64) :: pnp
835 do is = 1, st%d%nspin
836 call accel_write_buffer(st%buff_density, int(mesh%np, int64), st%rho(1:mesh%np, is), &
837 offset = (is - 1)*pnp)
846#include "density_inc.F90"
849#include "complex.F90"
850#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_init(this, st, gr, density)
initialize the density calculator
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, 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(st, gr, density, istin)
Computes the density from the orbitals in st.
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.
This module defines various routines, operating on mesh functions.
This module defines the meshes, which are used in Octopus.
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 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.
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)
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.
The states_elec_t class contains all electronic wave functions.
batches of electronic states