File: util.py

package info (click to toggle)
libxc 5.2.3-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 196,988 kB
  • sloc: ansic: 31,506; f90: 3,369; perl: 1,392; python: 966; makefile: 425; sh: 318
file content (255 lines) | stat: -rw-r--r-- 6,348 bytes parent folder | download | duplicates (2)
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
"""
Binds the LibXC utility functions.
"""

import ctypes
import numpy as np

from .core import core
from . import flags

### Set required ctypes bindings

core.xc_version.argtypes = (ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int))
core.xc_version.restype = None

core.xc_version_string.restype = ctypes.c_char_p
core.xc_reference.restype = ctypes.c_char_p
core.xc_reference_doi.restype = ctypes.c_char_p

core.xc_functional_get_number.argtypes = (ctypes.c_char_p, )
core.xc_functional_get_number.restype = ctypes.c_int

core.xc_functional_get_name.argtypes = (ctypes.c_int, )
core.xc_functional_get_name.restype = ctypes.c_char_p

core.xc_family_from_id.argtypes = (ctypes.c_int, ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int))
core.xc_family_from_id.restype = ctypes.c_int

core.xc_available_functional_numbers.argtypes = (np.ctypeslib.ndpointer(dtype=np.intc, ndim=1, flags=("W", "C", "A")), )
core.xc_available_functional_numbers_by_name.argtypes = (np.ctypeslib.ndpointer(dtype=np.intc, ndim=1, flags=("W", "C", "A")), )

core.xc_available_functional_names.argtypes = (ctypes.POINTER(ctypes.c_char_p), )

### Build wrapper functions


def xc_version():
    """
    Returns the current LibXC version as semantic versioning tuple.

    Returns
    -------
    version : tuple
        The (major, minor, patch) version of the linked LibXC shared object.

    Examples
    --------
    >>> pylibxc.util.xc_version()
    (4, 0, 1)

    """
    major = ctypes.c_int()
    minor = ctypes.c_int()
    patch = ctypes.c_int()
    core.xc_version(major, minor, patch)
    return (major.value, minor.value, patch.value)


def xc_version_string():
    """
    Returns the current LibXC version as a string.

    Returns
    -------
    version : str
        The string representation of the current LibXC version.

    Examples
    --------
    >>> pylibxc.util.xc_version_string()
    "4.0.1"

    """
    return core.xc_version_string().decode("UTF-8")


def xc_reference():
    """
    Returns the reference for the current LibXC version as a string.

    Returns
    -------
    reference : str
        The string representation of the literature reference for LibXC.

    Examples
    --------
    >>> pylibxc.util.xc_reference()
    "S. Lehtola, C. Steigemann, M. J. Oliveira, and M. A. Marques, SoftwareX 7, 1 (2018)"

    """
    return core.xc_reference().decode("UTF-8")


def xc_reference_doi():
    """
    Returns the doi of the reference for the current LibXC version as a string.

    Returns
    -------
    doi : str
        The string representation of the doi of the literature reference for LibXC.

    Examples
    --------
    >>> pylibxc.util.xc_reference_doi()
    "10.1016/j.softx.2017.11.002"

    """
    return core.xc_reference_doi().decode("UTF-8")


def xc_functional_get_number(name):
    """
    Returns the functional ID from a given string.

    Parameters
    ----------
    name : str
        The functional name to find the ID of.

    Returns
    -------
    id : int
        The ID of the requested functional.

    Examples
    --------
    >>> pylibxc.util.xc_functional_get_number("XC_GGA_X_GAM")
    32
    """
    if not isinstance(name, str):
        raise TypeError("xc_functional_get_number: name must be a string. Got {}".format(name))
    return core.xc_functional_get_number(ctypes.c_char_p(name.encode()))


def xc_functional_get_name(func_id):
    """
    Returns the functional name from a ID.

    Parameters
    ----------
    func_id : int
        The LibXC functional ID

    Returns
    -------
    functional_name : str
        The functional_name of the requested functional.

    Examples
    --------
    >>> pylibxc.util.xc_functional_get_name(32)
    "gga_x_gam"
    """
    if not isinstance(func_id, (int, np.integer)):
        raise TypeError("xc_functional_get_name: func_id must be an int. Got {}".format(func_id))
    ret = core.xc_functional_get_name(func_id)
    if ret is not None:
        ret = ret.decode("UTF-8")
    return ret

def xc_family_from_id(func_id):
    """
    Returns the family class and family number (?).

    Parameters
    ----------
    func_id : int
        The LibXC functional ID

    Returns
    -------
    functional_family : int
        The family ID.
    functional_number : int
        The functional number within a family.

    Examples
    --------
    >>> pylibxc.util.xc_family_from_id(72)
    (4, 4)

    """
    if not isinstance(func_id, (int, np.integer)):
        raise TypeError("xc_family_from_id: func_id must be an int. Got {}".format(func_id))
    family = ctypes.c_int()
    number = ctypes.c_int()
    core.xc_family_from_id(func_id, ctypes.pointer(family), ctypes.pointer(number))

    return (family.value, number.value)


def xc_number_of_functionals():
    """
    Returns the totaly number of XC functionals available in LibXC.

    Returns
    -------
    number_of_functinals : int
        The total number of functionals available in LibXC.

    Examples
    --------
    >>> pylibxc.util.xc_number_of_functionals()
    447
    """

    return core.xc_number_of_functionals()


def xc_available_functional_numbers():
    """
    Returns a list of all available XC functional IDs


    Returns
    -------
    functional_ids : list of ints
        All available LibXC functional IDs.

    Examples
    --------
    >>> xc_func_list = pylibxc.util.xc_available_functional_numbers()
    np.array([1, 2, ..., 568, 569])
    """

    nfunc = xc_number_of_functionals()

    ret = np.zeros(nfunc, dtype=np.intc)
    core.xc_available_functional_numbers(ret)
    return ret


def xc_available_functional_names():
    """
    Returns a list of all available XC functional names

    Returns
    -------
    functional_names : list of strs
        All available LibXC functional names.

    Examples
    --------
    >>> xc_func_list = pylibxc.util.xc_available_functional_names()
    ['lda_x', 'lda_c_wigner', ..., 'hyb_mgga_x_revscan0', 'hyb_mgga_xc_b98']
    """

    # I give up trying to get char** working, someone else can pick it up.
    nfunc = xc_number_of_functionals()
    func_ids = np.zeros(nfunc, dtype=np.intc)
    core.xc_available_functional_numbers_by_name(func_ids)
    available_names = [xc_functional_get_name(x) for x in func_ids]
    return available_names