|
| 1 | +#include "../libxc_abacus.h" |
| 2 | + |
| 3 | +#include "gtest/gtest.h" |
| 4 | +#include <algorithm> |
| 5 | +#include <array> |
| 6 | +#include <cmath> |
| 7 | +#include <vector> |
| 8 | + |
| 9 | +#ifdef __LIBXC |
| 10 | + |
| 11 | +TEST(LibxcSanitizer, FullPolarizationUsesTheWeightedEnergyDerivative) |
| 12 | +{ |
| 13 | + const std::array<int, 2> functional_ids = {{XC_LDA_X, XC_LDA_C_PZ}}; |
| 14 | + const double density = 0.45; |
| 15 | + |
| 16 | + for (std::size_t ifunc = 0; ifunc < functional_ids.size(); ++ifunc) |
| 17 | + { |
| 18 | + xc_func_type func; |
| 19 | + ASSERT_EQ(xc_func_init(&func, functional_ids[ifunc], XC_POLARIZED), 0); |
| 20 | + xc_func_set_dens_threshold(&func, 1.0e-6); |
| 21 | + |
| 22 | + const auto evaluate = [&func](const double rho_up, |
| 23 | + const double rho_down, |
| 24 | + XC_Functional_Libxc::LibxcWeightedDerivatives* const weighted) { |
| 25 | + const std::vector<double> rho = {rho_up, rho_down}; |
| 26 | + const std::vector<double> mask = {1.0, 1.0}; |
| 27 | + std::vector<double> exc(1, 0.0); |
| 28 | + std::vector<double> vrho(2, 0.0); |
| 29 | + xc_lda_exc_vxc(&func, 1, rho.data(), exc.data(), vrho.data()); |
| 30 | + if (weighted != nullptr) |
| 31 | + { |
| 32 | + *weighted = XC_Functional_Libxc::make_libxc_weighted_derivatives(func, |
| 33 | + 2, |
| 34 | + 1, |
| 35 | + mask, |
| 36 | + rho, |
| 37 | + std::vector<double>(), |
| 38 | + exc, |
| 39 | + vrho, |
| 40 | + std::vector<double>()); |
| 41 | + } |
| 42 | + return (rho_up + rho_down) * exc[0]; |
| 43 | + }; |
| 44 | + |
| 45 | + XC_Functional_Libxc::LibxcWeightedDerivatives weighted; |
| 46 | + const double energy = evaluate(density, 0.0, &weighted); |
| 47 | + ASSERT_EQ(weighted.drho.size(), 2U); |
| 48 | + EXPECT_DOUBLE_EQ(weighted.energy_sum, energy); |
| 49 | + |
| 50 | + const double steps[] = {1.0e-3, 5.0e-4, 2.5e-4, 1.25e-4}; |
| 51 | + std::array<double, 4> errors = {{0.0, 0.0, 0.0, 0.0}}; |
| 52 | + for (std::size_t ieps = 0; ieps < 4; ++ieps) |
| 53 | + { |
| 54 | + const double step = steps[ieps]; |
| 55 | + const double finite_difference |
| 56 | + = (evaluate(density + step, 0.0, nullptr) - evaluate(density - step, 0.0, nullptr)) / (2.0 * step); |
| 57 | + errors[ieps] = std::abs(weighted.drho[0] - finite_difference); |
| 58 | + EXPECT_LE(errors[ieps], 2.0e-7 * std::max(1.0, std::abs(weighted.drho[0]))) |
| 59 | + << "functional_id=" << functional_ids[ifunc] << ", step=" << step; |
| 60 | + } |
| 61 | + EXPECT_LE(errors[1], 0.4 * errors[0] + 1.0e-12); |
| 62 | + EXPECT_LE(errors[2], 0.4 * errors[1] + 1.0e-12); |
| 63 | + |
| 64 | + const double inactive_density = 0.5 * func.dens_threshold; |
| 65 | + const double inactive_step = 0.2 * func.dens_threshold; |
| 66 | + XC_Functional_Libxc::LibxcWeightedDerivatives inactive_weighted; |
| 67 | + evaluate(density, inactive_density, &inactive_weighted); |
| 68 | + const double inactive_finite_difference = (evaluate(density, inactive_density + inactive_step, nullptr) |
| 69 | + - evaluate(density, inactive_density - inactive_step, nullptr)) |
| 70 | + / (2.0 * inactive_step); |
| 71 | + EXPECT_NEAR(inactive_finite_difference, |
| 72 | + inactive_weighted.drho[1], |
| 73 | + 2.0e-8 * std::max(1.0, std::abs(inactive_finite_difference))); |
| 74 | + xc_func_end(&func); |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +TEST(LibxcSanitizer, GgaSigmaReverseMatchesTheWeightedEnergy) |
| 79 | +{ |
| 80 | + xc_func_type func; |
| 81 | + ASSERT_EQ(xc_func_init(&func, XC_GGA_C_PBE, XC_POLARIZED), 0); |
| 82 | + xc_func_set_dens_threshold(&func, 1.0e-6); |
| 83 | + xc_func_set_sigma_threshold(&func, 1.0e-2); |
| 84 | + |
| 85 | + const std::vector<double> mask = {1.0, 0.0}; |
| 86 | + const std::vector<double> density = {0.40, 0.20}; |
| 87 | + const double sigma_floor = func.sigma_threshold * func.sigma_threshold; |
| 88 | + const std::array<std::array<double, 3>, 5> sigma_states = {{{{0.040, 0.010, 0.030}}, |
| 89 | + {{0.5 * sigma_floor, 0.0, 0.030}}, |
| 90 | + {{0.040, 0.200, 0.030}}, |
| 91 | + {{0.040, -0.200, 0.030}}, |
| 92 | + {{0.5 * sigma_floor, 0.200, 0.030}}}}; |
| 93 | + |
| 94 | + const auto evaluate = [&func, &mask](const std::vector<double>& rho, |
| 95 | + const std::vector<double>& sigma, |
| 96 | + XC_Functional_Libxc::LibxcWeightedDerivatives* const weighted) { |
| 97 | + std::vector<double> exc(1, 0.0); |
| 98 | + std::vector<double> vrho(2, 0.0); |
| 99 | + std::vector<double> vsigma(3, 0.0); |
| 100 | + xc_gga_exc_vxc(&func, 1, rho.data(), sigma.data(), exc.data(), vrho.data(), vsigma.data()); |
| 101 | + if (weighted != nullptr) |
| 102 | + { |
| 103 | + *weighted |
| 104 | + = XC_Functional_Libxc::make_libxc_weighted_derivatives(func, 2, 1, mask, rho, sigma, exc, vrho, vsigma); |
| 105 | + } |
| 106 | + return (mask[0] * rho[0] + mask[1] * rho[1]) * exc[0]; |
| 107 | + }; |
| 108 | + |
| 109 | + for (std::size_t icase = 0; icase < sigma_states.size(); ++icase) |
| 110 | + { |
| 111 | + std::vector<double> sigma(sigma_states[icase].begin(), sigma_states[icase].end()); |
| 112 | + XC_Functional_Libxc::LibxcWeightedDerivatives weighted; |
| 113 | + const double energy = evaluate(density, sigma, &weighted); |
| 114 | + EXPECT_DOUBLE_EQ(weighted.energy_sum, energy); |
| 115 | + ASSERT_EQ(weighted.drho.size(), 2U); |
| 116 | + ASSERT_EQ(weighted.dsigma.size(), 3U); |
| 117 | + |
| 118 | + if (icase == 0) |
| 119 | + { |
| 120 | + for (int component = 0; component < 2; ++component) |
| 121 | + { |
| 122 | + std::vector<double> perturbed_density = density; |
| 123 | + const double step = 1.0e-6; |
| 124 | + perturbed_density[component] += step; |
| 125 | + const double energy_plus = evaluate(perturbed_density, sigma, nullptr); |
| 126 | + perturbed_density[component] -= 2.0 * step; |
| 127 | + const double energy_minus = evaluate(perturbed_density, sigma, nullptr); |
| 128 | + const double finite_difference = (energy_plus - energy_minus) / (2.0 * step); |
| 129 | + EXPECT_NEAR(finite_difference, |
| 130 | + weighted.drho[component], |
| 131 | + 2.0e-7 * std::max(1.0, std::abs(weighted.drho[component]))); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + if (icase == 1 || icase == 4) |
| 136 | + { |
| 137 | + EXPECT_DOUBLE_EQ(weighted.dsigma[0], 0.0); |
| 138 | + } |
| 139 | + if (icase >= 2) |
| 140 | + { |
| 141 | + EXPECT_DOUBLE_EQ(weighted.dsigma[1], 0.0); |
| 142 | + } |
| 143 | + |
| 144 | + for (int component = 0; component < 3; ++component) |
| 145 | + { |
| 146 | + const double step = 1.0e-6; |
| 147 | + sigma[component] += step; |
| 148 | + const double energy_plus = evaluate(density, sigma, nullptr); |
| 149 | + sigma[component] -= 2.0 * step; |
| 150 | + const double energy_minus = evaluate(density, sigma, nullptr); |
| 151 | + sigma[component] += step; |
| 152 | + const double finite_difference = (energy_plus - energy_minus) / (2.0 * step); |
| 153 | + EXPECT_NEAR(finite_difference, |
| 154 | + weighted.dsigma[component], |
| 155 | + 2.0e-7 * std::max(1.0, std::abs(weighted.dsigma[component]))) |
| 156 | + << "case=" << icase << ", sigma component=" << component; |
| 157 | + } |
| 158 | + } |
| 159 | + xc_func_end(&func); |
| 160 | +} |
| 161 | + |
| 162 | +TEST(LibxcSanitizer, UnpolarizedSelfSigmaReverseMatchesTheWeightedEnergy) |
| 163 | +{ |
| 164 | + xc_func_type func; |
| 165 | + ASSERT_EQ(xc_func_init(&func, XC_GGA_C_PBE, XC_UNPOLARIZED), 0); |
| 166 | + xc_func_set_dens_threshold(&func, 1.0e-6); |
| 167 | + xc_func_set_sigma_threshold(&func, 1.0e-2); |
| 168 | + |
| 169 | + const std::vector<double> mask = {1.0}; |
| 170 | + const std::vector<double> density = {0.40}; |
| 171 | + const double sigma_floor = func.sigma_threshold * func.sigma_threshold; |
| 172 | + |
| 173 | + const auto evaluate = [&func, &mask, &density](const double sigma_value, |
| 174 | + XC_Functional_Libxc::LibxcWeightedDerivatives* const weighted) { |
| 175 | + const std::vector<double> sigma = {sigma_value}; |
| 176 | + std::vector<double> exc(1, 0.0); |
| 177 | + std::vector<double> vrho(1, 0.0); |
| 178 | + std::vector<double> vsigma(1, 0.0); |
| 179 | + xc_gga_exc_vxc(&func, 1, density.data(), sigma.data(), exc.data(), vrho.data(), vsigma.data()); |
| 180 | + if (weighted != nullptr) |
| 181 | + { |
| 182 | + *weighted = XC_Functional_Libxc::make_libxc_weighted_derivatives(func, |
| 183 | + 1, |
| 184 | + 1, |
| 185 | + mask, |
| 186 | + density, |
| 187 | + sigma, |
| 188 | + exc, |
| 189 | + vrho, |
| 190 | + vsigma); |
| 191 | + } |
| 192 | + return mask[0] * density[0] * exc[0]; |
| 193 | + }; |
| 194 | + |
| 195 | + const double below_floor_sigma = 0.5 * sigma_floor; |
| 196 | + const double below_floor_step = 0.2 * sigma_floor; |
| 197 | + XC_Functional_Libxc::LibxcWeightedDerivatives below_floor_weighted; |
| 198 | + const double below_floor_energy = evaluate(below_floor_sigma, &below_floor_weighted); |
| 199 | + ASSERT_EQ(below_floor_weighted.dsigma.size(), 1U); |
| 200 | + EXPECT_DOUBLE_EQ(below_floor_weighted.energy_sum, below_floor_energy); |
| 201 | + EXPECT_DOUBLE_EQ(below_floor_weighted.dsigma[0], 0.0); |
| 202 | + const double below_floor_finite_difference = (evaluate(below_floor_sigma + below_floor_step, nullptr) |
| 203 | + - evaluate(below_floor_sigma - below_floor_step, nullptr)) |
| 204 | + / (2.0 * below_floor_step); |
| 205 | + EXPECT_NEAR(below_floor_finite_difference, below_floor_weighted.dsigma[0], 1.0e-12); |
| 206 | + |
| 207 | + const double above_floor_sigma = 0.040; |
| 208 | + const double above_floor_step = 1.0e-6; |
| 209 | + XC_Functional_Libxc::LibxcWeightedDerivatives above_floor_weighted; |
| 210 | + const double above_floor_energy = evaluate(above_floor_sigma, &above_floor_weighted); |
| 211 | + ASSERT_EQ(above_floor_weighted.dsigma.size(), 1U); |
| 212 | + EXPECT_DOUBLE_EQ(above_floor_weighted.energy_sum, above_floor_energy); |
| 213 | + EXPECT_GT(std::abs(above_floor_weighted.dsigma[0]), 1.0e-12); |
| 214 | + const double above_floor_finite_difference = (evaluate(above_floor_sigma + above_floor_step, nullptr) |
| 215 | + - evaluate(above_floor_sigma - above_floor_step, nullptr)) |
| 216 | + / (2.0 * above_floor_step); |
| 217 | + EXPECT_NEAR(above_floor_finite_difference, |
| 218 | + above_floor_weighted.dsigma[0], |
| 219 | + 2.0e-7 * std::max(1.0, std::abs(above_floor_weighted.dsigma[0]))); |
| 220 | + |
| 221 | + xc_func_end(&func); |
| 222 | +} |
| 223 | + |
| 224 | +#endif |
0 commit comments