Skip to content

Commit 0bb505d

Browse files
authored
Merge pull request brucefan1983#1193 from brucefan1983/ewald-pppm-switch
allow switching between ewald and pppm
2 parents ae0c701 + cd69c04 commit 0bb505d

File tree

3 files changed

+95
-23
lines changed

3 files changed

+95
-23
lines changed

src/force/nep_charge.cu

Lines changed: 89 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,39 @@ const std::string ELEMENTS[NUM_ELEMENTS] = {
4343
"Tb", "Dy", "Ho", "Er", "Tm", "Yb", "Lu", "Hf", "Ta", "W", "Re", "Os", "Ir", "Pt", "Au", "Hg",
4444
"Tl", "Pb", "Bi", "Po", "At", "Rn", "Fr", "Ra", "Ac", "Th", "Pa", "U", "Np", "Pu"};
4545

46+
void NEP_Charge::check_ewald_pppm()
47+
{
48+
std::ifstream input_run("run.in");
49+
if (!input_run.is_open()) {
50+
PRINT_INPUT_ERROR("Cannot open run.in.");
51+
}
52+
53+
use_pppm = true;
54+
std::string line;
55+
while (std::getline(input_run, line)) {
56+
std::vector<std::string> tokens = get_tokens(line);
57+
if (tokens.size() != 0) {
58+
if (tokens[0] == "kspace") {
59+
if (tokens.size() != 2) {
60+
std::cout << "kspace must have 1 parameter\n";
61+
exit(1);
62+
}
63+
std::string kspace_method = tokens[1];
64+
if (kspace_method == "ewald") {
65+
use_pppm = false;
66+
} else if (kspace_method == "pppm") {
67+
use_pppm = true;
68+
} else {
69+
std::cout << "kspace method can only be ewald or pppm\n";
70+
exit(1);
71+
}
72+
}
73+
}
74+
}
75+
76+
input_run.close();
77+
}
78+
4679
void NEP_Charge::initialize_dftd3()
4780
{
4881
std::ifstream input_run("run.in");
@@ -316,7 +349,12 @@ NEP_Charge::NEP_Charge(const char* file_potential, const int num_atoms)
316349

317350
// charge related parameters and data
318351
charge_para.alpha = float(PI) / paramb.rc_radial; // a good value
319-
pppm.initialize(charge_para.alpha);
352+
check_ewald_pppm();
353+
if (use_pppm) {
354+
pppm.initialize(charge_para.alpha);
355+
} else {
356+
ewald.initialize(charge_para.alpha);
357+
}
320358
charge_para.two_alpha_over_sqrt_pi = 2.0f * charge_para.alpha / sqrt(float(PI));
321359
charge_para.A = erfc(float(PI)) / (paramb.rc_radial * paramb.rc_radial);
322360
charge_para.A += charge_para.two_alpha_over_sqrt_pi * exp(-float(PI * PI)) / paramb.rc_radial;
@@ -1850,17 +1888,31 @@ void NEP_Charge::compute_large_box(
18501888
}
18511889

18521890
if (paramb.charge_mode == 1 || paramb.charge_mode == 2 || paramb.charge_mode == 4) {
1853-
pppm.find_force(
1854-
N,
1855-
N1,
1856-
N2,
1857-
box,
1858-
nep_data.charge,
1859-
position_per_atom,
1860-
nep_data.D_real,
1861-
force_per_atom,
1862-
virial_per_atom,
1863-
potential_per_atom);
1891+
if (use_pppm) {
1892+
pppm.find_force(
1893+
N,
1894+
N1,
1895+
N2,
1896+
box,
1897+
nep_data.charge,
1898+
position_per_atom,
1899+
nep_data.D_real,
1900+
force_per_atom,
1901+
virial_per_atom,
1902+
potential_per_atom);
1903+
} else {
1904+
ewald.find_force(
1905+
N,
1906+
N1,
1907+
N2,
1908+
box.cpu_h,
1909+
nep_data.charge,
1910+
position_per_atom,
1911+
nep_data.D_real,
1912+
force_per_atom,
1913+
virial_per_atom,
1914+
potential_per_atom);
1915+
}
18641916
}
18651917

18661918
if (paramb.charge_mode == 1) {
@@ -2176,17 +2228,31 @@ void NEP_Charge::compute_small_box(
21762228
}
21772229

21782230
if (paramb.charge_mode == 1 || paramb.charge_mode == 2 || paramb.charge_mode == 4) {
2179-
pppm.find_force(
2180-
N,
2181-
N1,
2182-
N2,
2183-
box,
2184-
nep_data.charge,
2185-
position_per_atom,
2186-
nep_data.D_real,
2187-
force_per_atom,
2188-
virial_per_atom,
2189-
potential_per_atom);
2231+
if (use_pppm) {
2232+
pppm.find_force(
2233+
N,
2234+
N1,
2235+
N2,
2236+
box,
2237+
nep_data.charge,
2238+
position_per_atom,
2239+
nep_data.D_real,
2240+
force_per_atom,
2241+
virial_per_atom,
2242+
potential_per_atom);
2243+
} else {
2244+
ewald.find_force(
2245+
N,
2246+
N1,
2247+
N2,
2248+
box.cpu_h,
2249+
nep_data.charge,
2250+
position_per_atom,
2251+
nep_data.D_real,
2252+
force_per_atom,
2253+
virial_per_atom,
2254+
potential_per_atom);
2255+
}
21902256
}
21912257

21922258
if (paramb.charge_mode == 1) {

src/force/nep_charge.cuh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "potential.cuh"
1919
#include "utilities/common.cuh"
2020
#include "utilities/gpu_vector.cuh"
21+
#include "ewald.cuh"
2122
#include "pppm.cuh"
2223

2324
struct NEP_Charge_Data {
@@ -152,6 +153,7 @@ private:
152153
ExpandedBox ebox;
153154
DFTD3 dftd3;
154155
Charge_Para charge_para;
156+
Ewald ewald;
155157
PPPM pppm;
156158

157159
void update_potential(float* parameters, ANN& ann);
@@ -177,6 +179,8 @@ private:
177179

178180
void find_k_and_G(const double* box);
179181

182+
bool use_pppm = true; // use PPPM by default
183+
void check_ewald_pppm();
180184
bool has_dftd3 = false;
181185
void initialize_dftd3();
182186
};

src/main_gpumd/run.cu

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,8 @@ void Run::parse_one_keyword(std::vector<std::string>& tokens)
566566
add_efield.parse(param, num_param, group);
567567
} else if (strcmp(param[0], "mc") == 0) {
568568
mc.parse_mc(param, num_param, group, atom);
569+
} else if (strcmp(param[0], "kspace") == 0) {
570+
// nothing here; will be handled elsewhere
569571
} else if (strcmp(param[0], "dftd3") == 0) {
570572
// nothing here; will be handled elsewhere
571573
} else if (strcmp(param[0], "compute_lsqt") == 0) {

0 commit comments

Comments
 (0)