Skip to content

Commit 84ec6f6

Browse files
committed
use std::vector instead of builtin array
1 parent 2ff417d commit 84ec6f6

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

source/module_base/grid/partition.cpp

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include <functional>
66
#include <numeric>
77
#include <algorithm>
8+
#include <vector>
9+
#include <cassert>
810

911
namespace Grid {
1012
namespace Partition {
@@ -19,8 +21,8 @@ double w_becke(
1921
const int* iR,
2022
int c
2123
) {
22-
double P[nR];
23-
std::fill(P, P + nR, 1.0);
24+
assert(nR > 0 && nR0 >= nR);
25+
std::vector<double> P(nR, 1.0);
2426
for (int i = 0; i < nR; ++i) {
2527
int I = iR[i];
2628
for (int j = i + 1; j < nR; ++j) {
@@ -31,7 +33,7 @@ double w_becke(
3133
P[J] *= (1.0 - s); // s(-mu) = 1 - s(mu)
3234
}
3335
}
34-
return P[c] / std::accumulate(P, P + nR, 0.0);
36+
return P[c] / std::accumulate(P.begin(), P.end(), 0.0);
3537
}
3638

3739

@@ -56,10 +58,11 @@ double w_stratmann(
5658
const double* drR,
5759
const double* dRR,
5860
const double* drR_thr,
59-
const int nR,
61+
int nR,
6062
int* iR,
6163
int c
6264
) {
65+
assert(nR > 0 && nR0 >= nR);
6366
int I = iR[c], J = 0;
6467

6568
// If r falls within the exclusive zone of a center, return immediately.
@@ -74,26 +77,28 @@ double w_stratmann(
7477
// center, the normalized weight could still be 0 or 1, and this can be
7578
// figured out by examining the unnormalized weight alone.
7679

77-
// Move the center to the first position for convenience. Swap back later.
80+
// Swap the grid center to the first position in iteration for convenience.
81+
// Restore the original order before return.
7882
std::swap(iR[0], iR[c]);
7983

80-
double P[nR];
84+
std::vector<double> P(nR);
8185
for (int j = 1; j < nR; ++j) {
8286
J = iR[j];
8387
double mu = (drR[I] - drR[J]) / dRR[I*nR0 + J];
8488
P[j] = s_stratmann(mu);
8589
}
86-
P[0] = std::accumulate(P + 1, P + nR, 1.0, std::multiplies<double>());
90+
P[0] = std::accumulate(P.begin() + 1, P.end(), 1.0,
91+
std::multiplies<double>());
8792

8893
if (P[0] == 0.0 || P[0] == 1.0) {
89-
std::swap(iR[0], iR[c]);
94+
std::swap(iR[0], iR[c]); // restore the original order
9095
return P[0];
9196
}
9297

93-
// If it passes all the screening, all unnormalized weights have to be
94-
// calculated in order to get the normalized weight.
98+
// If it passes all the screening above, all unnormalized weights
99+
// have to be calculated in order to get the normalized weight.
95100

96-
std::for_each(P + 1, P + nR, [](double& s) { s = 1.0 - s; });
101+
std::for_each(P.begin() + 1, P.end(), [](double& s) { s = 1.0 - s; });
97102
for (int i = 1; i < nR; ++i) {
98103
I = iR[i];
99104
for (int j = i + 1; j < nR; ++j) {
@@ -105,8 +110,8 @@ double w_stratmann(
105110
}
106111
}
107112

108-
std::swap(iR[0], iR[c]);
109-
return P[0] / std::accumulate(P, P + nR, 0.0);
113+
std::swap(iR[0], iR[c]); // restore the original order
114+
return P[0] / std::accumulate(P.begin(), P.end(), 0.0);
110115
}
111116

112117

@@ -129,7 +134,7 @@ double s_stratmann(double mu) {
129134

130135
bool mid = std::abs(x) < 1;
131136
double g = !mid * (1 - 2 * std::signbit(x)) + mid * h;
132-
return 0.5 * (1 - g);
137+
return 0.5 * (1.0 - g);
133138
}
134139

135140

0 commit comments

Comments
 (0)