Skip to content

Commit

Permalink
Merge pull request brucefan1983#699 from brucefan1983/report_line_of_…
Browse files Browse the repository at this point in the history
…error

report error from train.xyz
  • Loading branch information
brucefan1983 authored Aug 15, 2024
2 parents 0999cd4 + d06f53b commit c0cf55f
Showing 1 changed file with 45 additions and 31 deletions.
76 changes: 45 additions & 31 deletions src/main_nep/structure.cu
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ static void read_force(
const int force_offset,
std::ifstream& input,
const Parameters& para,
Structure& structure)
Structure& structure,
std::string& xyz_filename,
int& line_number)
{
structure.type.resize(structure.num_atom);
structure.x.resize(structure.num_atom);
Expand All @@ -99,17 +101,19 @@ static void read_force(

for (int na = 0; na < structure.num_atom; ++na) {
std::vector<std::string> tokens = get_tokens(input);
line_number++;

if (tokens.size() != num_columns) {
PRINT_INPUT_ERROR("Number of items for an atom line mismatches properties.");
}
std::string atom_symbol(tokens[0 + species_offset]);
structure.x[na] = get_float_from_token(tokens[0 + pos_offset], __FILE__, __LINE__);
structure.y[na] = get_float_from_token(tokens[1 + pos_offset], __FILE__, __LINE__);
structure.z[na] = get_float_from_token(tokens[2 + pos_offset], __FILE__, __LINE__);
structure.x[na] = get_float_from_token(tokens[0 + pos_offset], xyz_filename.c_str(), line_number);
structure.y[na] = get_float_from_token(tokens[1 + pos_offset], xyz_filename.c_str(), line_number);
structure.z[na] = get_float_from_token(tokens[2 + pos_offset], xyz_filename.c_str(), line_number);
if (num_columns > 4) {
structure.fx[na] = get_float_from_token(tokens[0 + force_offset], __FILE__, __LINE__);
structure.fy[na] = get_float_from_token(tokens[1 + force_offset], __FILE__, __LINE__);
structure.fz[na] = get_float_from_token(tokens[2 + force_offset], __FILE__, __LINE__);
structure.fx[na] = get_float_from_token(tokens[0 + force_offset], xyz_filename.c_str(), line_number);
structure.fy[na] = get_float_from_token(tokens[1 + force_offset], xyz_filename.c_str(), line_number);
structure.fz[na] = get_float_from_token(tokens[2 + force_offset], xyz_filename.c_str(), line_number);
}

bool is_allowed_element = false;
Expand All @@ -125,9 +129,16 @@ static void read_force(
}
}

static void read_one_structure(const Parameters& para, std::ifstream& input, Structure& structure)
static void read_one_structure(
const Parameters& para,
std::ifstream& input,
Structure& structure,
std::string& xyz_filename,
int& line_number)
{
std::vector<std::string> tokens = get_tokens_without_unwanted_spaces(input);
line_number++;

for (auto& token : tokens) {
std::transform(
token.begin(), token.end(), token.begin(), [](unsigned char c) { return std::tolower(c); });
Expand All @@ -143,7 +154,7 @@ static void read_one_structure(const Parameters& para, std::ifstream& input, Str
if (token.substr(0, energy_string.length()) == energy_string) {
has_energy_in_exyz = true;
structure.energy = get_float_from_token(
token.substr(energy_string.length(), token.length()), __FILE__, __LINE__);
token.substr(energy_string.length(), token.length()), xyz_filename.c_str(), line_number);
structure.energy /= structure.num_atom;
}
}
Expand All @@ -157,7 +168,7 @@ static void read_one_structure(const Parameters& para, std::ifstream& input, Str
if (token.substr(0, temperature_string.length()) == temperature_string) {
structure.has_temperature = true;
structure.temperature = get_float_from_token(
token.substr(temperature_string.length(), token.length()), __FILE__, __LINE__);
token.substr(temperature_string.length(), token.length()), xyz_filename.c_str(), line_number);
}
}
if (para.train_mode == 3 && !structure.has_temperature) {
Expand All @@ -172,7 +183,7 @@ static void read_one_structure(const Parameters& para, std::ifstream& input, Str
const std::string weight_string = "weight=";
if (token.substr(0, weight_string.length()) == weight_string) {
structure.weight = get_float_from_token(
token.substr(weight_string.length(), token.length()), __FILE__, __LINE__);
token.substr(weight_string.length(), token.length()), xyz_filename.c_str(), line_number);
if (structure.weight <= 0.0f || structure.weight > 100.0f) {
PRINT_INPUT_ERROR("Configuration weight should > 0 and <= 100.");
}
Expand All @@ -190,8 +201,7 @@ static void read_one_structure(const Parameters& para, std::ifstream& input, Str
tokens[n + m].substr(
(m == 0) ? (lattice_string.length() + 1) : 0,
(m == 8) ? (tokens[n + m].length() - 1) : tokens[n + m].length()),
__FILE__,
__LINE__);
xyz_filename.c_str(), line_number);
}
change_box(para, structure);
}
Expand All @@ -211,8 +221,7 @@ static void read_one_structure(const Parameters& para, std::ifstream& input, Str
tokens[n + m].substr(
(m == 0) ? (virial_string.length() + 1) : 0,
(m == 8) ? (tokens[n + m].length() - 1) : tokens[n + m].length()),
__FILE__,
__LINE__);
xyz_filename.c_str(), line_number);
structure.virial[reduced_index[m]] /= structure.num_atom;
}
}
Expand All @@ -231,8 +240,7 @@ static void read_one_structure(const Parameters& para, std::ifstream& input, Str
tokens[n + m].substr(
(m == 0) ? (stress_string.length() + 1) : 0,
(m == 8) ? (tokens[n + m].length() - 1) : tokens[n + m].length()),
__FILE__,
__LINE__);
xyz_filename.c_str(), line_number);
virials_from_stress[reduced_index[m]] *= -volume / structure.num_atom;
}
}
Expand Down Expand Up @@ -279,8 +287,7 @@ static void read_one_structure(const Parameters& para, std::ifstream& input, Str
tokens[n + m].substr(
(m == 0) ? (dipole_string.length() + 1) : 0,
(m == 2) ? (tokens[n + m].length() - 1) : tokens[n + m].length()),
__FILE__,
__LINE__);
xyz_filename.c_str(), line_number);
structure.virial[m] /= structure.num_atom;
}
}
Expand Down Expand Up @@ -309,8 +316,7 @@ static void read_one_structure(const Parameters& para, std::ifstream& input, Str
tokens[n + m].substr(
(m == 0) ? (pol_string.length() + 1) : 0,
(m == 8) ? (tokens[n + m].length() - 1) : tokens[n + m].length()),
__FILE__,
__LINE__);
xyz_filename.c_str(), line_number);
structure.virial[reduced_index[m]] /= structure.num_atom;
}
}
Expand Down Expand Up @@ -365,39 +371,46 @@ static void read_one_structure(const Parameters& para, std::ifstream& input, Str
}
for (int k = 0; k < sub_tokens.size() / 3; ++k) {
if (k < species_position) {
species_offset += get_int_from_token(sub_tokens[k * 3 + 2], __FILE__, __LINE__);
species_offset += get_int_from_token(sub_tokens[k * 3 + 2], xyz_filename.c_str(), line_number);
}
if (k < pos_position) {
pos_offset += get_int_from_token(sub_tokens[k * 3 + 2], __FILE__, __LINE__);
pos_offset += get_int_from_token(sub_tokens[k * 3 + 2], xyz_filename.c_str(), line_number);
}
if (k < force_position) {
force_offset += get_int_from_token(sub_tokens[k * 3 + 2], __FILE__, __LINE__);
force_offset += get_int_from_token(sub_tokens[k * 3 + 2], xyz_filename.c_str(), line_number);
}
num_columns += get_int_from_token(sub_tokens[k * 3 + 2], __FILE__, __LINE__);
num_columns += get_int_from_token(sub_tokens[k * 3 + 2], xyz_filename.c_str(), line_number);
}
}
}

read_force(num_columns, species_offset, pos_offset, force_offset, input, para, structure);
read_force(num_columns, species_offset, pos_offset, force_offset, input, para, structure, xyz_filename, line_number);
}

static void
read_exyz(const Parameters& para, std::ifstream& input, std::vector<Structure>& structures)
read_exyz(
const Parameters& para,
std::ifstream& input,
std::vector<Structure>& structures,
std::string& xyz_filename)
{
int line_number = 0;
int Nc = 0;
while (true) {
std::vector<std::string> tokens = get_tokens(input);
line_number++;

if (tokens.size() == 0) {
break;
} else if (tokens.size() > 1) {
PRINT_INPUT_ERROR("The first line for each frame should have one value.");
}
Structure structure;
structure.num_atom = get_int_from_token(tokens[0], __FILE__, __LINE__);
structure.num_atom = get_int_from_token(tokens[0], xyz_filename.c_str(), line_number);
if (structure.num_atom < 1) {
PRINT_INPUT_ERROR("Number of atoms for each frame should >= 1.");
}
read_one_structure(para, input, structure);
read_one_structure(para, input, structure, xyz_filename, line_number);
structures.emplace_back(structure);
++Nc;
}
Expand Down Expand Up @@ -545,9 +558,10 @@ bool read_structures(bool is_train, Parameters& para, std::vector<Structure>& st
}
} else {
print_line_1();
is_train ? printf("Started reading train.xyz.\n") : printf("Started reading test.xyz.\n");
std::string xyz_filename = is_train ? "train.xyz" : "test.xyz";
std::cout << "Started reading " << xyz_filename << std::endl;
print_line_2();
read_exyz(para, input, structures);
read_exyz(para, input, structures, xyz_filename);
input.close();
}

Expand Down

0 comments on commit c0cf55f

Please sign in to comment.