Octopus
current.F90
Go to the documentation of this file.
1!! Copyright (C) 2008-2019 X. Andrade, F. Bonafe, R. Jestaedt, H. Appel
2!!
3!! This program is free software; you can redistribute it and/or modify
4!! it under the terms of the GNU General Public License as published by
5!! the Free Software Foundation; either version 2, or (at your option)
6!! any later version.
7!!
8!! This program is distributed in the hope that it will be useful,
9!! but WITHOUT ANY WARRANTY; without even the implied warranty of
10!! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11!! GNU General Public License for more details.
12!!
13!! You should have received a copy of the GNU General Public License
14!! along with this program; if not, write to the Free Software
15!! Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16!! 02110-1301, USA.
17!!
18
19#include "global.h"
20
21module current_oct_m
22 use accel_oct_m
24 use batch_oct_m
27 use comm_oct_m
28 use debug_oct_m
32 use global_oct_m
33 use grid_oct_m
35 use, intrinsic :: iso_fortran_env
38 use lda_u_oct_m
39 use math_oct_m
42 use mesh_oct_m
47 use parser_oct_m
51 use ps_oct_m
53 use space_oct_m
56 use types_oct_m
57 use unit_oct_m
61 use xc_oct_m
62
63 implicit none
64
65 private
66
67 type current_t
68 private
69 integer :: method
70 logical :: include_diamag
71 end type current_t
72
73
74 public :: &
75 current_t, &
80
81 integer, parameter, public :: &
82 CURRENT_GRADIENT = 1, &
85
86contains
87
88 subroutine current_init(this, namespace)
89 type(current_t), intent(out) :: this
90 type(namespace_t), intent(in) :: namespace
91
92 push_sub(current_init)
94 !%Variable CurrentDensity
95 !%Default gradient_corrected
96 !%Type integer
97 !%Section Hamiltonian
98 !%Description
99 !% This variable selects the method used to
100 !% calculate the current density. For the moment this variable is
101 !% for development purposes and users should not need to use
102 !% it.
103 !%Option gradient 1
104 !% The calculation of current is done using the gradient operator. (Experimental)
105 !%Option gradient_corrected 2
106 !% The calculation of current is done using the gradient operator
107 !% with additional corrections for the total current from non-local operators.
108 !%Option hamiltonian 3
109 !% The current density is obtained from the commutator of the
110 !% Hamiltonian with the position operator. (Experimental)
111 !%End
112
113 call parse_variable(namespace, 'CurrentDensity', current_gradient_corr, this%method)
114 if (.not. varinfo_valid_option('CurrentDensity', this%method)) call messages_input_error(namespace, 'CurrentDensity')
115 if (this%method /= current_gradient_corr) then
116 call messages_experimental("CurrentDensity /= gradient_corrected")
117 end if
118
119 !%Variable CalculateDiamagneticCurrent
120 !%Default no
121 !%Type logical
122 !%Section Hamiltonian
123 !%Description
124 !% This variable decides whether the current density arising from the non-uniform
125 !% vector potential, defined as:
126 !% <math> \vec{J}_{dmc}(\vec{r}, t)=-\frac{e^2}{m_e c_0} n(\vec{r}, t) \vec{A}(\vec{r},t)$ </math>
127 !% is included in the total current density.
128 !%End
129 call parse_variable(namespace, 'CalculateDiamagneticCurrent', .false., this%include_diamag)
130
131 pop_sub(current_init)
132 end subroutine current_init
133
134 ! ---------------------------------------------------------
135
136 subroutine current_batch_accumulate(st, der, ik, ib, psib, gpsib)
137 type(states_elec_t), intent(inout) :: st
138 type(derivatives_t), intent(inout) :: der
139 integer, intent(in) :: ik
140 integer, intent(in) :: ib
141 type(wfs_elec_t), intent(in) :: psib
142 class(wfs_elec_t), contiguous, intent(in) :: gpsib(:)
143
144 integer :: ist, idir, ii, ip, idim, bsize, gsize
145 complex(real64), allocatable :: psi(:, :), gpsi(:, :)
146 real(real64), allocatable :: current_tmp(:, :)
147 complex(real64) :: c_tmp
148 real(real64) :: ww
149 real(real64), allocatable :: weight(:)
150 type(accel_mem_t) :: buff_weight, buff_current
151 type(accel_kernel_t), save :: kernel
152
153 safe_allocate(psi(1:der%mesh%np_part, 1:st%d%dim))
154 safe_allocate(gpsi(1:der%mesh%np_part, 1:st%d%dim))
155
156 if (st%d%ispin == spinors .or. (psib%status() == batch_device_packed .and. der%dim /= 3)) then
157
158 do idir = 1, der%dim
159 do ist = states_elec_block_min(st, ib), states_elec_block_max(st, ib)
161 ww = st%kweights(ik)*st%occ(ist, ik)
162 if (abs(ww) <= m_epsilon) cycle
163
164 do idim = 1, st%d%dim
165 ii = st%group%psib(ib, ik)%inv_index((/ist, idim/))
166 call batch_get_state(psib, ii, der%mesh%np, psi(:, idim))
167 call batch_get_state(gpsib(idir), ii, der%mesh%np, gpsi(:, idim))
168 end do
169
170 if (st%d%ispin /= spinors) then
171 !$omp parallel do
172 do ip = 1, der%mesh%np
173 st%current_kpt(ip, idir, ik) = st%current_kpt(ip, idir, ik) + ww*aimag(conjg(psi(ip, 1))*gpsi(ip, 1))
174 end do
175 !$omp end parallel do
176 else
177 !$omp parallel do private(c_tmp)
178 do ip = 1, der%mesh%np
179 st%current_para(ip, idir, 1) = st%current_para(ip, idir, 1) + ww*aimag(conjg(psi(ip, 1))*gpsi(ip, 1))
180 st%current_para(ip, idir, 2) = st%current_para(ip, idir, 2) + ww*aimag(conjg(psi(ip, 2))*gpsi(ip, 2))
181 c_tmp = -m_half*(conjg(psi(ip, 2))*gpsi(ip, 1) - psi(ip, 1)*conjg(gpsi(ip, 2)))
182 st%current_para(ip, idir, 3) = st%current_para(ip, idir, 3) - ww*aimag(c_tmp)
183 st%current_para(ip, idir, 4) = st%current_para(ip, idir, 4) + ww*real(c_tmp, real64)
184 end do
185 !$omp end parallel do
186 end if
187
188 end do
189 end do
190
191 else if (psib%status() == batch_device_packed) then
192
193 assert(der%dim == 3)
194
195 safe_allocate(weight(1:psib%nst))
196 do ist = 1, psib%nst
197 weight(ist) = st%kweights(ik)*st%occ(psib%ist(ist), ik)
198 end do
199
200
201 call accel_create_buffer(buff_weight, accel_mem_read_only, type_float, psib%nst)
202 call accel_write_buffer(buff_weight, psib%nst, weight, async=.true.)
203
204 call accel_create_buffer(buff_current, accel_mem_write_only, type_float, der%mesh%np*3)
205
206 call accel_kernel_start_call(kernel, 'density.cu', 'current_accumulate<double>')
207
208 call accel_set_kernel_arg(kernel, 0, psib%nst)
209 call accel_set_kernel_arg(kernel, 1, der%mesh%np)
210 call accel_set_kernel_arg(kernel, 2, buff_weight)
211 call accel_set_kernel_arg(kernel, 3, psib%ff_device)
212 call accel_set_kernel_arg(kernel, 4, log2(int(psib%pack_size(1), int32)))
213 call accel_set_kernel_arg(kernel, 5, gpsib(1)%ff_device)
214 call accel_set_kernel_arg(kernel, 6, gpsib(2)%ff_device)
215 call accel_set_kernel_arg(kernel, 7, gpsib(3)%ff_device)
216 call accel_set_kernel_arg(kernel, 8, log2(int(gpsib(1)%pack_size(1), int32)))
217 call accel_set_kernel_arg(kernel, 9, buff_current)
218
219 ! Compute the grid size
220 bsize = accel_kernel_block_size(kernel)
221 call accel_grid_size(der%mesh%np, bsize, gsize)
222
223 call accel_kernel_run(kernel, (/gsize/), (/bsize/))
224
225 safe_allocate(current_tmp(1:der%mesh%np, 1:der%dim))
226
227 call accel_read_buffer(buff_current, der%mesh%np, der%dim, current_tmp)
228
229 call lalg_axpy(der%mesh%np, der%dim, m_one, current_tmp, st%current_kpt(:,:,ik))
230
231 safe_deallocate_a(current_tmp)
232
233 call accel_free_buffer(buff_weight)
234 call accel_free_buffer(buff_current)
235
236 safe_deallocate_a(weight)
237
238 else
239
240 assert(psib%is_packed() .eqv. gpsib(1)%is_packed())
241
242 !$omp parallel private(ip, ist, ww, idir)
243 do ii = 1, psib%nst
244 ist = states_elec_block_min(st, ib) + ii - 1
245 ww = st%kweights(ik)*st%occ(ist, ik)
246 if (abs(ww) <= m_epsilon) cycle
247
248 if (psib%is_packed()) then
249 do idir = 1, der%dim
250 !$omp do
251 do ip = 1, der%mesh%np
252 st%current_kpt(ip, idir, ik) = st%current_kpt(ip, idir, ik) &
253 + ww*aimag(conjg(psib%zff_pack(ii, ip))*gpsib(idir)%zff_pack(ii, ip))
254 end do
255 !$omp end do nowait
256 end do
257 else
258 do idir = 1, der%dim
259 !$omp do
260 do ip = 1, der%mesh%np
261 st%current_kpt(ip, idir, ik) = st%current_kpt(ip, idir, ik) &
262 + ww*aimag(conjg(psib%zff(ip, 1, ii))*gpsib(idir)%zff(ip, 1, ii))
263 end do
264 !$omp end do nowait
265 end do
266 end if
267 end do
268 !$omp end parallel
269
270 end if
271
272 safe_deallocate_a(psi)
273 safe_deallocate_a(gpsi)
274
275 end subroutine current_batch_accumulate
276
277
278 ! ---------------------------------------------------------
280 subroutine current_calculate(this, namespace, gr, hm, space, st)
281 type(current_t), intent(in) :: this
282 type(namespace_t), intent(in) :: namespace
283 type(grid_t), intent(inout) :: gr
284 type(hamiltonian_elec_t), intent(inout) :: hm
285 class(space_t), intent(in) :: space
286 type(states_elec_t), intent(inout) :: st
287
288
289 call profiling_in("CURRENT_TOTAL")
290 push_sub(current_calculate)
291
292 ! The ultrasoft/PAW augmentation current is wired through the gradient-corrected packed path
293 ! (current_calculate_para). The ultrasoft compensation-charge current (current_aug_accumulate)
294 ! is collinear only; PAW does not use it (its one-center current, paw_ae_add_onsite_current,
295 ! also handles spinors).
296 if (st%requires_generalized_overlap) then
297 if (this%method /= current_gradient_corr) then
298 call messages_not_implemented("Ultrasoft/PAW current with CurrentDensity /= gradient_corrected", &
299 namespace=namespace)
300 end if
301 if (st%d%ispin == spinors .and. .not. hm%ep%has_paw) then
302 call messages_not_implemented("Ultrasoft current with spinors", namespace=namespace)
303 end if
304 end if
305
306 call current_calculate_para(this, namespace, gr, hm, space, st)
307 st%current = st%current_para
308
309 if (this%include_diamag) then
310 call current_calculate_dia_non_unif_vec_pot(gr%der, hm, st)
311 st%current = st%current + st%current_dia
312 end if
313
314 call profiling_out("CURRENT_TOTAL")
315 pop_sub(current_calculate)
316
317 end subroutine current_calculate
318
319 ! ---------------------------------------------------------
322 subroutine current_calculate_dia_non_unif_vec_pot(der, hm, st)
323 type(derivatives_t), intent(inout) :: der
324 type(hamiltonian_elec_t), intent(in) :: hm
325 type(states_elec_t), intent(inout) :: st
326
327 integer :: ispin, idir, ip
328
329 call profiling_in("CURRENT_DIA_NON_UNIF_A")
331
332 st%current_dia = m_zero
333
334 if(allocated(hm%hm_base%vector_potential)) then
335 do ispin = 1, st%d%nspin
336 do idir = 1, der%dim
337 !$omp parallel do
338 do ip = 1, der%mesh%np
339 ! the vector potential is assumed to be devided by c_0 already
340 st%current_dia(ip, idir, ispin) = st%current_dia(ip, idir, ispin) + &
341 st%rho(ip, ispin)*hm%hm_base%vector_potential(idir, ip)
342 end do
343 !$omp end parallel do
344 end do
345 end do
346 end if
347
348 call profiling_out("CURRENT_DIA_NON_UNIF_A")
350
353 ! ---------------------------------------------------------
357 subroutine current_calculate_mag(der, st)
358 type(derivatives_t), intent(inout) :: der
359 type(states_elec_t), intent(inout) :: st
360
361 real(real64), allocatable :: magnetization_density(:, :), curl_mag(:, :)
362
363 call profiling_in("CURRENT_MAG")
364 push_sub(current_calculate_mag)
365
366 st%current_mag = m_zero
367
368 if (st%d%ispin /= unpolarized) then
369 safe_allocate(magnetization_density(1:der%mesh%np_part, 1:der%dim))
370 safe_allocate(curl_mag(1:der%mesh%np_part, 1:der%dim))
371
372 call magnetic_density(der%mesh, st%d, st%rho, magnetization_density)
373 call dderivatives_curl(der, magnetization_density, curl_mag)
374
375 call lalg_axpy(der%mesh%np, der%dim, m_half, curl_mag, st%current_mag(:, :, 1))
376
377 safe_deallocate_a(magnetization_density)
378 safe_deallocate_a(curl_mag)
379 end if
380
381 call profiling_out("CURRENT_MAG")
382 pop_sub(current_calculate_mag)
383
384 end subroutine current_calculate_mag
385
386 ! ---------------------------------------------------------
389 subroutine current_calculate_para(this, namespace, gr, hm, space, st)
390 type(current_t), intent(in) :: this
391 type(namespace_t), intent(in) :: namespace
392 type(grid_t), intent(inout) :: gr
393 type(hamiltonian_elec_t), intent(inout) :: hm
394 class(space_t), intent(in) :: space
395 type(states_elec_t), target, intent(inout) :: st
396
397 integer :: ik, ist, idir, idim, ip, ib, ii, ispin
398 complex(real64), allocatable :: gpsi(:, :, :), psi(:, :), hpsi(:, :), rhpsi(:, :), rpsi(:, :), hrpsi(:, :)
399 type(wfs_elec_t) :: hpsib, rhpsib, rpsib, hrpsib, epsib
400 class(wfs_elec_t), allocatable :: commpsib(:)
401 real(real64) :: ww
402 complex(real64) :: c_tmp
403 real(real64), allocatable :: caug(:,:,:,:)
404 logical :: local_on_device, use_packed
405
406 call profiling_in("CURRENT_PARA")
407 push_sub(current_calculate_para)
408
409 ! spin not implemented or tested
410 assert(all(ubound(st%current_para) == (/gr%np_part, gr%der%dim, st%d%nspin/)))
411 assert(all(ubound(st%current_kpt) == (/gr%np, gr%der%dim, st%d%kpt%end/)))
412 assert(all(lbound(st%current_kpt) == (/1, 1, st%d%kpt%start/)))
413
414 safe_allocate(psi(1:gr%np_part, 1:st%d%dim))
415 safe_allocate(gpsi(1:gr%np, 1:gr%der%dim, 1:st%d%dim))
416 safe_allocate(hpsi(1:gr%np_part, 1:st%d%dim))
417 safe_allocate(rhpsi(1:gr%np_part, 1:st%d%dim))
418 safe_allocate(rpsi(1:gr%np_part, 1:st%d%dim))
419 safe_allocate(hrpsi(1:gr%np_part, 1:st%d%dim))
420 safe_allocate_type_array(wfs_elec_t, commpsib, (1:gr%der%dim))
421
422 !$omp parallel private(idir, ip, ispin)
423 do ik = st%d%kpt%start, st%d%kpt%end
424 do idir = 1, gr%der%dim
425 !$omp do
426 do ip = 1, gr%np
427 st%current_kpt(ip, idir, ik) = m_zero
428 end do
429 !$omp end do nowait
430 end do
431 end do
432 do ispin = 1, st%d%nspin
433 do idir = 1, gr%der%dim
434 !$omp do
435 do ip = 1, gr%np
436 st%current_para(ip, idir, ispin) = m_zero
437 end do
438 !$omp end do nowait
439 end do
440 end do
441 !$omp end parallel
442
443 select case (this%method)
444
446 if (family_is_hybrid(hm%xc)) then
447 if (.not. hm%exxop%useACE) then
448 hm%exxop%st => st
450 end if
451 end if
452
453 do ik = st%d%kpt%start, st%d%kpt%end
454 ispin = st%d%get_spin_index(ik)
455 do ib = st%group%block_start, st%group%block_end
456
457 call st%group%psib(ib, ik)%do_pack(copy = .true.)
458
459 call st%group%psib(ib, ik)%copy_to(hpsib)
460 call st%group%psib(ib, ik)%copy_to(rhpsib)
461 call st%group%psib(ib, ik)%copy_to(rpsib)
462 call st%group%psib(ib, ik)%copy_to(hrpsib)
463
464 call boundaries_set(gr%der%boundaries, gr, st%group%psib(ib, ik))
465 call zhamiltonian_elec_apply_batch(hm, namespace, gr, st%group%psib(ib, ik), hpsib, set_bc = .false.)
466
467 do idir = 1, gr%der%dim
468
469 call batch_mul_mf(gr%np, gr%x_t(:, idir), hpsib, rhpsib)
470 call batch_mul_mf(gr%np_part, gr%x_t(:, idir), st%group%psib(ib, ik), rpsib)
471
472 call zhamiltonian_elec_apply_batch(hm, namespace, gr, rpsib, hrpsib, set_bc = .false.)
473
474 do ist = states_elec_block_min(st, ib), states_elec_block_max(st, ib)
475 ww = st%kweights(ik)*st%occ(ist, ik)
476 if (ww <= m_epsilon) cycle
477
478 do idim = 1, st%d%dim
479 ii = st%group%psib(ib, ik)%inv_index((/ist, idim/))
480 call batch_get_state(st%group%psib(ib, ik), ii, gr%np, psi(:, idim))
481 call batch_get_state(hrpsib, ii, gr%np, hrpsi(:, idim))
482 call batch_get_state(rhpsib, ii, gr%np, rhpsi(:, idim))
483 end do
484
485 if (st%d%ispin /= spinors) then
486 !$omp parallel do
487 do ip = 1, gr%np
488 st%current_kpt(ip, idir, ik) = st%current_kpt(ip, idir, ik) &
489 - ww*aimag(conjg(psi(ip, 1))*hrpsi(ip, 1) - conjg(psi(ip, 1))*rhpsi(ip, 1))
490 end do
491 !$omp end parallel do
492 else
493 !$omp parallel do private(c_tmp)
494 do ip = 1, gr%np
495 st%current_para(ip, idir, 1) = st%current_para(ip, idir, 1) + &
496 ww*aimag(conjg(psi(ip, 1))*hrpsi(ip, 1) - conjg(psi(ip, 1))*rhpsi(ip, 1))
497 st%current_para(ip, idir, 2) = st%current_para(ip, idir, 2) + &
498 ww*aimag(conjg(psi(ip, 2))*hrpsi(ip, 2) - conjg(psi(ip, 2))*rhpsi(ip, 2))
499 c_tmp = -m_half*m_zi*(conjg(psi(ip, 2))*hrpsi(ip, 1) - conjg(psi(ip, 2))*rhpsi(ip, 1) &
500 -psi(ip, 1)*conjg(hrpsi(ip, 2)) - psi(ip, 1)*conjg(rhpsi(ip, 2)))
501 st%current_para(ip, idir, 3) = st%current_para(ip, idir, 3) + ww*real(c_tmp, real64)
502 st%current_para(ip, idir, 4) = st%current_para(ip, idir, 4) + ww*aimag(c_tmp)
503 end do
504 !$omp end parallel do
505 end if
506
507 end do
508
509 end do
510
511 call st%group%psib(ib, ik)%do_unpack(copy = .false.)
512
513 call hpsib%end()
514 call rhpsib%end()
515 call rpsib%end()
516 call hrpsib%end()
517
518 end do
519 end do
520
521 if (family_is_hybrid(hm%xc)) then
522 if (.not. hm%exxop%useACE) then
524 nullify(hm%exxop%st)
525 end if
526 end if
527
528 case (current_gradient, current_gradient_corr)
529
530 ! A purely local pseudopotential has no projector matrices, but its position commutator returns
531 ! early without them, so the packed path is safe. It is faster only for device-packed states; on
532 ! the CPU the non-packed path is faster, so this case is restricted to those. A scissor run
533 ! stays on the non-packed path, since the packed branch does not carry the scissor correction.
534 local_on_device = st%are_packed() .and. accel_is_enabled() .and. .not. hm%vnl%has_non_local_potential &
535 .and. .not. hm%scissor%apply
536 use_packed = this%method == current_gradient_corr .and. .not. family_is_mgga_with_exc(hm%xc) &
537 .and. hm%theory_level /= hartree_fock &
538 .and. hm%theory_level /= generalized_kohn_sham_dft &
539 .and. hm%theory_level /= rdmft &
540 .and. (hm%vnl%apply_projector_matrices .or. local_on_device)
541
542 if (use_packed) then
543
544 ! we can use the packed version
545
546 ! Ultrasoft: the compensation-charge augmentation current is accumulated alongside the smooth
547 ! current.
548 if (hm%ep%requires_generalized_overlap .and. .not. hm%ep%has_paw) then
549 safe_allocate(caug(1:hm%vnl%max_nprojs, 1:hm%vnl%max_nprojs, 1:gr%der%dim, 1:hm%vnl%nprojector_matrices))
550 end if
551
552 do ik = st%d%kpt%start, st%d%kpt%end
553 ispin = st%d%get_spin_index(ik)
554 if (hm%ep%requires_generalized_overlap .and. .not. hm%ep%has_paw) caug = m_zero
555 do ib = st%group%block_start, st%group%block_end
556
557 ! copy st%group%psib(ib, ik) to epsib and set the phase
558 call hm%phase%copy_and_set_phase(gr, st%d%kpt, st%group%psib(ib, ik), epsib)
559
560 ! this now takes non-orthogonal axis into account
561 call zderivatives_batch_grad(gr%der, epsib, commpsib, set_bc=.false.)
562
563 ! The nonlocal [r,V_nl] commutator is the pseudopotential velocity correction. For PAW the
564 ! true all-electron current inside the sphere is the paramagnetic partial-wave current
565 ! (added as j^1 - j~^1 below), so the smooth current stays purely paramagnetic and the
566 ! commutator / compensation-current model is not applied.
567 if (.not. hm%ep%has_paw) then
568 call hm%vnl%zposition_commutator(gr, st%d, gr%der%boundaries%spiral, epsib, commpsib, async=.true.)
569 end if
570
571 call zlda_u_commute_r(hm%lda_u, gr, space, st%d, namespace, epsib, commpsib)
572
573 call current_batch_accumulate(st, gr%der, ik, ib, epsib, commpsib)
574
575 ! Ultrasoft augmentation current: project the phased orbitals (epsib) and
576 ! commpsib = (grad + [r,V_nl]) psi on the beta functions and accumulate the per-pair
577 ! coefficients. Skipped for PAW (replaced by the one-center all-electron current).
578 if (hm%ep%requires_generalized_overlap .and. .not. hm%ep%has_paw) then
579 call current_aug_accumulate(hm, gr, st, ik, epsib, commpsib, caug)
580 end if
581
582 do idir = 1, gr%der%dim
583 call commpsib(idir)%end()
584 end do
585
586 call epsib%end()
587
588 call accel_finish()
589 end do
590 ! Ultrasoft: scatter the compensation-charge (Q_ij) augmentation current. For PAW this
591 ! model current is replaced by the exact one-center all-electron current j^1 - j~^1 added
592 ! after the k-sum below, so skip the compensation scatter here.
593 if (hm%ep%requires_generalized_overlap .and. .not. hm%ep%has_paw) then
594 call current_aug_scatter(hm, gr, st, ik, caug)
595 end if
596 end do
597
598 if (hm%ep%requires_generalized_overlap .and. .not. hm%ep%has_paw) then
599 safe_deallocate_a(caug)
600 end if
601
602 else
603
604 ! use the slow non-packed version
605
606 ! The augmentation current is only implemented in the packed version above
607 if (hm%ep%requires_generalized_overlap) then
608 call messages_not_implemented("Ultrasoft/PAW current with MGGA, hybrid functionals, RDMFT," &
609 // " or without projector matrices", namespace=namespace)
610 end if
611
612 do ik = st%d%kpt%start, st%d%kpt%end
613 ispin = st%d%get_spin_index(ik)
614 do ist = st%st_start, st%st_end
615
616 ww = st%kweights(ik)*st%occ(ist, ik)
617 if (abs(ww) <= m_epsilon) cycle
618
619 call states_elec_get_state(st, gr, ist, ik, psi)
620
621 if (hm%phase%is_allocated()) then
622 call hm%phase%apply_to_single(psi, gr%np, st%d%dim, ik, .false.)
623 ! apply phase correction while setting boundary -> memory needs to be
624 ! accessed only once
625 do idim = 1, st%d%dim
626 call boundaries_set(gr%der%boundaries, gr, psi(:, idim), phase_correction = hm%phase%phase_corr(:, ik), &
627 buff_phase_corr = hm%phase%buff_phase_corr, offset=int((ik-st%d%kpt%start)*(gr%np_part-gr%np)))
628 end do
629 else
630 do idim = 1, st%d%dim
631 call boundaries_set(gr%der%boundaries, gr, psi(:, idim))
632 end do
633 end if
634
635 do idim = 1, st%d%dim
636 call zderivatives_grad(gr%der, psi(:, idim), gpsi(:, :, idim), set_bc = .false.)
637 end do
638
639 if (this%method == current_gradient_corr) then
640 !A nonlocal contribution from the MGGA potential must be included
641 !This must be done first, as this is like a position-dependent mass
642 call hm%ks_pot%zcurrent_mass_renormalization(gpsi, gr%der%dim, st%d%dim, ispin)
643
644 !A nonlocal contribution from the pseudopotential must be included
645 ! For PAW the smooth current stays purely paramagnetic
646 if (.not. hm%ep%has_paw) then
647 call zprojector_commute_r_allatoms_alldir(hm%ep%proj, hm%ions, gr, st%d%dim, &
648 gr%der%boundaries, ik, psi, gpsi)
649 end if
650 !A nonlocal contribution from the scissor must be included
651 if (hm%scissor%apply) then
652 call scissor_commute_r(hm%scissor, gr, ik, psi, gpsi)
653 end if
654
655 call zlda_u_commute_r_single(hm%lda_u, gr, space, st%d, namespace, ist, ik, &
656 psi, gpsi, hm%phase%is_allocated())
657
658 call zexchange_operator_commute_r(hm%exxop, namespace, gr, st%d, ik, psi, gpsi)
659
660 end if
661
662 if (st%d%ispin /= spinors) then
663 do idir = 1, gr%der%dim
664 !$omp parallel do
665 do ip = 1, gr%np
666 st%current_kpt(ip, idir, ik) = st%current_kpt(ip, idir, ik) + &
667 ww*aimag(conjg(psi(ip, 1))*gpsi(ip, idir, 1))
668 end do
669 !$omp end parallel do
670 end do
671 else
672 do idir = 1, gr%der%dim
673 !$omp parallel do private(c_tmp)
674 do ip = 1, gr%np
675 st%current_para(ip, idir, 1) = st%current_para(ip, idir, 1) + &
676 ww*aimag(conjg(psi(ip, 1))*gpsi(ip, idir, 1))
677 st%current_para(ip, idir, 2) = st%current_para(ip, idir, 2) + &
678 ww*aimag(conjg(psi(ip, 2))*gpsi(ip, idir, 2))
679 c_tmp = -m_half*m_zi*(conjg(psi(ip, 2))*gpsi(ip, idir, 1) - psi(ip, 1)*conjg(gpsi(ip, idir, 2)))
680 st%current_para(ip, idir, 3) = st%current_para(ip, idir, 3) + ww*real(c_tmp, real64)
681 st%current_para(ip, idir, 4) = st%current_para(ip, idir, 4) + ww*aimag(c_tmp)
682 end do
683 !$omp end parallel do
684 end do
685 end if
686
687 end do
688 end do
689
690 end if
691
692 case default
693
694 assert(.false.)
695
696 end select
697
698 if (st%d%ispin /= spinors) then
699 !We sum the current over k-points
700 do ik = st%d%kpt%start, st%d%kpt%end
701 ispin = st%d%get_spin_index(ik)
702 call lalg_axpy(gr%np, gr%der%dim, m_one, st%current_kpt(:, :, ik), st%current_para(:, :, ispin))
703 end do
704 end if
705
706 if (st%parallel_in_states .or. st%d%kpt%parallel) then
707 call comm_allreduce(st%st_kpt_mpi_grp, st%current_para, dim = (/gr%np, gr%der%dim, st%d%nspin/))
708 end if
709
710 ! PAW: add the exact one-center all-electron current j^1 - j~^1 to the smooth current, using the
711 ! tabulated on-site current basis weighted by the antisymmetric one-center current matrix
712 ! Im[rho_ij] accumulated during the density calculation
713 ! This makes st%current (and hence the integrated total_current) the true all-electron current.
714 if (hm%ep%has_paw) then
715 call paw_ae_add_onsite_current(hm%aug_overlap, hm%ep, st%current_para)
716 end if
717
718 if (st%symmetrize_density) then
719 do ispin = 1, st%d%nspin
720 call dgrid_symmetrize_vector_field(gr, st%current_para(:, :, ispin), suppress_warning = .true.)
721 end do
722 end if
723
724 safe_deallocate_a(gpsi)
725 safe_deallocate_a(psi)
726 safe_deallocate_a(hpsi)
727 safe_deallocate_a(rhpsi)
728 safe_deallocate_a(rpsi)
729 safe_deallocate_a(hrpsi)
730 safe_deallocate_a(commpsib)
731
732 call profiling_out("CURRENT_PARA")
734
735 end subroutine current_calculate_para
736
737 ! ---------------------------------------------------------
755 subroutine current_aug_accumulate(hm, gr, st, ik, psib, gpsib, caug)
756 type(hamiltonian_elec_t), intent(in) :: hm
757 type(grid_t), intent(in) :: gr
758 type(states_elec_t), intent(in) :: st
759 integer, intent(in) :: ik
760 type(wfs_elec_t), intent(in) :: psib
761 class(wfs_elec_t), intent(inout) :: gpsib(:)
762 real(real64), intent(inout) :: caug(:,:,:,:)
763
764 type(projection_t) :: proj_psi, proj_g
765 integer :: idir, ist_global, imat, iproj, jproj, iprojection, ist
766 real(real64) :: ww
767
768 push_sub_with_profile(current_aug_accumulate)
769
770 ! <beta_j|psi> for the whole block at once.
771 call znonlocal_pseudopotential_start(hm%vnl, gr, st%d, .false., psib, proj_psi)
772 if (gr%parallel_in_domains) call gr%allreduce(proj_psi%zprojection)
773
774 do idir = 1, gr%der%dim
775 ! gpsib carries the same phase convention as psib (it is the gradient/commutator of the phased
776 ! orbitals), so flag it accordingly before the projection uses the matching (phased) projectors.
777 gpsib(idir)%has_phase = psib%has_phase
778 call znonlocal_pseudopotential_start(hm%vnl, gr, st%d, .false., gpsib(idir), proj_g)
779 if (gr%parallel_in_domains) call gr%allreduce(proj_g%zprojection)
780
781 ! caug_ij^alpha += ww Re[conj(<beta_i|v psi>) <beta_j|psi>], with <beta_i|v psi> = -i<beta_i|gpsi>,
782 ! so Re[conj(-i g_i) a_j] = -Im[conj(g_i) a_j].
783 do ist = 1, psib%nst_linear
784 ist_global = psib%linear_to_ist(ist)
785 ww = st%kweights(ik)*st%occ(ist_global, ik)
786 if (abs(ww) <= m_epsilon) cycle
787 iprojection = 0
788 do imat = 1, hm%vnl%nprojector_matrices
789 do iproj = 1, hm%vnl%projector_matrices(imat)%nprojs
790 do jproj = 1, hm%vnl%projector_matrices(imat)%nprojs
791 caug(iproj, jproj, idir, imat) = caug(iproj, jproj, idir, imat) &
792 - ww*aimag(conjg(proj_g%zprojection(ist, iprojection + iproj)) &
793 * proj_psi%zprojection(ist, iprojection + jproj))
794 end do
795 end do
796 iprojection = iprojection + hm%vnl%projector_matrices(imat)%nprojs
797 end do
798 end do
799
800 safe_deallocate_a(proj_g%zprojection)
801 end do
802 safe_deallocate_a(proj_psi%zprojection)
803
804 pop_sub_with_profile(current_aug_accumulate)
805 end subroutine current_aug_accumulate
806
807 ! ---------------------------------------------------------
815 subroutine current_aug_scatter(hm, gr, st, ik, caug)
816 type(hamiltonian_elec_t), target, intent(inout) :: hm
817 type(grid_t), intent(in) :: gr
818 type(states_elec_t), intent(inout) :: st
819 integer, intent(in) :: ik
820 real(real64), intent(in) :: caug(:,:,:,:)
821
822 type(projector_matrix_t), pointer :: pmat
823 type(projector_t), pointer :: proj
824 real(real64), allocatable :: acc(:), wcol(:, :)
825 integer :: imat, iatom, idir, is, ip, np
826
827 push_sub_with_profile(current_aug_scatter)
828
829 safe_allocate(acc(1:hm%vnl%total_points))
830
831 do imat = 1, hm%vnl%nprojector_matrices
832 pmat => hm%vnl%projector_matrices(imat)
833 iatom = hm%vnl%projector_to_atom(imat)
834 proj => hm%ep%proj(iatom)
835 if (.not. projector_has_augmentation(proj)) cycle
836 assert(allocated(proj%aug%qflm))
837
838 np = pmat%npoints
839
840 ! The Gaunt sums reduce to scalar weights per combined Q Y_LM column, and
841 ! the grid work to a single matrix-vector product acc = aug_qflm . wcol
842 ! per direction.
843 safe_allocate(wcol(1:proj%aug%qflm_ncombo, 1:1))
844 do idir = 1, gr%der%dim
845 wcol = m_zero
846 call projector_aug_pair_to_columns(proj, pmat, 1, caug(:, :, idir:idir, imat), wcol)
847
848 if (np > 0) then
849 call lalg_gemv(np, proj%aug%qflm_ncombo, m_one, proj%aug%qflm, wcol(:, 1), m_zero, acc(1:np))
850 ! The scatter writes distinct mesh points
851 !$omp parallel do private(ip)
852 do is = 1, np
853 ip = pmat%map(is)
854 st%current_kpt(ip, idir, ik) = st%current_kpt(ip, idir, ik) + acc(is)
855 end do
856 end if
857 end do
858 safe_deallocate_a(wcol)
859 end do
860
861 safe_deallocate_a(acc)
862
863 pop_sub_with_profile(current_aug_scatter)
864 end subroutine current_aug_scatter
865
866 ! ---------------------------------------------------------
867 subroutine current_heat_calculate(space, der, hm, st, current)
868 class(space_t), intent(in) :: space
869 type(derivatives_t), intent(in) :: der
870 type(hamiltonian_elec_t), intent(in) :: hm
871 type(states_elec_t), intent(in) :: st
872 real(real64), contiguous, intent(out) :: current(:, :, :)
873
874 integer :: ik, ist, idir, idim, ip, ispin, ndim
875 complex(real64), allocatable :: gpsi(:, :, :), psi(:, :), g2psi(:, :, :, :)
876 complex(real64) :: tmp
877
878 push_sub(current_heat_calculate)
879
880 assert(space%is_periodic())
881 assert(st%d%dim == 1)
882
883 ndim = space%dim
884
885 safe_allocate(psi(1:der%mesh%np_part, 1:st%d%dim))
886 safe_allocate(gpsi(1:der%mesh%np_part, 1:ndim, 1:st%d%dim))
887 safe_allocate(g2psi(1:der%mesh%np, 1:ndim, 1:ndim, 1:st%d%dim))
888
889 current(1:der%mesh%np, 1:ndim, 1:st%d%nspin) = 0.0_real64
890
891
892 do ik = st%d%kpt%start, st%d%kpt%end
893 ispin = st%d%get_spin_index(ik)
894 do ist = st%st_start, st%st_end
895
896 if (abs(st%kweights(ik)*st%occ(ist, ik)) <= m_epsilon) cycle
897
898 call states_elec_get_state(st, der%mesh, ist, ik, psi)
899 do idim = 1, st%d%dim
900 call boundaries_set(der%boundaries, der%mesh, psi(:, idim))
901 end do
902
903 if (hm%phase%is_allocated()) then
904 call hm%phase%apply_to_single(psi, der%mesh%np_part, st%d%dim, ik, conjugate = .false.)
905 end if
906
907 do idim = 1, st%d%dim
908 call zderivatives_grad(der, psi(:, idim), gpsi(:, :, idim), set_bc = .false.)
909 end do
910 do idir = 1, ndim
911 if (hm%phase%is_allocated()) then
912 call hm%phase%apply_to_single(gpsi(:, idir, :), der%mesh%np, st%d%dim, ik, conjugate = .true.)
913 end if
914
915 do idim = 1, st%d%dim
916 call boundaries_set(der%boundaries, der%mesh, gpsi(:,idir, idim))
917 end do
918
919 if (hm%phase%is_allocated()) then
920 call hm%phase%apply_to_single(gpsi(:, idir, :), der%mesh%np_part, st%d%dim, ik, conjugate = .false.)
921 end if
922
923 do idim = 1, st%d%dim
924 call zderivatives_grad(der, gpsi(:, idir, idim), g2psi(:, :, idir, idim), set_bc = .false.)
925 end do
926 end do
927 idim = 1
928 do ip = 1, der%mesh%np
929 do idir = 1, ndim
930 tmp = sum(conjg(g2psi(ip, 1:ndim, idir, idim))*gpsi(ip, 1:ndim, idim)) - &
931 sum(conjg(gpsi(ip, 1:ndim, idim))*g2psi(ip, 1:ndim, idir, idim))
932 tmp = tmp - conjg(gpsi(ip, idir, idim))*sum(g2psi(ip, 1:ndim, 1:ndim, idim)) + &
933 sum(conjg(g2psi(ip, 1:ndim, 1:ndim, idim)))*gpsi(ip, idir, idim)
934 current(ip, idir, ispin) = current(ip, idir, ispin) + st%kweights(ik)*st%occ(ist, ik)*aimag(tmp)/8.0_real64
935 end do
936 end do
937 end do
938 end do
940 if (st%parallel_in_states .or. st%d%kpt%parallel) then
941 call comm_allreduce(st%st_kpt_mpi_grp, current, dim = (/der%mesh%np, ndim, st%d%nspin/))
942 end if
943
944 do ip = 1, der%mesh%np
945 current(ip, 1:ndim, 1:st%d%nspin) = current(ip, 1:ndim, 1:st%d%nspin) + &
946 st%current(ip, 1:ndim, 1:st%d%nspin)*hm%ep%vpsl(ip)
947 end do
948
949
951
952 end subroutine current_heat_calculate
953
954end module current_oct_m
955
956!! Local Variables:
957!! mode: f90
958!! coding: utf-8
959!! End:
batchified multiplication by mesh function with optional conjugation:
Definition: batch_ops.F90:254
constant times a vector plus a vector
Definition: lalg_basic.F90:171
integer function, public accel_kernel_block_size(kernel)
Definition: accel.F90:1194
subroutine, public accel_free_buffer(this, async)
Definition: accel.F90:967
subroutine, public accel_kernel_start_call(this, file_name, kernel_name, flags)
Definition: accel.F90:1842
subroutine, public accel_finish()
Definition: accel.F90:1104
pure logical function, public accel_is_enabled()
Definition: accel.F90:391
integer, parameter, public accel_mem_write_only
Definition: accel.F90:162
integer, parameter, public accel_mem_read_only
Definition: accel.F90:162
This module implements batches of mesh functions.
Definition: batch.F90:112
integer, parameter, public batch_device_packed
functions are stored in device memory in packed order
Definition: batch.F90:293
This module implements common operations on batches of mesh functions.
Definition: batch_ops.F90:95
Module implementing boundary conditions in Octopus.
Definition: boundaries.F90:101
subroutine current_batch_accumulate(st, der, ik, ib, psib, gpsib)
Definition: current.F90:209
subroutine current_aug_accumulate(hm, gr, st, ik, psib, gpsib, caug)
Accumulate the ultrasoft/PAW augmentation-current coefficients for one block of states.
Definition: current.F90:828
subroutine current_calculate_dia_non_unif_vec_pot(der, hm, st)
Compute diamagnetic current density from non-uniform vector potential (the part coming from the unifo...
Definition: current.F90:395
subroutine, public current_calculate_mag(der, st)
Compute magnetization current Note: due to the the numerical curl, the magnetization current could de...
Definition: current.F90:430
integer, parameter, public current_hamiltonian
Definition: current.F90:153
integer, parameter, public current_gradient_corr
Definition: current.F90:153
subroutine, public current_heat_calculate(space, der, hm, st, current)
Definition: current.F90:940
subroutine current_calculate_para(this, namespace, gr, hm, space, st)
Compute paramagnetic current density (including full diamagnetic term if method = Hamiltonian us used...
Definition: current.F90:462
subroutine current_aug_scatter(hm, gr, st, ik, caug)
Scatter the ultrasoft/PAW augmentation current onto the k-resolved current density.
Definition: current.F90:888
subroutine, public current_calculate(this, namespace, gr, hm, space, st)
Compute total electronic current density.
Definition: current.F90:353
subroutine, public current_init(this, namespace)
Definition: current.F90:161
This module calculates the derivatives (gradients, Laplacians, etc.) of a function.
subroutine, public dderivatives_curl(der, ff, op_ff, ghost_update, set_bc)
apply the curl operator to a vector of mesh functions
subroutine, public zderivatives_batch_grad(der, ffb, opffb, ghost_update, set_bc, to_cartesian, factor)
apply the gradient to a batch of mesh functions
subroutine, public zderivatives_grad(der, ff, op_ff, ghost_update, set_bc, to_cartesian)
apply the gradient to a mesh function
integer, parameter, public unpolarized
Parameters...
integer, parameter, public spinors
subroutine, public zexchange_operator_commute_r(this, namespace, mesh, st_d, ik, psi, gpsi)
real(real64), parameter, public m_zero
Definition: global.F90:173
integer, parameter, public rdmft
Definition: global.F90:223
integer, parameter, public hartree_fock
Definition: global.F90:223
integer, parameter, public generalized_kohn_sham_dft
Definition: global.F90:223
complex(real64), parameter, public m_zi
Definition: global.F90:187
real(real64), parameter, public m_epsilon
Definition: global.F90:189
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
subroutine, public dgrid_symmetrize_vector_field(gr, field, suppress_warning)
Definition: grid.F90:685
subroutine, public zhamiltonian_elec_apply_batch(hm, namespace, mesh, psib, hpsib, terms, set_bc, set_phase, spsib)
A module to handle KS potential, without the external potential.
subroutine, public zlda_u_commute_r(this, mesh, space, d, namespace, psib, gpsib)
This routine computes [r,V_lda+u] .
Definition: lda_u.F90:5586
subroutine, public zlda_u_commute_r_single(this, mesh, space, d, namespace, ist, ik, psi, gpsi, has_phase)
Definition: lda_u.F90:5535
subroutine, public magnetic_density(mesh, std, rho, md)
Definition: magnetic.F90:142
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_not_implemented(feature, namespace)
Definition: messages.F90:1048
subroutine, public messages_input_error(namespace, var, details, row, column)
Definition: messages.F90:671
subroutine, public messages_experimental(name, namespace)
Definition: messages.F90:1020
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.
All-electron reconstruction of mesh fields from the PAW one-center terms.
subroutine, public paw_ae_add_onsite_current(this, epot, current)
Add the time-dependent one-center all-electron current to the current density.
/* 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_aug_pair_to_columns(proj, pmat, ncomp, pairw, wcol)
Apply the Gaunt coupling between projection pairs and the combined augmentation columns: wcol(col,...
Definition: projector.F90:740
subroutine, public zprojector_commute_r_allatoms_alldir(pj, ions, mesh, dim, bnd, ik, psi, cpsi)
This function calculates |cpsi> += [x, V_nl] |psi>
Definition: projector.F90:2189
logical elemental function, public projector_has_augmentation(p)
Whether the projector carries an augmentation contribution (charge augmentation Q_ij and a generalize...
Definition: projector.F90:213
Definition: ps.F90:93
integer pure function, public states_elec_block_max(st, ib)
return index of last state in block ib
integer pure function, public states_elec_block_min(st, ib)
return index of first state in block ib
This module provides routines for communicating states when using states parallelization.
subroutine, public states_elec_parallel_remote_access_stop(this)
stop remote memory access for states on other processors
subroutine, public states_elec_parallel_remote_access_start(this)
start remote memory access for states on other processors
type(type_t), parameter, public type_float
Definition: types.F90:113
brief This module defines the class unit_t which is used by the unit_systems_oct_m module.
Definition: unit.F90:111
This module defines the unit system, used for input and output.
Definition: xc.F90:97
logical pure function, public family_is_mgga_with_exc(xcs)
Is the xc function part of the mGGA family with an energy functional.
Definition: xc.F90:716
logical pure function, public family_is_hybrid(xcs)
Returns true if the functional is an hybrid functional.
Definition: xc.F90:731
class representing derivatives
Description of the grid, containing information on derivatives, stencil, and symmetries.
Definition: grid.F90:148
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 states_elec_t class contains all electronic wave functions.
batches of electronic states
Definition: wfs_elec.F90:120
int true(void)