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 :: &
110
112 integer, parameter :: PAW_KRESSE_JOUBERT = 1
113 integer, parameter :: PAW_BLOCHL = 2
114
116 integer, parameter :: &
117 PAW_XC_SCHEME_FULL = 1, & !< exact evaluation on the radial x angular grid
119
122 type paw_sphere_t
123 private
124 integer :: nspin = 1
125 integer :: spin_channels = 1
126
127 ! --- radial grid (native dataset mesh + integration weights), truncated at r_c ---
128 real(real64) :: rc = m_zero
129 integer :: nr = 0
130 real(real64), allocatable :: r(:)
131 real(real64), allocatable :: r2(:)
132 real(real64), allocatable :: invr(:)
135 real(real64), allocatable :: wr(:)
136 real(real64), allocatable :: vwr(:)
137
138 ! --- partial waves (radial channels, file order) ---
139 integer :: nrad = 0
140 integer, allocatable :: lrad(:)
141 real(real64), allocatable :: phi_ae(:,:)
142 real(real64), allocatable :: phi_ps(:,:)
143 integer, allocatable :: rad_2j(:)
144 logical :: has_small = .false.
145 real(real64), allocatable :: phi_ae_small(:,:)
146
147 ! --- (l,m)-resolved projector enumeration ---
148 integer :: nlm = 0
149 integer, allocatable :: p_rad(:)
150 integer, allocatable :: p_l(:)
151 integer, allocatable :: p_m(:)
152
153 ! --- compensation augmentation Qhat_ij^L(r), radial-channel pairs ---
154 integer :: laug = -1
155 real(real64), allocatable :: qaug(:,:,:)
156
157 ! --- precomputed radial pair products and packed Gaunt coupling ---
161 integer :: npair = 0
162 real(real64), allocatable :: pair_ae(:,:)
163 real(real64), allocatable :: pair_ps(:,:)
164 integer :: ngnt = 0
165 integer, allocatable :: gnt_pair(:)
166 integer, allocatable :: gnt_l(:)
167 integer, allocatable :: gnt_lm(:)
168 real(real64), allocatable :: gnt_c(:)
169 integer, allocatable :: gnt_ptr(:)
172
173 ! --- radial derivatives, only for GGA. They are computed once on the full dataset grid
174 ! (no boundary effect at r_c). As the derivative is linear, grad n = sum_ij rho_ij grad g_ij
175 ! holds exactly, so that D stays the exact derivative of the discrete E_xc. ---
176 real(real64), allocatable :: dpair_ae(:,:)
177 real(real64), allocatable :: dpair_ps(:,:)
178 real(real64), allocatable :: dqaug(:,:,:)
179 real(real64), allocatable :: nc_ae_der(:)
180 real(real64), allocatable :: nc_ps_der(:)
181
182 ! --- banded radial first-derivative operator of the moment XC scheme ---
189 integer :: der_nw = 5
190 real(real64), allocatable :: der_w(:,:)
191 integer, allocatable :: der_i0(:)
192 integer :: der_ntw = 0
193 real(real64), allocatable :: der_wt(:,:)
194 integer, allocatable :: der_trow0(:)
195 integer, allocatable :: der_tn(:)
197 ! --- core densities and reference potentials (spherical, radial) ---
198 logical :: has_ae_core = .false.
199 logical :: has_ps_core = .false.
200 real(real64), allocatable :: nc_ae(:)
201 real(real64), allocatable :: nc_ps(:)
202 real(real64), allocatable :: vref_ae(:)
203 real(real64), allocatable :: vref_ps(:)
205 ! --- kinetic matrices (constant over the SCF) ---
206 real(real64), allocatable :: kin_ae(:,:)
207 real(real64), allocatable :: kin_ps(:,:)
209 ! --- angular quadrature (Gauss-Legendre in cos(theta) x uniform in phi) ---
210 integer :: nang = 0
211 integer :: lmax_rho = 0
212 integer :: nlm_rho = 0
213 real(real64), allocatable :: wang(:)
214 real(real64), allocatable :: ylm(:,:)
215 real(real64), allocatable :: dylm_dth(:,:)
216 real(real64), allocatable :: dylm_dph(:,:)
217 real(real64), allocatable :: wylm(:,:)
218 real(real64), allocatable :: wdylm_dth(:,:)
219 real(real64), allocatable :: wdylm_dph(:,:)
223 real(real64), allocatable :: rhat(:,:)
224 real(real64), allocatable :: that(:,:)
225 real(real64), allocatable :: phat(:,:)
233 real(real64), allocatable :: tau_c_ae(:)
234 real(real64), allocatable :: tau_c_ps(:)
235 real(real64), allocatable :: nc_ae_lapl(:)
236 real(real64), allocatable :: nc_ps_lapl(:)
239 real(real64), allocatable :: dphi_ae(:,:)
240 real(real64), allocatable :: dphi_ps(:,:)
241 real(real64), allocatable :: d2phi_ae(:,:)
242 real(real64), allocatable :: d2phi_ps(:,:)
243
244 ! --- formulation / functional ---
245 integer :: formulation = paw_kresse_joubert
247 type(xc_functional_t), pointer :: xc_functl(:) => null()
248 logical :: owns_xc_functl = .true.
249 logical :: xc_is_gga = .false.
250 logical :: xc_is_mgga = .false.
251 logical :: xc_needs_laplacian = .false.
254 logical :: usexcnhat = .true.
255 integer :: xc_scheme = paw_xc_scheme_moments
256 end type paw_sphere_t
257
258 real(real64), parameter :: gaunt_tol = 1.0e-12_real64
259
261 real(real64), parameter :: paw_xc_tiny = 1.0e-12_real64
263contains
265 ! ---------------------------------------------------------
267 subroutine paw_onsite_init(this, ps, namespace, formulation, nspin, xc_ids, usexcnhat)
268 type(paw_sphere_t), intent(out) :: this
269 type(ps_t), intent(in) :: ps
270 type(namespace_t), intent(in) :: namespace
271 integer, intent(in) :: formulation
272 integer, intent(in) :: nspin
274 integer, optional, intent(in) :: xc_ids(2)
276 logical, optional, intent(in) :: usexcnhat
277
278 integer :: nr, nr_full, i, j, k, maxl, ilm, ll, mm, pair
279 integer, allocatable :: cpos(:)
280 real(real64), allocatable :: r_full(:), phi_ae_full(:,:), phi_ps_full(:,:), work(:), dwork(:)
281 real(real64), allocatable :: phi_ae_small_full(:,:)
282 real(real64), allocatable :: qaug_full(:,:,:)
284 push_sub_with_profile(paw_onsite_init)
286 assert(ps_is_paw(ps))
287 assert(formulation == paw_kresse_joubert .or. formulation == paw_blochl)
289 this%formulation = formulation
290 this%nspin = nspin
291 this%spin_channels = min(nspin, 2)
292
293 ! Exchange-correlation functionals, taken from the pseudopotential unless overridden
294 call paw_onsite_init_xc(this, ps, namespace, xc_ids)
296 this%usexcnhat = optional_default(usexcnhat, .not. this%xc_is_mgga)
297 assert(.not. (this%xc_is_mgga .and. this%usexcnhat))
298
299 !%Variable PAWXCScheme
300 !%Type integer
301 !%Default paw_xc_moments
302 !%Section Hamiltonian
303 !%Description
304 !% Scheme used to evaluate the PAW one-center (inside the augmentation spheres)
305 !% exchange-correlation energy and potential.
306 !%Option paw_xc_full 1
307 !% Exact evaluation of the exchange-correlation functional on the radial x angular
308 !% one-center grid.
309 !%Option paw_xc_moments 2
310 !% Second-order expansion of the exchange-correlation potential around the spherical
311 !% part of the one-center density, expressed over the density moments n_LM(r)
312 !% (Torrent et al., Comput. Mater. Sci. 42, 337 (2008), Eqs. (56)-(65)). The functional is
313 !% evaluated on the radial grid only, which makes the one-center update much
314 !% faster; with the analytic LibXC kernels the expansion is typically accurate
315 !% to 1e-8 Ha per atom or better.
316 !%End
317 call parse_variable(namespace, 'PAWXCScheme', paw_xc_scheme_moments, this%xc_scheme)
318 if (.not. varinfo_valid_option('PAWXCScheme', this%xc_scheme)) then
319 call messages_input_error(namespace, 'PAWXCScheme')
320 end if
322 ! The moment expansion has no kinetic-energy-density kernel: meta-GGA needs the full scheme.
323 if (this%xc_is_mgga .and. this%xc_scheme == paw_xc_scheme_moments) then
324 if (parse_is_defined(namespace, 'PAWXCScheme')) then
325 call messages_not_implemented("PAW meta-GGA with PAWXCScheme = paw_xc_moments " // &
326 "(use PAWXCScheme = paw_xc_full)", namespace=namespace)
327 end if
328 message(1) = "Info: meta-GGA functional in the PAW one-center terms: using PAWXCScheme = paw_xc_full."
329 call messages_info(1, namespace=namespace)
330 this%xc_scheme = paw_xc_scheme_full
331 end if
332
333 ! The moment expansion needs the second and third derivatives of the functionals, which
334 ! some functionals (or some builds of LibXC) do not provide.
335 if (this%xc_scheme == paw_xc_scheme_moments .and. .not. paw_onsite_xc_has_kernels(this)) then
336 if (parse_is_defined(namespace, 'PAWXCScheme')) then
337 message(1) = "PAWXCScheme = paw_xc_moments needs the second and third derivatives (fxc and kxc)"
338 message(2) = "of the exchange-correlation functional of the PAW dataset, which this functional"
339 message(3) = "or this LibXC library does not provide. Use PAWXCScheme = paw_xc_full instead."
340 call messages_fatal(3, namespace=namespace)
341 end if
342 message(1) = "Info: the exchange-correlation functional of the PAW dataset has no fxc/kxc in this"
343 message(2) = " LibXC library: using PAWXCScheme = paw_xc_full."
344 call messages_info(2, namespace=namespace)
345 this%xc_scheme = paw_xc_scheme_full
346 end if
347
348 ! --- radial grid ---
349 ! The radial functions are first evaluated on the full dataset grid, where the radial
350 ! derivatives are taken, and are then restricted to the augmentation sphere r <= r_c,
351 ! where all the one-center integrals are done. r_c is a point of the dataset grid; the
352 ! tolerance only absorbs the rounding of r_c in the file.
353 nr_full = size(ps%paw_grid)
354 safe_allocate(r_full(1:nr_full))
355 r_full(1:nr_full) = ps%paw_grid(1:nr_full)
356 this%rc = ps%paw_cutoff_radius
357 assert(this%rc > m_zero)
358 nr = 0
359 do k = 1, nr_full
360 if (r_full(k) > this%rc*(m_one + 1.0e-6_real64)) exit
361 nr = k
362 end do
363 assert(nr > 2)
364 this%nr = nr
365 safe_allocate(this%r(1:nr))
366 safe_allocate(this%r2(1:nr))
367 safe_allocate(this%wr(1:nr))
368 safe_allocate(this%vwr(1:nr))
369 this%r(1:nr) = r_full(1:nr)
370 this%r2(1:nr) = this%r(1:nr)**2
371 this%wr(1:nr) = ps%paw_rab(1:nr)
372 this%vwr(1:nr) = this%r2(1:nr)*this%wr(1:nr)
373 ! 1/r with the origin (r = 0) mapped to 0: the (phi/r) angular-gradient terms vanish there
374 ! for the physical partial waves, so this removes the r > 0 branch from the hot loops.
375 safe_allocate(this%invr(1:nr))
376 do k = 1, nr
377 if (this%r(k) > m_zero) then
378 this%invr(k) = m_one/this%r(k)
379 else
380 this%invr(k) = m_zero
381 end if
382 end do
383
384 ! Banded radial first-derivative operator of the moment XC scheme
386
387 ! --- radial partial waves ---
388 this%nrad = ps%paw_nwavefunctions
389 this%npair = (this%nrad*(this%nrad + 1))/2
390 safe_allocate(this%lrad(1:this%nrad))
391 this%lrad(1:this%nrad) = ps%paw_l(1:this%nrad)
392 safe_allocate(this%rad_2j(1:this%nrad))
393 this%rad_2j = 0
394 if (allocated(ps%paw_2j)) this%rad_2j(1:this%nrad) = ps%paw_2j(1:this%nrad)
395 safe_allocate(phi_ae_full(1:nr_full, 1:this%nrad))
396 safe_allocate(phi_ps_full(1:nr_full, 1:this%nrad))
397 do i = 1, this%nrad
398 do k = 1, nr_full
399 phi_ae_full(k, i) = spline_eval(ps%paw_phi_ae(i), r_full(k))
400 phi_ps_full(k, i) = spline_eval(ps%paw_phi_ps(i), r_full(k))
401 end do
402 end do
403 safe_allocate(this%phi_ae(1:nr, 1:this%nrad))
404 safe_allocate(this%phi_ps(1:nr, 1:this%nrad))
405 this%phi_ae(1:nr, :) = phi_ae_full(1:nr, :)
406 this%phi_ps(1:nr, :) = phi_ps_full(1:nr, :)
407
408 ! Fully-relativistic datasets: small component of the AE partial waves
409 this%has_small = ps%paw_has_small
410 if (this%has_small) then
411 safe_allocate(phi_ae_small_full(1:nr_full, 1:this%nrad))
412 do i = 1, this%nrad
413 do k = 1, nr_full
414 phi_ae_small_full(k, i) = spline_eval(ps%paw_phi_ae_small(i), r_full(k))
415 end do
416 end do
417 safe_allocate(this%phi_ae_small(1:nr, 1:this%nrad))
418 this%phi_ae_small(1:nr, :) = phi_ae_small_full(1:nr, :)
419 end if
420
421 maxl = maxval(this%lrad(1:this%nrad))
422 this%lmax_rho = 2*maxl
423
424 ! The real_gaunt cache is limited to l <= 6; the density couples up to 2*maxl.
425 if (this%lmax_rho > 6) then
426 call messages_not_implemented("PAW one-center engine for partial waves with l > 3", namespace=namespace)
427 end if
428
429 ! --- (l,m)-resolved projector enumeration (file order over radial channels) ---
430 this%nlm = 0
431 do i = 1, this%nrad
432 this%nlm = this%nlm + 2*this%lrad(i) + 1
433 end do
434 safe_allocate(this%p_rad(1:this%nlm))
435 safe_allocate(this%p_l(1:this%nlm))
436 safe_allocate(this%p_m(1:this%nlm))
437 ilm = 0
438 do i = 1, this%nrad
439 do mm = -this%lrad(i), this%lrad(i)
440 ilm = ilm + 1
441 this%p_rad(ilm) = i
442 this%p_l(ilm) = this%lrad(i)
443 this%p_m(ilm) = mm
444 end do
445 end do
446 assert(ilm == this%nlm)
447
448 this%nlm_rho = (this%lmax_rho + 1)**2
449
450 ! Compensation augmentation Qhat_ij^L(r)
451 ! ps%radial_augmentation is stored in the octopus (cmap) channel order, obtained in ps_xml_load
452 ! by mapping the projectors per l block through cmap. cpos translates the file radial index to
453 ! its cmap position: the identity for scalar-relativistic datasets, the j = l+1/2-first
454 ! permutation within each pair for fully-relativistic ones. This assumes:
455 ! - there is one augmentation channel per partial wave;
456 ! - the partial waves are stored grouped by increasing l (also assumed for paw_l).
457 ! Qhat is symmetric under i <-> j, so only the packed i <= j triangle is stored.
458 assert(ps%radial_augmentation_nprojectors == this%nrad)
459 do i = 2, this%nrad
460 assert(this%lrad(i) >= this%lrad(i-1))
461 end do
462 safe_allocate(cpos(1:this%nrad))
463 do i = 1, this%nrad
464 pair = 0
465 do j = 1, i
466 if (this%lrad(j) == this%lrad(i)) pair = pair + 1
467 end do
468 cpos(i) = pair
469 if (this%rad_2j(i) > 0 .and. this%lrad(i) > 0) then
470 if (this%rad_2j(i) == 2*this%lrad(i) - 1) then
471 cpos(i) = (pair - 1)/2*2 + 2
472 else
473 cpos(i) = (pair - 1)/2*2 + 1
474 end if
475 end if
476 cpos(i) = i - pair + cpos(i)
477 end do
478
479 this%laug = ps%radial_augmentation_lmax
480 assert(this%laug >= 0)
481 safe_allocate(qaug_full(1:nr_full, 1:this%npair, 0:this%laug))
482 do i = 1, this%nrad
483 do j = i, this%nrad
484 pair = paw_pair_index(i, j)
485 do ll = 0, this%laug
486 do k = 1, nr_full
487 qaug_full(k, pair, ll) = spline_eval(ps%radial_augmentation((cpos(i)-1)*this%nrad + cpos(j), ll), r_full(k))
488 end do
489 end do
490 end do
491 end do
492 safe_allocate(this%qaug(1:nr, 1:this%npair, 0:this%laug))
493 this%qaug(1:nr, :, :) = qaug_full(1:nr, :, :)
494 safe_deallocate_a(cpos)
495
496 ! Core densities and reference potentials
497 this%has_ae_core = ps%paw_has_ae_core
498 this%has_ps_core = ps_has_nlcc(ps)
499 safe_allocate(this%nc_ae(1:nr))
500 safe_allocate(this%nc_ps(1:nr))
501 safe_allocate(this%vref_ae(1:nr))
502 safe_allocate(this%vref_ps(1:nr))
503 this%nc_ae = m_zero
504 this%nc_ps = m_zero
505 do k = 1, nr
506 if (this%has_ae_core) this%nc_ae(k) = spline_eval(ps%paw_ae_core, this%r(k))
507 if (this%has_ps_core) this%nc_ps(k) = spline_eval(ps%core, this%r(k))
508 this%vref_ae(k) = spline_eval(ps%paw_ae_vlocal, this%r(k))
509 this%vref_ps(k) = spline_eval(ps%vl, this%r(k))
510 end do
511
512 ! Radial derivatives needed for the GGA gradient of the density.
513 if (this%xc_is_gga) then
514 safe_allocate(work(1:nr_full))
515 safe_allocate(dwork(1:nr_full))
516 safe_allocate(this%dpair_ae(1:nr, 1:this%npair))
517 safe_allocate(this%dpair_ps(1:nr, 1:this%npair))
518 safe_allocate(this%dqaug(1:nr, 1:this%npair, 0:this%laug))
519 do i = 1, this%nrad
520 do j = i, this%nrad
521 pair = paw_pair_index(i, j)
522 work(:) = phi_ae_full(:, i)*phi_ae_full(:, j)
523 ! the small component completes the AE pair density (see paw_onsite_init_pairs)
524 if (this%has_small) work(:) = work(:) + phi_ae_small_full(:, i)*phi_ae_small_full(:, j)
525 call paw_radial_derivative(r_full, work, dwork)
526 this%dpair_ae(1:nr, pair) = dwork(1:nr)
527 work(:) = phi_ps_full(:, i)*phi_ps_full(:, j)
528 call paw_radial_derivative(r_full, work, dwork)
529 this%dpair_ps(1:nr, pair) = dwork(1:nr)
530 do ll = 0, this%laug
531 call paw_radial_derivative(r_full, qaug_full(:, pair, ll), dwork)
532 this%dqaug(1:nr, pair, ll) = dwork(1:nr)
533 end do
534 end do
535 end do
536
537 safe_allocate(this%nc_ae_der(1:nr))
538 safe_allocate(this%nc_ps_der(1:nr))
539 this%nc_ae_der = m_zero
540 this%nc_ps_der = m_zero
541 if (this%has_ae_core) then
542 do k = 1, nr_full
543 work(k) = spline_eval(ps%paw_ae_core, r_full(k))
544 end do
545 call paw_radial_derivative(r_full, work, dwork)
546 this%nc_ae_der(1:nr) = dwork(1:nr)
547 end if
548 if (this%has_ps_core) then
549 do k = 1, nr_full
550 work(k) = spline_eval(ps%core, r_full(k))
551 end do
552 call paw_radial_derivative(r_full, work, dwork)
553 this%nc_ps_der(1:nr) = dwork(1:nr)
554 end if
555 safe_deallocate_a(work)
556 safe_deallocate_a(dwork)
557 end if
558
559 ! Meta-GGA: radial derivatives of the partial waves (and the second derivatives for a
560 ! Laplacian-dependent functional), the frozen-core kinetic-energy density and the core-density
561 ! Laplacians. As for the GGA quantities above, all derivatives are taken on the full grid.
562 if (this%xc_is_mgga) then
563 call paw_onsite_init_mgga(this, ps, namespace, r_full, phi_ae_full, phi_ps_full)
564 end if
565
566 ! Kinetic matrices
567 call paw_onsite_kinetic(this, r_full, phi_ae_full, phi_ps_full)
568
569 ! Angular grid + spherical harmonics
570 call paw_onsite_angular_grid(this)
571
572 ! Radial pair products and packed Gaunt couplings
573 call paw_onsite_init_pairs(this)
574
575 safe_deallocate_a(r_full)
576 safe_deallocate_a(phi_ae_full)
577 safe_deallocate_a(phi_ae_small_full)
578 safe_deallocate_a(phi_ps_full)
579 safe_deallocate_a(qaug_full)
580
581 pop_sub_with_profile(paw_onsite_init)
582 end subroutine paw_onsite_init
583
584 ! ---------------------------------------------------------
587 subroutine paw_onsite_set_shared_xc(this, functl)
588 type(paw_sphere_t), intent(inout) :: this
589 type(xc_functional_t), pointer, intent(in) :: functl(:)
590
592
593 assert(associated(functl))
594 ! Drop the owned handles (family flags and core KED were already derived from them in _init).
595 if (this%owns_xc_functl .and. associated(this%xc_functl)) then
596 call xc_functional_end(this%xc_functl(1))
597 call xc_functional_end(this%xc_functl(2))
598 safe_deallocate_p(this%xc_functl)
599 end if
600 this%xc_functl => functl
601 this%owns_xc_functl = .false.
602
604 end subroutine paw_onsite_set_shared_xc
605
606 ! ---------------------------------------------------------
610 integer pure function paw_pair_index(ri, rj)
611 integer, intent(in) :: ri, rj
612
613 integer :: i, j
614
615 i = min(ri, rj)
616 j = max(ri, rj)
617 paw_pair_index = (j*(j-1))/2 + i
618
619 end function paw_pair_index
620
621 ! ---------------------------------------------------------
624 subroutine paw_onsite_init_pairs(this)
625 type(paw_sphere_t), intent(inout) :: this
626
627 integer :: i, j, ri, rj, li, mi, lj, mj, ll, mm, pair, ig, ij, np, ipass
628 real(real64) :: c
629
630 push_sub(paw_onsite_init_pairs)
631
632 np = this%nr
633 assert(this%npair == (this%nrad*(this%nrad + 1))/2)
634 safe_allocate(this%pair_ae(1:np, 1:this%npair))
635 safe_allocate(this%pair_ps(1:np, 1:this%npair))
636 do i = 1, this%nrad
637 do j = i, this%nrad
638 pair = paw_pair_index(i, j)
639 this%pair_ae(:, pair) = this%phi_ae(:, i)*this%phi_ae(:, j)
640 this%pair_ps(:, pair) = this%phi_ps(:, i)*this%phi_ps(:, j)
641 ! Fully-relativistic datasets: the small-component product completes the AE pair
642 ! density, with the same (l_i, l_j) angular structure as the large component
643 if (this%has_small) then
644 this%pair_ae(:, pair) = this%pair_ae(:, pair) + this%phi_ae_small(:, i)*this%phi_ae_small(:, j)
645 end if
646 end do
647 end do
648
649 ! Two passes over the Gaunt-live couplings: count, then fill the CSR arrays.
650 safe_allocate(this%gnt_ptr(1:this%nlm*this%nlm + 1))
651 do ipass = 1, 2
652 ig = 0
653 do i = 1, this%nlm
654 ri = this%p_rad(i)
655 li = this%p_l(i)
656 mi = this%p_m(i)
657 do j = 1, this%nlm
658 rj = this%p_rad(j)
659 lj = this%p_l(j)
660 mj = this%p_m(j)
661 pair = paw_pair_index(ri, rj)
662 ij = (i-1)*this%nlm + j
663 if (ipass == 2) this%gnt_ptr(ij) = ig + 1
664 do ll = abs(li-lj), min(li+lj, this%lmax_rho), 2
665 do mm = -ll, ll
666 c = real_gaunt(li, mi, lj, mj, ll, mm)
667 if (abs(c) <= gaunt_tol) cycle
668 ig = ig + 1
669 if (ipass == 2) then
670 this%gnt_pair(ig) = pair
671 this%gnt_l(ig) = ll
672 this%gnt_lm(ig) = ll*(ll+1) + mm + 1
673 this%gnt_c(ig) = c
674 end if
675 end do
676 end do
677 end do
678 end do
679 if (ipass == 1) then
680 this%ngnt = ig
681 safe_allocate(this%gnt_pair(1:this%ngnt))
682 safe_allocate(this%gnt_l(1:this%ngnt))
683 safe_allocate(this%gnt_lm(1:this%ngnt))
684 safe_allocate(this%gnt_c(1:this%ngnt))
685 else
686 this%gnt_ptr(this%nlm*this%nlm + 1) = ig + 1
687 assert(ig == this%ngnt)
688 end if
689 end do
690
691 pop_sub(paw_onsite_init_pairs)
692 end subroutine paw_onsite_init_pairs
693
694 ! ---------------------------------------------------------
696 subroutine paw_onsite_init_xc(this, ps, namespace, xc_ids)
697 type(paw_sphere_t), intent(inout) :: this
698 type(ps_t), intent(in) :: ps
699 type(namespace_t), intent(in) :: namespace
701 integer, optional, intent(in) :: xc_ids(2)
702
703 integer :: ixc, ids(2)
704
705 push_sub(paw_onsite_init_xc)
706
707 if (present(xc_ids)) then
708 ! The functionals of the calculation: a zero id means no functional (e.g. independent
709 ! particles), not the one of the dataset.
710 ids(1) = xc_ids(1)
711 ids(2) = xc_ids(2)
712 else
713 ! A functional that the pseudopotential reader could not identify must not be silently
714 ! replaced by "no functional": the one-center XC would then be wrong.
715 if (ps%exchange_functional == pseudo_exchange_unknown .or. &
716 ps%correlation_functional == pseudo_correlation_unknown) then
717 message(1) = "PAW one-center engine: the exchange-correlation functional of the PAW dataset"
718 message(2) = "could not be identified. Only LDA, GGA and meta-GGA functionals are supported."
719 call messages_fatal(2, namespace=namespace)
720 end if
721
722 ids(1) = ps%exchange_functional
723 ids(2) = ps%correlation_functional
724 end if
725
726 this%xc_is_gga = .false.
727 this%xc_is_mgga = .false.
728 this%xc_needs_laplacian = .false.
729 ! The sphere owns these handles by default
730 safe_allocate(this%xc_functl(1:2))
731 this%owns_xc_functl = .true.
732 do ixc = 1, 2
733 ! a non-positive id means "not specified": initialize an empty (family NONE) functional
734 if (ids(ixc) > 0) then
735 call xc_functional_init(this%xc_functl(ixc), namespace, ids(ixc), 3, ps%z_val, this%spin_channels, .true.)
736 else
737 call xc_functional_init(this%xc_functl(ixc), namespace, 0, 3, ps%z_val, this%spin_channels, .true.)
738 end if
739 ! (Semi-)local LDA, GGA and meta-GGA are supported; hybrids are not.
740 select case (this%xc_functl(ixc)%family)
741 case (xc_family_none, xc_family_lda)
742 ! local: nothing to do
743 case (xc_family_gga)
744 this%xc_is_gga = .true.
745 case (xc_family_mgga)
746 this%xc_is_mgga = .true.
747 this%xc_is_gga = .true.
748 if (bitand(this%xc_functl(ixc)%flags, xc_flags_needs_laplacian) /= 0) then
749 this%xc_needs_laplacian = .true.
750 end if
751 case (xc_family_hyb_lda, xc_family_hyb_gga, xc_family_hyb_mgga)
752 call messages_not_implemented("PAW one-center engine with hybrid functionals", namespace=namespace)
753 case default
754 call messages_not_implemented("PAW one-center engine with this XC functional family " // &
755 "(only LDA, GGA and meta-GGA are supported)", namespace=namespace)
756 end select
757 end do
758
759 pop_sub(paw_onsite_init_xc)
760 end subroutine paw_onsite_init_xc
761
762 ! ---------------------------------------------------------
780 subroutine paw_onsite_init_mgga(this, ps, namespace, r_full, phi_ae_full, phi_ps_full)
781 type(paw_sphere_t), intent(inout) :: this
782 type(ps_t), intent(in) :: ps
783 type(namespace_t), intent(in) :: namespace
784 real(real64), intent(in) :: r_full(:)
785 real(real64), intent(in) :: phi_ae_full(:,:)
786 real(real64), intent(in) :: phi_ps_full(:,:)
787
788 integer :: nr, nr_full, k, i, ic, ll
789 real(real64) :: occ, c_tf, nn, q_orb, q_stored
790 real(real64), allocatable :: phi(:), dphi(:), work(:), dwork(:), nc_der_full(:)
791 real(real64), parameter :: tiny = 1.0e-10_real64
793 real(real64), parameter :: core_charge_tol = 1.0e-4_real64
794
795 push_sub(paw_onsite_init_mgga)
796
797 assert(this%xc_is_gga)
798
799 if (this%nspin == 4) then
800 call messages_not_implemented("PAW meta-GGA with noncollinear spins", namespace=namespace)
801 end if
802
803 ! The meta-GGA quantities are built from the large component of the AE partial waves only.
804 if (this%has_small) then
805 call messages_not_implemented("PAW meta-GGA with fully-relativistic (j-dependent) datasets", &
806 namespace=namespace)
807 end if
808
809 ! Without the core orbitals, the AE core kinetic-energy density cannot be built, and would
810 ! silently be taken as zero.
811 if (this%has_ae_core .and. ps%paw_ncore == 0) then
812 message(1) = "PAW meta-GGA: the dataset has an all-electron core density but no frozen-core orbitals"
813 message(2) = "(PP_GIPAW/PP_GIPAW_CORE_ORBITALS), which are needed for the core kinetic-energy density."
814 call messages_fatal(2, namespace=namespace)
815 end if
816
817 nr = this%nr
818 nr_full = size(r_full)
819
820 safe_allocate(work(1:nr_full))
821 safe_allocate(dwork(1:nr_full))
822
823 ! Partial-wave derivatives
824 safe_allocate(this%dphi_ae(1:nr, 1:this%nrad))
825 safe_allocate(this%dphi_ps(1:nr, 1:this%nrad))
826 if (this%xc_needs_laplacian) then
827 safe_allocate(this%d2phi_ae(1:nr, 1:this%nrad))
828 safe_allocate(this%d2phi_ps(1:nr, 1:this%nrad))
829 end if
830 do i = 1, this%nrad
831 call paw_radial_derivative(r_full, phi_ae_full(:, i), work)
832 this%dphi_ae(1:nr, i) = work(1:nr)
833 call paw_radial_derivative(r_full, phi_ps_full(:, i), work)
834 this%dphi_ps(1:nr, i) = work(1:nr)
835 if (this%xc_needs_laplacian) then
836 call paw_radial_second_derivative(r_full, phi_ae_full(:, i), work)
837 this%d2phi_ae(1:nr, i) = work(1:nr)
838 call paw_radial_second_derivative(r_full, phi_ps_full(:, i), work)
839 this%d2phi_ps(1:nr, i) = work(1:nr)
840 end if
841 end do
842
843 safe_allocate(this%tau_c_ae(1:nr))
844 safe_allocate(this%tau_c_ps(1:nr))
845 this%tau_c_ae = m_zero
846 this%tau_c_ps = m_zero
847
848 ! All-electron core KED from the frozen-core orbitals. The AE core density gradient nc_ae_der
849 ! is rebuilt here from the same core-orbital derivatives (grad n_c = sum_c occ_c 2 phi_c dphi_c
850 ! / 4pi) instead of the radial derivative of the stored density, so that tau_c and grad(n_c) are
851 ! consistent (Cauchy-Schwarz then keeps tau_c >= tau_W in the core region).
852 safe_allocate(nc_der_full(1:nr_full))
853 nc_der_full = m_zero
854 if (ps%paw_ncore > 0) then
855 safe_allocate(phi(1:nr_full))
856 safe_allocate(dphi(1:nr_full))
857 q_orb = m_zero
858 do ic = 1, ps%paw_ncore
859 ll = ps%paw_core_l(ic)
860 occ = real(2*(2*ll + 1), real64)
861 do k = 1, nr_full
862 phi(k) = spline_eval(ps%paw_core_orbital(ic), r_full(k))
863 end do
864 call paw_radial_derivative(r_full, phi, dphi)
865 do k = 1, nr
866 this%tau_c_ae(k) = this%tau_c_ae(k) + occ/(m_four*m_pi) &
867 *(dphi(k)**2 + real(ll*(ll+1), real64)*(phi(k)*this%invr(k))**2)
868 end do
869 nc_der_full(:) = nc_der_full(:) + occ*m_two*phi(:)*dphi(:)/(m_four*m_pi)
870 q_orb = q_orb + occ*sum(phi(:)**2*r_full(:)**2*ps%paw_rab(1:nr_full))
871 end do
872 safe_deallocate_a(phi)
873 safe_deallocate_a(dphi)
874
875 ! The closed-shell occupations 2(2l+1) are assumed: check that the core orbitals reproduce
876 ! the stored all-electron core density (PP_AE_NLCC).
877 q_stored = m_zero
878 if (this%has_ae_core) then
879 do k = 1, nr_full
880 work(k) = spline_eval(ps%paw_ae_core, r_full(k))
881 end do
882 q_stored = m_four*m_pi*sum(work(:)*r_full(:)**2*ps%paw_rab(1:nr_full))
883 end if
884 if (abs(q_orb - q_stored) > core_charge_tol) then
885 write(message(1), '(a)') "PAW meta-GGA: the frozen-core orbitals of the dataset do not reproduce its"
886 write(message(2), '(a,f12.6,a,f12.6,a)') "all-electron core density (core charge ", q_orb, &
887 " from the orbitals, ", q_stored, " stored)."
888 message(3) = "The core kinetic-energy density is inconsistent with the core density."
889 call messages_warning(3, namespace=namespace)
890 end if
891
892 this%nc_ae_der(1:nr) = nc_der_full(1:nr)
893 end if
894
895 ! Smooth core KED: Thomas-Fermi + (1/9) von Weizsaecker gradient expansion of the smooth core
896 ! density (no one half convention: tau_TF = (3/5)(3 pi^2)^(2/3) n^(5/3), tau_vW = (1/4)|grad n|^2/n).
897 if (this%has_ps_core) then
898 c_tf = (m_three/m_five)*(m_three*m_pi**2)**m_twothird
899 do k = 1, nr
900 nn = this%nc_ps(k)
901 if (nn > tiny) then
902 this%tau_c_ps(k) = c_tf*nn**(m_five/m_three) &
903 + (m_one/9.0_real64)*m_fourth*this%nc_ps_der(k)**2/nn
904 end if
905 end do
906 end if
907
908 ! Radial Laplacian of the core densities for Laplacian-dependent meta-GGAs (e.g. TB09):
909 ! lapl n = d2n/dr2 + (2/r) dn/dr.
910 if (this%xc_needs_laplacian) then
911 ! d2n/dr2 is the direct second derivative of the stored core density; dn/dr is the
912 ! (core-orbital) radial gradient above.
913 safe_allocate(this%nc_ae_lapl(1:nr))
914 safe_allocate(this%nc_ps_lapl(1:nr))
915 this%nc_ae_lapl = m_zero
916 this%nc_ps_lapl = m_zero
917 if (this%has_ae_core) then
918 do k = 1, nr_full
919 work(k) = spline_eval(ps%paw_ae_core, r_full(k))
920 end do
921 call paw_radial_second_derivative(r_full, work, dwork)
922 do k = 1, nr
923 this%nc_ae_lapl(k) = dwork(k) + m_two*this%invr(k)*this%nc_ae_der(k)
924 end do
925 end if
926 if (this%has_ps_core) then
927 do k = 1, nr_full
928 work(k) = spline_eval(ps%core, r_full(k))
929 end do
930 call paw_radial_second_derivative(r_full, work, dwork)
931 do k = 1, nr
932 this%nc_ps_lapl(k) = dwork(k) + m_two*this%invr(k)*this%nc_ps_der(k)
933 end do
934 end if
935 end if
936
937 safe_deallocate_a(work)
938 safe_deallocate_a(dwork)
939 safe_deallocate_a(nc_der_full)
940
941 ! These core quantities are added to each of the spin_channels density channels, so store them
942 ! already split per spin channel (spin_channels is fixed for the sphere) and avoid the per-call
943 ! division at the point of use. tau_c and the core Laplacian are meta-GGA-only fields with a single
944 ! consumer each; the core density (nc_ae) and its gradient (nc_ae_der) keep the total convention
945 ! because they are shared with the GGA path.
946 this%tau_c_ae(1:nr) = this%tau_c_ae(1:nr)/this%spin_channels
947 this%tau_c_ps(1:nr) = this%tau_c_ps(1:nr)/this%spin_channels
948 if (this%xc_needs_laplacian) then
949 this%nc_ae_lapl(1:nr) = this%nc_ae_lapl(1:nr)/this%spin_channels
950 this%nc_ps_lapl(1:nr) = this%nc_ps_lapl(1:nr)/this%spin_channels
951 end if
952
953 pop_sub(paw_onsite_init_mgga)
954 end subroutine paw_onsite_init_mgga
955
956 ! ---------------------------------------------------------
959 logical function paw_onsite_xc_has_kernels(this) result(has_kernels)
960 type(paw_sphere_t), intent(in) :: this
961
962 integer :: ixc
963
965
966 has_kernels = .true.
967 do ixc = 1, 2
968 if (this%xc_functl(ixc)%family == xc_family_none) cycle
969 if (bitand(this%xc_functl(ixc)%flags, xc_flags_have_fxc) == 0) has_kernels = .false.
970 if (bitand(this%xc_functl(ixc)%flags, xc_flags_have_kxc) == 0) has_kernels = .false.
971 end do
972
974 end function paw_onsite_xc_has_kernels
975
976 ! ---------------------------------------------------------
985 subroutine paw_onsite_kinetic(this, r_full, phi_ae_full, phi_ps_full)
986 type(paw_sphere_t), intent(inout) :: this
987 real(real64), intent(in) :: r_full(:)
988 real(real64), intent(in) :: phi_ae_full(:,:)
989 real(real64), intent(in) :: phi_ps_full(:,:)
990
991 integer :: i, j, k, nr, ll
992 real(real64), allocatable :: uae(:,:), ups(:,:), duae(:,:), dups(:,:)
993 real(real64) :: kae, kps, cent
994
995 push_sub_with_profile(paw_onsite_kinetic)
996
997 safe_allocate(uae(1:size(r_full), 1:this%nrad))
998 safe_allocate(ups(1:size(r_full), 1:this%nrad))
999 safe_allocate(duae(1:size(r_full), 1:this%nrad))
1000 safe_allocate(dups(1:size(r_full), 1:this%nrad))
1001
1002 do i = 1, this%nrad
1003 uae(:, i) = r_full(:) * phi_ae_full(:, i)
1004 ups(:, i) = r_full(:) * phi_ps_full(:, i)
1005 call paw_radial_derivative(r_full, uae(:, i), duae(:, i))
1006 call paw_radial_derivative(r_full, ups(:, i), dups(:, i))
1007 end do
1008
1009 nr = this%nr
1010 safe_allocate(this%kin_ae(1:this%nrad, 1:this%nrad))
1011 safe_allocate(this%kin_ps(1:this%nrad, 1:this%nrad))
1012 this%kin_ae = m_zero
1013 this%kin_ps = m_zero
1014 do i = 1, this%nrad
1015 do j = 1, this%nrad
1016 if (this%lrad(i) /= this%lrad(j)) cycle
1017 ll = this%lrad(i)
1018 cent = real(ll*(ll+1), real64)
1019 kae = m_zero
1020 kps = m_zero
1021 ! The r=0 point is skipped: u = r phi ~ r^(l+1) makes the integrand vanish there
1022 ! for l>0 and finite for l=0, but its first-interval weight (~r_2) is tiny on the
1023 ! log grid and its integration-weight convention is ambiguous, so including it only
1024 ! adds noise. The centrifugal term is well defined for k>=2 (r_k > 0).
1025 do k = 2, nr
1026 kae = kae + (duae(k,i)*duae(k,j) + cent*uae(k,i)*uae(k,j)/this%r2(k)) * this%wr(k)
1027 kps = kps + (dups(k,i)*dups(k,j) + cent*ups(k,i)*ups(k,j)/this%r2(k)) * this%wr(k)
1028 end do
1029 this%kin_ae(i, j) = m_half*kae
1030 this%kin_ps(i, j) = m_half*kps
1031 end do
1032 end do
1033
1034 safe_deallocate_a(uae)
1035 safe_deallocate_a(ups)
1036 safe_deallocate_a(duae)
1037 safe_deallocate_a(dups)
1038
1039 pop_sub_with_profile(paw_onsite_kinetic)
1040 end subroutine paw_onsite_kinetic
1041
1042 ! ---------------------------------------------------------
1044 subroutine paw_radial_derivative(r, f, df)
1045 real(real64), intent(in) :: r(:)
1046 real(real64), intent(in) :: f(:)
1047 real(real64), intent(out) :: df(:)
1048
1049 integer :: k, n
1050 type(spline_t) :: sp, dsp
1051
1052 push_sub(paw_radial_derivative)
1053
1054 n = size(r)
1055 call spline_init(sp)
1056 call spline_init(dsp)
1057 call spline_fit(n, r, f, sp)
1058 call spline_der(sp, dsp, m_zero)
1059 do k = 1, n
1060 df(k) = spline_eval(dsp, r(k))
1061 end do
1062 call spline_end(sp)
1063 call spline_end(dsp)
1064
1065 pop_sub(paw_radial_derivative)
1066 end subroutine paw_radial_derivative
1067
1068 ! ---------------------------------------------------------
1071 subroutine paw_radial_second_derivative(r, f, d2f)
1072 real(real64), intent(in) :: r(:)
1073 real(real64), intent(in) :: f(:)
1074 real(real64), intent(out) :: d2f(:)
1075
1076 integer :: k, n
1077 type(spline_t) :: sp, d2sp
1078
1080
1081 n = size(r)
1082 call spline_init(sp)
1083 call spline_init(d2sp)
1084 call spline_fit(n, r, f, sp)
1085 call spline_der2(sp, d2sp, m_zero)
1086 do k = 1, n
1087 d2f(k) = spline_eval(d2sp, r(k))
1088 end do
1089 call spline_end(sp)
1090 call spline_end(d2sp)
1091
1093 end subroutine paw_radial_second_derivative
1094
1095 ! ---------------------------------------------------------
1101 subroutine paw_onsite_init_derivative(this)
1102 type(paw_sphere_t), intent(inout) :: this
1103
1104 integer :: ip, i0, nw, jj, kk
1105 integer, allocatable :: first(:), last(:)
1106
1108
1109 nw = this%der_nw
1110 assert(this%nr >= nw)
1111 safe_allocate(this%der_w(1:nw, 1:this%nr))
1112 safe_allocate(this%der_i0(1:this%nr))
1113 do ip = 1, this%nr
1114 i0 = min(max(ip - (nw-1)/2, 1), this%nr - nw + 1)
1115 this%der_i0(ip) = i0
1116 call paw_fornberg_weights(this%r(ip), this%r(i0:i0+nw-1), nw, this%der_w(:, ip))
1117 end do
1118
1119 safe_allocate(first(1:this%nr))
1120 safe_allocate(last(1:this%nr))
1121 first = this%nr + 1
1122 last = 0
1123 do ip = 1, this%nr
1124 do kk = 1, nw
1125 jj = this%der_i0(ip) + kk - 1
1126 first(jj) = min(first(jj), ip)
1127 last(jj) = max(last(jj), ip)
1128 end do
1129 end do
1130 this%der_ntw = maxval(last - first + 1)
1131 safe_allocate(this%der_wt(1:this%der_ntw, 1:this%nr))
1132 safe_allocate(this%der_trow0(1:this%nr))
1133 safe_allocate(this%der_tn(1:this%nr))
1134 this%der_wt = m_zero
1135 this%der_trow0(1:this%nr) = first(1:this%nr)
1136 this%der_tn(1:this%nr) = last(1:this%nr) - first(1:this%nr) + 1
1137 do ip = 1, this%nr
1138 do kk = 1, nw
1139 jj = this%der_i0(ip) + kk - 1
1140 this%der_wt(ip - first(jj) + 1, jj) = this%der_w(kk, ip)
1141 end do
1142 end do
1143 safe_deallocate_a(first)
1144 safe_deallocate_a(last)
1145
1147 end subroutine paw_onsite_init_derivative
1148
1149 ! ---------------------------------------------------------
1152 subroutine paw_fornberg_weights(z, x, n, w)
1153 real(real64), intent(in) :: z
1154 real(real64), intent(in) :: x(:)
1155 integer, intent(in) :: n
1156 real(real64), intent(out) :: w(:)
1157
1158 integer :: i, j, k, mn
1159 real(real64) :: c1, c2, c3, c4, c5
1160 real(real64) :: c(n, 0:1)
1161
1162 ! no PUSH_SUB, called too often
1163
1164 c = m_zero
1165 c1 = m_one
1166 c4 = x(1) - z
1167 c(1, 0) = m_one
1168 do i = 2, n
1169 mn = min(i - 1, 1)
1170 c2 = m_one
1171 c5 = c4
1172 c4 = x(i) - z
1173 do j = 1, i - 1
1174 c3 = x(i) - x(j)
1175 c2 = c2*c3
1176 if (j == i - 1) then
1177 do k = mn, 1, -1
1178 c(i, k) = c1*(real(k, real64)*c(i-1, k-1) - c5*c(i-1, k))/c2
1179 end do
1180 c(i, 0) = -c1*c5*c(i-1, 0)/c2
1181 end if
1182 do k = mn, 1, -1
1183 c(j, k) = (c4*c(j, k) - real(k, real64)*c(j, k-1))/c3
1184 end do
1185 c(j, 0) = c4*c(j, 0)/c3
1186 end do
1187 c1 = c2
1188 end do
1189 w(1:n) = c(1:n, 1)
1190
1191 end subroutine paw_fornberg_weights
1192
1193 ! ---------------------------------------------------------
1196 subroutine paw_radial_derivative_fd(this, f, df)
1197 type(paw_sphere_t), intent(in) :: this
1198 real(real64), intent(in) :: f(:)
1199 real(real64), intent(out) :: df(:)
1200
1201 integer :: ip, k, i0
1202 real(real64) :: acc
1203
1204 ! no PUSH_SUB, called too often
1205
1206 do ip = 1, this%nr
1207 i0 = this%der_i0(ip)
1208 acc = m_zero
1209 do k = 1, this%der_nw
1210 acc = acc + this%der_w(k, ip)*f(i0 + k - 1)
1211 end do
1212 df(ip) = acc
1213 end do
1214
1215 end subroutine paw_radial_derivative_fd
1216
1217 ! ---------------------------------------------------------
1220 subroutine paw_onsite_radial_grid(this, r)
1221 type(paw_sphere_t), intent(in) :: this
1222 real(real64), allocatable, intent(out) :: r(:)
1223
1225
1226 safe_allocate(r(1:this%nr))
1227 r(1:this%nr) = this%r(1:this%nr)
1228
1229 pop_sub(paw_onsite_radial_grid)
1230 end subroutine paw_onsite_radial_grid
1231
1232 ! ---------------------------------------------------------
1238 subroutine paw_radial_derivative_t(this, f, df)
1239 type(paw_sphere_t), intent(in) :: this
1240 real(real64), intent(in) :: f(:)
1241 real(real64), intent(out) :: df(:)
1242
1243 integer :: ip, k, i0
1244 real(real64) :: acc
1245
1246 ! no PUSH_SUB, called too often
1247
1248 do ip = 1, this%nr
1249 i0 = this%der_trow0(ip)
1250 acc = m_zero
1251 do k = 1, this%der_tn(ip)
1252 acc = acc + this%der_wt(k, ip)*f(i0 + k - 1)
1253 end do
1254 df(ip) = acc
1255 end do
1256
1257 end subroutine paw_radial_derivative_t
1258
1259 ! ---------------------------------------------------------
1262 subroutine paw_onsite_angular_grid(this)
1263 type(paw_sphere_t), intent(inout) :: this
1264
1265 integer :: nth, nphi, ith, iphi, ia, ll, mm, lm
1266 real(real64), allocatable :: gx(:), gw(:)
1267 real(real64) :: dphi, phi, sinth, costh, xx(3), y, dyth, dyph
1269 push_sub_with_profile(paw_onsite_angular_grid)
1270
1271 ! Enough points to integrate products of harmonics up to lmax_rho exactly, with
1272 ! margin for the nonlinear XC integrand.
1273 nth = this%lmax_rho + 6
1274 nphi = 2*this%lmax_rho + 3
1275 this%nang = nth*nphi
1276 assert(this%nlm_rho == (this%lmax_rho + 1)**2)
1277
1278 safe_allocate(gx(1:nth))
1279 safe_allocate(gw(1:nth))
1280 call gauss_legendre_newton(nth, gx, gw)
1281
1282 safe_allocate(this%wang(1:this%nang))
1283 safe_allocate(this%ylm(1:this%nang, 1:this%nlm_rho))
1284 ! The angular derivatives dY/dtheta and (1/sin theta) dY/dphi are needed both for the
1285 ! GGA one-center XC gradient and for the kinetic-energy-density reconstruction, so they
1286 ! are always tabulated.
1287 safe_allocate(this%dylm_dth(1:this%nang, 1:this%nlm_rho))
1288 safe_allocate(this%dylm_dph(1:this%nang, 1:this%nlm_rho))
1289 safe_allocate(this%rhat(1:this%nang, 1:3))
1290 safe_allocate(this%that(1:this%nang, 1:3))
1291 safe_allocate(this%phat(1:this%nang, 1:3))
1293 dphi = m_two*m_pi/real(nphi, real64)
1294 ia = 0
1295 do ith = 1, nth
1296 costh = gx(ith)
1297 sinth = sqrt(max(m_one - costh**2, m_zero))
1298 do iphi = 1, nphi
1299 ia = ia + 1
1300 phi = (real(iphi, real64) - m_half)*dphi
1301 xx(1) = sinth*cos(phi)
1302 xx(2) = sinth*sin(phi)
1303 xx(3) = costh
1304 ! spherical orthonormal basis in Cartesian coordinates at this direction
1305 this%rhat(ia, 1:3) = xx(1:3)
1306 this%that(ia, 1) = costh*cos(phi)
1307 this%that(ia, 2) = costh*sin(phi)
1308 this%that(ia, 3) = -sinth
1309 this%phat(ia, 1) = -sin(phi)
1310 this%phat(ia, 2) = cos(phi)
1311 this%phat(ia, 3) = m_zero
1312 this%wang(ia) = gw(ith)*dphi
1313 do ll = 0, this%lmax_rho
1314 do mm = -ll, ll
1315 lm = ll*(ll+1) + mm + 1
1316 ! one call returns the harmonic and its two angular-gradient components
1317 call ylmr_real_with_grad(xx, ll, mm, y, dyth, dyph)
1318 this%ylm(ia, lm) = y
1319 this%dylm_dth(ia, lm) = dyth
1320 this%dylm_dph(ia, lm) = dyph
1321 end do
1322 end do
1323 end do
1324 end do
1325 assert(ia == this%nang)
1326
1327 ! Quadrature-weighted copies: the XC projections absorb the angular weight into the
1328 ! harmonics, so the pointwise field builders do not scale by it.
1329 safe_allocate(this%wylm(1:this%nang, 1:this%nlm_rho))
1330 safe_allocate(this%wdylm_dth(1:this%nang, 1:this%nlm_rho))
1331 safe_allocate(this%wdylm_dph(1:this%nang, 1:this%nlm_rho))
1332 do lm = 1, this%nlm_rho
1333 do ia = 1, this%nang
1334 this%wylm(ia, lm) = this%wang(ia)*this%ylm(ia, lm)
1335 this%wdylm_dth(ia, lm) = this%wang(ia)*this%dylm_dth(ia, lm)
1336 this%wdylm_dph(ia, lm) = this%wang(ia)*this%dylm_dph(ia, lm)
1337 end do
1338 end do
1339
1340 safe_deallocate_a(gx)
1341 safe_deallocate_a(gw)
1342
1343 pop_sub_with_profile(paw_onsite_angular_grid)
1344 end subroutine paw_onsite_angular_grid
1345
1346 ! ---------------------------------------------------------
1352 subroutine paw_onsite_compute(this, rho, e1, e1t, dij, hxc_only)
1353 type(paw_sphere_t), intent(in) :: this
1354 real(real64), contiguous, intent(in) :: rho(:,:,:)
1355 real(real64), intent(out) :: e1
1356 real(real64), intent(out) :: e1t
1357 real(real64), contiguous, intent(out) :: dij(:,:,:)
1360 logical, optional, intent(in) :: hxc_only
1361
1362 real(real64) :: e_ae, e_ps, kae, kps
1363 real(real64), allocatable :: d_ae(:,:,:), d_ps(:,:,:)
1364 integer :: i, j, is
1365 logical :: hxc_only_
1366
1367 push_sub_with_profile(paw_onsite_compute)
1368
1369 hxc_only_ = optional_default(hxc_only, .false.)
1370
1371 safe_allocate(d_ae(1:this%nlm, 1:this%nlm, 1:this%nspin))
1372 safe_allocate(d_ps(1:this%nlm, 1:this%nlm, 1:this%nspin))
1373
1374 ! All-electron side (compensation off).
1375 call paw_onsite_side(this, rho, .true., e_ae, d_ae, hxc_only_)
1376 ! Pseudo side (compensation on).
1377 call paw_onsite_side(this, rho, .false., e_ps, d_ps, hxc_only_)
1378
1379 e1 = e_ae
1380 e1t = e_ps
1381 dij(:, :, :) = d_ae(:, :, :) - d_ps(:, :, :)
1382
1383 ! Kinetic terms: E1 += sum_spin Tr[rho^s K^AE], E1t += sum_spin Tr[rho^s K^PS], and
1384 ! K^AE - K^PS in each spin channel of the D matrix. Skipped in the Hxc-only (SCF) mode: the
1385 ! kinetic difference is frozen and already carried by the descreened D^0 (PP_DIJ) on the mesh.
1386 if (.not. hxc_only_) then
1387 do is = 1, this%spin_channels
1388 do j = 1, this%nlm
1389 do i = 1, this%nlm
1390 if (this%p_l(i) /= this%p_l(j) .or. this%p_m(i) /= this%p_m(j)) cycle
1391 kae = this%kin_ae(this%p_rad(i), this%p_rad(j))
1392 kps = this%kin_ps(this%p_rad(i), this%p_rad(j))
1393 dij(i, j, is) = dij(i, j, is) + kae - kps
1394 e1 = e1 + rho(i, j, is)*kae
1395 e1t = e1t + rho(i, j, is)*kps
1396 end do
1397 end do
1398 end do
1399 end if
1400
1401 safe_deallocate_a(d_ae)
1402 safe_deallocate_a(d_ps)
1403
1404 pop_sub_with_profile(paw_onsite_compute)
1405 end subroutine paw_onsite_compute
1406
1407 ! ---------------------------------------------------------
1411 subroutine paw_onsite_side(this, rho, ae, energy, dij, hxc_only)
1412 type(paw_sphere_t), intent(in) :: this
1413 real(real64), contiguous, intent(in) :: rho(:,:,:)
1414 logical, intent(in) :: ae
1415 real(real64), intent(out) :: energy
1416 real(real64), contiguous, intent(out) :: dij(:,:,:)
1419 logical, optional, intent(in) :: hxc_only
1420
1421 integer :: nr, nlmr, lm, k, is
1422 logical :: xc_with_comp, hxc_only_, need_der
1423 real(real64), allocatable :: nlm_rho(:,:,:) ! (nr, nlm_rho, nspin) Hartree density (PS incl. comp)
1424 real(real64), allocatable :: nlm_xc(:,:,:) ! (nr, nlm_rho, nspin) XC density (comp per formulation)
1425 real(real64), allocatable :: nlm_xc_der(:,:,:) ! (nr, nlm_rho, nspin) radial derivative of nlm_xc (GGA)
1426 real(real64), allocatable :: nlm_tot(:,:) ! (nr, nlm_rho) total Hartree density
1427 real(real64), allocatable :: vlm(:,:) ! (nr, nlm_rho) Hartree + reference potential
1428 real(real64), allocatable :: vref(:), ncore(:)
1429 real(real64) :: e_h, e_ref, e_xc, sqrt4pi
1430
1431 push_sub_with_profile(paw_onsite_side)
1432
1433 nr = this%nr
1434 nlmr = this%nlm_rho
1435 sqrt4pi = sqrt(m_four*m_pi)
1436 hxc_only_ = optional_default(hxc_only, .false.)
1437
1438 ! Kresse-Joubert puts the compensation charge in the XC density; Bloechl does not.
1439 ! On the all-electron side there is no compensation charge. Meta-GGA always excludes it (the
1440 ! Bloechl usexcnhat=false treatment): the compensation charge is a charge density with no
1441 ! kinetic-energy-density counterpart, so keeping the density, its gradient and tau all built from
1442 ! the partial waves is what makes the one-center tau consistent (tau >= tau_W) and D = dE/drho.
1443 xc_with_comp = (.not. ae) .and. (this%formulation == paw_kresse_joubert) .and. this%usexcnhat
1444
1445 safe_allocate(nlm_rho(1:nr, 1:nlmr, 1:this%nspin))
1446 safe_allocate(nlm_xc(1:nr, 1:nlmr, 1:this%nspin))
1447 safe_allocate(nlm_tot(1:nr, 1:nlmr))
1448 safe_allocate(vlm(1:nr, 1:nlmr))
1449 safe_allocate(vref(1:nr))
1450 safe_allocate(ncore(1:nr))
1451 vlm = m_zero
1452
1453 if (ae) then
1454 vref(:) = this%vref_ae(:)
1455 ncore(:) = this%nc_ae(:)/this%spin_channels
1456 else
1457 vref(:) = this%vref_ps(:)
1458 ncore(:) = this%nc_ps(:)/this%spin_channels
1459 end if
1460
1461 ! Hartree density and XC density (compensation included only for Kresse-Joubert on the PS side).
1462 ! The radial derivative of the XC density is only needed by the GGA on the radial x angular
1463 ! grid; the moment expansion differentiates the spherical density itself, and the meta-GGA
1464 ! builds the gradient from the partial waves (paw_xc_quantities).
1465 need_der = this%xc_is_gga .and. this%xc_scheme == paw_xc_scheme_full .and. .not. this%xc_is_mgga
1466 if (need_der) then
1467 safe_allocate(nlm_xc_der(1:nr, 1:nlmr, 1:this%nspin))
1468 end if
1469 do is = 1, this%nspin
1470 call paw_build_density(this, rho(:,:,is), ae, .not. ae, nlm_rho(:,:,is))
1471 if (need_der) then
1472 call paw_build_density(this, rho(:,:,is), ae, xc_with_comp, nlm_xc(:,:,is), nlm_xc_der(:,:,is))
1473 else if (xc_with_comp .eqv. (.not. ae)) then
1474 nlm_xc(:,:,is) = nlm_rho(:,:,is)
1475 else
1476 call paw_build_density(this, rho(:,:,is), ae, xc_with_comp, nlm_xc(:,:,is))
1477 end if
1478 end do
1479 ! total density drives the spin-independent Hartree and reference terms
1480 nlm_tot = nlm_rho(:,:,1)
1481 do is = 2, this%spin_channels
1482 nlm_tot = nlm_tot + nlm_rho(:,:,is)
1483 end do
1484
1485 ! Hartree potential per L and Hartree energy
1486 e_h = m_zero
1487 do lm = 1, nlmr
1488 call paw_hartree_lm(this, lm, nlm_tot(:, lm), vlm(:, lm))
1489 do k = 1, nr
1490 e_h = e_h + m_half*nlm_tot(k, lm)*vlm(k, lm)*this%vwr(k)
1491 end do
1492 end do
1493
1494 ! Reference potential term (spherical; couples to the total L=0 density only)
1495 ! E_ref = int v_ref(r) n(r,Omega) d^3r = sqrt(4pi) int v_ref n_00 r^2 dr.
1496 ! Skipped in the Hxc-only (SCF) mode: the reference/nuclear/local coupling is frozen and is
1497 ! carried by the descreened D^0 (PP_DIJ) added by the mesh nonlocal machinery.
1498 e_ref = m_zero
1499 if (.not. hxc_only_) then
1500 do k = 1, nr
1501 e_ref = e_ref + sqrt4pi*vref(k)*nlm_tot(k, 1)*this%vwr(k)
1502 end do
1503 ! add v_ref into the L=0 potential channel so it enters D (spherical -> Y_00 coeff)
1504 call lalg_axpy(nr, sqrt4pi, vref, vlm(:, 1))
1505 end if
1506
1507 ! D matrix from the Hartree + reference potential
1508 dij = m_zero
1509 do is = 1, this%spin_channels
1510 call paw_accumulate_dij(this, ae, .not. ae, vlm, dij(:,:,is))
1511 end do
1512
1513 ! Exchange-correlation
1514 if (this%xc_scheme == paw_xc_scheme_moments) then
1515 call paw_xc_moments(this, nlm_xc, ncore, ae, xc_with_comp, e_xc, dij)
1516 else
1517 call paw_xc(this, nlm_xc, nlm_xc_der, rho, ncore, ae, xc_with_comp, e_xc, dij)
1518 end if
1519
1520 energy = e_h + e_ref + e_xc
1521
1522 safe_deallocate_a(nlm_rho)
1523 safe_deallocate_a(nlm_xc)
1524 safe_deallocate_a(nlm_xc_der)
1525 safe_deallocate_a(nlm_tot)
1526 safe_deallocate_a(vlm)
1527 safe_deallocate_a(vref)
1528 safe_deallocate_a(ncore)
1529
1530 pop_sub_with_profile(paw_onsite_side)
1531 end subroutine paw_onsite_side
1532
1533 ! ---------------------------------------------------------
1544 subroutine paw_build_density(this, rho, ae, with_comp, nlm_rho, nlm_der)
1545 type(paw_sphere_t), intent(in) :: this
1546 real(real64), contiguous, intent(in) :: rho(:,:)
1547 logical, intent(in) :: ae
1548 logical, intent(in) :: with_comp
1549 real(real64), contiguous, intent(out) :: nlm_rho(:,:)
1550 real(real64), contiguous, optional, intent(out) :: nlm_der(:,:)
1551
1552 integer :: i, j, ij, ig
1553 real(real64) :: rho_ij
1554 real(real64), allocatable :: cmat(:,:)
1555
1556 push_sub_with_profile(paw_build_density)
1557
1558 assert(.not. present(nlm_der) .or. this%xc_is_gga)
1559
1560 ! Contract rho_ij with the packed Gaunt couplings into per-(pair, LM) coefficients
1561 ! n_LM(r) = sum_pair pair_X(r, pair) cmat(pair, LM) (+ the qaug compensation per L).
1562 ! The radial derivative uses the same coefficients with the derivative tables.
1563 safe_allocate(cmat(1:this%npair, 1:this%nlm_rho))
1564 cmat = m_zero
1565 do i = 1, this%nlm
1566 do j = i, this%nlm
1567 rho_ij = rho(i, j)
1568 if (j > i) rho_ij = rho_ij + rho(j, i)
1569 if (abs(rho_ij) <= m_epsilon) cycle
1570 ij = (i-1)*this%nlm + j
1571 do ig = this%gnt_ptr(ij), this%gnt_ptr(ij+1) - 1
1572 cmat(this%gnt_pair(ig), this%gnt_lm(ig)) = cmat(this%gnt_pair(ig), this%gnt_lm(ig)) &
1573 + rho_ij*this%gnt_c(ig)
1574 end do
1575 end do
1576 end do
1577
1578 if (ae) then
1579 call lalg_gemm(this%pair_ae, cmat, nlm_rho)
1580 if (present(nlm_der)) call lalg_gemm(this%dpair_ae, cmat, nlm_der)
1581 else
1582 call lalg_gemm(this%pair_ps, cmat, nlm_rho)
1583 if (present(nlm_der)) call lalg_gemm(this%dpair_ps, cmat, nlm_der)
1584 if (with_comp) then
1585 call paw_qaug_gemv(this, this%qaug, 'N', cmat, nlm_rho)
1586 if (present(nlm_der)) call paw_qaug_gemv(this, this%dqaug, 'N', cmat, nlm_der)
1587 end if
1588 end if
1589
1590 safe_deallocate_a(cmat)
1591
1592 pop_sub_with_profile(paw_build_density)
1593 end subroutine paw_build_density
1594
1595 ! ---------------------------------------------------------
1600 subroutine paw_qaug_gemv(this, qtab, trans, xx, yy)
1601 type(paw_sphere_t), intent(in) :: this
1602 real(real64), contiguous, intent(in) :: qtab(:, :, 0:)
1603 character(1), intent(in) :: trans
1604 real(real64), contiguous, intent(in) :: xx(:, :)
1605 real(real64), contiguous, intent(inout) :: yy(:, :)
1606
1607 integer :: lm, ll
1608
1609 ! no PUSH_SUB, light wrapper called in the SCF hot loops
1610
1611 do lm = 1, this%nlm_rho
1612 ll = int(sqrt(real(lm-1, real64)))
1613 if (ll > this%laug) cycle
1614 call blas_gemv(trans, this%nr, this%npair, m_one, qtab(1, 1, ll), this%nr, &
1615 xx(1, lm), 1, m_one, yy(1, lm), 1)
1616 end do
1617
1618 end subroutine paw_qaug_gemv
1619
1620 ! ---------------------------------------------------------
1623 subroutine paw_gaunt_scatter_dij(this, w, dij)
1624 type(paw_sphere_t), intent(in) :: this
1625 real(real64), contiguous, intent(in) :: w(:, :)
1626 real(real64), contiguous, intent(inout) :: dij(:, :)
1627
1628 integer :: i, j, ij, ig
1629 real(real64) :: acc
1630
1631 ! no PUSH_SUB, light scatter called in the SCF hot loops
1632
1633 do i = 1, this%nlm
1634 do j = i, this%nlm
1635 ij = (i-1)*this%nlm + j
1636 acc = m_zero
1637 do ig = this%gnt_ptr(ij), this%gnt_ptr(ij+1) - 1
1638 acc = acc + this%gnt_c(ig)*w(this%gnt_pair(ig), this%gnt_lm(ig))
1639 end do
1640 dij(i, j) = dij(i, j) + acc
1641 if (j > i) dij(j, i) = dij(j, i) + acc
1642 end do
1643 end do
1644
1645 end subroutine paw_gaunt_scatter_dij
1646
1647 ! ---------------------------------------------------------
1650 subroutine paw_emit_lm_columns(this, field_lm, tol, ncol, spl, ll, mm, rcut)
1651 type(paw_sphere_t), intent(in) :: this
1652 real(real64), contiguous, intent(in) :: field_lm(:, :)
1653 real(real64), intent(in) :: tol
1654 integer, intent(out) :: ncol
1655 type(spline_t), allocatable, intent(out) :: spl(:)
1656 integer, allocatable, intent(out) :: ll(:)
1657 integer, allocatable, intent(out) :: mm(:)
1658 real(real64), intent(out) :: rcut
1659
1660 integer :: lm, l0
1661
1662 push_sub(paw_emit_lm_columns)
1663
1664 safe_allocate(spl(1:this%nlm_rho))
1665 safe_allocate(ll(1:this%nlm_rho))
1666 safe_allocate(mm(1:this%nlm_rho))
1667 ncol = 0
1668 do lm = 1, this%nlm_rho
1669 if (maxval(abs(field_lm(:, lm))) <= tol) cycle
1670 ncol = ncol + 1
1671 l0 = int(sqrt(real(lm-1, real64)))
1672 ll(ncol) = l0
1673 mm(ncol) = lm - l0*(l0+1) - 1
1674 call spline_init(spl(ncol))
1675 call spline_fit(this%nr, this%r, field_lm(:, lm), spl(ncol))
1676 end do
1677 rcut = this%r(this%nr)
1678
1679 pop_sub(paw_emit_lm_columns)
1680 end subroutine paw_emit_lm_columns
1681
1682 ! ---------------------------------------------------------
1685 subroutine paw_partial_wave_derivatives(this, dphi_ae, dphi_ps)
1686 type(paw_sphere_t), intent(in) :: this
1687 real(real64), contiguous, intent(out) :: dphi_ae(:, :)
1688 real(real64), contiguous, intent(out) :: dphi_ps(:, :)
1689
1690 integer :: i
1691
1693
1694 do i = 1, this%nrad
1695 call paw_radial_derivative(this%r, this%phi_ae(:, i), dphi_ae(:, i))
1696 call paw_radial_derivative(this%r, this%phi_ps(:, i), dphi_ps(:, i))
1697 end do
1698
1700 end subroutine paw_partial_wave_derivatives
1701
1702 ! ---------------------------------------------------------
1707 subroutine paw_adjoint_divergence_add(this, flux, div, vfield)
1708 type(paw_sphere_t), intent(in) :: this
1709 real(real64), contiguous, intent(in) :: flux(:)
1710 real(real64), contiguous, intent(inout) :: div(:)
1711 real(real64), contiguous, intent(inout) :: vfield(:)
1712
1713 integer :: ip
1714
1715 ! no PUSH_SUB, called per spin and kernel channel
1716
1717 call paw_radial_derivative_t(this, flux, div)
1718 do ip = 1, this%nr
1719 if (this%vwr(ip) > m_zero) then
1720 vfield(ip) = vfield(ip) + div(ip)/this%vwr(ip)
1721 end if
1722 end do
1723
1724 end subroutine paw_adjoint_divergence_add
1725
1726 ! ---------------------------------------------------------
1728 subroutine paw_onsite_reorder_pmat(nprojs, map, src, dst)
1729 integer, intent(in) :: nprojs
1730 integer, intent(in) :: map(:)
1731 real(real64), contiguous, intent(in) :: src(:, :)
1732 real(real64), contiguous, intent(out) :: dst(:, :)
1733
1734 integer :: iproj, jproj
1735
1736 ! no PUSH_SUB, light reorder called per projector matrix
1737
1738 do jproj = 1, nprojs
1739 do iproj = 1, nprojs
1740 dst(map(iproj), map(jproj)) = src(iproj, jproj)
1741 end do
1742 end do
1743
1744 end subroutine paw_onsite_reorder_pmat
1745
1746 ! ---------------------------------------------------------
1750 subroutine paw_hartree_lm(this, lm, nl, vl)
1751 type(paw_sphere_t), intent(in) :: this
1752 integer, intent(in) :: lm
1753 real(real64), intent(in) :: nl(:)
1754 real(real64), intent(out) :: vl(:)
1755
1756 integer :: k, nr, ll
1757 real(real64) :: pref, inner, outer, ti, to
1758 real(real64), allocatable :: cum_in(:)
1759
1760 push_sub_with_profile(paw_hartree_lm)
1761
1762 nr = this%nr
1763 ll = int(sqrt(real(lm-1, real64)))
1764 pref = m_four*m_pi/real(2*ll+1, real64)
1765
1766 safe_allocate(cum_in(1:nr))
1767 ! forward cumulative inner integral, with half-diagonal (midpoint) correction
1768 inner = m_zero
1769 do k = 1, nr
1770 ti = nl(k)*this%r(k)**(ll+2)*this%wr(k)
1771 inner = inner + ti
1772 cum_in(k) = inner - m_half*ti
1773 end do
1774 ! backward cumulative outer integral
1775 vl = m_zero
1776 outer = m_zero
1777 do k = nr, 1, -1
1778 if (this%r(k) > m_zero) then
1779 to = nl(k)*this%r(k)**(1-ll)*this%wr(k)
1780 else
1781 to = m_zero
1782 end if
1783 outer = outer + to
1784 if (this%r(k) > m_zero) then
1785 vl(k) = pref*( cum_in(k)/this%r(k)**(ll+1) + (outer - m_half*to)*this%r(k)**ll )
1786 end if
1787 end do
1788 ! origin: extrapolate
1789 if (this%r(1) <= m_zero) vl(1) = vl(2)
1790
1791 safe_deallocate_a(cum_in)
1792
1793 pop_sub_with_profile(paw_hartree_lm)
1794 end subroutine paw_hartree_lm
1795
1796 ! ---------------------------------------------------------
1818 subroutine paw_xc(this, nlm_rho, nlm_der, rho, ncore, ae, with_comp, e_xc, dij)
1819 type(paw_sphere_t), target, intent(in) :: this
1820 real(real64), contiguous, intent(in) :: nlm_rho(:,:,:) ! (nr, nlm_rho, nspin)
1822 real(real64), optional, contiguous, intent(in) :: nlm_der(:,:,:)
1823 real(real64), contiguous, intent(in) :: rho(:,:,:) ! (nlm, nlm, nspin) density matrix (meta-GGA)
1824 real(real64), contiguous, intent(in) :: ncore(:) ! (nr) core density per spin channel
1825 logical, intent(in) :: ae
1826 logical, intent(in) :: with_comp
1827 real(real64), intent(out) :: e_xc
1828 real(real64), contiguous, intent(inout) :: dij(:,:,:) ! (nlm, nlm, nspin)
1829
1830 logical :: gga, mgga, mgga_exc, needs_lapl
1831 integer :: np, npa, ibase, ip, ia, is, nsp, nsig
1832 real(real64) :: ntot
1833 real(real64), allocatable :: ncder(:) ! (np) core density gradient per spin channel
1834 ! Grid-major layout: the radial index is the leading (contiguous) dimension, the spin index is
1835 ! last. The density is built at ALL angular nodes first (radial point (ip, ia) at flat index
1836 ! (ia-1)*np + ip) so LibXC is evaluated ONCE on the np*nang points.
1837 real(real64), allocatable :: dens(:,:) ! (np*nang, nspin) density, native spin components
1838 real(real64), allocatable :: densp(:,:) ! (np*nang, nsp) density in the LibXC spin channels
1839 real(real64), allocatable :: sigma(:,:) ! (np*nang, nsig)
1840 real(real64), allocatable :: dpol(:) ! (np*nang) noncollinear polarization magnitude
1841 real(real64), allocatable :: exc(:), vs(:,:)
1842 real(real64), allocatable, target :: vr(:,:) ! (np*nang, nspin) potential
1843 real(real64), allocatable :: g(:,:,:,:) ! (np, nspin, nang, 3) grad n per native component
1844 real(real64), allocatable :: gp(:,:,:,:) ! (np, nsp, nang, 3) grad n per channel
1845 real(real64), allocatable :: vxc_lm(:,:,:) ! (np, nlm_rho, nspin) projected v_xc
1846 real(real64), allocatable :: tau(:,:), lapl(:,:) ! (np*nang, nsp) meta-GGA tau (no 1/2) and Laplacian
1847 real(real64), allocatable :: vtau(:,:), vlapl(:,:) ! (np*nang, nsp) dE/dtau and dE/d(lapl n)
1848 real(real64), pointer :: phi_pw(:,:), dphi_pw(:,:), d2phi_pw(:,:) ! partial waves + derivatives, this side (meta-GGA)
1849 real(real64), pointer :: vmat(:,:) ! (np, nang) matrix view of one potential component
1850
1851 push_sub_with_profile(paw_xc)
1852
1853 gga = this%xc_is_gga
1854 mgga = this%xc_is_mgga
1855 needs_lapl = this%xc_needs_laplacian
1856 np = this%nr
1857 npa = np*this%nang
1858 nsp = this%spin_channels
1859 nsig = 2*nsp - 1 ! 1 for one spin channel, 3 for two (collinear or projected)
1860
1861 safe_allocate(dens(1:npa, 1:this%nspin))
1862 safe_allocate(densp(1:npa, 1:nsp))
1863 safe_allocate(sigma(1:npa, 1:nsig))
1864 safe_allocate(exc(1:npa))
1865 safe_allocate(vr(1:npa, 1:this%nspin))
1866 safe_allocate(vs(1:npa, 1:nsig))
1867 sigma = m_zero
1868
1869 safe_allocate(dpol(1:npa))
1870 if (mgga) then
1871 ! The density is built below together with its gradient and tau (collinear only).
1872 dpol = m_zero
1873 safe_allocate(tau(1:npa, 1:nsp))
1874 safe_allocate(lapl(1:npa, 1:nsp))
1875 if (ae) then
1876 phi_pw => this%phi_ae
1877 dphi_pw => this%dphi_ae
1878 if (needs_lapl) d2phi_pw => this%d2phi_ae
1879 else
1880 phi_pw => this%phi_ps
1881 dphi_pw => this%dphi_ps
1882 if (needs_lapl) d2phi_pw => this%d2phi_ps
1883 end if
1884 else
1885 call paw_xc_density(this, nlm_rho, ncore, dens, densp, dpol)
1886 end if
1887
1888 if (gga) then
1889 safe_allocate(ncder(1:np))
1890 if (ae) then
1891 ncder(:) = this%nc_ae_der(:)/this%spin_channels
1892 else
1893 ncder(:) = this%nc_ps_der(:)/this%spin_channels
1894 end if
1895 safe_allocate(g(1:np, 1:this%nspin, 1:this%nang, 1:3))
1896 safe_allocate(gp(1:np, 1:nsp, 1:this%nang, 1:3))
1897 if (mgga) then
1898 ! density + grad(n) + tau (+ Laplacian) in one shared partial-wave loop
1899 if (needs_lapl) then
1900 call paw_xc_quantities(this, rho, ae, phi_pw, dphi_pw, d2phi=d2phi_pw, &
1901 ncore=ncore, ncder=ncder, dens=dens, gn=g, tau=tau, lapl=lapl)
1902 else
1903 call paw_xc_quantities(this, rho, ae, phi_pw, dphi_pw, &
1904 ncore=ncore, ncder=ncder, dens=dens, gn=g, tau=tau)
1905 lapl = m_zero
1906 end if
1907 densp(1:npa, 1:nsp) = dens(1:npa, 1:nsp)
1908 else
1909 assert(present(nlm_der))
1910 call paw_xc_gradient(this, nlm_rho, nlm_der, ncder, g)
1911 end if
1912 call paw_xc_sigma(this, dens, dpol, g, gp, sigma)
1913 end if
1914
1915 ! One LibXC evaluation for all np*nang points
1916 if (mgga) then
1917 safe_allocate(vtau(1:npa, 1:nsp))
1918 safe_allocate(vlapl(1:npa, 1:nsp))
1919 call paw_xc_eval(this, npa, nsp, nsig, densp, sigma, exc, vr(:, 1:nsp), vs, &
1920 lapl=lapl, tau=tau, vlapl=vlapl, vtau=vtau, mgga_exc=mgga_exc)
1921 else
1922 mgga_exc = .false.
1923 call paw_xc_eval(this, npa, nsp, nsig, densp, sigma, exc, vr(:, 1:nsp), vs)
1924 end if
1925
1926 ! Accumulate the energy per angular node (exc already sums only the energy functionals)
1927 e_xc = m_zero
1928 !$omp parallel do schedule(static) private(ibase, ip, is, ntot) reduction(+:e_xc)
1929 do ia = 1, this%nang
1930 ibase = (ia - 1)*np
1931 do ip = 1, np
1932 ntot = m_zero
1933 do is = 1, nsp
1934 ntot = ntot + max(densp(ibase + ip, is), m_zero)
1935 end do
1936 e_xc = e_xc + ntot*exc(ibase + ip)*this%vwr(ip)*this%wang(ia)
1937 end do
1938 end do
1939
1940 ! --- D matrix contribution (per native spin component) ---
1941 if (mgga) then
1942 ! Unified meta-GGA D = int [ vrho dn/drho + vsigma dsigma/drho + vtau dtau/drho + vlapl d(lapl n)/drho ],
1943 ! all built from the partial-wave gradients so it is the exact derivative of the discrete energy. vtau
1944 ! is passed only for an energy functional; a potential-only meta-GGA (TB09) omits it and carries its
1945 ! whole potential in the local vrho. vlapl (d2phi) is passed only when the functional needs the Laplacian.
1946 if (mgga_exc .and. needs_lapl) then
1947 call paw_xc_mgga_dij(this, phi_pw, dphi_pw, g, vr, vs, dij, vtau=vtau, d2phi=d2phi_pw, vlapl=vlapl)
1948 else if (mgga_exc) then
1949 call paw_xc_mgga_dij(this, phi_pw, dphi_pw, g, vr, vs, dij, vtau=vtau)
1950 else if (needs_lapl) then
1951 call paw_xc_mgga_dij(this, phi_pw, dphi_pw, g, vr, vs, dij, d2phi=d2phi_pw, vlapl=vlapl)
1952 else
1953 call paw_xc_mgga_dij(this, phi_pw, dphi_pw, g, vr, vs, dij)
1954 end if
1955 else if (gga) then
1956 call paw_xc_gga_process(this, ae, with_comp, dens, g, gp, vr, vs, dij)
1957 else
1958 ! Rotate the potential back
1959 call paw_xc_lda_process(this, dens, vr)
1960 ! Project onto the Y_LM basis as a matrix product of the grid-major potential
1961 ! with the quadrature-weighted harmonics
1962 safe_allocate(vxc_lm(1:np, 1:this%nlm_rho, 1:this%nspin))
1963 do is = 1, this%nspin
1964 vmat(1:np, 1:this%nang) => vr(:, is)
1965 call lalg_gemm(np, this%nlm_rho, this%nang, m_one, vmat, this%wylm, m_zero, vxc_lm(:, :, is))
1966 end do
1967 do is = 1, this%nspin
1968 call paw_accumulate_dij(this, ae, with_comp, vxc_lm(:,:,is), dij(:,:,is))
1969 end do
1970 end if
1971
1972 safe_deallocate_a(dens)
1973 safe_deallocate_a(densp)
1974 safe_deallocate_a(sigma)
1975 safe_deallocate_a(dpol)
1976 safe_deallocate_a(exc)
1977 safe_deallocate_a(vr)
1978 safe_deallocate_a(vs)
1979 safe_deallocate_a(ncder)
1980 safe_deallocate_a(g)
1981 safe_deallocate_a(gp)
1982 safe_deallocate_a(vxc_lm)
1983 safe_deallocate_a(tau)
1984 safe_deallocate_a(vtau)
1985 safe_deallocate_a(lapl)
1986 safe_deallocate_a(vlapl)
1987
1988 pop_sub_with_profile(paw_xc)
1989 end subroutine paw_xc
1990
1991 ! ---------------------------------------------------------
1995 subroutine paw_xc_density(this, nlm_rho, ncore, dens, densp, dpol)
1996 type(paw_sphere_t), intent(in) :: this
1997 real(real64), intent(in) :: nlm_rho(:,:,:)
1998 real(real64), intent(in) :: ncore(:)
1999 real(real64), contiguous, intent(out) :: dens(:,:) ! (np*nang, nspin)
2000 real(real64), contiguous, intent(out) :: densp(:,:) ! (np*nang, nsp)
2001 real(real64), contiguous, intent(out) :: dpol(:) ! (np*nang) |m|, noncollinear only
2002
2003 integer :: np, ibase, ip, ia, ic
2004
2005 push_sub_with_profile(paw_xc_density)
2006
2007 np = this%nr
2008
2009 ! For angular node ia the np radial points live in the flat segment [ibase+1, ibase+np],
2010 ! ibase = (ia-1)*np; every node writes disjoint segments.
2011 do ia = 1, this%nang
2012 ibase = (ia - 1)*np
2013 do ic = 1, this%nspin
2014 if (ic <= 2) then
2015 do ip = 1, np
2016 dens(ibase + ip, ic) = ncore(ip)
2017 end do
2018 else
2019 do ip = 1, np
2020 dens(ibase + ip, ic) = m_zero
2021 end do
2022 end if
2023 call blas_gemv('N', np, this%nlm_rho, m_one, nlm_rho(1, 1, ic), np, &
2024 this%ylm(ia, 1), this%nang, m_one, dens(ibase + 1, ic), 1)
2025 end do
2026 end do
2027
2028 if (this%nspin == 4) then
2029 !$omp parallel do schedule(static)
2030 do ip = 1, np*this%nang
2031 dpol(ip) = paw_xc_noncol_dpol(dens(ip, 1), dens(ip, 2), dens(ip, 3), dens(ip, 4))
2032 densp(ip, 1) = m_half*(dens(ip, 1) + dens(ip, 2) + dpol(ip))
2033 densp(ip, 2) = m_half*(dens(ip, 1) + dens(ip, 2) - dpol(ip))
2034 end do
2035 else
2036 do ic = 1, this%spin_channels
2037 call lalg_copy(np*this%nang, dens(:, ic), densp(:, ic))
2038 end do
2039 end if
2040
2041 pop_sub_with_profile(paw_xc_density)
2042 end subroutine paw_xc_density
2043
2044 ! ---------------------------------------------------------
2052 subroutine paw_xc_gradient(this, nlm_rho, nlm_der, ncder, g)
2053 type(paw_sphere_t), intent(in) :: this
2054 real(real64), contiguous, intent(in) :: nlm_rho(:,:,:) ! (nr, nlm_rho, nspin)
2055 real(real64), contiguous, intent(in) :: nlm_der(:,:,:) ! (nr, nlm_rho, nspin) d n_LM/dr
2056 real(real64), contiguous, intent(in) :: ncder(:) ! (nr) core density gradient per spin channel
2057 real(real64), contiguous, intent(out) :: g(:,:,:,:) ! (nr, nspin, nang, 3)
2058
2059 integer :: np, ip, ia, ic
2060
2061 push_sub_with_profile(paw_xc_gradient)
2062
2063 np = this%nr
2064
2065 do ia = 1, this%nang
2066 do ic = 1, this%nspin
2067 if (ic <= 2) then
2068 do ip = 1, np
2069 g(ip, ic, ia, 1) = ncder(ip) ! core is spherical: only the radial gradient
2070 end do
2071 else
2072 do ip = 1, np
2073 g(ip, ic, ia, 1) = m_zero
2074 end do
2075 end if
2076 call blas_gemv('N', np, this%nlm_rho, m_one, nlm_der(1, 1, ic), np, &
2077 this%ylm(ia, 1), this%nang, m_one, g(1, ic, ia, 1), 1)
2078 call blas_gemv('N', np, this%nlm_rho, m_one, nlm_rho(1, 1, ic), np, &
2079 this%dylm_dth(ia, 1), this%nang, m_zero, g(1, ic, ia, 2), 1)
2080 call blas_gemv('N', np, this%nlm_rho, m_one, nlm_rho(1, 1, ic), np, &
2081 this%dylm_dph(ia, 1), this%nang, m_zero, g(1, ic, ia, 3), 1)
2082 do ip = 1, np
2083 g(ip, ic, ia, 2) = g(ip, ic, ia, 2)*this%invr(ip)
2084 g(ip, ic, ia, 3) = g(ip, ic, ia, 3)*this%invr(ip)
2085 end do
2086 end do
2087 end do
2088
2089 pop_sub_with_profile(paw_xc_gradient)
2090 end subroutine paw_xc_gradient
2091
2092 ! ---------------------------------------------------------
2096 subroutine paw_xc_sigma(this, dens, dpol, g, gp, sigma)
2097 type(paw_sphere_t), intent(in) :: this
2098 real(real64), contiguous, intent(in) :: dens(:,:)
2099 real(real64), contiguous, intent(in) :: dpol(:) ! (np*nang) |m|, from paw_xc_density
2100 real(real64), contiguous, intent(in) :: g(:,:,:,:)
2101 real(real64), contiguous, intent(out) :: gp(:,:,:,:) ! (np, nsp, nang, 3)
2102 real(real64), contiguous, intent(out) :: sigma(:,:) ! (np*nang, nsig)
2103
2104 integer :: np, ibase, ip, ia, k
2105 real(real64) :: uu, gq
2106
2107 push_sub_with_profile(paw_xc_sigma)
2108
2109 np = this%nr
2110
2111 if (this%nspin == 4) then
2112 !$omp parallel do schedule(static) private(ibase, ip, uu, gq, k)
2113 do ia = 1, this%nang
2114 ibase = (ia - 1)*np
2115 do ip = 1, np
2116 uu = dens(ibase + ip, 1) - dens(ibase + ip, 2)
2117 do k = 1, 3
2118 if (dpol(ibase + ip) > m_zero) then
2119 gq = (uu*(g(ip, 1, ia, k) - g(ip, 2, ia, k)) &
2120 + m_four*(dens(ibase + ip, 3)*g(ip, 3, ia, k) &
2121 + dens(ibase + ip, 4)*g(ip, 4, ia, k)))/dpol(ibase + ip)
2122 else
2123 gq = m_zero
2124 end if
2125 gp(ip, 1, ia, k) = m_half*(g(ip, 1, ia, k) + g(ip, 2, ia, k) + gq)
2126 gp(ip, 2, ia, k) = m_half*(g(ip, 1, ia, k) + g(ip, 2, ia, k) - gq)
2127 end do
2128 end do
2129 end do
2130 else
2131 gp(:, :, :, :) = g(:, :, :, :)
2132 end if
2133
2134 !$omp parallel do schedule(static) private(ibase, ip)
2135 do ia = 1, this%nang
2136 ibase = (ia - 1)*np
2137 do ip = 1, np
2138 sigma(ibase + ip, 1) = gp(ip,1,ia,1)**2 + gp(ip,1,ia,2)**2 + gp(ip,1,ia,3)**2
2139 if (this%spin_channels == 2) then
2140 sigma(ibase + ip, 2) = gp(ip,1,ia,1)*gp(ip,2,ia,1) + gp(ip,1,ia,2)*gp(ip,2,ia,2) &
2141 + gp(ip,1,ia,3)*gp(ip,2,ia,3)
2142 sigma(ibase + ip, 3) = gp(ip,2,ia,1)**2 + gp(ip,2,ia,2)**2 + gp(ip,2,ia,3)**2
2143 end if
2144 end do
2145 end do
2146
2147 pop_sub_with_profile(paw_xc_sigma)
2148 end subroutine paw_xc_sigma
2149
2150 ! ---------------------------------------------------------
2152 subroutine paw_xc_lda_process(this, dens, vr)
2153 type(paw_sphere_t), intent(in) :: this
2154 real(real64), contiguous, intent(in) :: dens(:,:)
2155 real(real64), contiguous, intent(inout) :: vr(:,:) ! (np*nang, nspin)
2156
2157 integer :: ip, ic
2158 real(real64) :: dpol, fwp(4, 2), v1, v2
2159
2160 if (this%nspin /= 4) return
2161
2162 push_sub_with_profile(paw_xc_lda_process)
2163
2164 !$omp parallel do schedule(static) private(dpol, fwp, v1, v2, ic)
2165 do ip = 1, this%nr*this%nang
2166 call paw_xc_noncol_weights(dens(ip, 1), dens(ip, 2), dens(ip, 3), dens(ip, 4), dpol, fwp)
2167 v1 = vr(ip, 1)
2168 v2 = vr(ip, 2)
2169 do ic = 1, 4
2170 vr(ip, ic) = fwp(ic, 1)*v1 + fwp(ic, 2)*v2
2171 end do
2172 end do
2173
2174 pop_sub_with_profile(paw_xc_lda_process)
2175 end subroutine paw_xc_lda_process
2176
2177 ! ---------------------------------------------------------
2179 subroutine paw_xc_gga_process(this, ae, with_comp, dens, g, gp, vr, vs, dij)
2180 type(paw_sphere_t), intent(in) :: this
2181 logical, intent(in) :: ae
2182 logical, intent(in) :: with_comp
2183 real(real64), contiguous, intent(in) :: dens(:,:)
2184 real(real64), contiguous, intent(in) :: g(:,:,:,:), gp(:,:,:,:)
2185 real(real64), contiguous, intent(in) :: vr(:,:), vs(:,:)
2186 real(real64), contiguous, intent(inout) :: dij(:,:,:)
2187
2188 integer :: np, ibase, ip, ia, is, os, iss
2189 real(real64), allocatable :: a1(:,:,:), a2(:,:,:), a3(:,:,:), a4(:,:,:)
2190
2191 push_sub_with_profile(paw_xc_gga_process)
2192
2193 if (this%nspin == 4) then
2194 call paw_xc_dij_gga_noncollinear(this, ae, with_comp, dens, g, gp, vr, vs, dij)
2195 pop_sub_with_profile(paw_xc_gga_process)
2196 return
2197 end if
2198
2199 np = this%nr
2200 safe_allocate(a1(1:np, 1:this%nang, 1:this%nspin))
2201 safe_allocate(a2(1:np, 1:this%nang, 1:this%nspin))
2202 safe_allocate(a3(1:np, 1:this%nang, 1:this%nspin))
2203 safe_allocate(a4(1:np, 1:this%nang, 1:this%nspin))
2204
2205 !$omp parallel private(ibase, ip, os, iss)
2206 do is = 1, this%nspin
2207 !$omp do schedule(static)
2208 do ia = 1, this%nang
2209 ibase = (ia - 1)*np
2210 os = min(this%nspin, 3 - is) ! opposite spin (equals is for nspin == 1)
2211 iss = 2*is - 1 ! sigma channel (s, s): 1 = 11, 3 = 22
2212 if (this%nspin == 1) then
2213 do ip = 1, np
2214 a1(ip, ia, 1) = vr(ibase + ip, 1)
2215 a2(ip, ia, 1) = m_two*vs(ibase + ip, 1)*gp(ip, 1, ia, 1)
2216 a3(ip, ia, 1) = m_two*vs(ibase + ip, 1)*gp(ip, 1, ia, 2)
2217 a4(ip, ia, 1) = m_two*vs(ibase + ip, 1)*gp(ip, 1, ia, 3)
2218 end do
2219 else
2220 do ip = 1, np
2221 a1(ip, ia, is) = vr(ibase + ip, is)
2222 a2(ip, ia, is) = m_two*vs(ibase + ip, iss)*gp(ip, is, ia, 1) + vs(ibase + ip, 2)*gp(ip, os, ia, 1)
2223 a3(ip, ia, is) = m_two*vs(ibase + ip, iss)*gp(ip, is, ia, 2) + vs(ibase + ip, 2)*gp(ip, os, ia, 2)
2224 a4(ip, ia, is) = m_two*vs(ibase + ip, iss)*gp(ip, is, ia, 3) + vs(ibase + ip, 2)*gp(ip, os, ia, 3)
2225 end do
2226 end if
2227 end do
2228 end do
2229 !$omp end parallel
2230
2231 call paw_xc_dij_gga_fields(this, ae, with_comp, a1, a2, a3, a4, dij)
2232
2233 safe_deallocate_a(a1)
2234 safe_deallocate_a(a2)
2235 safe_deallocate_a(a3)
2236 safe_deallocate_a(a4)
2237
2238 pop_sub_with_profile(paw_xc_gga_process)
2239 end subroutine paw_xc_gga_process
2240
2241 ! ---------------------------------------------------------
2245 subroutine paw_xc_eval(this, np, nsp, nsig, dens, sigma, exc, vrho, vsigma, lapl, tau, vlapl, vtau, mgga_exc)
2246 type(paw_sphere_t), intent(in) :: this
2247 integer, intent(in) :: np, nsp, nsig
2248 real(real64), contiguous, intent(in) :: dens(:,:) ! (np, nsp) grid-major
2249 real(real64), contiguous, intent(in) :: sigma(:,:) ! (np, nsig) grid-major
2250 real(real64), contiguous, intent(out) :: exc(:) ! (np)
2251 real(real64), contiguous, intent(out) :: vrho(:,:) ! (np, nsp) grid-major
2252 real(real64), contiguous, intent(out) :: vsigma(:,:) ! (np, nsig) grid-major
2253 real(real64), contiguous, optional, intent(in) :: lapl(:,:) ! (np, nsp) meta-GGA density Laplacian
2254 real(real64), contiguous, optional, intent(in) :: tau(:,:) ! (np, nsp) meta-GGA tau (octopus, no 1/2)
2255 real(real64), contiguous, optional, intent(out) :: vlapl(:,:) ! (np, nsp) dE/d(lapl n)
2256 real(real64), contiguous, optional, intent(out) :: vtau(:,:) ! (np, nsp) dE/dtau (octopus convention)
2258 logical, optional, intent(out) :: mgga_exc
2259
2260 integer, parameter :: block_size = 4096
2261 integer :: ib, nblock, ixc
2262
2263 push_sub_with_profile(paw_xc_eval)
2264
2265 assert(present(tau) .eqv. this%xc_is_mgga)
2266
2267 if (present(mgga_exc)) then
2268 mgga_exc = .false.
2269 do ixc = 1, 2
2270 if (this%xc_functl(ixc)%family /= xc_family_none .and. &
2271 xc_functional_is_energy_functional(this%xc_functl(ixc))) mgga_exc = .true.
2272 end do
2273 end if
2274
2275 nblock = (np + block_size - 1)/block_size
2276
2277 !$omp parallel do schedule(dynamic)
2278 do ib = 1, nblock
2279 call paw_xc_eval_block(this, np, (ib - 1)*block_size, min(block_size, np - (ib - 1)*block_size), &
2280 nsp, nsig, dens, sigma, exc, vrho, vsigma, lapl, tau, vlapl, vtau)
2281 end do
2282
2283 pop_sub_with_profile(paw_xc_eval)
2284 end subroutine paw_xc_eval
2285
2286 ! ---------------------------------------------------------
2288 subroutine paw_xc_eval_block(this, np, ip0, bs, nsp, nsig, dens, sigma, exc, vrho, vsigma, lapl, tau, vlapl, vtau)
2289 type(paw_sphere_t), intent(in) :: this
2290 integer, intent(in) :: np, ip0, bs, nsp, nsig
2291 real(real64), contiguous, intent(in) :: dens(:,:) ! (np, nsp) grid-major
2292 real(real64), contiguous, intent(in) :: sigma(:,:) ! (np, nsig) grid-major
2293 real(real64), contiguous, intent(out) :: exc(:) ! (np)
2294 real(real64), contiguous, intent(out) :: vrho(:,:) ! (np, nsp) grid-major
2295 real(real64), contiguous, intent(out) :: vsigma(:,:) ! (np, nsig) grid-major
2296 real(real64), contiguous, optional, intent(in) :: lapl(:,:) ! (np, nsp) grid-major
2297 real(real64), contiguous, optional, intent(in) :: tau(:,:) ! (np, nsp) grid-major, no 1/2
2298 real(real64), contiguous, optional, intent(out) :: vlapl(:,:) ! (np, nsp) grid-major
2299 real(real64), contiguous, optional, intent(out) :: vtau(:,:) ! (np, nsp) grid-major, no 1/2
2300
2301 integer :: ixc, ip, is
2302 logical :: mgga, is_efunc
2303 real(real64) :: ntot
2304 real(real64) :: dclamp(nsp, bs), ssig(nsig, bs)
2305 real(real64) :: eb(bs), vb(nsp, bs), vsb(nsig, bs)
2306 real(real64) :: ez(bs), vz(nsp, bs), vsz(nsig, bs)
2307 ! meta-GGA only: LibXC inputs (tau with the 1/2 convention) and outputs
2308 real(real64) :: lb(nsp, bs), tb(nsp, bs), vlb(nsp, bs), vtb(nsp, bs), vlz(nsp, bs), vtz(nsp, bs)
2309
2310 ! no PUSH_SUB, called inside an OpenMP region
2311
2312 mgga = present(tau)
2313
2314 do ip = 1, bs
2315 do is = 1, nsp
2316 dclamp(is, ip) = max(dens(ip0 + ip, is), m_zero)
2317 end do
2318 end do
2319 if (this%xc_is_gga) then
2320 do ip = 1, bs
2321 do is = 1, nsig
2322 ssig(is, ip) = sigma(ip0 + ip, is)
2323 end do
2324 end do
2325 end if
2326 if (mgga) then
2327 do ip = 1, bs
2328 do is = 1, nsp
2329 lb(is, ip) = lapl(ip0 + ip, is)
2330 tb(is, ip) = m_half*tau(ip0 + ip, is)
2331 end do
2332 end do
2333 vlb = m_zero
2334 vtb = m_zero
2335 end if
2336
2337 eb = m_zero
2338 vb = m_zero
2339 vsb = m_zero
2340
2341 do ixc = 1, 2
2342 is_efunc = xc_functional_is_energy_functional(this%xc_functl(ixc))
2343 select case (this%xc_functl(ixc)%family)
2344 case (xc_family_none)
2345 cycle
2346 case (xc_family_lda)
2347 call xc_f03_lda_exc_vxc(this%xc_functl(ixc)%conf, int(bs, c_size_t), dclamp, ez, vz)
2348 case (xc_family_gga)
2349 call xc_f03_gga_exc_vxc(this%xc_functl(ixc)%conf, int(bs, c_size_t), dclamp, ssig, &
2350 ez, vz, vsz)
2351 case (xc_family_mgga)
2352 if (is_efunc) then
2353 call xc_f03_mgga_exc_vxc(this%xc_functl(ixc)%conf, int(bs, c_size_t), dclamp, ssig, lb, tb, &
2354 ez, vz, vsz, vlz, vtz)
2355 else
2356 ! potential-only meta-GGA (e.g. TB09): the model potential is returned in vz
2357 call xc_f03_mgga_vxc(this%xc_functl(ixc)%conf, int(bs, c_size_t), dclamp, ssig, lb, tb, &
2358 vz, vsz, vlz, vtz)
2359 end if
2360 case default
2361 message(1) = "PAW one-center engine: unsupported XC functional family."
2362 call messages_fatal(1)
2363 end select
2364 ! plain loops: this routine runs inside the OpenMP region of paw_xc_eval, where the
2365 ! lalg wrappers may not be called
2366 ! vrho (the local potential) always; the energy and the functional-derivative couplings only
2367 ! for energy functionals.
2368 do ip = 1, bs
2369 do is = 1, nsp
2370 vb(is, ip) = vb(is, ip) + vz(is, ip)
2371 end do
2372 end do
2373 if (.not. is_efunc) cycle
2374 do ip = 1, bs
2375 eb(ip) = eb(ip) + ez(ip)
2376 end do
2377 if (this%xc_functl(ixc)%family == xc_family_gga .or. this%xc_functl(ixc)%family == xc_family_mgga) then
2378 do ip = 1, bs
2379 do is = 1, nsig
2380 vsb(is, ip) = vsb(is, ip) + vsz(is, ip)
2381 end do
2382 end do
2383 end if
2384 if (mgga) then
2385 ! LibXC dE/dtau -> octopus vtau (no 1/2): factor 1/2 from the tau convention
2386 do ip = 1, bs
2387 do is = 1, nsp
2388 vlb(is, ip) = vlb(is, ip) + vlz(is, ip)
2389 vtb(is, ip) = vtb(is, ip) + m_half*vtz(is, ip)
2390 end do
2391 end do
2392 end if
2393 end do
2394
2395 ! The energy is evaluated with the clamped density, which does not depend on a spin
2396 ! density where it is negative: the corresponding vrho is zeroed for D to stay the
2397 ! derivative of the energy.
2398 ! The points with a negligible total density are then dropped altogether. This
2399 ! hard cutoff makes E_xc discontinuous in rho_ij, but only at a density where the energy
2400 ! density is itself negligible. The results are finally transposed back.
2401 do ip = 1, bs
2402 ntot = m_zero
2403 do is = 1, nsp
2404 if (dens(ip0 + ip, is) < m_zero) vb(is, ip) = m_zero
2405 ntot = ntot + dclamp(is, ip)
2406 end do
2407 if (ntot < m_epsilon) then
2408 eb(ip) = m_zero
2409 vb(:, ip) = m_zero
2410 vsb(:, ip) = m_zero
2411 if (mgga) then
2412 vlb(:, ip) = m_zero
2413 vtb(:, ip) = m_zero
2414 end if
2415 end if
2416 exc(ip0 + ip) = eb(ip)
2417 do is = 1, nsp
2418 vrho(ip0 + ip, is) = vb(is, ip)
2419 end do
2420 do is = 1, nsig
2421 vsigma(ip0 + ip, is) = vsb(is, ip)
2422 end do
2423 if (mgga) then
2424 do is = 1, nsp
2425 vlapl(ip0 + ip, is) = vlb(is, ip)
2426 vtau(ip0 + ip, is) = vtb(is, ip)
2427 end do
2428 end if
2429 end do
2430
2431 end subroutine paw_xc_eval_block
2432
2433 ! ---------------------------------------------------------
2446 subroutine paw_xc_quantities(this, rho, ae, phi, dphi, d2phi, ncore, ncder, dens, gn, tau, lapl)
2447 type(paw_sphere_t), target, intent(in) :: this
2448 real(real64), contiguous, intent(in) :: rho(:,:,:) ! (nlm, nlm, nspin) density matrix
2449 logical, intent(in) :: ae
2450 real(real64), contiguous, intent(in) :: phi(:,:) ! (np, nrad) partial waves for this side
2451 real(real64), contiguous, intent(in) :: dphi(:,:) ! (np, nrad) first radial derivatives
2452 real(real64), contiguous, optional, intent(in) :: d2phi(:,:) ! (np, nrad) second radial derivatives (lapl)
2453 real(real64), contiguous, optional, intent(in) :: ncore(:) ! (np) core density per spin channel (dens)
2454 real(real64), contiguous, optional, intent(in) :: ncder(:) ! (np) core radial gradient per spin channel (grad n)
2455 real(real64), contiguous, optional, intent(out) :: dens(:,:) ! (np*nang, nsp)
2456 real(real64), contiguous, optional, intent(out) :: gn(:,:,:,:) ! (np, nspin, nang, 3) grad n
2457 real(real64), contiguous, optional, intent(out) :: tau(:,:) ! (np*nang, nsp) tau, no 1/2
2458 real(real64), contiguous, optional, intent(out) :: lapl(:,:) ! (np*nang, nsp)
2459
2460 integer :: np, nsp, is, i, j, ip, ia, ri, rj, lmi, lmj, ibase
2461 real(real64) :: rho_ij, yi, yj, dti, dtj, dpi, dpj, cr, ct, cp, cv, cl, pij, rp, rpp
2462 logical :: ld, lg, lt, ll
2463 real(real64), pointer :: tcore(:), nclapl(:)
2464 real(real64), parameter :: tol = 1.0e-14_real64
2465
2466 push_sub_with_profile(paw_xc_quantities)
2467
2468 np = this%nr
2469 nsp = this%spin_channels
2470 ld = present(dens)
2471 lg = present(gn)
2472 lt = present(tau)
2473 ll = present(lapl)
2474 if (ae) then
2475 tcore => this%tau_c_ae ! already per spin channel
2476 nclapl => this%nc_ae_lapl
2477 else
2478 tcore => this%tau_c_ps
2479 nclapl => this%nc_ps_lapl
2480 end if
2481
2482 ! Zero every native component, then add the spherical (frozen) core to the diagonal channels only
2483 ! (the core is spin unpolarized). The off-diagonal slabs (noncollinear, ic = 3, 4) get no core.
2484 if (ld) dens(1:np*this%nang, 1:this%nspin) = m_zero
2485 if (lg) gn(1:np, 1:this%nspin, 1:this%nang, 1:3) = m_zero
2486 if (lt) tau(1:np*this%nang, 1:this%nspin) = m_zero
2487 if (ll) lapl(1:np*this%nang, 1:this%nspin) = m_zero
2488 do is = 1, nsp
2489 do ia = 1, this%nang
2490 ibase = (ia - 1)*np
2491 do ip = 1, np
2492 if (ld) dens(ibase + ip, is) = ncore(ip)
2493 if (lg) gn(ip, is, ia, 1) = ncder(ip)
2494 if (lt) tau(ibase + ip, is) = tcore(ip)
2495 if (ll) lapl(ibase + ip, is) = nclapl(ip)
2496 end do
2497 end do
2498 end do
2499
2500 ! Valence pair contraction over the native spin components (2 collinear, 4 noncollinear)
2501 do is = 1, this%nspin
2502 do ia = 1, this%nang
2503 ibase = (ia - 1)*np
2504 do i = 1, this%nlm
2505 ri = this%p_rad(i)
2506 lmi = this%p_l(i)*(this%p_l(i)+1) + this%p_m(i) + 1
2507 do j = 1, this%nlm
2508 rho_ij = rho(i, j, is)
2509 if (abs(rho_ij) <= tol) cycle
2510 rj = this%p_rad(j)
2511 lmj = this%p_l(j)*(this%p_l(j)+1) + this%p_m(j) + 1
2512 yi = this%ylm(ia, lmi)
2513 yj = this%ylm(ia, lmj)
2514 dti = this%dylm_dth(ia, lmi)
2515 dtj = this%dylm_dth(ia, lmj)
2516 dpi = this%dylm_dph(ia, lmi)
2517 dpj = this%dylm_dph(ia, lmj)
2518 cr = rho_ij*yi*yj ! Y_i Y_j weight (dens, grad radial, tau/lapl radial)
2519 ct = rho_ij*(dti*yj + yi*dtj) ! grad theta
2520 cp = rho_ij*(dpi*yj + yi*dpj) ! grad phi
2521 cv = rho_ij*(dti*dtj + dpi*dpj) ! grad_Omega(Y_i).grad_Omega(Y_j) (tau angular)
2522 cl = rho_ij*(-real(this%p_l(i)*(this%p_l(i)+1) + this%p_l(j)*(this%p_l(j)+1), real64)*yi*yj &
2523 + m_two*(dti*dtj + dpi*dpj)) ! lapl_Omega(Y_i Y_j)
2524 do ip = 1, np
2525 pij = phi(ip, ri)*phi(ip, rj)
2526 if (ld) dens(ibase + ip, is) = dens(ibase + ip, is) + cr*pij
2527 if (lg .or. ll) rp = dphi(ip, ri)*phi(ip, rj) + phi(ip, ri)*dphi(ip, rj) ! R`
2528 if (lg) then
2529 gn(ip, is, ia, 1) = gn(ip, is, ia, 1) + cr*rp
2530 gn(ip, is, ia, 2) = gn(ip, is, ia, 2) + ct*pij*this%invr(ip)
2531 gn(ip, is, ia, 3) = gn(ip, is, ia, 3) + cp*pij*this%invr(ip)
2532 end if
2533 if (lt) tau(ibase + ip, is) = tau(ibase + ip, is) &
2534 + cr*dphi(ip, ri)*dphi(ip, rj) + cv*(this%invr(ip)**2)*pij
2535 if (ll) then
2536 rpp = d2phi(ip, ri)*phi(ip, rj) + m_two*dphi(ip, ri)*dphi(ip, rj) + phi(ip, ri)*d2phi(ip, rj) ! R''
2537 lapl(ibase + ip, is) = lapl(ibase + ip, is) &
2538 + cr*(rpp + m_two*this%invr(ip)*rp) + cl*(this%invr(ip)**2)*pij
2539 end if
2540 end do
2541 end do
2542 end do
2543 end do
2544 end do
2545
2546 pop_sub_with_profile(paw_xc_quantities)
2547 end subroutine paw_xc_quantities
2548
2549 ! ---------------------------------------------------------
2555 subroutine paw_xc_mgga_dij(this, phi, dphi, gn, vr, vs, dij, vtau, d2phi, vlapl)
2556 type(paw_sphere_t), target, intent(in) :: this
2557 real(real64), contiguous, intent(in) :: phi(:,:) ! (np, nrad) partial waves for this side
2558 real(real64), contiguous, intent(in) :: dphi(:,:) ! (np, nrad) their radial derivatives
2559 real(real64), contiguous, intent(in) :: gn(:,:,:,:) ! (np, nspin, nang, 3) grad n per spin
2560 real(real64), contiguous, intent(in) :: vr(:,:) ! (np*nang, nspin) vrho
2561 real(real64), contiguous, intent(in) :: vs(:,:) ! (np*nang, nsig) vsigma
2562 real(real64), contiguous, intent(inout) :: dij(:,:,:) ! (nlm, nlm, nspin)
2563 real(real64), contiguous, optional, intent(in) :: vtau(:,:) ! (np*nang, nsp) vtau (no 1/2); optional term
2564 real(real64), contiguous, optional, intent(in) :: d2phi(:,:) ! (np, nrad) second radial derivatives (lapl)
2565 real(real64), contiguous, optional, intent(in) :: vlapl(:,:) ! (np*nang, nsp) dE/d(lapl n); optional term
2566
2567 integer :: np, nsp, is, os, iss, i, j, ip, ia, ri, rj, lmi, lmj, ibase
2568 real(real64) :: yi, yj, dti, dtj, dpi, dpj, yij, acc, w, pipj, vt, vl, rp, rpp, laplo
2569 real(real64) :: gradr, gradt, gradp, tauk
2570 logical :: lvt, lvl
2571 real(real64), allocatable :: fl(:,:,:,:) ! (np, nang, nsp, 3) sigma flux per spin
2572
2573 push_sub_with_profile(paw_xc_mgga_dij)
2574
2575 np = this%nr
2576 nsp = this%spin_channels
2577 lvt = present(vtau)
2578 lvl = present(vlapl) .and. present(d2phi)
2579
2580 ! Sigma flux F_sigma_s = 2 vsigma_ss grad(n_s) [+ vsigma_ud grad(n_os) for two spin channels]
2581 safe_allocate(fl(1:np, 1:this%nang, 1:nsp, 1:3))
2582 do is = 1, nsp
2583 iss = 2*is - 1
2584 os = min(nsp, 3 - is)
2585 do ia = 1, this%nang
2586 ibase = (ia - 1)*np
2587 do ip = 1, np
2588 fl(ip, ia, is, 1) = m_two*vs(ibase + ip, iss)*gn(ip, is, ia, 1)
2589 fl(ip, ia, is, 2) = m_two*vs(ibase + ip, iss)*gn(ip, is, ia, 2)
2590 fl(ip, ia, is, 3) = m_two*vs(ibase + ip, iss)*gn(ip, is, ia, 3)
2591 if (nsp == 2) then
2592 fl(ip, ia, is, 1) = fl(ip, ia, is, 1) + vs(ibase + ip, 2)*gn(ip, os, ia, 1)
2593 fl(ip, ia, is, 2) = fl(ip, ia, is, 2) + vs(ibase + ip, 2)*gn(ip, os, ia, 2)
2594 fl(ip, ia, is, 3) = fl(ip, ia, is, 3) + vs(ibase + ip, 2)*gn(ip, os, ia, 3)
2595 end if
2596 end do
2597 end do
2598 end do
2599
2600 do is = 1, nsp
2601 do ia = 1, this%nang
2602 ibase = (ia - 1)*np
2603 do i = 1, this%nlm
2604 ri = this%p_rad(i)
2605 lmi = this%p_l(i)*(this%p_l(i)+1) + this%p_m(i) + 1
2606 do j = 1, this%nlm
2607 rj = this%p_rad(j)
2608 lmj = this%p_l(j)*(this%p_l(j)+1) + this%p_m(j) + 1
2609 yi = this%ylm(ia, lmi)
2610 yj = this%ylm(ia, lmj)
2611 dti = this%dylm_dth(ia, lmi)
2612 dtj = this%dylm_dth(ia, lmj)
2613 dpi = this%dylm_dph(ia, lmi)
2614 dpj = this%dylm_dph(ia, lmj)
2615 yij = yi*yj
2616 acc = m_zero
2617 do ip = 1, np
2618 w = this%vwr(ip)
2619 pipj = phi(ip, ri)*phi(ip, rj)
2620 ! dn/drho, dgrad(n)/drho and dtau/drho at this node
2621 gradr = (dphi(ip, ri)*phi(ip, rj) + phi(ip, ri)*dphi(ip, rj))*yij
2622 gradt = pipj*this%invr(ip)*(dti*yj + yi*dtj)
2623 gradp = pipj*this%invr(ip)*(dpi*yj + yi*dpj)
2624 tauk = dphi(ip, ri)*dphi(ip, rj)*yij &
2625 + pipj*(this%invr(ip)**2)*(dti*dtj + dpi*dpj)
2626 vt = m_zero
2627 if (lvt) vt = vtau(ibase + ip, is)*tauk
2628 ! d(lapl n)/drho_ij = lapl(phi_i phi_j Y_i Y_j) = Y_iY_j(R'' + 2/r R`) + (R/r^2) lapl_Omega(Y_iY_j)
2629 vl = m_zero
2630 if (lvl) then
2631 rp = dphi(ip, ri)*phi(ip, rj) + phi(ip, ri)*dphi(ip, rj)
2632 rpp = d2phi(ip, ri)*phi(ip, rj) + m_two*dphi(ip, ri)*dphi(ip, rj) + phi(ip, ri)*d2phi(ip, rj)
2633 laplo = -real(this%p_l(i)*(this%p_l(i)+1) + this%p_l(j)*(this%p_l(j)+1), real64)*yij &
2634 + m_two*(dti*dtj + dpi*dpj)
2635 vl = vlapl(ibase + ip, is)*(yij*(rpp + m_two*this%invr(ip)*rp) + pipj*(this%invr(ip)**2)*laplo)
2636 end if
2637 acc = acc + w*(vr(ibase + ip, is)*pipj*yij &
2638 + fl(ip, ia, is, 1)*gradr + fl(ip, ia, is, 2)*gradt + fl(ip, ia, is, 3)*gradp &
2639 + vt + vl)
2640 end do
2641 dij(i, j, is) = dij(i, j, is) + this%wang(ia)*acc
2642 end do
2643 end do
2644 end do
2645 end do
2646
2647 safe_deallocate_a(fl)
2648
2649 pop_sub_with_profile(paw_xc_mgga_dij)
2650 end subroutine paw_xc_mgga_dij
2651
2652 ! ---------------------------------------------------------
2660 subroutine paw_xc_sph_potential(this, nsp, nsig, n0, sigma0, dn0, exc0, vsph)
2661 type(paw_sphere_t), intent(in) :: this
2662 integer, intent(in) :: nsp, nsig
2663 real(real64), contiguous, intent(in) :: n0(:,:) ! (np, nsp) spherical density
2664 real(real64), contiguous, intent(in) :: sigma0(:,:) ! (np, nsig) |dn0/dr|^2 channels
2665 real(real64), contiguous, intent(in) :: dn0(:,:) ! (np, nsp) radial gradient
2666 real(real64), contiguous, intent(out) :: exc0(:) ! (np) energy per particle
2667 real(real64), contiguous, intent(out) :: vsph(:,:) ! (np, nsp) potential
2668
2669 integer :: ip, is, os, iss
2670 real(real64), allocatable :: vs(:,:), flux(:), div(:)
2671
2672 push_sub(paw_xc_sph_potential)
2673
2674 safe_allocate(vs(1:this%nr, 1:nsig))
2675 call paw_xc_eval(this, this%nr, nsp, nsig, n0, sigma0, exc0, vsph, vs)
2676
2677 if (this%xc_is_gga) then
2678 safe_allocate(flux(1:this%nr))
2679 safe_allocate(div(1:this%nr))
2680 do is = 1, nsp
2681 os = min(nsp, 3 - is)
2682 iss = 2*is - 1
2683 if (nsp == 2) then
2684 do ip = 1, this%nr
2685 flux(ip) = (m_two*vs(ip, iss)*dn0(ip, is) + vs(ip, 2)*dn0(ip, os))*this%vwr(ip)
2686 end do
2687 else
2688 do ip = 1, this%nr
2689 flux(ip) = m_two*vs(ip, iss)*dn0(ip, is)*this%vwr(ip)
2690 end do
2691 end if
2692 call paw_adjoint_divergence_add(this, flux, div, vsph(:, is))
2693 end do
2694 safe_deallocate_a(flux)
2695 safe_deallocate_a(div)
2696 end if
2697
2698 safe_deallocate_a(vs)
2699
2700 pop_sub(paw_xc_sph_potential)
2701 end subroutine paw_xc_sph_potential
2702
2703 ! ---------------------------------------------------------
2712 subroutine paw_xc_kernels(this, nsp, nsig, n0, sigma0, dn0, kk, k2)
2713 type(paw_sphere_t), intent(in) :: this
2714 integer, intent(in) :: nsp, nsig
2715 real(real64), contiguous, intent(in) :: n0(:,:) ! (np, nsp) spherical density
2716 real(real64), contiguous, intent(in) :: sigma0(:,:) ! (np, nsig) |dn0/dr|^2 channels
2717 real(real64), contiguous, intent(in) :: dn0(:,:) ! (np, nsp) radial gradient
2718 real(real64), contiguous, intent(out) :: kk(:,:,:) ! (np, nsp, nsp) K_st
2719 real(real64), contiguous, intent(out) :: k2(:,:,:,:) ! (np, nsp, nsp, nsp) K2_stu
2720
2721 integer :: np, ip, is, it, iu, os, ixc, nf2, nrs, nk3, idx
2722 real(real64) :: ntot
2723 real(real64), allocatable :: dclamp(:,:), ssig(:,:)
2724 real(real64), allocatable :: v2rho2(:,:), v2rhosigma(:,:), v2sigma2(:,:)
2725 real(real64), allocatable :: s2rho2(:,:), s2rhosigma(:,:)
2726 real(real64), allocatable :: v3rho3(:,:), v3rho2s(:,:), v3rhos2(:,:), v3s3(:,:)
2727 real(real64), allocatable :: s3rho3(:,:)
2728 real(real64), allocatable :: flux(:), div(:)
2730 integer, parameter :: N_V3RHO2SIGMA(2) = [1, 9], n_v3rhosigma2(2) = [1, 12], n_v3sigma3(2) = [1, 10]
2731
2733
2734 ! The array sizes and component indices below follow the LibXC layout of the spin
2735 ! components (density: u, d; sigma: uu, ud, dd), which the checks here pin down:
2736 ! v2rho2: (uu, ud, dd); v2rhosigma: (rho t, sigma c) -> (t-1)*3 + c; v2sigma2: 6 pairs;
2737 ! v3rho3: (uuu, uud, udd, ddd); v3rho2sigma: 9; v3rhosigma2: 12; v3sigma3: 10.
2738 assert(nsp == 1 .or. nsp == 2)
2739 assert(nsig == 2*nsp - 1)
2740 np = this%nr
2741 nf2 = 2*nsp - 1 ! v2rho2 components: 1 (n n) or 3 (uu, ud, dd)
2742 nrs = nsp*(2*nsp - 1) ! v2rhosigma and v2sigma2 components: 1 or 6
2743 ! v3rho3 components: 1 unpolarized, 4 (uuu, uud, udd, ddd) polarized
2744 if (nsp == 1) then
2745 nk3 = 1
2746 else
2747 nk3 = 4
2748 end if
2749 assert(nf2 == nsig .and. nrs == nsp*nsig)
2750
2751 safe_allocate(dclamp(1:nsp, 1:np))
2752 do ip = 1, np
2753 do is = 1, nsp
2754 dclamp(is, ip) = max(n0(ip, is), m_zero)
2755 end do
2756 end do
2757 if (this%xc_is_gga) then
2758 safe_allocate(ssig(1:nsig, 1:np))
2759 do ip = 1, np
2760 do is = 1, nsig
2761 ssig(is, ip) = sigma0(ip, is)
2762 end do
2763 end do
2764 end if
2765
2766 safe_allocate(v2rho2(1:nf2, 1:np))
2767 safe_allocate(s2rho2(1:nf2, 1:np))
2768 safe_allocate(v3rho3(1:nk3, 1:np))
2769 safe_allocate(s3rho3(1:nk3, 1:np))
2770 s2rho2 = m_zero
2771 s3rho3 = m_zero
2772 if (this%xc_is_gga) then
2773 safe_allocate(v2rhosigma(1:nrs, 1:np))
2774 safe_allocate(v2sigma2(1:nrs, 1:np))
2775 safe_allocate(s2rhosigma(1:nrs, 1:np))
2776 ! output-only scratch for the kxc call, with the LibXC component counts of
2777 ! v3rho2sigma, v3rhosigma2 and v3sigma3 (unpolarized 1, 1, 1; polarized 9, 12, 10);
2778 ! only v3rho3 is read back
2779 safe_allocate(v3rho2s(1:n_v3rho2sigma(nsp), 1:np))
2780 safe_allocate(v3rhos2(1:n_v3rhosigma2(nsp), 1:np))
2781 safe_allocate(v3s3(1:n_v3sigma3(nsp), 1:np))
2782 s2rhosigma = m_zero
2783 end if
2785 do ixc = 1, 2
2786 select case (this%xc_functl(ixc)%family)
2787 case (xc_family_none)
2788 cycle
2789 case (xc_family_lda)
2790 call xc_f03_lda_fxc(this%xc_functl(ixc)%conf, int(np, c_size_t), dclamp, v2rho2)
2791 s2rho2 = s2rho2 + v2rho2
2792 call xc_f03_lda_kxc(this%xc_functl(ixc)%conf, int(np, c_size_t), dclamp, v3rho3)
2793 s3rho3 = s3rho3 + v3rho3
2794 case (xc_family_gga)
2795 call xc_f03_gga_fxc(this%xc_functl(ixc)%conf, int(np, c_size_t), dclamp, ssig, &
2796 v2rho2, v2rhosigma, v2sigma2)
2797 s2rho2 = s2rho2 + v2rho2
2798 s2rhosigma = s2rhosigma + v2rhosigma
2799 call xc_f03_gga_kxc(this%xc_functl(ixc)%conf, int(np, c_size_t), dclamp, ssig, &
2800 v3rho3, v3rho2s, v3rhos2, v3s3)
2801 s3rho3 = s3rho3 + v3rho3
2802 case default
2803 message(1) = "PAW one-center engine: unsupported XC functional family."
2804 call messages_fatal(1)
2805 end select
2806 end do
2807
2808 ! K_st: local density-density kernel (v2rho2 components 1 / (uu, ud, dd))
2809 do it = 1, nsp
2810 do is = 1, nsp
2811 idx = 1
2812 if (nsp == 2) idx = is + it - 1 ! (1,1) -> 1, cross -> 2, (2,2) -> 3
2813 do ip = 1, np
2814 kk(ip, is, it) = s2rho2(idx, ip)
2815 end do
2816 end do
2817 end do
2818
2819 ! GGA: discrete-adjoint divergence of the flux response at fixed gradient.
2820 ! v2rhosigma components (polarized): (rho t, sigma c) -> (t-1)*3 + c, c in (uu, ud, dd).
2821 if (this%xc_is_gga) then
2822 safe_allocate(flux(1:np))
2823 safe_allocate(div(1:np))
2824 do it = 1, nsp
2825 do is = 1, nsp
2826 os = min(nsp, 3 - is)
2827 do ip = 1, np
2828 if (nsp == 1) then
2829 flux(ip) = m_two*s2rhosigma(1, ip)*dn0(ip, 1)*this%vwr(ip)
2830 else
2831 flux(ip) = (m_two*s2rhosigma((it-1)*3 + 2*is - 1, ip)*dn0(ip, is) &
2832 + s2rhosigma((it-1)*3 + 2, ip)*dn0(ip, os))*this%vwr(ip)
2833 end if
2834 end do
2835 call paw_adjoint_divergence_add(this, flux, div, kk(:, is, it))
2836 end do
2837 end do
2838 safe_deallocate_a(flux)
2839 safe_deallocate_a(div)
2840 ! symmetrize: K is a second derivative of the energy; the two flux forms of the
2841 ! cross kernel differ only by discretization
2842 if (nsp == 2) then
2843 do ip = 1, np
2844 kk(ip, 1, 2) = m_half*(kk(ip, 1, 2) + kk(ip, 2, 1))
2845 kk(ip, 2, 1) = kk(ip, 1, 2)
2846 end do
2847 end if
2848 end if
2849
2850 ! K2_stu from v3rho3: 1 component unpolarized; polarized (uuu, uud, udd, ddd) indexed
2851 ! by the number of down-spin derivatives.
2852 do iu = 1, nsp
2853 do it = 1, nsp
2854 do is = 1, nsp
2855 idx = 1
2856 if (nsp == 2) idx = is + it + iu - 2
2857 do ip = 1, np
2858 k2(ip, is, it, iu) = s3rho3(idx, ip)
2859 end do
2860 end do
2861 end do
2862 end do
2863
2864 ! zero the kernels where the clamped density is negligible, consistent with the
2865 ! energy/potential fixup of paw_xc_eval
2866 do ip = 1, np
2867 ntot = m_zero
2868 do is = 1, nsp
2869 ntot = ntot + dclamp(is, ip)
2870 end do
2871 if (ntot < m_epsilon) then
2872 kk(ip, :, :) = m_zero
2873 k2(ip, :, :, :) = m_zero
2874 end if
2875 end do
2876
2877 safe_deallocate_a(dclamp)
2878 safe_deallocate_a(ssig)
2879 safe_deallocate_a(v2rho2)
2880 safe_deallocate_a(s2rho2)
2881 safe_deallocate_a(v3rho3)
2882 safe_deallocate_a(s3rho3)
2883 safe_deallocate_a(v2rhosigma)
2884 safe_deallocate_a(v2sigma2)
2885 safe_deallocate_a(s2rhosigma)
2886 safe_deallocate_a(v3rho2s)
2887 safe_deallocate_a(v3rhos2)
2888 safe_deallocate_a(v3s3)
2889
2890 pop_sub(paw_xc_kernels)
2891 end subroutine paw_xc_kernels
2892
2893 ! ---------------------------------------------------------
2913 subroutine paw_xc_moments(this, nlm_rho, ncore, ae, with_comp, e_xc, dij)
2914 type(paw_sphere_t), intent(in) :: this
2915 real(real64), contiguous, intent(in) :: nlm_rho(:,:,:) ! (np, nlm_rho, nspin)
2916 real(real64), contiguous, intent(in) :: ncore(:) ! (np) core density per spin channel
2917 logical, intent(in) :: ae
2918 logical, intent(in) :: with_comp
2919 real(real64), intent(out) :: e_xc
2920 real(real64), contiguous, intent(inout) :: dij(:,:,:) ! (nlm, nlm, nspin)
2921
2922 logical :: noncol
2923 integer :: np, nsp, nsig, is, it, iu, ic, lm, ip
2924 real(real64) :: sq4pi, acc, dpol, n0c(4), fwp(4, 2)
2925 real(real64), parameter :: wc(4) = [m_one, m_one, m_two, m_two]
2926 real(real64), allocatable :: wrad(:) ! (np) radial scratch for weighted reductions
2927 real(real64), allocatable :: f0(:,:,:) ! (np, 4, 2) frozen-axis weights (noncollinear)
2928 real(real64), allocatable :: n0(:,:), dn0(:,:), sigma0(:,:)
2929 real(real64), allocatable :: exc0(:), v0(:,:)
2930 real(real64), allocatable :: kk(:,:,:) ! (np, nsp, nsp) K_st = dv_s/dn_t
2931 real(real64), allocatable :: k2(:,:,:,:) ! (np, nsp, nsp, nsp) K2_stu
2932 real(real64), allocatable :: mlm(:,:,:) ! (np, nlm_rho, nsp) (projected) moment set
2933 real(real64), allocatable :: msum(:,:,:) ! (np, nsp, nsp) moment sums M_st
2934 real(real64), allocatable :: vxc_lm(:,:,:) ! (np, nlm_rho, nsp)
2935 real(real64), allocatable :: vrot(:,:) ! (np, nlm_rho) rotated slab potential
2936
2937 push_sub_with_profile(paw_xc_moments)
2938
2939 noncol = this%nspin == 4
2940 np = this%nr
2941 nsp = this%spin_channels
2942 nsig = 2*nsp - 1
2943 sq4pi = sqrt(m_four*m_pi)
2944
2945 ! Spherical density (00 moment plus core), its radial gradient and sigma channels.
2946 ! For the noncollinear case the spherical density also defines the frozen local axis;
2947 ! its weights f0 map the 4 slabs onto the two local channels.
2948 safe_allocate(n0(1:np, 1:nsp))
2949 safe_allocate(dn0(1:np, 1:nsp))
2950 safe_allocate(sigma0(1:np, 1:nsig))
2951 if (noncol) then
2952 safe_allocate(f0(1:np, 1:4, 1:2))
2953 do ip = 1, np
2954 do ic = 1, 4
2955 n0c(ic) = nlm_rho(ip, 1, ic)/sq4pi
2956 end do
2957 n0c(1) = n0c(1) + ncore(ip)
2958 n0c(2) = n0c(2) + ncore(ip)
2959 call paw_xc_noncol_weights(n0c(1), n0c(2), n0c(3), n0c(4), dpol, fwp)
2960 f0(ip, 1:4, 1:2) = fwp(1:4, 1:2)
2961 n0(ip, 1) = m_half*(n0c(1) + n0c(2) + dpol)
2962 n0(ip, 2) = m_half*(n0c(1) + n0c(2) - dpol)
2963 end do
2964 else
2965 do is = 1, nsp
2966 do ip = 1, np
2967 n0(ip, is) = nlm_rho(ip, 1, is)/sq4pi + ncore(ip)
2968 end do
2969 end do
2970 end if
2971 do is = 1, nsp
2972 call paw_radial_derivative_fd(this, n0(:, is), dn0(:, is))
2973 end do
2974 do ip = 1, np
2975 sigma0(ip, 1) = dn0(ip, 1)**2
2976 if (nsp == 2) then
2977 sigma0(ip, 2) = dn0(ip, 1)*dn0(ip, 2)
2978 sigma0(ip, 3) = dn0(ip, 2)**2
2979 end if
2980 end do
2981
2982 ! Base spherical potential and energy
2983 safe_allocate(exc0(1:np))
2984 safe_allocate(v0(1:np, 1:nsp))
2985 call paw_xc_sph_potential(this, nsp, nsig, n0, sigma0, dn0, exc0, v0)
2986
2987 safe_allocate(wrad(1:np))
2988 do ip = 1, np
2989 wrad(ip) = exc0(ip)*this%vwr(ip)
2990 end do
2991 e_xc = m_zero
2992 do is = 1, nsp
2993 e_xc = e_xc + blas_dot(np, n0(1, is), 1, wrad(1), 1)
2994 end do
2995 e_xc = m_four*m_pi*e_xc
2996
2997 ! Analytic kernels K_st = dv_s/dn_t, K2_stu = d2v_s/dn_t dn_u at fixed radial gradient
2998 ! (the fixed-gradient derivative of Eq. (57)), from the LibXC fxc/kxc
2999 safe_allocate(kk(1:np, 1:nsp, 1:nsp))
3000 safe_allocate(k2(1:np, 1:nsp, 1:nsp, 1:nsp))
3001 call paw_xc_kernels(this, nsp, nsig, n0, sigma0, dn0, kk, k2)
3002
3003 ! The moment set the expansion runs on: the L /= 00 moments projected on the frozen
3004 ! local axis for the noncollinear case, the moments themselves otherwise.
3005 safe_allocate(mlm(1:np, 1:this%nlm_rho, 1:nsp))
3006 if (noncol) then
3007 mlm = m_zero
3008 do is = 1, nsp
3009 do lm = 2, this%nlm_rho
3010 do ip = 1, np
3011 mlm(ip, lm, is) = wc(1)*f0(ip, 1, is)*nlm_rho(ip, lm, 1) &
3012 + wc(2)*f0(ip, 2, is)*nlm_rho(ip, lm, 2) &
3013 + wc(3)*f0(ip, 3, is)*nlm_rho(ip, lm, 3) &
3014 + wc(4)*f0(ip, 4, is)*nlm_rho(ip, lm, 4)
3015 end do
3016 end do
3017 end do
3018 else
3019 call lalg_copy(np, this%nlm_rho, nsp, nlm_rho, mlm)
3020 end if
3021
3022 ! Moment sums M_st(r) = sum_{LM /= 00} M_LM,s M_LM,t (Y_LM orthonormality has already
3023 ! collapsed the angular integrals) and their second-order energy contribution
3024 ! 1/2 sum_st int K_st(r) M_st(r) r^2 dr, accumulated in the same spin sweep
3025 safe_allocate(msum(1:np, 1:nsp, 1:nsp))
3026 msum = m_zero
3027 acc = m_zero
3028 do it = 1, nsp
3029 do is = 1, nsp
3030 do lm = 2, this%nlm_rho
3031 do ip = 1, np
3032 msum(ip, is, it) = msum(ip, is, it) + mlm(ip, lm, is)*mlm(ip, lm, it)
3033 end do
3034 end do
3035 do ip = 1, np
3036 acc = acc + kk(ip, is, it)*msum(ip, is, it)*this%vwr(ip)
3037 end do
3038 end do
3039 end do
3040 e_xc = e_xc + m_half*acc
3041
3042 ! Potential moments (Eqs. (60)-(61) without term 2) and the D matrix
3043 safe_allocate(vxc_lm(1:np, 1:this%nlm_rho, 1:nsp))
3044 do is = 1, nsp
3045 do ip = 1, np
3046 acc = m_zero
3047 do it = 1, nsp
3048 do iu = 1, nsp
3049 acc = acc + k2(ip, is, it, iu)*msum(ip, it, iu)
3050 end do
3051 end do
3052 vxc_lm(ip, 1, is) = sq4pi*v0(ip, is) + acc/(m_two*sq4pi)
3053 end do
3054 do lm = 2, this%nlm_rho
3055 do ip = 1, np
3056 acc = m_zero
3057 do it = 1, nsp
3058 acc = acc + kk(ip, is, it)*mlm(ip, lm, it)
3059 end do
3060 vxc_lm(ip, lm, is) = acc
3061 end do
3062 end do
3063 end do
3064
3065 ! Accumulate D; for the noncollinear case rotate the projected potential moments back
3066 ! to the 4 components with the frozen-axis weights first.
3067 if (noncol) then
3068 safe_allocate(vrot(1:np, 1:this%nlm_rho))
3069 do ic = 1, 4
3070 do lm = 1, this%nlm_rho
3071 do ip = 1, np
3072 vrot(ip, lm) = f0(ip, ic, 1)*vxc_lm(ip, lm, 1) + f0(ip, ic, 2)*vxc_lm(ip, lm, 2)
3073 end do
3074 end do
3075 call paw_accumulate_dij(this, ae, with_comp, vrot, dij(:,:,ic))
3076 end do
3077 else
3078 do is = 1, nsp
3079 call paw_accumulate_dij(this, ae, with_comp, vxc_lm(:,:,is), dij(:,:,is))
3080 end do
3081 end if
3082
3083 safe_deallocate_a(wrad)
3084 safe_deallocate_a(f0)
3085 safe_deallocate_a(n0)
3086 safe_deallocate_a(dn0)
3087 safe_deallocate_a(sigma0)
3088 safe_deallocate_a(exc0)
3089 safe_deallocate_a(v0)
3090 safe_deallocate_a(kk)
3091 safe_deallocate_a(k2)
3092 safe_deallocate_a(mlm)
3093 safe_deallocate_a(msum)
3094 safe_deallocate_a(vxc_lm)
3095 safe_deallocate_a(vrot)
3096
3097 pop_sub_with_profile(paw_xc_moments)
3098 end subroutine paw_xc_moments
3099
3100 ! ---------------------------------------------------------
3102 pure function paw_xc_noncol_dpol(n1, n2, n3, n4) result(dpol)
3103 real(real64), intent(in) :: n1, n2, n3, n4
3104 real(real64) :: dpol
3105
3106 dpol = sqrt((n1 - n2)**2 + m_four*(n3**2 + n4**2))
3107 if (dpol <= paw_xc_tiny*(n1 + n2)) dpol = m_zero
3108
3109 end function paw_xc_noncol_dpol
3110
3111 ! ---------------------------------------------------------
3113 pure subroutine paw_xc_noncol_weights(n1, n2, n3, n4, dpol, fw)
3114 real(real64), intent(in) :: n1, n2, n3, n4
3115 real(real64), intent(out) :: dpol
3116 real(real64), intent(out) :: fw(:,:)
3117
3118 real(real64) :: pp
3119
3120 dpol = sqrt((n1 - n2)**2 + m_four*(n3**2 + n4**2))
3121 if (dpol > paw_xc_tiny*(n1 + n2)) then
3122 pp = (n1 - n2)/dpol
3123 fw(1, 1) = m_half*(m_one + pp)
3124 fw(1, 2) = m_half*(m_one - pp)
3125 fw(2, 1) = m_half*(m_one - pp)
3126 fw(2, 2) = m_half*(m_one + pp)
3127 fw(3, 1) = n3/dpol
3128 fw(3, 2) = -n3/dpol
3129 fw(4, 1) = n4/dpol
3130 fw(4, 2) = -n4/dpol
3131 else
3132 dpol = m_zero
3133 fw(1, 1:2) = m_half
3134 fw(2, 1:2) = m_half
3135 fw(3, 1:2) = m_zero
3136 fw(4, 1:2) = m_zero
3137 end if
3138
3139 end subroutine paw_xc_noncol_weights
3140
3141 ! ---------------------------------------------------------
3150 subroutine paw_xc_dij_gga_noncollinear(this, ae, with_comp, dens, g, gp, vr, vs, dij)
3151 type(paw_sphere_t), intent(in) :: this
3152 logical, intent(in) :: ae
3153 logical, intent(in) :: with_comp
3154 real(real64), contiguous, intent(in) :: dens(:,:)
3155 real(real64), contiguous, intent(in) :: g(:,:,:,:), gp(:,:,:,:)
3156 real(real64), contiguous, intent(in) :: vr(:,:), vs(:,:)
3157 real(real64), contiguous, intent(inout) :: dij(:,:,:)
3158
3159 integer :: np, ibase, ip, ia, ic, k
3160 real(real64) :: dpol, uu, gradp
3161 real(real64) :: fwp(4, 2), gq(3), hh(3, 4), bs(3, 2), bd(3)
3162 real(real64), allocatable :: a1(:,:,:), a2(:,:,:), a3(:,:,:), a4(:,:,:)
3163
3164 push_sub_with_profile(paw_xc_dij_gga_noncollinear)
3165
3166 np = this%nr
3167
3168 safe_allocate(a1(1:np, 1:this%nang, 1:4))
3169 safe_allocate(a2(1:np, 1:this%nang, 1:4))
3170 safe_allocate(a3(1:np, 1:this%nang, 1:4))
3171 safe_allocate(a4(1:np, 1:this%nang, 1:4))
3172
3173 !$omp parallel do schedule(static) private(ibase, ip, dpol, uu, gradp, fwp, gq, hh, bs, bd, ic, k)
3174 do ia = 1, this%nang
3175 ibase = (ia - 1)*np
3176 do ip = 1, np
3177 call paw_xc_noncol_weights(dens(ibase + ip, 1), dens(ibase + ip, 2), &
3178 dens(ibase + ip, 3), dens(ibase + ip, 4), dpol, fwp)
3179 ! projected-channel flux B_s and its channel difference
3180 do k = 1, 3
3181 bs(k, 1) = m_two*vs(ibase + ip, 1)*gp(ip, 1, ia, k) + vs(ibase + ip, 2)*gp(ip, 2, ia, k)
3182 bs(k, 2) = m_two*vs(ibase + ip, 3)*gp(ip, 2, ia, k) + vs(ibase + ip, 2)*gp(ip, 1, ia, k)
3183 bd(k) = bs(k, 1) - bs(k, 2)
3184 end do
3185 ! weight gradients h_c = grad F_c^+ per component (dpol is zeroed by the weights helper
3186 ! below the polarization threshold)
3187 hh = m_zero
3188 if (dpol > m_zero) then
3189 uu = dens(ibase + ip, 1) - dens(ibase + ip, 2)
3190 do k = 1, 3
3191 gq(k) = (uu*(g(ip, 1, ia, k) - g(ip, 2, ia, k)) &
3192 + m_four*(dens(ibase + ip, 3)*g(ip, 3, ia, k) &
3193 + dens(ibase + ip, 4)*g(ip, 4, ia, k)))/dpol
3194 gradp = ((g(ip, 1, ia, k) - g(ip, 2, ia, k)) - (uu/dpol)*gq(k))/dpol
3195 hh(k, 1) = m_half*gradp
3196 hh(k, 2) = -m_half*gradp
3197 hh(k, 3) = (g(ip, 3, ia, k) - dens(ibase + ip, 3)*gq(k)/dpol)/dpol
3198 hh(k, 4) = (g(ip, 4, ia, k) - dens(ibase + ip, 4)*gq(k)/dpol)/dpol
3199 end do
3200 end if
3201 do ic = 1, 4
3202 a1(ip, ia, ic) = fwp(ic, 1)*vr(ibase + ip, 1) + fwp(ic, 2)*vr(ibase + ip, 2) &
3203 + bd(1)*hh(1, ic) + bd(2)*hh(2, ic) + bd(3)*hh(3, ic)
3204 a2(ip, ia, ic) = fwp(ic, 1)*bs(1, 1) + fwp(ic, 2)*bs(1, 2)
3205 a3(ip, ia, ic) = fwp(ic, 1)*bs(2, 1) + fwp(ic, 2)*bs(2, 2)
3206 a4(ip, ia, ic) = fwp(ic, 1)*bs(3, 1) + fwp(ic, 2)*bs(3, 2)
3207 end do
3208 end do
3209 end do
3210
3211 call paw_xc_dij_gga_fields(this, ae, with_comp, a1, a2, a3, a4, dij)
3212
3213 safe_deallocate_a(a1)
3214 safe_deallocate_a(a2)
3215 safe_deallocate_a(a3)
3216 safe_deallocate_a(a4)
3217
3218 pop_sub_with_profile(paw_xc_dij_gga_noncollinear)
3219 end subroutine paw_xc_dij_gga_noncollinear
3220
3221 ! ---------------------------------------------------------
3227 subroutine paw_xc_dij_gga_fields(this, ae, with_comp, a1, a2, a3, a4, dij)
3228 type(paw_sphere_t), intent(in) :: this
3229 logical, intent(in) :: ae
3230 logical, intent(in) :: with_comp
3231 real(real64), contiguous, intent(in) :: a1(:,:,:), a2(:,:,:), a3(:,:,:), a4(:,:,:) ! (np, nang, ncomp)
3232 real(real64), contiguous, intent(inout) :: dij(:,:,:) ! (nlm, nlm, ncomp)
3233
3234 integer :: np, ip, lm, ic
3235 real(real64), allocatable :: t1(:,:), t2(:,:), t3(:,:), t4(:,:) ! (np, nlm_rho) Y_LM projections
3236 real(real64), allocatable :: u1(:,:), u2(:,:) ! (np, nlm_rho) radial integrands
3237 real(real64), allocatable :: w(:,:) ! (npair, nlm_rho)
3238
3239 push_sub_with_profile(paw_xc_dij_gga_fields)
3240
3241 np = this%nr
3242
3243 safe_allocate(t1(1:np, 1:this%nlm_rho))
3244 safe_allocate(t2(1:np, 1:this%nlm_rho))
3245 safe_allocate(t3(1:np, 1:this%nlm_rho))
3246 safe_allocate(t4(1:np, 1:this%nlm_rho))
3247 safe_allocate(u1(1:np, 1:this%nlm_rho))
3248 safe_allocate(u2(1:np, 1:this%nlm_rho))
3249 safe_allocate(w(1:this%npair, 1:this%nlm_rho))
3250
3251 do ic = 1, size(dij, 3)
3252 ! Project onto the Y_LM channels: [np x nlm_rho] = [np x nang] . [nang x nlm_rho]
3253 call lalg_gemm(a1(:, :, ic), this%wylm, t1)
3254 call lalg_gemm(a2(:, :, ic), this%wylm, t2)
3255 call lalg_gemm(a3(:, :, ic), this%wdylm_dth, t3)
3256 call lalg_gemm(a4(:, :, ic), this%wdylm_dph, t4)
3257
3258 ! Radial integrands: the pair density couples to t1 + (t3 + t4)/r, its radial
3259 ! derivative to t2; both carry the radial volume weight.
3260 do lm = 1, this%nlm_rho
3261 do ip = 1, np
3262 u1(ip, lm) = (t1(ip, lm) + (t3(ip, lm) + t4(ip, lm))*this%invr(ip))*this%vwr(ip)
3263 u2(ip, lm) = t2(ip, lm)*this%vwr(ip)
3264 end do
3265 end do
3266
3267 ! Radial reduction against the pair tables (+ compensation shapes per L).
3268 if (ae) then
3269 call lalg_gemm(this%pair_ae, u1, w, transa = 'T')
3270 call lalg_gemm(this%dpair_ae, u2, w, transa = 'T', beta = m_one)
3271 else
3272 call lalg_gemm(this%pair_ps, u1, w, transa = 'T')
3273 call lalg_gemm(this%dpair_ps, u2, w, transa = 'T', beta = m_one)
3274 if (with_comp) then
3275 call paw_qaug_gemv(this, this%qaug, 'T', u1, w)
3276 call paw_qaug_gemv(this, this%dqaug, 'T', u2, w)
3277 end if
3278 end if
3279
3280 ! Scatter through the packed Gaunt couplings into the symmetric D matrix.
3281 call paw_gaunt_scatter_dij(this, w, dij(:, :, ic))
3282 end do
3283
3284 safe_deallocate_a(t1)
3285 safe_deallocate_a(t2)
3286 safe_deallocate_a(t3)
3287 safe_deallocate_a(t4)
3288 safe_deallocate_a(u1)
3289 safe_deallocate_a(u2)
3290 safe_deallocate_a(w)
3291
3292 pop_sub_with_profile(paw_xc_dij_gga_fields)
3293 end subroutine paw_xc_dij_gga_fields
3294
3295 ! ---------------------------------------------------------
3302 subroutine paw_accumulate_dij(this, ae, with_comp, vlm, dij)
3303 type(paw_sphere_t), intent(in) :: this
3304 logical, intent(in) :: ae
3305 logical, intent(in) :: with_comp
3306 real(real64), contiguous, intent(in) :: vlm(:,:)
3307 real(real64), contiguous, intent(inout) :: dij(:,:)
3308
3309 integer :: lm, ip
3310 real(real64), allocatable :: vw(:,:), w(:,:)
3311
3312 push_sub_with_profile(paw_accumulate_dij)
3313
3314 ! Radial reduction first: w(pair, LM) = int g_pair(r) v_LM(r) r^2 dr with one matrix
3315 ! product over the pair tables (+ the qaug compensation per L), then scatter through
3316 ! the packed Gaunt couplings: D_ij += sum c(i j; LM) w(pair_ij, LM).
3317 safe_allocate(vw(1:this%nr, 1:this%nlm_rho))
3318 do lm = 1, this%nlm_rho
3319 do ip = 1, this%nr
3320 vw(ip, lm) = vlm(ip, lm)*this%vwr(ip)
3321 end do
3322 end do
3323
3324 safe_allocate(w(1:this%npair, 1:this%nlm_rho))
3325 if (ae) then
3326 call lalg_gemm(this%pair_ae, vw, w, transa = 'T')
3327 else
3328 call lalg_gemm(this%pair_ps, vw, w, transa = 'T')
3329 if (with_comp) call paw_qaug_gemv(this, this%qaug, 'T', vw, w)
3330 end if
3331
3332 ! Scatter through the packed Gaunt couplings into the symmetric D matrix.
3333 call paw_gaunt_scatter_dij(this, w, dij)
3334
3335 safe_deallocate_a(vw)
3336 safe_deallocate_a(w)
3337
3338 pop_sub_with_profile(paw_accumulate_dij)
3339 end subroutine paw_accumulate_dij
3340
3341 ! ---------------------------------------------------------
3343 integer pure function paw_onsite_nlm(this)
3344 type(paw_sphere_t), intent(in) :: this
3345 paw_onsite_nlm = this%nlm
3346 end function paw_onsite_nlm
3347
3348 ! ---------------------------------------------------------
3351 integer pure function paw_onsite_nlm_rho(this)
3352 type(paw_sphere_t), intent(in) :: this
3353 paw_onsite_nlm_rho = this%nlm_rho
3354 end function paw_onsite_nlm_rho
3355
3356 ! ---------------------------------------------------------
3358 integer pure function paw_onsite_nspin(this)
3359 type(paw_sphere_t), intent(in) :: this
3360 paw_onsite_nspin = this%nspin
3361 end function paw_onsite_nspin
3362
3363 ! ---------------------------------------------------------
3372 subroutine paw_onsite_pmat_map(this, nprojs, pl, pm, pchannel, map)
3373 type(paw_sphere_t), intent(in) :: this
3374 integer, intent(in) :: nprojs
3375 integer, intent(in) :: pl(:)
3376 integer, intent(in) :: pm(:)
3377 integer, intent(in) :: pchannel(:)
3378 integer, intent(out) :: map(:)
3379
3380 integer :: i, ic, maxl, maxchan, iproj, ri, lm
3381 integer, allocatable :: cnt(:), rad_of(:,:)
3382
3383 push_sub(paw_onsite_pmat_map)
3384
3385 assert(nprojs == this%nlm)
3386
3387 maxl = maxval(this%lrad(1:this%nrad))
3388 maxchan = this%nrad ! upper bound on the number of channels per l
3389 safe_allocate(cnt(0:maxl))
3390 safe_allocate(rad_of(0:maxl, 1:maxchan))
3391 cnt = 0
3392 rad_of = 0
3393 do i = 1, this%nrad
3394 cnt(this%lrad(i)) = cnt(this%lrad(i)) + 1
3395 ic = cnt(this%lrad(i))
3396 ! Fully-relativistic datasets order the projector channels j = l+1/2 first within each
3397 ! pair while the partial waves keep the file order (j = l-1/2 first)
3398 ! remap so the channel index matches the projector enumeration.
3399 if (this%rad_2j(i) > 0 .and. this%lrad(i) > 0) then
3400 if (this%rad_2j(i) == 2*this%lrad(i) - 1) then
3401 ic = (ic - 1)/2*2 + 2
3402 else
3403 assert(this%rad_2j(i) == 2*this%lrad(i) + 1)
3404 ic = (ic - 1)/2*2 + 1
3405 end if
3406 end if
3407 rad_of(this%lrad(i), ic) = i
3408 end do
3409
3410 map = 0
3411 do iproj = 1, nprojs
3412 assert(pl(iproj) >= 0 .and. pl(iproj) <= maxl)
3413 assert(pchannel(iproj) >= 1 .and. pchannel(iproj) <= cnt(pl(iproj)))
3414 ri = rad_of(pl(iproj), pchannel(iproj))
3415 assert(ri > 0)
3416 assert(this%lrad(ri) == pl(iproj))
3417 do lm = 1, this%nlm
3418 if (this%p_rad(lm) == ri .and. this%p_m(lm) == pm(iproj) .and. this%p_l(lm) == pl(iproj)) then
3419 map(iproj) = lm
3420 exit
3421 end if
3422 end do
3423 assert(map(iproj) > 0)
3424 end do
3425
3426 safe_deallocate_a(cnt)
3427 safe_deallocate_a(rad_of)
3428
3429 pop_sub(paw_onsite_pmat_map)
3430 end subroutine paw_onsite_pmat_map
3431
3432 ! ---------------------------------------------------------
3437 subroutine paw_onsite_reference_density(this, occ, rho)
3438 type(paw_sphere_t), intent(in) :: this
3439 real(real64), intent(in) :: occ(:)
3440 real(real64), intent(out) :: rho(:,:,:)
3441
3442 integer :: i, is
3443
3445
3446 rho = m_zero
3447 do is = 1, this%spin_channels
3448 do i = 1, this%nlm
3449 rho(i, i, is) = occ(this%p_rad(i)) / real((2*this%p_l(i) + 1)*this%spin_channels, real64)
3450 end do
3451 end do
3452
3454 end subroutine paw_onsite_reference_density
3455
3456 ! ---------------------------------------------------------
3461 real(real64) function paw_onsite_charge(this, rho, ae) result(charge)
3462 type(paw_sphere_t), intent(in) :: this
3463 real(real64), intent(in) :: rho(:,:,:)
3464 logical, intent(in) :: ae
3465
3466 integer :: nr, is
3467 real(real64), allocatable :: nlm_rho(:,:)
3468 real(real64) :: sqrt4pi
3469
3470 push_sub(paw_onsite_charge)
3471
3472 nr = this%nr
3473 sqrt4pi = sqrt(m_four*m_pi)
3474 safe_allocate(nlm_rho(1:nr, 1:this%nlm_rho))
3475 charge = m_zero
3476 ! only the diagonal spin components carry charge (slabs 3/4 are the transverse magnetization)
3477 do is = 1, this%spin_channels
3478 call paw_build_density(this, rho(:,:,is), ae, .not. ae, nlm_rho)
3479 ! Q = int n d^3r = sqrt(4pi) int n_00(r) r^2 dr (only the L=0 multipole survives the angle)
3480 charge = charge + sqrt4pi*blas_dot(nr, nlm_rho(1, 1), 1, this%vwr(1), 1)
3481 end do
3482 safe_deallocate_a(nlm_rho)
3483
3484 pop_sub(paw_onsite_charge)
3485 end function paw_onsite_charge
3486
3487 ! ---------------------------------------------------------
3495 subroutine paw_onsite_density_diff(this, rho, ncol, spl, ll, mm, rcut)
3496 type(paw_sphere_t), intent(in) :: this
3497 real(real64), intent(in) :: rho(:,:)
3498 integer, intent(out) :: ncol
3499 type(spline_t), allocatable, intent(out) :: spl(:)
3500 integer, allocatable, intent(out) :: ll(:)
3501 integer, allocatable, intent(out) :: mm(:)
3502 real(real64), intent(out) :: rcut
3503
3504 real(real64), allocatable :: nae(:,:), nps(:,:)
3505 real(real64), parameter :: tol = 1.0e-14_real64
3506
3507 push_sub_with_profile(paw_onsite_density_diff)
3508
3509 safe_allocate(nae(1:this%nr, 1:this%nlm_rho))
3510 safe_allocate(nps(1:this%nr, 1:this%nlm_rho))
3511 ! all-electron one-center density and pseudo one-center density including nhat
3512 call paw_build_density(this, rho, .true., .false., nae)
3513 call paw_build_density(this, rho, .false., .true., nps)
3514
3515 call lalg_axpy(this%nr, this%nlm_rho, -m_one, nps, nae)
3516 call paw_emit_lm_columns(this, nae, tol, ncol, spl, ll, mm, rcut)
3517
3518 safe_deallocate_a(nae)
3519 safe_deallocate_a(nps)
3520
3521 pop_sub_with_profile(paw_onsite_density_diff)
3522 end subroutine paw_onsite_density_diff
3523
3524 ! ---------------------------------------------------------
3537 subroutine paw_onsite_tau_diff(this, rho, ncol, spl, ll, mm, rcut, tau_integral)
3538 type(paw_sphere_t), intent(in) :: this
3539 real(real64), intent(in) :: rho(:,:)
3540 integer, intent(out) :: ncol
3541 type(spline_t), allocatable, intent(out) :: spl(:)
3542 integer, allocatable, intent(out) :: ll(:)
3543 integer, allocatable, intent(out) :: mm(:)
3544 real(real64), intent(out) :: rcut
3548 real(real64), optional, intent(out) :: tau_integral
3549
3550 integer :: i, j, k, ia, ri, rj, lmi, lmj
3551 real(real64) :: rho_ij, ang_grad, ang_val, wa, cg, cv
3552 real(real64), allocatable :: dphi_ae(:,:), dphi_ps(:,:), dtau_lm(:,:), dtaua(:,:)
3553 real(real64), parameter :: tol = 1.0e-14_real64
3554
3555 push_sub_with_profile(paw_onsite_tau_diff)
3556
3557 safe_allocate(dphi_ae(1:this%nr, 1:this%nrad))
3558 safe_allocate(dphi_ps(1:this%nr, 1:this%nrad))
3559 call paw_partial_wave_derivatives(this, dphi_ae, dphi_ps)
3560
3561 ! Delta tau on the radial x angular grid, times the angular weight: dtaua(k, ia) = w(ia) *
3562 ! 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) ].
3563 ! The radial grid index k is the innermost loop so the (nr-leading) partial-wave and dtaua
3564 ! arrays are traversed contiguously; the angular factors are hoisted out of the k loop.
3565 safe_allocate(dtaua(1:this%nr, 1:this%nang))
3566 dtaua(1:this%nr, 1:this%nang) = m_zero
3567 do ia = 1, this%nang
3568 wa = this%wang(ia)
3569 do i = 1, this%nlm
3570 ri = this%p_rad(i)
3571 lmi = this%p_l(i)*(this%p_l(i)+1) + this%p_m(i) + 1
3572 do j = 1, this%nlm
3573 rho_ij = rho(i, j)
3574 if (abs(rho_ij) <= tol) cycle
3575 rj = this%p_rad(j)
3576 lmj = this%p_l(j)*(this%p_l(j)+1) + this%p_m(j) + 1
3577 ang_grad = this%ylm(ia, lmi)*this%ylm(ia, lmj)
3578 ang_val = this%dylm_dth(ia, lmi)*this%dylm_dth(ia, lmj) &
3579 + this%dylm_dph(ia, lmi)*this%dylm_dph(ia, lmj)
3580 cg = rho_ij*wa*ang_grad
3581 cv = rho_ij*wa*ang_val
3582 ! all-electron minus pseudo kinetic-energy density at (r_k, Omega_ia)
3583 do k = 1, this%nr
3584 dtaua(k, ia) = dtaua(k, ia) &
3585 + cg*(dphi_ae(k, ri)*dphi_ae(k, rj) - dphi_ps(k, ri)*dphi_ps(k, rj)) &
3586 + 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))
3587 end do
3588 end do
3589 end do
3590 end do
3591
3592 ! Project onto the real harmonics: Delta tau_LM(r) = int Delta tau Y_LM dOmega, i.e. the
3593 ! matrix product dtau_lm(nr, nlm_rho) = dtaua(nr, nang) . ylm(nang, nlm_rho).
3594 safe_allocate(dtau_lm(1:this%nr, 1:this%nlm_rho))
3595 call lalg_gemm(this%nr, this%nlm_rho, this%nang, m_one, dtaua, this%ylm, m_zero, dtau_lm)
3596 safe_deallocate_a(dtaua)
3597
3598 call paw_emit_lm_columns(this, dtau_lm, tol, ncol, spl, ll, mm, rcut)
3599
3600 ! int Delta tau d^3r = sqrt(4pi) int Delta tau_00(r) r^2 dr (only the L=0 multipole survives
3601 ! the angular integration), same convention as paw_onsite_charge. The radial integral is the
3602 ! dot product of the L=0 column with the radial volume weights r^2 dr.
3603 if (present(tau_integral)) then
3604 tau_integral = sqrt(m_four*m_pi)*blas_dot(this%nr, dtau_lm(1, 1), 1, this%vwr(1), 1)
3605 end if
3606
3607 safe_deallocate_a(dphi_ae)
3608 safe_deallocate_a(dphi_ps)
3609 safe_deallocate_a(dtau_lm)
3610
3611 pop_sub_with_profile(paw_onsite_tau_diff)
3612 end subroutine paw_onsite_tau_diff
3613
3614 ! ---------------------------------------------------------
3629 subroutine paw_onsite_current_diff(this, arho, idir, ncol, spl, ll, mm, rcut, current_integral)
3630 type(paw_sphere_t), intent(in) :: this
3631 real(real64), intent(in) :: arho(:,:)
3632 integer, intent(in) :: idir
3633 integer, intent(out) :: ncol
3634 type(spline_t), allocatable, intent(out) :: spl(:)
3635 integer, allocatable, intent(out) :: ll(:)
3636 integer, allocatable, intent(out) :: mm(:)
3637 real(real64), intent(out) :: rcut
3638 real(real64), optional, intent(out) :: current_integral
3639
3640 integer :: i, j, ip, ia, ri, rj, lmi, lmj, ra, rb, rp, npr
3641 real(real64) :: aij, yi
3642 real(real64), allocatable :: dphi_ae(:,:), dphi_ps(:,:), dj_lm(:,:), dja(:,:), rmat(:,:), cmat(:,:)
3643 real(real64), parameter :: tol = 1.0e-14_real64
3644
3645 push_sub_with_profile(paw_onsite_current_diff)
3646
3647 safe_allocate(dphi_ae(1:this%nr, 1:this%nrad))
3648 safe_allocate(dphi_ps(1:this%nr, 1:this%nrad))
3649 call paw_partial_wave_derivatives(this, dphi_ae, dphi_ps)
3650
3651 ! Express the contraction as matrix-matrix products, like paw_xc.
3652 ! The radial factors depend only on (ip, radial-channel-pair), the angular coefficients only on (radial-pair, ia):
3653 ! grad(phi Y) = phi`(r) Y rhat + (phi/r)(dY/dth that + (1/sin th) dY/dph phat), so
3654 ! Delta j^idir(ip,ia) = sum_ij arho_ij (g_i grad_idir g_j)^{AE-PS}
3655 ! = sum_rp [ Rg(ip,rp) Cg(rp,ia) + Rv(ip,rp) Cv(rp,ia) ]
3656 ! with radial-channel pair rp = (ra,rb), the radial-gradient block Rg = phi_a phi_b` and the
3657 ! transverse block Rv = phi_a phi_b / r (all-electron minus pseudo), and angular coefficients
3658 ! Cg(rp,ia) = sum_{(i,j):(ri,rj)=rp} arho_ij w(ia) Y_i (Y_j rhat_idir)
3659 ! Cv(rp,ia) = sum_{(i,j):(ri,rj)=rp} arho_ij w(ia) Y_i (dY_j/dth that + dY_j/dph phat)_idir
3660 ! stacked into rmat(nr, 2 nrad^2) and cmat(2 nrad^2, nang); dja = rmat . cmat.
3661 npr = this%nrad*this%nrad
3662 safe_allocate(rmat(1:this%nr, 1:2*npr))
3663 do ra = 1, this%nrad
3664 do rb = 1, this%nrad
3665 rp = (ra-1)*this%nrad + rb
3666 do ip = 1, this%nr
3667 rmat(ip, rp) = this%phi_ae(ip, ra)*dphi_ae(ip, rb) - this%phi_ps(ip, ra)*dphi_ps(ip, rb)
3668 rmat(ip, npr + rp) = this%invr(ip)*(this%phi_ae(ip, ra)*this%phi_ae(ip, rb) &
3669 - this%phi_ps(ip, ra)*this%phi_ps(ip, rb))
3670 end do
3671 end do
3672 end do
3673
3674 safe_allocate(cmat(1:2*npr, 1:this%nang))
3675 cmat(1:2*npr, 1:this%nang) = m_zero
3676 do i = 1, this%nlm
3677 ri = this%p_rad(i)
3678 lmi = this%p_l(i)*(this%p_l(i)+1) + this%p_m(i) + 1
3679 do j = 1, this%nlm
3680 aij = arho(i, j)
3681 if (abs(aij) <= tol) cycle
3682 rj = this%p_rad(j)
3683 lmj = this%p_l(j)*(this%p_l(j)+1) + this%p_m(j) + 1
3684 rp = (ri-1)*this%nrad + rj
3685 do ia = 1, this%nang
3686 yi = aij*this%wang(ia)*this%ylm(ia, lmi)
3687 cmat(rp, ia) = cmat(rp, ia) + yi*this%ylm(ia, lmj)*this%rhat(ia, idir)
3688 cmat(npr + rp, ia) = cmat(npr + rp, ia) &
3689 + yi*(this%dylm_dth(ia, lmj)*this%that(ia, idir) + this%dylm_dph(ia, lmj)*this%phat(ia, idir))
3690 end do
3691 end do
3692 end do
3693
3694 safe_allocate(dja(1:this%nr, 1:this%nang))
3695 call lalg_gemm(this%nr, this%nang, 2*npr, m_one, rmat, cmat, m_zero, dja)
3696 safe_deallocate_a(rmat)
3697 safe_deallocate_a(cmat)
3698
3699 ! Project onto the real harmonics: Delta j_LM(r) = int Delta j Y_LM dOmega = dja(nr,nang).ylm(nang,nlm_rho)
3700 safe_allocate(dj_lm(1:this%nr, 1:this%nlm_rho))
3701 call lalg_gemm(this%nr, this%nlm_rho, this%nang, m_one, dja, this%ylm, m_zero, dj_lm)
3702 safe_deallocate_a(dja)
3703
3704 call paw_emit_lm_columns(this, dj_lm, tol, ncol, spl, ll, mm, rcut)
3705
3706 if (present(current_integral)) then
3707 current_integral = sqrt(m_four*m_pi)*blas_dot(this%nr, dj_lm(1, 1), 1, this%vwr(1), 1)
3708 end if
3709
3710 safe_deallocate_a(dphi_ae)
3711 safe_deallocate_a(dphi_ps)
3712 safe_deallocate_a(dj_lm)
3713
3714 pop_sub_with_profile(paw_onsite_current_diff)
3715 end subroutine paw_onsite_current_diff
3716
3717 ! ---------------------------------------------------------
3723 subroutine paw_onsite_get_velocity_matrix(this, vmat)
3724 type(paw_sphere_t), intent(in) :: this
3725 real(real64), allocatable, intent(out) :: vmat(:,:,:)
3726
3727 integer :: i, j, ip, ia, ri, rj, lmi, lmj, idir
3728 real(real64) :: yi, aa, bb, c1, c2, wa, acc
3729 real(real64), allocatable :: dphi_ae(:,:), dphi_ps(:,:)
3730
3732
3733 safe_allocate(dphi_ae(1:this%nr, 1:this%nrad))
3734 safe_allocate(dphi_ps(1:this%nr, 1:this%nrad))
3735 do i = 1, this%nrad
3736 call paw_radial_derivative(this%r, this%phi_ae(:, i), dphi_ae(:, i))
3737 call paw_radial_derivative(this%r, this%phi_ps(:, i), dphi_ps(:, i))
3738 end do
3739
3740 ! Same integrand and factorization as paw_onsite_current_diff, integrated radially with the
3741 ! r^2 dr weight vwr
3742 safe_allocate(vmat(1:this%nlm, 1:this%nlm, 1:3))
3743 vmat = m_zero
3744 do idir = 1, 3
3745 do ia = 1, this%nang
3746 wa = this%wang(ia)
3747 do i = 1, this%nlm
3748 ri = this%p_rad(i)
3749 lmi = this%p_l(i)*(this%p_l(i)+1) + this%p_m(i) + 1
3750 yi = this%ylm(ia, lmi)
3751 do j = 1, this%nlm
3752 rj = this%p_rad(j)
3753 lmj = this%p_l(j)*(this%p_l(j)+1) + this%p_m(j) + 1
3754 aa = this%ylm(ia, lmj)*this%rhat(ia, idir)
3755 bb = this%dylm_dth(ia, lmj)*this%that(ia, idir) + this%dylm_dph(ia, lmj)*this%phat(ia, idir)
3756 c1 = yi*aa
3757 c2 = yi*bb
3758 acc = m_zero
3759 do ip = 1, this%nr
3760 acc = acc + this%vwr(ip)*( &
3761 c1*(this%phi_ae(ip, ri)*dphi_ae(ip, rj) - this%phi_ps(ip, ri)*dphi_ps(ip, rj)) &
3762 + c2*this%invr(ip)*(this%phi_ae(ip, ri)*this%phi_ae(ip, rj) - this%phi_ps(ip, ri)*this%phi_ps(ip, rj)))
3763 end do
3764 vmat(i, j, idir) = vmat(i, j, idir) + wa*acc
3765 end do
3766 end do
3767 end do
3768 end do
3769
3770 safe_deallocate_a(dphi_ae)
3771 safe_deallocate_a(dphi_ps)
3772
3774 end subroutine paw_onsite_get_velocity_matrix
3775
3776 ! ---------------------------------------------------------
3786 subroutine paw_onsite_partial_wave_diff(this, nprojs, pl, pm, pchannel, spl, ll, mm, rcut)
3787 type(paw_sphere_t), intent(in) :: this
3788 integer, intent(in) :: nprojs
3789 integer, intent(in) :: pl(:)
3790 integer, intent(in) :: pm(:)
3791 integer, intent(in) :: pchannel(:)
3792 type(spline_t), allocatable, intent(out) :: spl(:)
3793 integer, allocatable, intent(out) :: ll(:)
3794 integer, allocatable, intent(out) :: mm(:)
3795 real(real64), intent(out) :: rcut
3796
3797 integer :: i, ri
3798 integer, allocatable :: map(:)
3799 real(real64), allocatable :: diff(:)
3800
3802
3803 safe_allocate(map(1:nprojs))
3804 call paw_onsite_pmat_map(this, nprojs, pl, pm, pchannel, map)
3805
3806 safe_allocate(spl(1:nprojs))
3807 safe_allocate(ll(1:nprojs))
3808 safe_allocate(mm(1:nprojs))
3809 safe_allocate(diff(1:this%nr))
3810 do i = 1, nprojs
3811 ri = this%p_rad(map(i)) ! radial partial wave paired with projector i
3812 diff(1:this%nr) = this%phi_ae(1:this%nr, ri) - this%phi_ps(1:this%nr, ri)
3813 ll(i) = pl(i)
3814 mm(i) = pm(i)
3815 call spline_init(spl(i))
3816 call spline_fit(this%nr, this%r, diff, spl(i))
3817 end do
3818 rcut = this%r(this%nr)
3819
3820 safe_deallocate_a(map)
3821 safe_deallocate_a(diff)
3822
3824 end subroutine paw_onsite_partial_wave_diff
3825
3826 ! ---------------------------------------------------------
3834 subroutine paw_onsite_get_kinetic_data(this, deltaK, p_rad, p_l, p_m)
3835 type(paw_sphere_t), intent(in) :: this
3836 real(real64), allocatable, intent(out) :: deltak(:,:)
3837 integer, allocatable, intent(out) :: p_rad(:)
3838 integer, allocatable, intent(out) :: p_l(:)
3839 integer, allocatable, intent(out) :: p_m(:)
3840
3842
3843 safe_allocate(deltak(1:this%nrad, 1:this%nrad))
3844 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)
3845 safe_allocate(p_rad(1:this%nlm))
3846 p_rad(1:this%nlm) = this%p_rad(1:this%nlm)
3847 safe_allocate(p_l(1:this%nlm))
3848 p_l(1:this%nlm) = this%p_l(1:this%nlm)
3849 safe_allocate(p_m(1:this%nlm))
3850 p_m(1:this%nlm) = this%p_m(1:this%nlm)
3851
3853 end subroutine paw_onsite_get_kinetic_data
3854
3855 ! ---------------------------------------------------------
3856 subroutine paw_onsite_end(this)
3857 type(paw_sphere_t), intent(inout) :: this
3859 push_sub(paw_onsite_end)
3860
3861 ! Only free the LibXC handles when the sphere owns them; when shared with the main xc
3862 ! (paw_onsite_set_shared_xc) the main xc owns and frees them.
3863 if (this%owns_xc_functl) then
3864 if (associated(this%xc_functl)) then
3865 call xc_functional_end(this%xc_functl(1))
3866 call xc_functional_end(this%xc_functl(2))
3867 safe_deallocate_p(this%xc_functl)
3868 end if
3869 else
3870 nullify(this%xc_functl)
3871 end if
3872
3873 safe_deallocate_a(this%r)
3874 safe_deallocate_a(this%r2)
3875 safe_deallocate_a(this%invr)
3876 safe_deallocate_a(this%wr)
3877 safe_deallocate_a(this%vwr)
3878 safe_deallocate_a(this%der_w)
3879 safe_deallocate_a(this%der_i0)
3880 safe_deallocate_a(this%der_wt)
3881 safe_deallocate_a(this%der_trow0)
3882 safe_deallocate_a(this%der_tn)
3883 safe_deallocate_a(this%lrad)
3884 safe_deallocate_a(this%rad_2j)
3885 safe_deallocate_a(this%phi_ae_small)
3886 safe_deallocate_a(this%phi_ae)
3887 safe_deallocate_a(this%phi_ps)
3888 safe_deallocate_a(this%p_rad)
3889 safe_deallocate_a(this%p_l)
3890 safe_deallocate_a(this%p_m)
3891 safe_deallocate_a(this%qaug)
3892 safe_deallocate_a(this%pair_ae)
3893 safe_deallocate_a(this%pair_ps)
3894 safe_deallocate_a(this%gnt_pair)
3895 safe_deallocate_a(this%gnt_l)
3896 safe_deallocate_a(this%gnt_lm)
3897 safe_deallocate_a(this%gnt_c)
3898 safe_deallocate_a(this%gnt_ptr)
3899 safe_deallocate_a(this%dpair_ae)
3900 safe_deallocate_a(this%dpair_ps)
3901 safe_deallocate_a(this%dqaug)
3902 safe_deallocate_a(this%nc_ae)
3903 safe_deallocate_a(this%nc_ps)
3904 safe_deallocate_a(this%nc_ae_der)
3905 safe_deallocate_a(this%nc_ps_der)
3906 safe_deallocate_a(this%tau_c_ae)
3907 safe_deallocate_a(this%tau_c_ps)
3908 safe_deallocate_a(this%nc_ae_lapl)
3909 safe_deallocate_a(this%nc_ps_lapl)
3910 safe_deallocate_a(this%dphi_ae)
3911 safe_deallocate_a(this%dphi_ps)
3912 safe_deallocate_a(this%d2phi_ae)
3913 safe_deallocate_a(this%d2phi_ps)
3914 safe_deallocate_a(this%vref_ae)
3915 safe_deallocate_a(this%vref_ps)
3916 safe_deallocate_a(this%kin_ae)
3917 safe_deallocate_a(this%kin_ps)
3918 safe_deallocate_a(this%wang)
3919 safe_deallocate_a(this%ylm)
3920 safe_deallocate_a(this%dylm_dth)
3921 safe_deallocate_a(this%dylm_dph)
3922 safe_deallocate_a(this%wylm)
3923 safe_deallocate_a(this%wdylm_dth)
3924 safe_deallocate_a(this%wdylm_dph)
3925 safe_deallocate_a(this%rhat)
3926 safe_deallocate_a(this%that)
3927 safe_deallocate_a(this%phat)
3929 pop_sub(paw_onsite_end)
3930 end subroutine paw_onsite_end
3931
3932end module paw_onsite_oct_m
3933
3934!! Local Variables:
3935!! mode: f90
3936!! coding: utf-8
3937!! 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:697
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_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:333
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 paw_xc_eval_block(this, np, ip0, bs, nsp, nsig, dens, sigma, exc, vrho, vsigma, lapl, tau, vlapl, vtau)
Evaluate the functionals on one contiguous block of bs points.
subroutine, public paw_onsite_init(this, ps, namespace, formulation, nspin, xc_ids, usexcnhat)
Build the per-species one-center data from a (PAW) pseudopotential.
Definition: paw_onsite.F90:340
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:185
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,...
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:683
subroutine, public paw_onsite_set_shared_xc(this, functl)
Share the main (mesh) xc functional handles with the one-center engine, instead of the sphere-owned c...
Definition: paw_onsite.F90:660
subroutine paw_radial_derivative_fd(this, f, df)
Radial derivative of a function sampled on the sphere grid: apply the banded Fornberg operator built ...
subroutine paw_onsite_init_xc(this, ps, namespace, xc_ids)
Initialize the exchange and correlation functionals from the ones stored in the pseudopotential.
Definition: paw_onsite.F90:769
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:188
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) ...
integer, parameter, public paw_kresse_joubert
PAW formulation: selects the compensation-charge / reference treatment.
Definition: paw_onsite.F90:184
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_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>,...
subroutine paw_xc_eval(this, np, nsp, nsig, dens, sigma, exc, vrho, vsigma, lapl, tau, vlapl, vtau, mgga_exc)
Functional-specific part of the one-center XC.
real(real64), parameter gaunt_tol
Definition: paw_onsite.F90:330
subroutine paw_xc_lda_process(this, dens, vr)
Rotate the LDA potential from the local spin frame back to the global fram.
subroutine, public paw_radial_derivative(r, f, df)
Radial derivative of a grid-sampled function via the cubic-spline derivative.
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,...
integer, parameter paw_xc_scheme_moments
second-order moment expansion around the spherical density
Definition: paw_onsite.F90:188
subroutine paw_xc(this, nlm_rho, nlm_der, rho, ncore, ae, with_comp, e_xc, dij)
Exchange-correlation on the radial x angular grid: LibXC evaluated pointwise, accumulating the XC ene...
subroutine, public paw_onsite_radial_grid(this, r)
Accessor: return (allocate) the one-center radial grid. Used by the standalone test to drive the radi...
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_mgga_dij(this, phi, dphi, gn, vr, vs, dij, vtau, d2phi, vlapl)
Unified meta-GGA one-center D matrix, built entirely from the partial-wave gradients so it is the exa...
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,...
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_quantities(this, rho, ae, phi, dphi, d2phi, ncore, ncder, dens, gn, tau, lapl)
One-center quantities from the partial waves.
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....
subroutine paw_onsite_init_mgga(this, ps, namespace, r_full, phi_ae_full, phi_ps_full)
Meta-GGA one-center data, built once at init.
Definition: paw_onsite.F90:853
subroutine, public paw_radial_second_derivative(r, f, d2f)
Radial second derivative of a grid-sampled function via the cubic-spline second derivative....
/* 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:1854
pure logical function, public ps_has_nlcc(ps)
Definition: ps.F90:1838
integer, parameter, public pseudo_exchange_unknown
Definition: pseudo.F90:192
integer, parameter, public pseudo_correlation_unknown
Definition: pseudo.F90:196
real(real64) function, public spline_eval(spl, x)
Definition: splines.F90:578
subroutine, public xc_functional_end(functl)
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:194
A type storing the information and data about a pseudopotential.
Definition: ps.F90:169
int true(void)