-
Notifications
You must be signed in to change notification settings - Fork 0
/
m_libpaw_libxc.F90
2127 lines (1853 loc) · 68.5 KB
/
m_libpaw_libxc.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_libpaw_libxc_funcs
!! NAME
!! m_libpaw_libxc_funcs
!!
!! FUNCTION
!! Module containing interfaces to the LibXC library, for exchange
!! correlation potentials and energies.
!!
!! COPYRIGHT
!! Copyright (C) 2015-2022 ABINIT group (MO, MT)
!! 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
!! This file comes directly from m_libpaw_libxc.F90 module delivered with ABINIT.
!! It defines a structured datatype (libpaw_libxc_type) and associated methods
!! to initialize/finalize it and get properties from it.
!! * It uses by default a global variable (paw_xc_global, libpaw_libxc_type) which has
!! to be initialized/finalized with the libpaw_libxc_init and libpaw_libxc_end methods.
!! * It is also possible to define a local (private) variable of type libpaw_libxc_type.
!! For that, the different methods have to be called with an extra optional
!! argument (called xc_funcs in this example):
!! !!!!! call libpaw_libxc_init(ixc,nspden,xc_funcs)
!! !!!!! call libpaw_libxc_end(xc_funcs)
!!
!! SOURCE
!Need iso C bindings provided by the compiler
#define LIBPAW_ISO_C_BINDING 1
#include "libpaw.h"
module m_libpaw_libxc_funcs
USE_DEFS
USE_MSG_HANDLING
USE_MEMORY_PROFILING
!ISO C bindings are mandatory
#ifdef LIBPAW_ISO_C_BINDING
use iso_c_binding
#endif
implicit none
private
!Public functions
public :: libpaw_libxc_check ! Check if the code has been compiled with libXC
public :: libpaw_libxc_init ! Initialize the desired XC functional, from libXC
public :: libpaw_libxc_end ! End usage of libXC functional
public :: libpaw_libxc_fullname ! Return full name of the XC functional
public :: libpaw_libxc_getrefs ! Get references of a XC functional
public :: libpaw_libxc_getid ! Return identifer of a XC functional, from its name
public :: libpaw_libxc_family_from_id ! Retrieve family of a XC functional, from its id
public :: libpaw_libxc_ixc ! The value of ixc used to initialize the XC functionals
public :: libpaw_libxc_getvxc ! Return XC potential and energy, from input density
public :: libpaw_libxc_isgga ! Return TRUE if the XC functional is GGA or meta-GGA
public :: libpaw_libxc_ismgga ! Return TRUE if the XC functional is meta-GGA
public :: libpaw_libxc_needs_laplacian ! Return TRUE if functional uses LAPLACIAN
public :: libpaw_libxc_is_hybrid ! Return TRUE if the XC functional is hybrid
public :: libpaw_libxc_is_hybrid_from_id ! Return TRUE if a XC functional is hybrid, from its id
public :: libpaw_libxc_has_kxc ! Return TRUE if Kxc (3rd der) is available for the XC functional
public :: libpaw_libxc_has_k3xc ! Return TRUE if K3xc (4th der) is available for the XC functional
public :: libpaw_libxc_nspin ! The number of spin components for the XC functionals
public :: libpaw_libxc_get_hybridparams ! Retrieve parameter(s) of a hybrid functional
public :: libpaw_libxc_set_hybridparams ! Change parameter(s) of a hybrid functional
public :: libpaw_libxc_gga_from_hybrid ! Return the id of the XC-GGA used for the hybrid
!Private functions
private :: libpaw_libxc_constants_load ! Load libXC constants from C headers
private :: libpaw_libxc_compute_tb09 ! Compute c parameter for Tran-Blaha 2009 functional
#ifdef LIBPAW_ISO_C_BINDING
private :: char_f_to_c ! Convert a string from Fortran to C
private :: char_c_to_f ! Convert a string from C to Fortran
#endif
!Public constants (use libpaw_libxc_constants_load to init them)
integer,public,save :: LIBPAW_XC_FAMILY_UNKNOWN = -1
integer,public,save :: LIBPAW_XC_FAMILY_LDA = 1
integer,public,save :: LIBPAW_XC_FAMILY_GGA = 2
integer,public,save :: LIBPAW_XC_FAMILY_MGGA = 4
integer,public,save :: LIBPAW_XC_FAMILY_LCA = 8
integer,public,save :: LIBPAW_XC_FAMILY_OEP = 16
integer,public,save :: LIBPAW_XC_FAMILY_HYB_GGA = 32
integer,public,save :: LIBPAW_XC_FAMILY_HYB_MGGA = 64
integer,public,save :: LIBPAW_XC_FAMILY_HYB_LDA =128
integer,public,save :: LIBPAW_XC_FLAGS_HAVE_EXC = 1
integer,public,save :: LIBPAW_XC_FLAGS_HAVE_VXC = 2
integer,public,save :: LIBPAW_XC_FLAGS_HAVE_FXC = 4
integer,public,save :: LIBPAW_XC_FLAGS_HAVE_KXC = 8
integer,public,save :: LIBPAW_XC_FLAGS_HAVE_LXC = 16
integer,public,save :: LIBPAW_XC_FLAGS_NEEDS_LAPLACIAN= 32768
integer,public,save :: LIBPAW_XC_EXCHANGE = 0
integer,public,save :: LIBPAW_XC_CORRELATION = 1
integer,public,save :: LIBPAW_XC_EXCHANGE_CORRELATION = 2
integer,public,save :: LIBPAW_XC_KINETIC = 3
integer,public,save :: LIBPAW_XC_SINGLE_PRECISION = 0
logical,private,save :: libpaw_xc_constants_initialized=.false.
!XC functional public type
type,public :: libpaw_libxc_type
integer :: id ! identifier
integer :: family ! LDA, GGA, etc.
integer :: kind ! EXCHANGE, CORRELATION, etc.
integer :: nspin ! # of spin components
integer :: abi_ixc ! Abinit IXC id for this functional
logical :: has_exc ! TRUE is exc is available for the functional
logical :: has_vxc ! TRUE is vxc is available for the functional
logical :: has_fxc ! TRUE is fxc is available for the functional
logical :: has_kxc ! TRUE is kxc is available for the functional
logical :: needs_laplacian ! TRUE is functional needs laplacian of density
logical :: is_hybrid ! TRUE is functional is a hybrid functional
real(dp) :: hyb_mixing ! Hybrid functional: mixing factor of Fock contribution (default=0)
real(dp) :: hyb_mixing_sr ! Hybrid functional: mixing factor of SR Fock contribution (default=0)
real(dp) :: hyb_range ! Range (for separation) for a hybrid functional (default=0)
real(dp) :: xc_tb09_c ! Special TB09 functional parameter
real(dp) :: sigma_threshold ! Value of a threshold to be applied on density gradient (sigma)
! (temporary dur to a libxc bug) - If <0, apply no filter
#ifdef LIBPAW_ISO_C_BINDING
type(C_PTR),pointer :: conf => null() ! C pointer to the functional itself
#endif
end type libpaw_libxc_type
!List of functionals on which a filter has to be applied on sigma (density gradient)
! This should be done by libXC via _set_sigma_threshold but this is not (libXC 6)
real(dp),parameter :: libpaw_sigma_threshold_def = 1.0e-25_dp
integer,parameter :: libpaw_n_sigma_filtered = 17
character(len=28) :: libpaw_sigma_filtered(libpaw_n_sigma_filtered) = &
& ['XC_HYB_GGA_XC_HSE03 ','XC_HYB_GGA_XC_HSE06 ','XC_HYB_GGA_XC_HJS_PBE ',&
& 'XC_HYB_GGA_XC_HJS_PBE_SOL ','XC_HYB_GGA_XC_HJS_B88 ','XC_HYB_GGA_XC_HJS_B97X ',&
& 'XC_HYB_GGA_XC_LRC_WPBEH ','XC_HYB_GGA_XC_LRC_WPBE ','XC_HYB_GGA_XC_LC_WPBE ',&
& 'XC_HYB_GGA_XC_HSE12 ','XC_HYB_GGA_XC_HSE12S ','XC_HYB_GGA_XC_HSE_SOL ',&
& 'XC_HYB_GGA_XC_LC_WPBE_WHS ','XC_HYB_GGA_XC_LC_WPBEH_WHS ','XC_HYB_GGA_XC_LC_WPBE08_WHS ',&
& 'XC_HYB_GGA_XC_LC_WPBESOL_WHS','XC_HYB_GGA_XC_WHPBE0 ']
!----------------------------------------------------------------------
!Private global XC functional
type(libpaw_libxc_type),target,save :: paw_xc_global(2)
!----------------------------------------------------------------------
!Interfaces for C bindings
#ifdef LIBPAW_ISO_C_BINDING
interface
integer(C_INT) function xc_func_init(xc_func,functional,nspin) bind(C,name="xc_func_init")
use iso_c_binding, only : C_INT,C_PTR
integer(C_INT),value :: functional,nspin
type(C_PTR) :: xc_func
end function xc_func_init
end interface
!
interface
subroutine xc_func_end(xc_func) bind(C,name="xc_func_end")
use iso_c_binding, only : C_PTR
type(C_PTR) :: xc_func
end subroutine xc_func_end
end interface
!
interface
integer(C_INT) function xc_functional_get_number(name) &
& bind(C,name="xc_functional_get_number")
use iso_c_binding, only : C_INT,C_PTR
type(C_PTR),value :: name
end function xc_functional_get_number
end interface
!
interface
type(C_PTR) function xc_functional_get_name(number) &
& bind(C,name="xc_functional_get_name")
use iso_c_binding, only : C_INT,C_PTR
integer(C_INT),value :: number
end function xc_functional_get_name
end interface
!
interface
integer(C_INT) function xc_family_from_id(id,family,number) &
& bind(C,name="xc_family_from_id")
use iso_c_binding, only : C_INT,C_PTR
integer(C_INT),value :: id
type(C_PTR),value :: family,number
end function xc_family_from_id
end interface
!
interface
subroutine xc_hyb_cam_coef(xc_func,omega,alpha,beta) &
& bind(C,name="xc_hyb_cam_coef")
use iso_c_binding, only : C_DOUBLE,C_PTR
real(C_DOUBLE) :: omega,alpha,beta
type(C_PTR) :: xc_func
end subroutine xc_hyb_cam_coef
end interface
!
interface
subroutine libpaw_xc_get_lda(xc_func,np,rho,zk,vrho,v2rho2,v3rho3) &
& bind(C,name="libpaw_xc_get_lda")
use iso_c_binding, only : C_INT,C_PTR
integer(C_INT),value :: np
type(C_PTR),value :: rho,zk,vrho,v2rho2,v3rho3
type(C_PTR) :: xc_func
end subroutine libpaw_xc_get_lda
end interface
!
interface
subroutine libpaw_xc_get_gga(xc_func,np,rho,sigma,zk,vrho,vsigma, &
& v2rho2,v2rhosigma,v2sigma2,v3rho3,v3rho2sigma,v3rhosigma2,v3sigma3) &
& bind(C,name="libpaw_xc_get_gga")
use iso_c_binding, only : C_INT,C_PTR
integer(C_INT),value :: np
type(C_PTR),value :: rho,sigma,zk,vrho,vsigma,v2rho2,v2rhosigma,v2sigma2, &
& v3rho3,v3rho2sigma,v3rhosigma2,v3sigma3
type(C_PTR) :: xc_func
end subroutine libpaw_xc_get_gga
end interface
!
interface
subroutine libpaw_xc_get_mgga(xc_func,np,rho,sigma,lapl,tau,zk,vrho,vsigma,vlapl,vtau, &
& v2rho2,v2rhosigma,v2rholapl,v2rhotau,v2sigma2,v2sigmalapl, &
& v2sigmatau,v2lapl2,v2lapltau,v2tau2) &
& bind(C,name="libpaw_xc_get_mgga")
use iso_c_binding, only : C_INT,C_PTR
integer(C_INT),value :: np
type(C_PTR),value :: rho,sigma,lapl,tau,zk,vrho,vsigma,vlapl,vtau, &
& v2rho2,v2sigma2,v2lapl2,v2tau2,v2rhosigma,v2rholapl,v2rhotau, &
& v2sigmalapl,v2sigmatau,v2lapltau
type(C_PTR) :: xc_func
end subroutine libpaw_xc_get_mgga
end interface
!
interface
subroutine libpaw_xc_func_set_params(xc_func,params,n_params) bind(C)
use iso_c_binding, only : C_INT,C_DOUBLE,C_PTR
integer(C_INT),value :: n_params
real(C_DOUBLE) :: params(*)
type(C_PTR) :: xc_func
end subroutine libpaw_xc_func_set_params
end interface
!
interface
subroutine libpaw_xc_func_set_density_threshold(xc_func,dens_threshold) bind(C)
use iso_c_binding, only : C_DOUBLE,C_PTR
real(C_DOUBLE) :: dens_threshold
type(C_PTR) :: xc_func
end subroutine libpaw_xc_func_set_density_threshold
end interface
!
interface
subroutine libpaw_xc_func_set_sig_threshold(xc_func,sigma_threshold) bind(C)
use iso_c_binding, only : C_DOUBLE,C_PTR
real(C_DOUBLE) :: sigma_threshold
type(C_PTR) :: xc_func
end subroutine libpaw_xc_func_set_sig_threshold
end interface
!
interface
integer(C_INT) function libpaw_xc_func_is_hybrid_from_id(func_id) bind(C)
use iso_c_binding, only : C_INT
integer(C_INT),value :: func_id
end function libpaw_xc_func_is_hybrid_from_id
end interface
!
interface
subroutine libpaw_xc_get_singleprecision_constant(xc_cst_singleprecision) &
& bind(C,name="libpaw_xc_get_singleprecision_constant")
use iso_c_binding, only : C_INT
integer(C_INT) :: xc_cst_singleprecision
end subroutine libpaw_xc_get_singleprecision_constant
end interface
!
interface
subroutine libpaw_xc_get_family_constants(xc_cst_unknown,xc_cst_lda,xc_cst_gga, &
& xc_cst_mgga,xc_cst_lca,xc_cst_oep,xc_cst_hyb_gga,xc_cst_hyb_mgga, &
& xc_cst_hyb_lda) &
& bind(C,name="libpaw_xc_get_family_constants")
use iso_c_binding, only : C_INT
integer(C_INT) :: xc_cst_unknown,xc_cst_lda,xc_cst_gga,xc_cst_mgga, &
& xc_cst_lca,xc_cst_oep,xc_cst_hyb_gga,xc_cst_hyb_mgga, &
& xc_cst_hyb_lda
end subroutine libpaw_xc_get_family_constants
end interface
!
interface
subroutine libpaw_xc_get_flags_constants(xc_cst_flags_have_exc,xc_cst_flags_have_vxc, &
xc_cst_flags_have_fxc,xc_cst_flags_have_kxc,xc_cst_flags_have_lxc, &
& xc_cst_flags_needs_lapl) &
& bind(C,name="libpaw_xc_get_flags_constants")
use iso_c_binding, only : C_INT
integer(C_INT) :: xc_cst_flags_have_exc,xc_cst_flags_have_vxc,xc_cst_flags_have_fxc, &
& xc_cst_flags_have_kxc,xc_cst_flags_have_lxc,xc_cst_flags_needs_lapl
end subroutine libpaw_xc_get_flags_constants
end interface
!
interface
subroutine libpaw_xc_get_kind_constants(xc_cst_exchange,xc_cst_correlation, &
& xc_cst_exchange_correlation,xc_cst_kinetic) &
& bind(C,name="libpaw_xc_get_kind_constants")
use iso_c_binding, only : C_INT
integer(C_INT) :: xc_cst_exchange,xc_cst_correlation, &
& xc_cst_exchange_correlation,xc_cst_kinetic
end subroutine libpaw_xc_get_kind_constants
end interface
!
interface
type(C_PTR) function libpaw_xc_func_type_malloc() &
& bind(C,name="libpaw_xc_func_type_malloc")
use iso_c_binding, only : C_PTR
end function libpaw_xc_func_type_malloc
end interface
!
interface
subroutine libpaw_xc_func_type_free(xc_func) &
& bind(C,name="libpaw_xc_func_type_free")
use iso_c_binding, only : C_PTR
type(C_PTR) :: xc_func
end subroutine libpaw_xc_func_type_free
end interface
!
interface
type(C_PTR) function libpaw_xc_get_info_name(xc_func) &
& bind(C,name="libpaw_xc_get_info_name")
use iso_c_binding, only : C_PTR
type(C_PTR) :: xc_func
end function libpaw_xc_get_info_name
end interface
!
interface
type(C_PTR) function libpaw_xc_get_info_refs(xc_func,iref) &
& bind(C,name="libpaw_xc_get_info_refs")
use iso_c_binding, only : C_INT,C_PTR
type(C_PTR) :: xc_func
integer(C_INT) :: iref
end function libpaw_xc_get_info_refs
end interface
!
interface
integer(C_INT) function libpaw_xc_get_info_flags(xc_func) &
& bind(C,name="libpaw_xc_get_info_flags")
use iso_c_binding, only : C_INT,C_PTR
type(C_PTR) :: xc_func
end function libpaw_xc_get_info_flags
end interface
!
interface
integer(C_INT) function libpaw_xc_get_info_kind(xc_func) &
& bind(C,name="libpaw_xc_get_info_kind")
use iso_c_binding, only : C_INT,C_PTR
type(C_PTR) :: xc_func
end function libpaw_xc_get_info_kind
end interface
#endif
contains
!!***
!----------------------------------------------------------------------
!!****f* m_libpaw_libxc_funcs/libpaw_libxc_constants_load
!! NAME
!! libpaw_libxc_constants_load
!!
!! FUNCTION
!! Load libXC constants from C headers
!!
!! SOURCE
subroutine libpaw_libxc_constants_load()
!Local variables-------------------------------
#if defined LIBPAW_HAVE_LIBXC && defined LIBPAW_ISO_C_BINDING
integer(C_INT) :: i1,i2,i3,i4,i5,i6,i7,i8,i9
#endif
! *************************************************************************
#if defined LIBPAW_HAVE_LIBXC && defined LIBPAW_ISO_C_BINDING
call libpaw_xc_get_singleprecision_constant(i1)
LIBPAW_XC_SINGLE_PRECISION = int(i1)
call libpaw_xc_get_family_constants(i1,i2,i3,i4,i5,i6,i7,i8,i9)
LIBPAW_XC_FAMILY_UNKNOWN = int(i1)
LIBPAW_XC_FAMILY_LDA = int(i2)
LIBPAW_XC_FAMILY_GGA = int(i3)
LIBPAW_XC_FAMILY_MGGA = int(i4)
LIBPAW_XC_FAMILY_LCA = int(i5)
LIBPAW_XC_FAMILY_OEP = int(i6)
LIBPAW_XC_FAMILY_HYB_GGA = int(i7)
LIBPAW_XC_FAMILY_HYB_MGGA = int(i8)
LIBPAW_XC_FAMILY_HYB_LDA = int(i9)
call libpaw_xc_get_flags_constants(i1,i2,i3,i4,i5,i6)
LIBPAW_XC_FLAGS_HAVE_EXC = int(i1)
LIBPAW_XC_FLAGS_HAVE_VXC = int(i2)
LIBPAW_XC_FLAGS_HAVE_FXC = int(i3)
LIBPAW_XC_FLAGS_HAVE_KXC = int(i4)
LIBPAW_XC_FLAGS_HAVE_LXC = int(i5)
LIBPAW_XC_FLAGS_NEEDS_LAPLACIAN= int(i6)
call libpaw_xc_get_kind_constants(i1,i2,i3,i4)
LIBPAW_XC_EXCHANGE = int(i1)
LIBPAW_XC_CORRELATION = int(i2)
LIBPAW_XC_EXCHANGE_CORRELATION = int(i3)
LIBPAW_XC_KINETIC = int(i4)
libpaw_xc_constants_initialized=.true.
#endif
end subroutine libpaw_libxc_constants_load
!!***
!----------------------------------------------------------------------
!!****f* m_libpaw_libxc_funcs/libpaw_libxc_check
!! NAME
!! libpaw_libxc_check
!!
!! FUNCTION
!! Check if the code has been compiled with an usable version of libXC
!!
!! INPUTS
!! [stop_if_error]=optional flag; if TRUE the code stops if libXC is not correctly used
!!
!! SOURCE
function libpaw_libxc_check(stop_if_error)
!Arguments ------------------------------------
logical :: libpaw_libxc_check
logical,intent(in),optional :: stop_if_error
!Local variables-------------------------------
character(len=100) :: msg
! *************************************************************************
#if defined LIBPAW_HAVE_LIBXC
#if defined LIBPAW_ISO_C_BINDING
if (.not.libpaw_xc_constants_initialized) call libpaw_libxc_constants_load()
if (LIBPAW_XC_SINGLE_PRECISION==1) then
libpaw_libxc_check=.false.
msg='LibXC should be compiled with double precision!'
end if
#else
libpaw_libxc_check=.false.
msg='LibXC cannot be used without ISO_C_BINDING support by the Fortran compiler!'
#endif
#else
libpaw_libxc_check=.false.
msg='LibPAW was not compiled with LibXC support.'
#endif
if (present(stop_if_error)) then
if (stop_if_error.and.trim(msg)/="") then
LIBPAW_ERROR(msg)
end if
end if
end function libpaw_libxc_check
!!***
!----------------------------------------------------------------------
!!****f* m_libpaw_libxc_funcs/libpaw_libxc_init
!! NAME
!! libpaw_libxc_init
!!
!! FUNCTION
!! Initialize the desired XC functional, from LibXC.
!! * Call the LibXC initializer
!! * Fill preliminary fields in module structures.
!!
!! INPUTS
!! ixc=XC code for Abinit
!! nspden=number of spin-density components
!! [xc_tb09_c]=special argument for the Tran-Blaha 2009 functional
!!
!! SIDE EFFECTS
!! [xc_functionals(2)]=<type(libpaw_libxc_type)>, optional argument
!! XC functionals to initialize
!!
!! SOURCE
subroutine libpaw_libxc_init(ixc,nspden,xc_functionals,&
& xc_tb09_c) ! optional argument
!Arguments ------------------------------------
integer, intent(in) :: nspden
integer, intent(in) :: ixc
real(dp),intent(in),optional :: xc_tb09_c
type(libpaw_libxc_type),intent(inout),optional,target :: xc_functionals(2)
!Local variables-------------------------------
integer :: ii,jj,nspden_eff
character(len=500) :: msg
type(libpaw_libxc_type),pointer :: xc_func
#if defined LIBPAW_HAVE_LIBXC && defined LIBPAW_ISO_C_BINDING
integer :: flags
integer(C_INT) :: func_id_c,iref_c,npar_c,nspin_c,success_c
real(C_DOUBLE) :: alpha_c,beta_c,omega_c,param_c(1)
character(kind=C_CHAR,len=1),pointer :: strg_c
type(C_PTR) :: func_ptr_c
#endif
! *************************************************************************
!Check libXC
if (.not.libpaw_libxc_check(stop_if_error=.true.)) return
if (.not.libpaw_xc_constants_initialized) call libpaw_libxc_constants_load()
nspden_eff=min(nspden,2)
!Select XC functional(s) identifiers
if (present(xc_functionals)) then
xc_functionals(1)%id = -ixc/1000
xc_functionals(2)%id = -ixc + (ixc/1000)*1000
else
paw_xc_global(1)%id = -ixc/1000
paw_xc_global(2)%id = -ixc + (ixc/1000)*1000
end if
do ii = 1,2
! Select XC functional
if (present(xc_functionals)) then
xc_func => xc_functionals(ii)
else
xc_func => paw_xc_global(ii)
end if
xc_func%abi_ixc=ixc !Save abinit value for reference
xc_func%family=LIBPAW_XC_FAMILY_UNKNOWN
xc_func%kind=-1
xc_func%nspin=nspden_eff
xc_func%has_exc=.false.
xc_func%has_vxc=.false.
xc_func%has_fxc=.false.
xc_func%has_kxc=.false.
xc_func%needs_laplacian=.false.
xc_func%is_hybrid=.false.
xc_func%hyb_mixing=zero
xc_func%hyb_mixing_sr=zero
xc_func%hyb_range=zero
xc_func%xc_tb09_c=99.99_dp
xc_func%sigma_threshold=-one
if (xc_func%id<=0) cycle
! Get XC functional family
xc_func%family=libpaw_libxc_family_from_id(xc_func%id)
if (xc_func%family/=LIBPAW_XC_FAMILY_LDA .and. &
& xc_func%family/=LIBPAW_XC_FAMILY_GGA .and. &
& xc_func%family/=LIBPAW_XC_FAMILY_MGGA.and. &
& xc_func%family/=LIBPAW_XC_FAMILY_HYB_GGA) then
write(msg, '(a,i8,2a,i8,6a)' )&
& 'Invalid IXC = ',ixc,ch10,&
& 'The LibXC functional family ',xc_func%family,&
& 'is currently unsupported by LibPAW',ch10,&
& '(-1 means the family is unknown to the LibXC itself)',ch10,&
& 'Please consult the LibXC documentation',ch10
LIBPAW_ERROR(msg)
end if
#if defined LIBPAW_HAVE_LIBXC && defined LIBPAW_ISO_C_BINDING
! Allocate functional
func_ptr_c=libpaw_xc_func_type_malloc()
call c_f_pointer(func_ptr_c,xc_func%conf)
! Initialize functional
func_id_c=int(xc_func%id,kind=C_INT)
nspin_c=int(nspden_eff,kind=C_INT)
success_c=xc_func_init(xc_func%conf,func_id_c,nspin_c)
if (success_c/=0) then
msg='Error in libXC functional initialization!'
LIBPAW_ERROR(msg)
end if
! Special treatment for LDA_C_XALPHA functional
if (xc_func%id==libpaw_libxc_getid('XC_LDA_C_XALPHA')) then
param_c(1)=real(zero,kind=C_DOUBLE);npar_c=int(1,kind=C_INT)
call libpaw_xc_func_set_params(xc_func%conf,param_c,npar_c)
end if
! Special treatment for XC_MGGA_X_TB09 functional
if (xc_func%id==libpaw_libxc_getid('XC_MGGA_X_TB09')) then
if (.not.present(xc_tb09_c)) then
msg='xc_tb09_c argument is mandatory for TB09 functional!'
LIBPAW_BUG(msg)
end if
xc_func%xc_tb09_c=xc_tb09_c
end if
! Get functional kind
xc_func%kind=int(libpaw_xc_get_info_kind(xc_func%conf))
! Get functional flags
flags=int(libpaw_xc_get_info_flags(xc_func%conf))
xc_func%has_exc=(iand(flags,LIBPAW_XC_FLAGS_HAVE_EXC)>0)
xc_func%has_vxc=(iand(flags,LIBPAW_XC_FLAGS_HAVE_VXC)>0)
xc_func%has_fxc=(iand(flags,LIBPAW_XC_FLAGS_HAVE_FXC)>0)
xc_func%has_kxc=(iand(flags,LIBPAW_XC_FLAGS_HAVE_KXC)>0)
! Retrieve parameters for metaGGA functionals
if (xc_func%family==LIBPAW_XC_FAMILY_MGGA.or. &
& xc_func%family==LIBPAW_XC_FAMILY_HYB_MGGA) then
xc_func%needs_laplacian=(iand(flags,LIBPAW_XC_FLAGS_NEEDS_LAPLACIAN)>0)
end if
! Retrieve parameters for hybrid functionals
xc_func%is_hybrid=(libpaw_xc_func_is_hybrid_from_id(xc_func%id)==1)
if (xc_func%is_hybrid) then
call xc_hyb_cam_coef(xc_func%conf,omega_c,alpha_c,beta_c)
xc_func%hyb_mixing=real(alpha_c,kind=dp)
xc_func%hyb_mixing_sr=real(beta_c,kind=dp)
xc_func%hyb_range=real(omega_c,kind=dp)
end if
! Some functionals need a filter to be applied on sigma (density gradient)
! because libXC v6 doesn't implement sigma_threshold
if (xc_func%is_hybrid) then
do jj=1,libpaw_n_sigma_filtered
if (xc_func%id==libpaw_libxc_getid(trim(libpaw_sigma_filtered(jj)))) then
xc_func%sigma_threshold=libpaw_sigma_threshold_def
end if
end do
end if
! Dump functional information
call c_f_pointer(libpaw_xc_get_info_name(xc_func%conf),strg_c)
call char_c_to_f(strg_c,msg)
call wrtout(std_out,msg,'COLL')
iref_c=0
do while (iref_c>=0)
call c_f_pointer(libpaw_xc_get_info_refs(xc_func%conf,iref_c),strg_c)
if (associated(strg_c)) then
call char_c_to_f(strg_c,msg)
call wrtout(std_out,msg,'COLL')
iref_c=iref_c+1
else
iref_c=-1
end if
end do
#else
if (.False.) write(std_out,*)xc_tb09_c
#endif
end do
end subroutine libpaw_libxc_init
!!***
!----------------------------------------------------------------------
!!****f* m_libpaw_libxc_funcs/libpaw_libxc_end
!! NAME
!! libpaw_libxc_end
!!
!! FUNCTION
!! End usage of LibXC functional. Call LibXC end function,
!! and deallocate module contents.
!!
!! SIDE EFFECTS
!! [xc_functionals(2)]=<type(libpaw_libxc_type)>, optional argument
!! XC functionals to initialize
!!
!! SOURCE
subroutine libpaw_libxc_end(xc_functionals)
!Arguments ------------------------------------
type(libpaw_libxc_type),intent(inout),optional,target :: xc_functionals(2)
!Local variables-------------------------------
integer :: ii
type(libpaw_libxc_type),pointer :: xc_func
! *************************************************************************
do ii = 1,2
! Select XC functional
if (present(xc_functionals)) then
xc_func => xc_functionals(ii)
else
xc_func => paw_xc_global(ii)
end if
if (xc_func%id <= 0) cycle
xc_func%id=-1
xc_func%family=-1
xc_func%kind=-1
xc_func%nspin=1
xc_func%abi_ixc=huge(0)
xc_func%has_exc=.false.
xc_func%has_vxc=.false.
xc_func%has_fxc=.false.
xc_func%has_kxc=.false.
xc_func%needs_laplacian=.false.
xc_func%is_hybrid=.false.
xc_func%hyb_mixing_sr=zero
xc_func%hyb_range=zero
xc_func%xc_tb09_c=99.99_dp
xc_func%sigma_threshold=-one
#if defined LIBPAW_HAVE_LIBXC && defined LIBPAW_ISO_C_BINDING
if (associated(xc_func%conf)) then
call xc_func_end(xc_func%conf)
call libpaw_xc_func_type_free(c_loc(xc_func%conf))
end if
#endif
end do
end subroutine libpaw_libxc_end
!!***
!----------------------------------------------------------------------
!!****f* m_libpaw_libxc_funcs/libpaw_libxc_fullname
!! NAME
!! libpaw_libxc_fullname
!!
!! FUNCTION
!! Return full name of the XC functional
!!
!! INPUTS
!! [xc_functionals(2)]=<type(libpaw_libxc_type)>, optional argument
!! XC functionals to initialize
!!
!! SOURCE
function libpaw_libxc_fullname(xc_functionals)
!Arguments ------------------------------------
character(len=100) :: libpaw_libxc_fullname
type(libpaw_libxc_type),intent(in),optional,target :: xc_functionals(2)
!Local variables-------------------------------
integer :: nxc
type(libpaw_libxc_type),pointer :: xc_funcs(:)
#if defined LIBPAW_HAVE_LIBXC && defined LIBPAW_ISO_C_BINDING
character(len=100) :: xcname
character(kind=C_CHAR,len=1),pointer :: strg_c
#endif
! *************************************************************************
libpaw_libxc_fullname='No XC functional'
if (present(xc_functionals)) then
xc_funcs => xc_functionals
else
xc_funcs => paw_xc_global
end if
nxc=size(xc_funcs)
if (nxc<1) return
#if defined LIBPAW_HAVE_LIBXC && defined LIBPAW_ISO_C_BINDING
if (nxc<2) then
if (xc_funcs(1)%id /= 0) then
call c_f_pointer(xc_functional_get_name(xc_funcs(1)%id),strg_c)
call char_c_to_f(strg_c,libpaw_libxc_fullname)
end if
else if (xc_funcs(1)%id <= 0) then
if (xc_funcs(2)%id /= 0) then
call c_f_pointer(xc_functional_get_name(xc_funcs(2)%id),strg_c)
call char_c_to_f(strg_c,libpaw_libxc_fullname)
end if
else if (xc_funcs(2)%id <= 0) then
if (xc_funcs(1)%id /= 0) then
call c_f_pointer(xc_functional_get_name(xc_funcs(1)%id),strg_c)
call char_c_to_f(strg_c,libpaw_libxc_fullname)
end if
else
call c_f_pointer(xc_functional_get_name(xc_funcs(1)%id),strg_c)
call char_c_to_f(strg_c,libpaw_libxc_fullname)
call c_f_pointer(xc_functional_get_name(xc_funcs(2)%id),strg_c)
call char_c_to_f(strg_c,xcname)
libpaw_libxc_fullname=trim(libpaw_libxc_fullname)//'+'//trim(xcname)
end if
libpaw_libxc_fullname=trim(libpaw_libxc_fullname)
#endif
end function libpaw_libxc_fullname
!!***
!----------------------------------------------------------------------
!!****f* m_libpaw_libxc_funcs/libpaw_libxc_getrefs
!! NAME
!! libpaw_libxc_getrefs
!!
!! FUNCTION
!! Return the reference(s) of a XC functional
!!
!! INPUTS
!! xc_functional=<type(libpaw_libxc_type)>, handle for XC functional
!!
!! OUTPUT
!! xcrefs(:)= references(s) of the functional
!!
!! SOURCE
subroutine libpaw_libxc_getrefs(xcrefs,xc_functional)
!Arguments ------------------------------------
character(len=*),intent(out) :: xcrefs(:)
type(libpaw_libxc_type),intent(in) :: xc_functional
!Local variables-------------------------------
#if defined LIBPAW_HAVE_LIBXC && defined HAVE_FC_ISO_C_BINDING
integer(C_INT) :: iref_c
character(kind=C_CHAR,len=1),pointer :: strg_c
#endif
! *************************************************************************
xcrefs(:)=''
#if defined LIBPAW_HAVE_LIBXC && defined HAVE_FC_ISO_C_BINDING
iref_c=0
do while (iref_c>=0.and.iref_c<size(xcrefs))
call c_f_pointer(libpaw_xc_get_info_refs(xc_functional%conf,iref_c),strg_c)
if (associated(strg_c)) then
call char_c_to_f(strg_c,xcrefs(iref_c+1))
iref_c=iref_c+1
else
iref_c=-1
end if
end do
#else
if (.False.) write(std_out,*)xc_functional%id
#endif
end subroutine libpaw_libxc_getrefs
!!***
!----------------------------------------------------------------------
!!****f* m_libpaw_libxc_funcs/libpaw_libxc_family_from_id
!! NAME
!! libpaw_libxc_family_from_id
!!
!! FUNCTION
!! Return family of a XC functional from its id
!!
!! INPUTS
!! xcid= id of a LibXC functional
!!
!! SOURCE
function libpaw_libxc_family_from_id(xcid)
!Arguments ------------------------------------
integer :: libpaw_libxc_family_from_id
integer,intent(in) :: xcid
!Local variables-------------------------------
#if defined LIBPAW_HAVE_LIBXC && defined LIBPAW_ISO_C_BINDING
integer(C_INT) :: xcid_c
#endif
! *************************************************************************
#if defined LIBPAW_HAVE_LIBXC && defined LIBPAW_ISO_C_BINDING
xcid_c=int(xcid,kind=C_INT)
libpaw_libxc_family_from_id=int(xc_family_from_id(xcid_c,C_NULL_PTR,C_NULL_PTR))
#else
libpaw_libxc_family_from_id=-1
if (.false.) write(std_out,*) xcid
#endif
end function libpaw_libxc_family_from_id
!!***
!----------------------------------------------------------------------
!!****f* m_libpaw_libxc_funcs/libpaw_libxc_getid
!! NAME
!! libpaw_libxc_getid
!!
!! FUNCTION
!! Return identifer of a XC functional from its name
!! Return -1 if undefined
!!
!! INPUTS
!! xcname= string containing the name of a XC functional
!!
!! SOURCE
function libpaw_libxc_getid(xcname)
!Arguments ------------------------------------
integer :: libpaw_libxc_getid
character(len=*),intent(in) :: xcname
!Local variables-------------------------------
#if defined LIBPAW_HAVE_LIBXC && defined LIBPAW_ISO_C_BINDING
character(len=256) :: str
character(kind=C_CHAR,len=1),target :: name_c(len_trim(xcname)+1)
character(kind=C_CHAR,len=1),target :: name_c_xc(len_trim(xcname)-2)
type(C_PTR) :: name_c_ptr
#endif
! *************************************************************************
#if defined LIBPAW_HAVE_LIBXC && defined LIBPAW_ISO_C_BINDING
str=trim(xcname)
if (xcname(1:3)=="XC_".or.xcname(1:3)=="xc_") then
str=xcname(4:);name_c_xc=char_f_to_c(str)
name_c_ptr=c_loc(name_c_xc)
else
name_c=char_f_to_c(str)
name_c_ptr=c_loc(name_c)
end if
libpaw_libxc_getid=int(xc_functional_get_number(name_c_ptr))
#else
libpaw_libxc_getid=-1
if (.false.) write(std_out,*) xcname
#endif
end function libpaw_libxc_getid
!!***
!----------------------------------------------------------------------
!!****f* m_libpaw_libxc_funcs/libpaw_libxc_ixc
!! NAME
!! libpaw_libxc_ixc
!!
!! FUNCTION
!! Return the value of ixc used to initialize the XC structure
!!
!! INPUTS
!! [xc_functionals(2)]=<type(libpaw_libxc_type)>, optional argument
!! XC functionals to initialize
!!
!! SOURCE
function libpaw_libxc_ixc(xc_functionals)
!Arguments ------------------------------------
integer :: libpaw_libxc_ixc
type(libpaw_libxc_type),intent(in),optional :: xc_functionals(2)
! *************************************************************************
if (present(xc_functionals)) then
libpaw_libxc_ixc=xc_functionals(1)%abi_ixc
else
libpaw_libxc_ixc=paw_xc_global(1)%abi_ixc
end if
end function libpaw_libxc_ixc
!!***
!----------------------------------------------------------------------
!!****f* m_libpaw_libxc_funcs/libpaw_libxc_isgga
!! NAME
!! libpaw_libxc_isgga
!!
!! FUNCTION
!! Test function to identify whether the presently used functional
!! is a GGA or not
!!
!! INPUTS
!! [xc_functionals(2)]=<type(libpaw_libxc_type)>, optional argument
!! XC functionals to initialize
!!
!! SOURCE
function libpaw_libxc_isgga(xc_functionals)
!Arguments ------------------------------------
logical :: libpaw_libxc_isgga
type(libpaw_libxc_type),intent(in),optional :: xc_functionals(2)
! *************************************************************************
libpaw_libxc_isgga = .false.
if (.not.libpaw_xc_constants_initialized) call libpaw_libxc_constants_load()
if (present(xc_functionals)) then
libpaw_libxc_isgga=(any(xc_functionals%family==LIBPAW_XC_FAMILY_GGA) .or. &
& any(xc_functionals%family==LIBPAW_XC_FAMILY_HYB_GGA))
else
libpaw_libxc_isgga=(any(paw_xc_global%family==LIBPAW_XC_FAMILY_GGA) .or. &
& any(paw_xc_global%family==LIBPAW_XC_FAMILY_HYB_GGA))
end if
end function libpaw_libxc_isgga
!!***
!----------------------------------------------------------------------
!!****f* m_libpaw_libxc_funcs/libpaw_libxc_ismgga
!! NAME
!! libpaw_libxc_ismgga
!!
!! FUNCTION
!! Test function to identify whether the presently used functional
!! is a Meta-GGA or not
!!
!! INPUTS
!! [xc_functionals(2)]=<type(libpaw_libxc_type)>, optional argument