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
|
"""
Tests the LibXCFunctional class.
"""
import pytest
import numpy as np
import ctypes
import pylibxc
compute_test_dim = 5
np.random.seed(0)
def _dict_array_comp(test, ref, keys):
for key in keys:
tmp = np.testing.assert_allclose(test[key], ref[key])
# print(key, np.allclose(test[key], ref[key]), np.linalg.norm(test[key] - ref[key]))
return True
_size_tuples = {"unpolarized": (1, 1, 1, 1), "polarized": (2, 3, 2, 2)}
def test_libxc_functional_build():
pylibxc.LibXCFunctional(1, 1)
pylibxc.LibXCFunctional(1, 2)
pylibxc.LibXCFunctional("XC_LDA_C_VWN", "polarized")
pylibxc.LibXCFunctional("lda_c_vwn", "unpolarized")
# Check functional edge cases
with pytest.raises(KeyError):
pylibxc.LibXCFunctional("something", 1)
with pytest.raises(KeyError):
pylibxc.LibXCFunctional(5000, 1)
# Check spin edge cases
with pytest.raises(KeyError):
pylibxc.LibXCFunctional("lda_c_vwn", 10)
with pytest.raises(KeyError):
pylibxc.LibXCFunctional("lda_c_vwn", "something")
def test_libxc_functional_info():
func = pylibxc.LibXCFunctional(1, 1)
assert func.get_number() == 1
assert func.get_kind() == 0
assert func.get_name() == "Slater exchange"
assert func.get_family() == 1
# XC_FLAGS_3D + [HAVE_EXC | HAVE_VXC | HAVE_FXC | HAVE_KXC]
assert func.get_flags() in [129, 131, 135, 143, 159]
assert len(func.get_bibtex()) == 2
assert len(func.get_references()) == 2
assert len(func.get_doi()) == 2
func = pylibxc.LibXCFunctional("XC_HYB_MGGA_XC_WB97M_V", 1)
assert func.get_number() == 531
assert func.get_kind() == 2
assert func.get_name() == "wB97M-V exchange-correlation functional"
assert func.get_family() == 64
# XC_FLAGS_3D + XC_FLAGS_HYB_CAM + XC_FLAGS_VV10 + [HAVE_EXC | HAVE_VXC | HAVE_FXC | HAVE_KXC]
assert func.get_flags() in [1409, 1411, 1415, 1423, 1439]
assert len(func.get_bibtex()) == 1
assert len(func.get_references()) == 1
assert len(func.get_doi()) == 1
def test_ext_params():
func = pylibxc.LibXCFunctional(1, 1)
assert 0 == len(func.get_ext_param_descriptions())
assert 0 == len(func.get_ext_param_default_values())
func.set_dens_threshold(1.e-16)
func.set_dens_threshold(5)
with pytest.raises(ValueError):
func.set_ext_params([])
func = pylibxc.LibXCFunctional("XC_HYB_GGA_XC_HSE06", 1)
assert 3 == len(func.get_ext_param_descriptions())
assert 3 == len(func.get_ext_param_default_values())
assert all("param" in x for x in func.get_ext_param_descriptions())
func.set_dens_threshold(1.e-16)
func.set_dens_threshold(5)
# Segfaults, need to check it out
func.set_ext_params([5, 3, 3])
with pytest.raises(ValueError):
func.set_ext_params([5, 3])
with pytest.raises(ValueError):
func.set_dens_threshold(-1)
@pytest.mark.parametrize("polar", [("unpolarized"), ("polarized")])
def test_lda_compute(polar):
# Build input
ndim = _size_tuples[polar]
inp = {}
inp["rho"] = np.random.random((compute_test_dim * ndim[0]))
func = pylibxc.LibXCFunctional("lda_c_vwn", polar)
# Check capabilities
do_l = func._have_lxc
do_k = func._have_kxc
do_f = func._have_fxc
do_v = func._have_vxc
do_e = func._have_exc
# Compute
ret_full = func.compute(inp, do_exc=do_e, do_vxc=do_v, do_fxc=do_f, do_kxc=do_k, do_lxc=do_l)
ret_ev = func.compute(inp, do_exc=do_e, do_vxc=do_v, do_fxc=False, do_kxc=False, do_lxc=False)
ret_e = func.compute(inp, do_exc=do_e, do_vxc=False, do_fxc=False, do_kxc=False, do_lxc=False)
ret_v = func.compute(inp, do_exc=False, do_vxc=do_v, do_fxc=False, do_kxc=False, do_lxc=False)
ret_f = func.compute(inp, do_exc=False, do_vxc=False, do_fxc=do_f, do_kxc=False, do_lxc=False)
ret_k = func.compute(inp, do_exc=False, do_vxc=False, do_fxc=False, do_kxc=do_k, do_lxc=False)
ret_l = func.compute(inp, do_exc=False, do_vxc=False, do_fxc=False, do_kxc=False, do_lxc=do_l)
# Test consistency
if do_e:
assert ret_full["zk"].size == compute_test_dim
if do_v:
assert ret_full["vrho"].size == compute_test_dim * ndim[0]
if do_e:
assert np.allclose(ret_full["zk"], ret_ev["zk"])
if do_v:
assert np.allclose(ret_full["vrho"], ret_ev["vrho"])
if do_e:
assert np.allclose(ret_full["zk"], ret_e["zk"])
if do_v:
assert np.allclose(ret_full["vrho"], ret_v["vrho"])
if do_f:
assert np.allclose(ret_full["v2rho2"], ret_f["v2rho2"])
if do_k:
assert np.allclose(ret_full["v3rho3"], ret_k["v3rho3"])
if do_l:
assert np.allclose(ret_full["v4rho4"], ret_l["v4rho4"])
@pytest.mark.parametrize("polar", [("unpolarized"), ("polarized")])
def test_gga_compute(polar):
# Build input
ndim = _size_tuples[polar]
inp = {}
inp["rho"] = np.random.random((compute_test_dim * ndim[0]))
inp["sigma"] = np.random.random((compute_test_dim * ndim[1]))
# Compute
func = pylibxc.LibXCFunctional("gga_c_pbe", polar)
# Check capabilities
do_l = func._have_lxc
do_k = func._have_kxc
do_f = func._have_fxc
do_v = func._have_vxc
do_e = func._have_exc
# Compute
ret_full = func.compute(inp, do_exc=do_e, do_vxc=do_v, do_fxc=do_f, do_kxc=do_k, do_lxc=do_l)
ret_ev = func.compute(inp, do_exc=do_e, do_vxc=do_v, do_fxc=False, do_kxc=False, do_lxc=False)
ret_e = func.compute(inp, do_exc=do_e, do_vxc=False, do_fxc=False, do_kxc=False, do_lxc=False)
ret_v = func.compute(inp, do_exc=False, do_vxc=do_v, do_fxc=False, do_kxc=False, do_lxc=False)
ret_f = func.compute(inp, do_exc=False, do_vxc=False, do_fxc=do_f, do_kxc=False, do_lxc=False)
ret_k = func.compute(inp, do_exc=False, do_vxc=False, do_fxc=False, do_kxc=do_k, do_lxc=False)
ret_l = func.compute(inp, do_exc=False, do_vxc=False, do_fxc=False, do_kxc=False, do_lxc=do_l)
# Test consistency
if do_e:
assert ret_full["zk"].size == compute_test_dim
if do_v:
assert ret_full["vrho"].size == compute_test_dim * ndim[0]
assert ret_full["vsigma"].size == compute_test_dim * ndim[1]
if do_e and do_v:
assert _dict_array_comp(ret_full, ret_ev, ["zk", "vrho", "vsigma"])
if do_e:
assert _dict_array_comp(ret_full, ret_e, ["zk"])
if do_v:
assert _dict_array_comp(ret_full, ret_v, ["vrho", "vsigma"])
if do_f:
assert _dict_array_comp(ret_full, ret_f, ["v2rho2", "v2rhosigma", "v2sigma2"])
if do_k:
assert _dict_array_comp(ret_full, ret_k, ["v3rho3", "v3rho2sigma", "v3rhosigma2", "v3sigma3"])
if do_l:
assert _dict_array_comp(ret_full, ret_l, ["v4rho4", "v4rho3sigma", "v4rho2sigma2", "v4rhosigma3", "v4sigma4"])
@pytest.mark.parametrize("polar", [("unpolarized"), ("polarized")])
def test_mgga_compute(polar):
# Build input
ndim = _size_tuples[polar]
inp = {}
inp["rho"] = np.random.random((compute_test_dim * ndim[0]))
inp["sigma"] = np.random.random((compute_test_dim * ndim[1]))
inp["tau"] = np.random.random((compute_test_dim * ndim[3]))
# Compute
func = pylibxc.LibXCFunctional("mgga_c_tpss", polar)
# Test consistency
ret_ev = func.compute(inp, do_exc=True, do_vxc=True)
ret_e = func.compute(inp, do_exc=True, do_vxc=False)
ret_v = func.compute(inp, do_exc=False, do_vxc=True)
assert ret_ev["zk"].size == compute_test_dim
assert ret_ev["vrho"].size == compute_test_dim * ndim[0]
assert ret_ev["vsigma"].size == compute_test_dim * ndim[1]
assert _dict_array_comp(ret_ev, ret_e, ["zk"])
assert _dict_array_comp(ret_ev, ret_v, ["vrho", "vsigma", "vtau"])
@pytest.mark.parametrize("polar", [("unpolarized"), ("polarized")])
def test_mgga_lapl_compute(polar):
# Build input
ndim = _size_tuples[polar]
inp = {}
inp["rho"] = np.random.random((compute_test_dim * ndim[0]))
inp["sigma"] = np.random.random((compute_test_dim * ndim[1]))
inp["tau"] = np.random.random((compute_test_dim * ndim[3]))
inp["lapl"] = np.random.random((compute_test_dim * ndim[3]))
# Compute
func = pylibxc.LibXCFunctional("mgga_x_br89", polar)
# Test consistency
ret_ev = func.compute(inp, do_exc=True, do_vxc=True)
ret_e = func.compute(inp, do_exc=True, do_vxc=False)
ret_v = func.compute(inp, do_exc=False, do_vxc=True)
assert ret_ev["zk"].size == compute_test_dim
assert ret_ev["vrho"].size == compute_test_dim * ndim[0]
assert ret_ev["vsigma"].size == compute_test_dim * ndim[1]
assert _dict_array_comp(ret_ev, ret_e, ["zk"])
assert _dict_array_comp(ret_ev, ret_v, ["vrho", "vsigma", "vtau"])
# Test exception
del inp["lapl"]
with pytest.raises(KeyError):
func.compute(inp, do_exc=True, do_vxc=True)
@pytest.mark.parametrize("polar", [("unpolarized"), ("polarized")])
def test_deriv_flags(polar):
func = pylibxc.LibXCFunctional("mgga_c_tpss", polar)
ndim = _size_tuples[polar]
inp = {}
inp["rho"] = np.random.random((compute_test_dim * ndim[0]))
inp["sigma"] = np.random.random((compute_test_dim * ndim[1]))
inp["tau"] = np.random.random((compute_test_dim * ndim[3]))
inp["lapl"] = np.random.random((compute_test_dim * ndim[3]))
# disabled as mgga_c_tpss now has fxc
# please fix this better!
#with pytest.raises(ValueError):
# func.compute(inp, do_fxc=True)
def test_hyb_getters():
func = pylibxc.LibXCFunctional("hyb_gga_xc_b3lyp", "unpolarized")
assert pytest.approx(0.2) == func.get_hyb_exx_coef()
with pytest.raises(ValueError):
func.get_cam_coef()
with pytest.raises(ValueError):
func.get_vv10_coef()
def test_cam_getters():
func = pylibxc.LibXCFunctional("hyb_gga_xc_cam_b3lyp", "unpolarized")
with pytest.raises(ValueError):
assert pytest.approx(0.65) == func.get_hyb_exx_coef()
omega, alpha, beta = func.get_cam_coef()
assert pytest.approx(0.33) == omega
assert pytest.approx(0.65) == alpha
assert pytest.approx(-0.46) == beta
with pytest.raises(ValueError):
func.get_vv10_coef()
def test_vv10_getters():
func = pylibxc.LibXCFunctional("gga_xc_vv10", "unpolarized")
b, C = func.get_vv10_coef()
assert pytest.approx(5.9) == b
assert pytest.approx(0.0093) == C
with pytest.raises(ValueError):
func.get_cam_coef()
with pytest.raises(ValueError):
func.get_hyb_exx_coef()
|