Skip to content

Commit a01cecb

Browse files
authored
Merge pull request brucefan1983#1382 from brucefan1983/ewald_num_kopints
fix num_kpoints in ewald
2 parents 1b83521 + 0a6ff00 commit a01cecb

2 files changed

Lines changed: 7 additions & 48 deletions

File tree

src/force/ewald.cu

Lines changed: 6 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,6 @@ Ewald::~Ewald()
3636

3737
void Ewald::initialize(const float alpha_input)
3838
{
39-
#ifdef DEBUG
40-
rng = std::mt19937(12345678);
41-
#else
42-
rng = std::mt19937(std::chrono::system_clock::now().time_since_epoch().count());
43-
#endif
4439
alpha = alpha_input;
4540
alpha_factor = 0.25f / (alpha * alpha);
4641
kx.resize(num_kpoints_max);
@@ -109,40 +104,6 @@ void Ewald::find_k_and_G(const double* box)
109104
std::vector<float> cpu_kz;
110105
std::vector<float> cpu_G;
111106

112-
#ifdef USE_RBE
113-
std::uniform_real_distribution<float> rand_number(0.0f, 1.0f);
114-
float normalization_factor = 0.0f;
115-
for (int n1 = 0; n1 <= n1_max; ++n1) {
116-
for (int n2 = - n2_max; n2 <= n2_max; ++n2) {
117-
for (int n3 = - n3_max; n3 <= n3_max; ++n3) {
118-
const int nsq = n1 * n1 + n2 * n2 + n3 * n3;
119-
if (nsq == 0 || (n1 == 0 && n2 < 0) || (n1 == 0 && n2 == 0 && n3 < 0)) continue;
120-
const float kx = n1 * b1[0] + n2 * b2[0] + n3 * b3[0];
121-
const float ky = n1 * b1[1] + n2 * b2[1] + n3 * b3[1];
122-
const float kz = n1 * b1[2] + n2 * b2[2] + n3 * b3[2];
123-
const float ksq = kx * kx + ky * ky + kz * kz;
124-
if (ksq < ksq_max) {
125-
float exp_factor = exp(-ksq * alpha_factor);
126-
normalization_factor += exp_factor;
127-
if (rand_number(rng) < exp_factor) {
128-
cpu_kx.emplace_back(kx);
129-
cpu_ky.emplace_back(ky);
130-
cpu_kz.emplace_back(kz);
131-
float G = abs(two_pi_over_det) / ksq;
132-
cpu_G.emplace_back(2.0f * G);
133-
}
134-
}
135-
}
136-
}
137-
}
138-
139-
int num_kpoints = int(cpu_kx.size());
140-
for (int n = 0; n < num_kpoints; ++n) {
141-
cpu_G[n] *= normalization_factor / num_kpoints;
142-
}
143-
144-
#else
145-
146107
for (int n1 = 0; n1 <= n1_max; ++n1) {
147108
for (int n2 = - n2_max; n2 <= n2_max; ++n2) {
148109
for (int n3 = - n3_max; n3 <= n3_max; ++n3) {
@@ -163,8 +124,7 @@ void Ewald::find_k_and_G(const double* box)
163124
}
164125
}
165126

166-
int num_kpoints = int(cpu_kx.size());
167-
#endif
127+
num_kpoints = int(cpu_kx.size());
168128

169129
if (num_kpoints > num_kpoints_max) {
170130
num_kpoints_max = num_kpoints;
@@ -183,7 +143,7 @@ void Ewald::find_k_and_G(const double* box)
183143
}
184144

185145
static __global__ void find_structure_factor(
186-
const int num_kpoints_max,
146+
const int num_kpoints,
187147
const int N1,
188148
const int N2,
189149
const float* g_charge,
@@ -197,7 +157,7 @@ static __global__ void find_structure_factor(
197157
float* g_S_imag)
198158
{
199159
int nk = blockIdx.x * blockDim.x + threadIdx.x;
200-
if (nk < num_kpoints_max) {
160+
if (nk < num_kpoints) {
201161
float S_real = 0.0f;
202162
float S_imag = 0.0f;
203163
for (int n = N1; n < N2; ++n) {
@@ -300,8 +260,8 @@ void Ewald::find_force(
300260
GPU_Vector<double>& potential_per_atom)
301261
{
302262
find_k_and_G(box);
303-
find_structure_factor<<<(num_kpoints_max - 1) / 64 + 1, 64>>>(
304-
num_kpoints_max,
263+
find_structure_factor<<<(num_kpoints - 1) / 64 + 1, 64>>>(
264+
num_kpoints,
305265
N1,
306266
N2,
307267
charge.data(),
@@ -319,7 +279,7 @@ void Ewald::find_force(
319279
N,
320280
N1,
321281
N2,
322-
num_kpoints_max,
282+
num_kpoints,
323283
alpha_factor,
324284
charge.data(),
325285
position_per_atom.data(),

src/force/ewald.cuh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
#pragma once
1717
#include "utilities/gpu_vector.cuh"
18-
#include <random>
1918

2019
class Ewald
2120
{
@@ -36,6 +35,7 @@ public:
3635
GPU_Vector<double>& potential_per_atom);
3736
private:
3837
int num_kpoints_max = 1;
38+
int num_kpoints = 1;
3939
float alpha = 0.5f; // 1 / (2 Angstrom)
4040
float alpha_factor = 1.0f; // 1 / (4 * alpha * alpha)
4141
GPU_Vector<float> kx;
@@ -44,6 +44,5 @@ private:
4444
GPU_Vector<float> G;
4545
GPU_Vector<float> S_real;
4646
GPU_Vector<float> S_imag;
47-
std::mt19937 rng;
4847
void find_k_and_G(const double* box);
4948
};

0 commit comments

Comments
 (0)