-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathm_pawxc.F90
6089 lines (5500 loc) · 223 KB
/
m_pawxc.F90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
!!****m* ABINIT/m_pawxc
!! NAME
!! m_pawxc
!!
!! FUNCTION
!! XC+PAW related operations
!!
!! COPYRIGHT
!! Copyright (C) 2013-2022 ABINIT group (MT, FJ, TR, GJ, TD)
!! This file is distributed under the terms of the
!! GNU General Public License, see ~abinit/COPYING
!! or http://www.gnu.org/copyleft/gpl.txt .
!!
!! NOTES
!! FOR DEVELOPERS: in order to preserve the portability of libPAW library,
!! please consult ~abinit/src/??_libpaw/libpaw-coding-rules.txt
!!
!! SOURCE
#include "libpaw.h"
module m_pawxc
USE_DEFS
USE_MSG_HANDLING
USE_MEMORY_PROFILING
#ifdef LIBPAW_ISO_C_BINDING
use iso_c_binding, only : c_ptr,c_loc,c_f_pointer
#endif
#ifdef HAVE_LIBPAW_ABINIT
use m_xcpositron, only : xcpositron
use m_drivexc, only : drivexc,size_dvxc,xcmult,mkdenpos
use m_xc_noncoll, only : rotate_mag,rotate_back_mag,rotate_back_mag_dfpt
#endif
#ifdef HAVE_XC_ABINIT
use m_drivexc, only : drivexc,size_dvxc,xcmult,mkdenpos
#endif
use m_libpaw_libxc
use m_pawang, only : pawang_type
use m_pawrad, only : pawrad_type,nderiv_gen,pawrad_deducer0,simp_gen
implicit none
private
public :: pawxc ! Compute xc correlation potential and energies inside a paw sphere. USE (r,theta,phi)
public :: pawxcpositron ! Compute electron-positron correlation potential and energies inside a PAW sphere. USE (r,theta,phi)
public :: pawxc_dfpt ! Compute first-order change of XC potential and contribution to
! 2nd-order change of XC energy inside a PAW sphere. USE (r,theta,phi)
public :: pawxcsum ! Compute useful sums of moments of densities needed to compute on-site contributions to XC energy and potential
public :: pawxcm ! Compute xc correlation potential and energies inside a paw sphere. USE (L,M) MOMENTS
public :: pawxcmpositron ! Compute electron-positron correlation potential and energies inside a PAW sphere. USE (L,M) MOMENTS
public :: pawxcm_dfpt ! Compute 1st-order change of XC potential and contrib
! to 2nd-order change of XC ene inside a PAW sphere. USE (L,M) MOMENTS
public :: pawxc_get_nkxc ! Compute size of XC kernel (Kxc) according to spin polarization and XC type
public :: pawxc_get_xclevel ! Get XC level (1=LDA, 2=GGA/mGGA, 3=TDDFT)
public :: pawxc_get_usekden ! Assess whether kinetic energy density is used in XC functional
public :: pawxc_get_uselaplacian ! Assess whether laplacian of density is used in XC functional
!Private procedures
private :: pawxcsph ! Compute XC energy and potential for a spherical density rho(r) given as (up,dn)
private :: pawxcsphpositron ! Compute electron-positron XC energy and potential for spherical densities rho_el(r) rho_pos(r)
private :: pawxcsph_dfpt ! Compute XC 1st-order potential for a 1st-order spherical density rho1(r)
private :: pawxc_rotate_mag ! Rotate a non-collinear density wrt a magnetization
private :: pawxc_rotate_back_mag ! Rotate back a collinear XC potential wrt a magnetization
private :: pawxc_rotate_back_mag_dfpt ! Rotate back a collinear 1st-order XC potential wrt a magnetization
!Wrappers
private :: pawxc_drivexc_wrapper ! wrapper for drivexc
private :: pawxc_mkdenpos_wrapper ! wrapper for mkdenpos
private :: pawxc_xcmult_wrapper ! wrapper for xcmult
private :: pawxc_size_dvxc_wrapper ! wrapper for size_dvxc
private :: pawxc_xcpositron_wrapper ! wrapper for xcpositron
!Zero of density
real(dp),parameter :: rho_min=tol14
!!***
CONTAINS !===========================================================
!!***
!!****f* m_pawxc/pawxc_xcpositron_wrapper
!! NAME
!! pawxc_xcpositron_wrapper
!!
!! FUNCTION
!! Compute electron-positron correlation potentials and energy density.
!! Used electron-positron correlation functional is controlled by ipawxc_xcpositron_wrapper argument.
!! Returns Fxc, Vxc_pos, Vxc_el from input rhor_pos and rhor_el for positron and electrons.
!!
!! INPUTS
!! grhoe2(ngr)=square of the gradient of electronic density rhoe (needed for GGA)
!! ixcpositron=type of electron-positron correlation functional:
!! 1 or -1: LDA zero positron density limit parametrized by Arponen & Pajanne
!! and provided by Boronski & Nieminen [1,2]
!! 11: LDA zero positron density limit parametrized by Arponen & Pajanne
!! and fitted by Sterne & Kaiser [1,3]
!! 2: LDA electron-positron correlation
!! provided by Puska, Seitsonen, and Nieminen [1,4]
!! 3: GGA zero positron density limit parametrized by Arponen & Pajanne
!! and provided by Boronski & Nieminen [1,2,5]
!! 31: GGA zero positron density limit parametrized by Arponen & Pajanne
!! and fitted by Sterne & Kaiser [1,3,5]
!! See references below
!! ngr=size of grho2 array (0 if LDA, npt if GGA)
!! npt=number of real space points on which density is provided
!! posdensity0_limit=True if we are in the zero positron density limit
!! rhoer(npt)=electron density (bohr^-3)
!! rhopr(npt)=positron density (bohr^-3)
!!
!! OUTPUT
!! fnxc(npt)=correlation energy per unit volume fxc
!! vxce(npt)=correlation potential for electron dfxc/drhoe (hartree)
!! vxcp(npt)=correlation potential for positron dfxc/drhop (hartree)
!! vxcegr(ngr)= 1/|gradRhoe| dfxc/d|gradRhoe| (empty if LDA, i.e. ngr=0)
!! Optional outputs:
!! dvxce(npt)=partial second derivatives of the xc energy wr to the electronic density
!! dvxce(:)=dVxce/dRhoe
!! dvxcp(npt)=partial second derivatives of the xc energy wr to the positronic density
!! dvxcp(:)=dVxcp/drhop
!!
!! NOTES
!! References for electron-positron correlation functionals:
!! [1] J. Arponen and E. Pajanne, Ann. Phys. (N.Y.) 121, 343 (1979) [[cite:Arponen1979a]].
!! [2] E. Boronski and R.M. Nieminen, Phys. Rev. B 34, 3820 (1986) [[cite:Boronski1986]].
!! [3] P.A. Sterne and J.H. Kaiser, Phys. Rev. B 43, 13892 (1991) [[cite:Sterne1991]].
!! [4] M.J. Puska, A.P. Seitsonen and R.M. Nieminen, Phys. Rev. B 52, 10947 (1994) [[cite:Puska1994]].
!! [5] B. Barbiellini, M.J. Puska, T. Torsti and R.M.Nieminen, Phys. Rev. B 51, 7341 (1995) [[cite:Barbiellini1995]]
!!
!! SOURCE
subroutine pawxc_xcpositron_wrapper(fnxc,grhoe2,ixcpositron,ngr,npt,posdensity0_limit,&
& rhoer,rhopr,vxce,vxcegr,vxcp,&
& dvxce,dvxcp) ! optional arguments
!Arguments ------------------------------------
!scalars
integer,intent(in) :: ixcpositron,ngr,npt
logical,intent(in) :: posdensity0_limit
!arrays
real(dp),intent(in) :: grhoe2(ngr),rhoer(npt),rhopr(npt)
real(dp),intent(out) :: fnxc(npt),vxce(npt),vxcegr(ngr),vxcp(npt)
real(dp),intent(out),optional :: dvxce(npt),dvxcp(npt)
!Local variables-------------------------------
! *************************************************************************
#if defined HAVE_LIBPAW_ABINIT
call pawxc_xcpositron_abinit()
#else
call pawxc_xcpositron_local()
#endif
!!***
contains
!!***
#if defined HAVE_LIBPAW_ABINIT
!!****f* pawxc_xcpositron_wrapper/pawxc_xcpositron_abinit
!! NAME
!! pawxc_xcpositron_abinit
!!
!! FUNCTION
!! ABINIT version of electron-positron correlation
!!
!! SOURCE
subroutine pawxc_xcpositron_abinit()
! *************************************************************************
if(present(dvxce) .and. present(dvxcp)) then
call xcpositron(fnxc,grhoe2,ixcpositron,ngr,npt,posdensity0_limit,rhoer,rhopr,vxce,vxcegr,vxcp,&
& dvxce=dvxce,dvxcp=dvxcp) ! optional arguments
elseif( present(dvxce) .and. .not. present(dvxcp)) then
call xcpositron(fnxc,grhoe2,ixcpositron,ngr,npt,posdensity0_limit,rhoer,rhopr,vxce,vxcegr,vxcp,&
& dvxce=dvxce) ! optional arguments
elseif( .not. present(dvxce) .and. present(dvxcp)) then
call xcpositron(fnxc,grhoe2,ixcpositron,ngr,npt,posdensity0_limit,rhoer,rhopr,vxce,vxcegr,vxcp,&
& dvxcp=dvxcp) ! optional arguments
else
call xcpositron(fnxc,grhoe2,ixcpositron,ngr,npt,posdensity0_limit,rhoer,rhopr,vxce,vxcegr,vxcp)
end if
end subroutine pawxc_xcpositron_abinit
!!***
#else
!!****f* pawxc_xcpositron_wrapper/pawxc_xcpositron_local
!! NAME
!! pawxc_xcpositron_local
!!
!! FUNCTION
!! Local version of electron-positron correlation (to use outside ABINIT)
!! NOT AVAILABLE
!!
!! SOURCE
subroutine pawxc_xcpositron_local()
character(len=*), parameter :: msg='xcpositron only available in ABINIT!'
! *************************************************************************
LIBPAW_BUG(msg)
end subroutine pawxc_xcpositron_local
!!***
#endif
end subroutine pawxc_xcpositron_wrapper
!!***
!----------------------------------------------------------------------
!!****f* m_pawxc/pawxc_size_dvxc_wrapper
!! NAME
!! pawxc_size_dvxc_wrapper
!!
!! FUNCTION
!! Give the sizes of the several arrays involved in exchange-correlation calculation
!! needed to allocated them for the drivexc routine
!!
!! INPUTS
!! ixc= choice of exchange-correlation scheme
!! order= gives the maximal derivative of Exc computed.
!! 1=usual value (return exc and vxc)
!! 2=also computes the kernel (return exc,vxc,kxc)
!! -2=like 2, except (to be described)
!! 3=also computes the derivative of the kernel (return exc,vxc,kxc,k3xc)
!! nspden= number of spin components
!! [xc_funcs(2)]= <type(libxc_functional_type)>
!! [add_tfw]= optional flag controling the addition of Weiszacker gradient correction to Thomas-Fermi XC energy
!!
!! OUTPUT
!! --- All optionals
!! [usegradient]= [flag] 1 if the XC functional needs the gradient of the density (grho2_updn)
!! [uselaplacian]= [flag] 1 if the XC functional needs the laplacian of the density (lrho_updn)
!! [usekden]= [flag] 1 if the XC functional needs the kinetic energy density (lrho_updn)
!! [nvxcgrho]= size of the array dvxcdgr(npts,nvxcgrho) (derivative of Exc wrt to gradient)
!! [nvxclrho]= size of the array dvxclpl(npts,nvxclrho) (derivative of Exc wrt to laplacian)
!! [nvxctau]= size of the array dvxctau(npts,nvxctau) (derivative of Exc wrt to kin. ener. density)
!! [ndvxc]= size of the array dvxc(npts,ndvxc) (second derivatives of Exc wrt to density and gradient)
!! [nd2vxc]= size of the array d2vxc(npts,nd2vxc) (third derivatives of Exc wrt density)
!!
!! SOURCE
subroutine pawxc_size_dvxc_wrapper(ixc,order,nspden,&
& usegradient,uselaplacian,usekden,&
& nvxcgrho,nvxclrho,nvxctau,ndvxc,nd2vxc)
!Arguments----------------------
integer,intent(in) :: ixc,nspden,order
integer,intent(out),optional :: nvxcgrho,nvxclrho,nvxctau,ndvxc,nd2vxc
integer,intent(out),optional :: usegradient,uselaplacian,usekden
!Local variables----------------
integer :: nvxcgrho_,nvxclrho_,nvxctau_,ndvxc_,nd2vxc_
integer :: usegradient_,uselaplacian_,usekden_
! *************************************************************************
#if defined HAVE_LIBPAW_ABINIT
call size_dvxc(ixc,order,nspden,&
& usegradient=usegradient_,uselaplacian=uselaplacian_,usekden=usekden_,&
nvxcgrho=nvxcgrho_,nvxclrho=nvxclrho_,nvxctau=nvxctau_,&
& ndvxc=ndvxc_,nd2vxc=nd2vxc_)
#else
call pawxc_size_dvxc_local()
#endif
if (present(usegradient)) usegradient=usegradient_
if (present(uselaplacian)) uselaplacian=uselaplacian_
if (present(usekden)) usekden=usekden_
if (present(nvxcgrho)) nvxcgrho=nvxcgrho_
if (present(nvxclrho)) nvxclrho=nvxclrho_
if (present(nvxctau)) nvxctau=nvxctau_
if (present(ndvxc)) ndvxc=ndvxc_
if (present(nd2vxc)) nd2vxc=nd2vxc_
!!***
#if ! defined HAVE_LIBPAW_ABINIT
contains
!!***
!!****f* pawxc_size_dvxc_wrapper/pawxc_size_dvxc_local
!! NAME
!! pawxc_size_dvxc_local
!!
!! FUNCTION
!! Local version of size_dvxc routine (to use outside ABINIT)
!!
!! SOURCE
subroutine pawxc_size_dvxc_local()
!Local variables----------------
logical :: need_gradient,need_kden,need_laplacian
! *************************************************************************
!Do we use the gradient?
need_gradient=((ixc>=11.and.ixc<=17).or.(ixc==23.or.ixc==24).or. &
& (ixc==26.or.ixc==27).or.(ixc>=31.and.ixc<=35).or. &
& (ixc==41.or.ixc==42).or.ixc==1402000)
if (ixc<0) then
if (libxc_functionals_isgga().or.libxc_functionals_ismgga().or. &
& libxc_functionals_is_hybrid()) need_gradient=.true.
end if
usegradient_=0 ; if (need_gradient) usegradient_=2*min(nspden,2)-1
!Do we use the laplacian?
need_laplacian=(ixc==32.or.ixc==35)
if (ixc<0) need_laplacian=libxc_functionals_needs_laplacian()
uselaplacian_=0 ; if (need_laplacian) uselaplacian_=min(nspden,2)
!Do we use the kinetic energy density?
need_kden=(ixc==31.or.ixc==34.or.ixc==35)
if (ixc<0) need_kden=libxc_functionals_ismgga()
usekden_=0 ; if (need_kden) usekden_=min(nspden,2)
!First derivative(s) of XC functional wrt gradient of density
nvxcgrho_=0
if (abs(order)>=1) then
if (need_gradient) nvxcgrho_=3
if (ixc==16.or.ixc==17.or.ixc==26.or.ixc==27) nvxcgrho_=2
end if
!First derivative(s) of XC functional wrt laplacian of density
nvxclrho_=0
if (abs(order)>=1) then
if (need_laplacian) nvxclrho_=min(nspden,2)
end if
!First derivative(s) of XC functional wrt kinetic energy density
nvxctau_=0
if (abs(order)>=1) then
if (need_kden) nvxctau_=min(nspden,2)
end if
!Second derivative(s) of XC functional wrt density
ndvxc_=0
if (abs(order)>=2) then
if (ixc==1.or.ixc==7.or.ixc==8.or.ixc==9.or.ixc==10.or.ixc==13.or. &
& ixc==21.or.ixc==22) then
ndvxc_=min(nspden,2)+1
else if ((ixc>=2.and.ixc<=6).or.(ixc>=31.and.ixc<=35).or.ixc==50) then
ndvxc_=1
else if (ixc==12.or.ixc==24) then
ndvxc_=8
else if (ixc==11.or.ixc==12.or.ixc==14.or.ixc==15.or. &
& ixc==23.or.ixc==41.or.ixc==42.or.ixc==1402000) then
ndvxc_=15
else if (ixc<0) then
if (libxc_functionals_has_kxc()) then
ndvxc_=2*min(nspden,2)+1 ; if (order==-2) ndvxc_=2
if (need_gradient) ndvxc_=15
end if
end if
end if
!Third derivative(s) of XC functional wrt density
nd2vxc_=0
if (abs(order)>=3) then
if (ixc==3.or.(ixc>=11.and.ixc<=15.and.ixc/=13).or. &
& ixc==23.or.ixc==24.or.ixc==41.or.ixc==42) then
nd2vxc_=1
else if ((ixc>=7.and.ixc<=10).or.ixc==13.or.ixc==1402000) then
nd2vxc_=3*min(nspden,2)-2
else if (ixc<0) then
if (libxc_functionals_has_k3xc()) then
if (.not.need_gradient) nd2vxc_=3*min(nspden,2)-2
end if
end if
end if
end subroutine pawxc_size_dvxc_local
!!***
#endif
end subroutine pawxc_size_dvxc_wrapper
!!***
!----------------------------------------------------------------------
!!****f* m_pawxc/pawxc_xcmult_wrapper
!! NAME
!! pawxc_xcmult_wrapper
!!
!! FUNCTION
!! In the case of GGA, multiply the different gradient of spin-density
!! by the derivative of the XC functional with respect
!! to the norm of the gradient, then divide it by the
!! norm of the gradient
!!
!! INPUTS
!! depsxc(nfft,nspgrad)=derivative of Exc with respect to the (spin-)density,
!! or to the norm of the gradient of the (spin-)density,
!! further divided by the norm of the gradient of the (spin-)density
!! The different components of depsxc will be
!! for nspden=1, depsxc(:,1)=d(rho.exc)/d(rho)
!! and if ngrad=2, depsxc(:,2)=1/2*1/|grad rho_up|*d(rho.exc)/d(|grad rho_up|)
!! + 1/|grad rho|*d(rho.exc)/d(|grad rho|)
!! (do not forget : |grad rho| /= |grad rho_up| + |grad rho_down|
!! for nspden=2, depsxc(:,1)=d(rho.exc)/d(rho_up)
!! depsxc(:,2)=d(rho.exc)/d(rho_down)
!! and if ngrad=2, depsxc(:,3)=1/|grad rho_up|*d(rho.exc)/d(|grad rho_up|)
!! depsxc(:,4)=1/|grad rho_down|*d(rho.exc)/d(|grad rho_down|)
!! depsxc(:,5)=1/|grad rho|*d(rho.exc)/d(|grad rho|)
!! nfft=(effective) number of FFT grid points (for this processor)
!! ngrad = must be 2
!! nspden=number of spin-density components
!! nspgrad=number of spin-density and spin-density-gradient components
!!
!! OUTPUT
!! (see side effects)
!!
!! SIDE EFFECTS
!! rhonow(nfft,nspden,ngrad*ngrad)=
!! at input :
!! electron (spin)-density in real space and its gradient,
!! either on the unshifted grid (if ishift==0,
!! then equal to rhor), or on the shifted grid
!! rhonow(:,:,1)=electron density in electrons/bohr**3
!! rhonow(:,:,2:4)=gradient of electron density in el./bohr**4
!! at output :
!! rhonow(:,:,2:4) has been multiplied by the proper factor,
!! described above.
!!
!! SOURCE
subroutine pawxc_xcmult_wrapper(depsxc,nfft,ngrad,nspden,nspgrad,rhonow)
!Arguments ------------------------------------
!scalars
integer,intent(in) :: nfft,ngrad,nspden,nspgrad
!arrays
real(dp),intent(in) :: depsxc(nfft,nspgrad)
real(dp),intent(inout) :: rhonow(nfft,nspden,ngrad*ngrad)
! *************************************************************************
#if defined HAVE_LIBPAW_ABINIT
call xcmult(depsxc,nfft,ngrad,nspden,nspgrad,rhonow)
#else
call pawxc_xcmult_local()
#endif
!!***
#if ! defined HAVE_LIBPAW_ABINIT
contains
!!***
!!****f* pawxc_xcmult_wrapper/pawxc_xcmult_local
!! NAME
!! pawxc_xcmult_local
!!
!! FUNCTION
!! Local version of xcmult routine (to use outside ABINIT)
!!
!! SOURCE
subroutine pawxc_xcmult_local()
!Local variables-------------------------------
!scalars
integer :: idir,ifft
real(dp) :: rho_tot,rho_up
! *************************************************************************
do idir=1,3
if(nspden==1)then
!$OMP PARALLEL DO PRIVATE(ifft) SHARED(depsxc,idir,nfft,rhonow)
do ifft=1,nfft
rhonow(ifft,1,1+idir)=rhonow(ifft,1,1+idir)*depsxc(ifft,2)
end do
else
! In the spin-polarized case, there are more factors to take into account
!$OMP PARALLEL DO PRIVATE(ifft,rho_tot,rho_up) SHARED(depsxc,idir,nfft,rhonow)
do ifft=1,nfft
rho_tot=rhonow(ifft,1,1+idir)
rho_up =rhonow(ifft,2,1+idir)
rhonow(ifft,1,1+idir)=rho_up *depsxc(ifft,3) + rho_tot*depsxc(ifft,5)
rhonow(ifft,2,1+idir)=(rho_tot-rho_up)*depsxc(ifft,4)+ rho_tot*depsxc(ifft,5)
end do
end if ! nspden==1
end do ! End loop on directions
end subroutine pawxc_xcmult_local
!!***
#endif
end subroutine pawxc_xcmult_wrapper
!!***
!----------------------------------------------------------------------
!!****f* m_pawxc/pawxc_mkdenpos_wrapper
!! NAME
!! pawxc_mkdenpos_wrapper
!!
!! FUNCTION
!! Make a ground-state density positive everywhere :
!! when the density (or spin-density) is smaller than xc_denpos,
!! set it to the value of xc_denpos
!!
!! INPUTS
!! nfft=(effective) number of FFT grid points (for this processor)
!! nspden=number of spin-density components (max. 2)
!! option=0 if density rhonow is stored as (up,dn)
!! 1 if density rhonow is stored as (up+dn,up)
!! Active only when nspden=2
!! xc_denpos= lowest allowed density (usually for the computation of the XC functionals)
!!
!! OUTPUT
!!
!! SIDE EFFECTS
!! Input/output
!! iwarn=At input: iwarn=0 a warning will be printed when rho is negative
!! iwarn>0 no warning will be printed out
!! At output: iwarn is increased by 1
!! rhonow(nfft,nspden)=electron (spin)-density in real space,
!! either on the unshifted grid (if ishift==0,
!! then equal to rhor),or on the shifted grid
!!
!! SOURCE
subroutine pawxc_mkdenpos_wrapper(iwarn,nfft,nspden,option,rhonow,xc_denpos)
!Arguments ------------------------------------
!scalars
integer,intent(in) :: nfft,nspden,option
integer,intent(inout) :: iwarn
real(dp),intent(in) :: xc_denpos
!arrays
real(dp),intent(inout) :: rhonow(nfft,nspden)
! *************************************************************************
#if defined HAVE_LIBPAW_ABINIT
call mkdenpos(iwarn,nfft,nspden,option,rhonow,xc_denpos)
#else
call pawxc_mkdenpos_local()
#endif
!!***
#if ! defined HAVE_LIBPAW_ABINIT
contains
!!***
!!****f* pawxc_mkdenpos_wrapper/pawxc_mkdenpos_local
!! NAME
!! pawxc_mkdenpos_local
!!
!! FUNCTION
!! Local version of mkdenpos routine (to use outside ABINIT)
!!
!! SOURCE
subroutine pawxc_mkdenpos_local()
!Local variables-------------------------------
!scalars
integer :: ifft,ispden,numneg
real(dp) :: rhotmp,worst
character(len=500) :: msg
!arrays
real(dp) :: rho(2)
! *************************************************************************
numneg=0;worst=zero
if(nspden==1)then
! Non spin-polarized
!$OMP PARALLEL DO PRIVATE(ifft,rhotmp) REDUCTION(MIN:worst) REDUCTION(+:numneg) SHARED(nfft,rhonow)
do ifft=1,nfft
rhotmp=rhonow(ifft,1)
if(rhotmp<xc_denpos)then
if(rhotmp<-xc_denpos)then
! This case is probably beyond machine precision considerations
worst=min(worst,rhotmp)
numneg=numneg+1
end if
rhonow(ifft,1)=xc_denpos
end if
end do
else if (nspden==2) then
! Spin-polarized
! rhonow is stored as (up,dn)
if (option==0) then
!$OMP PARALLEL DO PRIVATE(ifft,ispden,rho,rhotmp) REDUCTION(MIN:worst) REDUCTION(+:numneg) &
!$OMP&SHARED(nfft,nspden,rhonow)
do ifft=1,nfft
! For polarized case, rho(1) is spin-up density, rho(2) is spin-down density
rho(1)=rhonow(ifft,1)
rho(2)=rhonow(ifft,2)
do ispden=1,nspden
if (rho(ispden)<xc_denpos) then
if (rho(ispden)<-xc_denpos) then
! This case is probably beyond machine precision considerations
worst=min(worst,rho(ispden))
numneg=numneg+1
end if
rhonow(ifft,ispden)=xc_denpos
end if
end do
end do
! rhonow is stored as (up+dn,up)
else if (option==1) then
!$OMP PARALLEL DO PRIVATE(ifft,ispden,rho,rhotmp) &
!$OMP&REDUCTION(MIN:worst) REDUCTION(+:numneg) &
!$OMP&SHARED(nfft,nspden,rhonow)
do ifft=1,nfft
! For polarized case, rho(1) is spin-up density, rho(2) is spin-down density
rho(1)=rhonow(ifft,2)
rho(2)=rhonow(ifft,1)-rho(1)
do ispden=1,nspden
if (rho(ispden)<xc_denpos) then
if (rho(ispden)<-xc_denpos) then
! This case is probably beyond machine precision considerations
worst=min(worst,rho(ispden))
numneg=numneg+1
end if
rho(ispden)=xc_denpos
rhonow(ifft,1)=rho(1)+rho(2)
rhonow(ifft,2)=rho(1)
end if
end do
end do
end if ! option
else
msg='nspden>2 not allowed !'
LIBPAW_BUG(msg)
end if ! End choice between non-spin polarized and spin-polarized.
if (numneg>0) then
if (iwarn==0) then
write(msg,'(a,i10,a,a,a,es10.2,a,e10.2,a,a,a,a)')&
& 'Density went too small (lower than xc_denpos) at',numneg,' points',ch10,&
& 'and was set to xc_denpos=',xc_denpos,'. Lowest was ',worst,'.',ch10,&
& 'Likely due to too low boxcut or too low ecut for','pseudopotential core charge.'
LIBPAW_WARNING(msg)
end if
iwarn=iwarn+1
end if
end subroutine pawxc_mkdenpos_local
!!***
#endif
end subroutine pawxc_mkdenpos_wrapper
!!***
!----------------------------------------------------------------------
!!****f* m_pawxc/pawxc_get_xclevel
!! NAME
!! pawxc_get_xclevel
!!
!! FUNCTION
!! Give the eXchange-Correlation "level" (1=LDA, 2=GGA/mGGA, 3=TDDFT)
!!
!! INPUTS
!! ixc= choice of exchange-correlation scheme
!!
!! SOURCE
function pawxc_get_xclevel(ixc)
!Arguments ------------------------------------
integer,intent(in) :: ixc
integer :: pawxc_get_xclevel
! *************************************************************************
pawxc_get_xclevel=0
!ABINIT
if ((1<=ixc.and.ixc<=10).or.(30<=ixc.and.ixc<=39).or.(ixc==50)) pawxc_get_xclevel=1 ! ABINIT LDA
if ((11<=ixc.and.ixc<=19).or.(23<=ixc.and.ixc<=29).or.ixc==1402000) pawxc_get_xclevel=2 ! ABINIT GGA
if (20<=ixc.and.ixc<=22) pawxc_get_xclevel=3 ! ABINIT TDDFT kernel tests
if (ixc>=31.and.ixc<=35) pawxc_get_xclevel=2 ! ABINIT internal fake mGGA
if (ixc>=41.and.ixc<=42) pawxc_get_xclevel=2 ! ABINIT internal hybrids using GGA
!LibXC functionals
if (ixc<0) then
pawxc_get_xclevel=1
if (libxc_functionals_isgga()) pawxc_get_xclevel=2
if (libxc_functionals_ismgga()) pawxc_get_xclevel=2
if (libxc_functionals_is_hybrid()) pawxc_get_xclevel=2
end if
end function pawxc_get_xclevel
!!***
!----------------------------------------------------------------------
!!****f* m_pawxc/pawxc_get_usekden
!! NAME
!! pawxc_get_usekden
!!
!! FUNCTION
!! Check if kinetic energy density is used in XC functional
!!
!! INPUTS
!! ixc= choice of exchange-correlation scheme
!!
!! SOURCE
function pawxc_get_usekden(ixc)
!Arguments ------------------------------------
integer,intent(in) :: ixc
integer :: pawxc_get_usekden
! *************************************************************************
pawxc_get_usekden=0
if (ixc<0) then
if (libxc_functionals_ismgga()) pawxc_get_usekden=1
else if (ixc==31.or.ixc==34.or.ixc==35) then
pawxc_get_usekden=1
end if
end function pawxc_get_usekden
!!***
!----------------------------------------------------------------------
!!****f* m_pawxc/pawxc_get_uselaplacian
!! NAME
!! pawxc_get_uselaplacian
!!
!! FUNCTION
!! Check if laplacian of density is used in XC functional
!!
!! INPUTS
!! ixc= choice of exchange-correlation scheme
!!
!! SOURCE
function pawxc_get_uselaplacian(ixc)
!Arguments ------------------------------------
integer,intent(in) :: ixc
integer :: pawxc_get_uselaplacian
! *************************************************************************
pawxc_get_uselaplacian=0
if (ixc<0) then
if (libxc_functionals_needs_laplacian()) pawxc_get_uselaplacian=1
else if (ixc==32.or.ixc==35) then
pawxc_get_uselaplacian=1
end if
end function pawxc_get_uselaplacian
!!***
!----------------------------------------------------------------------
!!****f* m_pawxc/pawxc
!! NAME
!! pawxc
!!
!! FUNCTION
!! Start from the density or spin-density, and compute xc correlation
!! potential and energies inside a paw sphere.
!! USE THE DENSITY OVER A WHOLE SPHERICAL GRID (r,theta,phi)
!! Driver of XC functionals.
!!
!! INPUTS
!! corexc(nrad)=core density on radial grid
!! ixc= choice of exchange-correlation scheme
!! lm_size=size of density array rhor (see below)
!! lmselect(lm_size)=select the non-zero LM-moments of input density rhor
!! nhat(nrad,lm_size,nspden)=compensation density
!! (total in 1st half and spin-up in 2nd half if nspden=2)
!! nkxc=second dimension of the kxc array. If /=0, the exchange-correlation kernel must be computed
!! non_magnetic_xc= if true, handle density/potential as non-magnetic (even if it is)
!! nrad=size of radial mesh for densities/potentials (might be different from pawrad%mesh_size)
!! nspden=number of spin-density components
!! option=0 compute both XC energies (direct+double-counting) and potential
!! 1 compute only XC potential
!! 2 compute only XC energies (direct+double-counting)
!! 3 compute only XC energy by direct scheme
!! 4 compute only XC energy by direct scheme for spherical part of the density
!! 5 compute only XC potential for spherical part of the density
!! pawang <type(pawang_type)>=paw angular mesh and related data
!! pawrad <type(pawrad_type)>=paw radial mesh and related data
!! rhor(nrad,lm_size,nspden)=electron density in real space in electrons/bohr**3
!! (total in 1st half and spin-up in 2nd half if nspden=2)
!! usecore= 1 if core density has to be used in Exc/Vxc ; 0 otherwise
!! usexcnhat= 0 if compensation density does not have to be used
!! 1 if compensation density has to be used in double counting energy term only
!! 2 if compensation density (nhat) has to be used in Exc/Vxc and double counting energy term
!! xclevel= XC functional level
!! xc_denpos= lowest allowed density (usually for the computation of the XC functionals)
!! ----- Optional arguments -----
!! [coretau(nrad*usekden)]= core kinetic energy density (optional)
!! [taur(nrad,lm_size,nspden*usekden)]= kinetic energy density on radial mesh (optional)
!!
!! OUTPUT
!! == if option=0, 2, 3, or 4 ==
!! enxc=returned exchange and correlation energy (hartree)
!! == if option=0 or 2 ==
!! enxcdc=returned exchange-cor. contribution to double-counting energy
!! == if option=0, 1 or 5 ==
!! vxc(nrad,pawang%angl_size,nspden)=xc potential
!! (spin up in 1st half and spin-down in 2nd half if nspden=2)
!! == if option=0, 1 or 5 and usekden=1 ==
!! [vxctau(nrad,pawang%angl_size,nspden*usekden)]=xc potential due to kinetic energy density
!! (spin up in 1st half and spin-down in 2nd half if nspden=2) (optional)
!! == if nkxc>0 ==
!! kxc(nrad,pawang%angl_size,nkxc)=xc kernel
!! (see notes below for nkxc)
!! == if nk3xc>0 ==
!! k3xc(nrad,pawang%angl_size,nk3xc)= derivative of xc kernel
!! (see notes below for nk3xc)
!!
!! NOTES
!! Content of Kxc array:
!! ===== if LDA
!! if nspden==1: kxc(:,1)= d2Exc/drho2
!! (kxc(:,2)= d2Exc/drho_up drho_dn)
!! if nspden>=2: kxc(:,1)= d2Exc/drho_up drho_up
!! kxc(:,2)= d2Exc/drho_up drho_dn
!! kxc(:,3)= d2Exc/drho_dn drho_dn
!! if nspden==4: kxc(:,4:6)= (m_x, m_y, m_z) (magnetization)
!! ===== if GGA
!! if nspden==1:
!! kxc(:,1)= d2Exc/drho2
!! kxc(:,2)= 1/|grad(rho)| dExc/d|grad(rho)|
!! kxc(:,3)= 1/|grad(rho)| d2Exc/d|grad(rho)| drho
!! kxc(:,4)= 1/|grad(rho)| * d/d|grad(rho)| ( 1/|grad(rho)| dExc/d|grad(rho)| )
!! kxc(:,5)= gradx(rho)
!! kxc(:,6)= grady(rho)
!! kxc(:,7)= gradz(rho)
!! if nspden>=2:
!! kxc(:,1)= d2Exc/drho_up drho_up
!! kxc(:,2)= d2Exc/drho_up drho_dn
!! kxc(:,3)= d2Exc/drho_dn drho_dn
!! kxc(:,4)= 1/|grad(rho_up)| dEx/d|grad(rho_up)|
!! kxc(:,5)= 1/|grad(rho_dn)| dEx/d|grad(rho_dn)|
!! kxc(:,6)= 1/|grad(rho_up)| d2Ex/d|grad(rho_up)| drho_up
!! kxc(:,7)= 1/|grad(rho_dn)| d2Ex/d|grad(rho_dn)| drho_dn
!! kxc(:,8)= 1/|grad(rho_up)| * d/d|grad(rho_up)| ( 1/|grad(rho_up)| dEx/d|grad(rho_up)| )
!! kxc(:,9)= 1/|grad(rho_dn)| * d/d|grad(rho_dn)| ( 1/|grad(rho_dn)| dEx/d|grad(rho_dn)| )
!! kxc(:,10)=1/|grad(rho)| dEc/d|grad(rho)|
!! kxc(:,11)=1/|grad(rho)| d2Ec/d|grad(rho)| drho_up
!! kxc(:,12)=1/|grad(rho)| d2Ec/d|grad(rho)| drho_dn
!! kxc(:,13)=1/|grad(rho)| * d/d|grad(rho)| ( 1/|grad(rho)| dEc/d|grad(rho)| )
!! kxc(:,14)=gradx(rho_up)
!! kxc(:,15)=gradx(rho_dn)
!! kxc(:,16)=grady(rho_up)
!! kxc(:,17)=grady(rho_dn)
!! kxc(:,18)=gradz(rho_up)
!! kxc(:,19)=gradz(rho_dn)
!! if nspden==4:
!! kxc(:,20:22)= (m_x, m_y, m_z) (magnetization)
!! Dimension of K3xc:
!! ===== if LDA (xclevel=1) :
!! if nspden==1: return k3xc(:,1)=d3Exc/drho3
!! if nspden>=2, return k3xc(:,1)=d3Exc/drho_up drho_up drho_up
!! k3xc(:,2)=d3Exc/drho_up drho_up drho_dn
!! k3xc(:,3)=d3Exc/drho_up drho_dn drho_dn
!! k3xc(:,4)=d3Exc/drho_dn drho_dn drho_dn
!!
!! SOURCE
subroutine pawxc(corexc,enxc,enxcdc,hyb_mixing,ixc,kxc,k3xc,lm_size,lmselect,nhat,nkxc,nk3xc,non_magnetic_xc,&
& nrad,nspden,option,pawang,pawrad,rhor,usecore,usexcnhat,vxc,xclevel,xc_denpos,&
& coretau,taur,vxctau) ! optional arguments
!Arguments ------------------------------------
!scalars
integer,intent(in) :: ixc,lm_size,nkxc,nk3xc,nrad,nspden,option,usecore,usexcnhat,xclevel
logical,intent(in) :: non_magnetic_xc
real(dp),intent(in) :: hyb_mixing,xc_denpos
real(dp),intent(out) :: enxc,enxcdc
type(pawang_type),intent(in) :: pawang
type(pawrad_type),intent(in) :: pawrad
!arrays
logical,intent(in) :: lmselect(lm_size)
real(dp),intent(in) :: corexc(nrad)
real(dp),intent(in) :: nhat(nrad,lm_size,nspden*((usexcnhat+1)/2))
real(dp),intent(in),target :: rhor(nrad,lm_size,nspden)
real(dp),intent(in),target,optional:: coretau(:),taur(:,:,:)
real(dp),intent(out) :: kxc(nrad,pawang%angl_size,nkxc)
real(dp),intent(out) :: k3xc(nrad,pawang%angl_size,nk3xc)
real(dp),intent(out),target :: vxc(nrad,pawang%angl_size,nspden)
real(dp),intent(out),target,optional :: vxctau(:,:,:)
!Local variables-------------------------------
!scalars
integer,parameter :: mu(3,3)=reshape([4,9,8,9,5,7,8,7,6],[3,3]) ! Voigt indices
integer :: ii,ilm,ipts,ir,ispden,iwarn,jj,lm_size_eff,ndvxc,nd2vxc,ngrad
integer :: nkxc_updn,npts,nspden_eff,nspden_updn,nspgrad,nu
integer :: nvxcgrho,nvxclrho,nvxctau,order
integer :: usecoretau,usegradient,usekden,uselaplacian
logical :: need_vxctau,with_taur
real(dp) :: enxcr,factor,vxcrho
character(len=500) :: msg
!arrays
real(dp),allocatable :: dgxc(:),dlxc(:),d2lxc(:),dnexcdn(:,:),drho(:),d2rho(:),drhocore(:)
real(dp),allocatable :: vxci(:,:),vxci_grho(:,:),vxci_lrho(:,:),vxci_tau(:,:)
real(dp),allocatable :: dvxci(:,:),d2vxci(:,:),dylmdr(:,:,:)
real(dp),allocatable :: exci(:),ff(:),grho2_updn(:,:),gxc(:,:,:,:),lxc(:,:,:)
real(dp),allocatable :: rhoarr(:,:),rho_updn(:,:),lrho_updn(:,:),lrhocore(:)
real(dp),allocatable :: tauarr(:,:),tau_updn(:,:),ylmlapl(:,:)
real(dp),allocatable,target :: mag(:,:,:),rhohat(:,:,:),rhonow(:,:,:)
real(dp),pointer :: mag_(:,:),rho_(:,:,:),tau_(:,:,:),vxctau_(:,:,:)
real(dp), LIBPAW_CONTIGUOUS pointer :: vxc_diag(:,:),vxc_nc(:,:),vxc_updn(:,:,:)
#ifdef LIBPAW_ISO_C_BINDING
type(C_PTR) :: cptr
#endif
! *************************************************************************
!----------------------------------------------------------------------
!----- Check options
!----------------------------------------------------------------------
!Some dimensions
nkxc_updn=merge(nkxc-3,nkxc,nkxc==6.or.nkxc==22)
!Compatibility tests
if(nspden==4.and.nk3xc>0) then
msg='K3xc for nspden=4 not implemented!'
LIBPAW_ERROR(msg)
end if
if(nk3xc>0.and.nkxc_updn==0) then
msg='nkxc must be non-zero if nk3xc is!'
LIBPAW_ERROR(msg)
end if
if(nspden==4.and.xclevel==2.and..not.non_magnetic_xc) then
msg='GGA/mGGA for nspden=4 not fully implemented! (only works if usepawu=4 or pawxcdev/=0)'
LIBPAW_ERROR(msg)
end if
if(pawang%angl_size==0) then
msg='pawang%angl_size=0!'
LIBPAW_BUG(msg)
end if
if(.not.allocated(pawang%ylmr)) then
msg='pawang%ylmr must be allocated!'
LIBPAW_BUG(msg)
end if
if(xclevel==2.and.(.not.allocated(pawang%ylmrgr))) then
msg='pawang%ylmrgr must be allocated!'
LIBPAW_BUG(msg)
end if
if(option==4.or.option==5) then
if (pawang%angl_size/=1) then
msg='When option=4 or 5, pawang%angl_size must be 1!'
LIBPAW_BUG(msg)
end if
if (pawang%ylm_size/=1) then
msg='When option=4 or 5, pawang%ylm_size must be 1!'
LIBPAW_BUG(msg)
end if
if (abs(pawang%anginit(1,1)-one)>tol12.or.abs(pawang%anginit(2,1))>tol12.or. &
& abs(pawang%anginit(3,1))>tol12) then
msg='When option=4 or 5, pawang%anginit must be (1 0 0)!'
LIBPAW_BUG(msg)
end if
end if
if (option/=1.and.option/=5) then
if (nrad<pawrad%int_meshsz) then
msg='When option=0,2,3,4, nrad must be greater than pawrad%int_meshsz!'
LIBPAW_BUG(msg)
end if
end if
!----------------------------------------------------------------------
!----- Initializations
!----------------------------------------------------------------------
iwarn=0
nspden_updn=min(nspden,2)
nspden_eff=nspden_updn;if (nspden==4.and.xclevel==2) nspden_eff=4
npts=pawang%angl_size
lm_size_eff=min(lm_size,pawang%ylm_size)
ngrad=1;if(xclevel==2)ngrad=2
nspgrad=0;if (xclevel==2) nspgrad=3*nspden_updn-1
if (option/=1.and.option/=5) enxc=zero
if (option==0.or.option==2) enxcdc=zero
if (option/=3.and.option/=4) vxc(:,:,:)=zero
if (present(vxctau).and.option/=3.and.option/=4) vxctau(:,:,:)=zero
if (nkxc>0) kxc(:,:,:)=zero
if (nk3xc>0) k3xc(:,:,:)=zero
order=1;if (nkxc_updn>0) order=2;if (nk3xc>0) order=3 ! to which der. of the energy the computation must be done
if (xclevel==0.or.ixc==0) then