Skip to content

Commit

Permalink
More unit tests and adjust threading.
Browse files Browse the repository at this point in the history
  • Loading branch information
ye-luo committed Jan 25, 2022
1 parent dc47d6c commit fcf42ed
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 15 deletions.
35 changes: 21 additions & 14 deletions src/QMCHamiltonians/EwaldRef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -365,26 +365,33 @@ real_t ewaldEnergy(const RealMat& a, const PosArray& R, const ChargeArray& Q, re
// Sum the interaction terms for all particle pairs
ScopedTimer EwaldSumTimer(*timer_manager.createTimer("EwaldSum"));

int_t Npairs = (N * (N - 1)) / 2;

std::vector<real_t> qq(Npairs);
for (size_t i = 0, n = 0; i < N; ++i)
for (size_t j = 0; j < i; ++j, ++n)
qq[n] = Q[i] * Q[j];

std::vector<RealVec> rr(Npairs);
for (size_t i = 0, n = 0; i < N; ++i)
for (size_t j = 0; j < i; ++j, ++n)
#pragma omp parallel for reduction(+ : ve)
for (size_t i = 1; i < N / 2 + 1; ++i)
{
for (size_t j = 0; j < i; ++j)
{
real_t qq = Q[i] * Q[j];
RealVec reduced = dot(R[i] - R[j], inverse(a));
for (size_t dim = 0; dim < DIM; dim++)
reduced[dim] -= std::floor(reduced[dim]);
rr[n] = dot(reduced, a);
RealVec rr = dot(reduced, a);
ve += qq * ewaldSum(rr, a, tol / qq);
}

#pragma omp parallel for reduction(+ : ve)
for (size_t n = 0; n < Npairs; ++n)
ve += qq[n] * ewaldSum(rr[n], a, tol / qq[n]);
const size_t i_reverse = N - i;
if (i == i_reverse)
continue;

for (size_t j = 0; j < i_reverse; ++j)
{
real_t qq = Q[i_reverse] * Q[j];
RealVec reduced = dot(R[i_reverse] - R[j], inverse(a));
for (size_t dim = 0; dim < DIM; dim++)
reduced[dim] -= std::floor(reduced[dim]);
RealVec rr = dot(reduced, a);
ve += qq * ewaldSum(rr, a, tol / qq);
}
}
}

return ve;
Expand Down
24 changes: 23 additions & 1 deletion src/QMCHamiltonians/tests/test_EwaldRef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,29 @@ TEST_CASE("EwaldRef", "[hamiltonian]")
R[3] = {5.05974173, 5.05974173, 1.68658058};

auto Vii_ref = ewaldref::ewaldEnergy(A, R, Q);
CHECK(Approx(Vii_ref) == -2.5551326969e+01);
CHECK(Approx(Vii_ref) == -25.551326969);
}

SECTION("diamondC_2x1x1 fake 5 ions")
{
ewaldref::RealMat A;
ewaldref::PosArray R;
ewaldref::ChargeArray Q;

A = {6.7463223, 6.7463223, 0.0, 0.0, 3.37316115, 3.37316115, 3.37316115, 0.0, 3.37316115};

const int num_centers = 5;
R.resize(num_centers);
Q.resize(num_centers, 4);

R[0] = {0.0, 0.0, 0.0};
R[1] = {1.68658058, 1.68658058, 1.68658058};
R[2] = {3.37316115, 3.37316115, 0.0};
R[3] = {5.05974173, 5.05974173, 1.68658058};
R[4] = {5.05974173, 3.37316115, 0.0};

auto Vii_ref = ewaldref::ewaldEnergy(A, R, Q);
CHECK(Approx(Vii_ref) == -30.9149928426);
}

SECTION("32 H2O particle outside the box")
Expand Down

0 comments on commit fcf42ed

Please sign in to comment.