(git:e753828)
Loading...
Searching...
No Matches
xc_libxc_wrap.F
Go to the documentation of this file.
1!--------------------------------------------------------------------------------------------------!
2! CP2K: A general program to perform molecular dynamics simulations !
3! Copyright 2000-2026 CP2K developers group <https://cp2k.org> !
4! !
5! SPDX-License-Identifier: GPL-2.0-or-later !
6!--------------------------------------------------------------------------------------------------!
7
8! **************************************************************************************************
9!> \brief Includes all necessary routines, functions and parameters from
10!> libxc. Provides CP2K routines/functions where the LibXC calling list
11!> is version dependent (>=4.0.3). The naming convention for such
12!> routines/functions is xc_f03_XXX --> 'xc_libxc_wrap_XXX'. All version
13!> independent routines/functions are just bypassed to higher level
14!> module file 'xc_libxc'.
15!>
16!> \par History
17!> 08.2015 created [A. Gloess (agloess)]
18!> 01.2018 refactoring [A. Gloess (agloess)]
19!> 10.2018/04.2019 added hyb_mgga [S. Simko, included by F. Stein]
20!> \author A. Gloess (agloess)
21! **************************************************************************************************
23#if defined (__LIBXC)
24#include <xc_version.h>
25! check for LibXC version
26#if (XC_MAJOR_VERSION < 5 || (XC_MAJOR_VERSION == 5 && XC_MINOR_VERSION < 1))
27 this version of cp2k only works with libxc versions 5.1.0 and above.
28 furthermore, -i${libxc_dir}/include needs to be added to fcflags.
29#else
30 USE kinds, ONLY: dp
31 USE xc_f03_lib_m, ONLY: xc_f03_func_end, &
32 xc_f03_func_init, &
33 xc_f03_functional_get_name, &
34 xc_f03_func_set_ext_params, &
35 xc_f03_func_set_dens_threshold, &
36 xc_f03_func_set_tau_threshold, &
37 xc_f03_version_string, &
38 xc_f03_reference, &
39 xc_f03_reference_doi, &
40 xc_f03_functional_get_number, &
41 xc_f03_available_functional_numbers, &
42 xc_f03_available_functional_names, &
43 xc_f03_maximum_name_length, &
44 xc_f03_number_of_functionals, &
45 !
46 xc_f03_gga_exc, &
47 xc_f03_gga_exc_vxc, &
48 xc_f03_gga_exc_vxc_fxc, &
49 xc_f03_gga_exc_vxc_fxc_kxc, &
50 xc_f03_mgga_exc_vxc_fxc_kxc, &
51 xc_f03_mgga_vxc_fxc_kxc, &
52 xc_f03_gga_fxc, &
53 xc_f03_gga_vxc, &
54 xc_f03_gga_vxc_fxc, &
55 xc_f03_gga_vxc_fxc_kxc, &
56 !
57 xc_f03_func_get_info, &
58 xc_f03_func_info_get_family, &
59 xc_f03_func_info_get_kind, &
60 xc_f03_func_info_get_name, &
61 xc_f03_func_info_get_references, &
62 xc_f03_func_info_get_flags, &
63 xc_f03_func_info_get_n_ext_params, &
64 xc_f03_func_info_get_ext_params_name, &
65 xc_f03_func_info_get_ext_params_default_value, &
66 xc_f03_func_info_get_ext_params_description, &
67 !
68 xc_f03_func_reference_get_ref, &
69 xc_f03_func_reference_get_doi, &
70 !
71 xc_f03_lda => xc_f03_lda_exc_vxc_fxc_kxc, &
72 xc_f03_lda_exc, &
73 xc_f03_lda_exc_vxc, &
74 xc_f03_lda_exc_vxc_fxc, &
75 xc_f03_lda_fxc, &
76 xc_f03_lda_kxc, &
77 xc_f03_lda_vxc, &
78 !
79 xc_f03_mgga => xc_f03_mgga_exc_vxc_fxc, &
80 xc_f03_mgga_exc, &
81 xc_f03_mgga_exc_vxc, &
82 xc_f03_mgga_fxc, &
83 xc_f03_mgga_vxc, &
84 xc_f03_mgga_vxc_fxc, &
85 !
86 xc_f03_func_t, &
87 xc_f03_func_info_t, &
88 xc_f03_func_reference_t, &
89 !
90 xc_family_lda, &
91 xc_family_gga, &
92 xc_family_mgga, &
93 xc_family_hyb_lda, &
94 xc_family_hyb_gga, &
95 xc_family_hyb_mgga, &
96 !
97 xc_unpolarized, &
98 xc_polarized, &
99 !
100 xc_exchange, &
101 xc_correlation, &
102 xc_exchange_correlation, &
103 xc_kinetic, &
104 !
105 xc_flags_needs_laplacian, &
106 xc_flags_have_exc, &
107 xc_flags_development
108
109#include "../base/base_uses.f90"
110#endif
111 IMPLICIT NONE
112 PRIVATE
113#if defined (__LIBXC)
114
115 CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'xc_libxc_wrap'
116
117 CHARACTER(LEN=*), PARAMETER, PUBLIC :: libxc_version = xc_version
118
119 PUBLIC :: xc_f03_func_t, xc_f03_func_info_t
120 PUBLIC :: xc_f03_func_init, xc_f03_func_end, xc_f03_func_set_ext_params
121 PUBLIC :: xc_f03_functional_get_name, xc_f03_available_functional_numbers, xc_f03_maximum_name_length, &
122 xc_f03_number_of_functionals, xc_f03_available_functional_names
123 PUBLIC :: xc_f03_func_get_info, xc_f03_func_info_get_family, xc_f03_func_info_get_kind, &
124 xc_f03_func_info_get_name, xc_f03_func_info_get_ext_params_name, &
125 xc_f03_func_info_get_ext_params_description, xc_f03_func_info_get_ext_params_default_value, &
126 xc_f03_func_info_get_n_ext_params
127 PUBLIC :: xc_f03_gga_exc, xc_f03_gga_exc_vxc, xc_f03_gga_exc_vxc_fxc, xc_f03_gga_fxc, &
128 xc_f03_gga_vxc, xc_f03_gga_vxc_fxc, &
129 xc_f03_gga_exc_vxc_fxc_kxc, xc_f03_gga_vxc_fxc_kxc
130 PUBLIC :: xc_f03_mgga_exc_vxc_fxc_kxc, xc_f03_mgga_vxc_fxc_kxc
131 PUBLIC :: xc_f03_lda, &
132 xc_f03_lda_exc, xc_f03_lda_exc_vxc, xc_f03_lda_exc_vxc_fxc, &
133 xc_f03_lda_fxc, xc_f03_lda_kxc, xc_f03_lda_vxc
134 PUBLIC :: xc_f03_mgga, xc_f03_mgga_exc, xc_f03_mgga_exc_vxc, xc_f03_mgga_fxc, &
135 xc_f03_mgga_vxc, xc_f03_mgga_vxc_fxc
136
137 PUBLIC :: xc_family_lda, xc_family_gga, xc_family_mgga, &
138 xc_family_hyb_lda, xc_family_hyb_gga, xc_family_hyb_mgga
139
140 PUBLIC :: xc_unpolarized, xc_polarized
141
142 PUBLIC :: xc_exchange, xc_correlation, xc_exchange_correlation, xc_kinetic
143
144! wrappers for routines
145 PUBLIC :: xc_libxc_wrap_info_refs, &
146 xc_libxc_wrap_version, &
147 xc_libxc_wrap_library_reference, &
148 xc_libxc_wrap_info_needs_laplace, &
149 xc_libxc_wrap_info_no_exc, &
150 xc_libxc_wrap_set_thresholds, &
151 xc_libxc_wrap_functional_get_number, &
152 xc_libxc_wrap_is_under_development, &
153 xc_libxc_get_reference_length, &
154 xc_libxc_check_functional
155
156CONTAINS
157
158! **************************************************************************************************
159!> \brief Provides the reference(s) for this functional.
160!> \param xc_info func_info object of the functional
161!> \return upper bound for the length of the reference string
162!> \author F. Stein
163! **************************************************************************************************
164 FUNCTION xc_libxc_get_reference_length(xc_info) RESULT(length)
165
166 TYPE(xc_f03_func_info_t), INTENT(IN) :: xc_info
167 INTEGER :: length
168
169 CHARACTER(LEN=*), PARAMETER :: routineN = 'xc_libxc_get_reference_length'
170 INTEGER, PARAMETER :: maxlen = 67
171
172 CHARACTER(LEN=128) :: descr_string
173 CHARACTER(LEN=1024) :: doi_string, ref_string
174 INTEGER :: i, i_ref, i_ref_old, n_params, handle
175 TYPE(xc_f03_func_reference_t) :: xc_ref
176
177 CALL timeset(routinen, handle)
178
179 ! We are counting the number of necessary lines by carrying out a dry run of xc_libxc_wrap_info_refs
180 i_ref = 0
181 i_ref_old = -1
182 length = 0
183 DO WHILE (i_ref >= 0 .AND. i_ref /= i_ref_old)
184 ! information about functional references
185 xc_ref = xc_f03_func_info_get_references(xc_info, i_ref)
186 ref_string = xc_f03_func_reference_get_ref(xc_ref)
187 doi_string = xc_f03_func_reference_get_doi(xc_ref)
188 length = length + len_trim(ref_string) + len_trim(doi_string) + 11
189 IF (mod(length, maxlen) /= 0) length = length + maxlen - mod(length, maxlen)
190 ! information about (optional) external parameters
191 n_params = xc_f03_func_info_get_n_ext_params(xc_info)
192 IF (n_params > 0) THEN
193 length = length + maxlen
194 END IF
195 DO i = 1, n_params
196 descr_string = xc_f03_func_info_get_ext_params_description(xc_info, i - 1)
197 length = length + len_trim(descr_string) + 3
198 IF (mod(length, maxlen) /= 0) length = length + maxlen - mod(length, maxlen)
199 END DO
200 i_ref_old = i_ref
201 END DO
202 ! two additional lines for spin polarization, scaling factor and buffer
203 length = length + 2*maxlen
204
205 CALL timestop(handle)
206
207 END FUNCTION xc_libxc_get_reference_length
208
209! **************************************************************************************************
210!> \brief Provides the reference(s) for this functional.
211!> \param xc_info ...
212!> \param polarized ...
213!> \param sc ...
214!> \param reference ...
215!>
216!> \author A. Gloess (agloess)
217! **************************************************************************************************
218 SUBROUTINE xc_libxc_wrap_info_refs(xc_info, polarized, sc, reference)
219 TYPE(xc_f03_func_info_t), INTENT(IN) :: xc_info
220 INTEGER, INTENT(IN) :: polarized
221 REAL(KIND=dp), INTENT(IN) :: sc
222 CHARACTER(LEN=*), INTENT(OUT) :: reference
223
224 CHARACTER(LEN=*), PARAMETER :: routineN = 'xc_libxc_wrap_info_refs'
225 INTEGER, PARAMETER :: maxlen = 67
226
227 CHARACTER(LEN=128) :: descr_string
228 CHARACTER(LEN=1028) :: doi_string, ref_string
229 ! conservative estimate of the necessary length: 2*1028+11=2067
230 CHARACTER(LEN=2067) :: tmp_string
231 INTEGER :: empty, first, handle, i, i_ref, i_ref_old, idx, &
232 last, n_params
233 TYPE(xc_f03_func_reference_t) :: xc_ref
234
235 CALL timeset(routinen, handle)
236
237 i_ref = 0
238 i_ref_old = -1
239 idx = 1
240 first = 1
241 DO WHILE (i_ref >= 0 .AND. i_ref /= i_ref_old)
242 ! information about functional references
243 xc_ref = xc_f03_func_info_get_references(xc_info, i_ref)
244 ref_string = xc_f03_func_reference_get_ref(xc_ref)
245 doi_string = xc_f03_func_reference_get_doi(xc_ref)
246 WRITE (tmp_string, '(a1,i1,a2,a,a7,a)') '[', idx, '] ', &
247 trim(ref_string), ', doi: ', trim(doi_string)
248 last = first + len_trim(tmp_string) - 1
249 reference(first:last) = trim(tmp_string)
250 first = last + 1
251 empty = last + (maxlen - 1) - mod(last - 1, maxlen)
252 ! fill up line with 'spaces'
253 IF (empty /= last) THEN
254 reference(first:empty) = ' '
255 first = empty + 1
256 END IF
257 ! information about (optional) external parameters
258 n_params = xc_f03_func_info_get_n_ext_params(xc_info)
259 IF (n_params > 0) THEN
260 reference(first:first + maxlen - 1) = 'Optional external parameters:'//repeat(' ', maxlen - 28)
261 first = first + maxlen
262 END IF
263 DO i = 1, n_params
264 descr_string = xc_f03_func_info_get_ext_params_description(xc_info, i - 1)
265 last = first + len_trim(descr_string) - 1 + 3
266 reference(first:last) = ' * '//trim(descr_string)
267 first = last + 1
268 empty = last + (maxlen - 1) - mod(last - 1, maxlen)
269 ! fill up line with 'spaces'
270
271 IF (empty /= last) THEN
272 reference(first:empty) = ' '
273 first = empty + 1
274 END IF
275 END DO
276 idx = idx + 1
277 i_ref_old = i_ref
278 END DO
279 SELECT CASE (polarized)
280 CASE (xc_unpolarized)
281 WRITE (tmp_string, "('{scale=',f5.3,', spin-unpolarized}')") sc
282 CASE (xc_polarized)
283 WRITE (tmp_string, "('{scale=',f5.3,', spin-polarized}')") sc
284 CASE default
285 cpabort("Unsupported value for variable 'polarized'.")
286 END SELECT
287 last = first + len_trim(tmp_string) - 1
288 reference(first:last) = trim(tmp_string)
289 first = last + 1
290 ! fill with 'spaces'
291 reference(first:len(reference)) = ' '
292
293 IF (last > len(reference)) &
294 cpabort("Faulty reference length.")
295
296 CALL timestop(handle)
297
298 END SUBROUTINE xc_libxc_wrap_info_refs
299
300! **************************************************************************************************
301!> \brief Provides the version of the LibXC library CP2K is running against.
302!> \param version version string reported by the library at run time
303!> \param compiled_version version of the headers CP2K was compiled against
304!> \author A. Gloess (agloess)
305!> \note The run-time and compile-time versions differ whenever CP2K is executed
306!> against a shared LibXC other than the one it was built with. Reporting
307!> the run-time version is what actually describes the numbers produced.
308!>
309! **************************************************************************************************
310 SUBROUTINE xc_libxc_wrap_version(version, compiled_version)
311 CHARACTER(LEN=*), INTENT(OUT) :: version
312 CHARACTER(LEN=*), INTENT(OUT), OPTIONAL :: compiled_version
313
314 CHARACTER(LEN=*), PARAMETER :: routineN = 'xc_libxc_wrap_version'
315
316 INTEGER :: handle
317
318 CALL timeset(routinen, handle)
319
320 CALL xc_f03_version_string(version)
321 IF (PRESENT(compiled_version)) compiled_version = trim(libxc_version)
322
323 CALL timestop(handle)
324
325 END SUBROUTINE xc_libxc_wrap_version
326
327! **************************************************************************************************
328!> \brief Provides the citation LibXC asks to be given for the library itself.
329!> \param reference bibliographic reference
330!> \param doi digital object identifier of that reference
331!> \author S. Lehtola
332!> \note Taken from the library at run time rather than hard-coded, so that the
333!> citation stays correct when LibXC updates its own self-citation.
334!>
335! **************************************************************************************************
336 SUBROUTINE xc_libxc_wrap_library_reference(reference, doi)
337 CHARACTER(LEN=*), INTENT(OUT) :: reference, doi
338
339 CHARACTER(LEN=*), PARAMETER :: routineN = 'xc_libxc_wrap_library_reference'
340
341 INTEGER :: handle
342
343 CALL timeset(routinen, handle)
344
345 CALL xc_f03_reference(reference)
346 CALL xc_f03_reference_doi(doi)
347
348 CALL timestop(handle)
349
350 END SUBROUTINE xc_libxc_wrap_library_reference
351
352! **************************************************************************************************
353!> \brief Applies the CP2K screening cutoffs to a LibXC functional object.
354!> \param xc_func LibXC functional object
355!> \param xc_info info object belonging to xc_func
356!> \param epsilon_rho density cutoff (XC%DENSITY_CUTOFF)
357!> \param epsilon_tau kinetic energy density cutoff (XC%TAU_CUTOFF)
358!> \author S. Lehtola
359!> \note LibXC screens every point against the density threshold internally, which
360!> is what allows CP2K to hand it whole blocks of points instead of filtering
361!> point by point. The sigma threshold is deliberately left at LibXC's own
362!> default: CP2K has no cutoff of its own on the gradient here, and raising
363!> it in step with the density cutoff would floor sigma for points with a
364!> small gradient that used to be evaluated as given.
365!>
366! **************************************************************************************************
367 SUBROUTINE xc_libxc_wrap_set_thresholds(xc_func, xc_info, epsilon_rho, epsilon_tau)
368 TYPE(xc_f03_func_t), INTENT(INOUT) :: xc_func
369 TYPE(xc_f03_func_info_t), INTENT(IN) :: xc_info
370 REAL(KIND=dp), INTENT(IN) :: epsilon_rho, epsilon_tau
371
372 CHARACTER(LEN=*), PARAMETER :: routineN = 'xc_libxc_wrap_set_thresholds'
373
374 INTEGER :: handle
375
376 CALL timeset(routinen, handle)
377
378 CALL xc_f03_func_set_dens_threshold(xc_func, epsilon_rho)
379
380 SELECT CASE (xc_f03_func_info_get_family(xc_info))
381 CASE (xc_family_mgga, xc_family_hyb_mgga)
382 CALL xc_f03_func_set_tau_threshold(xc_func, epsilon_tau)
383 END SELECT
384
385 CALL timestop(handle)
386
387 END SUBROUTINE xc_libxc_wrap_set_thresholds
388
389! **************************************************************************************************
390!> \brief Checks existence of functional in LibXC
391!> \param func_string ...
392!> \return ...
393!> \author F. Stein
394!> \note Remove prefix to keep compatibility, functionals can be specified (in
395!> LIBXC section) as:
396!> GGA_X_... or XC_GGA_X_...
397!> Starting from version 2.2.0 both name conventions are allowed, before
398!> the 'XC_' prefix was necessary.
399!>
400! **************************************************************************************************
401 LOGICAL FUNCTION xc_libxc_check_functional(func_string) RESULT(exists)
402 CHARACTER(LEN=*), INTENT(IN) :: func_string
403
404 CHARACTER(LEN=*), PARAMETER :: routineN = 'xc_libxc_check_functional'
405
406 INTEGER :: func_id, handle
407
408 CALL timeset(routinen, handle)
409
410 IF (func_string(1:3) == "XC_") THEN
411 func_id = xc_f03_functional_get_number(func_string(4:len_trim(func_string)))
412 ELSE
413 func_id = xc_f03_functional_get_number(func_string(1:len_trim(func_string)))
414 END IF
415
416 exists = .true.
417 IF (func_id == -1) exists = .false.
418
419 CALL timestop(handle)
420
421 END FUNCTION xc_libxc_check_functional
422
423! **************************************************************************************************
424!> \brief Provides the functional ID.
425!> \param func_string ...
426!> \return ...
427!> \author A. Gloess (agloess)
428!> \note Remove prefix to keep compatibility, functionals can be specified (in
429!> LIBXC section) as:
430!> GGA_X_... or XC_GGA_X_...
431!> Starting from version 2.2.0 both name conventions are allowed, before
432!> the 'XC_' prefix was necessary.
433!>
434! **************************************************************************************************
435 INTEGER FUNCTION xc_libxc_wrap_functional_get_number(func_string) RESULT(func_id)
436 CHARACTER(LEN=*), INTENT(IN) :: func_string
437
438 CHARACTER(LEN=*), PARAMETER :: routineN = 'xc_libxc_wrap_functional_get_number'
439
440 INTEGER :: handle
441
442 CALL timeset(routinen, handle)
443
444 IF (func_string(1:3) == "XC_") THEN
445 func_id = xc_f03_functional_get_number(func_string(4:len_trim(func_string)))
446 ELSE
447 func_id = xc_f03_functional_get_number(func_string(1:len_trim(func_string)))
448 END IF
449 IF (func_id == -1) THEN
450 cpabort(trim(func_string)//": wrong functional name")
451 END IF
452
453 CALL timestop(handle)
454
455 END FUNCTION xc_libxc_wrap_functional_get_number
456
457! **************************************************************************************************
458!> \brief Wrapper to test wether functional is considered under development in Libxc
459!> \param xc_info ...
460!>
461!> \return ...
462!> \author F. Stein (fstein93)
463! **************************************************************************************************
464 LOGICAL FUNCTION xc_libxc_wrap_is_under_development(xc_info)
465 TYPE(xc_f03_func_info_t) :: xc_info
466
467 IF (iand(xc_f03_func_info_get_flags(xc_info), xc_flags_development) == xc_flags_development) THEN
468 xc_libxc_wrap_is_under_development = .true.
469 ELSE
470 xc_libxc_wrap_is_under_development = .false.
471 END IF
472
473 END FUNCTION xc_libxc_wrap_is_under_development
474
475! **************************************************************************************************
476!> \brief Wrapper for functionals that need the Laplacian, all others can use
477!> a dummy array.
478!> \param xc_info info object of an already initialized functional
479!>
480!> \return ...
481!> \author A. Gloess (agloess)
482!> \note Reads the flag off an info object the caller already holds. Creating a
483!> throw-away functional object just to look at one flag used to cost an
484!> extra xc_f03_func_init/xc_f03_func_end pair per evaluated batch.
485! **************************************************************************************************
486 LOGICAL FUNCTION xc_libxc_wrap_info_needs_laplace(xc_info)
487 ! Only some MGGA functionals needs the laplacian
488 TYPE(xc_f03_func_info_t), INTENT(IN) :: xc_info
489
490 IF (iand(xc_f03_func_info_get_flags(xc_info), xc_flags_needs_laplacian) == xc_flags_needs_laplacian) THEN
491 xc_libxc_wrap_info_needs_laplace = .true.
492 ELSE
493 xc_libxc_wrap_info_needs_laplace = .false.
494 END IF
495
496 END FUNCTION xc_libxc_wrap_info_needs_laplace
497
498! **************************************************************************************************
499!> \brief Whether the functional does not provide the energy density.
500!> \param xc_info info object of an already initialized functional
501!>
502!> \return .TRUE. when LibXC cannot supply Exc for this functional
503!> \author S. Lehtola
504! **************************************************************************************************
505 LOGICAL FUNCTION xc_libxc_wrap_info_no_exc(xc_info)
506 TYPE(xc_f03_func_info_t), INTENT(IN) :: xc_info
507
508 IF (iand(xc_f03_func_info_get_flags(xc_info), xc_flags_have_exc) == xc_flags_have_exc) THEN
509 xc_libxc_wrap_info_no_exc = .false.
510 ELSE
511 xc_libxc_wrap_info_no_exc = .true.
512 END IF
513
514 END FUNCTION xc_libxc_wrap_info_no_exc
515
516#endif
517#endif
518END MODULE xc_libxc_wrap
program cp2k
Main program of CP2K.
Definition cp2k.F:42
Defines the basic variable types.
Definition kinds.F:23
integer, parameter, public dp
Definition kinds.F:34
Includes all necessary routines, functions and parameters from libxc. Provides CP2K routines/function...