|
| 1 | +#include "module_base/grid/partition.h" |
| 2 | +#include "module_base/constants.h" |
| 3 | + |
| 4 | +#include <cmath> |
| 5 | +#include <functional> |
| 6 | +#include <numeric> |
| 7 | +#include <algorithm> |
| 8 | +#include <vector> |
| 9 | +#include <cassert> |
| 10 | + |
| 11 | +namespace Grid { |
| 12 | +namespace Partition { |
| 13 | + |
| 14 | +const double stratmann_a = 0.64; |
| 15 | + |
| 16 | +double w_becke( |
| 17 | + int nR0, |
| 18 | + const double* drR, |
| 19 | + const double* dRR, |
| 20 | + int nR, |
| 21 | + const int* iR, |
| 22 | + int c |
| 23 | +) { |
| 24 | + assert(nR > 0 && nR0 >= nR); |
| 25 | + std::vector<double> P(nR, 1.0); |
| 26 | + for (int i = 0; i < nR; ++i) { |
| 27 | + int I = iR[i]; |
| 28 | + for (int j = i + 1; j < nR; ++j) { |
| 29 | + int J = iR[j]; |
| 30 | + double mu = (drR[I] - drR[J]) / dRR[I*nR0 + J]; |
| 31 | + double s = s_becke(mu); |
| 32 | + P[I] *= s; |
| 33 | + P[J] *= (1.0 - s); // s(-mu) = 1 - s(mu) |
| 34 | + } |
| 35 | + } |
| 36 | + return P[c] / std::accumulate(P.begin(), P.end(), 0.0); |
| 37 | +} |
| 38 | + |
| 39 | + |
| 40 | +double s_becke(double mu) { |
| 41 | + /* |
| 42 | + * Becke's iterated polynomials (3rd order) |
| 43 | + * |
| 44 | + * s(mu) = 0.5 * (1 - p(p(p(mu)))) |
| 45 | + * |
| 46 | + * p(x) = 0.5 * x * (3 - x^2) |
| 47 | + * |
| 48 | + */ |
| 49 | + double p = 0.5 * mu * (3.0 - mu*mu); |
| 50 | + p = 0.5 * p * (3.0 - p*p); |
| 51 | + p = 0.5 * p * (3.0 - p*p); |
| 52 | + return 0.5 * (1.0 - p); |
| 53 | +} |
| 54 | + |
| 55 | + |
| 56 | +double w_stratmann( |
| 57 | + int nR0, |
| 58 | + const double* drR, |
| 59 | + const double* dRR, |
| 60 | + const double* drR_thr, |
| 61 | + int nR, |
| 62 | + int* iR, |
| 63 | + int c |
| 64 | +) { |
| 65 | + assert(nR > 0 && nR0 >= nR); |
| 66 | + int I = iR[c], J = 0; |
| 67 | + |
| 68 | + // If r falls within the exclusive zone of a center, return immediately. |
| 69 | + for (int j = 0; j < nR; ++j) { |
| 70 | + J = iR[j]; |
| 71 | + if (drR[J] <= drR_thr[J]) { |
| 72 | + return static_cast<double>(I == J); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + // Even if the grid point does not fall within the exclusive zone of any |
| 77 | + // center, the normalized weight could still be 0 or 1, and this can be |
| 78 | + // figured out by examining the unnormalized weight alone. |
| 79 | + |
| 80 | + // Swap the grid center to the first position in iteration for convenience. |
| 81 | + // Restore the original order before return. |
| 82 | + std::swap(iR[0], iR[c]); |
| 83 | + |
| 84 | + std::vector<double> P(nR); |
| 85 | + for (int j = 1; j < nR; ++j) { |
| 86 | + J = iR[j]; |
| 87 | + double mu = (drR[I] - drR[J]) / dRR[I*nR0 + J]; |
| 88 | + P[j] = s_stratmann(mu); |
| 89 | + } |
| 90 | + P[0] = std::accumulate(P.begin() + 1, P.end(), 1.0, |
| 91 | + std::multiplies<double>()); |
| 92 | + |
| 93 | + if (P[0] == 0.0 || P[0] == 1.0) { |
| 94 | + std::swap(iR[0], iR[c]); // restore the original order |
| 95 | + return P[0]; |
| 96 | + } |
| 97 | + |
| 98 | + // If it passes all the screening above, all unnormalized weights |
| 99 | + // have to be calculated in order to get the normalized weight. |
| 100 | + |
| 101 | + std::for_each(P.begin() + 1, P.end(), [](double& s) { s = 1.0 - s; }); |
| 102 | + for (int i = 1; i < nR; ++i) { |
| 103 | + I = iR[i]; |
| 104 | + for (int j = i + 1; j < nR; ++j) { |
| 105 | + J = iR[j]; |
| 106 | + double mu = (drR[I] - drR[J]) / dRR[I*nR0 + J]; |
| 107 | + double s = s_stratmann(mu); |
| 108 | + P[i] *= s; |
| 109 | + P[j] *= (1.0 - s); // s(-mu) = 1 - s(mu) |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + std::swap(iR[0], iR[c]); // restore the original order |
| 114 | + return P[0] / std::accumulate(P.begin(), P.end(), 0.0); |
| 115 | +} |
| 116 | + |
| 117 | + |
| 118 | +double s_stratmann(double mu) { |
| 119 | + /* |
| 120 | + * Stratmann's piecewise cell function |
| 121 | + * |
| 122 | + * s(mu) = 0.5 * (1 - g(mu/a)) |
| 123 | + * |
| 124 | + * / -1 x <= -1 |
| 125 | + * | |
| 126 | + * g(x) = | (35x - 35x^3 + 21x^5 - 5x^7) / 16 |x| < 1 |
| 127 | + * | |
| 128 | + * \ +1 x >= +1 |
| 129 | + * |
| 130 | + */ |
| 131 | + double x = mu / stratmann_a; |
| 132 | + double x2 = x * x; |
| 133 | + double h = 0.0625 * x * (35 + x2 * (-35 + x2 * (21 - 5 * x2))); |
| 134 | + |
| 135 | + bool mid = std::abs(x) < 1; |
| 136 | + double g = !mid * (1 - 2 * std::signbit(x)) + mid * h; |
| 137 | + return 0.5 * (1.0 - g); |
| 138 | +} |
| 139 | + |
| 140 | + |
| 141 | +} // end of namespace Partition |
| 142 | +} // end of namespace Grid |
0 commit comments