Skip to content

Commit 728fb10

Browse files
authored
Merge pull request brucefan1983#1039 from brucefan1983/ref_total_charge
use ref charge
2 parents 27702f1 + 0c22d01 commit 728fb10

File tree

6 files changed

+39
-10
lines changed

6 files changed

+39
-10
lines changed

src/main_nep/dataset.cu

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -897,7 +897,7 @@ static __global__ void gpu_sum_charge_error(
897897
}
898898

899899
if (tid == 0) {
900-
float diff = s_charge[0] / Na - g_charge_ref[bid];
900+
float diff = (s_charge[0] - g_charge_ref[bid]) / Na;
901901
error_gpu[bid] = diff * diff;
902902
}
903903
}

src/main_nep/nep_charge.cu

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,7 +1075,8 @@ static __global__ void find_k_and_G(
10751075
static __global__ void zero_total_charge(
10761076
const int* Na,
10771077
const int* Na_sum,
1078-
float* g_charge,
1078+
const float* g_charge_ref,
1079+
const float* g_charge,
10791080
float* g_charge_shifted)
10801081
{
10811082
int tid = threadIdx.x;
@@ -1103,7 +1104,7 @@ static __global__ void zero_total_charge(
11031104
for (int batch = 0; batch < number_of_batches; ++batch) {
11041105
int n = tid + batch * 1024 + N1;
11051106
if (n < N2) {
1106-
g_charge_shifted[n] = g_charge[n] - s_charge[0] / (N2 - N1);
1107+
g_charge_shifted[n] = g_charge[n] + (g_charge_ref[blockIdx.x] - s_charge[0]) / (N2 - N1);
11071108
}
11081109
}
11091110
}
@@ -1245,14 +1246,16 @@ void NEP_Charge::find_force(
12451246
nep_data[device_id].charge_derivative.data());
12461247
GPU_CHECK_KERNEL
12471248

1248-
// enforce charge neutrality
1249+
// enforce total charge is the target
12491250
zero_total_charge<<<dataset[device_id].Nc, 1024>>>(
12501251
dataset[device_id].Na.data(),
12511252
dataset[device_id].Na_sum.data(),
1253+
dataset[device_id].charge_ref_gpu.data(),
12521254
dataset[device_id].charge.data(),
12531255
dataset[device_id].charge_shifted.data());
12541256
GPU_CHECK_KERNEL
12551257

1258+
// modes 1 and 2 have reciprocal space
12561259
if (paramb.charge_mode != 3) {
12571260
find_k_and_G<<<(dataset[device_id].Nc - 1) / 64 + 1, 64>>>(
12581261
dataset[device_id].Nc,
@@ -1309,8 +1312,7 @@ void NEP_Charge::find_force(
13091312
GPU_CHECK_KERNEL
13101313
}
13111314

1312-
// charge_mode = 1: include real space and self energy
1313-
// charge_mode = 2: exclude real space and self energy
1315+
// mode 1 has real space
13141316
if (paramb.charge_mode == 1) {
13151317
find_force_charge_real_space<<<grid_size, block_size>>>(
13161318
dataset[device_id].N,
@@ -1329,7 +1331,10 @@ void NEP_Charge::find_force(
13291331
dataset[device_id].energy.data(),
13301332
nep_data[device_id].D_real.data());
13311333
GPU_CHECK_KERNEL
1332-
} else if (paramb.charge_mode == 3) {
1334+
}
1335+
1336+
// mode 3 has real space only
1337+
if (paramb.charge_mode == 3) {
13331338
find_force_charge_real_space_only<<<grid_size, block_size>>>(
13341339
dataset[device_id].N,
13351340
charge_para.alpha,

src/main_nep/parameters.cu

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ void Parameters::calculate_parameters()
243243
}
244244
std::vector<std::string> tokens;
245245
const int NUM89 = 89;
246-
const int num_ann_per_element = (dim + 2) * num_neurons1;
246+
const int num_ann_per_element = (dim + (charge_mode ? 3 : 2)) * num_neurons1;
247247
const int num_ann = NUM89 * num_ann_per_element + 1;
248248
const int num_cnk_radial = NUM89 * NUM89 * (n_max_radial + 1) * (basis_size_radial + 1);
249249
const int num_cnk_angular = NUM89 * NUM89 * (n_max_angular + 1) * (basis_size_angular + 1);

src/main_nep/snes.cu

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ void SNES::initialize_mu_and_sigma_fine_tune(Parameters& para)
130130
};
131131
// read in the whole foundation file first
132132
const int NUM89 = 89;
133-
const int num_ann_per_element = (para.dim + 2) * para.num_neurons1;
133+
const int num_ann_per_element = (para.dim + (para.charge_mode ? 3 : 2)) * para.num_neurons1;
134134
const int num_ann = NUM89 * num_ann_per_element + 1;
135135
const int num_cnk_radial = NUM89 * NUM89 * (para.n_max_radial + 1) * (para.basis_size_radial + 1);
136136
const int num_cnk_angular = NUM89 * NUM89 * (para.n_max_angular + 1) * (para.basis_size_angular + 1);

src/main_nep/structure.cu

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,15 @@ static void read_one_structure(
215215
PRINT_INPUT_ERROR("'energy' is missing in the second line of a frame.");
216216
}
217217

218+
// get total charge (optional; default is 0)
219+
for (const auto& token : tokens) {
220+
const std::string charge_string = "charge=";
221+
if (token.substr(0, charge_string.length()) == charge_string) {
222+
structure.charge = get_double_from_token(
223+
token.substr(charge_string.length(), token.length()), xyz_filename.c_str(), line_number);
224+
}
225+
}
226+
218227
structure.has_temperature = false;
219228
for (const auto& token : tokens) {
220229
const std::string temperature_string = "temperature=";

tools/Miscellaneous/for_coding/for_perioidc_table/nep_data_toolkit.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <sstream>
2222
#include <string>
2323
#include <vector>
24+
#include <iomanip>
2425

2526
static std::string remove_spaces_step1(const std::string& line)
2627
{
@@ -161,6 +162,7 @@ struct Structure {
161162
bool has_sid = false;
162163
bool has_virial = false;
163164
bool has_stress = false;
165+
double charge = 0.0;
164166
double energy_weight = 1.0;
165167
double energy;
166168
double weight;
@@ -232,6 +234,15 @@ static void read_one_structure(std::ifstream& input, Structure& structure)
232234
}
233235
}
234236

237+
// get charge (optional)
238+
for (const auto& token : tokens) {
239+
const std::string charge_string = "charge=";
240+
if (token.substr(0, charge_string.length()) == charge_string) {
241+
structure.charge = get_double_from_token(
242+
token.substr(charge_string.length(), token.length()), __FILE__, __LINE__);
243+
}
244+
}
245+
235246
// get energy_weight (optional)
236247
for (const auto& token : tokens) {
237248
const std::string energy_weight_string = "energy_weight=";
@@ -408,6 +419,10 @@ static void write_one_structure(std::ofstream& output, const Structure& structur
408419
{
409420
output << structure.num_atom << "\n";
410421

422+
if (structure.charge != 0.0) {
423+
output << "charge=" << structure.charge << " ";
424+
}
425+
411426
if (structure.energy_weight != 1.0) {
412427
output << "energy_weight=" << structure.energy_weight << " ";
413428
}
@@ -421,7 +436,7 @@ static void write_one_structure(std::ofstream& output, const Structure& structur
421436
}
422437
output << "\" ";
423438

424-
output << "energy=" << structure.energy << " ";
439+
output << "energy=" << std::fixed << std::setprecision(6) << structure.energy << " ";
425440

426441
if (structure.has_virial) {
427442
output << "virial=\"";

0 commit comments

Comments
 (0)