Octopus
paw_onsite.F90
Go to the documentation of this file.
1!! Copyright (C) 2026 N. Tancogne-Dejean
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., 675 Mass Ave, Cambridge, MA 02139, USA.
16!!
17
18#include "global.h"
19
60 use blas_oct_m
61 use debug_oct_m
64 use global_oct_m
65 use, intrinsic :: iso_fortran_env
66 use iso_c_binding
67 use math_oct_m
70 use parser_oct_m
72 use ps_oct_m
76 use xc_f03_lib_m
77#ifdef HAVE_LIBXC_FUNCS
78 use xc_f03_funcs_m
79#endif
81
82 implicit none
83
84 private
85 public :: &
106
108 integer, parameter :: PAW_KRESSE_JOUBERT = 1
109 integer, parameter :: PAW_BLOCHL = 2
110
112 integer, parameter :: &
113 PAW_XC_SCHEME_FULL = 1, & !< exact evaluation on the radial x angular grid
115
118 type paw_sphere_t
119 private
120 integer :: nspin = 1
121 integer :: spin_channels = 1
122
123 ! --- radial grid (native dataset mesh + integration weights), truncated at r_c ---
124 real(real64) :: rc = m_zero
125 integer :: nr = 0
126 real(real64), allocatable :: r(:)
127 real(real64), allocatable :: r2(:)
128 real(real64), allocatable :: invr(:)
131 real(real64), allocatable :: wr(:)
132 real(real64), allocatable :: vwr(:)
133
134 ! --- partial waves (radial channels, file order) ---
135 integer :: nrad = 0
136 integer, allocatable :: lrad(:)
137 real(real64), allocatable :: phi_ae(:,:)
138 real(real64), allocatable :: phi_ps(:,:)
139 integer, allocatable :: rad_2j(:)
140 logical :: has_small = .false.
141 real(real64), allocatable :: phi_ae_small(:,:)
142
143 ! --- (l,m)-resolved projector enumeration ---
144 integer :: nlm = 0
145 integer, allocatable :: p_rad(:)
146 integer, allocatable :: p_l(:)
147 integer, allocatable :: p_m(:)
148
149 ! --- compensation augmentation Qhat_ij^L(r), radial-channel pairs ---
150 integer :: laug = -1
151 real(real64), allocatable :: qaug(:,:,:)
152
153 ! --- precomputed radial pair products and packed Gaunt coupling ---
157 integer :: npair = 0
158 real(real64), allocatable :: pair_ae(:,:)
159 real(real64), allocatable :: pair_ps(:,:)
160 integer :: ngnt = 0
161 integer, allocatable :: gnt_pair(:)
162 integer, allocatable :: gnt_l(:)
163 integer, allocatable :: gnt_lm(:)
164 real(real64), allocatable :: gnt_c(:)
165 integer, allocatable :: gnt_ptr(:)
168
169 ! --- radial derivatives, only for GGA. They are computed once on the full dataset grid
170 ! (no boundary effect at r_c). As the derivative is linear, grad n = sum_ij rho_ij grad g_ij
171 ! holds exactly, so that D stays the exact derivative of the discrete E_xc. ---
172 real(real64), allocatable :: dpair_ae(:,:)
173 real(real64), allocatable :: dpair_ps(:,:)
174 real(real64), allocatable :: dqaug(:,:,:)
175 real(real64), allocatable :: nc_ae_der(:)
176 real(real64), allocatable :: nc_ps_der(:)
177
178 ! --- banded radial first-derivative operator of the moment XC scheme ---
185 integer :: der_nw = 5
186 real(real64), allocatable :: der_w(:,:)
187 integer, allocatable :: der_i0(:)
188 integer :: der_ntw = 0
189 real(real64), allocatable :: der_wt(:,:)
190 integer, allocatable :: der_trow0(:)
191 integer, allocatable :: der_tn(:)
193 ! --- core densities and reference potentials (spherical, radial) ---
194 logical :: has_ae_core = .false.
195 logical :: has_ps_core = .false.
196 real(real64), allocatable :: nc_ae(:)
197 real(real64), allocatable :: nc_ps(:)
198 real(real64), allocatable :: vref_ae(:)
199 real(real64), allocatable :: vref_ps(:)
201 ! --- kinetic matrices (constant over the SCF) ---
202 real(real64), allocatable :: kin_ae(:,:)
203 real(real64), allocatable :: kin_ps(:,:)
205 ! --- angular quadrature (Gauss-Legendre in cos(theta) x uniform in phi) ---
206 integer :: nang = 0
207 integer :: lmax_rho = 0
208 integer :: nlm_rho = 0
209 real(real64), allocatable :: wang(:)
210 real(real64), allocatable :: ylm(:,:)
211 real(real64), allocatable :: dylm_dth(:,:)
212 real(real64), allocatable :: dylm_dph(:,:)
213 real(real64), allocatable :: wylm(:,:)
214 real(real64), allocatable :: wdylm_dth(:,:)
215 real(real64), allocatable :: wdylm_dph(:,:)
219 real(real64), allocatable :: rhat(:,:)
220 real(real64), allocatable :: that(:,:)
221 real(real64), allocatable :: phat(:,:)
223 ! --- formulation / functional ---
224 integer :: formulation = paw_kresse_joubert
226 type(xc_functional_t) :: xc_functl(2)
227 logical :: xc_is_gga = .false.
228 integer :: xc_scheme = paw_xc_scheme_moments
231 real(real64), parameter :: gaunt_tol = 1.0e-12_real64
234 real(real64), parameter :: paw_xc_tiny = 1.0e-12_real64
236contains
238 ! ---------------------------------------------------------
240 subroutine paw_onsite_init(this, ps, namespace, formulation, nspin)
241 type(paw_sphere_t), intent(out) :: this
242 type(ps_t), intent(in) :: ps
243 type(namespace_t), intent(in) :: namespace
244 integer, intent(in) :: formulation
245 integer, intent(in) :: nspin
247 integer :: nr, nr_full, i, j, k, maxl, ilm, ll, mm, pair
248 integer, allocatable :: cpos(:)
249 real(real64), allocatable :: r_full(:), phi_ae_full(:,:), phi_ps_full(:,:), work(:), dwork(:)
250 real(real64), allocatable :: phi_ae_small_full(:,:)
251 real(real64), allocatable :: qaug_full(:,:,:)
252
253 push_sub_with_profile(paw_onsite_init)
254
255 assert(ps_is_paw(ps))
256 assert(formulation == paw_kresse_joubert .or. formulation == paw_blochl)
258 this%formulation = formulation
259 this%nspin = nspin
260 this%spin_channels = min(nspin, 2)
262 ! --- exchange-correlation functionals, taken from the pseudopotential ---
263 call paw_onsite_init_xc(this, ps, namespace)
264
265 !%Variable PAWXCScheme
266 !%Type integer
267 !%Default paw_xc_moments
268 !%Section Hamiltonian
269 !%Description
270 !% Scheme used to evaluate the PAW one-center (inside the augmentation spheres)
271 !% exchange-correlation energy and potential.
272 !%Option paw_xc_full 1
273 !% Exact evaluation of the exchange-correlation functional on the radial x angular
274 !% one-center grid.
275 !%Option paw_xc_moments 2
276 !% Second-order expansion of the exchange-correlation potential around the spherical
277 !% part of the one-center density, expressed over the density moments n_LM(r)
278 !% (Torrent et al., Comput. Mater. Sci. 42, 337 (2008), Eqs. (56)-(65)). The functional is
279 !% evaluated on the radial grid only, which makes the one-center update much
280 !% faster; with the analytic LibXC kernels the expansion is typically accurate
281 !% to 1e-8 Ha per atom or better.
282 !%End
283 call parse_variable(namespace, 'PAWXCScheme', paw_xc_scheme_moments, this%xc_scheme)
284 if (.not. varinfo_valid_option('PAWXCScheme', this%xc_scheme)) then
285 call messages_input_error(namespace, 'PAWXCScheme')
286 end if
288 ! The moment expansion needs the second and third derivatives of the functionals, which
289 ! some functionals (or some builds of LibXC) do not provide.
290 if (this%xc_scheme == paw_xc_scheme_moments .and. .not. paw_onsite_xc_has_kernels(this)) then
291 if (parse_is_defined(namespace, 'PAWXCScheme')) then
292 message(1) = "PAWXCScheme = paw_xc_moments needs the second and third derivatives (fxc and kxc)"
293 message(2) = "of the exchange-correlation functional of the PAW dataset, which this functional"
294 message(3) = "or this LibXC library does not provide. Use PAWXCScheme = paw_xc_full instead."
295 call messages_fatal(3, namespace=namespace)
296 end if
297 message(1) = "Info: the exchange-correlation functional of the PAW dataset has no fxc/kxc in this"
298 message(2) = " LibXC library: using PAWXCScheme = paw_xc_full."
299 call messages_info(2, namespace=namespace)
300 this%xc_scheme = paw_xc_scheme_full
301 end if
302
303 ! --- radial grid ---
304 ! The radial functions are first evaluated on the full dataset grid, where the radial
305 ! derivatives are taken, and are then restricted to the augmentation sphere r <= r_c,
306 ! where all the one-center integrals are done. r_c is a point of the dataset grid; the
307 ! tolerance only absorbs the rounding of r_c in the file.
308 nr_full = size(ps%paw_grid)
309 safe_allocate(r_full(1:nr_full))
310 r_full(1:nr_full) = ps%paw_grid(1:nr_full)
311 this%rc = ps%paw_cutoff_radius
312 assert(this%rc > m_zero)
313 nr = 0
314 do k = 1, nr_full
315 if (r_full(k) > this%rc*(m_one + 1.0e-6_real64)) exit
316 nr = k
317 end do
318 assert(nr > 2)
319 this%nr = nr
320 safe_allocate(this%r(1:nr))
321 safe_allocate(this%r2(1:nr))
322 safe_allocate(this%wr(1:nr))
323 safe_allocate(this%vwr(1:nr))
324 this%r(1:nr) = r_full(1:nr)
325 this%r2(1:nr) = this%r(1:nr)**2
326 this%wr(1:nr) = ps%paw_rab(1:nr)
327 this%vwr(1:nr) = this%r2(1:nr)*this%wr(1:nr)
328 ! 1/r with the origin (r = 0) mapped to 0: the (phi/r) angular-gradient terms vanish there
329 ! for the physical partial waves, so this removes the r > 0 branch from the hot loops.
330 safe_allocate(this%invr(1:nr))
331 do k = 1, nr
332 if (this%r(k) > m_zero) then
333 this%invr(k) = m_one/this%r(k)
334 else
335 this%invr(k) = m_zero
336 end if
337 end do
338
339 ! Banded radial first-derivative operator of the moment XC scheme
341
342 ! --- radial partial waves ---
343 this%nrad = ps%paw_nwavefunctions
344 this%npair = (this%nrad*(this%nrad + 1))/2
345 safe_allocate(this%lrad(1:this%nrad))
346 this%lrad(1:this%nrad) = ps%paw_l(1:this%nrad)
347 safe_allocate(this%rad_2j(1:this%nrad))
348 this%rad_2j = 0
349 if (allocated(ps%paw_2j)) this%rad_2j(1:this%nrad) = ps%paw_2j(1:this%nrad)
350 safe_allocate(phi_ae_full(1:nr_full, 1:this%nrad))
351 safe_allocate(phi_ps_full(1:nr_full, 1:this%nrad))
352 do i = 1, this%nrad
353 do k = 1, nr_full
354 phi_ae_full(k, i) = spline_eval(ps%paw_phi_ae(i), r_full(k))
355 phi_ps_full(k, i) = spline_eval(ps%paw_phi_ps(i), r_full(k))
356 end do
357 end do
358 safe_allocate(this%phi_ae(1:nr, 1:this%nrad))
359 safe_allocate(this%phi_ps(1:nr, 1:this%nrad))
360 this%phi_ae(1:nr, :) = phi_ae_full(1:nr, :)
361 this%phi_ps(1:nr, :) = phi_ps_full(1:nr, :)
362
363 ! Fully-relativistic datasets: small component of the AE partial waves
364 this%has_small = ps%paw_has_small
365 if (this%has_small) then
366 safe_allocate(phi_ae_small_full(1:nr_full, 1:this%nrad))
367 do i = 1, this%nrad
368 do k = 1, nr_full
369 phi_ae_small_full(k, i) = spline_eval(ps%paw_phi_ae_small(i), r_full(k))
370 end do
371 end do
372 safe_allocate(this%phi_ae_small(1:nr, 1:this%nrad))
373 this%phi_ae_small(1:nr, :) = phi_ae_small_full(1:nr, :)
374 end if
375
376 maxl = maxval(this%lrad(1:this%nrad))
377 this%lmax_rho = 2*maxl
378
379 ! The real_gaunt cache is limited to l <= 6; the density couples up to 2*maxl.
380 if (this%lmax_rho > 6) then
381 call messages_not_implemented("PAW one-center engine for partial waves with l > 3", namespace=namespace)
382 end if
383
384 ! --- (l,m)-resolved projector enumeration (file order over radial channels) ---
385 this%nlm = 0
386 do i = 1, this%nrad
387 this%nlm = this%nlm + 2*this%lrad(i) + 1
388 end do
389 safe_allocate(this%p_rad(1:this%nlm))
390 safe_allocate(this%p_l(1:this%nlm))
391 safe_allocate(this%p_m(1:this%nlm))
392 ilm = 0
393 do i = 1, this%nrad
394 do mm = -this%lrad(i), this%lrad(i)
395 ilm = ilm + 1
396 this%p_rad(ilm) = i
397 this%p_l(ilm) = this%lrad(i)
398 this%p_m(ilm) = mm
399 end do
400 end do
401 assert(ilm == this%nlm)
402
403 this%nlm_rho = (this%lmax_rho + 1)**2
404
405 ! Compensation augmentation Qhat_ij^L(r)
406 ! ps%radial_augmentation is stored in the octopus (cmap) channel order, obtained in ps_xml_load
407 ! by mapping the projectors per l block through cmap. cpos translates the file radial index to
408 ! its cmap position: the identity for scalar-relativistic datasets, the j = l+1/2-first
409 ! permutation within each pair for fully-relativistic ones. This assumes:
410 ! - there is one augmentation channel per partial wave;
411 ! - the partial waves are stored grouped by increasing l (also assumed for paw_l).
412 ! Qhat is symmetric under i <-> j, so only the packed i <= j triangle is stored.
413 assert(ps%radial_augmentation_nprojectors == this%nrad)
414 do i = 2, this%nrad
415 assert(this%lrad(i) >= this%lrad(i-1))
416 end do
417 safe_allocate(cpos(1:this%nrad))
418 do i = 1, this%nrad
419 pair = 0
420 do j = 1, i
421 if (this%lrad(j) == this%lrad(i)) pair = pair + 1
422 end do
423 cpos(i) = pair
424 if (this%rad_2j(i) > 0 .and. this%lrad(i) > 0) then
425 if (this%rad_2j(i) == 2*this%lrad(i) - 1) then
426 cpos(i) = (pair - 1)/2*2 + 2
427 else
428 cpos(i) = (pair - 1)/2*2 + 1
429 end if
430 end if
431 cpos(i) = i - pair + cpos(i)
432 end do
433
434 this%laug = ps%radial_augmentation_lmax
435 assert(this%laug >= 0)
436 safe_allocate(qaug_full(1:nr_full, 1:this%npair, 0:this%laug))
437 do i = 1, this%nrad
438 do j = i, this%nrad
439 pair = paw_pair_index(i, j)
440 do ll = 0, this%laug
441 do k = 1, nr_full
442 qaug_full(k, pair, ll) = spline_eval(ps%radial_augmentation((cpos(i)-1)*this%nrad + cpos(j), ll), r_full(k))
443 end do
444 end do
445 end do
446 end do
447 safe_allocate(this%qaug(1:nr, 1:this%npair, 0:this%laug))
448 this%qaug(1:nr, :, :) = qaug_full(1:nr, :, :)
449 safe_deallocate_a(cpos)
450
451 ! Core densities and reference potentials
452 this%has_ae_core = ps%paw_has_ae_core
453 this%has_ps_core = ps_has_nlcc(ps)
454 safe_allocate(this%nc_ae(1:nr))
455 safe_allocate(this%nc_ps(1:nr))
456 safe_allocate(this%vref_ae(1:nr))
457 safe_allocate(this%vref_ps(1:nr))
458 this%nc_ae = m_zero
459 this%nc_ps = m_zero
460 do k = 1, nr
461 if (this%has_ae_core) this%nc_ae(k) = spline_eval(ps%paw_ae_core, this%r(k))
462 if (this%has_ps_core) this%nc_ps(k) = spline_eval(ps%core, this%r(k))
463 this%vref_ae(k) = spline_eval(ps%paw_ae_vlocal, this%r(k))
464 this%vref_ps(k) = spline_eval(ps%vl, this%r(k))
465 end do
466
467 ! Radial derivatives needed for the GGA gradient of the density.
468 if (this%xc_is_gga) then
469 safe_allocate(work(1:nr_full))
470 safe_allocate(dwork(1:nr_full))
471 safe_allocate(this%dpair_ae(1:nr, 1:this%npair))
472 safe_allocate(this%dpair_ps(1:nr, 1:this%npair))
473 safe_allocate(this%dqaug(1:nr, 1:this%npair, 0:this%laug))
474 do i = 1, this%nrad
475 do j = i, this%nrad
476 pair = paw_pair_index(i, j)
477 work(:) = phi_ae_full(:, i)*phi_ae_full(:, j)
478 ! the small component completes the AE pair density (see paw_onsite_init_pairs)
479 if (this%has_small) work(:) = work(:) + phi_ae_small_full(:, i)*phi_ae_small_full(:, j)
480 call paw_radial_derivative(r_full, work, dwork)
481 this%dpair_ae(1:nr, pair) = dwork(1:nr)
482 work(:) = phi_ps_full(:, i)*phi_ps_full(:, j)
483 call paw_radial_derivative(r_full, work, dwork)
484 this%dpair_ps(1:nr, pair) = dwork(1:nr)
485 do ll = 0, this%laug
486 call paw_radial_derivative(r_full, qaug_full(:, pair, ll), dwork)
487 this%dqaug(1:nr, pair, ll) = dwork(1:nr)
488 end do
489 end do
490 end do
491
492 safe_allocate(this%nc_ae_der(1:nr))
493 safe_allocate(this%nc_ps_der(1:nr))
494 this%nc_ae_der = m_zero
495 this%nc_ps_der = m_zero
496 if (this%has_ae_core) then
497 do k = 1, nr_full
498 work(k) = spline_eval(ps%paw_ae_core, r_full(k))
499 end do
500 call paw_radial_derivative(r_full, work, dwork)
501 this%nc_ae_der(1:nr) = dwork(1:nr)
502 end if
503 if (this%has_ps_core) then
504 do k = 1, nr_full
505 work(k) = spline_eval(ps%core, r_full(k))
506 end do
507 call paw_radial_derivative(r_full, work, dwork)
508 this%nc_ps_der(1:nr) = dwork(1:nr)
509 end if
510 safe_deallocate_a(work)
511 safe_deallocate_a(dwork)
512 end if
513
514 ! Kinetic matrices
515 call paw_onsite_kinetic(this, r_full, phi_ae_full, phi_ps_full)
516
517 ! Angular grid + spherical harmonics
518 call paw_onsite_angular_grid(this)
519
520 ! Radial pair products and packed Gaunt couplings
521 call paw_onsite_init_pairs(this)
522
523 safe_deallocate_a(r_full)
524 safe_deallocate_a(phi_ae_full)
525 safe_deallocate_a(phi_ae_small_full)
526 safe_deallocate_a(phi_ps_full)
527 safe_deallocate_a(qaug_full)
528
529 pop_sub_with_profile(paw_onsite_init)
530 end subroutine paw_onsite_init
531
532 ! ---------------------------------------------------------
536 integer pure function paw_pair_index(ri, rj)
537 integer, intent(in) :: ri, rj
538
539 integer :: i, j
540
541 i = min(ri, rj)
542 j = max(ri, rj)
543 paw_pair_index = (j*(j-1))/2 + i
544
545 end function paw_pair_index
546
547 ! ---------------------------------------------------------
550 subroutine paw_onsite_init_pairs(this)
551 type(paw_sphere_t), intent(inout) :: this
552
553 integer :: i, j, ri, rj, li, mi, lj, mj, ll, mm, pair, ig, ij, np, ipass
554 real(real64) :: c
555
556 push_sub(paw_onsite_init_pairs)
557
558 np = this%nr
559 assert(this%npair == (this%nrad*(this%nrad + 1))/2)
560 safe_allocate(this%pair_ae(1:np, 1:this%npair))
561 safe_allocate(this%pair_ps(1:np, 1:this%npair))
562 do i = 1, this%nrad
563 do j = i, this%nrad
564 pair = paw_pair_index(i, j)
565 this%pair_ae(:, pair) = this%phi_ae(:, i)*this%phi_ae(:, j)
566 this%pair_ps(:, pair) = this%phi_ps(:, i)*this%phi_ps(:, j)
567 ! Fully-relativistic datasets: the small-component product completes the AE pair
568 ! density, with the same (l_i, l_j) angular structure as the large component
569 if (this%has_small) then
570 this%pair_ae(:, pair) = this%pair_ae(:, pair) + this%phi_ae_small(:, i)*this%phi_ae_small(:, j)
571 end if
572 end do
573 end do
574
575 ! Two passes over the Gaunt-live couplings: count, then fill the CSR arrays.
576 safe_allocate(this%gnt_ptr(1:this%nlm*this%nlm + 1))
577 do ipass = 1, 2
578 ig = 0
579 do i = 1, this%nlm
580 ri = this%p_rad(i)
581 li = this%p_l(i)
582 mi = this%p_m(i)
583 do j = 1, this%nlm
584 rj = this%p_rad(j)
585 lj = this%p_l(j)
586 mj = this%p_m(j)
587 pair = paw_pair_index(ri, rj)
588 ij = (i-1)*this%nlm + j
589 if (ipass == 2) this%gnt_ptr(ij) = ig + 1
590 do ll = abs(li-lj), min(li+lj, this%lmax_rho), 2
591 do mm = -ll, ll
592 c = real_gaunt(li, mi, lj, mj, ll, mm)
593 if (abs(c) <= gaunt_tol) cycle
594 ig = ig + 1
595 if (ipass == 2) then
596 this%gnt_pair(ig) = pair
597 this%gnt_l(ig) = ll
598 this%gnt_lm(ig) = ll*(ll+1) + mm + 1
599 this%gnt_c(ig) = c
600 end if
601 end do
602 end do
603 end do
604 end do
605 if (ipass == 1) then
606 this%ngnt = ig
607 safe_allocate(this%gnt_pair(1:this%ngnt))
608 safe_allocate(this%gnt_l(1:this%ngnt))
609 safe_allocate(this%gnt_lm(1:this%ngnt))
610 safe_allocate(this%gnt_c(1:this%ngnt))
611 else
612 this%gnt_ptr(this%nlm*this%nlm + 1) = ig + 1
613 assert(ig == this%ngnt)
614 end if
615 end do
616
617 pop_sub(paw_onsite_init_pairs)
618 end subroutine paw_onsite_init_pairs
619
620 ! ---------------------------------------------------------
622 subroutine paw_onsite_init_xc(this, ps, namespace)
623 type(paw_sphere_t), intent(inout) :: this
624 type(ps_t), intent(in) :: ps
625 type(namespace_t), intent(in) :: namespace
626
627 integer :: ixc, ids(2)
628
629 push_sub(paw_onsite_init_xc)
630
631 ! A functional that the pseudopotential reader could not identify must not be silently
632 ! replaced by "no functional": the one-center XC would then be wrong.
633 if (ps%exchange_functional == pseudo_exchange_unknown .or. &
634 ps%correlation_functional == pseudo_correlation_unknown) then
635 message(1) = "PAW one-center engine: the exchange-correlation functional of the PAW dataset"
636 message(2) = "could not be identified. Only LDA and GGA functionals are supported."
637 call messages_fatal(2, namespace=namespace)
638 end if
639
640 ids(1) = ps%exchange_functional
641 ids(2) = ps%correlation_functional
642
643 this%xc_is_gga = .false.
644 do ixc = 1, 2
645 ! a non-positive id means "not specified": initialize an empty (family NONE) functional
646 if (ids(ixc) > 0) then
647 call xc_functional_init(this%xc_functl(ixc), namespace, ids(ixc), 3, ps%z_val, this%spin_channels, .true.)
648 else
649 call xc_functional_init(this%xc_functl(ixc), namespace, 0, 3, ps%z_val, this%spin_channels, .true.)
650 end if
651 ! Only (semi-)local LDA and GGA are supported.
652 select case (this%xc_functl(ixc)%family)
653 case (xc_family_none, xc_family_lda)
654 ! local: nothing to do
655 case (xc_family_gga)
656 this%xc_is_gga = .true.
657 case (xc_family_hyb_lda, xc_family_hyb_gga, xc_family_hyb_mgga)
658 call messages_not_implemented("PAW one-center engine with hybrid functionals", namespace=namespace)
659 case default
660 call messages_not_implemented("PAW one-center engine with this XC functional family " // &
661 "(only LDA and GGA are supported)", namespace=namespace)
662 end select
663 end do
664
665 pop_sub(paw_onsite_init_xc)
666 end subroutine paw_onsite_init_xc
667
668 ! ---------------------------------------------------------
671 logical function paw_onsite_xc_has_kernels(this) result(has_kernels)
672 type(paw_sphere_t), intent(in) :: this
673
674 integer :: ixc
675
677
678 has_kernels = .true.
679 do ixc = 1, 2
680 if (this%xc_functl(ixc)%family == xc_family_none) cycle
681 if (bitand(this%xc_functl(ixc)%flags, xc_flags_have_fxc) == 0) has_kernels = .false.
682 if (bitand(this%xc_functl(ixc)%flags, xc_flags_have_kxc) == 0) has_kernels = .false.
683 end do
684
686 end function paw_onsite_xc_has_kernels
687
688 ! ---------------------------------------------------------
697 subroutine paw_onsite_kinetic(this, r_full, phi_ae_full, phi_ps_full)
698 type(paw_sphere_t), intent(inout) :: this
699 real(real64), intent(in) :: r_full(:)
700 real(real64), intent(in) :: phi_ae_full(:,:)
701 real(real64), intent(in) :: phi_ps_full(:,:)
702
703 integer :: i, j, k, nr, ll
704 real(real64), allocatable :: uae(:,:), ups(:,:), duae(:,:), dups(:,:)
705 real(real64) :: kae, kps, cent
706
707 push_sub_with_profile(paw_onsite_kinetic)
708
709 safe_allocate(uae(1:size(r_full), 1:this%nrad))
710 safe_allocate(ups(1:size(r_full), 1:this%nrad))
711 safe_allocate(duae(1:size(r_full), 1:this%nrad))
712 safe_allocate(dups(1:size(r_full), 1:this%nrad))
713
714 do i = 1, this%nrad
715 uae(:, i) = r_full(:) * phi_ae_full(:, i)
716 ups(:, i) = r_full(:) * phi_ps_full(:, i)
717 call paw_radial_derivative(r_full, uae(:, i), duae(:, i))
718 call paw_radial_derivative(r_full, ups(:, i), dups(:, i))
719 end do
720
721 nr = this%nr
722 safe_allocate(this%kin_ae(1:this%nrad, 1:this%nrad))
723 safe_allocate(this%kin_ps(1:this%nrad, 1:this%nrad))
724 this%kin_ae = m_zero
725 this%kin_ps = m_zero
726 do i = 1, this%nrad
727 do j = 1, this%nrad
728 if (this%lrad(i) /= this%lrad(j)) cycle
729 ll = this%lrad(i)
730 cent = real(ll*(ll+1), real64)
731 kae = m_zero
732 kps = m_zero
733 ! The r=0 point is skipped: u = r phi ~ r^(l+1) makes the integrand vanish there
734 ! for l>0 and finite for l=0, but its first-interval weight (~r_2) is tiny on the
735 ! log grid and its integration-weight convention is ambiguous, so including it only
736 ! adds noise. The centrifugal term is well defined for k>=2 (r_k > 0).
737 do k = 2, nr
738 kae = kae + (duae(k,i)*duae(k,j) + cent*uae(k,i)*uae(k,j)/this%r2(k)) * this%wr(k)
739 kps = kps + (dups(k,i)*dups(k,j) + cent*ups(k,i)*ups(k,j)/this%r2(k)) * this%wr(k)
740 end do
741 this%kin_ae(i, j) = m_half*kae
742 this%kin_ps(i, j) = m_half*kps
743 end do
744 end do
745
746 safe_deallocate_a(uae)
747 safe_deallocate_a(ups)
748 safe_deallocate_a(duae)
749 safe_deallocate_a(dups)
750
751 pop_sub_with_profile(paw_onsite_kinetic)
752 end subroutine paw_onsite_kinetic
753
754 ! ---------------------------------------------------------
756 subroutine paw_radial_derivative(r, f, df)
757 real(real64), intent(in) :: r(:)
758 real(real64), intent(in) :: f(:)
759 real(real64), intent(out) :: df(:)
760
761 integer :: k, n
762 type(spline_t) :: sp, dsp
763
764 push_sub(paw_radial_derivative)
765
766 n = size(r)
767 call spline_init(sp)
768 call spline_init(dsp)
769 call spline_fit(n, r, f, sp)
770 call spline_der(sp, dsp, m_zero)
771 do k = 1, n
772 df(k) = spline_eval(dsp, r(k))
773 end do
774 call spline_end(sp)
775 call spline_end(dsp)
776
777 pop_sub(paw_radial_derivative)
778 end subroutine paw_radial_derivative
779
780 ! ---------------------------------------------------------
786 subroutine paw_onsite_init_derivative(this)
787 type(paw_sphere_t), intent(inout) :: this
788
789 integer :: ip, i0, nw, jj, kk
790 integer, allocatable :: first(:), last(:)
791
793
794 nw = this%der_nw
795 assert(this%nr >= nw)
796 safe_allocate(this%der_w(1:nw, 1:this%nr))
797 safe_allocate(this%der_i0(1:this%nr))
798 do ip = 1, this%nr
799 i0 = min(max(ip - (nw-1)/2, 1), this%nr - nw + 1)
800 this%der_i0(ip) = i0
801 call paw_fornberg_weights(this%r(ip), this%r(i0:i0+nw-1), nw, this%der_w(:, ip))
802 end do
803
804 safe_allocate(first(1:this%nr))
805 safe_allocate(last(1:this%nr))
806 first = this%nr + 1
807 last = 0
808 do ip = 1, this%nr
809 do kk = 1, nw
810 jj = this%der_i0(ip) + kk - 1
811 first(jj) = min(first(jj), ip)
812 last(jj) = max(last(jj), ip)
813 end do
814 end do
815 this%der_ntw = maxval(last - first + 1)
816 safe_allocate(this%der_wt(1:this%der_ntw, 1:this%nr))
817 safe_allocate(this%der_trow0(1:this%nr))
818 safe_allocate(this%der_tn(1:this%nr))
819 this%der_wt = m_zero
820 this%der_trow0(1:this%nr) = first(1:this%nr)
821 this%der_tn(1:this%nr) = last(1:this%nr) - first(1:this%nr) + 1
822 do ip = 1, this%nr
823 do kk = 1, nw
824 jj = this%der_i0(ip) + kk - 1
825 this%der_wt(ip - first(jj) + 1, jj) = this%der_w(kk, ip)
826 end do
827 end do
828 safe_deallocate_a(first)
829 safe_deallocate_a(last)
830
832 end subroutine paw_onsite_init_derivative
833
834 ! ---------------------------------------------------------
837 subroutine paw_fornberg_weights(z, x, n, w)
838 real(real64), intent(in) :: z
839 real(real64), intent(in) :: x(:)
840 integer, intent(in) :: n
841 real(real64), intent(out) :: w(:)
842
843 integer :: i, j, k, mn
844 real(real64) :: c1, c2, c3, c4, c5
845 real(real64) :: c(n, 0:1)
846
847 ! no PUSH_SUB, called too often
848
849 c = m_zero
850 c1 = m_one
851 c4 = x(1) - z
852 c(1, 0) = m_one
853 do i = 2, n
854 mn = min(i - 1, 1)
855 c2 = m_one
856 c5 = c4
857 c4 = x(i) - z
858 do j = 1, i - 1
859 c3 = x(i) - x(j)
860 c2 = c2*c3
861 if (j == i - 1) then
862 do k = mn, 1, -1
863 c(i, k) = c1*(real(k, real64)*c(i-1, k-1) - c5*c(i-1, k))/c2
864 end do
865 c(i, 0) = -c1*c5*c(i-1, 0)/c2
866 end if
867 do k = mn, 1, -1
868 c(j, k) = (c4*c(j, k) - real(k, real64)*c(j, k-1))/c3
869 end do
870 c(j, 0) = c4*c(j, 0)/c3
871 end do
872 c1 = c2
873 end do
874 w(1:n) = c(1:n, 1)
875
876 end subroutine paw_fornberg_weights
877
878 ! ---------------------------------------------------------
881 subroutine paw_radial_derivative_fd(this, f, df)
882 type(paw_sphere_t), intent(in) :: this
883 real(real64), intent(in) :: f(:)
884 real(real64), intent(out) :: df(:)
885
886 integer :: ip, k, i0
887 real(real64) :: acc
888
889 ! no PUSH_SUB, called too often
890
891 do ip = 1, this%nr
892 i0 = this%der_i0(ip)
893 acc = m_zero
894 do k = 1, this%der_nw
895 acc = acc + this%der_w(k, ip)*f(i0 + k - 1)
896 end do
897 df(ip) = acc
898 end do
899
900 end subroutine paw_radial_derivative_fd
901
902 ! ---------------------------------------------------------
908 subroutine paw_radial_derivative_t(this, f, df)
909 type(paw_sphere_t), intent(in) :: this
910 real(real64), intent(in) :: f(:)
911 real(real64), intent(out) :: df(:)
912
913 integer :: ip, k, i0
914 real(real64) :: acc
915
916 ! no PUSH_SUB, called too often
917
918 do ip = 1, this%nr
919 i0 = this%der_trow0(ip)
920 acc = m_zero
921 do k = 1, this%der_tn(ip)
922 acc = acc + this%der_wt(k, ip)*f(i0 + k - 1)
923 end do
924 df(ip) = acc
925 end do
926
927 end subroutine paw_radial_derivative_t
928
929 ! ---------------------------------------------------------
932 subroutine paw_onsite_angular_grid(this)
933 type(paw_sphere_t), intent(inout) :: this
934
935 integer :: nth, nphi, ith, iphi, ia, ll, mm, lm
936 real(real64), allocatable :: gx(:), gw(:)
937 real(real64) :: dphi, phi, sinth, costh, xx(3), y, dyth, dyph
938
939 push_sub_with_profile(paw_onsite_angular_grid)
940
941 ! Enough points to integrate products of harmonics up to lmax_rho exactly, with
942 ! margin for the nonlinear XC integrand.
943 nth = this%lmax_rho + 6
944 nphi = 2*this%lmax_rho + 3
945 this%nang = nth*nphi
946 assert(this%nlm_rho == (this%lmax_rho + 1)**2)
947
948 safe_allocate(gx(1:nth))
949 safe_allocate(gw(1:nth))
950 call gauss_legendre_newton(nth, gx, gw)
951
952 safe_allocate(this%wang(1:this%nang))
953 safe_allocate(this%ylm(1:this%nang, 1:this%nlm_rho))
954 ! The angular derivatives dY/dtheta and (1/sin theta) dY/dphi are needed both for the
955 ! GGA one-center XC gradient and for the kinetic-energy-density reconstruction, so they
956 ! are always tabulated.
957 safe_allocate(this%dylm_dth(1:this%nang, 1:this%nlm_rho))
958 safe_allocate(this%dylm_dph(1:this%nang, 1:this%nlm_rho))
959 safe_allocate(this%rhat(1:this%nang, 1:3))
960 safe_allocate(this%that(1:this%nang, 1:3))
961 safe_allocate(this%phat(1:this%nang, 1:3))
962
963 dphi = m_two*m_pi/real(nphi, real64)
964 ia = 0
965 do ith = 1, nth
966 costh = gx(ith)
967 sinth = sqrt(max(m_one - costh**2, m_zero))
968 do iphi = 1, nphi
969 ia = ia + 1
970 phi = (real(iphi, real64) - m_half)*dphi
971 xx(1) = sinth*cos(phi)
972 xx(2) = sinth*sin(phi)
973 xx(3) = costh
974 ! spherical orthonormal basis in Cartesian coordinates at this direction
975 this%rhat(ia, 1:3) = xx(1:3)
976 this%that(ia, 1) = costh*cos(phi)
977 this%that(ia, 2) = costh*sin(phi)
978 this%that(ia, 3) = -sinth
979 this%phat(ia, 1) = -sin(phi)
980 this%phat(ia, 2) = cos(phi)
981 this%phat(ia, 3) = m_zero
982 this%wang(ia) = gw(ith)*dphi
983 do ll = 0, this%lmax_rho
984 do mm = -ll, ll
985 lm = ll*(ll+1) + mm + 1
986 ! one call returns the harmonic and its two angular-gradient components
987 call ylmr_real_with_grad(xx, ll, mm, y, dyth, dyph)
988 this%ylm(ia, lm) = y
989 this%dylm_dth(ia, lm) = dyth
990 this%dylm_dph(ia, lm) = dyph
991 end do
992 end do
993 end do
994 end do
995 assert(ia == this%nang)
996
997 ! Quadrature-weighted copies: the XC projections absorb the angular weight into the
998 ! harmonics, so the pointwise field builders do not scale by it.
999 safe_allocate(this%wylm(1:this%nang, 1:this%nlm_rho))
1000 safe_allocate(this%wdylm_dth(1:this%nang, 1:this%nlm_rho))
1001 safe_allocate(this%wdylm_dph(1:this%nang, 1:this%nlm_rho))
1002 do lm = 1, this%nlm_rho
1003 do ia = 1, this%nang
1004 this%wylm(ia, lm) = this%wang(ia)*this%ylm(ia, lm)
1005 this%wdylm_dth(ia, lm) = this%wang(ia)*this%dylm_dth(ia, lm)
1006 this%wdylm_dph(ia, lm) = this%wang(ia)*this%dylm_dph(ia, lm)
1007 end do
1008 end do
1009
1010 safe_deallocate_a(gx)
1011 safe_deallocate_a(gw)
1012
1013 pop_sub_with_profile(paw_onsite_angular_grid)
1014 end subroutine paw_onsite_angular_grid
1015
1016 ! ---------------------------------------------------------
1022 subroutine paw_onsite_compute(this, rho, e1, e1t, dij, hxc_only)
1023 type(paw_sphere_t), intent(in) :: this
1024 real(real64), contiguous, intent(in) :: rho(:,:,:)
1025 real(real64), intent(out) :: e1
1026 real(real64), intent(out) :: e1t
1027 real(real64), contiguous, intent(out) :: dij(:,:,:)
1030 logical, optional, intent(in) :: hxc_only
1031
1032 real(real64) :: e_ae, e_ps, kae, kps
1033 real(real64), allocatable :: d_ae(:,:,:), d_ps(:,:,:)
1034 integer :: i, j, is
1035 logical :: hxc_only_
1036
1037 push_sub_with_profile(paw_onsite_compute)
1038
1039 hxc_only_ = optional_default(hxc_only, .false.)
1040
1041 safe_allocate(d_ae(1:this%nlm, 1:this%nlm, 1:this%nspin))
1042 safe_allocate(d_ps(1:this%nlm, 1:this%nlm, 1:this%nspin))
1043
1044 ! All-electron side (compensation off).
1045 call paw_onsite_side(this, rho, .true., e_ae, d_ae, hxc_only_)
1046 ! Pseudo side (compensation on).
1047 call paw_onsite_side(this, rho, .false., e_ps, d_ps, hxc_only_)
1048
1049 e1 = e_ae
1050 e1t = e_ps
1051 dij(:, :, :) = d_ae(:, :, :) - d_ps(:, :, :)
1052
1053 ! Kinetic terms: E1 += sum_spin Tr[rho^s K^AE], E1t += sum_spin Tr[rho^s K^PS], and
1054 ! K^AE - K^PS in each spin channel of the D matrix. Skipped in the Hxc-only (SCF) mode: the
1055 ! kinetic difference is frozen and already carried by the descreened D^0 (PP_DIJ) on the mesh.
1056 if (.not. hxc_only_) then
1057 do is = 1, this%spin_channels
1058 do j = 1, this%nlm
1059 do i = 1, this%nlm
1060 if (this%p_l(i) /= this%p_l(j) .or. this%p_m(i) /= this%p_m(j)) cycle
1061 kae = this%kin_ae(this%p_rad(i), this%p_rad(j))
1062 kps = this%kin_ps(this%p_rad(i), this%p_rad(j))
1063 dij(i, j, is) = dij(i, j, is) + kae - kps
1064 e1 = e1 + rho(i, j, is)*kae
1065 e1t = e1t + rho(i, j, is)*kps
1066 end do
1067 end do
1068 end do
1069 end if
1070
1071 safe_deallocate_a(d_ae)
1072 safe_deallocate_a(d_ps)
1073
1074 pop_sub_with_profile(paw_onsite_compute)
1075 end subroutine paw_onsite_compute
1076
1077 ! ---------------------------------------------------------
1081 subroutine paw_onsite_side(this, rho, ae, energy, dij, hxc_only)
1082 type(paw_sphere_t), intent(in) :: this
1083 real(real64), contiguous, intent(in) :: rho(:,:,:)
1084 logical, intent(in) :: ae
1085 real(real64), intent(out) :: energy
1086 real(real64), contiguous, intent(out) :: dij(:,:,:)
1089 logical, optional, intent(in) :: hxc_only
1090
1091 integer :: nr, nlmr, lm, k, is
1092 logical :: xc_with_comp, hxc_only_, need_der
1093 real(real64), allocatable :: nlm_rho(:,:,:) ! (nr, nlm_rho, nspin) Hartree density (PS incl. comp)
1094 real(real64), allocatable :: nlm_xc(:,:,:) ! (nr, nlm_rho, nspin) XC density (comp per formulation)
1095 real(real64), allocatable :: nlm_xc_der(:,:,:) ! (nr, nlm_rho, nspin) radial derivative of nlm_xc (GGA)
1096 real(real64), allocatable :: nlm_tot(:,:) ! (nr, nlm_rho) total Hartree density
1097 real(real64), allocatable :: vlm(:,:) ! (nr, nlm_rho) Hartree + reference potential
1098 real(real64), allocatable :: vref(:), ncore(:)
1099 real(real64) :: e_h, e_ref, e_xc, sqrt4pi
1100
1101 push_sub_with_profile(paw_onsite_side)
1102
1103 nr = this%nr
1104 nlmr = this%nlm_rho
1105 sqrt4pi = sqrt(m_four*m_pi)
1106 hxc_only_ = optional_default(hxc_only, .false.)
1107
1108 ! Kresse-Joubert puts the compensation charge in the XC density; Bloechl does not.
1109 ! On the all-electron side there is no compensation charge.
1110 xc_with_comp = (.not. ae) .and. (this%formulation == paw_kresse_joubert)
1111
1112 safe_allocate(nlm_rho(1:nr, 1:nlmr, 1:this%nspin))
1113 safe_allocate(nlm_xc(1:nr, 1:nlmr, 1:this%nspin))
1114 safe_allocate(nlm_tot(1:nr, 1:nlmr))
1115 safe_allocate(vlm(1:nr, 1:nlmr))
1116 safe_allocate(vref(1:nr))
1117 safe_allocate(ncore(1:nr))
1118 vlm = m_zero
1119
1120 if (ae) then
1121 vref(:) = this%vref_ae(:)
1122 ncore(:) = this%nc_ae(:)/this%spin_channels
1123 else
1124 vref(:) = this%vref_ps(:)
1125 ncore(:) = this%nc_ps(:)/this%spin_channels
1126 end if
1127
1128 ! Hartree density and XC density (compensation included only for Kresse-Joubert on the PS side).
1129 ! The radial derivative of the XC density is only needed by the GGA on the radial x angular
1130 ! grid; the moment expansion differentiates the spherical density itself.
1131 need_der = this%xc_is_gga .and. this%xc_scheme == paw_xc_scheme_full
1132 if (need_der) then
1133 safe_allocate(nlm_xc_der(1:nr, 1:nlmr, 1:this%nspin))
1134 end if
1135 do is = 1, this%nspin
1136 call paw_build_density(this, rho(:,:,is), ae, .not. ae, nlm_rho(:,:,is))
1137 if (need_der) then
1138 call paw_build_density(this, rho(:,:,is), ae, xc_with_comp, nlm_xc(:,:,is), nlm_xc_der(:,:,is))
1139 else if (xc_with_comp .eqv. (.not. ae)) then
1140 nlm_xc(:,:,is) = nlm_rho(:,:,is)
1141 else
1142 call paw_build_density(this, rho(:,:,is), ae, xc_with_comp, nlm_xc(:,:,is))
1143 end if
1144 end do
1145 ! total density drives the spin-independent Hartree and reference terms
1146 nlm_tot = nlm_rho(:,:,1)
1147 do is = 2, this%spin_channels
1148 nlm_tot = nlm_tot + nlm_rho(:,:,is)
1149 end do
1150
1151 ! Hartree potential per L and Hartree energy
1152 e_h = m_zero
1153 do lm = 1, nlmr
1154 call paw_hartree_lm(this, lm, nlm_tot(:, lm), vlm(:, lm))
1155 do k = 1, nr
1156 e_h = e_h + m_half*nlm_tot(k, lm)*vlm(k, lm)*this%vwr(k)
1157 end do
1158 end do
1159
1160 ! Reference potential term (spherical; couples to the total L=0 density only)
1161 ! E_ref = int v_ref(r) n(r,Omega) d^3r = sqrt(4pi) int v_ref n_00 r^2 dr.
1162 ! Skipped in the Hxc-only (SCF) mode: the reference/nuclear/local coupling is frozen and is
1163 ! carried by the descreened D^0 (PP_DIJ) added by the mesh nonlocal machinery.
1164 e_ref = m_zero
1165 if (.not. hxc_only_) then
1166 do k = 1, nr
1167 e_ref = e_ref + sqrt4pi*vref(k)*nlm_tot(k, 1)*this%vwr(k)
1168 end do
1169 ! add v_ref into the L=0 potential channel so it enters D (spherical -> Y_00 coeff)
1170 call lalg_axpy(nr, sqrt4pi, vref, vlm(:, 1))
1171 end if
1172
1173 ! D matrix from the Hartree + reference potential
1174 dij = m_zero
1175 do is = 1, this%spin_channels
1176 call paw_accumulate_dij(this, ae, .not. ae, vlm, dij(:,:,is))
1177 end do
1178
1179 ! Exchange-correlation
1180 if (this%xc_scheme == paw_xc_scheme_moments) then
1181 call paw_xc_moments(this, nlm_xc, ncore, ae, xc_with_comp, e_xc, dij)
1182 else
1183 call paw_xc(this, nlm_xc, nlm_xc_der, ncore, ae, xc_with_comp, e_xc, dij)
1184 end if
1185
1186 energy = e_h + e_ref + e_xc
1187
1188 safe_deallocate_a(nlm_rho)
1189 safe_deallocate_a(nlm_xc)
1190 safe_deallocate_a(nlm_xc_der)
1191 safe_deallocate_a(nlm_tot)
1192 safe_deallocate_a(vlm)
1193 safe_deallocate_a(vref)
1194 safe_deallocate_a(ncore)
1195
1196 pop_sub_with_profile(paw_onsite_side)
1197 end subroutine paw_onsite_side
1198
1199 ! ---------------------------------------------------------
1210 subroutine paw_build_density(this, rho, ae, with_comp, nlm_rho, nlm_der)
1211 type(paw_sphere_t), intent(in) :: this
1212 real(real64), contiguous, intent(in) :: rho(:,:)
1213 logical, intent(in) :: ae
1214 logical, intent(in) :: with_comp
1215 real(real64), contiguous, intent(out) :: nlm_rho(:,:)
1216 real(real64), contiguous, optional, intent(out) :: nlm_der(:,:)
1217
1218 integer :: i, j, ij, ig
1219 real(real64) :: rho_ij
1220 real(real64), allocatable :: cmat(:,:)
1221
1222 push_sub_with_profile(paw_build_density)
1223
1224 assert(.not. present(nlm_der) .or. this%xc_is_gga)
1225
1226 ! Contract rho_ij with the packed Gaunt couplings into per-(pair, LM) coefficients
1227 ! n_LM(r) = sum_pair pair_X(r, pair) cmat(pair, LM) (+ the qaug compensation per L).
1228 ! The radial derivative uses the same coefficients with the derivative tables.
1229 safe_allocate(cmat(1:this%npair, 1:this%nlm_rho))
1230 cmat = m_zero
1231 do i = 1, this%nlm
1232 do j = i, this%nlm
1233 rho_ij = rho(i, j)
1234 if (j > i) rho_ij = rho_ij + rho(j, i)
1235 if (abs(rho_ij) <= m_epsilon) cycle
1236 ij = (i-1)*this%nlm + j
1237 do ig = this%gnt_ptr(ij), this%gnt_ptr(ij+1) - 1
1238 cmat(this%gnt_pair(ig), this%gnt_lm(ig)) = cmat(this%gnt_pair(ig), this%gnt_lm(ig)) &
1239 + rho_ij*this%gnt_c(ig)
1240 end do
1241 end do
1242 end do
1243
1244 if (ae) then
1245 call lalg_gemm(this%pair_ae, cmat, nlm_rho)
1246 if (present(nlm_der)) call lalg_gemm(this%dpair_ae, cmat, nlm_der)
1247 else
1248 call lalg_gemm(this%pair_ps, cmat, nlm_rho)
1249 if (present(nlm_der)) call lalg_gemm(this%dpair_ps, cmat, nlm_der)
1250 if (with_comp) then
1251 call paw_qaug_gemv(this, this%qaug, 'N', cmat, nlm_rho)
1252 if (present(nlm_der)) call paw_qaug_gemv(this, this%dqaug, 'N', cmat, nlm_der)
1253 end if
1254 end if
1255
1256 safe_deallocate_a(cmat)
1257
1258 pop_sub_with_profile(paw_build_density)
1259 end subroutine paw_build_density
1260
1261 ! ---------------------------------------------------------
1266 subroutine paw_qaug_gemv(this, qtab, trans, xx, yy)
1267 type(paw_sphere_t), intent(in) :: this
1268 real(real64), contiguous, intent(in) :: qtab(:, :, 0:)
1269 character(1), intent(in) :: trans
1270 real(real64), contiguous, intent(in) :: xx(:, :)
1271 real(real64), contiguous, intent(inout) :: yy(:, :)
1272
1273 integer :: lm, ll
1274
1275 ! no PUSH_SUB, light wrapper called in the SCF hot loops
1276
1277 do lm = 1, this%nlm_rho
1278 ll = int(sqrt(real(lm-1, real64)))
1279 if (ll > this%laug) cycle
1280 call blas_gemv(trans, this%nr, this%npair, m_one, qtab(1, 1, ll), this%nr, &
1281 xx(1, lm), 1, m_one, yy(1, lm), 1)
1282 end do
1283
1284 end subroutine paw_qaug_gemv
1285
1286 ! ---------------------------------------------------------
1289 subroutine paw_gaunt_scatter_dij(this, w, dij)
1290 type(paw_sphere_t), intent(in) :: this
1291 real(real64), contiguous, intent(in) :: w(:, :)
1292 real(real64), contiguous, intent(inout) :: dij(:, :)
1293
1294 integer :: i, j, ij, ig
1295 real(real64) :: acc
1296
1297 ! no PUSH_SUB, light scatter called in the SCF hot loops
1298
1299 do i = 1, this%nlm
1300 do j = i, this%nlm
1301 ij = (i-1)*this%nlm + j
1302 acc = m_zero
1303 do ig = this%gnt_ptr(ij), this%gnt_ptr(ij+1) - 1
1304 acc = acc + this%gnt_c(ig)*w(this%gnt_pair(ig), this%gnt_lm(ig))
1305 end do
1306 dij(i, j) = dij(i, j) + acc
1307 if (j > i) dij(j, i) = dij(j, i) + acc
1308 end do
1309 end do
1310
1311 end subroutine paw_gaunt_scatter_dij
1312
1313 ! ---------------------------------------------------------
1316 subroutine paw_emit_lm_columns(this, field_lm, tol, ncol, spl, ll, mm, rcut)
1317 type(paw_sphere_t), intent(in) :: this
1318 real(real64), contiguous, intent(in) :: field_lm(:, :)
1319 real(real64), intent(in) :: tol
1320 integer, intent(out) :: ncol
1321 type(spline_t), allocatable, intent(out) :: spl(:)
1322 integer, allocatable, intent(out) :: ll(:)
1323 integer, allocatable, intent(out) :: mm(:)
1324 real(real64), intent(out) :: rcut
1325
1326 integer :: lm, l0
1327
1328 push_sub(paw_emit_lm_columns)
1329
1330 safe_allocate(spl(1:this%nlm_rho))
1331 safe_allocate(ll(1:this%nlm_rho))
1332 safe_allocate(mm(1:this%nlm_rho))
1333 ncol = 0
1334 do lm = 1, this%nlm_rho
1335 if (maxval(abs(field_lm(:, lm))) <= tol) cycle
1336 ncol = ncol + 1
1337 l0 = int(sqrt(real(lm-1, real64)))
1338 ll(ncol) = l0
1339 mm(ncol) = lm - l0*(l0+1) - 1
1340 call spline_init(spl(ncol))
1341 call spline_fit(this%nr, this%r, field_lm(:, lm), spl(ncol))
1342 end do
1343 rcut = this%r(this%nr)
1344
1345 pop_sub(paw_emit_lm_columns)
1346 end subroutine paw_emit_lm_columns
1347
1348 ! ---------------------------------------------------------
1351 subroutine paw_partial_wave_derivatives(this, dphi_ae, dphi_ps)
1352 type(paw_sphere_t), intent(in) :: this
1353 real(real64), contiguous, intent(out) :: dphi_ae(:, :)
1354 real(real64), contiguous, intent(out) :: dphi_ps(:, :)
1355
1356 integer :: i
1357
1359
1360 do i = 1, this%nrad
1361 call paw_radial_derivative(this%r, this%phi_ae(:, i), dphi_ae(:, i))
1362 call paw_radial_derivative(this%r, this%phi_ps(:, i), dphi_ps(:, i))
1363 end do
1364
1366 end subroutine paw_partial_wave_derivatives
1367
1368 ! ---------------------------------------------------------
1373 subroutine paw_adjoint_divergence_add(this, flux, div, vfield)
1374 type(paw_sphere_t), intent(in) :: this
1375 real(real64), contiguous, intent(in) :: flux(:)
1376 real(real64), contiguous, intent(inout) :: div(:)
1377 real(real64), contiguous, intent(inout) :: vfield(:)
1378
1379 integer :: ip
1380
1381 ! no PUSH_SUB, called per spin and kernel channel
1382
1383 call paw_radial_derivative_t(this, flux, div)
1384 do ip = 1, this%nr
1385 if (this%vwr(ip) > m_zero) then
1386 vfield(ip) = vfield(ip) + div(ip)/this%vwr(ip)
1387 end if
1388 end do
1389
1390 end subroutine paw_adjoint_divergence_add
1391
1392 ! ---------------------------------------------------------
1394 subroutine paw_onsite_reorder_pmat(nprojs, map, src, dst)
1395 integer, intent(in) :: nprojs
1396 integer, intent(in) :: map(:)
1397 real(real64), contiguous, intent(in) :: src(:, :)
1398 real(real64), contiguous, intent(out) :: dst(:, :)
1399
1400 integer :: iproj, jproj
1401
1402 ! no PUSH_SUB, light reorder called per projector matrix
1403
1404 do jproj = 1, nprojs
1405 do iproj = 1, nprojs
1406 dst(map(iproj), map(jproj)) = src(iproj, jproj)
1407 end do
1408 end do
1409
1410 end subroutine paw_onsite_reorder_pmat
1411
1412 ! ---------------------------------------------------------
1416 subroutine paw_hartree_lm(this, lm, nl, vl)
1417 type(paw_sphere_t), intent(in) :: this
1418 integer, intent(in) :: lm
1419 real(real64), intent(in) :: nl(:)
1420 real(real64), intent(out) :: vl(:)
1421
1422 integer :: k, nr, ll
1423 real(real64) :: pref, inner, outer, ti, to
1424 real(real64), allocatable :: cum_in(:)
1425
1426 push_sub_with_profile(paw_hartree_lm)
1427
1428 nr = this%nr
1429 ll = int(sqrt(real(lm-1, real64)))
1430 pref = m_four*m_pi/real(2*ll+1, real64)
1431
1432 safe_allocate(cum_in(1:nr))
1433 ! forward cumulative inner integral, with half-diagonal (midpoint) correction
1434 inner = m_zero
1435 do k = 1, nr
1436 ti = nl(k)*this%r(k)**(ll+2)*this%wr(k)
1437 inner = inner + ti
1438 cum_in(k) = inner - m_half*ti
1439 end do
1440 ! backward cumulative outer integral
1441 vl = m_zero
1442 outer = m_zero
1443 do k = nr, 1, -1
1444 if (this%r(k) > m_zero) then
1445 to = nl(k)*this%r(k)**(1-ll)*this%wr(k)
1446 else
1447 to = m_zero
1448 end if
1449 outer = outer + to
1450 if (this%r(k) > m_zero) then
1451 vl(k) = pref*( cum_in(k)/this%r(k)**(ll+1) + (outer - m_half*to)*this%r(k)**ll )
1452 end if
1453 end do
1454 ! origin: extrapolate
1455 if (this%r(1) <= m_zero) vl(1) = vl(2)
1456
1457 safe_deallocate_a(cum_in)
1458
1459 pop_sub_with_profile(paw_hartree_lm)
1460 end subroutine paw_hartree_lm
1461
1462 ! ---------------------------------------------------------
1479 subroutine paw_xc(this, nlm_rho, nlm_der, ncore, ae, with_comp, e_xc, dij)
1480 type(paw_sphere_t), intent(in) :: this
1481 real(real64), contiguous, intent(in) :: nlm_rho(:,:,:) ! (nr, nlm_rho, nspin)
1483 real(real64), optional, contiguous, intent(in) :: nlm_der(:,:,:)
1484 real(real64), contiguous, intent(in) :: ncore(:) ! (nr) core density per spin channel
1485 logical, intent(in) :: ae
1486 logical, intent(in) :: with_comp
1487 real(real64), intent(out) :: e_xc
1488 real(real64), contiguous, intent(inout) :: dij(:,:,:) ! (nlm, nlm, nspin)
1489
1490 logical :: gga
1491 integer :: np, npa, ibase, ip, ia, is, nsp, nsig
1492 real(real64) :: ntot
1493 real(real64), allocatable :: ncder(:) ! (np) core density gradient per spin channel
1494 ! Grid-major layout: the radial index is the leading (contiguous) dimension, the spin index is
1495 ! last. The density is built at ALL angular nodes first (radial point (ip, ia) at flat index
1496 ! (ia-1)*np + ip) so LibXC is evaluated ONCE on the np*nang points.
1497 real(real64), allocatable :: dens(:,:) ! (np*nang, nspin) density, native spin components
1498 real(real64), allocatable :: densp(:,:) ! (np*nang, nsp) density in the LibXC spin channels
1499 real(real64), allocatable :: sigma(:,:) ! (np*nang, nsig)
1500 real(real64), allocatable :: dpol(:) ! (np*nang) noncollinear polarization magnitude
1501 real(real64), allocatable :: exc(:), vs(:,:)
1502 real(real64), allocatable, target :: vr(:,:) ! (np*nang, nspin) potential
1503 real(real64), allocatable :: g(:,:,:,:) ! (np, nspin, nang, 3) grad n per native component
1504 real(real64), allocatable :: gp(:,:,:,:) ! (np, nsp, nang, 3) grad n per channel
1505 real(real64), allocatable :: vxc_lm(:,:,:) ! (np, nlm_rho, nspin) projected v_xc
1506 real(real64), pointer :: vmat(:,:) ! (np, nang) matrix view of one potential component
1507
1508 push_sub_with_profile(paw_xc)
1509
1510 gga = this%xc_is_gga
1511 np = this%nr
1512 npa = np*this%nang
1513 nsp = this%spin_channels
1514 nsig = 2*nsp - 1 ! 1 for one spin channel, 3 for two (collinear or projected)
1515
1516 safe_allocate(dens(1:npa, 1:this%nspin))
1517 safe_allocate(densp(1:npa, 1:nsp))
1518 safe_allocate(sigma(1:npa, 1:nsig))
1519 safe_allocate(exc(1:npa))
1520 safe_allocate(vr(1:npa, 1:this%nspin))
1521 safe_allocate(vs(1:npa, 1:nsig))
1522 sigma = m_zero
1523
1524 safe_allocate(dpol(1:npa))
1525 call paw_xc_density(this, nlm_rho, ncore, dens, densp, dpol)
1526
1527 if (gga) then
1528 assert(present(nlm_der))
1529 safe_allocate(ncder(1:np))
1530 if (ae) then
1531 ncder(:) = this%nc_ae_der(:)/this%spin_channels
1532 else
1533 ncder(:) = this%nc_ps_der(:)/this%spin_channels
1534 end if
1535 safe_allocate(g(1:np, 1:this%nspin, 1:this%nang, 1:3))
1536 safe_allocate(gp(1:np, 1:nsp, 1:this%nang, 1:3))
1537 call paw_xc_gradient(this, nlm_rho, nlm_der, ncder, g)
1538 call paw_xc_sigma(this, dens, dpol, g, gp, sigma)
1539 end if
1540
1541 ! One LibXC evaluation for all np*nang points
1542 call paw_xc_eval(this, npa, nsp, nsig, densp, sigma, exc, vr(:, 1:nsp), vs)
1543
1544 ! Accumulate the energy per angular node
1545 e_xc = m_zero
1546 !$omp parallel do schedule(static) private(ibase, ip, is, ntot) reduction(+:e_xc)
1547 do ia = 1, this%nang
1548 ibase = (ia - 1)*np
1549 do ip = 1, np
1550 ntot = m_zero
1551 do is = 1, nsp
1552 ntot = ntot + max(densp(ibase + ip, is), m_zero)
1553 end do
1554 e_xc = e_xc + ntot*exc(ibase + ip)*this%vwr(ip)*this%wang(ia)
1555 end do
1556 end do
1557
1558 ! --- D matrix contribution (per native spin component) ---
1559 if (gga) then
1560 call paw_xc_gga_process(this, ae, with_comp, dens, g, gp, vr, vs, dij)
1561 else
1562 ! Rotate the potential back
1563 call paw_xc_lda_process(this, dens, vr)
1564 ! Project onto the Y_LM basis as a matrix product of the grid-major potential
1565 ! with the quadrature-weighted harmonics
1566 safe_allocate(vxc_lm(1:np, 1:this%nlm_rho, 1:this%nspin))
1567 do is = 1, this%nspin
1568 vmat(1:np, 1:this%nang) => vr(:, is)
1569 call lalg_gemm(np, this%nlm_rho, this%nang, m_one, vmat, this%wylm, m_zero, vxc_lm(:, :, is))
1570 end do
1571 do is = 1, this%nspin
1572 call paw_accumulate_dij(this, ae, with_comp, vxc_lm(:,:,is), dij(:,:,is))
1573 end do
1574 end if
1575
1576 safe_deallocate_a(dens)
1577 safe_deallocate_a(densp)
1578 safe_deallocate_a(sigma)
1579 safe_deallocate_a(dpol)
1580 safe_deallocate_a(exc)
1581 safe_deallocate_a(vr)
1582 safe_deallocate_a(vs)
1583 safe_deallocate_a(ncder)
1584 safe_deallocate_a(g)
1585 safe_deallocate_a(gp)
1586 safe_deallocate_a(vxc_lm)
1587
1588 pop_sub_with_profile(paw_xc)
1589 end subroutine paw_xc
1590
1591 ! ---------------------------------------------------------
1595 subroutine paw_xc_density(this, nlm_rho, ncore, dens, densp, dpol)
1596 type(paw_sphere_t), intent(in) :: this
1597 real(real64), intent(in) :: nlm_rho(:,:,:)
1598 real(real64), intent(in) :: ncore(:)
1599 real(real64), contiguous, intent(out) :: dens(:,:) ! (np*nang, nspin)
1600 real(real64), contiguous, intent(out) :: densp(:,:) ! (np*nang, nsp)
1601 real(real64), contiguous, intent(out) :: dpol(:) ! (np*nang) |m|, noncollinear only
1602
1603 integer :: np, ibase, ip, ia, ic
1604
1605 push_sub_with_profile(paw_xc_density)
1606
1607 np = this%nr
1608
1609 ! For angular node ia the np radial points live in the flat segment [ibase+1, ibase+np],
1610 ! ibase = (ia-1)*np; every node writes disjoint segments.
1611 do ia = 1, this%nang
1612 ibase = (ia - 1)*np
1613 do ic = 1, this%nspin
1614 if (ic <= 2) then
1615 do ip = 1, np
1616 dens(ibase + ip, ic) = ncore(ip)
1617 end do
1618 else
1619 do ip = 1, np
1620 dens(ibase + ip, ic) = m_zero
1621 end do
1622 end if
1623 call blas_gemv('N', np, this%nlm_rho, m_one, nlm_rho(1, 1, ic), np, &
1624 this%ylm(ia, 1), this%nang, m_one, dens(ibase + 1, ic), 1)
1625 end do
1626 end do
1627
1628 if (this%nspin == 4) then
1629 !$omp parallel do schedule(static)
1630 do ip = 1, np*this%nang
1631 dpol(ip) = paw_xc_noncol_dpol(dens(ip, 1), dens(ip, 2), dens(ip, 3), dens(ip, 4))
1632 densp(ip, 1) = m_half*(dens(ip, 1) + dens(ip, 2) + dpol(ip))
1633 densp(ip, 2) = m_half*(dens(ip, 1) + dens(ip, 2) - dpol(ip))
1634 end do
1635 else
1636 do ic = 1, this%spin_channels
1637 call lalg_copy(np*this%nang, dens(:, ic), densp(:, ic))
1638 end do
1639 end if
1640
1641 pop_sub_with_profile(paw_xc_density)
1642 end subroutine paw_xc_density
1643
1644 ! ---------------------------------------------------------
1652 subroutine paw_xc_gradient(this, nlm_rho, nlm_der, ncder, g)
1653 type(paw_sphere_t), intent(in) :: this
1654 real(real64), contiguous, intent(in) :: nlm_rho(:,:,:) ! (nr, nlm_rho, nspin)
1655 real(real64), contiguous, intent(in) :: nlm_der(:,:,:) ! (nr, nlm_rho, nspin) d n_LM/dr
1656 real(real64), contiguous, intent(in) :: ncder(:) ! (nr) core density gradient per spin channel
1657 real(real64), contiguous, intent(out) :: g(:,:,:,:) ! (nr, nspin, nang, 3)
1658
1659 integer :: np, ip, ia, ic
1660
1661 push_sub_with_profile(paw_xc_gradient)
1662
1663 np = this%nr
1664
1665 do ia = 1, this%nang
1666 do ic = 1, this%nspin
1667 if (ic <= 2) then
1668 do ip = 1, np
1669 g(ip, ic, ia, 1) = ncder(ip) ! core is spherical: only the radial gradient
1670 end do
1671 else
1672 do ip = 1, np
1673 g(ip, ic, ia, 1) = m_zero
1674 end do
1675 end if
1676 call blas_gemv('N', np, this%nlm_rho, m_one, nlm_der(1, 1, ic), np, &
1677 this%ylm(ia, 1), this%nang, m_one, g(1, ic, ia, 1), 1)
1678 call blas_gemv('N', np, this%nlm_rho, m_one, nlm_rho(1, 1, ic), np, &
1679 this%dylm_dth(ia, 1), this%nang, m_zero, g(1, ic, ia, 2), 1)
1680 call blas_gemv('N', np, this%nlm_rho, m_one, nlm_rho(1, 1, ic), np, &
1681 this%dylm_dph(ia, 1), this%nang, m_zero, g(1, ic, ia, 3), 1)
1682 do ip = 1, np
1683 g(ip, ic, ia, 2) = g(ip, ic, ia, 2)*this%invr(ip)
1684 g(ip, ic, ia, 3) = g(ip, ic, ia, 3)*this%invr(ip)
1685 end do
1686 end do
1687 end do
1688
1689 pop_sub_with_profile(paw_xc_gradient)
1690 end subroutine paw_xc_gradient
1691
1692 ! ---------------------------------------------------------
1696 subroutine paw_xc_sigma(this, dens, dpol, g, gp, sigma)
1697 type(paw_sphere_t), intent(in) :: this
1698 real(real64), contiguous, intent(in) :: dens(:,:)
1699 real(real64), contiguous, intent(in) :: dpol(:) ! (np*nang) |m|, from paw_xc_density
1700 real(real64), contiguous, intent(in) :: g(:,:,:,:)
1701 real(real64), contiguous, intent(out) :: gp(:,:,:,:) ! (np, nsp, nang, 3)
1702 real(real64), contiguous, intent(out) :: sigma(:,:) ! (np*nang, nsig)
1703
1704 integer :: np, ibase, ip, ia, k
1705 real(real64) :: uu, gq
1706
1707 push_sub_with_profile(paw_xc_sigma)
1708
1709 np = this%nr
1710
1711 if (this%nspin == 4) then
1712 !$omp parallel do schedule(static) private(ibase, ip, uu, gq, k)
1713 do ia = 1, this%nang
1714 ibase = (ia - 1)*np
1715 do ip = 1, np
1716 uu = dens(ibase + ip, 1) - dens(ibase + ip, 2)
1717 do k = 1, 3
1718 if (dpol(ibase + ip) > m_zero) then
1719 gq = (uu*(g(ip, 1, ia, k) - g(ip, 2, ia, k)) &
1720 + m_four*(dens(ibase + ip, 3)*g(ip, 3, ia, k) &
1721 + dens(ibase + ip, 4)*g(ip, 4, ia, k)))/dpol(ibase + ip)
1722 else
1723 gq = m_zero
1724 end if
1725 gp(ip, 1, ia, k) = m_half*(g(ip, 1, ia, k) + g(ip, 2, ia, k) + gq)
1726 gp(ip, 2, ia, k) = m_half*(g(ip, 1, ia, k) + g(ip, 2, ia, k) - gq)
1727 end do
1728 end do
1729 end do
1730 else
1731 gp(:, :, :, :) = g(:, :, :, :)
1732 end if
1733
1734 !$omp parallel do schedule(static) private(ibase, ip)
1735 do ia = 1, this%nang
1736 ibase = (ia - 1)*np
1737 do ip = 1, np
1738 sigma(ibase + ip, 1) = gp(ip,1,ia,1)**2 + gp(ip,1,ia,2)**2 + gp(ip,1,ia,3)**2
1739 if (this%spin_channels == 2) then
1740 sigma(ibase + ip, 2) = gp(ip,1,ia,1)*gp(ip,2,ia,1) + gp(ip,1,ia,2)*gp(ip,2,ia,2) &
1741 + gp(ip,1,ia,3)*gp(ip,2,ia,3)
1742 sigma(ibase + ip, 3) = gp(ip,2,ia,1)**2 + gp(ip,2,ia,2)**2 + gp(ip,2,ia,3)**2
1743 end if
1744 end do
1745 end do
1746
1747 pop_sub_with_profile(paw_xc_sigma)
1748 end subroutine paw_xc_sigma
1749
1750 ! ---------------------------------------------------------
1752 subroutine paw_xc_lda_process(this, dens, vr)
1753 type(paw_sphere_t), intent(in) :: this
1754 real(real64), contiguous, intent(in) :: dens(:,:)
1755 real(real64), contiguous, intent(inout) :: vr(:,:) ! (np*nang, nspin)
1756
1757 integer :: ip, ic
1758 real(real64) :: dpol, fwp(4, 2), v1, v2
1759
1760 if (this%nspin /= 4) return
1761
1762 push_sub_with_profile(paw_xc_lda_process)
1763
1764 !$omp parallel do schedule(static) private(dpol, fwp, v1, v2, ic)
1765 do ip = 1, this%nr*this%nang
1766 call paw_xc_noncol_weights(dens(ip, 1), dens(ip, 2), dens(ip, 3), dens(ip, 4), dpol, fwp)
1767 v1 = vr(ip, 1)
1768 v2 = vr(ip, 2)
1769 do ic = 1, 4
1770 vr(ip, ic) = fwp(ic, 1)*v1 + fwp(ic, 2)*v2
1771 end do
1772 end do
1773
1774 pop_sub_with_profile(paw_xc_lda_process)
1775 end subroutine paw_xc_lda_process
1776
1777 ! ---------------------------------------------------------
1779 subroutine paw_xc_gga_process(this, ae, with_comp, dens, g, gp, vr, vs, dij)
1780 type(paw_sphere_t), intent(in) :: this
1781 logical, intent(in) :: ae
1782 logical, intent(in) :: with_comp
1783 real(real64), contiguous, intent(in) :: dens(:,:)
1784 real(real64), contiguous, intent(in) :: g(:,:,:,:), gp(:,:,:,:)
1785 real(real64), contiguous, intent(in) :: vr(:,:), vs(:,:)
1786 real(real64), contiguous, intent(inout) :: dij(:,:,:)
1787
1788 integer :: np, ibase, ip, ia, is, os, iss
1789 real(real64), allocatable :: a1(:,:,:), a2(:,:,:), a3(:,:,:), a4(:,:,:)
1790
1791 push_sub_with_profile(paw_xc_gga_process)
1792
1793 if (this%nspin == 4) then
1794 call paw_xc_dij_gga_noncollinear(this, ae, with_comp, dens, g, gp, vr, vs, dij)
1795 pop_sub_with_profile(paw_xc_gga_process)
1796 return
1797 end if
1798
1799 np = this%nr
1800 safe_allocate(a1(1:np, 1:this%nang, 1:this%nspin))
1801 safe_allocate(a2(1:np, 1:this%nang, 1:this%nspin))
1802 safe_allocate(a3(1:np, 1:this%nang, 1:this%nspin))
1803 safe_allocate(a4(1:np, 1:this%nang, 1:this%nspin))
1804
1805 !$omp parallel private(ibase, ip, os, iss)
1806 do is = 1, this%nspin
1807 !$omp do schedule(static)
1808 do ia = 1, this%nang
1809 ibase = (ia - 1)*np
1810 os = min(this%nspin, 3 - is) ! opposite spin (equals is for nspin == 1)
1811 iss = 2*is - 1 ! sigma channel (s, s): 1 = 11, 3 = 22
1812 if (this%nspin == 1) then
1813 do ip = 1, np
1814 a1(ip, ia, 1) = vr(ibase + ip, 1)
1815 a2(ip, ia, 1) = m_two*vs(ibase + ip, 1)*gp(ip, 1, ia, 1)
1816 a3(ip, ia, 1) = m_two*vs(ibase + ip, 1)*gp(ip, 1, ia, 2)
1817 a4(ip, ia, 1) = m_two*vs(ibase + ip, 1)*gp(ip, 1, ia, 3)
1818 end do
1819 else
1820 do ip = 1, np
1821 a1(ip, ia, is) = vr(ibase + ip, is)
1822 a2(ip, ia, is) = m_two*vs(ibase + ip, iss)*gp(ip, is, ia, 1) + vs(ibase + ip, 2)*gp(ip, os, ia, 1)
1823 a3(ip, ia, is) = m_two*vs(ibase + ip, iss)*gp(ip, is, ia, 2) + vs(ibase + ip, 2)*gp(ip, os, ia, 2)
1824 a4(ip, ia, is) = m_two*vs(ibase + ip, iss)*gp(ip, is, ia, 3) + vs(ibase + ip, 2)*gp(ip, os, ia, 3)
1825 end do
1826 end if
1827 end do
1828 end do
1829 !$omp end parallel
1830
1831 call paw_xc_dij_gga_fields(this, ae, with_comp, a1, a2, a3, a4, dij)
1832
1833 safe_deallocate_a(a1)
1834 safe_deallocate_a(a2)
1835 safe_deallocate_a(a3)
1836 safe_deallocate_a(a4)
1837
1838 pop_sub_with_profile(paw_xc_gga_process)
1839 end subroutine paw_xc_gga_process
1840
1841 ! ---------------------------------------------------------
1845 subroutine paw_xc_eval(this, np, nsp, nsig, dens, sigma, exc, vrho, vsigma)
1846 type(paw_sphere_t), intent(in) :: this
1847 integer, intent(in) :: np, nsp, nsig
1848 real(real64), contiguous, intent(in) :: dens(:,:) ! (np, nsp) grid-major
1849 real(real64), contiguous, intent(in) :: sigma(:,:) ! (np, nsig) grid-major
1850 real(real64), contiguous, intent(out) :: exc(:) ! (np)
1851 real(real64), contiguous, intent(out) :: vrho(:,:) ! (np, nsp) grid-major
1852 real(real64), contiguous, intent(out) :: vsigma(:,:) ! (np, nsig) grid-major
1853
1854 integer, parameter :: block_size = 4096
1855 integer :: ib, nblock
1856
1857 push_sub_with_profile(paw_xc_eval)
1858
1859 nblock = (np + block_size - 1)/block_size
1860
1861 !$omp parallel do schedule(dynamic)
1862 do ib = 1, nblock
1863 call paw_xc_eval_block(this, np, (ib - 1)*block_size, min(block_size, np - (ib - 1)*block_size), &
1864 nsp, nsig, dens, sigma, exc, vrho, vsigma)
1865 end do
1866
1867 pop_sub_with_profile(paw_xc_eval)
1868 end subroutine paw_xc_eval
1869
1870 ! ---------------------------------------------------------
1872 subroutine paw_xc_eval_block(this, np, ip0, bs, nsp, nsig, dens, sigma, exc, vrho, vsigma)
1873 type(paw_sphere_t), intent(in) :: this
1874 integer, intent(in) :: np, ip0, bs, nsp, nsig
1875 real(real64), contiguous, intent(in) :: dens(:,:) ! (np, nsp) grid-major
1876 real(real64), contiguous, intent(in) :: sigma(:,:) ! (np, nsig) grid-major
1877 real(real64), contiguous, intent(out) :: exc(:) ! (np)
1878 real(real64), contiguous, intent(out) :: vrho(:,:) ! (np, nsp) grid-major
1879 real(real64), contiguous, intent(out) :: vsigma(:,:) ! (np, nsig) grid-major
1880
1881 integer :: ixc, ip, is
1882 real(real64) :: ntot
1883 real(real64) :: dclamp(nsp, bs), ssig(nsig, bs)
1884 real(real64) :: eb(bs), vb(nsp, bs), vsb(nsig, bs)
1885 real(real64) :: ez(bs), vz(nsp, bs), vsz(nsig, bs)
1886
1887 ! no PUSH_SUB, called inside an OpenMP region
1888
1889 do ip = 1, bs
1890 do is = 1, nsp
1891 dclamp(is, ip) = max(dens(ip0 + ip, is), m_zero)
1892 end do
1893 end do
1894 if (this%xc_is_gga) then
1895 do ip = 1, bs
1896 do is = 1, nsig
1897 ssig(is, ip) = sigma(ip0 + ip, is)
1898 end do
1899 end do
1900 end if
1901
1902 eb = m_zero
1903 vb = m_zero
1904 vsb = m_zero
1905
1906 do ixc = 1, 2
1907 ! Only LDA and GGA reach here: hybrids/meta-GGAs are rejected in paw_onsite_init_xc.
1908 select case (this%xc_functl(ixc)%family)
1909 case (xc_family_none)
1910 cycle
1911 case (xc_family_lda)
1912 call xc_f03_lda_exc_vxc(this%xc_functl(ixc)%conf, int(bs, c_size_t), dclamp, ez, vz)
1913 case (xc_family_gga)
1914 call xc_f03_gga_exc_vxc(this%xc_functl(ixc)%conf, int(bs, c_size_t), dclamp, ssig, &
1915 ez, vz, vsz)
1916 case default
1917 message(1) = "PAW one-center engine: unsupported XC functional family."
1918 call messages_fatal(1)
1919 end select
1920 ! plain loops: this routine runs inside the OpenMP region of paw_xc_eval, where the
1921 ! lalg wrappers may not be called
1922 do ip = 1, bs
1923 eb(ip) = eb(ip) + ez(ip)
1924 do is = 1, nsp
1925 vb(is, ip) = vb(is, ip) + vz(is, ip)
1926 end do
1927 end do
1928 if (this%xc_functl(ixc)%family == xc_family_gga) then
1929 do ip = 1, bs
1930 do is = 1, nsig
1931 vsb(is, ip) = vsb(is, ip) + vsz(is, ip)
1932 end do
1933 end do
1934 end if
1935 end do
1936
1937 ! The energy is evaluated with the clamped density, which does not depend on a spin
1938 ! density where it is negative: the corresponding vrho is zeroed for D to stay the
1939 ! derivative of the energy.
1940 ! The points with a negligible total density are then dropped altogether. This
1941 ! hard cutoff makes E_xc discontinuous in rho_ij, but only at a density where the energy
1942 ! density is itself negligible. The results are finally transposed back.
1943 do ip = 1, bs
1944 ntot = m_zero
1945 do is = 1, nsp
1946 if (dens(ip0 + ip, is) < m_zero) vb(is, ip) = m_zero
1947 ntot = ntot + dclamp(is, ip)
1948 end do
1949 if (ntot < m_epsilon) then
1950 eb(ip) = m_zero
1951 vb(:, ip) = m_zero
1952 vsb(:, ip) = m_zero
1953 end if
1954 exc(ip0 + ip) = eb(ip)
1955 do is = 1, nsp
1956 vrho(ip0 + ip, is) = vb(is, ip)
1957 end do
1958 do is = 1, nsig
1959 vsigma(ip0 + ip, is) = vsb(is, ip)
1960 end do
1961 end do
1962
1963 end subroutine paw_xc_eval_block
1964
1965 ! ---------------------------------------------------------
1973 subroutine paw_xc_sph_potential(this, nsp, nsig, n0, sigma0, dn0, exc0, vsph)
1974 type(paw_sphere_t), intent(in) :: this
1975 integer, intent(in) :: nsp, nsig
1976 real(real64), contiguous, intent(in) :: n0(:,:) ! (np, nsp) spherical density
1977 real(real64), contiguous, intent(in) :: sigma0(:,:) ! (np, nsig) |dn0/dr|^2 channels
1978 real(real64), contiguous, intent(in) :: dn0(:,:) ! (np, nsp) radial gradient
1979 real(real64), contiguous, intent(out) :: exc0(:) ! (np) energy per particle
1980 real(real64), contiguous, intent(out) :: vsph(:,:) ! (np, nsp) potential
1981
1982 integer :: ip, is, os, iss
1983 real(real64), allocatable :: vs(:,:), flux(:), div(:)
1984
1985 push_sub(paw_xc_sph_potential)
1986
1987 safe_allocate(vs(1:this%nr, 1:nsig))
1988 call paw_xc_eval(this, this%nr, nsp, nsig, n0, sigma0, exc0, vsph, vs)
1989
1990 if (this%xc_is_gga) then
1991 safe_allocate(flux(1:this%nr))
1992 safe_allocate(div(1:this%nr))
1993 do is = 1, nsp
1994 os = min(nsp, 3 - is)
1995 iss = 2*is - 1
1996 if (nsp == 2) then
1997 do ip = 1, this%nr
1998 flux(ip) = (m_two*vs(ip, iss)*dn0(ip, is) + vs(ip, 2)*dn0(ip, os))*this%vwr(ip)
1999 end do
2000 else
2001 do ip = 1, this%nr
2002 flux(ip) = m_two*vs(ip, iss)*dn0(ip, is)*this%vwr(ip)
2003 end do
2004 end if
2005 call paw_adjoint_divergence_add(this, flux, div, vsph(:, is))
2006 end do
2007 safe_deallocate_a(flux)
2008 safe_deallocate_a(div)
2009 end if
2010
2011 safe_deallocate_a(vs)
2012
2013 pop_sub(paw_xc_sph_potential)
2014 end subroutine paw_xc_sph_potential
2015
2016 ! ---------------------------------------------------------
2025 subroutine paw_xc_kernels(this, nsp, nsig, n0, sigma0, dn0, kk, k2)
2026 type(paw_sphere_t), intent(in) :: this
2027 integer, intent(in) :: nsp, nsig
2028 real(real64), contiguous, intent(in) :: n0(:,:) ! (np, nsp) spherical density
2029 real(real64), contiguous, intent(in) :: sigma0(:,:) ! (np, nsig) |dn0/dr|^2 channels
2030 real(real64), contiguous, intent(in) :: dn0(:,:) ! (np, nsp) radial gradient
2031 real(real64), contiguous, intent(out) :: kk(:,:,:) ! (np, nsp, nsp) K_st
2032 real(real64), contiguous, intent(out) :: k2(:,:,:,:) ! (np, nsp, nsp, nsp) K2_stu
2033
2034 integer :: np, ip, is, it, iu, os, ixc, nf2, nrs, nk3, idx
2035 real(real64) :: ntot
2036 real(real64), allocatable :: dclamp(:,:), ssig(:,:)
2037 real(real64), allocatable :: v2rho2(:,:), v2rhosigma(:,:), v2sigma2(:,:)
2038 real(real64), allocatable :: s2rho2(:,:), s2rhosigma(:,:)
2039 real(real64), allocatable :: v3rho3(:,:), v3rho2s(:,:), v3rhos2(:,:), v3s3(:,:)
2040 real(real64), allocatable :: s3rho3(:,:)
2041 real(real64), allocatable :: flux(:), div(:)
2043 integer, parameter :: N_V3RHO2SIGMA(2) = [1, 9], n_v3rhosigma2(2) = [1, 12], n_v3sigma3(2) = [1, 10]
2044
2046
2047 ! The array sizes and component indices below follow the LibXC layout of the spin
2048 ! components (density: u, d; sigma: uu, ud, dd), which the checks here pin down:
2049 ! v2rho2: (uu, ud, dd); v2rhosigma: (rho t, sigma c) -> (t-1)*3 + c; v2sigma2: 6 pairs;
2050 ! v3rho3: (uuu, uud, udd, ddd); v3rho2sigma: 9; v3rhosigma2: 12; v3sigma3: 10.
2051 assert(nsp == 1 .or. nsp == 2)
2052 assert(nsig == 2*nsp - 1)
2053 np = this%nr
2054 nf2 = 2*nsp - 1 ! v2rho2 components: 1 (n n) or 3 (uu, ud, dd)
2055 nrs = nsp*(2*nsp - 1) ! v2rhosigma and v2sigma2 components: 1 or 6
2056 ! v3rho3 components: 1 unpolarized, 4 (uuu, uud, udd, ddd) polarized
2057 if (nsp == 1) then
2058 nk3 = 1
2059 else
2060 nk3 = 4
2061 end if
2062 assert(nf2 == nsig .and. nrs == nsp*nsig)
2063
2064 safe_allocate(dclamp(1:nsp, 1:np))
2065 do ip = 1, np
2066 do is = 1, nsp
2067 dclamp(is, ip) = max(n0(ip, is), m_zero)
2068 end do
2069 end do
2070 if (this%xc_is_gga) then
2071 safe_allocate(ssig(1:nsig, 1:np))
2072 do ip = 1, np
2073 do is = 1, nsig
2074 ssig(is, ip) = sigma0(ip, is)
2075 end do
2076 end do
2077 end if
2078
2079 safe_allocate(v2rho2(1:nf2, 1:np))
2080 safe_allocate(s2rho2(1:nf2, 1:np))
2081 safe_allocate(v3rho3(1:nk3, 1:np))
2082 safe_allocate(s3rho3(1:nk3, 1:np))
2083 s2rho2 = m_zero
2084 s3rho3 = m_zero
2085 if (this%xc_is_gga) then
2086 safe_allocate(v2rhosigma(1:nrs, 1:np))
2087 safe_allocate(v2sigma2(1:nrs, 1:np))
2088 safe_allocate(s2rhosigma(1:nrs, 1:np))
2089 ! output-only scratch for the kxc call, with the LibXC component counts of
2090 ! v3rho2sigma, v3rhosigma2 and v3sigma3 (unpolarized 1, 1, 1; polarized 9, 12, 10);
2091 ! only v3rho3 is read back
2092 safe_allocate(v3rho2s(1:n_v3rho2sigma(nsp), 1:np))
2093 safe_allocate(v3rhos2(1:n_v3rhosigma2(nsp), 1:np))
2094 safe_allocate(v3s3(1:n_v3sigma3(nsp), 1:np))
2095 s2rhosigma = m_zero
2096 end if
2098 do ixc = 1, 2
2099 select case (this%xc_functl(ixc)%family)
2100 case (xc_family_none)
2101 cycle
2102 case (xc_family_lda)
2103 call xc_f03_lda_fxc(this%xc_functl(ixc)%conf, int(np, c_size_t), dclamp, v2rho2)
2104 s2rho2 = s2rho2 + v2rho2
2105 call xc_f03_lda_kxc(this%xc_functl(ixc)%conf, int(np, c_size_t), dclamp, v3rho3)
2106 s3rho3 = s3rho3 + v3rho3
2107 case (xc_family_gga)
2108 call xc_f03_gga_fxc(this%xc_functl(ixc)%conf, int(np, c_size_t), dclamp, ssig, &
2109 v2rho2, v2rhosigma, v2sigma2)
2110 s2rho2 = s2rho2 + v2rho2
2111 s2rhosigma = s2rhosigma + v2rhosigma
2112 call xc_f03_gga_kxc(this%xc_functl(ixc)%conf, int(np, c_size_t), dclamp, ssig, &
2113 v3rho3, v3rho2s, v3rhos2, v3s3)
2114 s3rho3 = s3rho3 + v3rho3
2115 case default
2116 message(1) = "PAW one-center engine: unsupported XC functional family."
2117 call messages_fatal(1)
2118 end select
2119 end do
2120
2121 ! K_st: local density-density kernel (v2rho2 components 1 / (uu, ud, dd))
2122 do it = 1, nsp
2123 do is = 1, nsp
2124 idx = 1
2125 if (nsp == 2) idx = is + it - 1 ! (1,1) -> 1, cross -> 2, (2,2) -> 3
2126 do ip = 1, np
2127 kk(ip, is, it) = s2rho2(idx, ip)
2128 end do
2129 end do
2130 end do
2131
2132 ! GGA: discrete-adjoint divergence of the flux response at fixed gradient.
2133 ! v2rhosigma components (polarized): (rho t, sigma c) -> (t-1)*3 + c, c in (uu, ud, dd).
2134 if (this%xc_is_gga) then
2135 safe_allocate(flux(1:np))
2136 safe_allocate(div(1:np))
2137 do it = 1, nsp
2138 do is = 1, nsp
2139 os = min(nsp, 3 - is)
2140 do ip = 1, np
2141 if (nsp == 1) then
2142 flux(ip) = m_two*s2rhosigma(1, ip)*dn0(ip, 1)*this%vwr(ip)
2143 else
2144 flux(ip) = (m_two*s2rhosigma((it-1)*3 + 2*is - 1, ip)*dn0(ip, is) &
2145 + s2rhosigma((it-1)*3 + 2, ip)*dn0(ip, os))*this%vwr(ip)
2146 end if
2147 end do
2148 call paw_adjoint_divergence_add(this, flux, div, kk(:, is, it))
2149 end do
2150 end do
2151 safe_deallocate_a(flux)
2152 safe_deallocate_a(div)
2153 ! symmetrize: K is a second derivative of the energy; the two flux forms of the
2154 ! cross kernel differ only by discretization
2155 if (nsp == 2) then
2156 do ip = 1, np
2157 kk(ip, 1, 2) = m_half*(kk(ip, 1, 2) + kk(ip, 2, 1))
2158 kk(ip, 2, 1) = kk(ip, 1, 2)
2159 end do
2160 end if
2161 end if
2162
2163 ! K2_stu from v3rho3: 1 component unpolarized; polarized (uuu, uud, udd, ddd) indexed
2164 ! by the number of down-spin derivatives.
2165 do iu = 1, nsp
2166 do it = 1, nsp
2167 do is = 1, nsp
2168 idx = 1
2169 if (nsp == 2) idx = is + it + iu - 2
2170 do ip = 1, np
2171 k2(ip, is, it, iu) = s3rho3(idx, ip)
2172 end do
2173 end do
2174 end do
2175 end do
2176
2177 ! zero the kernels where the clamped density is negligible, consistent with the
2178 ! energy/potential fixup of paw_xc_eval
2179 do ip = 1, np
2180 ntot = m_zero
2181 do is = 1, nsp
2182 ntot = ntot + dclamp(is, ip)
2183 end do
2184 if (ntot < m_epsilon) then
2185 kk(ip, :, :) = m_zero
2186 k2(ip, :, :, :) = m_zero
2187 end if
2188 end do
2189
2190 safe_deallocate_a(dclamp)
2191 safe_deallocate_a(ssig)
2192 safe_deallocate_a(v2rho2)
2193 safe_deallocate_a(s2rho2)
2194 safe_deallocate_a(v3rho3)
2195 safe_deallocate_a(s3rho3)
2196 safe_deallocate_a(v2rhosigma)
2197 safe_deallocate_a(v2sigma2)
2198 safe_deallocate_a(s2rhosigma)
2199 safe_deallocate_a(v3rho2s)
2200 safe_deallocate_a(v3rhos2)
2201 safe_deallocate_a(v3s3)
2202
2203 pop_sub(paw_xc_kernels)
2204 end subroutine paw_xc_kernels
2205
2206 ! ---------------------------------------------------------
2226 subroutine paw_xc_moments(this, nlm_rho, ncore, ae, with_comp, e_xc, dij)
2227 type(paw_sphere_t), intent(in) :: this
2228 real(real64), contiguous, intent(in) :: nlm_rho(:,:,:) ! (np, nlm_rho, nspin)
2229 real(real64), contiguous, intent(in) :: ncore(:) ! (np) core density per spin channel
2230 logical, intent(in) :: ae
2231 logical, intent(in) :: with_comp
2232 real(real64), intent(out) :: e_xc
2233 real(real64), contiguous, intent(inout) :: dij(:,:,:) ! (nlm, nlm, nspin)
2234
2235 logical :: noncol
2236 integer :: np, nsp, nsig, is, it, iu, ic, lm, ip
2237 real(real64) :: sq4pi, acc, dpol, n0c(4), fwp(4, 2)
2238 real(real64), parameter :: wc(4) = [m_one, m_one, m_two, m_two]
2239 real(real64), allocatable :: wrad(:) ! (np) radial scratch for weighted reductions
2240 real(real64), allocatable :: f0(:,:,:) ! (np, 4, 2) frozen-axis weights (noncollinear)
2241 real(real64), allocatable :: n0(:,:), dn0(:,:), sigma0(:,:)
2242 real(real64), allocatable :: exc0(:), v0(:,:)
2243 real(real64), allocatable :: kk(:,:,:) ! (np, nsp, nsp) K_st = dv_s/dn_t
2244 real(real64), allocatable :: k2(:,:,:,:) ! (np, nsp, nsp, nsp) K2_stu
2245 real(real64), allocatable :: mlm(:,:,:) ! (np, nlm_rho, nsp) (projected) moment set
2246 real(real64), allocatable :: msum(:,:,:) ! (np, nsp, nsp) moment sums M_st
2247 real(real64), allocatable :: vxc_lm(:,:,:) ! (np, nlm_rho, nsp)
2248 real(real64), allocatable :: vrot(:,:) ! (np, nlm_rho) rotated slab potential
2249
2250 push_sub_with_profile(paw_xc_moments)
2251
2252 noncol = this%nspin == 4
2253 np = this%nr
2254 nsp = this%spin_channels
2255 nsig = 2*nsp - 1
2256 sq4pi = sqrt(m_four*m_pi)
2257
2258 ! Spherical density (00 moment plus core), its radial gradient and sigma channels.
2259 ! For the noncollinear case the spherical density also defines the frozen local axis;
2260 ! its weights f0 map the 4 slabs onto the two local channels.
2261 safe_allocate(n0(1:np, 1:nsp))
2262 safe_allocate(dn0(1:np, 1:nsp))
2263 safe_allocate(sigma0(1:np, 1:nsig))
2264 if (noncol) then
2265 safe_allocate(f0(1:np, 1:4, 1:2))
2266 do ip = 1, np
2267 do ic = 1, 4
2268 n0c(ic) = nlm_rho(ip, 1, ic)/sq4pi
2269 end do
2270 n0c(1) = n0c(1) + ncore(ip)
2271 n0c(2) = n0c(2) + ncore(ip)
2272 call paw_xc_noncol_weights(n0c(1), n0c(2), n0c(3), n0c(4), dpol, fwp)
2273 f0(ip, 1:4, 1:2) = fwp(1:4, 1:2)
2274 n0(ip, 1) = m_half*(n0c(1) + n0c(2) + dpol)
2275 n0(ip, 2) = m_half*(n0c(1) + n0c(2) - dpol)
2276 end do
2277 else
2278 do is = 1, nsp
2279 do ip = 1, np
2280 n0(ip, is) = nlm_rho(ip, 1, is)/sq4pi + ncore(ip)
2281 end do
2282 end do
2283 end if
2284 do is = 1, nsp
2285 call paw_radial_derivative_fd(this, n0(:, is), dn0(:, is))
2286 end do
2287 do ip = 1, np
2288 sigma0(ip, 1) = dn0(ip, 1)**2
2289 if (nsp == 2) then
2290 sigma0(ip, 2) = dn0(ip, 1)*dn0(ip, 2)
2291 sigma0(ip, 3) = dn0(ip, 2)**2
2292 end if
2293 end do
2294
2295 ! Base spherical potential and energy
2296 safe_allocate(exc0(1:np))
2297 safe_allocate(v0(1:np, 1:nsp))
2298 call paw_xc_sph_potential(this, nsp, nsig, n0, sigma0, dn0, exc0, v0)
2299
2300 safe_allocate(wrad(1:np))
2301 do ip = 1, np
2302 wrad(ip) = exc0(ip)*this%vwr(ip)
2303 end do
2304 e_xc = m_zero
2305 do is = 1, nsp
2306 e_xc = e_xc + blas_dot(np, n0(1, is), 1, wrad(1), 1)
2307 end do
2308 e_xc = m_four*m_pi*e_xc
2309
2310 ! Analytic kernels K_st = dv_s/dn_t, K2_stu = d2v_s/dn_t dn_u at fixed radial gradient
2311 ! (the fixed-gradient derivative of Eq. (57)), from the LibXC fxc/kxc
2312 safe_allocate(kk(1:np, 1:nsp, 1:nsp))
2313 safe_allocate(k2(1:np, 1:nsp, 1:nsp, 1:nsp))
2314 call paw_xc_kernels(this, nsp, nsig, n0, sigma0, dn0, kk, k2)
2315
2316 ! The moment set the expansion runs on: the L /= 00 moments projected on the frozen
2317 ! local axis for the noncollinear case, the moments themselves otherwise.
2318 safe_allocate(mlm(1:np, 1:this%nlm_rho, 1:nsp))
2319 if (noncol) then
2320 mlm = m_zero
2321 do is = 1, nsp
2322 do lm = 2, this%nlm_rho
2323 do ip = 1, np
2324 mlm(ip, lm, is) = wc(1)*f0(ip, 1, is)*nlm_rho(ip, lm, 1) &
2325 + wc(2)*f0(ip, 2, is)*nlm_rho(ip, lm, 2) &
2326 + wc(3)*f0(ip, 3, is)*nlm_rho(ip, lm, 3) &
2327 + wc(4)*f0(ip, 4, is)*nlm_rho(ip, lm, 4)
2328 end do
2329 end do
2330 end do
2331 else
2332 call lalg_copy(np, this%nlm_rho, nsp, nlm_rho, mlm)
2333 end if
2334
2335 ! Moment sums M_st(r) = sum_{LM /= 00} M_LM,s M_LM,t (Y_LM orthonormality has already
2336 ! collapsed the angular integrals) and their second-order energy contribution
2337 ! 1/2 sum_st int K_st(r) M_st(r) r^2 dr, accumulated in the same spin sweep
2338 safe_allocate(msum(1:np, 1:nsp, 1:nsp))
2339 msum = m_zero
2340 acc = m_zero
2341 do it = 1, nsp
2342 do is = 1, nsp
2343 do lm = 2, this%nlm_rho
2344 do ip = 1, np
2345 msum(ip, is, it) = msum(ip, is, it) + mlm(ip, lm, is)*mlm(ip, lm, it)
2346 end do
2347 end do
2348 do ip = 1, np
2349 acc = acc + kk(ip, is, it)*msum(ip, is, it)*this%vwr(ip)
2350 end do
2351 end do
2352 end do
2353 e_xc = e_xc + m_half*acc
2354
2355 ! Potential moments (Eqs. (60)-(61) without term 2) and the D matrix
2356 safe_allocate(vxc_lm(1:np, 1:this%nlm_rho, 1:nsp))
2357 do is = 1, nsp
2358 do ip = 1, np
2359 acc = m_zero
2360 do it = 1, nsp
2361 do iu = 1, nsp
2362 acc = acc + k2(ip, is, it, iu)*msum(ip, it, iu)
2363 end do
2364 end do
2365 vxc_lm(ip, 1, is) = sq4pi*v0(ip, is) + acc/(m_two*sq4pi)
2366 end do
2367 do lm = 2, this%nlm_rho
2368 do ip = 1, np
2369 acc = m_zero
2370 do it = 1, nsp
2371 acc = acc + kk(ip, is, it)*mlm(ip, lm, it)
2372 end do
2373 vxc_lm(ip, lm, is) = acc
2374 end do
2375 end do
2376 end do
2377
2378 ! Accumulate D; for the noncollinear case rotate the projected potential moments back
2379 ! to the 4 components with the frozen-axis weights first.
2380 if (noncol) then
2381 safe_allocate(vrot(1:np, 1:this%nlm_rho))
2382 do ic = 1, 4
2383 do lm = 1, this%nlm_rho
2384 do ip = 1, np
2385 vrot(ip, lm) = f0(ip, ic, 1)*vxc_lm(ip, lm, 1) + f0(ip, ic, 2)*vxc_lm(ip, lm, 2)
2386 end do
2387 end do
2388 call paw_accumulate_dij(this, ae, with_comp, vrot, dij(:,:,ic))
2389 end do
2390 else
2391 do is = 1, nsp
2392 call paw_accumulate_dij(this, ae, with_comp, vxc_lm(:,:,is), dij(:,:,is))
2393 end do
2394 end if
2395
2396 safe_deallocate_a(wrad)
2397 safe_deallocate_a(f0)
2398 safe_deallocate_a(n0)
2399 safe_deallocate_a(dn0)
2400 safe_deallocate_a(sigma0)
2401 safe_deallocate_a(exc0)
2402 safe_deallocate_a(v0)
2403 safe_deallocate_a(kk)
2404 safe_deallocate_a(k2)
2405 safe_deallocate_a(mlm)
2406 safe_deallocate_a(msum)
2407 safe_deallocate_a(vxc_lm)
2408 safe_deallocate_a(vrot)
2409
2410 pop_sub_with_profile(paw_xc_moments)
2411 end subroutine paw_xc_moments
2412
2413 ! ---------------------------------------------------------
2415 pure function paw_xc_noncol_dpol(n1, n2, n3, n4) result(dpol)
2416 real(real64), intent(in) :: n1, n2, n3, n4
2417 real(real64) :: dpol
2418
2419 dpol = sqrt((n1 - n2)**2 + m_four*(n3**2 + n4**2))
2420 if (dpol <= paw_xc_tiny*(n1 + n2)) dpol = m_zero
2421
2422 end function paw_xc_noncol_dpol
2423
2424 ! ---------------------------------------------------------
2426 pure subroutine paw_xc_noncol_weights(n1, n2, n3, n4, dpol, fw)
2427 real(real64), intent(in) :: n1, n2, n3, n4
2428 real(real64), intent(out) :: dpol
2429 real(real64), intent(out) :: fw(:,:)
2430
2431 real(real64) :: pp
2432
2433 dpol = sqrt((n1 - n2)**2 + m_four*(n3**2 + n4**2))
2434 if (dpol > paw_xc_tiny*(n1 + n2)) then
2435 pp = (n1 - n2)/dpol
2436 fw(1, 1) = m_half*(m_one + pp)
2437 fw(1, 2) = m_half*(m_one - pp)
2438 fw(2, 1) = m_half*(m_one - pp)
2439 fw(2, 2) = m_half*(m_one + pp)
2440 fw(3, 1) = n3/dpol
2441 fw(3, 2) = -n3/dpol
2442 fw(4, 1) = n4/dpol
2443 fw(4, 2) = -n4/dpol
2444 else
2445 dpol = m_zero
2446 fw(1, 1:2) = m_half
2447 fw(2, 1:2) = m_half
2448 fw(3, 1:2) = m_zero
2449 fw(4, 1:2) = m_zero
2450 end if
2451
2452 end subroutine paw_xc_noncol_weights
2453
2454 ! ---------------------------------------------------------
2463 subroutine paw_xc_dij_gga_noncollinear(this, ae, with_comp, dens, g, gp, vr, vs, dij)
2464 type(paw_sphere_t), intent(in) :: this
2465 logical, intent(in) :: ae
2466 logical, intent(in) :: with_comp
2467 real(real64), contiguous, intent(in) :: dens(:,:)
2468 real(real64), contiguous, intent(in) :: g(:,:,:,:), gp(:,:,:,:)
2469 real(real64), contiguous, intent(in) :: vr(:,:), vs(:,:)
2470 real(real64), contiguous, intent(inout) :: dij(:,:,:)
2471
2472 integer :: np, ibase, ip, ia, ic, k
2473 real(real64) :: dpol, uu, gradp
2474 real(real64) :: fwp(4, 2), gq(3), hh(3, 4), bs(3, 2), bd(3)
2475 real(real64), allocatable :: a1(:,:,:), a2(:,:,:), a3(:,:,:), a4(:,:,:)
2476
2477 push_sub_with_profile(paw_xc_dij_gga_noncollinear)
2478
2479 np = this%nr
2480
2481 safe_allocate(a1(1:np, 1:this%nang, 1:4))
2482 safe_allocate(a2(1:np, 1:this%nang, 1:4))
2483 safe_allocate(a3(1:np, 1:this%nang, 1:4))
2484 safe_allocate(a4(1:np, 1:this%nang, 1:4))
2485
2486 !$omp parallel do schedule(static) private(ibase, ip, dpol, uu, gradp, fwp, gq, hh, bs, bd, ic, k)
2487 do ia = 1, this%nang
2488 ibase = (ia - 1)*np
2489 do ip = 1, np
2490 call paw_xc_noncol_weights(dens(ibase + ip, 1), dens(ibase + ip, 2), &
2491 dens(ibase + ip, 3), dens(ibase + ip, 4), dpol, fwp)
2492 ! projected-channel flux B_s and its channel difference
2493 do k = 1, 3
2494 bs(k, 1) = m_two*vs(ibase + ip, 1)*gp(ip, 1, ia, k) + vs(ibase + ip, 2)*gp(ip, 2, ia, k)
2495 bs(k, 2) = m_two*vs(ibase + ip, 3)*gp(ip, 2, ia, k) + vs(ibase + ip, 2)*gp(ip, 1, ia, k)
2496 bd(k) = bs(k, 1) - bs(k, 2)
2497 end do
2498 ! weight gradients h_c = grad F_c^+ per component (dpol is zeroed by the weights helper
2499 ! below the polarization threshold)
2500 hh = m_zero
2501 if (dpol > m_zero) then
2502 uu = dens(ibase + ip, 1) - dens(ibase + ip, 2)
2503 do k = 1, 3
2504 gq(k) = (uu*(g(ip, 1, ia, k) - g(ip, 2, ia, k)) &
2505 + m_four*(dens(ibase + ip, 3)*g(ip, 3, ia, k) &
2506 + dens(ibase + ip, 4)*g(ip, 4, ia, k)))/dpol
2507 gradp = ((g(ip, 1, ia, k) - g(ip, 2, ia, k)) - (uu/dpol)*gq(k))/dpol
2508 hh(k, 1) = m_half*gradp
2509 hh(k, 2) = -m_half*gradp
2510 hh(k, 3) = (g(ip, 3, ia, k) - dens(ibase + ip, 3)*gq(k)/dpol)/dpol
2511 hh(k, 4) = (g(ip, 4, ia, k) - dens(ibase + ip, 4)*gq(k)/dpol)/dpol
2512 end do
2513 end if
2514 do ic = 1, 4
2515 a1(ip, ia, ic) = fwp(ic, 1)*vr(ibase + ip, 1) + fwp(ic, 2)*vr(ibase + ip, 2) &
2516 + bd(1)*hh(1, ic) + bd(2)*hh(2, ic) + bd(3)*hh(3, ic)
2517 a2(ip, ia, ic) = fwp(ic, 1)*bs(1, 1) + fwp(ic, 2)*bs(1, 2)
2518 a3(ip, ia, ic) = fwp(ic, 1)*bs(2, 1) + fwp(ic, 2)*bs(2, 2)
2519 a4(ip, ia, ic) = fwp(ic, 1)*bs(3, 1) + fwp(ic, 2)*bs(3, 2)
2520 end do
2521 end do
2522 end do
2523
2524 call paw_xc_dij_gga_fields(this, ae, with_comp, a1, a2, a3, a4, dij)
2525
2526 safe_deallocate_a(a1)
2527 safe_deallocate_a(a2)
2528 safe_deallocate_a(a3)
2529 safe_deallocate_a(a4)
2530
2531 pop_sub_with_profile(paw_xc_dij_gga_noncollinear)
2532 end subroutine paw_xc_dij_gga_noncollinear
2533
2534 ! ---------------------------------------------------------
2540 subroutine paw_xc_dij_gga_fields(this, ae, with_comp, a1, a2, a3, a4, dij)
2541 type(paw_sphere_t), intent(in) :: this
2542 logical, intent(in) :: ae
2543 logical, intent(in) :: with_comp
2544 real(real64), contiguous, intent(in) :: a1(:,:,:), a2(:,:,:), a3(:,:,:), a4(:,:,:) ! (np, nang, ncomp)
2545 real(real64), contiguous, intent(inout) :: dij(:,:,:) ! (nlm, nlm, ncomp)
2546
2547 integer :: np, ip, lm, ic
2548 real(real64), allocatable :: t1(:,:), t2(:,:), t3(:,:), t4(:,:) ! (np, nlm_rho) Y_LM projections
2549 real(real64), allocatable :: u1(:,:), u2(:,:) ! (np, nlm_rho) radial integrands
2550 real(real64), allocatable :: w(:,:) ! (npair, nlm_rho)
2551
2552 push_sub_with_profile(paw_xc_dij_gga_fields)
2553
2554 np = this%nr
2555
2556 safe_allocate(t1(1:np, 1:this%nlm_rho))
2557 safe_allocate(t2(1:np, 1:this%nlm_rho))
2558 safe_allocate(t3(1:np, 1:this%nlm_rho))
2559 safe_allocate(t4(1:np, 1:this%nlm_rho))
2560 safe_allocate(u1(1:np, 1:this%nlm_rho))
2561 safe_allocate(u2(1:np, 1:this%nlm_rho))
2562 safe_allocate(w(1:this%npair, 1:this%nlm_rho))
2563
2564 do ic = 1, size(dij, 3)
2565 ! Project onto the Y_LM channels: [np x nlm_rho] = [np x nang] . [nang x nlm_rho]
2566 call lalg_gemm(a1(:, :, ic), this%wylm, t1)
2567 call lalg_gemm(a2(:, :, ic), this%wylm, t2)
2568 call lalg_gemm(a3(:, :, ic), this%wdylm_dth, t3)
2569 call lalg_gemm(a4(:, :, ic), this%wdylm_dph, t4)
2570
2571 ! Radial integrands: the pair density couples to t1 + (t3 + t4)/r, its radial
2572 ! derivative to t2; both carry the radial volume weight.
2573 do lm = 1, this%nlm_rho
2574 do ip = 1, np
2575 u1(ip, lm) = (t1(ip, lm) + (t3(ip, lm) + t4(ip, lm))*this%invr(ip))*this%vwr(ip)
2576 u2(ip, lm) = t2(ip, lm)*this%vwr(ip)
2577 end do
2578 end do
2579
2580 ! Radial reduction against the pair tables (+ compensation shapes per L).
2581 if (ae) then
2582 call lalg_gemm(this%pair_ae, u1, w, transa = 'T')
2583 call lalg_gemm(this%dpair_ae, u2, w, transa = 'T', beta = m_one)
2584 else
2585 call lalg_gemm(this%pair_ps, u1, w, transa = 'T')
2586 call lalg_gemm(this%dpair_ps, u2, w, transa = 'T', beta = m_one)
2587 if (with_comp) then
2588 call paw_qaug_gemv(this, this%qaug, 'T', u1, w)
2589 call paw_qaug_gemv(this, this%dqaug, 'T', u2, w)
2590 end if
2591 end if
2592
2593 ! Scatter through the packed Gaunt couplings into the symmetric D matrix.
2594 call paw_gaunt_scatter_dij(this, w, dij(:, :, ic))
2595 end do
2596
2597 safe_deallocate_a(t1)
2598 safe_deallocate_a(t2)
2599 safe_deallocate_a(t3)
2600 safe_deallocate_a(t4)
2601 safe_deallocate_a(u1)
2602 safe_deallocate_a(u2)
2603 safe_deallocate_a(w)
2604
2605 pop_sub_with_profile(paw_xc_dij_gga_fields)
2606 end subroutine paw_xc_dij_gga_fields
2607
2608 ! ---------------------------------------------------------
2615 subroutine paw_accumulate_dij(this, ae, with_comp, vlm, dij)
2616 type(paw_sphere_t), intent(in) :: this
2617 logical, intent(in) :: ae
2618 logical, intent(in) :: with_comp
2619 real(real64), contiguous, intent(in) :: vlm(:,:)
2620 real(real64), contiguous, intent(inout) :: dij(:,:)
2621
2622 integer :: lm, ip
2623 real(real64), allocatable :: vw(:,:), w(:,:)
2624
2625 push_sub_with_profile(paw_accumulate_dij)
2626
2627 ! Radial reduction first: w(pair, LM) = int g_pair(r) v_LM(r) r^2 dr with one matrix
2628 ! product over the pair tables (+ the qaug compensation per L), then scatter through
2629 ! the packed Gaunt couplings: D_ij += sum c(i j; LM) w(pair_ij, LM).
2630 safe_allocate(vw(1:this%nr, 1:this%nlm_rho))
2631 do lm = 1, this%nlm_rho
2632 do ip = 1, this%nr
2633 vw(ip, lm) = vlm(ip, lm)*this%vwr(ip)
2634 end do
2635 end do
2636
2637 safe_allocate(w(1:this%npair, 1:this%nlm_rho))
2638 if (ae) then
2639 call lalg_gemm(this%pair_ae, vw, w, transa = 'T')
2640 else
2641 call lalg_gemm(this%pair_ps, vw, w, transa = 'T')
2642 if (with_comp) call paw_qaug_gemv(this, this%qaug, 'T', vw, w)
2643 end if
2644
2645 ! Scatter through the packed Gaunt couplings into the symmetric D matrix.
2646 call paw_gaunt_scatter_dij(this, w, dij)
2647
2648 safe_deallocate_a(vw)
2649 safe_deallocate_a(w)
2650
2651 pop_sub_with_profile(paw_accumulate_dij)
2652 end subroutine paw_accumulate_dij
2653
2654 ! ---------------------------------------------------------
2656 integer pure function paw_onsite_nlm(this)
2657 type(paw_sphere_t), intent(in) :: this
2658 paw_onsite_nlm = this%nlm
2659 end function paw_onsite_nlm
2660
2661 ! ---------------------------------------------------------
2664 integer pure function paw_onsite_nlm_rho(this)
2665 type(paw_sphere_t), intent(in) :: this
2666 paw_onsite_nlm_rho = this%nlm_rho
2667 end function paw_onsite_nlm_rho
2668
2669 ! ---------------------------------------------------------
2671 integer pure function paw_onsite_nspin(this)
2672 type(paw_sphere_t), intent(in) :: this
2673 paw_onsite_nspin = this%nspin
2674 end function paw_onsite_nspin
2675
2676 ! ---------------------------------------------------------
2685 subroutine paw_onsite_pmat_map(this, nprojs, pl, pm, pchannel, map)
2686 type(paw_sphere_t), intent(in) :: this
2687 integer, intent(in) :: nprojs
2688 integer, intent(in) :: pl(:)
2689 integer, intent(in) :: pm(:)
2690 integer, intent(in) :: pchannel(:)
2691 integer, intent(out) :: map(:)
2692
2693 integer :: i, ic, maxl, maxchan, iproj, ri, lm
2694 integer, allocatable :: cnt(:), rad_of(:,:)
2695
2696 push_sub(paw_onsite_pmat_map)
2697
2698 assert(nprojs == this%nlm)
2699
2700 maxl = maxval(this%lrad(1:this%nrad))
2701 maxchan = this%nrad ! upper bound on the number of channels per l
2702 safe_allocate(cnt(0:maxl))
2703 safe_allocate(rad_of(0:maxl, 1:maxchan))
2704 cnt = 0
2705 rad_of = 0
2706 do i = 1, this%nrad
2707 cnt(this%lrad(i)) = cnt(this%lrad(i)) + 1
2708 ic = cnt(this%lrad(i))
2709 ! Fully-relativistic datasets order the projector channels j = l+1/2 first within each
2710 ! pair while the partial waves keep the file order (j = l-1/2 first)
2711 ! remap so the channel index matches the projector enumeration.
2712 if (this%rad_2j(i) > 0 .and. this%lrad(i) > 0) then
2713 if (this%rad_2j(i) == 2*this%lrad(i) - 1) then
2714 ic = (ic - 1)/2*2 + 2
2715 else
2716 assert(this%rad_2j(i) == 2*this%lrad(i) + 1)
2717 ic = (ic - 1)/2*2 + 1
2718 end if
2719 end if
2720 rad_of(this%lrad(i), ic) = i
2721 end do
2722
2723 map = 0
2724 do iproj = 1, nprojs
2725 assert(pl(iproj) >= 0 .and. pl(iproj) <= maxl)
2726 assert(pchannel(iproj) >= 1 .and. pchannel(iproj) <= cnt(pl(iproj)))
2727 ri = rad_of(pl(iproj), pchannel(iproj))
2728 assert(ri > 0)
2729 assert(this%lrad(ri) == pl(iproj))
2730 do lm = 1, this%nlm
2731 if (this%p_rad(lm) == ri .and. this%p_m(lm) == pm(iproj) .and. this%p_l(lm) == pl(iproj)) then
2732 map(iproj) = lm
2733 exit
2734 end if
2735 end do
2736 assert(map(iproj) > 0)
2737 end do
2738
2739 safe_deallocate_a(cnt)
2740 safe_deallocate_a(rad_of)
2741
2742 pop_sub(paw_onsite_pmat_map)
2743 end subroutine paw_onsite_pmat_map
2744
2745 ! ---------------------------------------------------------
2750 subroutine paw_onsite_reference_density(this, occ, rho)
2751 type(paw_sphere_t), intent(in) :: this
2752 real(real64), intent(in) :: occ(:)
2753 real(real64), intent(out) :: rho(:,:,:)
2754
2755 integer :: i, is
2756
2758
2759 rho = m_zero
2760 do is = 1, this%spin_channels
2761 do i = 1, this%nlm
2762 rho(i, i, is) = occ(this%p_rad(i)) / real((2*this%p_l(i) + 1)*this%spin_channels, real64)
2763 end do
2764 end do
2765
2767 end subroutine paw_onsite_reference_density
2768
2769 ! ---------------------------------------------------------
2774 real(real64) function paw_onsite_charge(this, rho, ae) result(charge)
2775 type(paw_sphere_t), intent(in) :: this
2776 real(real64), intent(in) :: rho(:,:,:)
2777 logical, intent(in) :: ae
2778
2779 integer :: nr, is
2780 real(real64), allocatable :: nlm_rho(:,:)
2781 real(real64) :: sqrt4pi
2782
2783 push_sub(paw_onsite_charge)
2784
2785 nr = this%nr
2786 sqrt4pi = sqrt(m_four*m_pi)
2787 safe_allocate(nlm_rho(1:nr, 1:this%nlm_rho))
2788 charge = m_zero
2789 ! only the diagonal spin components carry charge (slabs 3/4 are the transverse magnetization)
2790 do is = 1, this%spin_channels
2791 call paw_build_density(this, rho(:,:,is), ae, .not. ae, nlm_rho)
2792 ! Q = int n d^3r = sqrt(4pi) int n_00(r) r^2 dr (only the L=0 multipole survives the angle)
2793 charge = charge + sqrt4pi*blas_dot(nr, nlm_rho(1, 1), 1, this%vwr(1), 1)
2794 end do
2795 safe_deallocate_a(nlm_rho)
2796
2797 pop_sub(paw_onsite_charge)
2798 end function paw_onsite_charge
2799
2800 ! ---------------------------------------------------------
2808 subroutine paw_onsite_density_diff(this, rho, ncol, spl, ll, mm, rcut)
2809 type(paw_sphere_t), intent(in) :: this
2810 real(real64), intent(in) :: rho(:,:)
2811 integer, intent(out) :: ncol
2812 type(spline_t), allocatable, intent(out) :: spl(:)
2813 integer, allocatable, intent(out) :: ll(:)
2814 integer, allocatable, intent(out) :: mm(:)
2815 real(real64), intent(out) :: rcut
2816
2817 real(real64), allocatable :: nae(:,:), nps(:,:)
2818 real(real64), parameter :: tol = 1.0e-14_real64
2819
2820 push_sub_with_profile(paw_onsite_density_diff)
2821
2822 safe_allocate(nae(1:this%nr, 1:this%nlm_rho))
2823 safe_allocate(nps(1:this%nr, 1:this%nlm_rho))
2824 ! all-electron one-center density and pseudo one-center density including nhat
2825 call paw_build_density(this, rho, .true., .false., nae)
2826 call paw_build_density(this, rho, .false., .true., nps)
2827
2828 call lalg_axpy(this%nr, this%nlm_rho, -m_one, nps, nae)
2829 call paw_emit_lm_columns(this, nae, tol, ncol, spl, ll, mm, rcut)
2830
2831 safe_deallocate_a(nae)
2832 safe_deallocate_a(nps)
2833
2834 pop_sub_with_profile(paw_onsite_density_diff)
2835 end subroutine paw_onsite_density_diff
2836
2837 ! ---------------------------------------------------------
2850 subroutine paw_onsite_tau_diff(this, rho, ncol, spl, ll, mm, rcut, tau_integral)
2851 type(paw_sphere_t), intent(in) :: this
2852 real(real64), intent(in) :: rho(:,:)
2853 integer, intent(out) :: ncol
2854 type(spline_t), allocatable, intent(out) :: spl(:)
2855 integer, allocatable, intent(out) :: ll(:)
2856 integer, allocatable, intent(out) :: mm(:)
2857 real(real64), intent(out) :: rcut
2861 real(real64), optional, intent(out) :: tau_integral
2862
2863 integer :: i, j, k, ia, ri, rj, lmi, lmj
2864 real(real64) :: rho_ij, ang_grad, ang_val, wa, cg, cv
2865 real(real64), allocatable :: dphi_ae(:,:), dphi_ps(:,:), dtau_lm(:,:), dtaua(:,:)
2866 real(real64), parameter :: tol = 1.0e-14_real64
2867
2868 push_sub_with_profile(paw_onsite_tau_diff)
2869
2870 safe_allocate(dphi_ae(1:this%nr, 1:this%nrad))
2871 safe_allocate(dphi_ps(1:this%nr, 1:this%nrad))
2872 call paw_partial_wave_derivatives(this, dphi_ae, dphi_ps)
2873
2874 ! Delta tau on the radial x angular grid, times the angular weight: dtaua(k, ia) = w(ia) *
2875 ! sum_ij rho_ij [ dphi_i dphi_j Y_i Y_j + (phi_i phi_j / r^2)(dth Y_i dth Y_j + dph Y_i dph Y_j) ].
2876 ! The radial grid index k is the innermost loop so the (nr-leading) partial-wave and dtaua
2877 ! arrays are traversed contiguously; the angular factors are hoisted out of the k loop.
2878 safe_allocate(dtaua(1:this%nr, 1:this%nang))
2879 dtaua(1:this%nr, 1:this%nang) = m_zero
2880 do ia = 1, this%nang
2881 wa = this%wang(ia)
2882 do i = 1, this%nlm
2883 ri = this%p_rad(i)
2884 lmi = this%p_l(i)*(this%p_l(i)+1) + this%p_m(i) + 1
2885 do j = 1, this%nlm
2886 rho_ij = rho(i, j)
2887 if (abs(rho_ij) <= tol) cycle
2888 rj = this%p_rad(j)
2889 lmj = this%p_l(j)*(this%p_l(j)+1) + this%p_m(j) + 1
2890 ang_grad = this%ylm(ia, lmi)*this%ylm(ia, lmj)
2891 ang_val = this%dylm_dth(ia, lmi)*this%dylm_dth(ia, lmj) &
2892 + this%dylm_dph(ia, lmi)*this%dylm_dph(ia, lmj)
2893 cg = rho_ij*wa*ang_grad
2894 cv = rho_ij*wa*ang_val
2895 ! all-electron minus pseudo kinetic-energy density at (r_k, Omega_ia)
2896 do k = 1, this%nr
2897 dtaua(k, ia) = dtaua(k, ia) &
2898 + cg*(dphi_ae(k, ri)*dphi_ae(k, rj) - dphi_ps(k, ri)*dphi_ps(k, rj)) &
2899 + cv*this%invr(k)**2*(this%phi_ae(k, ri)*this%phi_ae(k, rj) - this%phi_ps(k, ri)*this%phi_ps(k, rj))
2900 end do
2901 end do
2902 end do
2903 end do
2904
2905 ! Project onto the real harmonics: Delta tau_LM(r) = int Delta tau Y_LM dOmega, i.e. the
2906 ! matrix product dtau_lm(nr, nlm_rho) = dtaua(nr, nang) . ylm(nang, nlm_rho).
2907 safe_allocate(dtau_lm(1:this%nr, 1:this%nlm_rho))
2908 call lalg_gemm(this%nr, this%nlm_rho, this%nang, m_one, dtaua, this%ylm, m_zero, dtau_lm)
2909 safe_deallocate_a(dtaua)
2910
2911 call paw_emit_lm_columns(this, dtau_lm, tol, ncol, spl, ll, mm, rcut)
2912
2913 ! int Delta tau d^3r = sqrt(4pi) int Delta tau_00(r) r^2 dr (only the L=0 multipole survives
2914 ! the angular integration), same convention as paw_onsite_charge. The radial integral is the
2915 ! dot product of the L=0 column with the radial volume weights r^2 dr.
2916 if (present(tau_integral)) then
2917 tau_integral = sqrt(m_four*m_pi)*blas_dot(this%nr, dtau_lm(1, 1), 1, this%vwr(1), 1)
2918 end if
2919
2920 safe_deallocate_a(dphi_ae)
2921 safe_deallocate_a(dphi_ps)
2922 safe_deallocate_a(dtau_lm)
2923
2924 pop_sub_with_profile(paw_onsite_tau_diff)
2925 end subroutine paw_onsite_tau_diff
2926
2927 ! ---------------------------------------------------------
2942 subroutine paw_onsite_current_diff(this, arho, idir, ncol, spl, ll, mm, rcut, current_integral)
2943 type(paw_sphere_t), intent(in) :: this
2944 real(real64), intent(in) :: arho(:,:)
2945 integer, intent(in) :: idir
2946 integer, intent(out) :: ncol
2947 type(spline_t), allocatable, intent(out) :: spl(:)
2948 integer, allocatable, intent(out) :: ll(:)
2949 integer, allocatable, intent(out) :: mm(:)
2950 real(real64), intent(out) :: rcut
2951 real(real64), optional, intent(out) :: current_integral
2952
2953 integer :: i, j, ip, ia, ri, rj, lmi, lmj, ra, rb, rp, npr
2954 real(real64) :: aij, yi
2955 real(real64), allocatable :: dphi_ae(:,:), dphi_ps(:,:), dj_lm(:,:), dja(:,:), rmat(:,:), cmat(:,:)
2956 real(real64), parameter :: tol = 1.0e-14_real64
2957
2958 push_sub_with_profile(paw_onsite_current_diff)
2959
2960 safe_allocate(dphi_ae(1:this%nr, 1:this%nrad))
2961 safe_allocate(dphi_ps(1:this%nr, 1:this%nrad))
2962 call paw_partial_wave_derivatives(this, dphi_ae, dphi_ps)
2963
2964 ! Express the contraction as matrix-matrix products, like paw_xc.
2965 ! The radial factors depend only on (ip, radial-channel-pair), the angular coefficients only on (radial-pair, ia):
2966 ! grad(phi Y) = phi`(r) Y rhat + (phi/r)(dY/dth that + (1/sin th) dY/dph phat), so
2967 ! Delta j^idir(ip,ia) = sum_ij arho_ij (g_i grad_idir g_j)^{AE-PS}
2968 ! = sum_rp [ Rg(ip,rp) Cg(rp,ia) + Rv(ip,rp) Cv(rp,ia) ]
2969 ! with radial-channel pair rp = (ra,rb), the radial-gradient block Rg = phi_a phi_b` and the
2970 ! transverse block Rv = phi_a phi_b / r (all-electron minus pseudo), and angular coefficients
2971 ! Cg(rp,ia) = sum_{(i,j):(ri,rj)=rp} arho_ij w(ia) Y_i (Y_j rhat_idir)
2972 ! Cv(rp,ia) = sum_{(i,j):(ri,rj)=rp} arho_ij w(ia) Y_i (dY_j/dth that + dY_j/dph phat)_idir
2973 ! stacked into rmat(nr, 2 nrad^2) and cmat(2 nrad^2, nang); dja = rmat . cmat.
2974 npr = this%nrad*this%nrad
2975 safe_allocate(rmat(1:this%nr, 1:2*npr))
2976 do ra = 1, this%nrad
2977 do rb = 1, this%nrad
2978 rp = (ra-1)*this%nrad + rb
2979 do ip = 1, this%nr
2980 rmat(ip, rp) = this%phi_ae(ip, ra)*dphi_ae(ip, rb) - this%phi_ps(ip, ra)*dphi_ps(ip, rb)
2981 rmat(ip, npr + rp) = this%invr(ip)*(this%phi_ae(ip, ra)*this%phi_ae(ip, rb) &
2982 - this%phi_ps(ip, ra)*this%phi_ps(ip, rb))
2983 end do
2984 end do
2985 end do
2986
2987 safe_allocate(cmat(1:2*npr, 1:this%nang))
2988 cmat(1:2*npr, 1:this%nang) = m_zero
2989 do i = 1, this%nlm
2990 ri = this%p_rad(i)
2991 lmi = this%p_l(i)*(this%p_l(i)+1) + this%p_m(i) + 1
2992 do j = 1, this%nlm
2993 aij = arho(i, j)
2994 if (abs(aij) <= tol) cycle
2995 rj = this%p_rad(j)
2996 lmj = this%p_l(j)*(this%p_l(j)+1) + this%p_m(j) + 1
2997 rp = (ri-1)*this%nrad + rj
2998 do ia = 1, this%nang
2999 yi = aij*this%wang(ia)*this%ylm(ia, lmi)
3000 cmat(rp, ia) = cmat(rp, ia) + yi*this%ylm(ia, lmj)*this%rhat(ia, idir)
3001 cmat(npr + rp, ia) = cmat(npr + rp, ia) &
3002 + yi*(this%dylm_dth(ia, lmj)*this%that(ia, idir) + this%dylm_dph(ia, lmj)*this%phat(ia, idir))
3003 end do
3004 end do
3005 end do
3006
3007 safe_allocate(dja(1:this%nr, 1:this%nang))
3008 call lalg_gemm(this%nr, this%nang, 2*npr, m_one, rmat, cmat, m_zero, dja)
3009 safe_deallocate_a(rmat)
3010 safe_deallocate_a(cmat)
3011
3012 ! Project onto the real harmonics: Delta j_LM(r) = int Delta j Y_LM dOmega = dja(nr,nang).ylm(nang,nlm_rho)
3013 safe_allocate(dj_lm(1:this%nr, 1:this%nlm_rho))
3014 call lalg_gemm(this%nr, this%nlm_rho, this%nang, m_one, dja, this%ylm, m_zero, dj_lm)
3015 safe_deallocate_a(dja)
3016
3017 call paw_emit_lm_columns(this, dj_lm, tol, ncol, spl, ll, mm, rcut)
3018
3019 if (present(current_integral)) then
3020 current_integral = sqrt(m_four*m_pi)*blas_dot(this%nr, dj_lm(1, 1), 1, this%vwr(1), 1)
3021 end if
3022
3023 safe_deallocate_a(dphi_ae)
3024 safe_deallocate_a(dphi_ps)
3025 safe_deallocate_a(dj_lm)
3026
3027 pop_sub_with_profile(paw_onsite_current_diff)
3028 end subroutine paw_onsite_current_diff
3029
3030 ! ---------------------------------------------------------
3036 subroutine paw_onsite_get_velocity_matrix(this, vmat)
3037 type(paw_sphere_t), intent(in) :: this
3038 real(real64), allocatable, intent(out) :: vmat(:,:,:)
3039
3040 integer :: i, j, ip, ia, ri, rj, lmi, lmj, idir
3041 real(real64) :: yi, aa, bb, c1, c2, wa, acc
3042 real(real64), allocatable :: dphi_ae(:,:), dphi_ps(:,:)
3043
3045
3046 safe_allocate(dphi_ae(1:this%nr, 1:this%nrad))
3047 safe_allocate(dphi_ps(1:this%nr, 1:this%nrad))
3048 do i = 1, this%nrad
3049 call paw_radial_derivative(this%r, this%phi_ae(:, i), dphi_ae(:, i))
3050 call paw_radial_derivative(this%r, this%phi_ps(:, i), dphi_ps(:, i))
3051 end do
3052
3053 ! Same integrand and factorization as paw_onsite_current_diff, integrated radially with the
3054 ! r^2 dr weight vwr
3055 safe_allocate(vmat(1:this%nlm, 1:this%nlm, 1:3))
3056 vmat = m_zero
3057 do idir = 1, 3
3058 do ia = 1, this%nang
3059 wa = this%wang(ia)
3060 do i = 1, this%nlm
3061 ri = this%p_rad(i)
3062 lmi = this%p_l(i)*(this%p_l(i)+1) + this%p_m(i) + 1
3063 yi = this%ylm(ia, lmi)
3064 do j = 1, this%nlm
3065 rj = this%p_rad(j)
3066 lmj = this%p_l(j)*(this%p_l(j)+1) + this%p_m(j) + 1
3067 aa = this%ylm(ia, lmj)*this%rhat(ia, idir)
3068 bb = this%dylm_dth(ia, lmj)*this%that(ia, idir) + this%dylm_dph(ia, lmj)*this%phat(ia, idir)
3069 c1 = yi*aa
3070 c2 = yi*bb
3071 acc = m_zero
3072 do ip = 1, this%nr
3073 acc = acc + this%vwr(ip)*( &
3074 c1*(this%phi_ae(ip, ri)*dphi_ae(ip, rj) - this%phi_ps(ip, ri)*dphi_ps(ip, rj)) &
3075 + c2*this%invr(ip)*(this%phi_ae(ip, ri)*this%phi_ae(ip, rj) - this%phi_ps(ip, ri)*this%phi_ps(ip, rj)))
3076 end do
3077 vmat(i, j, idir) = vmat(i, j, idir) + wa*acc
3078 end do
3079 end do
3080 end do
3081 end do
3082
3083 safe_deallocate_a(dphi_ae)
3084 safe_deallocate_a(dphi_ps)
3085
3087 end subroutine paw_onsite_get_velocity_matrix
3088
3089 ! ---------------------------------------------------------
3099 subroutine paw_onsite_partial_wave_diff(this, nprojs, pl, pm, pchannel, spl, ll, mm, rcut)
3100 type(paw_sphere_t), intent(in) :: this
3101 integer, intent(in) :: nprojs
3102 integer, intent(in) :: pl(:)
3103 integer, intent(in) :: pm(:)
3104 integer, intent(in) :: pchannel(:)
3105 type(spline_t), allocatable, intent(out) :: spl(:)
3106 integer, allocatable, intent(out) :: ll(:)
3107 integer, allocatable, intent(out) :: mm(:)
3108 real(real64), intent(out) :: rcut
3109
3110 integer :: i, ri
3111 integer, allocatable :: map(:)
3112 real(real64), allocatable :: diff(:)
3113
3115
3116 safe_allocate(map(1:nprojs))
3117 call paw_onsite_pmat_map(this, nprojs, pl, pm, pchannel, map)
3118
3119 safe_allocate(spl(1:nprojs))
3120 safe_allocate(ll(1:nprojs))
3121 safe_allocate(mm(1:nprojs))
3122 safe_allocate(diff(1:this%nr))
3123 do i = 1, nprojs
3124 ri = this%p_rad(map(i)) ! radial partial wave paired with projector i
3125 diff(1:this%nr) = this%phi_ae(1:this%nr, ri) - this%phi_ps(1:this%nr, ri)
3126 ll(i) = pl(i)
3127 mm(i) = pm(i)
3128 call spline_init(spl(i))
3129 call spline_fit(this%nr, this%r, diff, spl(i))
3130 end do
3131 rcut = this%r(this%nr)
3132
3133 safe_deallocate_a(map)
3134 safe_deallocate_a(diff)
3135
3137 end subroutine paw_onsite_partial_wave_diff
3138
3139 ! ---------------------------------------------------------
3147 subroutine paw_onsite_get_kinetic_data(this, deltaK, p_rad, p_l, p_m)
3148 type(paw_sphere_t), intent(in) :: this
3149 real(real64), allocatable, intent(out) :: deltak(:,:)
3150 integer, allocatable, intent(out) :: p_rad(:)
3151 integer, allocatable, intent(out) :: p_l(:)
3152 integer, allocatable, intent(out) :: p_m(:)
3153
3155
3156 safe_allocate(deltak(1:this%nrad, 1:this%nrad))
3157 deltak(1:this%nrad, 1:this%nrad) = this%kin_ae(1:this%nrad, 1:this%nrad) - this%kin_ps(1:this%nrad, 1:this%nrad)
3158 safe_allocate(p_rad(1:this%nlm))
3159 p_rad(1:this%nlm) = this%p_rad(1:this%nlm)
3160 safe_allocate(p_l(1:this%nlm))
3161 p_l(1:this%nlm) = this%p_l(1:this%nlm)
3162 safe_allocate(p_m(1:this%nlm))
3163 p_m(1:this%nlm) = this%p_m(1:this%nlm)
3164
3166 end subroutine paw_onsite_get_kinetic_data
3167
3168 ! ---------------------------------------------------------
3169 subroutine paw_onsite_end(this)
3170 type(paw_sphere_t), intent(inout) :: this
3172 push_sub(paw_onsite_end)
3173
3174 call xc_functional_end(this%xc_functl(1))
3175 call xc_functional_end(this%xc_functl(2))
3176
3177 safe_deallocate_a(this%r)
3178 safe_deallocate_a(this%r2)
3179 safe_deallocate_a(this%invr)
3180 safe_deallocate_a(this%wr)
3181 safe_deallocate_a(this%vwr)
3182 safe_deallocate_a(this%der_w)
3183 safe_deallocate_a(this%der_i0)
3184 safe_deallocate_a(this%der_wt)
3185 safe_deallocate_a(this%der_trow0)
3186 safe_deallocate_a(this%der_tn)
3187 safe_deallocate_a(this%lrad)
3188 safe_deallocate_a(this%rad_2j)
3189 safe_deallocate_a(this%phi_ae_small)
3190 safe_deallocate_a(this%phi_ae)
3191 safe_deallocate_a(this%phi_ps)
3192 safe_deallocate_a(this%p_rad)
3193 safe_deallocate_a(this%p_l)
3194 safe_deallocate_a(this%p_m)
3195 safe_deallocate_a(this%qaug)
3196 safe_deallocate_a(this%pair_ae)
3197 safe_deallocate_a(this%pair_ps)
3198 safe_deallocate_a(this%gnt_pair)
3199 safe_deallocate_a(this%gnt_l)
3200 safe_deallocate_a(this%gnt_lm)
3201 safe_deallocate_a(this%gnt_c)
3202 safe_deallocate_a(this%gnt_ptr)
3203 safe_deallocate_a(this%dpair_ae)
3204 safe_deallocate_a(this%dpair_ps)
3205 safe_deallocate_a(this%dqaug)
3206 safe_deallocate_a(this%nc_ae)
3207 safe_deallocate_a(this%nc_ps)
3208 safe_deallocate_a(this%nc_ae_der)
3209 safe_deallocate_a(this%nc_ps_der)
3210 safe_deallocate_a(this%vref_ae)
3211 safe_deallocate_a(this%vref_ps)
3212 safe_deallocate_a(this%kin_ae)
3213 safe_deallocate_a(this%kin_ps)
3214 safe_deallocate_a(this%wang)
3215 safe_deallocate_a(this%ylm)
3216 safe_deallocate_a(this%dylm_dth)
3217 safe_deallocate_a(this%dylm_dph)
3218 safe_deallocate_a(this%wylm)
3219 safe_deallocate_a(this%wdylm_dth)
3220 safe_deallocate_a(this%wdylm_dph)
3221 safe_deallocate_a(this%rhat)
3222 safe_deallocate_a(this%that)
3223 safe_deallocate_a(this%phat)
3224
3225 pop_sub(paw_onsite_end)
3226 end subroutine paw_onsite_end
3227
3228end module paw_onsite_oct_m
3229
3230!! Local Variables:
3231!! mode: f90
3232!! coding: utf-8
3233!! End:
double sin(double __x) __attribute__((__nothrow__
double sqrt(double __x) __attribute__((__nothrow__
double cos(double __x) __attribute__((__nothrow__
This module contains interfaces for BLAS routines You should not use these routines directly....
Definition: blas.F90:97
real(real64), parameter, public m_zero
Definition: global.F90:173
real(real64), parameter, public m_one
Definition: global.F90:174
This module is intended to contain "only mathematical" functions and procedures.
Definition: math.F90:94
subroutine, public messages_not_implemented(feature, namespace)
Definition: messages.F90:1048
character(len=256), dimension(max_lines), public message
to be output by fatal, warning
Definition: messages.F90:138
subroutine, public messages_fatal(no_lines, only_root_writes, namespace)
Definition: messages.F90:386
subroutine, public messages_input_error(namespace, var, details, row, column)
Definition: messages.F90:671
subroutine, public messages_info(no_lines, iunit, debug_only, stress, all_nodes, namespace)
Definition: messages.F90:570
logical function, public parse_is_defined(namespace, name)
Definition: parser.F90:440
PAW radial engine.
Definition: paw_onsite.F90:131
subroutine paw_onsite_init_pairs(this)
Tabulate the species-constant radial pair products phi_i phi_j and pack the nonzero real Gaunt coupli...
Definition: paw_onsite.F90:623
subroutine, public paw_onsite_tau_diff(this, rho, ncol, spl, ll, mm, rcut, tau_integral)
Build the one-center all-electron-minus-pseudo kinetic-energy-density difference for a single colline...
subroutine paw_partial_wave_derivatives(this, dphi_ae, dphi_ps)
Radial derivatives of all (AE, PS) partial waves phi (not u = r phi): grad(phi Y) uses dphi/dr.
subroutine paw_onsite_init_xc(this, ps, namespace)
Initialize the exchange and correlation functionals from the ones stored in the pseudopotential.
Definition: paw_onsite.F90:695
subroutine paw_qaug_gemv(this, qtab, trans, xx, yy)
Contract the packed compensation shapes against per-LM fields, restricted to the Gaunt-live L channel...
real(real64), parameter paw_xc_tiny
Relative polarization threshold of the noncollinear local-frame projection.
Definition: paw_onsite.F90:306
subroutine, public paw_onsite_partial_wave_diff(this, nprojs, pl, pm, pchannel, spl, ll, mm, rcut)
Build the partial-wave difference (phi_i - phitilde_i)(r) for each projector, as radial splines ready...
subroutine, public paw_onsite_end(this)
subroutine paw_gaunt_scatter_dij(this, w, dij)
Scatter the per-(pair, LM) reductions through the packed Gaunt couplings into the symmetric D matrix:...
integer, parameter, public paw_blochl
Bloechl (GPAW/ABINIT); nhat excluded from XC.
Definition: paw_onsite.F90:181
subroutine paw_build_density(this, rho, ae, with_comp, nlm_rho, nlm_der)
Build the density coefficients n_LM(r) from rho_ij ([Torrent08] Eqs. (44)-(47)). On the PS side,...
subroutine paw_onsite_init_derivative(this)
Build the five-point Fornberg first-derivative weights for every radial point (B. Fornberg,...
Definition: paw_onsite.F90:859
integer pure function paw_pair_index(ri, rj)
Packed index of the unordered radial channel pair (ri, rj). The radial pair products and compensation...
Definition: paw_onsite.F90:609
subroutine paw_xc_eval_block(this, np, ip0, bs, nsp, nsig, dens, sigma, exc, vrho, vsigma)
Evaluate the functionals on one contiguous block of bs points.
subroutine paw_radial_derivative_fd(this, f, df)
Radial derivative of a function sampled on the sphere grid: apply the banded Fornberg operator built ...
Definition: paw_onsite.F90:954
subroutine, public paw_onsite_compute(this, rho, e1, e1t, dij, hxc_only)
Given the one-center density matrix rho_ij, compute the one-center energies E1 (AE) and E1t (PS) and ...
integer, parameter paw_xc_scheme_full
exact evaluation on the radial x angular grid
Definition: paw_onsite.F90:184
subroutine, public paw_onsite_current_diff(this, arho, idir, ncol, spl, ll, mm, rcut, current_integral)
One-center all-electron current-density correction Delta j^alpha = j^1 - j~^1.
subroutine paw_xc_kernels(this, nsp, nsig, n0, sigma0, dn0, kk, k2)
Analytic XC kernels of the spherical one-center problem at fixed radial gradient: K_st(r) = d v_s(r) ...
subroutine, public paw_onsite_init(this, ps, namespace, formulation, nspin)
Build the per-species one-center data from a (PAW) pseudopotential.
Definition: paw_onsite.F90:313
integer, parameter, public paw_kresse_joubert
PAW formulation: selects the compensation-charge / reference treatment.
Definition: paw_onsite.F90:180
subroutine paw_xc(this, nlm_rho, nlm_der, ncore, ae, with_comp, e_xc, dij)
Exchange-correlation on the radial x angular grid: LibXC evaluated pointwise, accumulating the XC ene...
subroutine paw_xc_dij_gga_fields(this, ae, with_comp, a1, a2, a3, a4, dij)
Accumulate the D-matrix components from the angular fields of the White-Bird GGA coupling,...
subroutine paw_xc_density(this, nlm_rho, ncore, dens, densp, dpol)
: Prepare the density for the XC calculation with libxc The polarization magnitude dpol (noncollinear...
integer pure function, public paw_onsite_nlm(this)
Number of (l,m)-resolved projectors, i.e. the leading dimension of rho_ij.
subroutine paw_xc_dij_gga_noncollinear(this, ae, with_comp, dens, g, gp, vr, vs, dij)
Noncollinear GGA D-matrix contribution: the exact derivative of the local-frame projected E_xc with r...
pure real(real64) function paw_xc_noncol_dpol(n1, n2, n3, n4)
Noncollinear polarization magnitude dpol = |m| at one point.
integer pure function, public paw_onsite_nlm_rho(this)
Number of real-harmonic columns (lmax_rho+1)^2 of the one-center density expansion; the upper bound o...
subroutine, public paw_onsite_reorder_pmat(nprojs, map, src, dst)
Reorder one spin channel of a projector-ordered matrix.
subroutine, public paw_onsite_pmat_map(this, nprojs, pl, pm, pchannel, map)
Map the projector-matrix enumeration (l, m, channel) used by the mesh nonlocal machinery to the one-c...
subroutine paw_xc_sigma(this, dens, dpol, g, gp, sigma)
Conversion from gradients to sigma, for GGA with libxc.
subroutine, public paw_onsite_get_kinetic_data(this, deltaK, p_rad, p_l, p_m)
Read-only access to the frozen kinetic correction DeltaK = K^AE - K^PS (the radial-channel matrix,...
subroutine paw_radial_derivative(r, f, df)
Radial derivative of a grid-sampled function via the cubic-spline derivative.
Definition: paw_onsite.F90:829
subroutine paw_onsite_kinetic(this, r_full, phi_ae_full, phi_ps_full)
Kinetic matrices K^AE_ij = <phi_i|T|phi_j> and K^PS_ij = <phitilde_i|T|phitilde_j>,...
Definition: paw_onsite.F90:770
real(real64), parameter gaunt_tol
Definition: paw_onsite.F90:303
subroutine paw_xc_lda_process(this, dens, vr)
Rotate the LDA potential from the local spin frame back to the global fram.
subroutine paw_adjoint_divergence_add(this, flux, div, vfield)
Add the discrete-adjoint divergence of a vwr-weighted radial flux to a potential-like field: vfield(i...
subroutine paw_xc_gga_process(this, ae, with_comp, dens, g, gp, vr, vs, dij)
Rotate the GGA part of the potential from the local spin frame back to the global fram.
subroutine, public paw_onsite_reference_density(this, occ, rho)
Build the spherical reference density matrix from per-radial-channel occupations occ(1:nrad): each sh...
subroutine paw_xc_sph_potential(this, nsp, nsig, n0, sigma0, dn0, exc0, vsph)
Energy density and full XC potential of a spherically symmetric density on the radial grid....
subroutine paw_onsite_angular_grid(this)
Angular grid: Gauss-Legendre in cos(theta) x uniform in phi, plus the real spherical harmonics tabula...
subroutine, public paw_onsite_get_velocity_matrix(this, vmat)
Velocity (momentum) matrix of the partial waves, V^alpha_ij = int (g_i grad_alpha g_j) dV,...
subroutine paw_fornberg_weights(z, x, n, w)
First-derivative weights at z from function values at the n points x (Fornberg 1988 recursion,...
Definition: paw_onsite.F90:910
integer, parameter paw_xc_scheme_moments
second-order moment expansion around the spherical density
Definition: paw_onsite.F90:184
subroutine paw_hartree_lm(this, lm, nl, vl)
Radial Hartree potential for one (L,M) channel (classical multipole expansion of 1/|r-r_prime|,...
subroutine paw_xc_moments(this, nlm_rho, ncore, ae, with_comp, e_xc, dij)
One-center XC by the second-order moment expansion around the spherical density ([Torrent08] Sec....
integer pure function, public paw_onsite_nspin(this)
Number of collinear spin channels (1 or 2) the sphere was built for.
subroutine paw_emit_lm_columns(this, field_lm, tol, ncol, spl, ll, mm, rcut)
Fit the nonzero (L, M) columns of a one-center radial field field_lm(np, nlm_rho) into splines ready ...
subroutine paw_accumulate_dij(this, ae, with_comp, vlm, dij)
Accumulate D_ij = sum_LM gaunt(l_i m_i, l_j m_j, L M) int v_LM(r) g_ij^L(r) r^2 dr where g_ij^L is th...
logical function paw_onsite_xc_has_kernels(this)
Whether all the functionals of the dataset provide the second and third derivatives (fxc,...
Definition: paw_onsite.F90:744
subroutine, public paw_onsite_side(this, rho, ae, energy, dij, hxc_only)
One side (AE or PS) of the one-center calculation: build the density in the Y_LM expansion,...
real(real64) function, public paw_onsite_charge(this, rho, ae)
Integrated L=0 charge of the one-center density (spin-summed) for a given rho: the all-electron (ae==...
subroutine paw_xc_eval(this, np, nsp, nsig, dens, sigma, exc, vrho, vsigma)
Functional-specific part of the one-center XC.
pure subroutine paw_xc_noncol_weights(n1, n2, n3, n4, dpol, fw)
Noncollinear local-frame projection weights F_c^s = d n_s / d n_c at one point.
subroutine, public paw_onsite_density_diff(this, rho, ncol, spl, ll, mm, rcut)
Build the one-center all-electron-minus-(pseudo+compensation) density difference for a single colline...
subroutine paw_xc_gradient(this, nlm_rho, nlm_der, ncder, g)
Gradient stage of paw_xc.
subroutine paw_radial_derivative_t(this, f, df)
Apply the transpose of the banded radial-derivative operator: df_j = sum_i D_ij f_i....
Definition: paw_onsite.F90:981
/* To use the profiling module, call profiling_in and profiling_out with the label of the region as a...
Definition: profiling.F90:122
Definition: ps.F90:93
pure logical function, public ps_is_paw(ps)
Definition: ps.F90:1834
pure logical function, public ps_has_nlcc(ps)
Definition: ps.F90:1818
integer, parameter, public pseudo_exchange_unknown
Definition: pseudo.F90:188
integer, parameter, public pseudo_correlation_unknown
Definition: pseudo.F90:192
real(real64) function, public spline_eval(spl, x)
Definition: splines.F90:578
static double f(double w, void *p)
Per-species, geometry-independent one-center data. Built once at setup and immutable during the SCF; ...
Definition: paw_onsite.F90:190
A type storing the information and data about a pseudopotential.
Definition: ps.F90:169
int true(void)